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.

FP8: strip activation_scale sources when matching the 16-bit save conversion (#16156)

Daniel Han1c23549a2ba6
diff --git a/tests/test_fp8_dequant_save_policy.py b/tests/test_fp8_dequant_save_policy.py
index 89a849bbdc0..5ead565c8b2 100644
--- a/tests/test_fp8_dequant_save_policy.py
+++ b/tests/test_fp8_dequant_save_policy.py
@@ -220,9 +220,6 @@ def test_explicit_and_automatic_dequantize_reload(tmp_path, explicit, monkeypatc
@pytest.mark.skipif(not _HAS_DEQUANTIZE, reason = "transformers has no fp8 dequantize flag")
def test_heterogeneous_composite_keeps_one_copy_of_an_unwrapped_converter(monkeypatch):
- # Two submodels whose converters share a type and target but differ in sources: only the
- # `.weight`-sourced one gets the fp8 dequant prepended. The other one is kept as loaded and must
- # not gain a second copy, or save_pretrained would reverse it twice.
import transformers.conversion_mapping as cm
from types import SimpleNamespace
from transformers.core_model_loading import (
@@ -232,6 +229,7 @@ def test_heterogeneous_composite_keeps_one_copy_of_an_unwrapped_converter(monkey
WeightConverter,
)
from transformers.quantizers.quantizer_finegrained_fp8 import FineGrainedFP8HfQuantizer
+ from transformers.quantizers.base import HfQuantizer
target = "mlp.experts.gate_up_proj"
@@ -252,8 +250,13 @@ def test_heterogeneous_composite_keeps_one_copy_of_an_unwrapped_converter(monkey
FineGrainedFP8Config(dequantize = True, weight_block_size = [BLOCK, BLOCK])
)
quantizer.pre_quantized = True
- loaded = quantizer.update_weight_conversions(plain())
- model = SimpleNamespace(_weight_conversions = loaded)
+ model = SimpleNamespace(named_parameters = lambda: iter(()))
+ # Zoo's subclass wrapper hides the model argument; the base interface retains it.
+ if "model" in inspect.signature(HfQuantizer.update_weight_conversions).parameters:
+ loaded = quantizer.update_weight_conversions(plain(), model)
+ else:
+ loaded = quantizer.update_weight_conversions(plain())
+ model._weight_conversions = loaded
monkeypatch.setattr(cm, "get_model_conversion_mapping", lambda *a, **k: plain())
assert _helper()(model) > 0
diff --git a/unsloth/models/loader_utils.py b/unsloth/models/loader_utils.py
index 7774ab77912..068e6f60c5c 100644
--- a/unsloth/models/loader_utils.py
+++ b/unsloth/models/loader_utils.py
@@ -2128,9 +2128,7 @@ def _fp8_needs_offline_quantize(resolved_name, requested_name, fast_inference):
def _drop_fp8_dequantize_conversions(model):
- """Forget the Fp8Dequantize load conversions of a dequantizing fp8 load. save_pretrained reverses
- them into Fp8Quantize, writing fp8 weights under a config with no quantization_config, so a reload
- reads the raw fp8 values as 16bit weights. Returns how many conversions changed."""
+ """Drop reverse FP8 conversions that would save unmarked FP8 weights as a 16-bit checkpoint."""
conversions = getattr(model, "_weight_conversions", None)
if not conversions:
return 0
@@ -2144,9 +2142,12 @@ def _drop_fp8_dequantize_conversions(model):
return tuple(p[:-1] if p.endswith("$") else p for p in patterns)
def _sources(patterns):
- # The dequantizing copy anchors `.weight` sources with `$` and adds `.weight_scale_inv$` ones.
patterns = [p[:-1] if p.endswith("$") else p for p in patterns or []]
- return tuple(sorted(p for p in patterns if not p.endswith(".weight_scale_inv")))
+ return tuple(
+ sorted(
+ p for p in patterns if not p.endswith((".weight_scale_inv", ".activation_scale"))
+ )
+ )
def _source_forms(conversion):
return {
@@ -2174,7 +2175,6 @@ def _drop_fp8_dequantize_conversions(model):
plain = None
kept, changed = [], 0
- # Converters the load left untouched stay as they are; a counterpart equal to one is not re-added.
retained = {_signature(c) for c in conversions if not _has_dequant(c)}
for conversion in conversions:
operations = getattr(conversion, "operations", None) or []
@@ -2185,19 +2185,15 @@ def _drop_fp8_dequantize_conversions(model):
# 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
continue
- # A model converter (expert merge, ...) with the dequant prepended: keep its quantizer-free form.
if plain is None:
try:
from transformers.conversion_mapping import get_model_conversion_mapping
plain = get_model_conversion_mapping(model, None, None)
except Exception:
plain = []
- # A composite model scopes one converter per submodel; the dequantizing copy can lose its
- # scope_prefix, so an unscoped conversion is replaced by every scoped counterpart with the same
- # sources, targets and operations (another submodel's converter can share type and targets).
+ # Dequantization can lose scope_prefix; restore every matching scoped counterpart.
scope = getattr(conversion, "scope_prefix", None)
sources = _source_forms(conversion)
matches = [