Repository: pytorch/ignite

Issue:
Make epoch_length optional
## 🚀 Feature

Following discussions and the message: https://github.com/pytorch/ignite/issues/853#issuecomment-606732951 it could be useful to make `epoch_length` optional for finite iterators.




Discussion:
@vfdev-5 thanks for opening this issue. I'll be following on the progress. It's currently a blocker for me to move to 0.4.0. Once this is added, I can migrate.
Any update on this?
@snie2012 this is currently blocked by https://github.com/pytorch/ignite/pull/895

Once it is merged I can try integrate this option `epoch_lenght=None` for iterators and ensure that all tests are passing.

Thanks for asking.
@vfdev-5 Thanks for the quick response. Looking forward to the upcoming updates!

Resolving patch:
diff --git a/ignite/engine/engine.py b/ignite/engine/engine.py
--- a/ignite/engine/engine.py
+++ b/ignite/engine/engine.py
@@ -554,7 +554,8 @@ def run(
                 If a new state should be created (first run or run again from ended engine), it's default value is 1.
                 This argument should be `None` if run is resuming from a state.
             epoch_length (int, optional): Number of iterations to count as one epoch. By default, it can be set as
-                `len(data)`. If `data` is an iterator and `epoch_length` is not set, an error is raised.
+                `len(data)`. If `data` is an iterator and `epoch_length` is not set, then it will be automatically
+                determined as the iteration on which data iterator raises `StopIteration`.
                 This argument should be `None` if run is resuming from a state.
             seed (int, optional): Deprecated argument. Please, use `torch.manual_seed` or
                 :meth:`~ignite.utils.manual_seed`.
@@ -590,8 +591,7 @@ def switch_batch(engine):
                     epoch_length = len(data)
                     if epoch_length < 1:
                         raise ValueError("Input data has zero size. Please provide non-empty data")
-                else:
-                    raise ValueError("Argument `epoch_length` should be defined if `data` is an iterator")
+
             self.state = State(iteration=0, epoch=0, max_epochs=max_epochs, epoch_length=epoch_length)
             self.logger.info("Engine run starting with max_epochs={}.".format(max_epochs))
         else:
@@ -694,6 +694,7 @@ def _run_once_on_dataset(self) -> Tuple[int, int, int]:
                     iter_counter += 1
                     should_exit = False
                 except StopIteration:
+
                     if self._dataloader_len is None:
                         if iter_counter > 0:
                             self._dataloader_len = iter_counter
@@ -701,6 +702,12 @@ def _run_once_on_dataset(self) -> Tuple[int, int, int]:
                             # this can happen when data is finite iterator and epoch_length is equal to its size
                             self._dataloader_len = self.state.iteration
 
+                    # Define self.state.epoch_length if it is not yet set
+                    if self.state.epoch_length is None:
+                        # Define epoch length and stop the epoch
+                        self.state.epoch_length = iter_counter
+                        break
+
                     # Should exit while loop if we can not iterate
                     if should_exit:
                         if not self._is_done(self.state):
@@ -734,7 +741,7 @@ def _run_once_on_dataset(self) -> Tuple[int, int, int]:
                     self._dataloader_iter = iter(self.state.dataloader)
                     break
 
-                if iter_counter == self.state.epoch_length:
+                if self.state.epoch_length is not None and iter_counter == self.state.epoch_length:
                     break
 
         except Exception as e:


Test patch:
diff --git a/tests/ignite/engine/__init__.py b/tests/ignite/engine/__init__.py
--- a/tests/ignite/engine/__init__.py
+++ b/tests/ignite/engine/__init__.py
@@ -10,7 +10,8 @@ def __init__(self, data, init_counter=0):
     def check(self, batch):
         self.true_batch = self.data[self.counter % len(self.data)]
         self.counter += 1
-        return (self.true_batch == batch).all()
+        res = self.true_batch == batch
+        return res.all() if not isinstance(res, bool) else res
 
 
 class IterationCounter:
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
@@ -10,7 +10,7 @@
 from ignite.engine.deterministic import keep_random_state
 from ignite.metrics import Average
 
-from tests.ignite.engine import IterationCounter, EpochCounter
+from tests.ignite.engine import IterationCounter, EpochCounter, BatchChecker
 
 
 def test_terminate():
@@ -341,9 +341,6 @@ def test_run_asserts():
     with pytest.raises(ValueError, match=r"Input data has zero size. Please provide non-empty data"):
         engine.run([])
 
-    with pytest.raises(ValueError, match=r"Argument `epoch_length` should be defined if `data` is an iterator"):
-        engine.run(iter([0, 1, 2, 3]))
-
     with pytest.warns(UserWarning, match="Argument seed is deprecated"):
         engine.run([0, 1, 2, 3, 4], seed=1234)
 
@@ -638,3 +635,235 @@ def foo(e, b):
     engine.run(data_loader, epoch_length=10, max_epochs=5)
 
     assert counter[0] == 50
+
+
+def test_run_once_finite_iterator_no_epoch_length():
+    # FR: https://github.com/pytorch/ignite/issues/871
+
+    unknown_size = 11
+
+    def finite_unk_size_data_iter():
+        for i in range(unknown_size):
+            yield i
+
+    bc = BatchChecker(data=list(range(unknown_size)))
+
+    engine = Engine(lambda e, b: bc.check(b))
+
+    completed_handler = MagicMock()
+    engine.add_event_handler(Events.COMPLETED, completed_handler)
+
+    data_iter = finite_unk_size_data_iter()
+    engine.run(data_iter)
+
+    assert engine.state.epoch == 1
+    assert engine.state.iteration == unknown_size
+    assert completed_handler.call_count == 1
+
+
+def test_run_finite_iterator_no_epoch_length():
+    # FR: https://github.com/pytorch/ignite/issues/871
+    unknown_size = 11
+
+    def finite_unk_size_data_iter():
+        for i in range(unknown_size):
+            yield i
+
+    bc = BatchChecker(data=list(range(unknown_size)))
+
+    engine = Engine(lambda e, b: bc.check(b))
+
+    @engine.on(Events.DATALOADER_STOP_ITERATION)
+    def restart_iter():
+        engine.state.dataloader = finite_unk_size_data_iter()
+
+    data_iter = finite_unk_size_data_iter()
+    engine.run(data_iter, max_epochs=5)
+
+    assert engine.state.epoch == 5
+    assert engine.state.iteration == unknown_size * 5
+
+
+def test_run_finite_iterator_no_epoch_length_2():
+    # FR: https://github.com/pytorch/ignite/issues/871
+    known_size = 11
+
+    def finite_size_data_iter(size):
+        for i in range(size):
+            yield i
+
+    bc = BatchChecker(data=list(range(known_size)))
+
+    engine = Engine(lambda e, b: bc.check(b))
+
+    @engine.on(Events.ITERATION_COMPLETED(every=known_size))
+    def restart_iter():
+        engine.state.dataloader = finite_size_data_iter(known_size)
+
+    data_iter = finite_size_data_iter(known_size)
+    engine.run(data_iter, max_epochs=5)
+
+    assert engine.state.epoch == 5
+    assert engine.state.iteration == known_size * 5
+
+
+def test_faq_inf_iterator_with_epoch_length():
+    # Code snippet from FAQ
+
+    import torch
+
+    torch.manual_seed(12)
+
+    def infinite_iterator(batch_size):
+        while True:
+            batch = torch.rand(batch_size, 3, 32, 32)
+            yield batch
+
+    def train_step(trainer, batch):
+        # ...
+        s = trainer.state
+        print("{}/{} : {} - {:.3f}".format(s.epoch, s.max_epochs, s.iteration, batch.norm()))
+
+    trainer = Engine(train_step)
+    # We need to specify epoch_length to define the epoch
+    trainer.run(infinite_iterator(4), epoch_length=5, max_epochs=3)
+
+    assert trainer.state.epoch == 3
+    assert trainer.state.iteration == 3 * 5
+
+
+def test_faq_inf_iterator_no_epoch_length():
+    # Code snippet from FAQ
+
+    import torch
+
+    torch.manual_seed(12)
+
+    def infinite_iterator(batch_size):
+        while True:
+            batch = torch.rand(batch_size, 3, 32, 32)
+            yield batch
+
+    def train_step(trainer, batch):
+        # ...
+        s = trainer.state
+        print("{}/{} : {} - {:.3f}".format(s.epoch, s.max_epochs, s.iteration, batch.norm()))
+
+    trainer = Engine(train_step)
+
+    @trainer.on(Events.ITERATION_COMPLETED(once=15))
+    def stop_training():
+        trainer.terminate()
+
+    trainer.run(infinite_iterator(4))
+
+    assert trainer.state.epoch == 1
+    assert trainer.state.iteration == 15
+
+
+def test_faq_fin_iterator_unknw_size():
+    # Code snippet from FAQ
+
+    import torch
+
+    torch.manual_seed(12)
+
+    def finite_unk_size_data_iter():
+        for i in range(11):
+            yield i
+
+    def train_step(trainer, batch):
+        # ...
+        s = trainer.state
+        print("{}/{} : {} - {:.3f}".format(s.epoch, s.max_epochs, s.iteration, batch))
+
+    trainer = Engine(train_step)
+
+    @trainer.on(Events.DATALOADER_STOP_ITERATION)
+    def restart_iter():
+        trainer.state.dataloader = finite_unk_size_data_iter()
+
+    data_iter = finite_unk_size_data_iter()
+    trainer.run(data_iter, max_epochs=5)
+
+    assert trainer.state.epoch == 5
+    assert trainer.state.iteration == 5 * 11
+
+    # # # # #
+
+    import torch
+
+    torch.manual_seed(12)
+
+    def finite_unk_size_data_iter():
+        for i in range(11):
+            yield i
+
+    def val_step(evaluator, batch):
+        # ...
+        s = evaluator.state
+        print("{}/{} : {} - {:.3f}".format(s.epoch, s.max_epochs, s.iteration, batch))
+
+    evaluator = Engine(val_step)
+
+    data_iter = finite_unk_size_data_iter()
+    evaluator.run(data_iter)
+
+    assert evaluator.state.epoch == 1
+    assert evaluator.state.iteration == 1 * 11
+
+
+def test_faq_fin_iterator():
+    # Code snippet from FAQ
+
+    import torch
+
+    torch.manual_seed(12)
+
+    size = 11
+
+    def finite_size_data_iter(size):
+        for i in range(size):
+            yield i
+
+    def train_step(trainer, batch):
+        # ...
+        s = trainer.state
+        print("{}/{} : {} - {:.3f}".format(s.epoch, s.max_epochs, s.iteration, batch))
+
+    trainer = Engine(train_step)
+
+    @trainer.on(Events.ITERATION_COMPLETED(every=size))
+    def restart_iter():
+        trainer.state.dataloader = finite_size_data_iter(size)
+
+    data_iter = finite_size_data_iter(size)
+    trainer.run(data_iter, max_epochs=5)
+
+    assert trainer.state.epoch == 5
+    assert trainer.state.iteration == 5 * size
+
+    # # # # #
+
+    import torch
+
+    torch.manual_seed(12)
+
+    size = 11
+
+    def finite_size_data_iter(size):
+        for i in range(size):
+            yield i
+
+    def val_step(evaluator, batch):
+        # ...
+        s = evaluator.state
+        print("{}/{} : {} - {:.3f}".format(s.epoch, s.max_epochs, s.iteration, batch))
+
+    evaluator = Engine(val_step)
+
+    data_iter = finite_size_data_iter(size)
+    evaluator.run(data_iter)
+
+    assert evaluator.state.epoch == 1
+    assert evaluator.state.iteration == size