marcoabreu commented on a change in pull request #9791: CI test randomness 3
URL: https://github.com/apache/incubator-mxnet/pull/9791#discussion_r168135862
 
 

 ##########
 File path: tests/python/unittest/common.py
 ##########
 @@ -30,3 +35,190 @@ def assertRaises(expected_exception, func, *args, 
**kwargs):
     else:
         # Did not raise exception
         assert False, "%s did not raise %s" % (func.__name__, 
expected_exception.__name__)
+
+def default_logger():
+    """A logger used to output seed information to nosetests logs."""
+    logger = logging.getLogger(__name__)
+    # getLogger() lookups will return the same logger, but only add the 
handler once.
+    if not len(logger.handlers):
+        handler = logging.StreamHandler(sys.stderr)
+        handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
+        logger.addHandler(handler)
+        if (logger.getEffectiveLevel() == logging.NOTSET):
+            logger.setLevel(logging.INFO)
+    return logger
+
+@contextmanager
+def random_seed(seed=None):
+    """
+    Runs a code block with a new seed for np, mx and python's random.
+
+    Parameters
+    ----------
+
+    seed : the seed to pass to np.random, mx.random and python's random.
+
+    To impose rng determinism, invoke e.g. as in:
+
+    with random_seed(1234):
+        ...
+
+    To impose rng non-determinism, invoke as in:
+
+    with random_seed():
+        ...
+
+    Upon conclusion of the block, the rng's are returned to
+    a state that is a function of their pre-block state, so
+    any prior non-determinism is preserved.
+
+    """
+
+    try:
+        next_seed = np.random.randint(0, np.iinfo(np.int32).max)
 
 Review comment:
   Could you elaborate how this restores the pre-block state? To me this looks 
like it will always pick a new random seed after this block finishes. I rather 
expected that the old sequence generator is being restored. Imagine two 
sequences:
   ``1: 100 -> 101 -> 102 -> ...``
   ``2: 200 -> 201 -> 202 -> ...``
   
   What I would expect is something along the lines of:
   ```
   100
   101
   START: [random_seed(seq_2)]
   200
   201
   202
   END: [random_seed(seq_2)]
   102
   103
   ...
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to