|
17 | 17 | sys.path.insert(1, str(Path(__file__).parent / "gguf-py" / "gguf")) |
18 | 18 | import gguf |
19 | 19 |
|
20 | | - |
21 | | -def bytes_to_unicode(): |
22 | | - # ref: https://github.com/openai/gpt-2/blob/master/src/encoder.py |
23 | | - """ |
24 | | - Returns list of utf-8 byte and a corresponding list of unicode strings. |
25 | | - The reversible bpe codes work on unicode strings. |
26 | | - This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. |
27 | | - When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. |
28 | | - This is a significant percentage of your normal, say, 32K bpe vocab. |
29 | | - To avoid that, we want lookup tables between utf-8 bytes and unicode strings. |
30 | | - And avoids mapping to whitespace/control characters the bpe code barfs on. |
31 | | - """ |
32 | | - bs = ( |
33 | | - list(range(ord("!"), ord("~") + 1)) |
34 | | - + list(range(ord("¡"), ord("¬") + 1)) |
35 | | - + list(range(ord("®"), ord("ÿ") + 1)) |
36 | | - ) |
37 | | - cs = bs[:] |
38 | | - n = 0 |
39 | | - for b in range(2**8): |
40 | | - if b not in bs: |
41 | | - bs.append(b) |
42 | | - cs.append(2**8 + n) |
43 | | - n += 1 |
44 | | - return dict(zip(bs, (chr(n) for n in cs))) |
45 | | - |
46 | | - |
47 | 20 | def count_model_parts(dir_model: Path) -> int: |
48 | 21 | num_parts = 0 |
49 | 22 | for filename in os.listdir(dir_model): |
@@ -153,53 +126,25 @@ def parse_args() -> argparse.Namespace: |
153 | 126 | scores: list[float] = [] |
154 | 127 | toktypes: list[int] = [] |
155 | 128 |
|
156 | | -tokenizer_json_file = dir_model / "tokenizer.json" |
157 | | -if not tokenizer_json_file.is_file(): |
158 | | - print(f"Error: Missing {tokenizer_json_file}", file=sys.stderr) |
159 | | - sys.exit(1) |
160 | | - |
161 | 129 | # gpt2 tokenizer |
162 | 130 | gguf_writer.add_tokenizer_model("gpt2") |
163 | 131 |
|
164 | | -with open(tokenizer_json_file, "r", encoding="utf-8") as f: |
165 | | - tokenizer_json = json.load(f) |
166 | | - |
167 | 132 | print("gguf: get gpt2 tokenizer vocab") |
168 | 133 |
|
| 134 | +# ref: https://github.com/cmp-nct/ggllm.cpp/blob/master/falcon_convert.py |
| 135 | +tokenizer = AutoTokenizer.from_pretrained(dir_model) |
| 136 | + |
169 | 137 | # The number of tokens in tokenizer.json can differ from the expected vocab size. |
170 | 138 | # This causes downstream issues with mismatched tensor sizes when running the inference |
171 | | -vocab_size = ( |
172 | | - hparams["vocab_size"] |
173 | | - if "vocab_size" in hparams |
174 | | - else len(tokenizer_json["model"]["vocab"]) |
175 | | -) |
176 | | - |
177 | | -tokenizer = AutoTokenizer.from_pretrained(dir_model, trust_remote_code=True) |
| 139 | +vocab_size = hparams.get("vocab_size", len(tokenizer.vocab)) |
| 140 | +assert max(tokenizer.vocab.values()) < vocab_size |
178 | 141 |
|
179 | 142 | reverse_vocab = {id: encoded_tok for encoded_tok, id in tokenizer.vocab.items()} |
180 | | -byte_encoder = bytes_to_unicode() |
181 | | -byte_decoder = {v: k for k, v in byte_encoder.items()} |
182 | 143 |
|
183 | 144 | for i in range(vocab_size): |
184 | | - if i in reverse_vocab: |
185 | | - text = reverse_vocab[i] |
186 | | - try: |
187 | | - text = bytearray([byte_decoder[c] for c in reverse_vocab[i]]) |
188 | | - except KeyError: |
189 | | - text = bytearray() |
190 | | - for c in reverse_vocab[i]: |
191 | | - if ord(c) < 256: # single byte character |
192 | | - text.append(byte_decoder[ord(c)]) |
193 | | - else: # multibyte special token character |
194 | | - text.extend(c.encode("utf-8")) |
195 | | - else: |
196 | | - print(f"Key {i} not in tokenizer vocabulary. Padding with an arbitrary token.") |
197 | | - pad_token = f"[PAD{i}]".encode("utf8") |
198 | | - text = bytearray(pad_token) |
199 | | - |
200 | | - tokens.append(text) |
201 | | - scores.append(0.0) # dymmy |
202 | | - toktypes.append(gguf.TokenType.NORMAL) # dummy |
| 145 | + tokens.append(reverse_vocab[i] if i in reverse_vocab else f"[PAD{i}]") |
| 146 | + scores.append(0.0) # dummy |
| 147 | + toktypes.append(gguf.TokenType.NORMAL) |
203 | 148 |
|
204 | 149 | gguf_writer.add_token_list(tokens) |
205 | 150 | gguf_writer.add_token_scores(scores) |
|
0 commit comments