Skip to content

fix(calibration): render a messages row with its tools and documents (#4858) - #4927

Merged
Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/4858-chat-template-tools
Nov 17, 2026
Merged

Qubitium merged 1 commit into
ModelCloud:mainfrom
Qubitium:fix/4858-chat-template-tools

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

The standard HF tool-calling calibration row is {"messages": [...], "tools": [...]}. _tokenize_messages_value rendered only messages and silently dropped every other chat-template input, so the tool-schema block the model sees at inference never reached the Hessian. Closes #4858.

model before after (matches apply_chat_template(tools=...))
Qwen2.5-0.5B-Instruct 101 tokens 236
Llama-3.2-1B-Instruct 112 tokens 251

tool_calls and tool turns were still rendered, so the tool-call turns were calibrated without the definitions they refer to.

Fix

_tokenize_messages_value now takes the whole row and forwards tools and documents to apply_chat_template, so the rendered text matches what inference sends. documents covers RAG templates the same way.

A tokenizer whose template does not use those inputs raises inside Jinja. That row is then rendered exactly as before and one warning says the schema was dropped — a calibration run is not aborted over a template that has no tool support.

Tests

New tests/test_calibration_messages_tools_4858.py, four of five red on the parent commit:

python -m pytest tests/test_calibration_messages_tools_4858.py -q
# 5 passed
# parent: test_tools_are_forwarded_to_the_chat_template,
#         test_documents_are_forwarded_to_the_chat_template,
#         test_a_template_that_rejects_tools_keeps_the_row_and_warns and
#         test_qwen2_5_renders_the_tool_schema fail

The last case uses the real Qwen2.5 tokenizer and asserts the calibration tokens are byte-for-byte apply_chat_template(messages, tools=...).

Related calibration suites (CPU, this host):

python -m pytest tests/test_calibration_empty_text_4306.py tests/test_calibration_data_device.py \
    tests/test_calibration_attention_sinks.py tests/test_calibration_messages_tools_4858.py -q
# 58 passed, 4 skipped, 2 failed -- the same 2 in test_calibration_data_device.py fail on
# the unmodified parent commit here (they need a visible GPU)

ruff check on the module and the new test: All checks passed.

Logic Flow Documentation

Not applicable: one call site now forwards the row's template inputs.

Model Support / Module Plan

Not applicable: no definition or plan change.

…odelCloud#4858)

The standard HF tool-calling calibration row is
`{"messages": [...], "tools": [...]}`. `_tokenize_messages_value` rendered only
`messages` and dropped every other chat-template input, so the tool schema block
the model sees at inference never reached the Hessian. On Qwen2.5-0.5B a
one-tool row produced 101 tokens instead of 236 (Llama-3.2-1B: 112 vs 251), and
nothing was logged.

The row's `tools` and `documents` are now forwarded to `apply_chat_template`, so
the rendered text matches what inference sends. A tokenizer whose template does
not use those inputs raises inside Jinja; that row is then rendered as before
and a single warning says the schema was dropped, instead of failing the whole
calibration run.

Regression tests (red on the parent commit): `tests/test_calibration_messages_tools_4858.py`
pins the forwarding for `tools` and `documents`, the unchanged no-tools row, the
rejecting-template fallback with its warning, and a real Qwen2.5 render that must
equal `apply_chat_template(messages, tools=...)` token for token.
@Qubitium
Qubitium merged commit cb99385 into ModelCloud:main Nov 17, 2026
1 check passed
sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
…odelCloud#4934) (ModelCloud#4997)

* fix(calibration): render Hub `tools` columns the way inference does (ModelCloud#4934)

ModelCloud#4927 forwarded a row's `tools`/`documents` to `apply_chat_template` exactly as
stored, which is not the layout an HF dataset yields:

* a JSON-string `tools` column (Arrow cannot hold a heterogeneous struct
  column) is rejected by the template ("Tools should either be a JSON schema,
  ..."), and the row was then rendered without any tools at all;
* an Arrow-unified struct column gives every row's JSON schema the other rows'
  parameter names as `null`, so the rendered block differs from what the model
  sees at inference;
* a one-tool payload stored as one object is not iterable for the templates,
  which take a list of schemas;
* `dataset[:]` splits only the four primary columns, so `tools`/`documents`
  were dropped before they could reach the template at all.

`_normalize_chat_template_payload()` now decodes an object/array JSON string,
drops the `null` mapping entries Arrow injects, and presents a one-tool payload
as the list the templates iterate. A plain (non-JSON) string is left alone, so
a `documents` text value is untouched. `_columnar_mapping_examples()` splits the
chat-template columns next to `messages`.

Red on unmodified main (test copied into an origin/main worktree): 5 failed,
6 passed. Green: 11 passed, including the pre-existing real-tokenizer node,
which now also finds the cached AWQ repo's tokenizer instead of skipping.

* test(calibration): port the HF tool-layout matrix and share one normalizer

Folds the extra coverage from ModelCloud#5004 (`tests/test_calibration_tools_formats_4934.py`)
into the ModelCloud#4934 fix so a single PR carries both:

* the helper is renamed to `_normalize_template_input_value()` (the name ModelCloud#5004
  introduced) with `_normalize_template_input_payload()` on top for the
  one-tool `tools` list wrapping, so both test files exercise the same walk;
* the JSON decode now runs `json.loads` for any string and only recurses into
  a container, so a document that reads as a JSON scalar (`"42"`, `"true"`)
  stays text and a real JSON-schema `null` (`{"type": "null"}`) survives;
* the ported tokenizer helper falls back to the cached
  `Qwen/Qwen2.5-0.5B-Instruct-AWQ` repo (identical tokenizer files) instead of
  skipping when only the quantized repo is cached offline;
* both regression files run in the pre-merge `AWQ CPU (unit)` suite.

Red on unmodified main with both files copied in: 9 failed, 7 passed.
Green here: 11 + 5 nodes, 39 passed together with the docs contract.

* test(calibration): raise the tokenizer skip exception explicitly (ModelCloud#4934)

A bare `pytest.skip(...)` after the offline-repo loop left that path as an
implicit fall-through return, mixing explicit and implicit returns in
`_offline_qwen_tokenizer()`. Raise the skip exception instead; the test still
skips with the same message.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Calibration messages rows: the tools column is silently dropped; tool schemas never reach the Hessian (101 of 236 intended tokens on Qwen2.5)

1 participant