Repository navigation
fix(calibration): render a messages row with its tools and documents (#4858) - #4927
Merged
Merged
Conversation
…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.
This was referenced Nov 17, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The standard HF tool-calling calibration row is
{"messages": [...], "tools": [...]}._tokenize_messages_valuerendered onlymessagesand silently dropped every other chat-template input, so the tool-schema block the model sees at inference never reached the Hessian. Closes #4858.apply_chat_template(tools=...))tool_callsandtoolturns were still rendered, so the tool-call turns were calibrated without the definitions they refer to.Fix
_tokenize_messages_valuenow takes the whole row and forwardstoolsanddocumentstoapply_chat_template, so the rendered text matches what inference sends.documentscovers 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: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 checkon 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.