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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Keep the user override when the torch config guard restores a deleted entry (#16652)
Daniel Han1c43803441eb
diff --git a/tests/_shared/torch_config_guard.py b/tests/_shared/torch_config_guard.pyindex 829ed1fcdf7..c449bbdf0a2 100644--- a/tests/_shared/torch_config_guard.py+++ b/tests/_shared/torch_config_guard.py@@ -17,6 +17,7 @@ from __future__ import annotationsimport sysimport threadingimport traceback+from contextvars import ContextVarfrom typing import AnyCONFIG_MODULES = (@@ -31,6 +32,19 @@ _DELETES: dict[tuple[str, str], tuple[Any, str]] = {}_installed = False+def _read_override(entry: Any) -> Any:+ # torch 2.12+ keeps user_override in a ContextVar that __delattr__ resets, so save its value, not the var.+ override = entry.user_override+ return override.get() if isinstance(override, ContextVar) else override+++def _write_override(entry: Any, value: Any) -> None:+ if isinstance(entry.user_override, ContextVar):+ entry.user_override.set(value)+ else:+ entry.user_override = value++def _entries(module_name: str) -> dict[str, Any] | None:module = sys.modules.get(module_name)# vars(): a ConfigModule answers unknown attributes through __getattr__.@@ -55,7 +69,7 @@ def install_delete_recorder() -> bool:if entry is not None:with _LOCK:_DELETES[(self.__name__, name)] = (- entry.user_override,+ _read_override(entry),"".join(traceback.format_stack(limit = 16)[:-1]),)return original(self, name)@@ -141,7 +155,7 @@ def restore(hidden: list[tuple[str, str]]) -> str:with _LOCK:prior = _DELETES.pop((module_name, key), None)if prior is not None:- entry.user_override = prior[0]+ _write_override(entry, prior[0])entry.hide = Falsemodule = sys.modules.get(module_name)if module is not None:diff --git a/tests/test_torch_config_guard.py b/tests/test_torch_config_guard.pyindex b4ac940e064..3df4c2bdabb 100644--- a/tests/test_torch_config_guard.py+++ b/tests/test_torch_config_guard.py@@ -40,6 +40,15 @@ def test_a_deleted_entry_is_found_named_and_restored(monkeypatch):assert guard.hidden_entries() == []+def test_a_scoped_override_survives_the_restore():+ # config.patch values live only in the ContextVar on torch 2.12+, unlike plain writes Unsloth mirrors into the default.+ assert guard.install_delete_recorder()+ with torch._dynamo.config.patch(**{_LIMIT: 37}):+ delattr(torch._dynamo.config, _LIMIT)+ guard.restore(guard.hidden_entries())+ assert getattr(torch._dynamo.config, _LIMIT) == 37++def test_check_after_test_raises_and_leaves_the_process_clean():guard.install_delete_recorder()before = torch._inductor.config.emulate_precision_casts