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.

Stamp no bitsandbytes config on a dequantized fp8 load, and read a quantization_config object before stamping (#14803)

Daniel Hanf0c87b1dcec3
diff --git a/tests/test_fp8_16bit_text_loader_dequant.py b/tests/test_fp8_16bit_text_loader_dequant.py
index 08c7f16650e..015d0d7cbae 100644
--- a/tests/test_fp8_16bit_text_loader_dequant.py
+++ b/tests/test_fp8_16bit_text_loader_dequant.py
@@ -212,3 +212,97 @@ def test_dequantized_load_saves_and_reloads_16bit_weights(tmp_path, arch):
after = AutoModelForCausalLM.from_pretrained(str(dst), dtype = torch.bfloat16).state_dict()
assert before.keys() == after.keys()
assert all(torch.equal(before[k], after[k].cpu()) for k in before)
+
+
[email protected](not _HAS_DEQUANTIZE, reason = "transformers has no fp8 dequantize flag")
+def test_transformers_own_dequantize_is_marked_and_saves_16bit(tmp_path):
+ """No GPU: transformers dequantizes by itself, the branch it takes below sm89 too."""
+ src, dst = tmp_path / "src", tmp_path / "saved"
+ src.mkdir()
+ _write_tiny_block_fp8_llama(str(src))
+ code = f"""
+import os, json, torch
+from transformers import AutoModelForCausalLM
+from safetensors import safe_open
+from unsloth.models.loader_utils import _drop_fp8_dequantize_conversions
+model = AutoModelForCausalLM.from_pretrained({str(src)!r}, dtype = torch.bfloat16)
+assert getattr(model.config, "quantization_config", None) is None
+print("DROPPED", _drop_fp8_dequantize_conversions(model))
+print("MARKED", int(getattr(model, "_unsloth_fp8_dequantized", False)))
+model.save_pretrained({str(dst)!r})
+dtypes = set()
+for name in os.listdir({str(dst)!r}):
+ if name.endswith(".safetensors"):
+ with safe_open(os.path.join({str(dst)!r}, name), "pt") as f:
+ dtypes |= {{f.get_slice(k).get_dtype() for k in f.keys()}}
+print("DTYPES", ",".join(sorted(dtypes)))
+"""
+ env = dict(os.environ, CUDA_VISIBLE_DEVICES = "", UNSLOTH_ALLOW_CPU = "1")
+ out = subprocess.run(
+ [sys.executable, "-c", code], capture_output = True, text = True, timeout = 600, env = env
+ )
+ lines = dict(
+ l.split(" ", 1)
+ for l in out.stdout.splitlines()
+ if l.startswith(("DROPPED", "MARKED", "DTYPES"))
+ )
+ assert "DTYPES" in lines, (out.stdout[-2000:], out.stderr[-2000:])
+ assert int(lines["DROPPED"]) > 0 and lines["MARKED"] == "1", lines
+ assert lines["DTYPES"] == "BF16", lines
+
+
+def test_a_dequantized_fp8_load_gets_no_bitsandbytes_stamp():
+ # transformers deletes the fp8 config of a dequantizing load, so the quant type reads None like an
+ # unquantized load; the default load_in_4bit stamp then put a bnb config over 16bit weights.
+ import ast
+
+ path = os.path.join(
+ os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "unsloth/models/loader.py"
+ )
+ tree = ast.parse(open(path, encoding = "utf-8").read())
+
+ def assigned(name):
+ return [
+ ast.unparse(node.value)
+ for node in ast.walk(tree)
+ if isinstance(node, ast.Assign)
+ and any(getattr(t, "id", None) == name for t in node.targets)
+ ]
+
+ stamps = [v for v in assigned("_stamp_bnb") if "_quant" in v]
+ assert len(stamps) == 2 and all("_unsloth_fp8_dequantized" in v for v in stamps), stamps
+ # A config object (FineGrainedFP8Config kept by an sm89+ fp8 load) is read too.
+ quants = assigned("_quant")
+ assert len(quants) == 2 and all("_loaded_quant_method" in v for v in quants), quants
+
+
+def test_loaded_quant_method_reads_dicts_and_config_objects():
+ from unsloth.models.loader_utils import _loaded_quant_method
+
+ assert _loaded_quant_method(SimpleNamespace(quantization_config = None)) is None
+ assert _loaded_quant_method(SimpleNamespace()) is None
+ assert _loaded_quant_method(SimpleNamespace(quantization_config = {"load_in_4bit": True})) is None
+ assert _loaded_quant_method(SimpleNamespace(quantization_config = dict(_BLOCK_FP8))) == "fp8"
+ if _HAS_DEQUANTIZE:
+ qc = FineGrainedFP8Config.from_dict(dict(_BLOCK_FP8))
+ assert _loaded_quant_method(SimpleNamespace(quantization_config = qc)) == "fp8"
+
+
[email protected](not _HAS_DEQUANTIZE, reason = "transformers has no fp8 dequantize flag")
+def test_only_a_load_without_fp8_config_is_marked_dequantized():
+ # sm89+ keeps FP8Linear and its fp8 quantization_config: the bitsandbytes stamp must still see fp8.
+ from transformers.integrations.finegrained_fp8 import Fp8Dequantize
+ from unsloth.models.loader_utils import _drop_fp8_dequantize_conversions
+
+ def model(config):
+ conversion = SimpleNamespace(
+ operations = [Fp8Dequantize(None)], _original_target_patterns = ["weight"]
+ )
+ return SimpleNamespace(config = config, _weight_conversions = [conversion])
+
+ kept = model(SimpleNamespace(quantization_config = dict(_BLOCK_FP8)))
+ _drop_fp8_dequantize_conversions(kept)
+ assert not getattr(kept, "_unsloth_fp8_dequantized", False)
+ dequantized = model(SimpleNamespace())
+ assert _drop_fp8_dequantize_conversions(dequantized) == 1
+ assert dequantized._weight_conversions == [] and dequantized._unsloth_fp8_dequantized
diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py
index 1901d5865ad..4a361dd98c7 100644
--- a/unsloth/models/loader.py
+++ b/unsloth/models/loader.py
@@ -54,6 +54,7 @@ from .loader_utils import (
_fp8_load_as_is,
_fp8_dequantize_quantization_config,
_drop_fp8_dequantize_conversions,
+ _loaded_quant_method,
_fp8_needs_offline_quantize,
_offline_quantize_to_fp8,
_tag_model_with_fp8_torchao_config,
@@ -1982,7 +1983,12 @@ class FastLanguageModel(FastLlamaModel):
# 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 a synthetic bnb config would corrupt its real one.
try:
from unsloth_zoo.utils import get_quant_type
- _stamp_bnb = get_quant_type(model.config) in (None, "bitsandbytes")
+
+ # get_quant_type skips config objects (kept FineGrainedFP8Config); a dequantized fp8 load is 16bit.
+ _quant = get_quant_type(model.config) or _loaded_quant_method(model.config)
+ _stamp_bnb = _quant in (None, "bitsandbytes") and not getattr(
+ model, "_unsloth_fp8_dequantized", False
+ )
except Exception:
_stamp_bnb = True
if _stamp_bnb:
@@ -3398,7 +3404,12 @@ class FastModel(FastBaseModel):
# load_in_4bit is the requested flag, not the effective one: a non-bnb checkpoint had bnb disabled by check_and_disable, so a synthetic bnb config would corrupt its real one.
try:
from unsloth_zoo.utils import get_quant_type
- _stamp_bnb = get_quant_type(model.config) in (None, "bitsandbytes")
+
+ # get_quant_type skips config objects (kept FineGrainedFP8Config); a dequantized fp8 load is 16bit.
+ _quant = get_quant_type(model.config) or _loaded_quant_method(model.config)
+ _stamp_bnb = _quant in (None, "bitsandbytes") and not getattr(
+ model, "_unsloth_fp8_dequantized", False
+ )
except Exception:
_stamp_bnb = True
if _stamp_bnb:
diff --git a/unsloth/models/loader_utils.py b/unsloth/models/loader_utils.py
index 9d676d7f129..adaec60fa5b 100644
--- a/unsloth/models/loader_utils.py
+++ b/unsloth/models/loader_utils.py
@@ -2086,6 +2086,9 @@ def _drop_fp8_dequantize_conversions(model):
if not _has_dequant(conversion):
kept.append(conversion)
continue
+ if getattr(getattr(model, "config", None), "quantization_config", None) is None:
+ # transformers dropped the fp8 config: 16bit weights, so no bitsandbytes stamp either.
+ model._unsloth_fp8_dequantized = True
if all(isinstance(op, Fp8Dequantize) for op in operations):
# weight + weight_scale_inv -> weight: the 16bit weight saves as it is.
changed += 1
@@ -2129,6 +2132,17 @@ def _drop_fp8_dequantize_conversions(model):
return changed
+def _loaded_quant_method(config):
+ """quant_method of `config.quantization_config` as a lower-case string, dict or config object, else None."""
+ qc = getattr(config, "quantization_config", None)
+ if qc is None:
+ return None
+ method = qc.get("quant_method") if isinstance(qc, dict) else getattr(qc, "quant_method", None)
+ if method is None:
+ return None
+ return str(getattr(method, "value", method)).lower()
+
+
def _fp8_dequantize_quantization_config(model_config):
"""The checkpoint's fine-grained fp8 config with `dequantize = True` for an explicit 16bit load, else None.
On FP8-capable GPUs transformers otherwise keeps FP8Linear layers; FastModel passes the same flag."""