Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

Merge branch 'feat/loader-conformance-driver' into feat/loader-conformance-matrix

Alexander Eichhorn12f867d6b8ae
diff --cc invokeai/backend/model_manager/load/model_loaders/comfyui_state_dict_utils.py
index f64b433e1f,d1f8e29d5d..1795f89c66
--- a/invokeai/backend/model_manager/load/model_loaders/comfyui_state_dict_utils.py
+++ b/invokeai/backend/model_manager/load/model_loaders/comfyui_state_dict_utils.py
@@@ -15,10 -15,9 +15,10 @@@ import torc
from invokeai.backend.quantization.fp8_scaled import (
expand_weight_scale,
is_scale_metadata_key,
- reject_mx_block_scale,
+ reject_undecoded_mx_scale,
)
from invokeai.backend.quantization.int8_convrot import reject_int8_layers_a_plain_fold_cannot_decode
+from invokeai.backend.quantization.nvfp4 import reject_nvfp4_layers_a_plain_fold_cannot_decode
def _strip_comfyui_prefix(sd: dict) -> dict:
@@@ -74,9 -72,7 +74,9 @@@ def _dequantize_comfyui_fp8(sd: dict, c
break
if weight_key not in sd:
continue
+ # Before the cast: `.to(compute_dtype)` on an E8M0 grid converts the exponent bytes to
+ # floats and loses the only evidence of what they were.
- reject_mx_block_scale(weight_key[: -len(".weight")], sd[scale_key])
+ reject_undecoded_mx_scale(weight_key[: -len(".weight")], sd[scale_key])
weight = sd[weight_key].to(compute_dtype)
scale = sd[scale_key].to(compute_dtype)
# Through the shared expansion rather than a local copy of it. The copy that used to live
diff --cc invokeai/backend/model_manager/load/model_loaders/mistral_encoder.py
index 50598152be,62889637c9..bdff17bae4
--- a/invokeai/backend/model_manager/load/model_loaders/mistral_encoder.py
+++ b/invokeai/backend/model_manager/load/model_loaders/mistral_encoder.py
@@@ -68,7 -68,6 +68,7 @@@ from invokeai.backend.quantization.fp8_
parse_quantization_metadata,
predict_cast_state_dict_size,
read_safetensors_metadata,
- reject_mx_block_scale,
++ reject_undecoded_mx_scale,
should_keep_fp8_weights,
split_fp8_scaled_layers,
strip_layer_path_prefix,
@@@ -566,11 -565,6 +566,11 @@@ def _drop_quantization_metadata(sd: dic
"""
dequantized = 0
for weight_key, scale_key in list(iter_weight_scale_pairs(sd)):
- # The one scheme no pass upstream of this fold catches. `extract_fp8_scaled_layers` refuses
++ # The one scheme no pass upstream of this fold catches. `extract_fp8_scaled_layers` decodes
+ # an MX grid, but it only runs on the branch that *keeps* fp8; this is the other one, taken
+ # wherever fp8 cannot be held at all. Without this the exponent bytes are folded as linear
+ # multipliers -- around 120-135 -- at the right shape and dtype, with nothing logged.
- reject_mx_block_scale(weight_key[: -len(".weight")], sd[scale_key])
++ reject_undecoded_mx_scale(weight_key[: -len(".weight")], sd[scale_key])
weight = sd[weight_key].float()
# `expand_weight_scale` rather than a local broadcast: a per-output-channel scale is 1-D of
# length `out`, and `(out, in) * (out,)` aligns on the *last* axis, so it scales input
diff --cc invokeai/backend/model_manager/load/model_loaders/z_image.py
index decfec4be5,5a93c37ef7..273c6dd4f9
--- a/invokeai/backend/model_manager/load/model_loaders/z_image.py
+++ b/invokeai/backend/model_manager/load/model_loaders/z_image.py
@@@ -52,8 -52,7 +52,8 @@@ from invokeai.backend.quantization.fp8_
parse_quantization_metadata,
predict_cast_state_dict_size,
read_safetensors_metadata,
- reject_mx_block_scale,
reject_quantized_side_channel,
++ reject_undecoded_mx_scale,
split_fp8_scaled_layers,
split_qkv_sidechannel,
strip_layer_path_prefix,
@@@ -1166,9 -1165,6 +1166,9 @@@ def _fold_comfy_scaled_weights(sd: dict
"""
folded = 0
for weight_key, scale_key in list(iter_weight_scale_pairs(sd)):
+ # Before the cast: `.float()` on an E8M0 grid turns the exponent bytes into ordinary numbers
+ # and loses the only evidence of what they were.
- reject_mx_block_scale(weight_key[: -len(".weight")], sd[scale_key])
++ reject_undecoded_mx_scale(weight_key[: -len(".weight")], sd[scale_key])
# Float8 needs `.float()`; torch has no direct type promotion for it.
weight_float = sd[weight_key].float()
scale = expand_weight_scale(weight_float, sd[scale_key].float(), weight_key)
diff --cc tests/backend/model_manager/load/test_scaled_fp8_fold_axis.py
index 301e93cebf,480664b084..20a997ac9d
--- a/tests/backend/model_manager/load/test_scaled_fp8_fold_axis.py
+++ b/tests/backend/model_manager/load/test_scaled_fp8_fold_axis.py
@@@ -61,21 -59,6 +61,21 @@@ class TestMistralEncoderFold
rows = torch.tensor([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [3.0, 3.0, 3.0]], dtype=torch.bfloat16)
assert torch.equal(sd["layer.weight"], rows), "scale must vary down the rows, not across them"
+ def test_an_mx_block_scale_is_refused_rather_than_folded_as_a_multiplier(self) -> None:
+ """An MXFP8 grid is E8M0 exponent *bytes*: 127 means 2**0, i.e. a scale of 1.0. Folded as a
+ linear multiplier it makes every weight ~127x too large, at the right shape and the right
- dtype, with nothing raised and nothing logged. `extract_fp8_scaled_layers` has refused this
- since MXFP8 was recognised -- but these folds do not go through it, so the refusal never
- reached them.
++ dtype, with nothing raised and nothing logged. `extract_fp8_scaled_layers` decodes these grids
++ (`decode_mx_block_scales`) -- but these folds do not go through it, so they took the bytes as
++ numbers.
+ """
+ sd = {
+ "layer.weight": torch.ones(128, 128).to(torch.float8_e4m3fn),
+ "layer.weight_scale": torch.full((128, 4), 127, dtype=torch.uint8),
+ }
+
+ with pytest.raises(NotImplementedError, match="MXFP8"):
+ _drop_quantization_metadata(sd, MagicMock(), target_dtype=torch.bfloat16)
+
class TestZImageQwen3EncoderFold:
"""The Z-Image Qwen3 encoder's single-file loader folds the same way."""
@@@ -118,21 -101,6 +118,21 @@@
}
)
+ def test_an_mx_block_scale_is_refused_rather_than_folded_as_a_multiplier(self) -> None:
+ """An MXFP8 grid is E8M0 exponent *bytes*: 127 means 2**0, i.e. a scale of 1.0. Folded as a
+ linear multiplier it makes every weight ~127x too large, at the right shape and the right
- dtype, with nothing raised and nothing logged. `extract_fp8_scaled_layers` has refused this
- since MXFP8 was recognised -- but these folds do not go through it, so the refusal never
- reached them.
++ dtype, with nothing raised and nothing logged. `extract_fp8_scaled_layers` decodes these grids
++ (`decode_mx_block_scales`) -- but these folds do not go through it, so they took the bytes as
++ numbers.
+ """
+ sd = {
+ "layer.weight": torch.ones(128, 128).to(torch.float8_e4m3fn),
+ "layer.weight_scale": torch.full((128, 4), 127, dtype=torch.uint8),
+ }
+
+ with pytest.raises(NotImplementedError, match="MXFP8"):
+ _fold_comfy_scaled_weights(sd, torch.bfloat16)
+
class TestSharedComfyFold:
"""`_dequantize_comfyui_fp8`, the copy the Wan (`wan.py:511`) and Qwen-Image
@@@ -187,57 -155,6 +187,57 @@@
assert torch.equal(folded, torch.full((3, 2), 2.0))
+ def test_an_nvfp4_layer_is_refused_rather_than_folded_over_its_packed_codes(self) -> None:
+ """nvfp4 packs two 4-bit codes per byte and carries a block-scale grid, one entry per 16
+ logical elements. The shapes line up by accident -- eight packed columns per grid entry --
+ so this fold, which has no dtype gate, multiplied the packed bytes and reported a
+ dequantized weight. The result is half the width the model needs, so `load_state_dict`
+ catches it; it just says "size mismatch" after a log line claiming success.
+ """
+ tensors, _ = nvfp4_signed_tensors("layer", torch.randint(0, 2, (128, 64), dtype=torch.bool))
+
+ with pytest.raises(ValueError, match="does not support nvfp4"):
+ _dequantize_comfyui_fp8(tensors, torch.float32)
+
+ def test_an_nvfp4_layer_missing_its_global_scale_is_refused_too(self) -> None:
+ """The half-state: packed codes and a block-scale grid, no `weight_scale_2`. It is the shape
+ `_find_nvfp4_layers` refuses by name, so a build that loses the global scale somewhere
+ upstream arrives here looking like ordinary scaled fp8 -- and the fold takes it exactly as
+ readily as the whole layer, for the same reason and to the same end. Keying the guard on
+ `weight_scale_2` alone, which is what the decode keys on, would let this one through."""
+ tensors, _ = nvfp4_signed_tensors("layer", torch.randint(0, 2, (128, 64), dtype=torch.bool))
+ del tensors["layer.weight_scale_2"]
+
+ with pytest.raises(ValueError, match="does not support nvfp4"):
+ _dequantize_comfyui_fp8(tensors, torch.float32)
+
+ def test_a_layer_whose_packed_payload_was_taken_out_first_still_folds(self) -> None:
+ """Two loaders reach this fold: Wan, which has no nvfp4 support at all, and the Qwen2.5-VL
+ encoder, which calls `pop_nvfp4_layers` first (`qwen_image.py:473`) and ships the layers
+ packed. That pop takes the weight and both scales out together, so by the time the fold runs
+ there is nothing for the guard to see -- asserted, because a guard that fired here anyway
+ would break the one supported nvfp4 build that passes through."""
+ tensors, _ = nvfp4_signed_tensors("packed", torch.randint(0, 2, (128, 64), dtype=torch.bool))
+ pop_nvfp4_layers(tensors, header_layers={"packed": {"format": "nvfp4"}})
+ tensors.update({"layer.weight": torch.ones(4, 3), "layer.weight_scale": torch.tensor([1.0, 2.0, 3.0, 4.0])})
+
+ assert _dequantize_comfyui_fp8(tensors, torch.float32) == 1
+
+ def test_an_mx_block_scale_is_refused_rather_than_folded_as_a_multiplier(self) -> None:
+ """An MXFP8 grid is E8M0 exponent *bytes*: 127 means 2**0, i.e. a scale of 1.0. Folded as a
+ linear multiplier it makes every weight ~127x too large, at the right shape and the right
- dtype, with nothing raised and nothing logged. `extract_fp8_scaled_layers` has refused this
- since MXFP8 was recognised -- but these folds do not go through it, so the refusal never
- reached them.
++ dtype, with nothing raised and nothing logged. `extract_fp8_scaled_layers` decodes these grids
++ (`decode_mx_block_scales`) -- but these folds do not go through it, so they took the bytes as
++ numbers.
+ """
+ sd = {
+ "layer.weight": torch.ones(128, 128).to(torch.float8_e4m3fn),
+ "layer.weight_scale": torch.full((128, 4), 127, dtype=torch.uint8),
+ }
+
+ with pytest.raises(NotImplementedError, match="MXFP8"):
+ _dequantize_comfyui_fp8(sd, torch.bfloat16)
+
class TestFlux2Fold:
"""FLUX.2's own copy (`flux.py`). It is unreachable at its one call site -- extraction takes