Skip to content

fix(data_loader): handle dataloaders missing batch_sampler attribute - #4179

Open
SparshM8 wants to merge 1 commit into
huggingface:mainfrom
SparshM8:fix-missing-bs-882
Open

fix(data_loader): handle dataloaders missing batch_sampler attribute#4179
SparshM8 wants to merge 1 commit into
huggingface:mainfrom
SparshM8:fix-missing-bs-882

Conversation

@SparshM8

Copy link
Copy Markdown

What does this PR do?

This PR fixes issue #882 where prepare_data_loader (and consequently Accelerator.prepare) fails with an AttributeError when a DataLoader object does not have a batch_sampler attribute. While standard PyTorch DataLoader objects always have this attribute, some custom or wrapped dataloaders might not, causing crashes in accelerate.

Changes

  • Updated get_sampler helper to safely access batch_sampler using getattr.
  • Updated prepare_data_loader to use getattr(dataloader, "batch_sampler", None) instead of direct access.
  • Added a native regression test tests/test_issue_882.py that simulates a dataloader without a batch_sampler attribute.

Validation

  • Verified with a reproduction script that was previously failing.
  • Native regression test passes locally.
  • Confirmed that it still preserves custom sampler structures if present.

Fixes #882

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.

accelerator.prepare(dataloader) fails when batch_sampler is not given

1 participant