diff --git a/src/accelerate/data_loader.py b/src/accelerate/data_loader.py
index 84c2ebd67..1eb8ff79d 100644
--- a/src/accelerate/data_loader.py
+++ b/src/accelerate/data_loader.py
@@ -364,10 +364,11 @@ def __iter__(self):
 
     @property
     def total_batch_size(self):
+        batch_sampler = self.sampler if isinstance(self.sampler, BatchSampler) else self.batch_sampler
         return (
-            self.batch_sampler.batch_size
-            if self.batch_sampler.split_batches
-            else (self.batch_sampler.batch_size * self.batch_sampler.num_processes)
+            batch_sampler.batch_size
+            if batch_sampler.split_batches
+            else (batch_sampler.batch_size * batch_sampler.num_processes)
         )
 
     @property
@@ -639,14 +640,17 @@ def prepare_data_loader(
             )
         else:
             # New batch sampler for the current process.
-            if hasattr(dataloader.sampler, "generator"):
-                if dataloader.sampler.generator is None:
-                    dataloader.sampler.generator = torch.Generator()
-                    generator = dataloader.sampler.generator
-                    generator.manual_seed(int(torch.empty((), dtype=torch.int64).random_().item()))
-            elif getattr(dataloader.batch_sampler, "generator", None) is not None:
-                generator = dataloader.batch_sampler.generator
             sampler_is_batch_sampler = isinstance(dataloader.sampler, BatchSampler)
+            if sampler_is_batch_sampler:
+                sampler = dataloader.sampler.sampler
+            else:
+                sampler = dataloader.batch_sampler.sampler
+            if hasattr(sampler, "generator"):
+                if sampler.generator is None:
+                    sampler.generator = torch.Generator()
+                generator = sampler.generator
+                generator.manual_seed(int(torch.empty((), dtype=torch.int64).random_().item()))
+
             batch_sampler = dataloader.sampler if sampler_is_batch_sampler else dataloader.batch_sampler
             new_batch_sampler = BatchSamplerShard(
                 batch_sampler,
@@ -692,6 +696,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=getattr(dataloader, "batch_size", _PYTORCH_DATALOADER_KWARGS["batch_size"]),
             rng_types=rng_types,
             generator=generator,
             **kwargs,


Review comments: body: I think we should use a instance check on the sampler here, as in my tests, there was a `batch_sampler` when using a `BatchSampler` for `sampler`:
```py
batch_sampler = self.sampler if isinstance(self.sampler, BatchSampler) else self.batch_sampler
```
diff_hunk: @@ -364,10 +364,14 @@ def __iter__(self):
 
     @property
     def total_batch_size(self):
+        if self.batch_sampler is None:
+            batch_sampler = self.sampler
+        else:
+            batch_sampler = self.batch_sampler
body: Would reuse the same `batch_size` as for the original `datalaoder` here.
diff_hunk: @@ -692,6 +699,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=None,
body: This is necessary else `batch_sampler` would get created with `batch_size=1` when it shouldn't do that as the `sampler` is already an instance of `batch_sampler`. This line here: https://github.com/pytorch/pytorch/blob/master/torch/utils/data/dataloader.py#L357

This leads to the issue `1.a` specified in the PR description

diff_hunk: @@ -692,6 +699,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=None,
body: But it's not our place to fix users mistakes and they might have wanted to use `batch_size=1` with this for some reason instead of `batch_size=None`.
diff_hunk: @@ -692,6 +699,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=None,
body: Here, the Dataloader default is batch_size=1 and not the one fixed by user. The one fixed by user is used which is part of sampler which is an instance of BatchSampler. When sampler is an object of BatchSampler, batch_sampler shouldn't be created and for that batch_size to dataloader has to be None so that dataloader can use sampler directly to create batches. 
diff_hunk: @@ -692,6 +699,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=None,
body: Yes, `batch_size` should be None, but in the examples of the issue linked above, the user did not do that. We should honor the same argument as was passed to the original dataloader which is saved [here](https://github.com/pytorch/pytorch/blob/d2d145a40001d1e1f815a144160bd0b8d0f60ea0/torch/utils/data/dataloader.py#L361).
diff_hunk: @@ -692,6 +699,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=None,
body: Got it. However, even if the user passes `None` to dataloader, it isn't being passed to the final `DataLoaderShard` as batch_size is part of `ignore_kwargs` and that leads to default value of 1 being used. https://github.com/huggingface/accelerate/blob/main/src/accelerate/data_loader.py#L659
diff_hunk: @@ -692,6 +699,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=None,
body: Yes when there is a `batch_sampler`, because PyTorch raises an error and does not let us do that. But here we only pass a sampler ;-)
diff_hunk: @@ -692,6 +699,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=None,
body: Done.
diff_hunk: @@ -364,10 +364,14 @@ def __iter__(self):
 
     @property
     def total_batch_size(self):
+        if self.batch_sampler is None:
+            batch_sampler = self.sampler
+        else:
+            batch_sampler = self.batch_sampler
body: Done.
diff_hunk: @@ -692,6 +699,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=None,
body: If `(batch_size != 1 and batch_size is not None) and sampler_is_batch_sampler`, is there any "default" way to shard batches without explicit instruction from user? I think at least a warning should be raised.
diff_hunk: @@ -692,6 +696,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=getattr(dataloader, "batch_size", _PYTORCH_DATALOADER_KWARGS["batch_size"]),
body: BTW, `hasattr(dataloader, "batch_size")` is always `True` for `DataLoader`.
diff_hunk: @@ -692,6 +696,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=getattr(dataloader, "batch_size", _PYTORCH_DATALOADER_KWARGS["batch_size"]),
body: > BTW, `hasattr(dataloader, "batch_size")` is always `True` for `DataLoader`.

resolving this in the other PR. wrt warning, current logic follows PyTorch API and as such warning isn't warranted.
diff_hunk: @@ -692,6 +696,7 @@ def prepare_data_loader(
             new_dataset,
             device=device if put_on_device and state.distributed_type != DistributedType.TPU else None,
             sampler=new_batch_sampler,
+            batch_size=getattr(dataloader, "batch_size", _PYTORCH_DATALOADER_KWARGS["batch_size"]),

Discussion: body: _The documentation is not available anymore as the PR was closed or merged._