From 990194afd5238373bd3f002af942a759dbdbf03e Mon Sep 17 00:00:00 2001 From: azaidy Date: Tue, 3 Sep 2024 22:01:54 -0400 Subject: [PATCH 1/3] Generate random seed using a pytest fixture --- umi/sumi/tests/conftest.py | 15 +++++++++++++++ umi/sumi/tests/test_crossbar.py | 11 +++++------ umi/sumi/tests/test_fifo.py | 5 ++++- umi/sumi/tests/test_fifo_flex.py | 5 ++++- umi/sumi/tests/test_mem_agent.py | 4 +++- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/umi/sumi/tests/conftest.py b/umi/sumi/tests/conftest.py index bc56321..27e1eaa 100644 --- a/umi/sumi/tests/conftest.py +++ b/umi/sumi/tests/conftest.py @@ -68,3 +68,18 @@ def sumi_dut(build_dir, request): yield dut dut.terminate() + + +def pytest_addoption(parser): + parser.addoption("--seed", type=int, action="store", help="Provide a fixed seed") + + +@pytest.fixture +def random_seed(request): + fixed_seed = request.config.getoption("--seed") + if fixed_seed is not None: + test_seed = fixed_seed + else: + test_seed = os.getpid() + yield test_seed + print(f'Random seed used: {test_seed}') diff --git a/umi/sumi/tests/test_crossbar.py b/umi/sumi/tests/test_crossbar.py index 4d74cd6..be77240 100755 --- a/umi/sumi/tests/test_crossbar.py +++ b/umi/sumi/tests/test_crossbar.py @@ -10,11 +10,10 @@ from switchboard import UmiTxRx, random_umi_packet, delete_queue -def umi_send(x, n, ports): - import os +def umi_send(x, n, ports, seed): - random.seed(os.getpid()) - np.random.seed(os.getpid()) + random.seed(seed) + np.random.seed(seed) umi = UmiTxRx(f'client2rtl_{x}.q', '') tee = UmiTxRx(f'tee_{x}.q', '') @@ -32,7 +31,7 @@ def umi_send(x, n, ports): @pytest.mark.skip(reason="Crossbar asserts output valid even when in reset") -def test_crossbar(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode): +def test_crossbar(sumi_dut, random_seed, sb_umi_valid_mode, sb_umi_ready_mode): n = 100 ports = 4 for x in range(ports): @@ -59,7 +58,7 @@ def test_crossbar(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode): procs = [] for x in range(ports): - procs.append(multiprocessing.Process(target=umi_send, args=(x, n, ports,))) + procs.append(multiprocessing.Process(target=umi_send, args=(x, n, ports, (random_seed+x),))) for proc in procs: proc.start() diff --git a/umi/sumi/tests/test_fifo.py b/umi/sumi/tests/test_fifo.py index e86e74b..29d8cb9 100755 --- a/umi/sumi/tests/test_fifo.py +++ b/umi/sumi/tests/test_fifo.py @@ -9,7 +9,10 @@ from switchboard import UmiTxRx -def test_fifo(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode): +def test_fifo(sumi_dut, random_seed, sb_umi_valid_mode, sb_umi_ready_mode): + + random.seed(random_seed) + np.random.seed(random_seed) # launch the simulation sumi_dut.simulate(plusargs=[('valid_mode', sb_umi_valid_mode), ('ready_mode', sb_umi_ready_mode)]) diff --git a/umi/sumi/tests/test_fifo_flex.py b/umi/sumi/tests/test_fifo_flex.py index 53714ca..7415cb1 100755 --- a/umi/sumi/tests/test_fifo_flex.py +++ b/umi/sumi/tests/test_fifo_flex.py @@ -9,7 +9,10 @@ from switchboard import UmiTxRx -def test_fifo_flex(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode): +def test_fifo_flex(sumi_dut, random_seed, sb_umi_valid_mode, sb_umi_ready_mode): + + random.seed(random_seed) + np.random.seed(random_seed) # launch the simulation sumi_dut.simulate(plusargs=[('valid_mode', sb_umi_valid_mode), ('ready_mode', sb_umi_ready_mode)]) diff --git a/umi/sumi/tests/test_mem_agent.py b/umi/sumi/tests/test_mem_agent.py index f418c3e..69f1430 100755 --- a/umi/sumi/tests/test_mem_agent.py +++ b/umi/sumi/tests/test_mem_agent.py @@ -52,7 +52,9 @@ def apply_atomic(origdata, atomicdata, operation, maxrange): return tempval -def test_mem_agent(sumi_dut, sb_umi_valid_mode, sb_umi_ready_mode): +def test_mem_agent(sumi_dut, random_seed, sb_umi_valid_mode, sb_umi_ready_mode): + + np.random.seed(random_seed) # launch the simulation sumi_dut.simulate(plusargs=[('valid_mode', sb_umi_valid_mode), ('ready_mode', sb_umi_ready_mode)]) From 0b9c5f5b0e079bc2c37f25f56e8f06979e1ae9b5 Mon Sep 17 00:00:00 2001 From: azaidy Date: Wed, 4 Sep 2024 11:04:45 -0400 Subject: [PATCH 2/3] Print random seed before yield --- umi/sumi/tests/conftest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/umi/sumi/tests/conftest.py b/umi/sumi/tests/conftest.py index 27e1eaa..4591f1c 100644 --- a/umi/sumi/tests/conftest.py +++ b/umi/sumi/tests/conftest.py @@ -81,5 +81,6 @@ def random_seed(request): test_seed = fixed_seed else: test_seed = os.getpid() + print(f'Random seed used: {test_seed}') yield test_seed print(f'Random seed used: {test_seed}') From 31a376288be07a809d34ef6159ee2caf571df24c Mon Sep 17 00:00:00 2001 From: azaidy Date: Wed, 4 Sep 2024 11:05:55 -0400 Subject: [PATCH 3/3] Remove unused random seed initialization --- umi/sumi/tests/test_crossbar.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/umi/sumi/tests/test_crossbar.py b/umi/sumi/tests/test_crossbar.py index be77240..d8b5a1b 100755 --- a/umi/sumi/tests/test_crossbar.py +++ b/umi/sumi/tests/test_crossbar.py @@ -6,14 +6,12 @@ import pytest import multiprocessing import random -import numpy as np from switchboard import UmiTxRx, random_umi_packet, delete_queue def umi_send(x, n, ports, seed): random.seed(seed) - np.random.seed(seed) umi = UmiTxRx(f'client2rtl_{x}.q', '') tee = UmiTxRx(f'tee_{x}.q', '')