Skip to content

Fix metric padding removal after skip_first_batches - #4235

Merged
SunMarc merged 3 commits into
huggingface:mainfrom
Excelius-Wang:fix-skip-batches-metric-padding
Oct 1, 2026
Merged

SunMarc merged 3 commits into
huggingface:mainfrom
Excelius-Wang:fix-skip-batches-metric-padding

Conversation

@Excelius-Wang

@Excelius-Wang Excelius-Wang commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

skip_first_batches wraps the batch sampler in SkipBatchSampler, which does not expose batch_size. This makes DataLoaderShard.total_batch_size raise; the suppressed exception leaves remainder=-1, so gather_for_metrics keeps duplicated padding samples in the final batch.

With 10 samples, two processes and a per-process batch size of 2, normal iteration gathers 10 samples. Calling skip_first_batches(loader, 0) instead gathers 12, including duplicated samples 0 and 1.

Unwrap skip samplers before reading the existing batch-size metadata. This also handles nested skipping without changing sampling order. The existing loader-configuration preservation from #4296 is retained.

Validation

  • 51 related tests passed, 16 skipped, including the existing loader-configuration regression from Fix skip_first_batches dropping drop_last, non_blocking, slice_fn, and mesh聽#4296.
  • Two focused unit tests check batch metadata and drop_last after skipping one batch.
  • The batch-metadata and distributed regressions fail on current main and pass with this fix.
  • Actual two-process CPU/Gloo testing passed: with 10 samples and a per-process batch size of 2, skipping one batch gathers exactly samples 4 through 9.
  • Full make quality and git diff --check against current main pass.

GPU/TPU execution was not tested. Changes were prepared with AI assistance and reviewed by the contributor.

Before submitting

  • Read the contribution guidelines.
  • Added regression tests.
  • Prior issue discussion: none.
  • Documentation changes: not needed; this restores existing behavior.

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, can you try to iterate a bit on the test to make them simpler ? thanks !

Comment thread tests/test_data_loader.py Outdated
Comment on lines +617 to +627
@parameterized.expand(
[
(split, drop, skips, sampler_arg)
for split in (False, True)
for drop in (False, True)
for skips in ((0,), (1,), (1, 1))
for sampler_arg in (False, True)
if not (split and sampler_arg) # batch_size=None does not support split_batches.
]
)
def test_skip_first_batches_preserves_batch_metadata(self, split_batches, drop_last, skips, sampler_arg):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit too much parametrized. we can just check the most common case.

Comment on lines +379 to +381
for num_items in (3 * total_batch_size, 3 * total_batch_size + 1):
for drop_last in (False, True):
for skips in ((0,), (1,), (1, 1)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@Excelius-Wang

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I鈥檒l simplify both tests to focus on the common case and keep the regression coverage concise.

@SunMarc SunMarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, thanks a lot !

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@SunMarc
SunMarc merged commit 01c73fb into huggingface:main Oct 1, 2026
25 checks passed
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.

3 participants