Repository navigation
Description
Found on main @ 4f287eb, CPU (x86_64), transformers 5.22.1.
What is wrong
prepare_calibration_dataset (gptqmodel/utils/calibration.py:617-631, _tokenize_messages_value) renders a messages row with
templated = apply_fn(messages_value, tokenize=False, add_generation_prompt=False)and passes nothing else from the row. A tool-calling chat row in the standard Hugging Face layout, {"messages": [...], "tools": [...]} (the format TRL/SFT datasets and apply_chat_template(..., tools=...) use), loses its tools column without any log line. The chat template is rendered as if no tools were declared, so the tool schema block the model sees at inference is never in the calibration tokens. The tool_calls and tool turns are still rendered, so the tool-call turns are calibrated without the tool definitions they refer to. The same applies to the other row-level template inputs (documents for RAG templates).
On a typical one-tool row, more than half of the intended tokens never reach layer 0:
| model | tokens at layer 0 | apply_chat_template(messages, tools=tools) |
apply_chat_template(messages) |
|---|---|---|---|
| Qwen2.5-0.5B-Instruct | 101 | 236 | 101 |
| Llama-3.2-1B-Instruct | 112 | 251 | 112 |
Repro (CPU; layer-0 input captured by patching StageInputsCapture.cache_inputs)
from gptqmodel import GPTQModel, CalibrationConfig, GPTQConfig
m = GPTQModel.load("Qwen/Qwen2.5-0.5B-Instruct", GPTQConfig(bits=4, group_size=128), device="cpu")
tools = [{"type": "function", "function": {"name": "get_weather", "description": "Get the current weather for a city",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}}]
msgs = [
{"role": "system", "content": "You are a helpful assistant that answers weather questions precisely."},
{"role": "user", "content": "What is the weather in Paris today? Please be brief."},
{"role": "assistant", "content": "", "tool_calls": [{"type": "function", "function": {"name": "get_weather", "arguments": {"city": "Paris"}}}]},
{"role": "tool", "content": "{\"temp_c\": 18, \"sky\": \"cloudy\"}"},
{"role": "assistant", "content": "It is 18C and cloudy in Paris."},
]
# hook: capture the batch input_ids handed to the layer-0 input capture, then abort
m.quantize(CalibrationConfig(dataset=[{"messages": msgs, "tools": tools}], sort=None, min_length=0))Output:
layer0 tokens: 101 | HF with tools: 236 | HF without tools: 101
== with tools: False == without tools: True
tool name in layer0 text: True | 'Get the current weather' in text: False
(Llama-3.2-1B-Instruct: 112 vs 251, same pattern.)
Suggested fix
In _tokenize_messages_value, take the template inputs from the row, not only messages: pass tools=example.get("tools") (and documents=example.get("documents") when present) to apply_chat_template. Alternatively, reject rows that carry tools/documents with a clear error so the loss is not silent. The VLM normalize_chat_calibration_sample path (calibration.py:98-100) returns sample["messages"] and drops tools the same way.
Test that should have caught it
tests/test_calibration_messages_ids_precedence_4670.py / tests/test_calibration_special_tokens_4191.py cover messages rows but only with plain user/assistant turns. Add a case with a tools column that compares the prepared ids against tokenizer.apply_chat_template(messages, tools=tools).
Severity
Medium. Silent accuracy loss for anyone calibrating an agent/tool-use model on its own chat-format data: the tool schema block (often the largest part of an agent prompt) never reaches the Hessian, and nothing is logged.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Agent claim:
agent-root-4858-chat-toolsis working on this issue now.Started 2026-11-17T18:36:15Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.
claiming: forward the row's tools/documents (and other chat-template inputs) into apply_chat_template for messages calibration rows
fix(calibration): render a messages row with its tools and documents (#…
Qubitium commentedon Nov 17, 2026
Agent claim released — fixed and merged.
#4927 landed as
cb9938582onmain; the issue is closed.fix(calibration): keep `messages` conversations from numpy/pandas sli…
fix(calibration): drop Arrow's injected nulls from `messages` (#5082) (…