Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test_script.py on TPU v2/v3 #2542

Merged
merged 7 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/accelerate/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@ def prepare_data_loader(
generator=getattr(sampler, "generator", torch.Generator()),
)

if isinstance(dataloader.sampler, RandomSampler) and state.distributed_type == DistributedType.XLA:
# isinstance(dataloader.sampler, RandomSampler) indicates the original dataloader has `shuffle` enabled.
generator = torch.Generator().manual_seed(42)
dataloader.generator = generator
dataloader.sampler.generator = generator
dataloader.batch_sampler.sampler = dataloader.sampler
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same reasoning, that doesn't do anything. We need to adjust sampler, we should never be modifying the dataloader directly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually modifying the dataloader has some impact on the new dataloader: #2542 (comment)

If I don't do dataloader.generator = generator, the test would fail a later test with an error AssertionError: Did not obtain the same model on CPU or distributed training.: https://gist.github.com/vanbasten23/ce6d416859b89c23cfd72e272d356504.

So I think it's needed.

# No change if no multiprocess
if (num_processes != 1 or state.distributed_type == DistributedType.MEGATRON_LM) and not dispatch_batches:
if isinstance(new_dataset, IterableDataset):
Expand Down
2 changes: 1 addition & 1 deletion src/accelerate/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def __init__(self, cpu: bool = False, **kwargs):
elif is_torch_xla_available() and not cpu:
self.distributed_type = DistributedType.XLA
self.device = xm.xla_device()
xm.set_replication(self.device, [self.device])
xm.set_replication(self.device, xm.get_xla_supported_devices())
self.num_processes = xm.xrt_world_size()
self.process_index = xm.get_ordinal()
if is_torch_xla_available(check_is_tpu=True):
Expand Down