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.

loader: build UNSLOTH_MODEL_NAME fresh per load (fixes gpt-oss merged_16bit reload) (#6504)

Daniel Hand8d2d5c84d26
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index 08802f030e8..2f449453f95 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -2460,7 +2460,10 @@ class FastLlamaModel:
# Add to kwargs
kwargs["rope_scaling"] = rope_scaling
- from .loader_utils import check_and_disable_bitsandbytes_loading
+ from .loader_utils import (
+ check_and_disable_bitsandbytes_loading,
+ sync_unsloth_model_name_bnb_flags,
+ )
from unsloth_zoo.utils import get_quant_type
# Extract load_in_8bit from kwargs if provided
@@ -2470,6 +2473,9 @@ class FastLlamaModel:
load_in_4bit, load_in_8bit, _ckpt_quant_method = check_and_disable_bitsandbytes_loading(
model_config, load_in_4bit = load_in_4bit, load_in_8bit = load_in_8bit
)
+ # Correct UNSLOTH_MODEL_NAME's bnb tokens now that the effective bnb state is known
+ # (the per-load env was built before remap/disable). gpt-oss only; no-op otherwise.
+ sync_unsloth_model_name_bnb_flags(load_in_4bit, load_in_8bit)
bnb_config = None
_ckpt_qcfg = getattr(model_config, "quantization_config", None)
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 459b6ed32d1..5fc1755febf 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -826,21 +826,30 @@ class FastLanguageModel(FastLlamaModel):
if load_in_4bit:
# Fix up bitsandbytes config, but respect user-provided quantization_config
if quantization_config is None:
- compute_dtype = dtype_from_config(model.config)
- quantization_config = {
- # Sometimes compute_dtype is not a string!!
- "bnb_4bit_compute_dtype": compute_dtype,
- "bnb_4bit_quant_type": "nf4",
- "bnb_4bit_use_double_quant": True,
- "llm_int8_enable_fp32_cpu_offload": False,
- "llm_int8_has_fp16_weight": False,
- "llm_int8_skip_modules": None,
- "llm_int8_threshold": 6.0,
- "load_in_4bit": True,
- "load_in_8bit": False,
- "quant_method": "bitsandbytes",
- }
- model.config.update({"quantization_config": quantization_config})
+ # `load_in_4bit` is the requested flag, not the effective one: a non-bnb
+ # checkpoint (MXFP4/gptq/awq) had bnb disabled by check_and_disable, so stamping a
+ # synthetic bnb config would corrupt its real one. Only stamp bnb/unquantized.
+ try:
+ from unsloth_zoo.utils import get_quant_type
+ _stamp_bnb = get_quant_type(model.config) in (None, "bitsandbytes")
+ except Exception:
+ _stamp_bnb = True
+ if _stamp_bnb:
+ compute_dtype = dtype_from_config(model.config)
+ quantization_config = {
+ # Sometimes compute_dtype is not a string!!
+ "bnb_4bit_compute_dtype": compute_dtype,
+ "bnb_4bit_quant_type": "nf4",
+ "bnb_4bit_use_double_quant": True,
+ "llm_int8_enable_fp32_cpu_offload": False,
+ "llm_int8_has_fp16_weight": False,
+ "llm_int8_skip_modules": None,
+ "llm_int8_threshold": 6.0,
+ "load_in_4bit": True,
+ "load_in_8bit": False,
+ "quant_method": "bitsandbytes",
+ }
+ model.config.update({"quantization_config": quantization_config})
else:
if hasattr(quantization_config, "to_dict"):
model.config.update({"quantization_config": quantization_config.to_dict()})
@@ -1234,10 +1243,27 @@ class FastModel(FastBaseModel):
# Save model types and loading method
lowered_model_name = model_name.lower()
- string = os.environ.get("UNSLOTH_MODEL_NAME", "") + model_types_all
- if load_in_4bit:
+ # Build UNSLOTH_MODEL_NAME fresh from THIS load's model types + flags; do not prepend the
+ # inherited os.environ value (a stale "_load_in_4bit_" from an earlier load, e.g. across a
+ # save->reload subprocess, would push gpt-oss onto the BnB router patch when later loading
+ # a 16bit checkpoint -> "weights not initialized"). Only the type tokens and the load flags
+ # below are consumed downstream; the raw model name/path is excluded so a path containing a
+ # flag sentinel cannot be misread.
+ #
+ # Encode the EFFECTIVE bnb state: a non-bnb checkpoint (MXFP4/gptq/awq) has load_in_4bit
+ # disabled later by check_and_disable, so recording the requested flag here would route a
+ # native MXFP4 gpt-oss onto the BnB router patch. This is only an EARLY best-effort (an
+ # adapter-only PEFT repo has model_config=None here, and the base may be remapped); the
+ # authoritative correction is sync_unsloth_model_name_bnb_flags(...) after check_and_disable.
+ try:
+ from unsloth_zoo.utils import get_quant_type
+ _bnb_compatible_quant = get_quant_type(model_config) in (None, "bitsandbytes")
+ except Exception:
+ _bnb_compatible_quant = True
+ string = model_types_all
+ if load_in_4bit and _bnb_compatible_quant:
string += "_load_in_4bit_"
- if load_in_8bit:
+ if load_in_8bit and _bnb_compatible_quant:
string += "_load_in_8bit_"
if load_in_16bit:
string += "_load_in_16bit_"
@@ -1659,21 +1685,30 @@ class FastModel(FastBaseModel):
if load_in_4bit:
# Fix up bitsandbytes config, but respect user-provided quantization_config
if quantization_config is None:
- compute_dtype = dtype_from_config(model.config)
- quantization_config = {
- # Sometimes compute_dtype is not a string!!
- "bnb_4bit_compute_dtype": compute_dtype,
- "bnb_4bit_quant_type": "nf4",
- "bnb_4bit_use_double_quant": True,
- "llm_int8_enable_fp32_cpu_offload": False,
- "llm_int8_has_fp16_weight": False,
- "llm_int8_skip_modules": None,
- "llm_int8_threshold": 6.0,
- "load_in_4bit": True,
- "load_in_8bit": False,
- "quant_method": "bitsandbytes",
- }
- model.config.update({"quantization_config": quantization_config})
+ # `load_in_4bit` is the requested flag, not the effective one: a non-bnb
+ # checkpoint (MXFP4/gptq/awq) had bnb disabled by check_and_disable, so stamping a
+ # synthetic bnb config would corrupt its real one. Only stamp bnb/unquantized.
+ try:
+ from unsloth_zoo.utils import get_quant_type
+ _stamp_bnb = get_quant_type(model.config) in (None, "bitsandbytes")
+ except Exception:
+ _stamp_bnb = True
+ if _stamp_bnb:
+ compute_dtype = dtype_from_config(model.config)
+ quantization_config = {
+ # Sometimes compute_dtype is not a string!!
+ "bnb_4bit_compute_dtype": compute_dtype,
+ "bnb_4bit_quant_type": "nf4",
+ "bnb_4bit_use_double_quant": True,
+ "llm_int8_enable_fp32_cpu_offload": False,
+ "llm_int8_has_fp16_weight": False,
+ "llm_int8_skip_modules": None,
+ "llm_int8_threshold": 6.0,
+ "load_in_4bit": True,
+ "load_in_8bit": False,
+ "quant_method": "bitsandbytes",
+ }
+ model.config.update({"quantization_config": quantization_config})
else:
if hasattr(quantization_config, "to_dict"):
model.config.update({"quantization_config": quantization_config.to_dict()})
diff --git a/unsloth/models/loader_utils.py b/unsloth/models/loader_utils.py
index 06937af7db1..9b9ba5220dd 100644
--- a/unsloth/models/loader_utils.py
+++ b/unsloth/models/loader_utils.py
@@ -395,6 +395,25 @@ def check_and_disable_bitsandbytes_loading(
return load_in_4bit, load_in_8bit, quant_method
+def sync_unsloth_model_name_bnb_flags(load_in_4bit, load_in_8bit):
+ """Make UNSLOTH_MODEL_NAME's `_load_in_4bit_`/`_load_in_8bit_` tokens match the EFFECTIVE bnb
+ state (after get_model_name remap + check_and_disable). The per-load env is built from the
+ pre-remap config (None for adapter-only PEFT repos), so its tokens can be wrong once the base
+ resolves. Only the gpt-oss patch reads them, so this is gated to gpt-oss; no-op otherwise."""
+ name = os.environ.get("UNSLOTH_MODEL_NAME", "")
+ if "gpt_oss" not in name.replace("-", "_"):
+ return
+ for flag, present in (
+ ("_load_in_4bit_", bool(load_in_4bit)),
+ ("_load_in_8bit_", bool(load_in_8bit)),
+ ):
+ if present and flag not in name:
+ name += flag
+ elif not present and flag in name:
+ name = name.replace(flag, "")
+ os.environ["UNSLOTH_MODEL_NAME"] = name
+
+
def _get_fp8_mode_and_check_settings(
load_in_fp8: Union[bool, str],
fast_inference: bool,
diff --git a/unsloth/models/vision.py b/unsloth/models/vision.py
index 3f8d9e6ce0f..b68f0361e3f 100644
--- a/unsloth/models/vision.py
+++ b/unsloth/models/vision.py
@@ -812,11 +812,17 @@ class FastBaseModel:
user_quantization_config = kwargs.get("quantization_config", None)
# Check if model already has a non-bitsandbytes quantization config (e.g. compressed-tensors/NVFP4)
- from .loader_utils import check_and_disable_bitsandbytes_loading
+ from .loader_utils import (
+ check_and_disable_bitsandbytes_loading,
+ sync_unsloth_model_name_bnb_flags,
+ )
load_in_4bit, load_in_8bit, _ = check_and_disable_bitsandbytes_loading(
auto_config, load_in_4bit = load_in_4bit, load_in_8bit = load_in_8bit
)
+ # Correct UNSLOTH_MODEL_NAME's bnb tokens now that the effective bnb state is known
+ # (the per-load env was built before remap/disable). gpt-oss only; no-op otherwise.
+ sync_unsloth_model_name_bnb_flags(load_in_4bit, load_in_8bit)
if full_finetuning and (load_in_4bit or load_in_8bit):
print(