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

update dataloader wrappers to have total_batch_size attribute #493

Merged
Merged
Changes from 3 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
30 changes: 28 additions & 2 deletions src/accelerate/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
if is_tpu_available(check_device=False):
import torch_xla.distributed.parallel_loader as xpl

class MpDeviceLoaderWrapper(xpl.MpDeviceLoader):
def __init__(self, loader, device, **kwargs):
super().__init__(loader, device, **kwargs)
pacman100 marked this conversation as resolved.
Show resolved Hide resolved

@property
def total_batch_size(self):
return self._loader.total_batch_size


logger = get_logger(__name__)

# kwargs of the DataLoader in min version 1.4.0.
Expand Down Expand Up @@ -321,6 +330,14 @@ def __iter__(self):
yield current_batch
break

@property
def total_batch_size(self):
return (
self.batch_sampler.batch_size
if self.batch_sampler.split_batches
else (self.batch_sampler.batch_size * self.batch_sampler.num_processes)
)


class DataLoaderDispatcher(DataLoader):
"""
Expand Down Expand Up @@ -432,6 +449,12 @@ def __len__(self):
else:
return math.ceil(whole_length / self.state.num_processes)

@property
def total_batch_size(self):
return (
self.dataset.batch_size if self.split_batches else (self.dataset.batch_size * self.dataset.num_processes)
)


def prepare_data_loader(
dataloader: DataLoader,
Expand Down Expand Up @@ -577,7 +600,10 @@ def prepare_data_loader(

if dispatch_batches:
dataloader = DataLoaderDispatcher(
new_dataset, split_batches=split_batches, batch_sampler=new_batch_sampler, **kwargs
new_dataset,
split_batches=split_batches,
batch_sampler=new_batch_sampler,
**kwargs,
)
else:
dataloader = DataLoaderShard(
Expand All @@ -590,5 +616,5 @@ def prepare_data_loader(
)

if state.distributed_type == DistributedType.TPU:
return xpl.MpDeviceLoader(dataloader, device)
return MpDeviceLoaderWrapper(dataloader, device)
return dataloader