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 1 commit
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
20 changes: 20 additions & 0 deletions src/accelerate/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@
import torch_xla.distributed.parallel_loader as xpl

class MpDeviceLoaderWrapper(xpl.MpDeviceLoader):
"""
Wrapper for the xpl.MpDeviceLoader class. This class is used to add `total_batch_size` property to the
xpl.MpDeviceLoader class.
"""

pacman100 marked this conversation as resolved.
Show resolved Hide resolved
@property
def total_batch_size(self):
"""
Get the total batch size of the dataloader. It is the resulting batch size across processes. It is same as
the original batch size of the dataloader when `split_batches=True`. Otherwise, it is the product of the
orginal batch size of the dataloader and the number of processes.
"""
return self._loader.total_batch_size


Expand Down Expand Up @@ -329,6 +339,11 @@ def __iter__(self):

@property
def total_batch_size(self):
"""
Get the total batch size of the dataloader. It is the resulting batch size across processes. It is same as the
original batch size of the dataloader when `split_batches=True`. Otherwise, it is the product of the orginal
batch size of the dataloader and the number of processes.
"""
return (
self.batch_sampler.batch_size
if self.batch_sampler.split_batches
Expand Down Expand Up @@ -448,6 +463,11 @@ def __len__(self):

@property
def total_batch_size(self):
"""
Get the total batch size of the dataloader. It is the resulting batch size across processes. It is same as the
original batch size of the dataloader when `split_batches=True`. Otherwise, it is the product of the orginal
batch size of the dataloader and the number of processes.
"""
return (
self.dataset.batch_size if self.split_batches else (self.dataset.batch_size * self.dataset.num_processes)
)
Expand Down