Skip to content

[BUG] Calibration rows with both text and input_ids are silently re-tokenized from text: Dataset.map(tokenize, truncation=True, max_length=256) feeds 2455-5405-token documents instead of 256 #4635

Closed

Description

@Qubitium

Found on main @ 597b39b, RTX 4090 host (repro is CPU-only, real Qwen2.5-0.5B-Instruct tokenizer).

What is wrong

gptqmodel/utils/calibration.py:600-611 (prepare_calibration_dataset) checks the row keys in this order: messages, then text, then input_ids. A row that carries both text and input_ids is always re-tokenized from text with default settings, and the user's input_ids / attention_mask are dropped without a message.

Rows like that are the normal output of the most common pre-tokenization recipe. Dataset.map(lambda x: tok(x["text"], truncation=True, max_length=N)) keeps the text column next to the new input_ids/attention_mask, as does ds[:] of such a dataset. So any tokenization choice the user made is silently undone:

  • truncation=True, max_length=N (the usual way to cap calibration memory/time): the full documents are fed instead, up to max_position_embeddings (32768 for Qwen2.5). Activation memory and capture time grow by more than 20x in the repro below.
  • add_special_tokens=False, a custom BOS/EOS, a manually applied chat template on the ids, or ids from another preprocessing pipeline: all replaced by tokenizer(text).

Nothing is logged. The same dataset with remove_columns(["text"]) gives the expected result, so the text column alone, which looks harmless, changes the calibration set.

Repro

import gzip, json, types
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)   # same call quantize() makes via BaseQModel.prepare_dataset
texts = [...]  # 4 C4 documents longer than 6000 chars
ds = Dataset.from_dict({"text": texts}).map(lambda x: tok(x["text"], truncation=True, max_length=256))
print(ds.column_names, [len(r) for r in ds["input_ids"]])
print([int(b["attention_mask"].sum()) for b in prepare_calibration_dataset(q, ds)])
print([int(b["attention_mask"].sum()) for b in prepare_calibration_dataset(q, ds.remove_columns(["text"]))])
print(prepare_calibration_dataset(q, [{"text": "hello world " * 20, "input_ids": [1, 2, 3] * 20}])[0]["input_ids"][0][:6])

Output:

['text', 'input_ids', 'attention_mask'] [256, 256, 256, 256]
[2455, 1628, 5405, 1690]        # tokens actually fed to layer 0: user truncation ignored
[256, 256, 256, 256]            # same data without the text column
tensor([14990, 1879, 23811, 1879, 23811, 1879])   # user ids [1, 2, 3, ...] discarded

Suggested fix

When a row (or a columnar mapping) supplies input_ids, use them: explicit token ids should win over text. If keeping text precedence is intended, log a warning (once) that input_ids were ignored, and document the precedence. VLM definitions route such rows through split_calibration_dataset (which classifies them as pre-tokenized) into this same base path, so they inherit the fix.

Test that should have caught it

tests/test_prepare_dataset.py / tests/test_calibration_mapping_input.py have no row with both text and input_ids. Add a Dataset.map(tokenize, truncation=True, max_length=N) case that asserts the prepared rows equal the mapped input_ids (or that a warning is raised).

Severity

Low-medium. The input form is standard, the override is silent, and it changes both the calibration set and the memory/time budget (an OOM risk on long-document corpora). No crash.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed. This issue is now owned by a dedicated worker; a fix PR referencing it will follow. Please avoid duplicating work on it in the meantime.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Taking this. Explicit input_ids will win over text (the row's tokenization is the caller's decision), with the row/columnar paths covered by a regression test built from a Dataset.map(tokenize, truncation=True, max_length=N) result.

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