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(loaders): close the last three nvfp4 seams against the degraded payload

Alexander Eichhorn8a2576a8ca99
diff --git a/tests/backend/model_manager/load/test_krea2_loader_boundaries.py b/tests/backend/model_manager/load/test_krea2_loader_boundaries.py
index 3a814d9121..88c3b568a2 100644
--- a/tests/backend/model_manager/load/test_krea2_loader_boundaries.py
+++ b/tests/backend/model_manager/load/test_krea2_loader_boundaries.py
@@ -855,3 +855,27 @@ def test_room_is_reserved_before_the_scaled_weights_are_widened(monkeypatch, tmp
assert torch.allclose(model.transformer_blocks[0].attn.to_q.weight.float(), payload.dequantized, atol=1e-5)
assert [step for step, _ in run.order] == list(widening), run.order
assert all(reserved for _step, reserved in run.order), run.order
+
+
+def test_an_nvfp4_layer_missing_its_global_scale_is_refused_before_the_cache_is_evicted(monkeypatch, tmp_path) -> None:
+ """The degraded nvfp4 half-state, at this seam rather than at the detector.
+
+ A packed uint8 weight with a block-scale grid and no `weight_scale_2` is the state a guard keyed
+ on `weight_scale_2` -- the key the decode itself keys on -- lets straight through.
+ `_find_nvfp4_layers` refuses it and `test_nvfp4.py` pins that; what only a seam can answer is
+ whether this loader reaches the detector before it asks the cache for room. Krea-2 pops on the
+ *native* keys, before the conversion that remaps the payload paths, so the refusal has to survive
+ being upstream of the rename as well as upstream of the reservation.
+ """
+ width = _TinyNativeKrea2.WIDTH
+ state_dict = {
+ **_native_block(width),
+ "blocks.0.attn.wq.weight": torch.zeros(width, width // 2, dtype=torch.uint8),
+ "blocks.0.attn.wq.weight_scale": torch.zeros(width, width // 16).to(torch.float8_e4m3fn),
+ }
+ run, config = _native_fp8_driver(monkeypatch, tmp_path, state_dict)
+
+ with pytest.raises(ValueError, match="no weight_scale_2"):
+ run.load(config)
+
+ assert run.reserved == []
diff --git a/tests/backend/model_manager/load/test_qwen3_encoder_checkpoint_loader.py b/tests/backend/model_manager/load/test_qwen3_encoder_checkpoint_loader.py
index cb79e847f6..94c091faf5 100644
--- a/tests/backend/model_manager/load/test_qwen3_encoder_checkpoint_loader.py
+++ b/tests/backend/model_manager/load/test_qwen3_encoder_checkpoint_loader.py
@@ -251,3 +251,34 @@ def test_an_int8_convrot_encoder_stays_int8_resident_and_encodes_like_its_dense_
loader._ram_cache.make_room.assert_called_once()
(reserved,), _ = loader._ram_cache.make_room.call_args
assert reserved == codes_and_scales + dense_bytes
+
+
+def test_an_nvfp4_layer_missing_its_global_scale_is_refused_before_the_cache_is_evicted(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ """The degraded nvfp4 half-state, at this seam rather than at the detector.
+
+ A packed uint8 weight with a block-scale grid and no `weight_scale_2` is the state a guard keyed
+ on `weight_scale_2` -- the key the decode keys on -- lets straight through. `_find_nvfp4_layers`
+ refuses it and `test_nvfp4.py` pins that; what only a seam can answer is whether this loader
+ reaches the detector before it asks the cache for room. Asserted through the cache double this
+ file already uses, in the same idiom as the cells above, because this loader reads its header
+ from the real path rather than from a served state dict.
+ """
+ tensors = {
+ "model.embed_tokens.weight": torch.randn(VOCAB, HIDDEN),
+ "model.layers.0.self_attn.q_proj.weight": torch.zeros(HIDDEN, HIDDEN // 2, dtype=torch.uint8),
+ "model.layers.0.self_attn.q_proj.weight_scale": torch.zeros(HIDDEN, HIDDEN // 16).to(torch.float8_e4m3fn),
+ }
+ checkpoint = tmp_path / "qwen_3_4b_fp4_half.safetensors"
+ save_file(tensors, checkpoint)
+ monkeypatch.setattr(z_image.TorchDevice, "choose_torch_device", lambda: torch.device("cpu"))
+ monkeypatch.setattr(z_image.TorchDevice, "choose_bfloat16_safe_dtype", lambda _device: torch.float32)
+ loader = object.__new__(Qwen3EncoderCheckpointLoader)
+ loader._ram_cache = SimpleNamespace(make_room=MagicMock())
+ config = Qwen3Encoder_Checkpoint_Config.model_construct(path=str(checkpoint), variant=Qwen3VariantType.Qwen3_4B)
+
+ with pytest.raises(ValueError, match="no weight_scale_2"):
+ loader._load_from_singlefile(config)
+
+ loader._ram_cache.make_room.assert_not_called()
diff --git a/tests/backend/model_manager/load/test_qwen_image_nvfp4_loader.py b/tests/backend/model_manager/load/test_qwen_image_nvfp4_loader.py
index 33209b5364..192ced86c6 100644
--- a/tests/backend/model_manager/load/test_qwen_image_nvfp4_loader.py
+++ b/tests/backend/model_manager/load/test_qwen_image_nvfp4_loader.py
@@ -340,3 +340,47 @@ def test_an_nvfp4_layer_missing_its_global_scale_is_refused_before_the_cache_is_
run.load(QwenVLEncoder_Checkpoint_Config.model_construct(path=str(checkpoint)))
assert run.reserved == []
+
+
+def test_the_transformer_refuses_an_nvfp4_layer_missing_its_global_scale_before_the_cache_is_evicted(
+ monkeypatch: pytest.MonkeyPatch, tmp_path
+) -> None:
+ """The same degraded half-state at the transformer seam, which had no `Seam` of its own.
+
+ The encoder cell above covers the other Qwen-Image seam; this one is the transformer, where the
+ pop sits 25 lines above the reservation. Declared through the shared driver rather than the
+ hand-rolled loader the cells above use, because `run.reserved` is the assertion -- the recorded
+ reservations are what say "before", and a `MagicMock` would only say "not called at all".
+ """
+ import diffusers
+
+ state_dict = {
+ "img_in.weight": torch.randn(128, 64),
+ "img_in.bias": torch.randn(128),
+ "transformer_blocks.0.attn.to_q.weight": torch.zeros(128, 32, dtype=torch.uint8),
+ "transformer_blocks.0.attn.to_q.weight_scale": torch.zeros(128, 4).to(torch.float8_e4m3fn),
+ }
+ checkpoint = tmp_path / "qwen_image_nvfp4_half.safetensors"
+ checkpoint.touch()
+ seam = Seam(
+ loader=QwenImageCheckpointModel,
+ module=qwen_image,
+ entry="_load_from_singlefile",
+ load_file_host=safetensors.torch,
+ compute_dtype=COMPUTE_DTYPE,
+ patches_device=True,
+ )
+ run = prepare(
+ seam,
+ monkeypatch,
+ state_dict=state_dict,
+ metadata=None,
+ geometry=lambda patch: patch.setattr(
+ diffusers, "QwenImageTransformer2DModel", _TinyQwenImageTransformer, raising=False
+ ),
+ )
+
+ with pytest.raises(ValueError, match="no weight_scale_2"):
+ run.load(Main_Checkpoint_QwenImage_Config.model_construct(path=str(checkpoint), name="qwen_image_nvfp4_half"))
+
+ assert run.reserved == []