File size: 1,172 Bytes
5f02c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from hades.tokenizer.export import encode_fast, export_hf, load_fast
from tests.conftest import FIXTURE_LINES


def test_parity_on_fixture(trained_small, tmp_path):
    export_hf(trained_small, tmp_path / "t.json")
    fast = load_fast(tmp_path / "t.json")
    texts = FIXTURE_LINES + ["<|eos|> literal", "", "  ", "é漢字 CVE-2024-3094", "\x00\x1b[31m", "á ①②"]
    assert encode_fast(fast, texts) == [trained_small.encode(t) for t in texts]


def test_fast_decode_matches(trained_small, tmp_path):
    export_hf(trained_small, tmp_path / "t.json")
    fast = load_fast(tmp_path / "t.json")
    ids = trained_small.encode(FIXTURE_LINES[0])
    assert fast.decode(ids) == FIXTURE_LINES[0]


def test_parity_random_unicode(trained_small, tmp_path):
    import random

    export_hf(trained_small, tmp_path / "t.json")
    fast = load_fast(tmp_path / "t.json")
    rng = random.Random(1)
    pool = "ab Z9 \t\n\r   é́漢①٣_-./[]:;<>|" + "".join(chr(c) for c in range(0x80, 0x100))
    texts = ["".join(rng.choice(pool) for _ in range(rng.randint(0, 40))) for _ in range(500)]
    assert encode_fast(fast, texts) == [trained_small.encode(t) for t in texts]