| |
| """ |
| Tokenization Parity Harness - Validates fast_split atoms against HF reference. |
| """ |
|
|
| import json |
| from dataclasses import dataclass |
| from typing import List, Tuple |
|
|
| @dataclass |
| class TestCase: |
| input: str |
| expected_spans: List[Tuple[int, int]] |
| description: str |
|
|
| |
| TEST_VECTORS = { |
| "A1_split": { |
| "WhitespaceSplit": [ |
| TestCase("Hello world", [(0,5), (5,11)], "simple split"), |
| TestCase("a b", [(0,1), (1,4)], "multiple spaces"), |
| ], |
| "Metaspace": [ |
| TestCase("Hello", [(0,5)], "no leading space"), |
| TestCase(" Hello", [(0,6)], "leading space becomes \u2581"), |
| ], |
| "Digits": [ |
| TestCase("abc123def", [(0,3), (3,6), (6,9)], "digits contiguous"), |
| TestCase("a1b2c3", [(0,1), (1,2), (2,3), (3,4), (4,5), (5,6)], "single digits"), |
| ], |
| }, |
| |
| "A2_class_runs": { |
| "BertPreTokenizer": [ |
| TestCase("Hello, world!", [(0,5), (5,6), (6,7), (7,12), (12,13)], "bert-style"), |
| TestCase("caf\u00e9", [(0,5)], "unicode preserved"), |
| ], |
| "Whitespace": [ |
| TestCase("a\u00d7b c!d", [(0,1), (1,4), (4,8)], "word|symbol runs"), |
| ], |
| }, |
| |
| "A3_cl100k": { |
| "cl100k": [ |
| TestCase("don't", [(0,3), (3,5)], "apostrophe-t contraction"), |
| TestCase("we're", [(0,2), (2,5)], "apostrophe-re contraction"), |
| TestCase("Hello", [(0,5)], "pure letters"), |
| TestCase("_Hello", [(0,6)], "underscore prefix"), |
| TestCase("a1234", [(0,1), (1,4), (4,5)], "1-3 number cap"), |
| TestCase("a, b", [(0,1), (1,2), (2,4)], "punctuation split"), |
| TestCase(" hi", [(0,1), (1,4)], "ws split"), |
| ], |
| }, |
| |
| "A4_deepseek": { |
| "deepseek": [ |
| TestCase("abc123", [(0,3), (3,6)], "letters | digits"), |
| TestCase("abc\u4e2ddef", [(0,3), (3,6), (6,9)], "letters | CJK | letters"), |
| TestCase("abc\u4e2d\u4e2cdef", [(0,3), (3,9), (9,12)], "multi-CJK run"), |
| TestCase("_abc", [(0,4)], "punct + letters"), |
| TestCase("hello world", [(0,5), (5,11)], "words with ws prefix"), |
| TestCase("!!!", [(0,3)], "punct run"), |
| ], |
| }, |
| |
| "A5_byte_level": { |
| "byte_level": [ |
| TestCase("don't", [(0,3), (3,5)], "contraction"), |
| TestCase("12345", [(0,5)], "unbounded numbers"), |
| TestCase("Hello world", [(0,5), (5,11)], "words"), |
| ], |
| "byte_level_regex": [ |
| TestCase("don't split", [(0,3), (3,5), (5,11)], "contractions + words"), |
| ], |
| }, |
| |
| "A6_script_run": { |
| "unicode_scripts": [ |
| TestCase("Hello\u0645\u0631\u062d\u0628\u0627", [(0,5), (5,15)], "Latin|Arabic"), |
| TestCase("Hello\u4e16\u754c", [(0,5), (5,11)], "Latin|Han"), |
| TestCase("Hello \u4e16\u754c", [(0,6), (6,12)], "Latin+space|Han"), |
| ], |
| }, |
| |
| "null": { |
| "sentencepiece": [ |
| TestCase("This is a test.", [(0,15)], "SPM handles internally"), |
| ], |
| }, |
| } |
|
|
| def generate_rust_test_suite(output_path: str = "/Users/arthurzucker/Work/tokenizers/tokenizers/fast_split/tests/test_gen_atom_parity.rs"): |
| lines = [ |
| "// GENERATED FILE - Do not edit manually", "", |
| "use crate::{classify, fsm, Atom, Atoms, mask};", |
| "use crate::fsm::*;", |
| "", |
| "/// Byte-exact parity tests for each atom shape.", |
| "#[cfg(test)]", "mod atom_parity {", " use super::*;", |
| "", |
| ] |
| |
| for atom_shape, configs in TEST_VECTORS.items(): |
| for config_name, tests in configs.items(): |
| fn_name = f"test_{atom_shape.lower()}_{config_name.lower()}" |
| lines.append(f" #[test]") |
| lines.append(f" fn {fn_name}() {{") |
| |
| for i, tc in enumerate(tests): |
| text = tc.input.replace('\\', '\\\\').replace('"', '\\"') |
| spans = ", ".join(f"({s},{e})" for s,e in tc.expected_spans) |
| |
| lines.append(f" // {tc.description}") |
| lines.append(f" let text{i} = b\"{text}\";") |
| lines.append(f" let expected{i} = vec![{spans}];") |
| lines.append(f" // TODO: call actual fast_split tokenizer") |
| lines.append(f" // assert_eq!(tokenize(text{i}), expected{i});") |
| lines.append("") |
| |
| lines.append(" }") |
| lines.append("") |
| |
| lines.extend([ |
| "}", |
| ]) |
| |
| code = "\n".join(lines) |
| |
| with open(output_path, "w") as f: |
| f.write(code) |
| |
| print(f"Generated Rust test suite: {output_path}") |
| print(f"\nTotal test cases: {sum(len(v) for configs in TEST_VECTORS.values() for v in configs.values())}") |
| return code |
|
|
| if __name__ == "__main__": |
| code = generate_rust_test_suite() |
| print("First 80 lines:") |
| print("\n".join(code.split("\n")[:80])) |
|
|