| """Byte-equality check of tools/glimmer_fmt.py against chat_template.jinja. |
| |
| python tools/test_glimmer_fmt.py --model-dir /workspace/gguf/ref/meta |
| |
| Renders every case through transformers' jinja sandbox and through the port and |
| asserts the two strings are identical. If this fails, the calibration markup is |
| wrong and the build must not run. |
| """ |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import sys |
|
|
| import glimmer_fmt as G |
|
|
| DATE = "2026-08-10" |
| CUTOFF = "2026-01-04" |
|
|
| WEATHER = { |
| "type": "function", |
| "function": { |
| "name": "weather.get_forecast", |
| "description": "Get the weather forecast for a location.", |
| "parameters": { |
| "type": "object", |
| "properties": { |
| "city": {"type": "string", "description": "City name"}, |
| "days": {"type": "integer", "description": "How many days ahead"}, |
| }, |
| "required": ["city"], |
| }, |
| }, |
| } |
| SHELL = { |
| "type": "function", |
| "function": { |
| "name": "shell.run", |
| "description": "Run a shell command and return stdout/stderr.", |
| "parameters": { |
| "type": "object", |
| "properties": { |
| "cmd": {"type": "string"}, |
| "timeout_s": {"type": "integer"}, |
| "env": {"type": "object"}, |
| "cwd": {"type": "string"}, |
| "capture_stderr": {"type": "boolean"}, |
| }, |
| "required": ["cmd"], |
| }, |
| }, |
| } |
|
|
|
|
| def cases() -> list[dict]: |
| out = [] |
|
|
| |
| out.append(dict(name="plain", messages=[ |
| {"role": "user", "content": "Hi."}, |
| {"role": "assistant", "content": "Hello."}, |
| ])) |
|
|
| |
| for rs in G.REASONING_STRENGTHS: |
| out.append(dict(name=f"system-{rs}", reasoning_strength=rs, messages=[ |
| {"role": "system", "content": "You are a build engineer."}, |
| {"role": "user", "content": "Explain rpath."}, |
| {"role": "assistant", "reasoning_content": "rpath is a linker search path.", |
| "content": "It is a search path baked into the binary."}, |
| ])) |
|
|
| |
| out.append(dict(name="tool-1arg", tools=[WEATHER], messages=[ |
| {"role": "user", "content": "Weather in Kyoto?"}, |
| {"role": "assistant", "tool_calls": [ |
| {"id": "c1", "type": "function", "function": { |
| "name": "weather.get_forecast", "arguments": {"city": "Kyoto"}}}]}, |
| {"role": "tool", "name": "weather.get_forecast", "content": "18C, clear"}, |
| {"role": "assistant", "content": "18 degrees and clear."}, |
| ])) |
|
|
| |
| out.append(dict(name="tool-5arg-nested", tools=[SHELL], messages=[ |
| {"role": "user", "content": "Build it."}, |
| {"role": "assistant", "tool_calls": [ |
| {"id": "c2", "type": "function", "function": { |
| "name": "shell.run", |
| "arguments": { |
| "cmd": "cmake --build build -j", |
| "timeout_s": 900, |
| "env": {"CC": "clang", "FLAGS": ["-O2", "-g"], "NESTED": {"a": [1, 2]}}, |
| "cwd": None, |
| "capture_stderr": True, |
| }}}]}, |
| {"role": "tool", "name": "shell.run", "content": "ninja: build stopped."}, |
| ])) |
|
|
| |
| out.append(dict(name="tool-parallel", tools=[WEATHER, SHELL], messages=[ |
| {"role": "user", "content": "Both please."}, |
| {"role": "assistant", "tool_calls": [ |
| {"id": "a", "type": "function", "function": { |
| "name": "weather.get_forecast", "arguments": {"city": "Oslo", "days": 3}}}, |
| {"id": "b", "type": "function", "function": { |
| "name": "shell.run", "arguments": {"cmd": "uptime"}}}, |
| ]}, |
| ])) |
|
|
| |
| out.append(dict(name="tool-by-id", tools=[WEATHER], messages=[ |
| {"role": "user", "content": "?"}, |
| {"role": "assistant", "tool_calls": [ |
| {"id": "xyz", "type": "function", "function": { |
| "name": "weather.get_forecast", "arguments": {"city": "Lima"}}}]}, |
| {"role": "tool", "tool_call_id": "xyz", "content": "rain"}, |
| ])) |
|
|
| |
| out.append(dict(name="consecutive", messages=[ |
| {"role": "user", "content": "one"}, |
| {"role": "user", "content": "two"}, |
| {"role": "assistant", "content": "ok"}, |
| ])) |
|
|
| |
| out.append(dict(name="image", messages=[ |
| {"role": "user", "content": [ |
| {"type": "text", "text": "What is this? "}, |
| {"type": "image"}, |
| ]}, |
| {"role": "assistant", "content": "A plot."}, |
| ])) |
|
|
| |
| out.append(dict(name="genprompt", add_generation_prompt=True, messages=[ |
| {"role": "user", "content": "go"}, |
| ])) |
|
|
| |
| out.append(dict(name="recipient", messages=[ |
| {"role": "user", "content": "think"}, |
| {"role": "assistant", "recipient": "self", "content": "planning", "end_turn": False}, |
| {"role": "assistant", "content": "done"}, |
| ])) |
| return out |
|
|
|
|
| def main() -> int: |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--model-dir", required=True, |
| help="directory holding chat_template.jinja + tokenizer_config.json") |
| args = ap.parse_args() |
|
|
| from transformers import AutoTokenizer |
| tok = AutoTokenizer.from_pretrained(args.model_dir) |
|
|
| failures = 0 |
| for c in cases(): |
| kwargs = dict( |
| tools=c.get("tools"), |
| reasoning_strength=c.get("reasoning_strength", G.DEFAULT_REASONING), |
| knowledge_cutoff=CUTOFF, |
| current_date=DATE, |
| add_generation_prompt=c.get("add_generation_prompt", False), |
| ) |
| want = tok.apply_chat_template(c["messages"], tokenize=False, **kwargs) |
| got = G.render(c["messages"], **kwargs) |
| if want != got: |
| failures += 1 |
| print(f"FAIL {c['name']}") |
| for i, (a, b) in enumerate(zip(want, got)): |
| if a != b: |
| print(f" first difference at char {i}") |
| print(f" template: {want[max(0,i-60):i+60]!r}") |
| print(f" port : {got[max(0,i-60):i+60]!r}") |
| break |
| else: |
| print(f" length {len(want)} vs {len(got)}") |
| print(f" template tail: {want[len(got)-60:][:160]!r}") |
| print(f" port tail : {got[len(want)-60:][:160]!r}") |
| else: |
| print(f"ok {c['name']} ({len(got)} chars)") |
|
|
| print(f"\n{len(cases()) - failures}/{len(cases())} cases byte-identical") |
| return 1 if failures else 0 |
|
|
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |
|
|