Repository navigation
Conversation
When prepare_data_loader wraps a sized IterableDataset in IterableDatasetShard (dispatch_batches=False), DataLoaderStateMixin.begin() previously fell back to len(self.dataset), which returns the padded per-rank shard length rather than the underlying dataset length. In addition, DataLoaderShard.total_batch_size inspected the default batch_sampler (where num_processes is not set) instead of the IterableDatasetShard configuration, returning the per-process batch_size rather than the global total_batch_size when split_batches=False. As a result, dataloader.remainder evaluated to 0 whenever the padded per-rank length was divisible by batch_size, causing Accelerator.gather_for_metrics() to retain padded duplicate samples at the end of evaluation. Expose IterableDatasetShard.total_dataset_length and teach DataLoaderShard's total_batch_size and total_dataset_length properties to read from IterableDatasetShard so remainder is computed against the true dataset length and global batch size. Fixes huggingface#4359 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Parth Vadera <parthvadera@gmail.com>
3 of 4 tasks
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 #4359.
When
dispatch_batches=Falseis used with a sizedIterableDatasetacross multiple processes (num_processes > 1),prepare_data_loaderwraps the dataset inIterableDatasetShardand returns aDataLoaderShard.Previously, two metadata mismatches caused
DataLoaderStateMixin.begin()to computeself.remainder = 0instead of the true remainder at the end of the dataset:IterableDatasetShard.__len__returns the padded per-rank shard length (math.ceil(len(dataset) / (batch_size * num_processes)) * batch_size), which is always a multiple ofbatch_size. BecauseIterableDatasetSharddid not definetotal_dataset_length,DataLoaderStateMixin.begin()fell back tolen(self.dataset)(the padded shard length) instead oflen(self.dataset.dataset)(the unpadded dataset length).DataLoaderShard.total_batch_sizereadnum_processesandsplit_batchesfromself.batch_sampler. For anIterableDataset, PyTorch's default_InfiniteConstantSampler/BatchSampleris not wrapped inBatchSamplerShard, sogetattr(batch_sampler, "num_processes", 1)evaluated to1andtotal_batch_sizereturned the per-rankbatch_sizeinstead ofbatch_size * num_processes(whensplit_batches=False).Consequently,
self.remainder = length % self.total_batch_sizeevaluated to0, andAccelerator.gather_for_metrics()did not truncate the padded duplicate samples on the final batch.Changes
total_dataset_lengthproperty onIterableDatasetShardreturninglen(self.dataset)(which still raisesTypeError/NotImplementedErrorfor unsizedIterableDatasets and is safely ignored bywith suppress(Exception):inDataLoaderStateMixin.begin()).DataLoaderShard.total_batch_sizeandDataLoaderShard.total_dataset_lengthto handleIterableDatasetShard.tests/test_data_loader.pycovering bothsplit_batches=Falseandsplit_batches=True.Credits & Acknowledgments
gather_for_metricskeeps padding duplicates for a sizedIterableDatasetwithdispatch_batches=False #4359, diagnosed both underlying causes (IterableDatasetShard.total_dataset_lengthandDataLoaderShard.total_batch_size), and proposed the exact 2-part fix and unit test in the issue description. Since @Zhuoxi2000 noted ingather_for_metricskeeps padding duplicates for a sizedIterableDatasetwithdispatch_batches=False #4359 that they already had a local fix ready and were waiting for assignment, please feel free to close this PR in favor of @Zhuoxi2000's PR or credit @Zhuoxi2000 as the author.