Skip to content

fix(calibration): keep messages conversations from numpy/pandas slices (#4936) - #4994

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4936
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4936

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4936
Follow-up to #4902 (fix of #4882).

Root cause

_is_columnar_mapping classified a messages mapping as a single row unless entries[0] was a list/tuple. with_format("numpy")[:] yields a 1-D ndarray(dtype=object) of conversations (or a 2-D object array of turns when all conversations have equal length), and pandas yields a Series of arrays — both ndarray, so the whole column went to apply_chat_template as one "conversation". The template rendered only its default system turn and the data was silently dropped.

Repro before the fix: reference 6 rows / 312 tokens; numpy 1 / 21; pandas 1 / 21; the decoded prompt was the system turn only.

A second gap surfaced while testing: _columnar_mapping_examples copied only _CALIBRATION_COLUMNS, so tools/documents were dropped from columnar rows — the #4858 forwarding only worked for plain dict rows. Fixed too.

Changes

  • _is_conversation_entry() accepts ndarray/Series/sequence entries as conversations, while a single-row mapping (entries starting with a role mapping) still classifies as one row.
  • _to_python_containers() recursively copies rows into plain list/dict/str/number containers: mappings, numpy arrays (including 2-D object arrays), pandas Series, pandas DataFrame rows (to_dict("list")), structured/record arrays (dtype.names, np.void), numpy scalars, and pyarrow scalars (as_py). torch.Tensor is returned untouched and caller slices are not mutated.
  • Applied once to every row in prepare_calibration_dataset before templating, so all input shapes share one path.
  • _columnar_mapping_examples now also carries _TEMPLATE_INPUT_COLUMNS = ("tools", "documents").

Verification

  • New tests/test_calibration_messages_formats_4936.py: 12 passed — [:] plus numpy/pandas/torch/arrow, 2-D object array, iteration, tools, documents, multi-part content, no-mutation, single-row mapping, and a real Qwen2.5 tokenizer.
  • Red on parent (source swapped from origin/main): 6 failed, 5 passed (numpy/pandas/torch/arrow slices, 2-D numpy, real Qwen).
  • tests/test_calibration_*.py tests/test_prepare_dataset.py: 262 passed, 5 skipped.
  • After merging current main: tests/test_calibration_messages_formats_4936.py tests/test_prepare_dataset.py → 33 passed.
  • ruff check gptqmodel tests: clean.

Residual risk

  • The pyarrow scalar path (as_py) is implemented but not directly unit-tested.
  • The VLM conversations pipeline (batched_conversations / normalize_chat_calibration_sample) is untouched.
  • Non-calibration columns other than tools/documents are still dropped from columnar mappings (pre-existing).
  • No real quantization run was performed; this is a calibration-dataset plumbing change.

A `messages` column sliced from a numpy- or pandas-formatted HF dataset
(`with_format("numpy")[:]`, `with_format("pandas")[:]`) was read as one
row: `_is_columnar_mapping` only accepted a list/tuple entry, while numpy
gives a 1-D object array of conversations (or a 2-D object array of turns
when all conversations are equal length) and pandas a Series of arrays.
The whole column then went to `apply_chat_template` as a single
"conversation" and rendered only the template's default system turn, so
calibration silently collapsed to one prompt (#4936).

- classify an ndarray/Series `messages` entry as a conversation container;
- recursively copy rows into plain list/dict/str/number containers before
  templating (ndarray, Series, DataFrame rows, record arrays, pyarrow
  scalars); torch tensors stay untouched and the caller's slice is not
  mutated;
- carry `tools`/`documents` through columnar mappings so a sliced row
  passes the same chat-template inputs a plain row does (#4858).

Tests cover `[:]`, numpy/pandas/torch/arrow slices, 2-D object arrays,
plain iteration, tools/documents columns, multi-part content, and a real
Qwen2.5 tokenizer.
@Qubitium
Qubitium merged commit 5b621d0 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4936 branch November 17, 2026 21:35
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