Bug: pack_dataset BFD fails when all sequences are empty
Reproduction
pack_dataset filters empty sequences before the BFD binning step. When every typed token sequence is empty, the bin list is also empty. Passing the resulting untyped Python list of indices to Arrow makes it a null array, so both BFD strategies fail instead of returning an empty packed dataset.
from datasets import Dataset, Features, Sequence, Value
from trl import pack_dataset
dataset = Dataset.from_dict(
{"input_ids": [[], []]},
features=Features({"input_ids": Sequence(Value("int64"))}),
)
pack_dataset(dataset, seq_length=4, strategy="bfd")
Changing strategy to "bfd_split" produces the same failure.
Output:
pyarrow.lib.ArrowNotImplementedError: Function 'array_take' has no kernel matching input types (list<item: int64>, null)
The expected result is an empty packed dataset with the original list column and an empty seq_lengths column.
System Info
- Platform: macOS-26.5.2-arm64-arm-64bit-Mach-O
- Python version: 3.13.5
- TRL version: 1.13.0.dev0+6630e17
- PyTorch version: 2.13.0 (MPS available)
- Transformers version: 5.16.1
- Datasets version: 5.0.1
- PyArrow version: 25.0.1
Checklist
- [x] I have checked that my issue isn't already filed (see open issues)
- [x] I have included my system information
- [x] Any code provided is minimal, complete, and reproducible (more on MREs)
- [x] Any code provided is properly formatted in code blocks, (no screenshot, more on code blocks)
- [x] Any traceback provided is complete
Hey @YZJF, thanks for the detailed report.
The Arrow error is real, but I'd like to understand when anyone reaches it. Packing maps in batches of 1000 and shuffle_dataset defaults to False, so this needs a thousand consecutive rows that tokenize to empty. That usually means the text column is wrong or empty, and in that case an empty packed dataset is not what you want either, you want to be told the column is empty.
What's the actual training setup where you hit this?
Thanks for flagging this — you're right to question the reachability.
I rechecked the original report and should narrow its scope: I do not have an actual SFTTrainer training run, dataset, tokenizer configuration, or training log that hit this. The issue was a direct public pack_dataset MRE using typed empty input_ids; the later batch_size=2 test only pins all-empty per-map-batch behavior and is not evidence that the default SFT path commonly reaches it.
There is a supported but narrow path: SFTTrainer accepts pre-tokenized datasets with input_ids, and with packing=True passes them to pack_dataset. It does not set a packing batch size, so Datasets uses its default of 1000 and shuffle_dataset defaults to False. One detail I missed is that drop_last_batch=False, so an all-empty final partial batch may be smaller than 1000. That makes the Arrow error technically reachable for pre-tokenized data with an all-empty batch, but it does not establish an ordinary raw-text SFT incident; the non-conversational raw-text path appends EOS before tokenization.
I agree that silently returning an empty packed dataset may hide a data-quality problem. Also, by the time SFT calls pack_dataset, it has retained only input_ids and labels, so a clear error identifying the original text column would require a separate design earlier in preprocessing rather than a small change in _pack_bfd.
I don't want to overstate the scope of #6971. If you prefer, I can close it as too narrow; otherwise, please let me know whether you would prefer a separately designed validation error for an all-empty packing batch.
Closing this one. Thanks for being straight that there's no run behind it.
To be direct, and I hope it's useful for anyone reading this thread, this is not useful contribution. You found a code path that can be reached, not a problem anyone has, and shipped 23 lines of tests for a one-line fix. What's left needs a pre-tokenized dataset whose trailing rows are all empty and unfiltered: broken data plus a positional coincidence, and the loud Arrow error beats silently returning an empty dataset.
Nothing personal here. But now that everyone can point an agent at a repo, maintainers get a steady stream of this kind of findings that are technically correct and that nobody has ever hit, and each one costs a real review from a small team.