Repository navigation
Conversation
#4927 forwarded a row's `tools`/`documents` to `apply_chat_template` exactly as stored, but function-calling data on the Hub arrives in two layouts that a template cannot consume verbatim: * a JSON string, because Arrow cannot hold a different struct schema per row. The template raised `ValueError: Tools should either be a JSON schema, ...` and #4927's fallback re-rendered the row *without* the tools, so the schema still never reached the Hessian (#4858 again), with one warning per run. * a list of dicts read from an HF `Dataset`, where Arrow unifies the struct schema across rows and gives every row every other row's parameter names with a `None` value, e.g. `"properties": {"city": {...}, "tz": null}`. The rendered tool block then silently differed from what inference sends. Decode the string form and drop dict entries whose value is `None` before forwarding. A real JSON-schema `null` is the string `"null"` (`{"type": "null"}`) and a `None` list element is a legitimate JSON value, so both survive; only Arrow's injected `None` entries are removed. The walk copies every container, so the caller's rows are never mutated, and the rejecting-template fallback now only covers templates that truly do not use tools. Measured with Qwen2.5-0.5B-Instruct (no tools 41, `t1` 176, `t2` 172), after `prepare_calibration_dataset`: | calibration input | before | after | | --- | --- | --- | | list of dicts, real dict tools | 176, 172 | 176, 172 | | list of dicts, JSON-string tools | 41, 41 | 176, 172 | | HF `Dataset`, list tools | 180, 177 | 176, 172 | | HF `Dataset`, JSON-string tools | 41, 41 | 176, 172 | Regression tests (red on the parent commit: 5 of 5 fail): `tests/test_calibration_tools_formats_4934.py`. Fixes #4934
Collaborator
Author
|
Coordination note: #4997 ( |
A bare `pytest.skip(...)` inside the `except` block left that path as an implicit fall-through return, mixing explicit and implicit returns in `_qwen2_5_tokenizer()`. Raise the skip exception instead; behaviour is unchanged (the test still skips with the same message).
Collaborator
Author
|
Superseded by #4997, which includes Adjustments made while folding it in:
Verified with the ported file copied onto unmodified |
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.
Fixes #4934
Follow-up to #4927 (fix of #4858).
Root cause
_tokenize_messages_valueforwarded a row'stools/documentstoapply_chat_templateexactly as stored. Two layouts that real HF function-calling data arrives in were mishandled:toolsstored as a JSON string (common, because Arrow cannot hold heterogeneous schemas).apply_chat_templateraisesValueError: Tools should either be a JSON schema, or a callable ...; the rejecting-template fallback then re-rendered the row without tools, so the schema never reached the Hessian — the [BUG] Calibration messages rows: the tools column is silently dropped; tool schemas never reach the Hessian (101 of 236 intended tokens on Qwen2.5) #4858 symptom, with one warning for the whole run.toolsas a list of dicts inside an HFDataset. Arrow unifies struct schemas across rows, so every row's schema gains the other rows' parameter names asnull(e.g."properties": {"city": {...}, "tz": null}). The rendered tool block then differs from what inference sends, silently.Measured on Qwen2.5-0.5B-Instruct (references: no tools 41,
t1176,t2172):Dataset, list toolsDataset, JSON-string toolsChanges
_normalize_template_input_value()ingptqmodel/utils/calibration.py:json.loadsastr/bytesvalue oftools/documents, and recursively drop dict entries whose value isNone(Arrow's injected nulls; a JSON schema never legitimately carries anullproperty definition). Containers are copied, so the caller's dataset rows are never mutated._tokenize_messages_valuenormalizes before forwarding, so the rejecting-template fallback is reserved for templates that genuinely do not support tools.tests/test_calibration_tools_formats_4934.py: 5 tests — HFDatasetwith different schemas, HFDatasetwith JSON-string tools, list-of-rows with JSON-string tools (each asserting exact token-id equality withapply_chat_template(messages, tools=<original list>)), a null-stripping safety case, and non-JSON text passthrough. Uses the real Qwen2.5-0.5B tokenizer and skips only with a precise reason.Verification
tests/test_calibration_*.py tests/test_prepare_dataset.py: 267 passed, 5 skipped (pre-existing skips: ≥2 CUDA devices, missing TinyLlama path).main(which now contains fix(calibration): keepmessagesconversations from numpy/pandas slices (#4936) #4994's_to_python_containers/_columnar_mapping_exampleswork — composed, no conflict):tests/test_calibration_tools_formats_4934.py tests/test_calibration_messages_formats_4936.py tests/test_prepare_dataset.py→ 38 passed.ruff check gptqmodel tests: clean.Residual risk
None-valued dict entries also removes a legitimate"default": null(rare; per the issue's directive). Anulltype ({"type": "null"}) is preserved — onlyNonevalues are dropped.documentsstring that decodes to a list/dict is parsed; scalar-looking text is kept verbatim.Nonevalue types is not addressed.tools/documentsare normalized, not other custom template kwargs.