Repository navigation
fix(calibration): render Hub tools columns the way inference does (#4934) - #4997
Merged
Merged
Conversation
…4934) #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.
Qubitium
force-pushed
the
root/tools-normalize-4934
branch
from
November 17, 2026 21:35
f73a09d to
94b5cf9
Compare
…4934) #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. `_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. Red on unmodified main: 4 failed, 7 passed. Green: 11 passed.
Collaborator
Author
|
Rebased onto |
…lizer Folds the extra coverage from #5004 (`tests/test_calibration_tools_formats_4934.py`) into the #4934 fix so a single PR carries both: * the helper is renamed to `_normalize_template_input_value()` (the name #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.
Qubitium
force-pushed
the
root/tools-normalize-4934
branch
from
November 17, 2026 22:12
7dd8968 to
0e107cd
Compare
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 was referenced Nov 17, 2026
Merged
5 tasks done
Qubitium
pushed a commit
that referenced
this pull request
Nov 18, 2026
…#5112) #4997 normalized the `tools`/`documents` columns only. The `messages` column of the same rows keeps Arrow's unified struct schema, so every turn gains `tool_calls: None` (and any other key some turn uses) and every tool call's `arguments` mapping gains the other rows' argument names with a `None` value. Templates then see a different conversation than the plain Python rows: * Qwen2.5/Qwen3 render `{"city": "Paris", "query": null}`, silently calibrating tool-call rows on tokens inference never emits; * Llama-3.x tests `'tool_calls' in message`, so an injected `tool_calls: None` turns a user turn into a tool call and raises `TypeError: object of type 'NoneType' has no len()`. `_normalize_chat_messages()` drops dict entries whose value is `None` before `apply_chat_template`, for every Arrow-backed shape (`Dataset`, `[:]`, `to_pandas()`, `with_format("numpy")`, `list(Dataset)`). Two values are kept because they are not Arrow padding: `content: None`, which an OpenAI-style tool-call turn uses and DeepSeek-R1 tests with `is none`, and `arguments` stored as a JSON string, which raw-text templates insert verbatim. An explicit `None` argument value is indistinguishable from the injected padding, so it is dropped too, matching the `tools` rule from #4934. Red (fix reverted, same test file): 9 failed, 1 passed -- the Llama nodes fail with the reported `TypeError`. Green: 10 passed, plus 32 pre-existing nodes in the #4934/#4936/#4858/#4670 files.
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
Fixes #4934. #4927 started forwarding a calibration row's
tools/documentstoapply_chat_template, but it passed them exactly as the dataset stored them.An HF dataset does not store them the way inference sends them, so the schema
block either still never reached the Hessian or rendered differently:
toolscolumn (Arrow cannot hold a heterogeneous struct column)apply_chat_templateraisesTools should either be a JSON schema, ...; the #4927 fallback then renders without themdatasets.Datasetrows (Arrow unifies the struct schema)null"tz": nulletc.ValueErrorfrom the templatetools, so they need a list of schemasWhat Changed
gptqmodel/utils/calibration.py_normalize_template_input_value(): decodes an object/array JSONstring, drops the
Nonemapping entries Arrow injects, and keeps a realJSON-schema
null({"type": "null"}) andNonelist elements. The walkcopies containers, so the caller's rows are never mutated.
_normalize_template_input_payload(): on top of that, presents aone-tool
toolspayload as the list the chat templates iterate._tokenize_messages_value()normalizestools/documentsbefore thetemplate call (a plain, non-JSON
documentsstring is untouched).tests/test_calibration_messages_tools_4858.pynullproperties, plain-string
documents(guard), columnar mapping.listrows,Datasetrows,Dataset[:],with_format("numpy"|"pandas"|"arrow")[:])against
apply_chat_template(messages, tools=[schema]).also runs when only the cached
Qwen/Qwen2.5-0.5B-Instruct-AWQrepo (sametokenizer files) is available instead of skipping.
tests/test_calibration_tools_formats_4934.py: the tool-layout matrix fromfix(calibration): normalize the tool schemas HF datasets store (#4934) #5004, folded in here (that PR is closed as superseded). All five nodes kept
unweakened; its tokenizer helper falls back to the cached AWQ repo for the
same reason.
.github/workflows/awq_cpu.yml: both regression files run in the pre-mergeAWQ CPU (unit)suite, andgptqmodel/utils/calibration.py/tests/test_calibration_*.pytrigger that workflow.docs/decision-transformer-calibration.md+tests/test_docs_contract_refs.py:the three
calibration.pycitations this diff would have moved are nowsymbol-anchored (
calibration.py::symbol) and guarded by the docs-contracttest.
Tests
Red on unmodified
origin/main(2be068cc6, both regression files copied intoa detached
origin/mainworktree, fix absent):Green here (merged onto
8ab5fefd6):Real-tokenizer evidence (Qwen2.5 tokenizer, two rows with different one-tool
schemas): reference ids 164/164; before the fix every layout rendered 41/41
(tools dropped); after the fix every layout renders 164/164 —
listrows withdict tools,
listrows with JSON-string tools,Datasetrows (dict andJSON-string),
Dataset[:]columnar, andwith_format("numpy"|"pandas"|"arrow")[:].Wider calibration suites (
tests/test_calibration_*.py,tests/test_prepare_dataset.py, plus the two new files and the docs contract):294 passed, 5 skipped, 2 failed. The two failures are
tests/test_calibration_data_device.py::test_stage_capture_balanced_mode_empty_filter_fallbackand
::test_balanced_mode_fallback_when_input_device_not_in_forward_devices,both pre-existing on this host (no GPU devices in the test process). Earlier the
same tree also ran the three VLM calibration suites: 455 passed, 4 skipped,
82 failed, with the identical 82 node names failing on unmodified
origin/main(
commdiff empty; 79 are pre-existing intest_vlm_prepare_dataset_contract_4633.py+test_vlm_pretokenized_calibration.py).ruff check gptqmodel/utils/calibration.py tests/test_calibration_messages_tools_4858.py tests/test_calibration_tools_formats_4934.py tests/test_docs_contract_refs.py: clean.