diff --git a/hls4ml/__init__.py b/hls4ml/__init__.py index 52061c05df..92141e34d6 100644 --- a/hls4ml/__init__.py +++ b/hls4ml/__init__.py @@ -1,3 +1,5 @@ +import sys + from hls4ml import converters, report, utils # noqa: F401, E402 try: @@ -9,16 +11,7 @@ def reseed(newseed): - print(f'\npytest-randomly: reseed with {newseed}') - try: - import tensorflow - - tensorflow.random.set_seed(newseed) - except ImportError: - print('\nTensorFlow seed not set') - try: - import torch - - torch.manual_seed(newseed) - except ImportError: - print('\nPyTorch seed not set') + if 'tensorflow' in sys.modules: + sys.modules['tensorflow'].random.set_seed(newseed) + if 'torch' in sys.modules: + sys.modules['torch'].manual_seed(newseed) diff --git a/pyproject.toml b/pyproject.toml index fd82192626..70a156b2a2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,7 +88,7 @@ optional-dependencies.testing-qkeras-v3 = [ optional-dependencies.xls = [ "xls-python>=0.1.9875" ] urls.Homepage = "https://fastmachinelearning.org/hls4ml" scripts.hls4ml = "hls4ml.cli:main" -entry-points.pytest_randomly.random_seeder = "hls4ml:reseed" +entry-points."pytest_randomly.random_seeder".hls4ml = "hls4ml:reseed" [tool.setuptools] packages = [ "hls4ml" ] diff --git a/test/pytest/ci-template.yml b/test/pytest/ci-template.yml index e1033c982a..dab70837f4 100644 --- a/test/pytest/ci-template.yml +++ b/test/pytest/ci-template.yml @@ -36,7 +36,7 @@ - source /home/hls4ml/ahls/altera_hls/EAP/aclsycl/fpgavars.sh script: - cd test/pytest - - pytest $PYTESTFILE -rA --cov-report xml --cov-report term --cov=hls4ml --junitxml=report.xml --randomly-seed=42 --randomly-dont-reorganize --randomly-dont-reset-seed + - pytest $PYTESTFILE -rA --cov-report xml --cov-report term --cov=hls4ml --junitxml=report.xml --randomly-seed=42 --randomly-dont-reorganize artifacts: when: always reports: diff --git a/test/pytest/test_reseed.py b/test/pytest/test_reseed.py new file mode 100644 index 0000000000..a4e01562e4 --- /dev/null +++ b/test/pytest/test_reseed.py @@ -0,0 +1,6 @@ +from importlib.metadata import entry_points + + +def test_reseed_entry_point_registered(): + eps = entry_points(group='pytest_randomly.random_seeder') + assert any(ep.value == 'hls4ml:reseed' for ep in eps)