# Buggy code
def _setup_engine(self):
    try:
        self._dataloader_len = (
            len(self.state.dataloader)
            if hasattr(self.state.dataloader, "__len__")
            else None
        )
    except TypeError:
        # _InfiniteConstantSampler can raise a TypeError on DataLoader length of a IterableDataset
        self._dataloader_len = None

    # setup seed here, as iter(data) can start prefetching
    self.setup_seed()

    # if input data is torch dataloader we replace batch sampler by a batch sampler
    # such that its random sampling indices are reproducible by prefetching them before data iteration
    if isinstance(self.state.dataloader, torch.utils.data.DataLoader):
        if (self._dataloader_len is not None) and hasattr(
            self.state.dataloader.sampler, "epoch"
        ):
            if self._dataloader_len != self.state.epoch_length:
                warnings.warn(
                    "When defined engine's epoch length is different of input dataloader length, "
                    "distributed sampler indices can not be setup in a reproducible manner"
                )

        batch_sampler = self.state.dataloader.batch_sampler
        if not isinstance(batch_sampler, ReproducibleBatchSampler):
            self.state.dataloader = _update_dataloader(
                self.state.dataloader, ReproducibleBatchSampler(batch_sampler)
            )

    iteration = self.state.iteration
    self._dataloader_iter = self._from_iteration(
        self.state.dataloader, iteration, self.state.epoch_length
    )

    # Below we define initial counter value for _run_once_on_dataset to measure a single epoch
    if self.state.epoch_length is not None:
        iteration %= self.state.epoch_length
    self._init_iter.append(iteration)


# Traceback (ValueError)
ValueErrorTraceback (most recent call last)
<ipython-input-19-1c8004fbf46e> in <module>
21
22 engine = Engine(foo)
---> 23 engine.run(data_loader, epoch_length=10)

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in run(self, data, max_epochs, epoch_length, seed)
848
849         self.state.dataloader = data
--> 850         return self._internal_run()
851
852     def _setup_engine(self):

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in _internal_run(self)
950             self._dataloader_iter = self._dataloader_len = None
951             self.logger.error("Engine run is terminating due to exception: %s.", str(e))
--> 952             self._handle_exception(e)
953
954         self._dataloader_iter = self._dataloader_len = None

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in _handle_exception(self, e)
714             self._fire_event(Events.EXCEPTION_RAISED, e)
715         else:
--> 716             raise e
717
718     def state_dict(self):

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in _internal_run(self)
933
934                 if self._dataloader_iter is None:
--> 935                     self._setup_engine()
936
937                 hours, mins, secs = self._run_once_on_dataset()

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in _setup_engine(self)
873             if not isinstance(batch_sampler, ReproducibleBatchSampler):
874                 self.state.dataloader = _update_dataloader(self.state.dataloader,
--> 875                                                            ReproducibleBatchSampler(batch_sampler))
876
877         iteration = self.state.iteration

/opt/conda/lib/python3.7/site-packages/ignite/engine/engine.py in _update_dataloader(dataloader, new_batch_sampler)
963     params = {k: getattr(dataloader, k) for k in params_keys}
964     params['batch_sampler'] = new_batch_sampler
--> 965     return torch.utils.data.DataLoader(**params)
966
967

/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __init__(self, dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn, multiprocessing_context)
182                 raise ValueError(
183                     "DataLoader with IterableDataset: expected unspecified "
--> 184                     "batch_sampler option, but got batch_sampler={}".format(batch_sampler))
185         else:
186             self._dataset_kind = _DatasetKind.Map

ValueError: DataLoader with IterableDataset: expected unspecified batch_sampler option, but got batch_sampler=<ignite.engine.engine.ReproducibleBatchSampler object at 0x7f6000e3fad0>

# Fixed code
def _setup_engine(self):
    try:
        self._dataloader_len = (
            len(self.state.dataloader)
            if hasattr(self.state.dataloader, "__len__")
            else None
        )
    except TypeError:
        # _InfiniteConstantSampler can raise a TypeError on DataLoader length of a IterableDataset
        self._dataloader_len = None

    # setup seed here, as iter(data) can start prefetching
    self.setup_seed()

    # if input data is torch dataloader we replace batch sampler by a batch sampler
    # such that its random sampling indices are reproducible by prefetching them before data iteration
    if isinstance(self.state.dataloader, torch.utils.data.DataLoader):
        _dataloader_kind = self.state.dataloader._dataset_kind
        if _dataloader_kind == torch.utils.data.dataloader._DatasetKind.Map:
            if (self._dataloader_len is not None) and hasattr(
                self.state.dataloader.sampler, "epoch"
            ):
                if self._dataloader_len != self.state.epoch_length:
                    warnings.warn(
                        "When defined engine's epoch length is different of input dataloader length, "
                        "distributed sampler indices can not be setup in a reproducible manner"
                    )

            batch_sampler = self.state.dataloader.batch_sampler
            if not isinstance(batch_sampler, ReproducibleBatchSampler):
                self.state.dataloader = _update_dataloader(
                    self.state.dataloader, ReproducibleBatchSampler(batch_sampler)
                )

    iteration = self.state.iteration
    self._dataloader_iter = self._from_iteration(self.state.dataloader, iteration)

    # Below we define initial counter value for _run_once_on_dataset to measure a single epoch
    if self.state.epoch_length is not None:
        iteration %= self.state.epoch_length
    self._init_iter.append(iteration)