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.

Do not reinstall llm-compressor when it is already installed (#6806)

Daniel Han949b94a2785b
diff --git a/tests/saving/test_llm_compressor_reinstall_guard.py b/tests/saving/test_llm_compressor_reinstall_guard.py
new file mode 100644
index 00000000000..f17cc196c1c
--- /dev/null
+++ b/tests/saving/test_llm_compressor_reinstall_guard.py
@@ -0,0 +1,101 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+"""install_llm_compressor() must not pip-reinstall an llm-compressor that only THIS process fails to import.
+
+The probe is real: a fresh interpreter imports a fake llmcompressor placed on PYTHONPATH. Only the
+in-process import is blocked, and only pip is stubbed.
+"""
+
+import importlib.abc
+import subprocess
+import sys
+
+import pytest
+
+import unsloth.save as save
+
+
+def _write_fake(
+ root,
+ version = "0.11.0",
+ broken = False,
+):
+ pkg = root / "llmcompressor"
+ (pkg / "modifiers" / "quantization").mkdir(parents = True, exist_ok = True)
+ body = "raise ImportError('broken install')\n" if broken else ""
+ (pkg / "__init__.py").write_text(f"{body}__version__ = {version!r}\ndef oneshot(**kw): pass\n")
+ (pkg / "modifiers" / "__init__.py").write_text("")
+ (pkg / "modifiers" / "quantization" / "__init__.py").write_text(
+ "class QuantizationModifier: pass\n"
+ )
+
+
+class _BlockInProcess(importlib.abc.MetaPathFinder):
+ def find_spec(
+ self,
+ name,
+ path,
+ target = None,
+ ):
+ if name == "llmcompressor" or name.startswith("llmcompressor."):
+ raise ImportError("simulated in-process failure")
+
+
+def env(tmp_path, monkeypatch):
+ monkeypatch.setenv("PYTHONPATH", str(tmp_path))
+ monkeypatch.delenv("UNSLOTH_DISABLE_LLM_COMPRESSOR_AUTOINSTALL", raising = False)
+ blocker = _BlockInProcess()
+ sys.meta_path.insert(0, blocker)
+ pip_calls = []
+ monkeypatch.setattr(save.subprocess, "check_call", lambda cmd, *a, **k: pip_calls.append(cmd))
+ yield tmp_path, pip_calls
+ sys.meta_path.remove(blocker)
+
+
[email protected]("optout", ["0", "1"])
+def test_installed_but_blocked_in_process_skips_pip(env, monkeypatch, optout):
+ root, pip_calls = env
+ monkeypatch.setenv("UNSLOTH_DISABLE_LLM_COMPRESSOR_AUTOINSTALL", optout)
+ _write_fake(root)
+ assert save.install_llm_compressor() == (None, None)
+ assert pip_calls == []
+
+
[email protected]("fake", [dict(broken = True), dict(version = "0.99.0")])
+def test_broken_or_out_of_range_install_is_still_reinstalled(env, fake):
+ root, pip_calls = env
+ _write_fake(root, **fake)
+ with pytest.raises(RuntimeError, match = "installed but could not be imported"):
+ save.install_llm_compressor()
+ assert len(pip_calls) == 1
+
+
+def test_install_that_the_subprocess_can_use_is_accepted(env, monkeypatch):
+ root, pip_calls = env
+ _write_fake(root, broken = True)
+ monkeypatch.setattr(
+ save.subprocess,
+ "check_call",
+ lambda cmd, *a, **k: (pip_calls.append(cmd), _write_fake(root)),
+ )
+ assert save.install_llm_compressor() == (None, None)
+ assert len(pip_calls) == 1
+
+
+def test_probe_timeout_is_not_a_yes(monkeypatch):
+ def _timeout(*a, **k):
+ raise subprocess.TimeoutExpired("python", 600)
+
+ monkeypatch.setattr(save.subprocess, "run", _timeout)
+ assert save._llm_compressor_imports_in_subprocess() is False
+
+
+def test_relative_pythonpath_resolves_against_the_callers_cwd(env, monkeypatch):
+ root, pip_calls = env
+ _write_fake(root / "vendor")
+ monkeypatch.chdir(root)
+ monkeypatch.setenv("PYTHONPATH", "vendor")
+ assert save.install_llm_compressor() == (None, None)
+ assert pip_calls == []
diff --git a/unsloth/save.py b/unsloth/save.py
index a4ae826c638..da92f36592a 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -1596,6 +1596,34 @@ def _compressed_quantize_pythonpath():
return pp or None
+def _llm_compressor_imports_in_subprocess():
+ """True only if a fresh interpreter, launched like the export's quantize runner, imports an llm-compressor inside _LLM_COMPRESSOR_SPEC."""
+ # sys.path[0] as `python _compressed_quantize.py` sets it; the caller's cwd is kept so relative PYTHONPATH entries resolve the same way.
+ probe = (
+ f"import sys; sys.path[0] = {os.path.dirname(os.path.abspath(__file__))!r}\n"
+ "import llmcompressor\n"
+ "from llmcompressor import oneshot\n"
+ "from llmcompressor.modifiers.quantization import QuantizationModifier\n"
+ "print(llmcompressor.__version__)\n"
+ )
+ try:
+ completed = subprocess.run(
+ [sys.executable, "-c", probe],
+ stdout = subprocess.PIPE,
+ stderr = subprocess.DEVNULL,
+ text = True,
+ timeout = 600,
+ )
+ if completed.returncode != 0:
+ return False
+ from packaging.requirements import Requirement
+
+ version = completed.stdout.strip().splitlines()[-1].strip()
+ return Requirement(_LLM_COMPRESSOR_SPEC).specifier.contains(version, prereleases = True)
+ except Exception:
+ return False
+
+
def install_llm_compressor():
"""Import llm-compressor, installing a version-pinned copy on first use for FP8/FP4 export and pinning the current torch + transformers so pip does not upgrade them. UNSLOTH_DISABLE_LLM_COMPRESSOR_AUTOINSTALL=1 forbids the auto-install. Returns (oneshot, QuantizationModifier)."""
try:
@@ -1605,6 +1633,10 @@ def install_llm_compressor():
except Exception:
pass
+ # The in-process import can fail under Unsloth's transformers patches while the unpatched quantize subprocess imports fine. Reinstalling cannot fix that, and pip's pinned re-resolve backtracks destructively (numpy<2 from source), so skip it. The caller discards the return value.
+ if _llm_compressor_imports_in_subprocess():
+ return None, None
+
# Opt-out for locked-down / air-gapped setups: forbid the auto-install, require a manual one.
if os.environ.get("UNSLOTH_DISABLE_LLM_COMPRESSOR_AUTOINSTALL", "0").lower() not in (
"0",
@@ -1681,6 +1713,8 @@ def install_llm_compressor():
from llmcompressor import oneshot
from llmcompressor.modifiers.quantization import QuantizationModifier
except Exception as e:
+ if _llm_compressor_imports_in_subprocess():
+ return None, None
raise RuntimeError(
"Unsloth: llm-compressor was installed but could not be imported. "
"Please restart your Python session and try again.\n"