Initialize both tokenizers with identical configuration.
(self)
| 189 | return special_tokens |
| 190 | |
| 191 | def setup_tokenizers(self): |
| 192 | """Initialize both tokenizers with identical configuration.""" |
| 193 | print("Setting up tokenizers...") |
| 194 | |
| 195 | # Load configuration based on tokenizer type |
| 196 | if self.tokenizer_type == "llama": |
| 197 | print("Using Llama 4 configuration...") |
| 198 | pattern, vocab, special_tokens = self.load_llama_config() |
| 199 | elif self.tokenizer_type == "mistral": |
| 200 | print("Using Mistral Tekken 7 configuration...") |
| 201 | pattern, vocab, special_tokens = self.load_mistral_config() |
| 202 | else: |
| 203 | raise ValueError(f"Unsupported tokenizer type: {self.tokenizer_type}") |
| 204 | |
| 205 | # Convert TokenDagger format to TikToken format |
| 206 | mergeable_ranks = {} |
| 207 | for item in vocab: |
| 208 | if isinstance(item["token_bytes"], list): |
| 209 | token_bytes = bytes(item["token_bytes"]) |
| 210 | else: |
| 211 | token_bytes = item["token_bytes"] |
| 212 | mergeable_ranks[token_bytes] = item["rank"] |
| 213 | |
| 214 | # Add special tokens to mergeable_ranks |
| 215 | for token_str, rank in special_tokens.items(): |
| 216 | mergeable_ranks[token_str.encode('utf-8')] = rank |
| 217 | |
| 218 | tokenizer_name = f"{self.tokenizer_type}_perf_test" |
| 219 | |
| 220 | # Initialize TokenDagger using TikToken-compatible API |
| 221 | self.tokendagger_tokenizer = tokendagger.Encoding( |
| 222 | name=tokenizer_name, |
| 223 | pat_str=pattern, |
| 224 | mergeable_ranks=mergeable_ranks, |
| 225 | special_tokens=special_tokens |
| 226 | ) |
| 227 | |
| 228 | # Initialize TikToken with the same configuration |
| 229 | self.tiktoken_tokenizer = tiktoken.Encoding( |
| 230 | name=tokenizer_name, |
| 231 | pat_str=pattern, |
| 232 | mergeable_ranks=mergeable_ranks, |
| 233 | special_tokens=special_tokens |
| 234 | ) |
| 235 | |
| 236 | print(f"✓ TokenDagger tokenizer initialized ({self.tokenizer_type})") |
| 237 | print(f"✓ TikToken tokenizer initialized ({self.tokenizer_type})") |
| 238 | |
| 239 | def generate_test_texts(self) -> Dict[str, List[str]]: |
| 240 | """Generate comprehensive test corpus with various edge cases.""" |
no test coverage detected