fix(datasets): stop default_collater reshaping the caller's example tensors - #3934
Open
kabirvashisht4-glitch wants to merge 1 commit into
Conversation
…ensors `batchify` unsqueezes a 1-D tensor in place and returns the same object -- a contract `test_batchify_adds_batch_dimension` pins with `assert out is vec`. `default_collater` applied it to tensors taken straight out of the caller's examples, so collating rewrote each example's field from `[S]` to `[1, S]`. The collated batch is unaffected, but the example is left reshaped, and `len(sample["input_ids"])` then reports 1 instead of S. Any later reader of that object sees the wrong shape: a second epoch over an in-memory dataset, a metric, or a packer measuring `ids.shape[0]`. Add the batch axis out-of-place at the call site rather than changing `batchify`, whose in-place behavior is deliberate and tested, and note the side effect in its docstring so the next caller sees it. Signed-off-by: kabirvashisht4-glitch <kabirvashisht4@gmail.com>
yuhezhang-ai
approved these changes
Sep 18, 2026
Contributor
|
/ok to test 7f69807 |
yuhezhang-ai
enabled auto-merge (squash)
September 18, 2026 15:57
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
Fixes #3933:
default_collaterreshaped the caller's example tensors as a side effect ofcollating.
The pre-batched branch built its output with
torch.cat([batchify(v) for v in values]),where
valuesare the tensors taken straight out of the caller's examples.batchifyunsqueezes a 1-D tensor in place and returns the same object, so each example's field
was rewritten from
[S]to[1, S]:Where the fix goes, and why not in
batchifybatchify's in-place behavior is deliberate, not a typo —test_batchify_adds_batch_dimensionpins it with
assert out is vecand a comment saying so. Changingunsqueeze_tounsqueezewould break that contract for every caller to fix one of them.Every other
batchifycall site passes a freshly constructed tensor (torch.stack(...),torch.LongTensor(...)), sodefault_collater's pre-batched branch is the only place themutation escapes into data the function does not own. The fix is therefore at that call
site: add the batch axis out-of-place.
batchifykeeps its contract and gains a docstringwarning so the next caller sees the side effect before hitting it.
Impact
Latent rather than actively corrupting runs, and I'd rather say so than oversell it. The
collated batch is correct, re-collating the same examples still yields the right
[B, S],and the one in-tree consumer that measures a sample after construction —
LengthGroupedSampler._compute_lengths— happens to useids.numel(), which isshape-independent.
What is broken is the example object the dataset may hand out again:
len(sample["input_ids"])andsample["input_ids"].shape[0]both become1. That bites asecond epoch over an in-memory dataset, a metric read off the sample, or packing code
measuring
ids.shape[0]—neat_pack_datasetdoes exactly that.Changelog
v.unsqueeze(0)instead ofbatchify(v), sodefault_collaterno longer mutates the examples it was given.batchifythat a 1-D input is unsqueezed in place and returned as the sameobject, and on
default_collaterthat — apart from the existing___PAD_TOKEN_IDS___pop — inputs are left unmodified.
[S]shape andlen()across acollate, and one that collating the same example objects twice (an in-memory dataset
across epochs) leaves them intact and gives the same batch.
Before your PR is "Ready for review"
Pre checks:
Verified locally:
mainand pass here. Reverting only thenemo_automodel/changeand keeping the tests reproduces both failures.
pytest tests/unit_tests/datasets/-> 1361 passed, 24 failed. The same 24 fail on cleanmain(1359 passed there); they are pre-existing and unrelated (object-storage andretrieval-launcher tests). The delta is exactly the two tests added here.
ruff format --checkandruff checkclean on both changed files.Additional Information
pad_within_micro(batch, pad_token_id=None)pads every row withbatch[0][-1]rather than each row's own last token, which affects fields with nodefault pad token such as
position_ids. It is written up at the end of default_collater reshapes the caller's example tensors from [S] to [1, S] #3933 anddeliberately left out of this PR, since the intended semantics of that branch are not
obvious from the code. Happy to split it out into its own change.