Skip to content

[BUG] Calibration input: Dataset.with_format("numpy")[:] (object-dtype columns) crashes, and a pandas DataFrame is iterated as its column labels (calibrates on the text "text") #4652

Closed

Description

@Qubitium

Found on main @ 597b39b, RTX 4090 host (CPU-only repro, real tokenizers).

What is wrong

prepare_calibration_dataset (gptqmodel/utils/calibration.py) mishandles two common tabular containers that hold exactly the columns it accepts.

  1. A column-major mapping with NumPy object columns crashes. Dataset.with_format("numpy")[:] (and {"input_ids": np.array(list_of_ragged_arrays, dtype=object)}) stores each column as a 1-D ndarray of dtype=object. _is_batched_column (:140-161) checks ndim first and returns ndim >= 2, so the 1-D object array is classed as one token row. _to_2d_long_tensor then fails:
    ValueError: Quantize: failed to convert input_ids to tensor for calibration item 0 (cause: can't convert np.ndarray of type numpy.object_).
    The same data works as with_format("torch")[:], as with_format(None)[:], as tok(texts, return_tensors="np", padding=True), and as a list of 1-D arrays. Only the object-dtype column fails, and the message points at the data instead of the container.

  2. A pandas.DataFrame calibrates on its column names. A DataFrame is not a Mapping, so it reaches raw_examples = list(calibration_dataset) (:391). Iterating a DataFrame yields its column labels, so every label becomes one raw text row:

    • With the default calibration_data_min_length=10, the 1-3-token label rows are dropped and the run fails with the misleading "every input_ids was shorter than calibration_data_min_length" error.
    • With a small calibration_data_min_length, it silently quantizes on the literal text "text" (or "input_ids", "attention_mask"). Dataset.with_format("pandas")[:] and ds.to_pandas() return such a DataFrame.

Repro

import types, numpy as np, pandas as pd
from transformers import AutoTokenizer, AutoConfig
from datasets import Dataset
from gptqmodel.utils.calibration import prepare_calibration_dataset

P = "Qwen/Qwen2.5-0.5B-Instruct"
tok = AutoTokenizer.from_pretrained(P)
q = types.SimpleNamespace(tokenizer=tok, model=types.SimpleNamespace(config=AutoConfig.from_pretrained(P)),
                          support_batch_quantize=True)
texts = ["The quick brown fox jumps over the lazy dog. " * 3,
         "Hello world, this is a calibration sample with some words in it."]
ds = Dataset.from_dict({"text": texts}).map(lambda x: tok(x["text"]), remove_columns=["text"])

for fmt in ("torch", "numpy"):
    try:
        print(fmt, [int(b["attention_mask"].sum()) for b in prepare_calibration_dataset(q, ds.with_format(fmt)[:])])
    except Exception as e:
        print(fmt, type(e).__name__, e, "|", repr(e.__cause__)[:80])

b = prepare_calibration_dataset(q, pd.DataFrame({"text": texts * 3}), calibration_data_min_length=0)
print(len(b), [tok.decode(x["input_ids"][0]) for x in b])

Output:

torch [31, 14]
numpy ValueError Quantize: failed to convert `input_ids` to tensor for calibration item 0. | TypeError("can't convert np.ndarray of type numpy.object_ ...
1 ['text']          # 6 documents -> one 1-token row, the column label (Llama tokenizer: '<|begin_of_text|>text')

Suggested fix

  • _is_batched_column: treat a 1-D np.ndarray with dtype=object (and pandas.Series) as a column of rows, then convert each element on its own. In _columnar_mapping_examples, index such columns element-wise.
  • Treat a pandas.DataFrame (any object with .to_dict("list") / .columns) as a columnar mapping. Alternatively reject it with a clear error, instead of iterating it as an iterable of strings.

Test that should have caught it

tests/test_calibration_mapping_input.py covers lists and tensors only. Add ds.with_format("numpy")[:] and a DataFrame case.

Severity

Low. Item 1 is a crash with a misleading message. Item 2 is silent only with a lowered calibration_data_min_length; under defaults it is a misleading error.

🤖 Generated with Claude Code

Activity

  1. ZX-ModelCloud commented on Nov 17, 2026

    @ZX-ModelCloud
    Collaborator

    Claimed. I'm working on this issue and will submit a fix PR. Please avoid duplicate work in the meantime.

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

Metadata

Metadata

Assignees

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