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.

Keep the LoRA GGUF and compressed-tensors converters off the operator's Hugging Face token (#10263)

Sourav Rajvi5ae462df3802
diff --git a/tests/saving/test_export_dispatch.py b/tests/saving/test_export_dispatch.py
index a92d4a5002d..b31258cc488 100644
--- a/tests/saving/test_export_dispatch.py
+++ b/tests/saving/test_export_dispatch.py
@@ -97,7 +97,7 @@ def test_gguf_lora_passes_valid_outtype(monkeypatch, tmp_path):
monkeypatch.setattr(
save_mod,
"_unsloth_save_lora_gguf",
- lambda model, tok, sd, outtype = None: seen.update(outtype = outtype),
+ lambda *_a, **kw: seen.update(kw),
)
save_mod.unsloth_save_pretrained_gguf(
_FakeModel(),
@@ -114,7 +114,7 @@ def test_gguf_lora_invalid_outtype_falls_back_to_f16(monkeypatch, tmp_path):
monkeypatch.setattr(
save_mod,
"_unsloth_save_lora_gguf",
- lambda model, tok, sd, outtype = None: seen.update(outtype = outtype),
+ lambda *_a, **kw: seen.update(kw),
)
save_mod.unsloth_save_pretrained_gguf(
_FakeModel(),
@@ -128,6 +128,26 @@ def test_gguf_lora_invalid_outtype_falls_back_to_f16(monkeypatch, tmp_path):
), "a GGUF model quant (q4_k_m) is not a valid LoRA outtype -> f16"
[email protected]("token", [False, "caller-token", None])
+def test_gguf_lora_forwards_the_caller_token(monkeypatch, tmp_path, token):
+ """Dropping it here sends _unsloth_save_lora_gguf to get_token(), i.e. the host credential."""
+ seen = {}
+ monkeypatch.setattr(
+ save_mod,
+ "_unsloth_save_lora_gguf",
+ lambda *_a, **kw: seen.update(kw),
+ )
+ save_mod.unsloth_save_pretrained_gguf(
+ _FakeModel(),
+ str(tmp_path),
+ tokenizer = object(),
+ save_method = "lora",
+ quantization_method = "q8_0",
+ token = token,
+ )
+ assert seen["token"] is token
+
+
def test_gguf_lora_push_to_hub_is_rejected(tmp_path):
with pytest.raises(ValueError):
save_mod.unsloth_save_pretrained_gguf(
@@ -316,3 +336,194 @@ def test_torchao_requires_config_or_qat(tmp_path):
tokenizer = object(),
torchao_config = None,
)
+
+
+def _run_lora_gguf(monkeypatch, tmp_path, token):
+ """Drive _unsloth_save_lora_gguf to the converter call and return the env it would use."""
+ captured = {}
+
+ class _FakePeft:
+ config = _FakeModel.config
+
+ class _FakePopen:
+ def __init__(self, _cmd, **kwargs):
+ captured["env"] = kwargs["env"]
+ self.stdout = []
+ self.returncode = 0
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_exc):
+ return False
+
+ def wait(self):
+ return 0
+
+ llama_dir = tmp_path / "llama.cpp"
+ llama_dir.mkdir()
+ (llama_dir / "convert_lora_to_gguf.py").write_text("", encoding = "utf-8")
+
+ monkeypatch.setattr(save_mod, "PeftModelForCausalLM", _FakePeft)
+ monkeypatch.setattr(save_mod, "LLAMA_CPP_DEFAULT_DIR", str(llama_dir))
+ monkeypatch.setattr(save_mod, "save_lora_to_custom_dir", lambda *_a: None)
+ monkeypatch.setattr(save_mod, "install_llama_cpp", lambda **_kw: None)
+ monkeypatch.setattr(save_mod, "_lora_base_model_id", lambda _m: "org/private-base")
+ monkeypatch.setattr(save_mod, "_loaded_via_remote_code", lambda _m: False)
+ monkeypatch.setattr(save_mod, "get_token", lambda: "host-ambient-token")
+ monkeypatch.setattr(save_mod.subprocess, "Popen", _FakePopen)
+ monkeypatch.setenv("HF_TOKEN", "host-ambient-token")
+ monkeypatch.setenv("HUGGINGFACEHUB_API_TOKEN", "host-legacy-alias")
+ monkeypatch.setenv("HF_HUB_DISABLE_IMPLICIT_TOKEN", "1")
+
+ save_mod._unsloth_save_lora_gguf(
+ _FakePeft(), _FakeTokenizer(), str(tmp_path / "out"), outtype = "f16", token = token
+ )
+ return captured["env"]
+
+
+def test_lora_gguf_converter_is_denied_the_host_token(monkeypatch, tmp_path):
+ env = _run_lora_gguf(monkeypatch, tmp_path, token = False)
+ for key in save_mod._HF_TOKEN_ENV_KEYS:
+ assert key not in env, f"{key} survived into a forced-anonymous converter"
+ assert env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == "1", "the cached token is still implicit"
+ # Scrubbing the env still leaves the operator's token FILE readable by get_token().
+ assert env["HF_TOKEN_PATH"] == os.devnull
+
+
+def test_lora_gguf_converter_gets_an_explicit_token(monkeypatch, tmp_path):
+ env = _run_lora_gguf(monkeypatch, tmp_path, token = "caller-token")
+ assert env["HF_TOKEN"] == "caller-token"
+ assert env["HUGGING_FACE_HUB_TOKEN"] == "caller-token"
+ assert "HUGGINGFACEHUB_API_TOKEN" not in env
+ assert env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == "0"
+
+
+def test_lora_gguf_converter_keeps_the_ambient_token_when_none(monkeypatch, tmp_path):
+ env = _run_lora_gguf(monkeypatch, tmp_path, token = None)
+ assert env["HF_TOKEN"] == "host-ambient-token"
+
+
+def test_lora_gguf_converter_does_not_overrule_the_operator_optout(monkeypatch, tmp_path):
+ # get_token() ignores the flag, so a caller who passed nothing holds the token the operator
+ # switched off; only a token they supplied earns clearing it.
+ env = _run_lora_gguf(monkeypatch, tmp_path, token = None)
+ assert env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == "1"
+ assert env["HUGGINGFACEHUB_API_TOKEN"] == "host-legacy-alias"
+
+
+ "token,expected",
+ [
+ ("", None),
+ (" ", None),
+ (" hf_caller ", "hf_caller"),
+ (None, None),
+ (False, False),
+ (True, True),
+ ],
+)
+def test_clean_save_token(token, expected):
+ # Blank reaches HfApi as a literal "Bearer " header, which 1.x rejects. False must survive:
+ # collapsing it to None is the ambient token, not anonymity.
+ result = save_mod._clean_save_token(token)
+ assert result is expected if expected in (None, False, True) else result == expected
+
+
[email protected]("blank", ["", " "])
+def test_lora_gguf_converter_reads_a_blank_token_as_absent(monkeypatch, tmp_path, blank):
+ env = _run_lora_gguf(monkeypatch, tmp_path, token = blank)
+ assert env["HF_TOKEN"] == "host-ambient-token"
+ assert env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == "1"
+ assert env["HUGGINGFACEHUB_API_TOKEN"] == "host-legacy-alias"
+
+
+def test_lora_gguf_converter_denies_the_oidc_material(monkeypatch, tmp_path):
+ # hub >= 1.19 exchanges these inside get_token() ahead of HF_TOKEN, so scrubbing the aliases
+ # alone still lets a denied child mint one.
+ monkeypatch.setenv("HF_OIDC_RESOURCE", "https://huggingface.co")
+ monkeypatch.setenv("HF_OIDC_ID_TOKEN", "operator-oidc-assertion")
+ env = _run_lora_gguf(monkeypatch, tmp_path, token = False)
+ assert "HF_OIDC_RESOURCE" not in env
+ assert "HF_OIDC_ID_TOKEN" not in env
+
+
+def test_lora_gguf_converter_honours_token_true(monkeypatch, tmp_path):
+ # True means "use the cached token" and outranks the flag; falling through every branch made
+ # it plain inheritance, which an ambient =1 voided.
+ env = _run_lora_gguf(monkeypatch, tmp_path, token = True)
+ assert env["HF_TOKEN"] == "host-ambient-token"
+ assert env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == "0"
+
+
+ "token,explicit,expected",
+ [
+ (False, True, {"scrubbed": True, "granted": None, "implicit": "1"}),
+ ("caller", True, {"scrubbed": True, "granted": "caller", "implicit": "0"}),
+ ("ambient", False, {"scrubbed": False, "granted": "ambient", "implicit": None}),
+ (None, False, {"scrubbed": False, "granted": None, "implicit": None}),
+ ],
+)
+def test_apply_token_to_child_env(token, explicit, expected):
+ env = {
+ "HF_TOKEN": "operator",
+ "HUGGINGFACEHUB_API_TOKEN": "operator-legacy",
+ "HF_OIDC_RESOURCE": "https://huggingface.co",
+ "PATH": "/usr/bin",
+ "HF_HUB_DISABLE_IMPLICIT_TOKEN": "1",
+ }
+ save_mod._apply_token_to_child_env(env, token, explicit = explicit)
+
+ assert env["PATH"] == "/usr/bin", "an unrelated variable was disturbed"
+ if expected["scrubbed"]:
+ assert "HUGGINGFACEHUB_API_TOKEN" not in env
+ assert "HF_OIDC_RESOURCE" not in env
+ else:
+ assert env["HUGGINGFACEHUB_API_TOKEN"] == "operator-legacy"
+ assert env["HF_OIDC_RESOURCE"] == "https://huggingface.co"
+ if expected["granted"] is None:
+ assert env.get("HF_TOKEN") in (None, "operator")
+ else:
+ assert env["HF_TOKEN"] == expected["granted"]
+ assert env["HUGGING_FACE_HUB_TOKEN"] == expected["granted"]
+ # None means "leave the inherited flag exactly as the operator set it".
+ assert env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] == (expected["implicit"] or "1")
+
+
+def test_every_converter_child_env_goes_through_the_token_boundary():
+ """No child env in save.py may be built without applying the caller boundary to it.
+
+ Structural rather than textual: a third subprocess added next to these two would otherwise
+ repeat the leak silently, which is how _unsloth_save_compressed_tensors came to have it.
+ """
+ import ast
+ import pathlib
+
+ def _is_environ_copy(node):
+ # os.environ.copy() exactly -- not os.environ.get(...) next to some other .copy().
+ return (
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Attribute)
+ and node.func.attr == "copy"
+ and isinstance(node.func.value, ast.Attribute)
+ and node.func.value.attr == "environ"
+ and isinstance(node.func.value.value, ast.Name)
+ and node.func.value.value.id == "os"
+ )
+
+ tree = ast.parse(pathlib.Path(save_mod.__file__).read_text(encoding = "utf-8"))
+ builders, offenders = [], []
+ for func in ast.walk(tree):
+ if not isinstance(func, (ast.FunctionDef, ast.AsyncFunctionDef)):
+ continue
+ if not any(_is_environ_copy(node) for node in ast.walk(func)):
+ continue
+ builders.append(func.name)
+ if "_apply_token_to_child_env" not in ast.dump(func):
+ offenders.append(f"{func.name} (line {func.lineno})")
+
+ assert builders, "the AST matcher found no child-env builders at all; it has drifted"
+ assert not offenders, (
+ "these build a child env without applying the token boundary: " + ", ".join(offenders)
+ )
diff --git a/unsloth/save.py b/unsloth/save.py
index 4ce5509e420..2272006dd6f 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -1883,6 +1883,9 @@ def _split_main_gguf(initial_files, gguf_shard_size: str, quantizer_location: st
check = True,
capture_output = True,
text = True,
+ # Windows defaults to cp1252 and crashes on the child's output (#2660).
+ encoding = "utf-8",
+ errors = "replace",
)
except subprocess.CalledProcessError as exception:
details = (exception.stderr or exception.stdout or "").strip()
@@ -4549,7 +4552,9 @@ def unsloth_save_pretrained_gguf(
f"Valid LoRA outtypes: {_LORA_GGUF_OUTTYPES}."
)
_outtype = "f16"
- return _unsloth_save_lora_gguf(self, tokenizer, save_directory, outtype = _outtype)
+ return _unsloth_save_lora_gguf(
+ self, tokenizer, save_directory, outtype = _outtype, token = token
+ )
# base_model_name keeps the full id for create_ollama_modelfile's mapper lookup; only the filename stem is
# trimmed.
@@ -5567,6 +5572,62 @@ def save_lora_to_custom_dir(model, tokenizer, save_directory):
# Valid output float types for llama.cpp's convert_lora_to_gguf.py.
_LORA_GGUF_OUTTYPES = ("f32", "f16", "bf16", "q8_0", "auto")
+# get_token() reads only the first two; the rest are third-party. HF_OIDC_* mint one, ahead of
+# HF_TOKEN, on hub >= 1.19.
+_HF_TOKEN_ENV_KEYS = (
+ "HF_TOKEN",
+ "HF_HUB_TOKEN",
+ "HUGGING_FACE_HUB_TOKEN",
+ "HUGGINGFACE_HUB_TOKEN",
+ "HUGGINGFACEHUB_API_TOKEN",
+ "HF_OIDC_RESOURCE",
+ "HF_OIDC_ID_TOKEN",
+)
+
+
+def _clean_save_token(token):
+ """Trim a token, keeping huggingface_hub's forced-anonymous ``False`` out of ``None``.
+
+ Blank is not a credential: it reaches the Hub as a literal ``Bearer `` header, which 1.x
+ rejects outright. It means "I passed nothing", which is ``None``.
+ """
+ if token is False or token is True:
+ return token
+ if isinstance(token, str):
+ return token.strip() or None
+ return token
+
+
+def _apply_token_to_child_env(env, token, *, explicit):
+ """Hand a spawned converter exactly one credential, or none.
+
+ The child inherits our env, so withholding a token is not denying one: the deny branch has to
+ scrub, token file included. Only an *explicit* token may clear the operator's
+ HF_HUB_DISABLE_IMPLICIT_TOKEN, since a token we resolved ourselves is the very implicit one
+ that flag suppresses.
+ """
+ if token is False:
+ for key in _HF_TOKEN_ENV_KEYS:
+ env.pop(key, None)
+ # The flag blocks the header; this blocks the read.
+ env["HF_TOKEN_PATH"] = os.devnull
+ env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] = "1"
+ return
+ if not (isinstance(token, str) and token):
+ return
+ if not explicit:
+ # Ambient: promote as before, scrub nothing.
+ env["HF_TOKEN"] = token
+ env["HUGGING_FACE_HUB_TOKEN"] = token
+ return
+ # Scrub first, or granting HF_TOKEN leaves ours in an alias and the child holds two.
+ for key in _HF_TOKEN_ENV_KEYS:
+ env.pop(key, None)
+ env["HF_TOKEN"] = token
+ env["HUGGING_FACE_HUB_TOKEN"] = token
+ # An inherited =1 would make the child ignore what we just granted.
+ env["HF_HUB_DISABLE_IMPLICIT_TOKEN"] = "0"
+
def _lora_base_model_id(model):
"""Base model id for a PEFT model: prefer the active adapter's recorded base, else the
@@ -5659,8 +5720,9 @@ def _resolve_imatrix_file(model, imatrix_file, token, dest_dir):
# (save.py top); hf_hub_download is imported here since nothing else needs it.
from huggingface_hub import hf_hub_download
- if token is None:
- token = get_token()
+ # As-is: pre-resolving made an ambient token look explicit, and only the header builder
+ # honours HF_HUB_DISABLE_IMPLICIT_TOKEN.
+ token = _clean_save_token(token)
api = HfApi(token = token)
repos = _gguf_repo_candidates(model)
for repo in repos:
@@ -5711,7 +5773,11 @@ def _unsloth_save_lora_gguf(
f"Unsloth: LoRA GGUF outtype must be one of {_LORA_GGUF_OUTTYPES} (got '{outtype}')."
)
# Resolve a token even for local saves: the converter may fetch a gated/private base config.
- if token is None:
+ # False must not resolve one. Explicitness BEFORE the fallback: get_token() ignores the flag,
+ # so what it returns is the implicit token itself.
+ token = _clean_save_token(token)
+ token_is_explicit = token is not None
+ if token is None or token is True:
token = get_token()
# Resolve the dequantized base id, since the adapter usually references a 4bit repo.
@@ -5772,9 +5838,7 @@ def _unsloth_save_lora_gguf(
# Expose the token to the converter so it can fetch a gated/private base config from the Hub.
env = os.environ.copy()
- if isinstance(token, str) and token:
- env["HF_TOKEN"] = token
- env["HUGGING_FACE_HUB_TOKEN"] = token
+ _apply_token_to_child_env(env, token, explicit = token_is_explicit)
print(f"Unsloth: Converting LoRA adapter at '{lora_dir}' to GGUF -> '{out_gguf}'")
# Resolve the cache from the live env like the merge, not huggingface_hub's frozen constants: a runtime
@@ -7050,8 +7114,10 @@ def _unsloth_save_compressed_tensors(
if isinstance(tokenizer, (PreTrainedTokenizerBase, ProcessorMixin)):
tokenizer = patch_saving_functions(tokenizer)
- # Resolve a token for the hub push and/or loading a gated calibration dataset in the subprocess.
- if token is None:
+ # For the hub push and/or a gated calibration dataset, as in the LoRA GGUF converter.
+ token = _clean_save_token(token)
+ token_is_explicit = token is not None
+ if token is None or token is True:
token = get_token()
# Only the main process installs deps, merges, quantizes and uploads, mirroring the non-PEFT save path;
@@ -7248,11 +7314,9 @@ def _unsloth_save_compressed_tensors(
if torch.cuda.is_available():
torch.cuda.empty_cache()
- # Expose the token so the subprocess can load a gated/private calibration dataset.
+ # Same boundary as the LoRA GGUF converter: False scrubs, it does not merely withhold.
env = os.environ.copy()
- if isinstance(token, str) and token:
- env["HF_TOKEN"] = token
- env["HUGGING_FACE_HUB_TOKEN"] = token
+ _apply_token_to_child_env(env, token, explicit = token_is_explicit)
# Clean PYTHONPATH means shadow only: torch still comes from site-packages while transformers 5.x and
# llm-compressor main come from the shadow, and dropping the inherited PYTHONPATH removes any parent