Skip to content

fix(calibration): render Hub tools columns the way inference does (#4934) - #4997

Merged
Qubitium merged 10 commits into
mainfrom
root/tools-normalize-4934
Nov 17, 2026
Merged

Qubitium merged 10 commits into
mainfrom
root/tools-normalize-4934

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4934. #4927 started forwarding a calibration row's tools/documents to
apply_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:

layout before why
JSON-string tools column (Arrow cannot hold a heterogeneous struct column) row rendered without any tools (41 vs 164 ids) apply_chat_template raises Tools should either be a JSON schema, ...; the #4927 fallback then renders without them
datasets.Dataset rows (Arrow unifies the struct schema) rendered schema gains other rows' properties as null every row's JSON schema is polluted with "tz": null etc.
one tool stored as one object ValueError from the template the templates iterate tools, so they need a list of schemas

What Changed

  • gptqmodel/utils/calibration.py
    • new _normalize_template_input_value(): decodes an object/array JSON
      string, drops the None mapping entries Arrow injects, and keeps a real
      JSON-schema null ({"type": "null"}) and None list elements. The walk
      copies containers, so the caller's rows are never mutated.
    • new _normalize_template_input_payload(): on top of that, presents a
      one-tool tools payload as the list the chat templates iterate.
    • _tokenize_messages_value() normalizes tools/documents before the
      template call (a plain, non-JSON documents string is untouched).
  • tests/test_calibration_messages_tools_4858.py
    • five new nodes: JSON-string tools, single-tool JSON, Arrow-injected null
      properties, plain-string documents (guard), columnar mapping.
    • one real-tokenizer node comparing every Hub layout (list rows,
      Dataset rows, Dataset[:], with_format("numpy"|"pandas"|"arrow")[:])
      against apply_chat_template(messages, tools=[schema]).
    • the pre-existing real-tokenizer node now shares the tokenizer helper, so it
      also runs when only the cached Qwen/Qwen2.5-0.5B-Instruct-AWQ repo (same
      tokenizer files) is available instead of skipping.
  • tests/test_calibration_tools_formats_4934.py: the tool-layout matrix from
    fix(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-merge
    AWQ CPU (unit) suite, and gptqmodel/utils/calibration.py /
    tests/test_calibration_*.py trigger that workflow.
  • docs/decision-transformer-calibration.md + tests/test_docs_contract_refs.py:
    the three calibration.py citations this diff would have moved are now
    symbol-anchored (calibration.py::symbol) and guarded by the docs-contract
    test.

Tests

Red on unmodified origin/main (2be068cc6, both regression files copied into
a detached origin/main worktree, fix absent):

$ python /tmp/root-rt.py tests/test_calibration_messages_tools_4858.py tests/test_calibration_tools_formats_4934.py -q
FAILED ...::test_json_string_tools_are_decoded_before_rendering
FAILED ...::test_a_single_json_tool_is_presented_as_a_list
FAILED ...::test_arrow_injected_null_properties_are_dropped
FAILED ...::test_hub_tool_layouts_render_exactly_like_inference
FAILED ...::test_hf_dataset_with_different_tool_schemas_matches_inference
FAILED ...::test_hf_dataset_with_json_string_tools_matches_inference
FAILED ...::test_plain_list_rows_with_json_string_tools_match_inference
FAILED ...::test_null_stripping_keeps_a_real_null_schema_and_does_not_mutate
FAILED ...::test_non_json_text_is_forwarded_unchanged
9 failed, 7 passed

Green here (merged onto 8ab5fefd6):

$ python /tmp/root-rt.py tests/test_calibration_messages_tools_4858.py       tests/test_calibration_tools_formats_4934.py tests/test_docs_contract_refs.py -q
39 passed

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 — list rows with
dict tools, list rows with JSON-string tools, Dataset rows (dict and
JSON-string), Dataset[:] columnar, and with_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_fallback
and ::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
(comm diff empty; 79 are pre-existing in
test_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.

…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
Qubitium force-pushed the root/tools-normalize-4934 branch from f73a09d to 94b5cf9 Compare November 17, 2026 21:35
…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.
@Qubitium

Copy link
Copy Markdown
Collaborator Author

Rebased onto main (c918fb9fd) to clear the CONFLICTING state: the only conflict was .github/workflows/awq_cpu.yml, where main now also lists tests/test_awq_hybrid_moe_root_4903.py (from #5000). Resolution keeps both new test files in the AWQ CPU unit suite. Verified on the merge commit: tests/test_calibration_messages_tools_4858.py + tests/test_docs_contract_refs.py → 34 passed.

…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
Qubitium force-pushed the root/tools-normalize-4934 branch from 7dd8968 to 0e107cd Compare November 17, 2026 22:12
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.
@Qubitium Qubitium closed this Nov 17, 2026
@Qubitium Qubitium reopened this Nov 17, 2026
@Qubitium
Qubitium merged commit 9c5efff into main Nov 17, 2026
12 of 13 checks passed
@Qubitium
Qubitium deleted the root/tools-normalize-4934 branch November 17, 2026 22:31
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant