diff --git a/dpnp/tests/conftest.py b/dpnp/tests/conftest.py index 8e3cb97ad41..c4cb94845db 100644 --- a/dpnp/tests/conftest.py +++ b/dpnp/tests/conftest.py @@ -31,6 +31,7 @@ import warnings import dpctl +import dpctl.utils as dpu import numpy import pytest @@ -186,6 +187,27 @@ def pytest_collection_modifyitems(config, items): item.add_marker(skip_slow) +def pytest_runtest_teardown(item, nextitem): + """ + Release transient SYCL queues at the end of each test file. + + Every distinct ``SyclQueue`` a test creates is retained forever as a key in + dpctl's ``SequentialOrderManager`` (keyed by queue identity), which pins its + host-task events and the backing USM memory for the whole session. Over a + full run this accumulates hundreds of queues and steadily drains device + memory. Draining and dropping them at each file boundary keeps the footprint + flat without paying the cost after every individual test. + """ + # Only act on the last test of the current file (``nextitem`` is None at the + # very end of the session, or points at the first test of the next file). + if nextitem is not None and item.path == nextitem.path: + return + try: + dpu.SequentialOrderManager.clear() + except Exception as e: # never let cleanup break the run + warnings.warn(f"Failed to clear SequentialOrderManager: {e}") + + @pytest.fixture def allow_fall_back_on_numpy(monkeypatch): monkeypatch.setattr(