Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions hls4ml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

from hls4ml import converters, report, utils # noqa: F401, E402

try:
Expand All @@ -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)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the entry point fixed, hls4ml.reseed runs for the first time: once in the report header and again at setup, call and teardown of every test. That brings up two things in hls4ml/__init__.py:

  • It imports frameworks unconditionally and only catches ImportError. If import torch raises OSError (for example missing CUDA libraries), or import tensorflow fails with something else (a protobuf or numpy ABI mismatch), the whole pytest session errors out at the header. That includes jobs that never use either framework.
  • Every job now loads both TensorFlow and torch. The KERAS_BACKEND=torch/jax jobs (e.g. test_pquant_pytorch) import TF, and the TF-only jobs import torch. That costs startup time and memory, and it puts both frameworks in one process where they weren't before.

Both go away if reseed only seeds frameworks that are already imported, since there's nothing to seed in a framework nobody has loaded yet:

import sys

def reseed(newseed):
    if 'tensorflow' in sys.modules:
        sys.modules['tensorflow'].random.set_seed(newseed)
    if 'torch' in sys.modules:
        sys.modules['torch'].manual_seed(newseed)

One caveat: a framework imported for the first time inside a test (rather than at module level) would skip that test's setup reseed, but it would still be seeded from the next reseed point on.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a small test guard this registration? It has been silently broken since #1094, and a future edit or formatter rewrite of this key could break it again without any test failing. Something like:

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)

Because this reads the installed metadata, it will also fail in an editable install made before this change, which is a useful hint to reinstall.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.


[tool.setuptools]
packages = [ "hls4ml" ]
Expand Down
2 changes: 1 addition & 1 deletion test/pytest/ci-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions test/pytest/test_reseed.py
Original file line number Diff line number Diff line change
@@ -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)
Loading