Skip to content

Commit 3253a28

Browse files
authored
Reseed the Cortex-M test RNG before each test module is imported (pytorch#21794)
### Summary Model tests that build calibration data or example inputs at module scope draw from the global RNG during pytest collection, not during the test. Seeding only in pytest_configure therefore left those draws dependent on how many other modules pytest happened to import first, so a test's quantization could change purely because a file was added elsewhere in the directory. Adding a test_silero_vad import ahead of test_mobilenet_v2 moves mv2's first calibration sample sum from -310.9496 to -42.2950; the reverse order moves silero's input sum from 15.9070 to 7.8727. Reseeding in pytest_collectstart makes both stable, and leaves the existing per-test autouse fixture untouched. The TEST_RUNTIME_IS_NOT_OSS guard mirrors the one already in pytest_configure, which in turn follows backends/arm/test/conftest.py. Authored with assistance from Claude Code.
1 parent 66967ef commit 3253a28

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

backends/cortex_m/test/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ def pytest_configure(config):
6464
_set_random_seed(seed)
6565

6666

67+
def pytest_collectstart(collector):
68+
"""Reseed before each test module is imported. Model tests that build
69+
calibration data at module scope draw during collection, so seeding only in
70+
``pytest_configure`` would leave their quantization dependent on how many
71+
modules were imported first."""
72+
if not isinstance(collector, pytest.Module):
73+
return
74+
if os.environ.get("TEST_RUNTIME_IS_NOT_OSS", "0") != "1":
75+
_set_random_seed(collector.config._test_seed)
76+
77+
6778
@pytest.fixture(autouse=True)
6879
def set_random_seed(request):
6980
"""Control random numbers in the Cortex-M test suite.

0 commit comments

Comments
 (0)