Skip to content

fix(data): count Hugging Face samples after token packing - #387

Open
Linxiushen wants to merge 1 commit into
Tencent:mainfrom
Linxiushen:fix/hf-text-packing
Open

Linxiushen wants to merge 1 commit into
Tencent:mainfrom
Linxiushen:fix/hf-text-packing

Conversation

@Linxiushen

Copy link
Copy Markdown
Contributor

TextDataset packs Hugging Face text into 2048-token blocks but currently chooses the number of output samples from the number of original text rows. Two short rows can therefore raise IndexError after producing one block, while a single long row silently loses its additional blocks. Empty input also fails. Separately, the HF path returns an unbatched attention mask, so collating two samples produces a [4096] mask for [2, 2048] input IDs.

Apply num_samples to the available packed blocks, return no samples for an empty token stream, and give each attention mask the same leading dimension as its input IDs and labels. The existing partial-block/tail behavior is preserved and documented in the dataset guide.

The new CPU tests use real Hugging Face datasets, a local PreTrainedTokenizerFast, PyTorch tensors, and the existing collator. Only the external dataset-loading boundary is patched to avoid network access. They cover short rows, long rows, positive/unlimited sample limits, empty text, tail handling, and two-sample batches.

Validation (Python 3.12.14, PyTorch 2.10.0+cpu, Transformers 5.6.0):

  • Before the fix: 12 new cases fail, 1 passes.
  • After the fix: python -m pytest tests/test_text_dataset_hf.py tests/test_text_dataset_messages.py tests/test_dataloader.py -q — 35 passed.
  • An additional offline smoke run uses the real HF loader on local JSONL datasets (no patched loader), then the real PyTorch DataLoader: short rows produce one [1, 4] sample, a 6144-token row produces three blocks, and two full rows form a [2, 2048] batch with aligned masks. A tiny randomly initialized GPT-2 model consumes the short batch and returns finite loss; no model weights are downloaded.
  • Configured Black 24.4.0, isort 5.13.2, and flake8 7.0.0 with bugbear checks pass for both Python files; git diff --check passes.

Full GPU compression, model-quality evaluation, and the unrelated repository test suite were not run.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant