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.

gpt-oss: accept an absolute transformers_weights inside the local model dir (#16061)

Daniel Hand4ebf72c8610
diff --git a/tests/test_gpt_oss_transformers_weights_layout.py b/tests/test_gpt_oss_transformers_weights_layout.py
index 254efff5f78..560050bf1d1 100644
--- a/tests/test_gpt_oss_transformers_weights_layout.py
+++ b/tests/test_gpt_oss_transformers_weights_layout.py
@@ -119,3 +119,24 @@ def test_hub_probe_reads_the_explicit_file(monkeypatch, tmp_path):
cfg = _cfg("chosen.safetensors")
assert loader_utils.gpt_oss_checkpoint_has_fused_experts(cfg, "org/repo") is True
assert asked == ["chosen.safetensors"]
+
+
[email protected]("subfolder", [None, "sub"])
+def test_absolute_explicit_name_inside_the_model_dir_decides_the_layout(tmp_path, subfolder):
+ # transformers accepts an absolute name inside the real model dir (+ subfolder).
+ path = _repo(tmp_path, SPLIT, SPLIT)
+ root = tmp_path / "ckpt" / subfolder if subfolder else tmp_path / "ckpt"
+ root.mkdir(exist_ok = True)
+ _save(root / "model.safetensors", SPLIT)
+ _save(root / "chosen.safetensors", FUSED)
+ cfg = _cfg(str(root / "chosen.safetensors"))
+ probe = loader_utils.gpt_oss_checkpoint_has_fused_experts
+ assert probe(cfg, path, subfolder = subfolder) is True
+ _save(tmp_path / "outside.safetensors", FUSED)
+ outside = str(tmp_path / "outside.safetensors")
+ assert (
+ loader_utils._checkpoint_tensor_dtypes(
+ path, subfolder = subfolder, transformers_weights = outside
+ )
+ is None
+ )
diff --git a/unsloth/models/loader_utils.py b/unsloth/models/loader_utils.py
index 5d670bb83d8..14d907ea6bd 100644
--- a/unsloth/models/loader_utils.py
+++ b/unsloth/models/loader_utils.py
@@ -4192,13 +4192,14 @@ def _hub_live_commit(
_ESCAPING_WEIGHTS_FILENAME = object()
-def _explicit_weights_filename(transformers_weights):
- """File transformers loads instead of standard names; None if unused, sentinel if it escapes the model dir."""
+def _explicit_weights_filename(transformers_weights, model_dir = None):
+ """File transformers loads instead of standard names; None if unused, sentinel if it escapes the model dir.
+ `model_dir` is the real local dir (+ subfolder): transformers accepts an absolute name inside it."""
if not isinstance(transformers_weights, str) or not transformers_weights.endswith(
(".safetensors", ".safetensors.index.json")
):
return None
- base = os.path.abspath("model_dir")
+ base = os.path.abspath(model_dir or "model_dir")
try:
contained = (
os.path.commonpath([base, os.path.abspath(os.path.join(base, transformers_weights))])
@@ -4224,7 +4225,10 @@ def _checkpoint_tensor_dtypes(
file order, `variant`, `use_safetensors` and `transformers_weights`. Raises _UnreadableCheckpointNames for an unreadable lone .bin."""
if not isinstance(model_name, str) or not model_name:
return None
- explicit = _explicit_weights_filename(transformers_weights)
+ local_root = None
+ if os.path.isdir(model_name):
+ local_root = os.path.join(model_name, subfolder) if subfolder else model_name
+ explicit = _explicit_weights_filename(transformers_weights, local_root)
if explicit is _ESCAPING_WEIGHTS_FILENAME:
return None
st_index, st_single = (