From 888b41bdd939f2e08e59baf2cb9ddc7140620b17 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Thu, 2 Jul 2026 09:03:02 +0000 Subject: [PATCH] Do not reinstall llm-compressor when it is already installed The FP8/FP4 compressed export calls install_llm_compressor() in the export worker process to ensure the dependency is present. That in-process import can fail even when llm-compressor is installed, because Unsloth's transformers patches interfere in this process (the actual quantization runs in a separate clean subprocess that re-imports it fine). On that spurious failure the code fell through to a pip install of the version-capped, torch/transformers-pinned spec, which made pip re-resolve the whole graph and backtrack over every llm-compressor release, eventually trying to build numpy<2 from source and failing the export with a confusing pip error. Guard the install on importlib.metadata: when a llm-compressor distribution is already present, return instead of reinstalling. Reinstalling cannot fix an in-process import error, and the isolated quantization subprocess imports it cleanly. A genuinely absent install still triggers the auto-install as before. --- unsloth/save.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/unsloth/save.py b/unsloth/save.py index 226ca5fed8..2692f5595c 100644 --- a/unsloth/save.py +++ b/unsloth/save.py @@ -1383,6 +1383,23 @@ def install_llm_compressor(): except Exception: pass + # Already installed but not importable in THIS process? Do not reinstall. The in-process import + # can fail under Unsloth's transformers patches (the compressed export quantizes in an isolated + # subprocess that re-imports cleanly), and reinstalling the version-capped, torch/transformers- + # pinned spec makes pip re-resolve and backtrack destructively (it drags in numpy<2 built from + # source and the export fails). Only fall through to a pip install when it is genuinely absent. + try: + from importlib.metadata import version as _iv, PackageNotFoundError as _PNF + try: + _iv("llmcompressor") + # Present; the compressed-export subprocess performs the real import. The caller only + # uses this to trigger the install and fail fast, so returning None here is safe. + return None, None + except _PNF: + pass + except Exception: + pass + # 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",