Skip to content

Commit 2ece063

Browse files
committed
Cortex-M backend: Make pytest collection seeding deterministic
Seed the Cortex-M test RNG during pytest configuration in OSS/local test runs so module-level random inputs are deterministic before collection imports test modules. Use TEST_SEED for the Cortex-M test session seed, matching the Arm backend test behavior. Keep packaged non-OSS Buck behavior unchanged by skipping that early torch import there, while preserving the per-test seeding fixture. Signed-off-by: Zingo Andersen <Zingo.Andersen@arm.com> Change-Id: I69a8b6757b1c6960f470861df1c02fc6292f7770
1 parent 0567b0a commit 2ece063

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

backends/cortex_m/test/TARGETS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2026 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

78
load("@fbcode_macros//build_defs:python_unittest.bzl", "python_unittest")
9+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
810
load("targets.bzl", "define_common_targets")
911

1012
oncall("executorch")
1113
python_unittest(
1214
name="test_replace_quant_nodes",
15+
env={
16+
"TEST_RUNTIME_IS_NOT_OSS": "1" if not runtime.is_oss else "0",
17+
},
1318
srcs=[
1419
"test_helpers_passes_utils.py",
1520
"test_replace_quant_nodes.py",

backends/cortex_m/test/conftest.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2026 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

8+
import os
9+
import random
10+
711
import pytest
812
from executorch.backends.cortex_m.target_config import CortexMTargetConfig
913

@@ -29,7 +33,10 @@ def _selected_targets(config) -> list[str]:
2933

3034

3135
def pytest_report_header(config):
32-
return f"cortex-m op-test targets: {', '.join(_selected_targets(config))}"
36+
return (
37+
f"cortex-m op-test targets: {', '.join(_selected_targets(config))} "
38+
f"{config._test_seed_label}"
39+
)
3340

3441

3542
def pytest_generate_tests(metafunc):
@@ -46,3 +53,45 @@ def cortex_m_target(request) -> CortexMTargetConfig:
4653
the AoT target config (and, for implementation tests, the matching prebuilt
4754
FVP runner)."""
4855
return CortexMTargetConfig.from_target_string(request.param)
56+
57+
58+
def pytest_configure(config):
59+
seed, seed_label = _setup_random_seed()
60+
config._test_seed = seed
61+
config._test_seed_label = seed_label
62+
63+
if os.environ.get("TEST_RUNTIME_IS_NOT_OSS", "0") != "1":
64+
_set_random_seed(seed)
65+
66+
67+
@pytest.fixture(autouse=True)
68+
def set_random_seed(request):
69+
"""Control random numbers in the Cortex-M test suite.
70+
71+
By default this uses a fixed seed (0) for reproducible tests. Use
72+
TEST_SEED to set a custom session seed, or set it to RANDOM to choose a
73+
random session seed.
74+
"""
75+
_set_random_seed(request.config._test_seed)
76+
77+
78+
def _setup_random_seed():
79+
seed_env = os.environ.get("TEST_SEED", "0")
80+
if seed_env == "RANDOM":
81+
random.seed() # reset seed, in case any other test has fiddled with it
82+
seed = random.randint(0, 2**32 - 1) # nosec B311 - test seed
83+
seed_label = f"TEST_SEED=RANDOM using:{seed}"
84+
elif str.isdigit(seed_env):
85+
seed = int(seed_env)
86+
seed_label = f"TEST_SEED={seed}"
87+
else:
88+
raise TypeError("TEST_SEED env variable must be integers or the string RANDOM")
89+
90+
return seed, seed_label
91+
92+
93+
def _set_random_seed(seed):
94+
import torch
95+
96+
random.seed(seed)
97+
torch.manual_seed(seed)

0 commit comments

Comments
 (0)