Repository: pytorch/ignite

Issue:
Replace conda install in github CI by actions/setup-python@v2
## 🚀 Feature

In order to accelerate env and dependencies installation we can opt to replace:
https://github.com/pytorch/ignite/blob/cc78b3bbd764da9ae7dee0df21676b2b8a50fba8/.github/workflows/unittests.yml#L19-L25
by 
https://github.com/pytorch/ignite/blob/cc78b3bbd764da9ae7dee0df21676b2b8a50fba8/.github/workflows/tpu-tests.yml#L14-L19

with correct python version


Discussion:

Resolving patch:
diff --git a/ignite/contrib/handlers/neptune_logger.py b/ignite/contrib/handlers/neptune_logger.py
--- a/ignite/contrib/handlers/neptune_logger.py
+++ b/ignite/contrib/handlers/neptune_logger.py
@@ -578,7 +578,9 @@ def __call__(self, checkpoint: Mapping, filename: str, metadata: Optional[Mappin
         # wont work on XLA
 
         with tempfile.NamedTemporaryFile() as tmp:
-            torch.save(checkpoint, tmp.name)
+            # we can not use tmp.name to open tmp.file twice on Win32
+            # https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
+            torch.save(checkpoint, tmp.file)
             self._logger.log_artifact(tmp.name, filename)
 
     @idist.one_rank_only(with_barrier=True)


Test patch:
diff --git a/tests/ignite/contrib/engines/test_common.py b/tests/ignite/contrib/engines/test_common.py
--- a/tests/ignite/contrib/engines/test_common.py
+++ b/tests/ignite/contrib/engines/test_common.py
@@ -340,7 +340,7 @@ def test_setup_tb_logging(dirname):
     tb_logger.close()
     tb_logger = _test_setup_logging(
         setup_logging_fn=setup_tb_logging,
-        kwargs_dict={"output_path": os.path.join(dirname, "t2")},
+        kwargs_dict={"output_path": os.path.join(dirname, "t3")},
         output_handler_cls=handlers.tensorboard_logger.OutputHandler,
         opt_params_handler_cls=handlers.tensorboard_logger.OptimizerParamsHandler,
         with_eval=True,
diff --git a/tests/ignite/contrib/handlers/test_time_profilers.py b/tests/ignite/contrib/handlers/test_time_profilers.py
--- a/tests/ignite/contrib/handlers/test_time_profilers.py
+++ b/tests/ignite/contrib/handlers/test_time_profilers.py
@@ -1,11 +1,16 @@
 import os
+import sys
 import time
 
+import pytest
 from pytest import approx
 
 from ignite.contrib.handlers.time_profilers import BasicTimeProfiler
 from ignite.engine import Engine, Events
 
+if sys.platform.startswith("darwin"):
+    pytest.skip("Skip if on MacOS", allow_module_level=True)
+
 
 def _do_nothing_update_fn(engine, batch):
     pass
diff --git a/tests/ignite/contrib/handlers/test_tqdm_logger.py b/tests/ignite/contrib/handlers/test_tqdm_logger.py
--- a/tests/ignite/contrib/handlers/test_tqdm_logger.py
+++ b/tests/ignite/contrib/handlers/test_tqdm_logger.py
@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+import sys
 import time
 
 import numpy as np
@@ -10,6 +11,9 @@
 from ignite.handlers import TerminateOnNan
 from ignite.metrics import RunningAverage
 
+if sys.platform.startswith("win"):
+    pytest.skip("Skip if on Windows", allow_module_level=True)
+
 
 def update_fn(engine, batch):
     a = 1
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
@@ -1,5 +1,13 @@
 import torch
 
+try:
+    from torch.utils.data import IterableDataset
+except ImportError:
+
+    class IterableDataset:
+
+        pass
+
 
 class BatchChecker:
     def __init__(self, data, init_counter=0):
@@ -56,3 +64,18 @@ def setup_sampler(sampler_type, num_iters, batch_size):
 
         dataset = torch.zeros(num_iters * batch_size)
         return DistributedSampler(dataset, num_replicas=num_replicas, rank=rank), batch_size // num_replicas
+
+
+class MyIterableDataset(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))
+
+
+def get_iterable_dataset(*args, **kwargs):
+    return MyIterableDataset(*args, **kwargs)
diff --git a/tests/ignite/engine/test_deterministic.py b/tests/ignite/engine/test_deterministic.py
--- a/tests/ignite/engine/test_deterministic.py
+++ b/tests/ignite/engine/test_deterministic.py
@@ -1,5 +1,6 @@
 import os
 import random
+import sys
 from unittest.mock import patch
 
 import numpy as np
@@ -249,7 +250,7 @@ def _test(epoch_length=None):
         if epoch_length is None:
             epoch_length = num_iters
 
-        for resume_epoch in range(1, max_epochs):
+        for resume_epoch in range(1, max_epochs, 2):
 
             for num_workers in [0, 4]:
                 sampler, batch_size = _setup_sampler(sampler_type, num_iters, total_batch_size)
@@ -325,6 +326,7 @@ def _(engine):
         _test(15)
 
 
+@pytest.mark.skipif("win" in sys.platform, reason="Skip extremely slow test on Windows/MacOSX")
 def test_resume_random_dataloader_from_epoch():
     _test_resume_random_dataloader_from_epoch("cpu", setup_sampler)
     _test_resume_random_dataloader_from_epoch("cpu", setup_sampler, sampler_type="weighted")
@@ -356,7 +358,7 @@ def _test(epoch_length=None):
         if epoch_length is None:
             epoch_length = num_iters
 
-        for resume_iteration in range(2, min(num_iters * max_epochs, epoch_length * max_epochs), 7):
+        for resume_iteration in range(2, min(num_iters * max_epochs, epoch_length * max_epochs), 13):
 
             for num_workers in [0, 4]:
 
@@ -433,6 +435,7 @@ def _(engine):
         _test(11)
 
 
+@pytest.mark.skipif("win" in sys.platform, reason="Skip extremely slow test on Windows/MacOSX")
 def test_resume_random_dataloader_from_iter():
     _test_resume_random_dataloader_from_iter("cpu", setup_sampler)
     _test_resume_random_dataloader_from_iter("cpu", setup_sampler, sampler_type="weighted")
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
@@ -1,5 +1,6 @@
 import os
 import time
+from distutils.version import LooseVersion
 from unittest.mock import MagicMock, Mock, call
 
 import numpy as np
@@ -10,7 +11,7 @@
 from ignite.engine import Engine, Events, State
 from ignite.engine.deterministic import keep_random_state
 from ignite.metrics import Average
-from tests.ignite.engine import BatchChecker, EpochCounter, IterationCounter
+from tests.ignite.engine import BatchChecker, EpochCounter, IterationCounter, get_iterable_dataset
 
 
 def test_terminate():
@@ -513,26 +514,16 @@ def test_multinode_distrib_gpu(distributed_context_multi_node_nccl):
     _test_run_check_triggered_events()
 
 
+@pytest.mark.skipif(LooseVersion(torch.__version__) < LooseVersion("1.2.0"), reason="No IterableDataset in torch<1.2.0")
 def test_engine_with_iterable_dataloader():
 
-    from torch.utils.data import DataLoader, IterableDataset
-
-    # This can not be pickled on windows
-    class MyIterableDataset(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))
+    from torch.utils.data import DataLoader
 
     def _test(epoch_length=None):
 
         le = 50
         num_workers = 4
-        ds = MyIterableDataset(0, le)
+        ds = get_iterable_dataset(0, le)
         data_loader = DataLoader(ds, num_workers=num_workers)
 
         counter = [0]
diff --git a/tests/ignite/handlers/test_timing.py b/tests/ignite/handlers/test_timing.py
--- a/tests/ignite/handlers/test_timing.py
+++ b/tests/ignite/handlers/test_timing.py
@@ -1,8 +1,14 @@
+import sys
 import time
 
+import pytest
+
 from ignite.engine import Engine, Events
 from ignite.handlers import Timer
 
+if sys.platform.startswith("darwin"):
+    pytest.skip("Skip if on MacOS", allow_module_level=True)
+
 
 def test_timer():
     sleep_t = 0.2
diff --git a/tests/ignite/metrics/test_frequency.py b/tests/ignite/metrics/test_frequency.py
--- a/tests/ignite/metrics/test_frequency.py
+++ b/tests/ignite/metrics/test_frequency.py
@@ -1,4 +1,5 @@
 import os
+import sys
 import time
 
 import pytest
@@ -7,7 +8,11 @@
 from ignite.engine import Engine, Events
 from ignite.metrics import Frequency
 
+if sys.platform.startswith("darwin"):
+    pytest.skip("Skip if on MacOS", allow_module_level=True)
 
+
+@pytest.mark.skipif(sys.platform.startswith("win"), reason="Skip on Windows")
 def test_nondistributed_average():
     artificial_time = 1  # seconds
     num_tokens = 100
@@ -49,6 +54,8 @@ def assert_wps(e):
     engine.run(data, max_epochs=1)
 
 
+@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="Skip on MacOS")
+@pytest.mark.skipif(sys.platform.startswith("win"), reason="Skip on Windows")
 def test_frequency_with_engine():
     device = "cpu"
     _test_frequency_with_engine(device, workers=1)
diff --git a/tests/ignite/test_utils.py b/tests/ignite/test_utils.py
--- a/tests/ignite/test_utils.py
+++ b/tests/ignite/test_utils.py
@@ -113,3 +113,6 @@ def _(_):
     for source in [err, data]:
         assert "trainer INFO: Engine run starting with max_epochs=5." in source[0]
         assert "evaluator INFO: Engine run starting with max_epochs=1." in source[1]
+
+    # Needed by windows to release FileHandler in the loggers
+    logging.shutdown()
diff --git a/tests/run_cpu_tests.sh b/tests/run_cpu_tests.sh
--- a/tests/run_cpu_tests.sh
+++ b/tests/run_cpu_tests.sh
@@ -4,6 +4,11 @@ set -xeu
 
 CUDA_VISIBLE_DEVICES="" py.test --tx 4*popen//python=python$CI_PYTHON_VERSION --cov ignite --cov-report term-missing -vvv tests/
 
+# https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02
+if [ "${SKIP_DISTRIB_TESTS:-0}" -eq "1" ]; then
+    exit 0
+fi
+
 export WORLD_SIZE=2
 
 CUDA_VISIBLE_DEVICES="" py.test --cov ignite --cov-append --cov-report term-missing --dist=each --tx $WORLD_SIZE*popen//python=python$CI_PYTHON_VERSION tests -m distributed -vvv