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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
test(fp8): capture a real scaled-fp8 Z-Image key layout
Alexander Eichhorn772e3b0edc77
diff --git a/tests/backend/model_manager/load/state_dicts/z_image_transformer_scaled_fp8_keys.py b/tests/backend/model_manager/load/state_dicts/z_image_transformer_scaled_fp8_keys.pynew file mode 100644index 0000000000..b971d339dc--- /dev/null+++ b/tests/backend/model_manager/load/state_dicts/z_image_transformer_scaled_fp8_keys.py@@ -0,0 +1,94 @@+"""Representative key layout of a *scaled* fp8 Z-Image transformer checkpoint.++Captured from `zImageTurboFP8Kijai_fp8ScaledE4m3fn.safetensors`. Unlike+`z_image_transformer_comfyui_keys`, which is a bf16 checkpoint, this one is ComfyUI "scaled fp8":+each quantized Linear carries an fp8 `<name>.weight` plus a scalar `<name>.scale_weight`, and the+file is marked with a stray `scaled_fp8` key.++Two things make it worth capturing. It spells the scale `.scale_weight` rather than+`.weight_scale` -- the spelling several loaders used to ignore -- and its scales are all *above*+one (1.5 to 7.6 in the real file), so dropping them leaves each weight at a different fraction of+its true magnitude rather than uniformly wrong.++Same subsetting rule as the sibling fixture: block 0 of each stack plus every non-block key.+Values are `(shape, dtype)`.+"""++state_dict_keys: dict[str, tuple[list[int], str]] = {+ "cap_embedder.0.weight": ([2560], "F32"),+ "cap_embedder.1.bias": ([3840], "F32"),+ "cap_embedder.1.scale_weight": ([1], "F32"),+ "cap_embedder.1.weight": ([3840, 2560], "F8_E4M3"),+ "cap_pad_token": ([1, 3840], "F32"),+ "context_refiner.0.attention.k_norm.weight": ([128], "F32"),+ "context_refiner.0.attention.out.scale_weight": ([1], "F32"),+ "context_refiner.0.attention.out.weight": ([3840, 3840], "F8_E4M3"),+ "context_refiner.0.attention.q_norm.weight": ([128], "F32"),+ "context_refiner.0.attention.qkv.scale_weight": ([1], "F32"),+ "context_refiner.0.attention.qkv.weight": ([11520, 3840], "F8_E4M3"),+ "context_refiner.0.attention_norm1.weight": ([3840], "F32"),+ "context_refiner.0.attention_norm2.weight": ([3840], "F32"),+ "context_refiner.0.feed_forward.w1.scale_weight": ([1], "F32"),+ "context_refiner.0.feed_forward.w1.weight": ([10240, 3840], "F8_E4M3"),+ "context_refiner.0.feed_forward.w2.scale_weight": ([1], "F32"),+ "context_refiner.0.feed_forward.w2.weight": ([3840, 10240], "F8_E4M3"),+ "context_refiner.0.feed_forward.w3.scale_weight": ([1], "F32"),+ "context_refiner.0.feed_forward.w3.weight": ([10240, 3840], "F8_E4M3"),+ "context_refiner.0.ffn_norm1.weight": ([3840], "F32"),+ "context_refiner.0.ffn_norm2.weight": ([3840], "F32"),+ "final_layer.adaLN_modulation.1.bias": ([3840], "F32"),+ "final_layer.adaLN_modulation.1.scale_weight": ([1], "F32"),+ "final_layer.adaLN_modulation.1.weight": ([3840, 256], "F8_E4M3"),+ "final_layer.linear.bias": ([64], "F32"),+ "final_layer.linear.scale_weight": ([1], "F32"),+ "final_layer.linear.weight": ([64, 3840], "F8_E4M3"),+ "layers.0.adaLN_modulation.0.bias": ([15360], "F32"),+ "layers.0.adaLN_modulation.0.scale_weight": ([1], "F32"),+ "layers.0.adaLN_modulation.0.weight": ([15360, 256], "F8_E4M3"),+ "layers.0.attention.k_norm.weight": ([128], "F32"),+ "layers.0.attention.out.scale_weight": ([1], "F32"),+ "layers.0.attention.out.weight": ([3840, 3840], "F8_E4M3"),+ "layers.0.attention.q_norm.weight": ([128], "F32"),+ "layers.0.attention.qkv.scale_weight": ([1], "F32"),+ "layers.0.attention.qkv.weight": ([11520, 3840], "F8_E4M3"),+ "layers.0.attention_norm1.weight": ([3840], "F32"),+ "layers.0.attention_norm2.weight": ([3840], "F32"),+ "layers.0.feed_forward.w1.scale_weight": ([1], "F32"),+ "layers.0.feed_forward.w1.weight": ([10240, 3840], "F8_E4M3"),+ "layers.0.feed_forward.w2.scale_weight": ([1], "F32"),+ "layers.0.feed_forward.w2.weight": ([3840, 10240], "F8_E4M3"),+ "layers.0.feed_forward.w3.scale_weight": ([1], "F32"),+ "layers.0.feed_forward.w3.weight": ([10240, 3840], "F8_E4M3"),+ "layers.0.ffn_norm1.weight": ([3840], "F32"),+ "layers.0.ffn_norm2.weight": ([3840], "F32"),+ "noise_refiner.0.adaLN_modulation.0.bias": ([15360], "F32"),+ "noise_refiner.0.adaLN_modulation.0.scale_weight": ([1], "F32"),+ "noise_refiner.0.adaLN_modulation.0.weight": ([15360, 256], "F8_E4M3"),+ "noise_refiner.0.attention.k_norm.weight": ([128], "F32"),+ "noise_refiner.0.attention.out.scale_weight": ([1], "F32"),+ "noise_refiner.0.attention.out.weight": ([3840, 3840], "F8_E4M3"),+ "noise_refiner.0.attention.q_norm.weight": ([128], "F32"),+ "noise_refiner.0.attention.qkv.scale_weight": ([1], "F32"),+ "noise_refiner.0.attention.qkv.weight": ([11520, 3840], "F8_E4M3"),+ "noise_refiner.0.attention_norm1.weight": ([3840], "F32"),+ "noise_refiner.0.attention_norm2.weight": ([3840], "F32"),+ "noise_refiner.0.feed_forward.w1.scale_weight": ([1], "F32"),+ "noise_refiner.0.feed_forward.w1.weight": ([10240, 3840], "F8_E4M3"),+ "noise_refiner.0.feed_forward.w2.scale_weight": ([1], "F32"),+ "noise_refiner.0.feed_forward.w2.weight": ([3840, 10240], "F8_E4M3"),+ "noise_refiner.0.feed_forward.w3.scale_weight": ([1], "F32"),+ "noise_refiner.0.feed_forward.w3.weight": ([10240, 3840], "F8_E4M3"),+ "noise_refiner.0.ffn_norm1.weight": ([3840], "F32"),+ "noise_refiner.0.ffn_norm2.weight": ([3840], "F32"),+ "scaled_fp8": ([2], "F8_E4M3"),+ "t_embedder.mlp.0.bias": ([1024], "F32"),+ "t_embedder.mlp.0.scale_weight": ([1], "F32"),+ "t_embedder.mlp.0.weight": ([1024, 256], "F8_E4M3"),+ "t_embedder.mlp.2.bias": ([256], "F32"),+ "t_embedder.mlp.2.scale_weight": ([1], "F32"),+ "t_embedder.mlp.2.weight": ([256, 1024], "F8_E4M3"),+ "x_embedder.bias": ([3840], "F32"),+ "x_embedder.scale_weight": ([1], "F32"),+ "x_embedder.weight": ([3840, 64], "F8_E4M3"),+ "x_pad_token": ([1, 3840], "F32"),+}diff --git a/tests/backend/model_manager/load/test_z_image_scaled_fp8_keys.py b/tests/backend/model_manager/load/test_z_image_scaled_fp8_keys.pynew file mode 100644index 0000000000..2665255e6d--- /dev/null+++ b/tests/backend/model_manager/load/test_z_image_scaled_fp8_keys.py@@ -0,0 +1,70 @@+"""The scaled-fp8 Z-Image key layout must be recognized, scales and all.++A ComfyUI "scaled fp8" checkpoint stores each quantized Linear as an fp8 `<name>.weight` plus a+`<name>.scale_weight`. Before the scaled-fp8 handling landed, the Z-Image loader deleted those+scale keys in its `keys_to_remove` filter and cast the weight — so every quantized weight was off+by a factor of `weight_scale`, with nothing logged. On the captured checkpoint the scales run from+1.5 to 7.6, which leaves each weight at a *different* fraction of its true magnitude.++The fixture is a real key layout; the tensors are synthetic because only shapes and dtypes matter+to the code under test.+"""++import torch++from invokeai.backend.quantization.fp8_scaled import (+ FP8_DTYPE,+ extract_comfy_quant_hints,+ extract_fp8_scaled_layers,+)+from tests.backend.model_manager.load.state_dicts.z_image_transformer_scaled_fp8_keys import (+ state_dict_keys as scaled_keys,+)++_DTYPES = {"F8_E4M3": FP8_DTYPE, "F32": torch.float32, "BF16": torch.bfloat16}+++def _mock_state_dict(scale_value: float = 4.0) -> dict[str, torch.Tensor]:+ sd: dict[str, torch.Tensor] = {}+ for key, (shape, dtype) in scaled_keys.items():+ if key.endswith(".scale_weight"):+ sd[key] = torch.full(shape, scale_value, dtype=torch.float32)+ else:+ sd[key] = torch.ones(shape, dtype=torch.float32).to(_DTYPES[dtype])+ return sd+++def test_the_fixture_really_is_scaled_fp8() -> None:+ """Guard the fixture itself: a bf16 recapture would make every assertion below vacuous."""+ assert any(k.endswith(".scale_weight") for k in scaled_keys)+ assert not any(k.endswith(".weight_scale") for k in scaled_keys)+ assert any(dtype == "F8_E4M3" for _, dtype in scaled_keys.values())+ assert "scaled_fp8" in scaled_keys+++def test_every_scale_weight_is_recognized_as_a_scaled_layer() -> None:+ sd = _mock_state_dict()+ expected = {k[: -len(".scale_weight")] for k in scaled_keys if k.endswith(".scale_weight")}++ layers = extract_fp8_scaled_layers(sd, layer_hints=extract_comfy_quant_hints(sd))++ assert set(layers) == expected, "a .scale_weight spelling was not recognized"+++def test_extraction_consumes_the_scale_keys() -> None:+ # Anything left behind reaches `load_state_dict(..., strict=True)` as an unexpected key.+ sd = _mock_state_dict()++ extract_fp8_scaled_layers(sd, layer_hints=extract_comfy_quant_hints(sd))++ assert not [k for k in sd if k.endswith((".scale_weight", ".weight_scale"))]+++def test_the_scale_is_carried_through_not_discarded() -> None:+ sd = _mock_state_dict(scale_value=4.0)++ layers = extract_fp8_scaled_layers(sd, layer_hints=extract_comfy_quant_hints(sd))++ assert layers+ for layer in layers.values():+ assert layer.weight_scale.float().reshape(-1)[0].item() == 4.0