Repository: pytorch/ignite

Issue:
Issue with DataLoader with _dataset_kind = Iterable
The following code wont work:
```python
import torch

class MyIterableDataset(torch.utils.data.IterableDataset):
    def __init__(self, start, end):
        super(MyIterableDataset).__init__()
        assert end > start, "this example code only works with end >= start"
        self.start = start
        self.end = end

    def __iter__(self):
        return iter(range(self.start, self.end))

ds = MyIterableDataset(0, 1000)
data_loader = torch.utils.data.DataLoader(ds, num_workers=2)

from ignite.engine import Engine

def foo(e, b):
    print("{}-{}: {}".format(e.state.epoch, e.state.iteration, b))

engine = Engine(foo)
engine.run(data_loader, epoch_length=10)
```
and gives the error
```


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>
```

Patching it like [here](https://github.com/pytorch/ignite/issues/618#issuecomment-577183132) can be a workaround:
```python
engine.run(map(lambda x: x, data_loader), epoch_length=10)
```


Discussion:

Resolving patch:
diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py
--- a/ignite/engine/engine.py
+++ b/ignite/engine/engine.py
@@ -863,19 +863,20 @@ def _setup_engine(self):
         # 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))
+            _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, self.state.epoch_length)
+        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:
@@ -883,12 +884,17 @@ def _setup_engine(self):
         self._init_iter.append(iteration)
 
     @staticmethod
-    def _from_iteration(data, iteration, epoch_length):
+    def _from_iteration(data, iteration):
         if isinstance(data, torch.utils.data.DataLoader):
-            iteration %= len(data.batch_sampler)
-            if iteration > 0:
-                # batch sampler is ReproducibleBatchSampler
-                data.batch_sampler.start_iteration = iteration
+            try:
+                # following is unsafe for IterableDatasets
+                iteration %= len(data.batch_sampler)
+                if iteration > 0:
+                    # batch sampler is ReproducibleBatchSampler
+                    data.batch_sampler.start_iteration = iteration
+            except TypeError:
+                # Probably we can do nothing with DataLoader built upon IterableDatasets
+                pass
             data_iter = iter(data)
         else:
             if hasattr(data, "__len__"):


Test patch:
diff --git a/tests/ignite/engine/test_engine.py b/tests/ignite/engine/test_engine.py
--- a/tests/ignite/engine/test_engine.py
+++ b/tests/ignite/engine/test_engine.py
@@ -496,3 +496,30 @@ def test_multinode_distrib_cpu(distributed_context_multi_node_gloo):
 def test_multinode_distrib_gpu(distributed_context_multi_node_nccl):
     _test_run_check_triggered_events_on_iterator()
     _test_run_check_triggered_events()
+
+
+def test_engine_with_iterable_dataloader():
+
+    class MyIterableDataset(torch.utils.data.IterableDataset):
+        def __init__(self, start, end):
+            super(MyIterableDataset).__init__()
+            assert end > start, "this example code only works with end >= start"
+            self.start = start
+            self.end = end
+
+        def __iter__(self):
+            return iter(range(self.start, self.end))
+
+    ds = MyIterableDataset(0, 1000)
+    data_loader = torch.utils.data.DataLoader(ds, num_workers=2)
+
+    counter = [0]
+
+    def foo(e, b):
+        print("{}-{}: {}".format(e.state.epoch, e.state.iteration, b))
+        counter[0] += 1
+
+    engine = Engine(foo)
+    engine.run(data_loader, epoch_length=10, max_epochs=5)
+
+    assert counter[0] == 50