(self)
| 2194 | model_arch = gguf.MODEL_ARCH.XVERSE |
| 2195 | |
| 2196 | def set_vocab(self): |
| 2197 | assert (self.dir_model / "tokenizer.json").is_file() |
| 2198 | dir_model = self.dir_model |
| 2199 | hparams = self.hparams |
| 2200 | |
| 2201 | tokens: list[bytes] = [] |
| 2202 | toktypes: list[int] = [] |
| 2203 | |
| 2204 | from transformers import AutoTokenizer |
| 2205 | tokenizer = AutoTokenizer.from_pretrained(dir_model) |
| 2206 | vocab_size = hparams.get("vocab_size", len(tokenizer.vocab)) |
| 2207 | # Since we are checking the maximum index, we need to ensure it's strictly less than vocab_size, |
| 2208 | # because vocab_size is the count of items, and indexes start at 0. |
| 2209 | max_vocab_index = max(tokenizer.get_vocab().values()) |
| 2210 | if max_vocab_index >= vocab_size: |
| 2211 | raise ValueError("Vocabulary size exceeds expected maximum size.") |
| 2212 | |
| 2213 | reverse_vocab: dict[int, str] = {id_: encoded_tok for encoded_tok, id_ in tokenizer.vocab.items()} |
| 2214 | added_vocab = tokenizer.get_added_vocab() |
| 2215 | |
| 2216 | for token_id in range(vocab_size): |
| 2217 | token_text = reverse_vocab[token_id].encode('utf-8') |
| 2218 | # replace "\x00" to string with length > 0 |
| 2219 | if token_text == b"\x00": |
| 2220 | toktype = gguf.TokenType.BYTE # special |
| 2221 | token_text = f"<{token_text}>".encode('utf-8') |
| 2222 | elif re.fullmatch(br"<0x[0-9A-Fa-f]{2}>", token_text): |
| 2223 | toktype = gguf.TokenType.BYTE # special |
| 2224 | elif reverse_vocab[token_id] in added_vocab: |
| 2225 | if tokenizer.added_tokens_decoder[token_id].special: |
| 2226 | toktype = gguf.TokenType.CONTROL |
| 2227 | else: |
| 2228 | toktype = gguf.TokenType.USER_DEFINED |
| 2229 | else: |
| 2230 | toktype = gguf.TokenType.NORMAL |
| 2231 | |
| 2232 | tokens.append(token_text) |
| 2233 | toktypes.append(toktype) |
| 2234 | |
| 2235 | self.gguf_writer.add_tokenizer_model("llama") |
| 2236 | self.gguf_writer.add_tokenizer_pre("default") |
| 2237 | self.gguf_writer.add_token_list(tokens) |
| 2238 | self.gguf_writer.add_token_types(toktypes) |
| 2239 | |
| 2240 | special_vocab = gguf.SpecialVocab(dir_model, n_vocab=len(tokens)) |
| 2241 | special_vocab.add_to_gguf(self.gguf_writer) |
| 2242 | |
| 2243 | def set_gguf_parameters(self): |
| 2244 | super().set_gguf_parameters() |
nothing calls this directly
no test coverage detected