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.

test(fp8): capture a mixed fp8 FLUX.2 layout with input scales

Alexander Eichhorn83dd95e9e59a
diff --git a/tests/backend/model_manager/load/state_dicts/flux2_transformer_fp8mixed_keys.py b/tests/backend/model_manager/load/state_dicts/flux2_transformer_fp8mixed_keys.py
new file mode 100644
index 0000000000..a5082d4bc9
--- /dev/null
+++ b/tests/backend/model_manager/load/state_dicts/flux2_transformer_fp8mixed_keys.py
@@ -0,0 +1,56 @@
+"""Representative key layout of a *mixed* fp8 FLUX.2 checkpoint.
+
+Captured from Comfy-Org's `flux2_dev_fp8mixed.safetensors`. Two properties make it worth having
+next to the all-fp8 fixtures:
+
+- It is *mixed*: only some Linears are quantized, and the rest stay bf16 in the same file. A
+ metadata filter that is too broad silently deletes those bf16 weights instead of only the scale
+ bookkeeping, which no all-fp8 fixture can catch.
+- Every quantized Linear carries a calibrated `.input_scale` next to its `.weight_scale`. That key
+ has to be stripped as well, or `load_state_dict(..., strict=True)` rejects it.
+
+Same subsetting rule as the sibling fixtures: block 0 of each stack plus every non-block key.
+Values are `(shape, dtype)`.
+"""
+
+state_dict_keys: dict[str, tuple[list[int], str]] = {
+ "double_blocks.0.img_attn.norm.key_norm.scale": ([128], "BF16"),
+ "double_blocks.0.img_attn.norm.query_norm.scale": ([128], "BF16"),
+ "double_blocks.0.img_attn.proj.weight": ([6144, 6144], "BF16"),
+ "double_blocks.0.img_attn.qkv.weight": ([18432, 6144], "BF16"),
+ "double_blocks.0.img_mlp.0.input_scale": ([], "F32"),
+ "double_blocks.0.img_mlp.0.weight": ([36864, 6144], "F8_E4M3"),
+ "double_blocks.0.img_mlp.0.weight_scale": ([], "F32"),
+ "double_blocks.0.img_mlp.2.input_scale": ([], "F32"),
+ "double_blocks.0.img_mlp.2.weight": ([6144, 18432], "F8_E4M3"),
+ "double_blocks.0.img_mlp.2.weight_scale": ([], "F32"),
+ "double_blocks.0.txt_attn.norm.key_norm.scale": ([128], "BF16"),
+ "double_blocks.0.txt_attn.norm.query_norm.scale": ([128], "BF16"),
+ "double_blocks.0.txt_attn.proj.weight": ([6144, 6144], "BF16"),
+ "double_blocks.0.txt_attn.qkv.weight": ([18432, 6144], "BF16"),
+ "double_blocks.0.txt_mlp.0.input_scale": ([], "F32"),
+ "double_blocks.0.txt_mlp.0.weight": ([36864, 6144], "F8_E4M3"),
+ "double_blocks.0.txt_mlp.0.weight_scale": ([], "F32"),
+ "double_blocks.0.txt_mlp.2.input_scale": ([], "F32"),
+ "double_blocks.0.txt_mlp.2.weight": ([6144, 18432], "F8_E4M3"),
+ "double_blocks.0.txt_mlp.2.weight_scale": ([], "F32"),
+ "double_stream_modulation_img.lin.weight": ([36864, 6144], "BF16"),
+ "double_stream_modulation_txt.lin.weight": ([36864, 6144], "BF16"),
+ "final_layer.adaLN_modulation.1.weight": ([12288, 6144], "BF16"),
+ "final_layer.linear.weight": ([128, 6144], "BF16"),
+ "guidance_in.in_layer.weight": ([6144, 256], "BF16"),
+ "guidance_in.out_layer.weight": ([6144, 6144], "BF16"),
+ "img_in.weight": ([6144, 128], "BF16"),
+ "single_blocks.0.linear1.input_scale": ([], "F32"),
+ "single_blocks.0.linear1.weight": ([55296, 6144], "F8_E4M3"),
+ "single_blocks.0.linear1.weight_scale": ([], "F32"),
+ "single_blocks.0.linear2.input_scale": ([], "F32"),
+ "single_blocks.0.linear2.weight": ([6144, 24576], "F8_E4M3"),
+ "single_blocks.0.linear2.weight_scale": ([], "F32"),
+ "single_blocks.0.norm.key_norm.scale": ([128], "BF16"),
+ "single_blocks.0.norm.query_norm.scale": ([128], "BF16"),
+ "single_stream_modulation.lin.weight": ([18432, 6144], "BF16"),
+ "time_in.in_layer.weight": ([6144, 256], "BF16"),
+ "time_in.out_layer.weight": ([6144, 6144], "BF16"),
+ "txt_in.weight": ([6144, 15360], "BF16"),
+}
diff --git a/tests/backend/model_manager/load/test_flux2_fp8mixed_keys.py b/tests/backend/model_manager/load/test_flux2_fp8mixed_keys.py
new file mode 100644
index 0000000000..1ab3165eb6
--- /dev/null
+++ b/tests/backend/model_manager/load/test_flux2_fp8mixed_keys.py
@@ -0,0 +1,120 @@
+"""A mixed fp8 FLUX.2 checkpoint must lose its scale bookkeeping and nothing else.
+
+`flux2_dev_fp8mixed` quantizes only some Linears; the rest stay bf16 in the same file. It also
+carries a calibrated `.input_scale` beside every `.weight_scale`, and — the reason this fixture
+earns its place — real learned parameters whose names end in `.scale`
+(`img_attn.norm.query_norm.scale`). A metadata filter that matches on "scale" anywhere, or on a
+bare `.scale` suffix, deletes those weights. An all-fp8 fixture cannot catch either mistake.
+"""
+
+import torch
+
+from invokeai.backend.model_manager.load.model_loaders.flux import Flux2CheckpointModel
+from invokeai.backend.quantization.fp8_scaled import FP8_DTYPE, is_scale_metadata_key
+from tests.backend.model_manager.load.state_dicts.flux2_transformer_fp8mixed_keys import (
+ state_dict_keys as mixed_keys,
+)
+
+_DTYPES = {"F8_E4M3": FP8_DTYPE, "F32": torch.float32, "BF16": torch.bfloat16}
+
+SCALE_VALUE = 0.25
+
+
+def _mock_state_dict() -> dict[str, torch.Tensor]:
+ sd: dict[str, torch.Tensor] = {}
+ for key, (shape, dtype) in mixed_keys.items():
+ if key.endswith((".weight_scale", ".input_scale")):
+ sd[key] = torch.tensor(SCALE_VALUE, dtype=torch.float32)
+ else:
+ sd[key] = torch.ones(shape, dtype=torch.float32).to(_DTYPES[dtype])
+ return sd
+
+
+def _quantized_weights() -> set[str]:
+ return {k[: -len(".weight_scale")] + ".weight" for k in mixed_keys if k.endswith(".weight_scale")}
+
+
+def test_the_fixture_is_really_mixed_and_carries_input_scales() -> None:
+ """Guard the fixture: an all-fp8 or scale-free recapture makes the tests below vacuous."""
+ dtypes = {dtype for _, dtype in mixed_keys.values()}
+ assert "F8_E4M3" in dtypes and "BF16" in dtypes, "fixture is no longer mixed"
+ assert any(k.endswith(".weight_scale") for k in mixed_keys)
+ assert any(k.endswith(".input_scale") for k in mixed_keys)
+ assert any(k.endswith(".scale") for k in mixed_keys), "no learned `.scale` parameter left to protect"
+
+
+def test_learned_scale_parameters_are_not_treated_as_metadata() -> None:
+ learned = [k for k in mixed_keys if k.endswith(".scale")]
+
+ assert learned
+ for key in learned:
+ assert not is_scale_metadata_key(key), f"{key} is a weight, not quantization bookkeeping"
+
+
+def test_dequantization_strips_every_scale_key() -> None:
+ sd = Flux2CheckpointModel._dequantize_fp8_weights(None, _mock_state_dict())
+
+ assert not [k for k in sd if k.endswith((".weight_scale", ".scale_weight", ".input_scale"))]
+
+
+def test_unquantized_tensors_survive_untouched() -> None:
+ before = _mock_state_dict()
+ quantized = _quantized_weights()
+
+ after = Flux2CheckpointModel._dequantize_fp8_weights(None, dict(before))
+
+ for key, value in before.items():
+ if key.endswith((".weight_scale", ".input_scale")) or key in quantized:
+ continue
+ assert key in after, f"{key} was deleted"
+ assert torch.equal(after[key], value), f"{key} was modified"
+
+
+def test_the_scale_is_folded_into_the_quantized_weights() -> None:
+ sd = Flux2CheckpointModel._dequantize_fp8_weights(None, _mock_state_dict())
+
+ quantized = _quantized_weights()
+ assert quantized
+ for key in quantized:
+ # Each mock fp8 weight holds 1.0, so the folded result is exactly the scale.
+ assert torch.allclose(sd[key].float(), torch.full_like(sd[key].float(), SCALE_VALUE))
+ assert sd[key].dtype is torch.bfloat16
+
+
+def test_the_scale_weight_spelling_is_folded_on_this_layout_too() -> None:
+ """The regression guard: same real layout, scales spelled the other way.
+
+ Reading only `.weight_scale` while stripping both spellings left each quantized weight at its
+ raw fp8 codes — measured at a relative error of ~4700 on the real checkpoint, with nothing
+ logged.
+ """
+ renamed = {
+ (k[: -len(".weight_scale")] + ".scale_weight" if k.endswith(".weight_scale") else k): v
+ for k, v in _mock_state_dict().items()
+ }
+
+ sd = Flux2CheckpointModel._dequantize_fp8_weights(None, renamed)
+
+ quantized = _quantized_weights()
+ assert quantized
+ for key in quantized:
+ assert torch.allclose(sd[key].float(), torch.full_like(sd[key].float(), SCALE_VALUE))
+ assert not [k for k in sd if k.endswith((".weight_scale", ".scale_weight"))]
+
+
+def test_a_per_channel_input_scale_is_stripped_by_name_not_by_rank() -> None:
+ """A vector-valued `input_scale` must go too.
+
+ Scalar scales were removed incidentally, by the "0-dimensional tensors are metadata" branch
+ rather than by name. Anything with a shape slipped past it and reached
+ `load_state_dict(..., strict=True)` as an unexpected key.
+ """
+ sd = _mock_state_dict()
+ per_channel = [k for k in sd if k.endswith(".input_scale")]
+ assert per_channel
+ for key in per_channel:
+ sd[key] = torch.full((8,), SCALE_VALUE, dtype=torch.float32)
+
+ out = Flux2CheckpointModel._dequantize_fp8_weights(None, sd)
+
+ assert not [k for k in out if k.endswith(".input_scale")]