Skip to content

Commit 48d46bb

Browse files
authored
[TEST] Use two different monkey patch objects for environment variable restore (#7807)
This is a quite interesting problem. Long story short, without the PR we cannot clean up variables set in the tests. And running `pytest test_knobs.py` after applying the following diff will fail ``` diff --git a/python/test/unit/test_knobs.py b/python/test/unit/test_knobs.py index ba2c851..808198076 100644 --- a/python/test/unit/test_knobs.py +++ b/python/test/unit/test_knobs.py @@ -117,6 +117,10 @@ def test_env_updated(fresh_knobs, monkeypatch): assert os.environ["TRITON_HOME"] == "/foo/bar" +def test_none(): + assert "TRITON_HIP_LOCAL_PREFETCH" not in os.environ + + ```
1 parent 6205481 commit 48d46bb

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

python/test/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ def fresh_triton_cache():
2525

2626

2727
@pytest.fixture
28-
def fresh_knobs(monkeypatch):
28+
def fresh_knobs():
2929
from triton._internal_testing import _fresh_knobs_impl
30-
fresh_function, reset_function = _fresh_knobs_impl(monkeypatch)
30+
fresh_function, reset_function = _fresh_knobs_impl()
3131
try:
3232
yield fresh_function()
3333
finally:
3434
reset_function()
3535

3636

3737
@pytest.fixture
38-
def fresh_knobs_except_libraries(monkeypatch):
38+
def fresh_knobs_except_libraries():
3939
"""
4040
A variant of `fresh_knobs` that keeps library path
4141
information from the environment as these may be
4242
needed to successfully compile kernels.
4343
"""
4444
from triton._internal_testing import _fresh_knobs_impl
45-
fresh_function, reset_function = _fresh_knobs_impl(monkeypatch, skipped_attr={"build", "nvidia", "amd"})
45+
fresh_function, reset_function = _fresh_knobs_impl(skipped_attr={"build", "nvidia", "amd"})
4646
try:
4747
yield fresh_function()
4848
finally:

python/triton/_internal_testing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,14 @@ def unwrap_tensor(t: Union[torch.Tensor, triton.runtime.jit.TensorWrapper]) -> t
204204
return t
205205

206206

207-
def _fresh_knobs_impl(monkeypatch, skipped_attr: Optional[Set[str]] = None):
207+
def _fresh_knobs_impl(skipped_attr: Optional[Set[str]] = None):
208208
from triton import knobs
209209

210210
if skipped_attr is None:
211211
skipped_attr = set()
212212

213+
monkeypatch = pytest.MonkeyPatch()
214+
213215
knobs_map = {
214216
name: knobset
215217
for name, knobset in knobs.__dict__.items()
@@ -237,6 +239,9 @@ def fresh_function():
237239
def reset_function():
238240
for name, knobset in knobs_map.items():
239241
setattr(knobs, name, knobset)
242+
# `undo` should be placed before `del os.environ`
243+
# Otherwise, it may restore environment variables that monkeypatch deleted
244+
monkeypatch.undo()
240245
for k in env_to_unset:
241246
if k in os.environ:
242247
del os.environ[k]

python/triton_kernels/tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def device(request):
1111

1212

1313
@pytest.fixture
14-
def fresh_knobs(monkeypatch):
14+
def fresh_knobs():
1515
from triton._internal_testing import _fresh_knobs_impl
16-
fresh_function, reset_function = _fresh_knobs_impl(monkeypatch)
16+
fresh_function, reset_function = _fresh_knobs_impl()
1717
try:
1818
yield fresh_function()
1919
finally:

0 commit comments

Comments
 (0)