forked from mmastrac/diffgemma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chat_template.py
More file actions
67 lines (55 loc) · 1.66 KB
/
Copy pathtest_chat_template.py
File metadata and controls
67 lines (55 loc) · 1.66 KB
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
"""Chat template parity: Rust formatter vs HuggingFace apply_chat_template."""
from __future__ import annotations
import json
import shutil
import subprocess
from pathlib import Path
import pytest
from transformers import AutoTokenizer
ROOT = Path(__file__).resolve().parents[2]
MODEL_DIR = ROOT / "model" / "transformer"
BINARY = ROOT / "target" / "release" / "diffgemma"
def _cargo_release_binary() -> Path | None:
if shutil.which("cargo") is None:
return None
if not BINARY.exists():
subprocess.run(
["cargo", "build", "--release"],
cwd=ROOT,
check=True,
)
return BINARY
@pytest.fixture(scope="module")
def hf_tokenizer():
if not (MODEL_DIR / "tokenizer.json").exists():
pytest.skip("model/transformer/tokenizer.json not present")
return AutoTokenizer.from_pretrained(MODEL_DIR)
@pytest.mark.parametrize(
"text",
[
"Why is the sky blue?",
"Hello",
"Tell me about DiffusionGemma.",
],
)
def test_rust_chat_tokenize_matches_hf(text: str, hf_tokenizer) -> None:
binary = _cargo_release_binary()
if binary is None:
pytest.skip("cargo not available")
messages = [{"role": "user", "content": text}]
expected = hf_tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_dict=True,
)["input_ids"]
proc = subprocess.run(
[str(binary), "tokenize", text],
cwd=ROOT,
check=True,
capture_output=True,
text=True,
)
payload = json.loads(proc.stdout)
assert payload["chat_template"] is True
assert payload["ids"] == expected