Skip to content

[BUG] #4902 (fix of #4882) incomplete: a messages column from with_format("numpy")[:] / ("pandas")[:] collapses every conversation into one 21-token system-prompt-only row #4936

Closed

Description

@Qubitium

Found on main @ 3867c82, CPU (tokenizer-only path).

What is wrong

#4902 (fix of #4882) makes text and input_ids columns reach the same token rows in every HF datasets format, both iterated and sliced. A messages column sliced from a numpy- or pandas-formatted dataset (ds.with_format("numpy")[:], ds.with_format("pandas")[:]) is still mis-read. All conversations silently collapse into one row that renders only the template's default system prompt.

The cause is in _is_columnar_mapping (gptqmodel/utils/calibration.py:405-409):

if "messages" in mapping:
    entries = _column_entries(mapping["messages"])
    if not entries:
        return False
    return isinstance(entries[0], (list, tuple))

In the numpy format, a messages column is a 2-D ndarray(dtype=object) of dicts when all conversations have the same number of turns, otherwise a 1-D object array of arrays. pandas gives a Series of ndarrays. In both cases entries[0] is an ndarray, not a list/tuple, so the mapping is classified as a single row. The whole column then goes to apply_chat_template as one "conversation", which renders as just the default system turn. The run logs no warning.

Repro (Qwen2.5-0.5B-Instruct tokenizer)

texts = [f"This is calibration sentence number {i} with enough words to pass the filter." for i in range(6)]
msgs = [[{"role":"user","content":t},{"role":"assistant","content":"ok fine sure"}] + ([{"role":"user","content":"more"}] if i % 2 else []) for i, t in enumerate(texts)]
ds = Dataset.from_dict({"messages": msgs})
prepare_calibration_dataset(q, [{"messages": m} for m in msgs], batch_size=1)   # reference
prepare_calibration_dataset(q, ds.with_format("numpy")[:], batch_size=1)
prepare_calibration_dataset(q, ds.with_format("pandas")[:], batch_size=1)
(rows, real tokens)
reference list      (6, 312)
numpy  [:]          (1, 21)     decoded: '<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n'
pandas [:]          (1, 21)

The same happens with equal-length conversations (2-D object array): 294 expected, 21 calibrated. Every other format and shape gives 294 (None/torch/numpy/python/pandas/arrow, iterated, and [:] for None/torch/python/arrow). This is pre-existing at c41fa937c^ too, but #4902's stated contract ("every datasets format in both shapes must reach the same token rows") does not hold for messages.

Suggested fix

In _is_columnar_mapping and _columnar_mapping_examples, treat an ndarray/Series entry as a conversation: test getattr(entries[0], "ndim", 0) >= 1 or isinstance(entries[0], Sequence | np.ndarray). Convert each row's messages to a list of dicts (.tolist()) before apply_chat_template. A cheaper alternative: when the mapping has a messages column, raise if the "single row" conversation is not a list of mappings with role, instead of rendering an empty prompt.

Test that should have caught it

tests/test_calibration_mapping_input.py runs the format x shape matrix only for text/input_ids rows. Add a messages dataset to the same matrix.

Severity: medium. The calibration data is silently replaced by one system-prompt-only row (95-99% of the tokens are lost), and quantize then proceeds on it.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4991

    Finish #4902: a messages column read through with_format('numpy')[:] or ('pandas')[:] collapses every conversation (structured fields lost); normalize the container back to the row dicts the template expects and add tests for both formats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions