Repository navigation
fix(calibration): drop Arrow-injected nulls from messages before the chat template (#5082) - #5112
Merged
Merged
Conversation
#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 #5082.
#4997 normalized the
tools/documentscolumns for Arrow-backed calibrationrows but left the
messagescolumn of those same rows unnormalized. Arrowstores one struct type per column, so every turn gains a
Nonevalue for eachkey another turn uses, and every tool call's
argumentsmapping gains the otherrows' argument names:
That reaches
apply_chat_templateunchanged, so calibration renders a differentconversation than inference:
{"name": "get_weather", "arguments": {"city": "Paris", "query": null}}, soevery tool-call row is calibrated on tokens the model never sees (+5 tokens
per call in the issue's two-row fixture).
'tool_calls' in message, sothe injected
tool_calls: Noneon a user turn reachesmessage.tool_calls|lengthand raisesTypeError: object of type 'NoneType' has no len().Every Arrow-backed input shape is affected:
Dataset,Dataset[:],Dataset.to_pandas(),with_format("numpy"),list(Dataset).What Changed
gptqmodel/utils/calibration.py: new_normalize_chat_messages()walk, calledfrom
_tokenize_messages_value()before the template runs. It drops mappingentries whose value is
None, recursively, and copies every container so thecaller's rows are never mutated.
content: None, which is how an OpenAI-style tool-call turn spells "notext" and which templates such as DeepSeek-R1 test with
message.content is none;argumentsstored as a JSON string, which raw-text templates insertverbatim (DeepSeek-R1 writes it into a
jsonblock). Decoding it into amapping, as the issue's suggested fix proposed, would corrupt that prompt
and turn a working dataset into
TypeError: can only concatenate str.Noneargument value is indistinguishable fromArrow's injected padding for the same key, so it is dropped as well. That is
the same rule [BUG] #4927 (fix of #4858) incomplete: tools from HF datasets still don't render as at inference (JSON-string tools column dropped; Arrow-unified tool schemas gain null properties) #4934 already applies to a
toolsschema'sproperties; aNoneelement inside a list is still preserved..github/workflows/awq_cpu.yml: the new regression file runs in the existingpre-merge
AWQ CPU (unit)calibration suite.Tests
Red evidence: the new test file with only the
calibration.pychange revertedin the same worktree, so the rest of the tree is the fix:
The failures are the reported defect, not import or collection errors: the
Llama nodes fail inside the template with
TypeError: object of type 'NoneType' has no len(), and the Qwen nodes differfrom the plain-row token ids.
Green:
The real tokenizers (Qwen2.5-0.5B-Instruct, Llama-3.2-1B-Instruct,
DeepSeek-R1-Distill-Llama-8B) run against cached files locally; none of the new
nodes skips. The issue's own repro now prints identical token counts for the
list and
Datasetshapes:Coverage in
tests/test_calibration_messages_arrow_5082.py:test_hf_dataset_messages_match_the_plain_rows[qwen2.5|llama3.2]— tokenequality with
apply_chat_templateon the original Python list, plus thelist-input control.
test_every_arrow_backed_input_shape_matches_the_plain_rows[...]—Dataset,[:],to_pandas(),with_format("numpy")[:],list(Dataset).test_llama3_2_dataset_does_not_raise_on_tool_calls_none— the crash half,asserting the rendered prompts equal the plain-row prompts.
test_json_string_arguments_stay_verbatim_for_raw_text_templates— theDeepSeek-R1 raw
jsonblock stays{"city": "Paris"}and token ids match.test_normalize_chat_messages_keeps_content_none_and_does_not_mutate— theNonerules and the no-mutation guarantee.Not exercised: no quantization run was performed, since the change is confined
to calibration tokenization; the token ids produced for the affected rows are
compared directly. GPU paths are untouched by this change.
Model Support / Module Plan
Not applicable: no model, alias, definition, or module-plan change.
AWQ onboarding safety
Not applicable: no model/alias onboarding and no AWQ grouping, search, replay,
or folding change.
Logic Flow Documentation
Not applicable as a new mechanism. The single decision point
(
_tokenize_messages_value→ template) is unchanged; only the conversationpayload it hands to the template is normalized, exactly like the existing
tools/documentsnormalization from #4997. There is no new branch, fallback,or selection order, and the failure path (template rejects the optional inputs)
is unmodified.
Review Requirements
AI-assisted code is welcome. Draft PR.
mirrors
_normalize_template_input_value).tools, multi-part
content,content: None, documents column).Kernel Accuracy Requirements
Not applicable: no kernel or fusion change.
Notes
argumentsinto a mapping. Thatis intentionally not done: it silently breaks raw-text templates such as
DeepSeek-R1, and the decoded form is not what those datasets send at
inference. The regression covers that case.
content: Noneinjection is left in place. Templates that concatenate aplain turn's
contentalso fail on the plain Python rows, so it is not anArrow-specific difference.