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.

torchao alias tests: stop leaving a fake diffusers in sys.modules (#13336)

Daniel Han41485d3ca3db
diff --git a/studio/backend/core/_torchao_stub.py b/studio/backend/core/_torchao_stub.py
index 47067d8cd35..b09c44cce30 100644
--- a/studio/backend/core/_torchao_stub.py
+++ b/studio/backend/core/_torchao_stub.py
@@ -252,7 +252,12 @@ def _alias_fqn_to_config_now() -> None:
class _FqnAliasBeforeDiffusers(importlib.abc.MetaPathFinder):
"""Aliases on the first ``import diffusers`` and never resolves a module itself."""
- def find_spec(self, fullname, path = None, target = None):
+ def find_spec(
+ self,
+ fullname,
+ path = None,
+ target = None,
+ ):
if fullname == "diffusers":
try:
_alias_fqn_to_config_now()
diff --git a/studio/backend/tests/test_torchao_fqn_alias.py b/studio/backend/tests/test_torchao_fqn_alias.py
index e2dcf2b3320..3650556f521 100644
--- a/studio/backend/tests/test_torchao_fqn_alias.py
+++ b/studio/backend/tests/test_torchao_fqn_alias.py
@@ -14,6 +14,30 @@ import pytest
from core import _torchao_stub
+_PACKAGES = ("diffusers", "torchao")
+
+
+def _loaded_packages() -> dict:
+ return {n: m for n, m in sys.modules.items() if n.split(".")[0] in _PACKAGES}
+
+
[email protected](autouse = True)
+def _restore_diffusers_and_torchao_modules():
+ """Put sys.modules back exactly as it was for diffusers and torchao after every test.
+
+ Tests here import a fake ``diffusers`` from tmp_path. monkeypatch.delitem only records
+ entries that existed, so when the real diffusers had not been imported yet in this worker
+ the fake one stayed in sys.modules, and every later test there got a diffusers without
+ AutoencoderKLWan, the pipelines, and so on.
+ """
+ before = _loaded_packages()
+ yield
+ for name in list(_loaded_packages()):
+ if before.get(name) is not sys.modules.get(name):
+ sys.modules.pop(name, None)
+ sys.modules.update(before)
+
+
class _ModuleFqnToConfig:
pass
@@ -42,7 +66,9 @@ def fake_torchao(monkeypatch):
return use
[email protected]("version", ["0.14.0", "0.14.1", "0.13.0", "0.14.0+cu126", "0.14.0.dev20251001"])
+ "version", ["0.14.0", "0.14.1", "0.13.0", "0.14.0+cu126", "0.14.0.dev20251001"]
+)
def test_an_old_torchao_gains_the_new_name(fake_torchao, version):
quant = fake_torchao(version)
_torchao_stub.alias_torchao_fqn_to_config()
@@ -84,7 +110,9 @@ def test_an_old_torchao_is_aliased_on_the_first_diffusers_import_not_before(monk
(pkg / "__init__.py").write_text("")
(pkg / "quantization" / "__init__.py").write_text("class ModuleFqnToConfig:\n pass\n")
(tmp_path / "diffusers").mkdir()
- (tmp_path / "diffusers" / "__init__.py").write_text("from torchao.quantization import FqnToConfig\n")
+ (tmp_path / "diffusers" / "__init__.py").write_text(
+ "from torchao.quantization import FqnToConfig\n"
+ )
monkeypatch.syspath_prepend(str(tmp_path))
monkeypatch.setattr(_torchao_stub.importlib.metadata, "version", lambda name: "0.14.0")
@@ -126,7 +154,9 @@ def test_an_existing_fqn_to_config_is_kept(fake_torchao):
assert quant.FqnToConfig is sentinel
[email protected]("module", ["run", "core.inference.diffusion", "core.training.diffusion_train_common"])
+ "module", ["run", "core.inference.diffusion", "core.training.diffusion_train_common"]
+)
def test_every_diffusers_importer_aliases_after_the_rocm_stub(module):
"""Static order check: each module-level hook block runs the alias right after the torchao stub."""
import importlib.util