File size: 5,063 Bytes
ad01ed3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | #!/usr/bin/env python3
"""
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]] # byte offsets
description: str
# Test vectors for each atom shape
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]))
|