Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Tests: clear the cached tensor parallel layout before every test (#17472)

Daniel Han4b10969b3bf5
diff --git a/tests/conftest.py b/tests/conftest.py
index 41383c8ed68..d681923a8f4 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -83,6 +83,16 @@ def _no_deleted_torch_config_entries():
check_after_test()
[email protected](autouse = True)
+def _fresh_parallel_layout():
+ """unsloth.models.tp_env reads the UNSLOTH_TP_SIZE / UNSLOTH_PARALLEL layout once per process. A test
+ that read it under another environment would hand every later test in its worker that stale layout,
+ so each test starts with it unread. Only when the module is already imported: no unsloth import here."""
+ tp_env = sys.modules.get("unsloth.models.tp_env")
+ if tp_env is not None:
+ tp_env._reset_env_layout()
+
+
@pytest.hookimpl(trylast = True)
def pytest_collection_modifyitems(config, items):
"""After xdist tags grouped tests; see tests/_shared/xdist_group_check.py."""
diff --git a/tests/test_tp_layout_isolation.py b/tests/test_tp_layout_isolation.py
new file mode 100644
index 00000000000..720814930b9
--- /dev/null
+++ b/tests/test_tp_layout_isolation.py
@@ -0,0 +1,24 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""tp_env caches the env layout per process; tests/conftest.py clears it before every test.
+
+Without that, a test reading the layout with UNSLOTH_TP_SIZE unset left None cached, and a later test
+in the same xdist worker that set UNSLOTH_TP_SIZE=2 / UNSLOTH_CP_SIZE=2 got the generic "does not
+compose" refusal instead of its layout's (test_pre_init_query_reads_a_pending_peft_config). Run in
+file order, the second test here fails without the fixture.
+"""
+
+
+def test_a_test_may_leave_the_layout_read(monkeypatch):
+ from unsloth.models import tp_env
+
+ monkeypatch.delenv("UNSLOTH_TP_SIZE", raising = False)
+ monkeypatch.delenv("UNSLOTH_PARALLEL", raising = False)
+ tp_env.env_layout()
+ assert tp_env._LAYOUT is not tp_env._UNSET
+
+
+def test_the_next_test_starts_with_the_layout_unread():
+ from unsloth.models import tp_env
+ assert tp_env._LAYOUT is tp_env._UNSET, "a layout read by an earlier test leaked into this one"