code: def test_custom_torch_batch_sampler_doppelganger(predicting):
      """Test we can reinstantiate a sampler that mimics PyTorch's BatchSampler even if it does not inherit from it.
  
      This is only possible if that sampler accepts the `batch_size` and `drop_last` arguments, and stores them
      as attributes.
  
      """
  
      class BatchSamplerDoppelganger:
          """A batch sampler that mimics `torch.utils.data.BatchSampler` but does not inherit from it."""
  
          def __init__(self, sampler, batch_size, drop_last):
              self.sampler = sampler
              self.batch_size = batch_size
              self.drop_last = drop_last
  
          def __iter__(self):
              while True:
                  yield [0, 1, 2, 3]
  
          def __len__(self) -> int:
              return 4
  
      batch_sampler = BatchSamplerDoppelganger(
          sampler=Mock(), batch_size=2, drop_last=True
      )
      dataloader = DataLoader(range(100), batch_sampler=batch_sampler)
      new_sampler = Mock()
      dataloader = _update_dataloader(
          dataloader,
          sampler=new_sampler,
          mode=(RunningStage.PREDICTING if predicting else None),
      )
  
      batch_sampler = dataloader.batch_sampler
  
      if predicting:
          assert isinstance(batch_sampler, _IndexBatchSamplerWrapper)
          batch_sampler = batch_sampler._batch_sampler
  
      assert isinstance(batch_sampler, BatchSamplerDoppelganger)
      assert batch_sampler.sampler == new_sampler
      assert batch_sampler.drop_last == (not predicting)
  
imports: from dataclasses import dataclass
  from unittest.mock import Mock
  import numpy
  import pytest
  import torch
  from lightning.fabric.utilities.data import _replace_dunder_methods
  from lightning.pytorch import Trainer
  from lightning.pytorch.demos.boring_classes import RandomDataset, RandomIterableDataset
  from lightning.pytorch.overrides.distributed import _IndexBatchSamplerWrapper
  from lightning.pytorch.trainer.states import RunningStage
  from lightning.pytorch.utilities.data import _get_dataloader_init_args_and_kwargs, _update_dataloader, extract_batch_size, has_len_all_ranks, warning_cache
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning_utilities.test.warning import no_warning_call
  from torch import Tensor
  from torch.utils.data import BatchSampler, DataLoader, RandomSampler
label_desc: Use image node_id 5 for calling a global function with example usage: test_custom_torch_batch_sampler_doppelganger(predicting) and returns: int