szha commented on a change in pull request #18025:
URL: https://github.com/apache/incubator-mxnet/pull/18025#discussion_r411729150



##########
File path: conftest.py
##########
@@ -0,0 +1,226 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""conftest.py contains configuration for pytest.
+
+Configuration file for tests in tests/ and scripts/ folders.
+
+Note that fixtures of higher-scoped fixtures (such as ``session``) are
+instantiated before lower-scoped fixtures (such as ``function``).
+
+"""
+
+import logging
+import os
+import random
+
+import pytest
+
+
+def pytest_sessionfinish(session, exitstatus):
+    if exitstatus == 5:  # Don't fail if no tests were run
+        session.exitstatus = 0
+
+
+# * Random seed setup
+def pytest_configure():
+    """Pytest configuration hook to help reproduce test segfaults
+
+    Sets and outputs rng seeds.
+
+    The segfault-debug procedure on a module called test_module.py is:
+
+    1. run "pytest --verbose test_module.py".  A seg-faulting output might be:
+
+       [INFO] np, mx and python random seeds = 4018804151
+       test_module.test1 ... ok
+       test_module.test2 ... Illegal instruction (core dumped)
+
+    2. Copy the module-starting seed into the next command, then run:
+
+       MXNET_MODULE_SEED=4018804151 pytest --log-level=DEBUG --verbose 
test_module.py
+
+       Output might be:
+
+       [WARNING] **** module-level seed is set: all tests running 
deterministically ****
+       [INFO] np, mx and python random seeds = 4018804151
+       test_module.test1 ... [DEBUG] np and mx random seeds = 3935862516
+       ok
+       test_module.test2 ... [DEBUG] np and mx random seeds = 1435005594
+       Illegal instruction (core dumped)
+
+    3. Copy the segfaulting-test seed into the command:
+       MXNET_TEST_SEED=1435005594 pytest --log-level=DEBUG --verbose 
test_module.py:test2
+       Output might be:
+
+       [INFO] np, mx and python random seeds = 2481884723
+       test_module.test2 ... [DEBUG] np and mx random seeds = 1435005594
+       Illegal instruction (core dumped)
+
+    3. Finally reproduce the segfault directly under gdb (might need 
additional os packages)
+       by editing the bottom of test_module.py to be
+
+       if __name__ == '__main__':
+           logging.getLogger().setLevel(logging.DEBUG)
+           test2()
+
+       MXNET_TEST_SEED=1435005594 gdb -ex r --args python test_module.py
+
+    4. When finished debugging the segfault, remember to unset any exported 
MXNET_ seed
+       variables in the environment to return to non-deterministic testing (a 
good thing).
+    """
+
+    module_seed_str = os.getenv('MXNET_MODULE_SEED')
+    if module_seed_str is None:
+        seed = random.randint(0, 2**31-1)
+    else:
+        seed = int(module_seed_str)
+        logging.warning('*** module-level seed is set: '
+                        'all tests running deterministically ***')
+    print('Setting module np/mx/python random seeds, '
+          'use MXNET_MODULE_SEED={} to reproduce.'.format(seed))
+
+    random.seed(seed)
+    try:
+        import numpy as np
+        import mxnet as mx
+        np.random.seed(seed)
+        mx.random.seed(seed)
+    except:
+        print('Unable to import numpy/mxnet. Skipping conftest.')

Review comment:
       switched all prints to logging.info/warning




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to