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.

89.8 KB
Raw Blame
import contextlib
import json
import logging
import weakref
from unittest import mock

import pytest
import torch

from invokeai.backend.quantization import fp8_scaled
from invokeai.backend.quantization.block_scale_tiles import unblock_scale_grid
from invokeai.backend.quantization.fp8_scaled import (
    FP8_DTYPE,
    QUANT_METADATA_KEY,
    Fp8ScaledLayer,
    attach_fp8_scales,
    cast_state_dict,
    count_fp8_weights,
    decode_mx_block_scales,
    dequantize_fp8_scaled,
    dequantize_weight,
    detach_layer_sidechannel,
    device_supports_fp8_matmul,
    expand_weight_scale,
    extract_comfy_quant_hints,
    extract_fp8_scaled_layers,
    find_torchao_fp8_problem,
    fold_transient_bytes,
    forget_inconclusive_fp8_matmul_probe,
    is_matmul_usable_scale,
    is_scale_metadata_key,
    iter_weight_scale_pairs,
    parse_quantization_metadata,
    predict_cast_state_dict_size,
    reattach_layer_sidechannel,
    reset_fp8_matmul_support_cache,
    scaled_mm_linear,
    set_fp8_matmul_enabled,
    set_full_precision_hints_respected,
    should_keep_fp8_weights,
    split_fp8_scaled_layers,
    strip_layer_path_prefix,
    translate_torchao_fp8_layers,
    warn_on_unattached_scales,
)
from tests.fixtures.quantized_payloads import (
    MX_BLOCK_SIZE,
    mxfp8_marker,
    mxfp8_tensors,
)

cuda_fp8 = pytest.mark.skipif(
    not (torch.cuda.is_available() and device_supports_fp8_matmul(torch.device("cuda"))),
    reason="requires a CUDA device with fp8 tensor cores (SM 8.9+)",
)


def _fp8_weight(out_f: int, in_f: int, per_channel: bool = False):
    w = torch.randn(out_f, in_f, dtype=torch.bfloat16) * 0.02
    if per_channel:
        scale = (w.abs().amax(dim=1) / torch.finfo(FP8_DTYPE).max).float().clamp(min=1e-12)
        q = (w / scale.reshape(-1, 1)).to(FP8_DTYPE)
    else:
        scale = (w.abs().amax() / torch.finfo(FP8_DTYPE).max).float().clamp(min=1e-12)
        q = (w / scale).to(FP8_DTYPE)
    return q, scale


class TestExtract:
    def test_pops_scales_and_keys_layer_by_path(self):
        q, scale = _fp8_weight(32, 16)
        sd = {"blk.0.lin.weight": q, "blk.0.lin.weight_scale": scale, "blk.0.lin.bias": torch.zeros(32)}
        layers = extract_fp8_scaled_layers(sd)
        assert set(layers) == {"blk.0.lin"}
        assert "blk.0.lin.weight_scale" not in sd, "scale keys must be removed so the sd loads cleanly"
        assert sd["blk.0.lin.weight"].dtype == FP8_DTYPE, "weights must stay quantized"

    def test_accepts_scale_weight_suffix(self):
        q, scale = _fp8_weight(32, 16)
        sd = {"lin.weight": q, "lin.scale_weight": scale}
        assert set(extract_fp8_scaled_layers(sd)) == {"lin"}

    def test_input_scale_is_captured(self):
        q, scale = _fp8_weight(32, 16)
        sd = {"lin.weight": q, "lin.weight_scale": scale, "lin.input_scale": torch.tensor(0.5)}
        assert extract_fp8_scaled_layers(sd)["lin"].input_scale == pytest.approx(0.5)

    def test_scale_without_fp8_weight_is_dropped(self):
        """A scale applied to an already-dequantized weight would corrupt it."""
        sd = {"lin.weight": torch.randn(32, 16, dtype=torch.bfloat16), "lin.weight_scale": torch.tensor(2.0)}
        assert extract_fp8_scaled_layers(sd) == {}
        assert "lin.weight_scale" not in sd

    def test_unparsed_scale_beside_fp8_weight_is_refused(self):
        """Loaders now drop unexpected keys at DEBUG, so a scale spelling the extractor does not read
        must fail here instead of letting the weight load unscaled."""
        q, scale = _fp8_weight(32, 16)
        sd = {"lin.weight": q, "lin.weight_scale_inv": scale}
        with pytest.raises(NotImplementedError, match="lin.weight_scale_inv"):
            extract_fp8_scaled_layers(sd)

    def test_scale_like_key_beside_non_fp8_weight_is_not_a_scale(self):
        sd = {"norm.weight": torch.ones(16, dtype=torch.bfloat16), "norm.scale_shift": torch.ones(16)}
        assert extract_fp8_scaled_layers(sd) == {}
        assert "norm.scale_shift" in sd

    def test_strips_stray_marker_keys(self):
        q, scale = _fp8_weight(32, 16)
        sd = {
            "lin.weight": q,
            "lin.weight_scale": scale,
            "scaled_fp8": torch.tensor(0.0),
            "comfy_quant_x": torch.tensor(1),
        }
        extract_fp8_scaled_layers(sd)
        assert set(sd) == {"lin.weight"}

    def test_full_precision_hint_from_metadata(self):
        q, scale = _fp8_weight(32, 16)
        sd = {"a.weight": q, "a.weight_scale": scale, "b.weight": q.clone(), "b.weight_scale": scale.clone()}
        meta = {"_quantization_metadata": '{"layers": {"a": {"full_precision_matrix_mult": true}, "b": {}}}'}
        layers = extract_fp8_scaled_layers(sd, meta)
        assert layers["a"].full_precision_matmul is True
        assert layers["b"].full_precision_matmul is False

    def test_malformed_metadata_is_ignored(self):
        assert parse_quantization_metadata({"_quantization_metadata": "not json"}) == {}
        assert parse_quantization_metadata(None) == {}

    def test_layer_hints_override_metadata(self):
        q, scale = _fp8_weight(32, 16)
        sd = {"renamed.lin.weight": q, "renamed.lin.weight_scale": scale}
        # Metadata uses the pre-rename path, so only the explicitly remapped hints can match.
        meta = {"_quantization_metadata": '{"layers": {"native.lin": {"full_precision_matrix_mult": true}}}'}
        layers = extract_fp8_scaled_layers(
            dict(sd), metadata=meta, layer_hints={"renamed.lin": {"full_precision_matrix_mult": True}}
        )
        assert layers["renamed.lin"].full_precision_matmul is True

        # Without the remap the flag silently matches nothing - the regression this guards against.
        layers = extract_fp8_scaled_layers(dict(sd), metadata=meta)
        assert layers["renamed.lin"].full_precision_matmul is False


def _comfy_quant_blob(full_precision: bool):
    """The per-layer marker some ComfyUI exports write instead of the header entry."""
    payload = f'{{"format": "float8_e4m3fn", "full_precision_matrix_mult": {"true" if full_precision else "false"}}}'
    return torch.tensor(list(payload.encode("utf-8")), dtype=torch.uint8)


class TestRawFp8:
    """Checkpoints that ship fp8 weights with no weight_scale at all."""

    def test_cast_state_dict_preserves_fp8_when_asked(self):
        q, _ = _fp8_weight(32, 16)
        sd = {"lin.weight": q, "lin.bias": torch.zeros(32), "norm.weight": torch.ones(16)}
        kept = cast_state_dict(sd, torch.bfloat16, keep_fp8=True)
        assert kept == 1
        assert sd["lin.weight"].dtype is FP8_DTYPE, "raw fp8 must survive the load"
        assert sd["lin.bias"].dtype is torch.bfloat16
        assert sd["norm.weight"].dtype is torch.bfloat16

    def test_cast_state_dict_dequantizes_when_matmul_unavailable(self):
        """Without the matmul, staying quantized costs a dequantize per forward for no gain."""
        q, _ = _fp8_weight(32, 16)
        sd = {"lin.weight": q}
        assert cast_state_dict(sd, torch.bfloat16, keep_fp8=False) == 0
        assert sd["lin.weight"].dtype is torch.bfloat16

    def test_e5m2_is_never_preserved(self):
        """scaled_mm cannot take e5m2 as the weight operand on Ada, so keeping it buys nothing."""
        sd = {"lin.weight": torch.zeros(32, 16).to(torch.float8_e5m2)}
        assert cast_state_dict(sd, torch.bfloat16, keep_fp8=True) == 0
        assert sd["lin.weight"].dtype is torch.bfloat16

    def test_only_linear_weights_stay_quantized(self):
        """The regression an end-to-end run caught: checkpoints exist that quantize *everything*.

        A Z-Image checkpoint had 243 of its 453 fp8 tensors 1-D — biases, norm weights, a learned
        pad token. Keeping those quantized saves nothing usable and breaks inference: the fp8 value
        flows into the activations and the next Linear receives an fp8 *input*, which dies in
        `x.abs()` with `"abs_cuda" not implemented for 'Float8_e4m3fn'`.
        """
        model = torch.nn.Sequential()
        model.add_module("lin", torch.nn.Linear(16, 32))
        model.add_module("norm", torch.nn.LayerNorm(32))
        q, _ = _fp8_weight(32, 16)
        sd = {
            "lin.weight": q,  # keep: a Linear weight the matmul can use
            "lin.bias": torch.zeros(32).to(FP8_DTYPE),  # dequantize: bias
            "norm.weight": torch.ones(32).to(FP8_DTYPE),  # dequantize: 1-D norm
            "pad_token": torch.zeros(1, 32).to(FP8_DTYPE),  # dequantize: not a module weight
        }
        assert cast_state_dict(sd, torch.bfloat16, keep_fp8=True, model=model) == 1
        assert sd["lin.weight"].dtype is FP8_DTYPE
        for key in ("lin.bias", "norm.weight", "pad_token"):
            assert sd[key].dtype is torch.bfloat16, f"{key} must not stay fp8"

    def test_skip_patterns_dequantize_named_modules(self):
        """Modules a model marks precision-sensitive must be dequantized even if they are Linears.

        Z-Image declares `_skip_layerwise_casting_patterns = ["t_embedder", "cap_embedder"]`, and
        `TimestepEmbedder.forward` casts its activations to `self.mlp[0].weight.dtype` — an fp8
        weight there makes the activations fp8.
        """
        model = torch.nn.Sequential()
        model.add_module("t_embedder", torch.nn.Linear(16, 32))
        model.add_module("blocks", torch.nn.Linear(16, 32))
        q, _ = _fp8_weight(32, 16)
        sd = {"t_embedder.weight": q, "blocks.weight": q.clone()}
        assert cast_state_dict(sd, torch.bfloat16, keep_fp8=True, model=model, skip_patterns=["t_embedder"]) == 1
        assert sd["t_embedder.weight"].dtype is torch.bfloat16
        assert sd["blocks.weight"].dtype is FP8_DTYPE

    def test_count_fp8_weights(self):
        model = torch.nn.Sequential(torch.nn.Linear(16, 32, bias=False), torch.nn.Linear(32, 16, bias=False))
        assert count_fp8_weights(model) == 0
        model[0].weight = torch.nn.Parameter(_fp8_weight(32, 16)[0], requires_grad=False)
        assert count_fp8_weights(model) == 1

    @cuda_fp8
    def test_fp8_weight_without_scale_uses_the_tensor_cores(self):
        """The runtime already supports scale-less fp8 — `weight_scale` is optional in
        `scaled_mm_linear`, and `_can_use_fp8_matmul` only requires the fp8 dtype. This pins that
        down so a future change cannot quietly make a scale mandatory."""
        from invokeai.backend.model_manager.load.model_cache.torch_module_autocast.torch_module_autocast import (
            apply_custom_layers_to_model,
        )

        torch.manual_seed(0)
        net = torch.nn.Sequential(torch.nn.Linear(64, 64, bias=False)).cuda()
        net[0].weight.data = (net[0].weight.data * 0.02).to(FP8_DTYPE)
        apply_custom_layers_to_model(net)
        lin = net[0]
        x = torch.randn(1, 32, 64, device="cuda", dtype=torch.bfloat16)

        set_fp8_matmul_enabled(True)
        try:
            assert getattr(lin, "weight_scale", None) is None
            assert lin._can_use_fp8_matmul(x) is True
            out = net(x)
        finally:
            set_fp8_matmul_enabled(None)

        reference = torch.nn.functional.linear(x, dequantize_weight(lin.weight, None, x.dtype))
        rel = ((out.float() - reference.float()).norm() / reference.float().norm()).item()
        assert torch.isfinite(out).all()
        assert rel < 0.1, f"unit-scaled fp8 matmul drifted too far from bf16: {rel:.4f}"


class TestInputScale:
    def test_calibrated_scale_is_kept(self):
        q, scale = _fp8_weight(32, 16)
        sd = {"lin.weight": q, "lin.weight_scale": scale, "lin.input_scale": torch.tensor(0.017)}
        layer = extract_fp8_scaled_layers(sd)["lin"]
        assert layer.input_scale is not None
        assert pytest.approx(layer.input_scale.item()) == 0.017
        assert "lin.input_scale" not in sd

    def test_scale_input_spelling_is_accepted(self):
        """`.scale_input` is the other spelling in the wild; ignoring it discards the calibration."""
        q, scale = _fp8_weight(32, 16)
        sd = {"lin.weight": q, "lin.scale_weight": scale, "lin.scale_input": torch.tensor(0.017)}
        layer = extract_fp8_scaled_layers(sd)["lin"]
        assert pytest.approx(layer.input_scale.item()) == 0.017
        assert not [k for k in sd if "scale_input" in k], "must be popped, not left for load_state_dict"

    @pytest.mark.parametrize(
        "value", [1.0, 0.0, -0.5, float("nan"), float("inf")], ids=["placeholder", "zero", "negative", "nan", "inf"]
    )
    def test_unusable_scales_fall_back_to_dynamic(self, value: float):
        """A 1.0 input_scale is an uncalibrated placeholder. Using it means *no* activation scaling,
        so everything above the fp8 max saturates -- strictly worse than the per-forward amax it
        would replace. Zero/negative/non-finite cannot be a divisor at all."""
        q, scale = _fp8_weight(32, 16)
        sd = {"lin.weight": q, "lin.weight_scale": scale, "lin.input_scale": torch.tensor(value)}
        assert extract_fp8_scaled_layers(sd)["lin"].input_scale is None

    @cuda_fp8
    def test_placeholder_scale_saturates_activations_above_the_fp8_range(self):
        """End-to-end cost of trusting a 1.0 placeholder.

        fp8_e4m3 is a floating-point format, so a scale factor does not buy relative precision the
        way it would for int8 — for activations inside +/-448 both paths are equivalent. The damage
        appears only above the representable range, where an unscaled cast clamps hard while the
        dynamic amax scale maps the whole tensor into range. Transformer activations do reach that
        regime, which is why a calibrated input_scale exists at all.
        """
        torch.manual_seed(0)
        q, scale = _fp8_weight(64, 64)
        q, scale = q.cuda(), scale.cuda()
        x = (torch.randn(32, 64, dtype=torch.bfloat16, device="cuda") * 2000.0).unsqueeze(0)
        assert x.abs().max() > 448, "test must exercise the saturating regime"
        reference = torch.nn.functional.linear(x, dequantize_weight(q, scale, x.dtype))

        def err(got: torch.Tensor) -> float:
            return ((got.float() - reference.float()).norm() / reference.float().norm()).item()

        dynamic = scaled_mm_linear(x, q, scale, None, input_scale=None)
        unscaled = scaled_mm_linear(x, q, scale, None, input_scale=torch.tensor(1.0, device="cuda"))
        assert err(dynamic) < err(unscaled) / 2, f"dynamic={err(dynamic):.4f} unscaled={err(unscaled):.4f}"


class TestComfyQuantHints:
    def test_reads_per_layer_markers_and_pops_them(self):
        sd = {
            "blk.0.lin.weight": _fp8_weight(32, 16)[0],
            "blk.0.lin.comfy_quant": _comfy_quant_blob(True),
            "blk.1.lin.comfy_quant": _comfy_quant_blob(False),
        }
        hints = extract_comfy_quant_hints(sd)
        assert hints["blk.0.lin"]["full_precision_matrix_mult"] is True
        assert hints["blk.1.lin"]["full_precision_matrix_mult"] is False
        assert not [k for k in sd if "comfy_quant" in k], "markers must not reach load_state_dict"

    def test_full_precision_flag_reaches_the_layer(self):
        """The regression this guards: a checkpoint carrying the flags *only* in per-layer markers
        had them silently ignored, so layers the producer marked unsafe were multiplied in fp8."""
        q, scale = _fp8_weight(32, 16)
        sd = {"blk.0.lin.weight": q, "blk.0.lin.weight_scale": scale, "blk.0.lin.comfy_quant": _comfy_quant_blob(True)}
        hints = extract_comfy_quant_hints(sd)
        layers = extract_fp8_scaled_layers(sd, layer_hints=hints)
        assert layers["blk.0.lin"].full_precision_matmul is True

        # Reading only the header (no hints) is what used to happen - the flag matches nothing.
        sd2 = {"blk.0.lin.weight": q, "blk.0.lin.weight_scale": scale, "blk.0.lin.comfy_quant": _comfy_quant_blob(True)}
        assert extract_fp8_scaled_layers(sd2).get("blk.0.lin").full_precision_matmul is False

    def test_nul_padded_and_malformed_blobs(self):
        padded = torch.cat([_comfy_quant_blob(True), torch.zeros(8, dtype=torch.uint8)])
        sd = {
            "a.comfy_quant": padded,
            "b.comfy_quant": torch.tensor(list(b"not json"), dtype=torch.uint8),
        }
        hints = extract_comfy_quant_hints(sd)
        assert hints["a"]["full_precision_matrix_mult"] is True
        # A malformed marker is a lost hint, never a failed load.
        assert "b" not in hints
        assert not sd


class TestQwen3VLKeyRemap:
    def test_scale_keys_and_hint_paths_land_on_the_same_module(self):
        """attach_fp8_scales resolves hint paths against the *model*, so the state-dict remap and the
        hint remap must agree - otherwise every recovered scale silently matches nothing."""
        from invokeai.backend.model_manager.load.model_loaders.krea2 import _remap_qwen3vl_singlefile_keys
        from invokeai.backend.model_manager.util.qwen3_vl import qwen3vl_target_key as _qwen3vl_target_key

        q, scale = _fp8_weight(32, 16)
        sd = _remap_qwen3vl_singlefile_keys(
            {
                "model.layers.0.mlp.down_proj.weight": q,
                "model.layers.0.mlp.down_proj.weight_scale": scale,
                "model.visual.blocks.0.attn.qkv.weight": torch.zeros(16, 16),
            }
        )
        assert "language_model.layers.0.mlp.down_proj.weight_scale" in sd
        assert "visual.blocks.0.attn.qkv.weight" in sd

        hints = {_qwen3vl_target_key("model.layers.0.mlp.down_proj"): {"full_precision_matrix_mult": True}}
        layers = extract_fp8_scaled_layers(sd, layer_hints=hints)
        assert set(layers) == {"language_model.layers.0.mlp.down_proj"}
        assert layers["language_model.layers.0.mlp.down_proj"].full_precision_matmul is True


class TestFullPrecisionHintToggle:
    def test_marker_is_applied_by_default(self):
        q, scale = _fp8_weight(32, 16)
        module = torch.nn.Linear(16, 32, bias=False)
        module.weight = torch.nn.Parameter(q, requires_grad=False)
        model = torch.nn.Sequential()
        model.add_module("lin", module)
        attach_fp8_scales(model, {"lin": Fp8ScaledLayer(weight_scale=scale, full_precision_matmul=True)})
        assert module._fp8_full_precision_matmul is True

    def test_marker_suppressed_when_hints_are_off(self):
        """Turning the hints off must reach the module flag CustomLinear reads, not just the parse."""
        q, scale = _fp8_weight(32, 16)
        module = torch.nn.Linear(16, 32, bias=False)
        module.weight = torch.nn.Parameter(q, requires_grad=False)
        model = torch.nn.Sequential()
        model.add_module("lin", module)
        set_full_precision_hints_respected(False)
        try:
            attach_fp8_scales(model, {"lin": Fp8ScaledLayer(weight_scale=scale, full_precision_matmul=True)})
        finally:
            set_full_precision_hints_respected(None)
        assert module._fp8_full_precision_matmul is False


class TestKrea2MetadataRemap:
    def test_native_layer_paths_are_remapped_like_the_state_dict(self):
        """The quantization metadata names layers natively; the scales are keyed after renaming."""
        from invokeai.backend.model_manager.load.model_loaders.krea2 import _remap_native_layer_paths

        mapping = _remap_native_layer_paths(
            ["blocks.0.attn.wq", "blocks.0.attn.wo", "blocks.3.mlp.down", "txtfusion.refiner_blocks.1.attn.wk"]
        )
        assert mapping["blocks.0.attn.wq"] == "transformer_blocks.0.attn.to_q"
        assert mapping["blocks.0.attn.wo"] == "transformer_blocks.0.attn.to_out.0"
        assert mapping["blocks.3.mlp.down"] == "transformer_blocks.3.ff.down"
        assert mapping["txtfusion.refiner_blocks.1.attn.wk"] == "text_fusion.refiner_blocks.1.attn.to_k"


class TestSplitScaledLayers:
    """A scaled layer must never reach `cast_state_dict` still holding an unapplied scale."""

    def _model(self):
        model = torch.nn.Sequential()
        model.add_module("time_embed", torch.nn.Sequential())
        model.time_embed.add_module("linear_2", torch.nn.Linear(16, 32))
        model.add_module("attn", torch.nn.Linear(16, 32))
        return model

    def test_skip_pattern_layer_is_dequantized_with_its_scale(self):
        """The Krea-2 regression: `time_embed` matches the model's skip patterns, so the cast would
        turn its fp8 weight into bf16 *codes* — the scale silently dropped, the weight off by
        1/weight_scale, and `attach_fp8_scales` unable to repair it afterwards."""
        model = self._model()
        w = torch.randn(32, 16, dtype=torch.bfloat16) * 0.02
        scale = (w.abs().amax() / torch.finfo(FP8_DTYPE).max).float().clamp(min=1e-12)
        q = (w / scale).to(FP8_DTYPE)
        sd = {"time_embed.linear_2.weight": q, "attn.weight": q.clone()}
        layers = {
            "time_embed.linear_2": Fp8ScaledLayer(weight_scale=scale),
            "attn": Fp8ScaledLayer(weight_scale=scale),
        }

        remaining = split_fp8_scaled_layers(
            sd, layers, torch.bfloat16, model=model, skip_patterns=["time_embed", "norm"]
        )

        assert set(remaining) == {"attn"}, "only the layer that can stay quantized is left to attach"
        assert sd["time_embed.linear_2.weight"].dtype is torch.bfloat16
        rel = ((sd["time_embed.linear_2.weight"].float() - w.float()).norm() / w.float().norm()).item()
        assert rel < 0.05, f"the scale was not applied on the way down: rel-err {rel:.4f}"

        # And the survivor is untouched by the subsequent cast, so its scale still attaches.
        assert cast_state_dict(sd, torch.bfloat16, keep_fp8=True, model=model, skip_patterns=["time_embed"]) == 1
        model.load_state_dict(sd, assign=True, strict=False)
        assert attach_fp8_scales(model, remaining) == 1
        assert torch.equal(model.attn.weight_scale, scale)

    def test_non_linear_scaled_weight_is_dequantized_with_its_scale(self):
        """Same failure without any skip pattern: a scaled tensor that is not an nn.Linear weight
        fails `_is_fp8_matmul_weight`, so the cast would strip its scale."""
        model = self._model()
        q, scale = _fp8_weight(32, 16)
        sd = {"pad_token.weight": q}
        remaining = split_fp8_scaled_layers(
            sd, {"pad_token": Fp8ScaledLayer(weight_scale=scale)}, torch.bfloat16, model=model
        )
        assert remaining == {}
        assert torch.equal(sd["pad_token.weight"], dequantize_weight(q, scale, torch.bfloat16))

    def test_warns_when_a_scale_finds_no_module(self):
        logger = logging.getLogger("fp8-test")
        with mock.patch.object(logger, "warning") as warn:
            warn_on_unattached_scales(logger, "Krea-2", 1, {"a": object(), "b": object()})
        assert warn.call_count == 1
        assert "1 of 2" in warn.call_args[0][0]

    def test_silent_when_every_scale_landed(self):
        logger = logging.getLogger("fp8-test")
        with mock.patch.object(logger, "warning") as warn:
            warn_on_unattached_scales(logger, "Krea-2", 2, {"a": object(), "b": object()})
        assert warn.call_count == 0


class TestPredictedSize:
    def test_matches_what_cast_state_dict_actually_leaves(self):
        """`make_room` reserves against this, so a mismatch is a silent under-reservation."""
        model = torch.nn.Sequential()
        model.add_module("lin", torch.nn.Linear(16, 32))
        model.add_module("t_embedder", torch.nn.Linear(16, 32))
        model.add_module("norm", torch.nn.LayerNorm(32))
        q, _ = _fp8_weight(32, 16)
        sd = {
            "lin.weight": q,
            "lin.bias": torch.zeros(32).to(FP8_DTYPE),
            "t_embedder.weight": q.clone(),
            "norm.weight": torch.ones(32).to(FP8_DTYPE),
            "pad_token": torch.zeros(1, 32).to(FP8_DTYPE),
        }
        kwargs = {"model": model, "skip_patterns": ["t_embedder"]}
        # What the loaders used to reserve: 1 byte/element for *every* fp8 tensor.
        naive = sum(t.nelement() * (t.element_size() if t.dtype is FP8_DTYPE else 2) for t in sd.values())

        predicted = predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=True, **kwargs)
        cast_state_dict(sd, torch.bfloat16, keep_fp8=True, **kwargs)
        actual = sum(t.nelement() * t.element_size() for t in sd.values())
        assert predicted == actual

        # Only `lin.weight` stays 1 byte/element; the rest lands at 2, so the old sum under-counted.
        assert naive < actual

    def test_all_dequantized_when_keep_fp8_is_off(self):
        q, _ = _fp8_weight(32, 16)
        sd = {"lin.weight": q}
        assert predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=False) == q.nelement() * 2


class TestDeviceSupport:
    def test_probe_decides_rather_than_the_capability_number(self):
        """ROCm reports the gfx arch from `get_device_capability`, so RDNA3 (gfx1100 -> (11, 0))
        passes a `>= (8, 9)` test and then raises on every forward. The probe is what decides."""
        reset_fp8_matmul_support_cache()
        with mock.patch("torch.cuda.is_available", return_value=True):
            with mock.patch("torch.cuda.get_device_capability", return_value=(11, 0)):
                with mock.patch(
                    "invokeai.backend.quantization.fp8_scaled._probe_fp8_matmul", return_value=False
                ) as probe:
                    assert device_supports_fp8_matmul(torch.device("cuda", 0)) is False
                    assert probe.called
        reset_fp8_matmul_support_cache()

    def test_probe_result_is_cached_per_device(self):
        reset_fp8_matmul_support_cache()
        with mock.patch("torch.cuda.is_available", return_value=True):
            with mock.patch("torch.cuda.get_device_capability", return_value=(8, 9)):
                with mock.patch(
                    "invokeai.backend.quantization.fp8_scaled._probe_fp8_matmul", return_value=True
                ) as probe:
                    assert device_supports_fp8_matmul(torch.device("cuda", 0)) is True
                    assert device_supports_fp8_matmul(torch.device("cuda", 0)) is True
                    assert probe.call_count == 1
        reset_fp8_matmul_support_cache()

    def test_non_cuda_never_probes(self):
        reset_fp8_matmul_support_cache()
        with mock.patch("invokeai.backend.quantization.fp8_scaled._probe_fp8_matmul") as probe:
            assert device_supports_fp8_matmul(torch.device("cpu")) is False
            assert not probe.called


class TestDequantize:
    @pytest.mark.parametrize("per_channel", [False, True])
    def test_roundtrip_close_to_original(self, per_channel: bool):
        w = torch.randn(64, 32, dtype=torch.bfloat16) * 0.02
        scale_src = w.abs().amax(dim=1) if per_channel else w.abs().amax()
        scale = (scale_src / torch.finfo(FP8_DTYPE).max).float().clamp(min=1e-12)
        q = (w / (scale.reshape(-1, 1) if per_channel else scale)).to(FP8_DTYPE)

        out = dequantize_weight(q, scale, torch.bfloat16)
        assert out.dtype == torch.bfloat16
        assert ((out.float() - w.float()).norm() / w.float().norm()).item() < 0.05

    def test_missing_scale_is_a_plain_cast(self):
        """fp8_storage layerwise casting produces scale-free fp8 weights."""
        q = torch.randn(8, 8, dtype=torch.bfloat16).to(FP8_DTYPE)
        assert torch.equal(dequantize_weight(q, None, torch.bfloat16), q.to(torch.bfloat16))

    def test_state_dict_dequantization_matches_helper(self):
        q, scale = _fp8_weight(64, 32)
        sd = {"lin.weight": q}
        layers = {"lin": Fp8ScaledLayer(weight_scale=scale)}
        dequantize_fp8_scaled(sd, layers)
        assert torch.equal(sd["lin.weight"], dequantize_weight(q, scale, torch.bfloat16))

    @pytest.mark.parametrize("grid_dtype", [torch.uint8, getattr(torch, "float8_e8m0fnu", torch.uint8)])
    def test_an_undecoded_mx_grid_is_refused_not_multiplied(self, grid_dtype: torch.dtype):
        """`reject_undecoded_mx_scale` keys on the scale's dtype, and this is the one caller that
        used to cast the scale to the compute dtype first — which erases the evidence. An exponent
        byte multiplied in as a linear factor is ~127x, paired with the wrong rows besides: the
        finite-but-wrong noise the whole MX decode exists to prevent."""
        q = torch.randn(256, 8, dtype=torch.bfloat16).to(FP8_DTYPE)
        grid = torch.full((256, 8), 127, dtype=grid_dtype)

        with pytest.raises(NotImplementedError, match="MXFP8"):
            dequantize_weight(q, grid, torch.bfloat16)


class TestAttach:
    def test_registers_non_persistent_buffers(self):
        lin = torch.nn.Linear(16, 32, bias=False)
        q, scale = _fp8_weight(32, 16)
        lin.weight = torch.nn.Parameter(q, requires_grad=False)
        model = torch.nn.Sequential(lin)

        assert attach_fp8_scales(model, {"0": Fp8ScaledLayer(weight_scale=scale, full_precision_matmul=True)}) == 1
        assert torch.equal(model[0].weight_scale, scale)
        assert model[0]._fp8_full_precision_matmul is True
        # Re-saving a model whose scales landed in state_dict() would double-scale on reload.
        assert "0.weight_scale" not in model.state_dict()

    def test_skips_non_fp8_modules(self):
        model = torch.nn.Sequential(torch.nn.Linear(16, 32))
        assert attach_fp8_scales(model, {"0": Fp8ScaledLayer(weight_scale=torch.tensor(1.0))}) == 0


@cuda_fp8
class TestScaledMm:
    @pytest.mark.parametrize("per_channel", [False, True])
    @pytest.mark.parametrize("tokens", [64, 100])  # 100 is deliberately not a multiple of 16
    def test_matches_reference_linear(self, per_channel: bool, tokens: int):
        dev = torch.device("cuda")
        q, scale = _fp8_weight(256, 128, per_channel)
        q, scale = q.to(dev), scale.to(dev)
        w_ref = dequantize_weight(q, scale, torch.bfloat16)
        x = torch.randn(tokens, 128, device=dev, dtype=torch.bfloat16)

        got = scaled_mm_linear(x, q, scale)
        expected = torch.nn.functional.linear(x, w_ref)

        assert got.shape == expected.shape
        rel = ((got.float() - expected.float()).norm() / expected.float().norm()).item()
        assert rel < 0.08, f"relative error {rel:.4f} too high"

    def test_preserves_leading_dims_and_bias(self):
        dev = torch.device("cuda")
        q, scale = _fp8_weight(64, 32)
        q, scale = q.to(dev), scale.to(dev)
        bias = torch.randn(64, device=dev, dtype=torch.bfloat16)
        x = torch.randn(2, 48, 32, device=dev, dtype=torch.bfloat16)

        got = scaled_mm_linear(x, q, scale, bias)
        expected = torch.nn.functional.linear(x, dequantize_weight(q, scale, torch.bfloat16), bias)
        assert got.shape == (2, 48, 64)
        assert ((got.float() - expected.float()).norm() / expected.float().norm()).item() < 0.08

    def test_static_input_scale_path(self):
        dev = torch.device("cuda")
        q, scale = _fp8_weight(64, 32)
        q, scale = q.to(dev), scale.to(dev)
        x = torch.randn(32, 32, device=dev, dtype=torch.bfloat16)
        static = (x.abs().amax() / torch.finfo(FP8_DTYPE).max).float()

        got = scaled_mm_linear(x, q, scale, input_scale=static)
        expected = torch.nn.functional.linear(x, dequantize_weight(q, scale, torch.bfloat16))
        assert ((got.float() - expected.float()).norm() / expected.float().norm()).item() < 0.08


class TestCustomLinearIntegration:
    """The fp8 matmul must be opt-in and must degrade to the dequantized path, never raise."""

    @pytest.fixture(autouse=True)
    def _restore_matmul_override(self):
        """Clear the override after each test, rather than leaving it pinned to ``False``.

        These tests reset with ``set_fp8_matmul_enabled(False)``, which is not the neutral state --
        it is an explicit "off" that outlives the class and silently overrides the config for
        anything that runs later in the same process.
        """
        yield
        set_fp8_matmul_enabled(None)

    def _module(self, device: torch.device, in_f=64, out_f=128, device_autocasting: bool = False):
        from invokeai.backend.model_manager.load.model_cache.torch_module_autocast.torch_module_autocast import (
            apply_custom_layers_to_model,
        )

        lin = torch.nn.Linear(in_f, out_f, bias=False)
        q, scale = _fp8_weight(out_f, in_f)
        lin.weight = torch.nn.Parameter(q.to(device), requires_grad=False)
        lin.register_buffer("weight_scale", scale.to(device), persistent=False)
        model = torch.nn.Sequential(lin).to(device)
        apply_custom_layers_to_model(model, device_autocasting_enabled=device_autocasting)
        return model

    @cuda_fp8
    @pytest.mark.parametrize("device_autocasting", [False, True])
    def test_fp8_path_runs_regardless_of_device_autocasting(self, device_autocasting: bool):
        """`apply_custom_layers_to_model` leaves autocasting off for fully-resident models.

        A fp8 check that only lives in `_autocast_forward` is therefore skipped in the common case:
        `forward` falls through to the dtype-mismatch branch and silently dequantizes instead. This
        regression cost the entire speedup while every other test stayed green.
        """
        dev = torch.device("cuda")
        model = self._module(dev, device_autocasting=device_autocasting)
        x = torch.randn(32, 64, device=dev, dtype=torch.bfloat16)
        custom = model[0]

        calls = []
        original = custom._maybe_fp8_forward
        custom._maybe_fp8_forward = lambda inp: (calls.append(1), original(inp))[1]

        set_fp8_matmul_enabled(True)
        try:
            out = model(x)
        finally:
            set_fp8_matmul_enabled(False)
            custom._maybe_fp8_forward = original

        assert calls, "the fp8 branch was never consulted"
        assert out.shape == (32, 128)
        # And it must actually have taken the fp8 path, not just been asked.
        w_ref = dequantize_weight(custom.weight, custom.weight_scale, torch.bfloat16)
        assert not torch.equal(out, torch.nn.functional.linear(x, w_ref)), (
            "output is bit-identical to the dequantized path, so _scaled_mm did not run"
        )

    def test_disabled_by_default_uses_scaled_dequant(self):
        """With the matmul off, the weight must still be dequantized *with* its scale."""
        set_fp8_matmul_enabled(False)
        dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        # CPU inference is float32 in the product (TorchDevice.choose_torch_dtype). A bf16 input
        # here would also hit an illegal-instruction fault (0xC000001D) in the Windows CPU torch
        # build's bf16 GEMM on some GitHub runner CPUs, taking the whole xdist worker down.
        dtype = torch.bfloat16 if dev.type == "cuda" else torch.float32
        model = self._module(dev)
        x = torch.randn(32, 64, device=dev, dtype=dtype)

        got = model(x)
        w_ref = dequantize_weight(model[0].weight, model[0].weight_scale, dtype)
        expected = torch.nn.functional.linear(x, w_ref)
        assert torch.equal(got, expected)

    @cuda_fp8
    def test_full_precision_flag_forces_fallback(self):
        set_fp8_matmul_enabled(True)
        try:
            dev = torch.device("cuda")
            model = self._module(dev)
            model[0]._fp8_full_precision_matmul = True
            x = torch.randn(32, 64, device=dev, dtype=torch.bfloat16)

            got = model(x)
            w_ref = dequantize_weight(model[0].weight, model[0].weight_scale, torch.bfloat16)
            assert torch.equal(got, torch.nn.functional.linear(x, w_ref))
        finally:
            set_fp8_matmul_enabled(False)

    @cuda_fp8
    def test_sidecar_patched_fp8_layer_uses_the_fp8_matmul(self):
        """LoRAs on fp8 weights are force-routed to sidecar patching (layer_patcher), and the
        sidecar wrapper calls `_autocast_forward`. The fp8 branch must survive that route, and the
        LoRA residual must still be added on top."""
        from invokeai.backend.patches.layers.lora_layer import LoRALayer

        dev = torch.device("cuda")
        model = self._module(dev)
        custom = model[0]
        rank = 8
        lora = LoRALayer(
            up=torch.randn(custom.out_features, rank, device=dev, dtype=torch.bfloat16) * 0.05,
            mid=None,
            down=torch.randn(rank, custom.in_features, device=dev, dtype=torch.bfloat16) * 0.05,
            alpha=float(rank),
            bias=None,
        )
        custom.add_patch(lora, 1.0)
        x = torch.randn(32, 64, device=dev, dtype=torch.bfloat16)

        set_fp8_matmul_enabled(True)
        try:
            patched = model(x)
        finally:
            set_fp8_matmul_enabled(False)
        unpatched_ref = torch.nn.functional.linear(
            x, dequantize_weight(custom.weight, custom.weight_scale, torch.bfloat16)
        )

        assert patched.shape == (32, 128)
        # The patch must actually change the output...
        assert not torch.allclose(patched, unpatched_ref, atol=1e-2)
        # ...and the base contribution must still be roughly the fp8 linear, not garbage.
        residual = (lora.get_weight(1.0) * lora.scale()).to(torch.bfloat16)
        expected = unpatched_ref + torch.nn.functional.linear(x, residual)
        rel = ((patched.float() - expected.float()).norm() / expected.float().norm()).item()
        assert rel < 0.1, f"sidecar-patched fp8 output diverges by {rel:.4f}"

    @cuda_fp8
    def test_unaligned_features_fall_back(self):
        set_fp8_matmul_enabled(True)
        try:
            dev = torch.device("cuda")
            model = self._module(dev, in_f=60, out_f=120)  # not multiples of 16
            x = torch.randn(16, 60, device=dev, dtype=torch.bfloat16)
            assert model(x).shape == (16, 120)  # must not raise
        finally:
            set_fp8_matmul_enabled(False)

    @cuda_fp8
    def test_enabled_path_agrees_with_fallback(self):
        dev = torch.device("cuda")
        model = self._module(dev)
        x = torch.randn(48, 64, device=dev, dtype=torch.bfloat16)

        set_fp8_matmul_enabled(False)
        baseline = model(x)
        set_fp8_matmul_enabled(True)
        try:
            fp8 = model(x)
        finally:
            set_fp8_matmul_enabled(False)

        rel = ((fp8.float() - baseline.float()).norm() / baseline.float().norm()).item()
        assert rel < 0.1, f"fp8 matmul diverges from the dequantized path by {rel:.4f}"


class TestScaleSpellingHelpers:
    """Both spellings of the weight scale must be handled everywhere.

    Reading only `.weight_scale` is the mistake that keeps recurring in the per-loader dequant
    helpers. Depending on what the loader strips afterwards it either deletes a `.scale_weight`
    without applying it — leaving the weight off by `1/weight_scale`, silently — or leaves the key
    behind for `load_state_dict(..., strict=True)` to reject.
    """

    @pytest.mark.parametrize("spelling", [".weight_scale", ".scale_weight"])
    def test_pairs_are_found_in_either_spelling(self, spelling: str) -> None:
        sd = {"blk.weight": torch.ones(2, 2), f"blk{spelling}": torch.tensor(0.5)}

        assert list(iter_weight_scale_pairs(sd)) == [("blk.weight", f"blk{spelling}")]

    def test_a_scale_without_its_weight_is_not_paired(self) -> None:
        # Pairing it would invent a weight key the checkpoint never had.
        sd = {"other.weight": torch.ones(1), "blk.weight_scale": torch.tensor(0.5)}

        assert list(iter_weight_scale_pairs(sd)) == []

    def test_non_string_keys_are_ignored(self) -> None:
        # `.pt`/`.ckpt` sources can carry int keys; `endswith` would raise on them.
        assert list(iter_weight_scale_pairs({0: torch.ones(1)})) == []

    @pytest.mark.parametrize(
        "key",
        [".weight_scale", ".scale_weight", ".input_scale", ".scale_input"],
    )
    def test_metadata_keys_are_recognized_in_either_spelling(self, key: str) -> None:
        assert is_scale_metadata_key(f"blk{key}")

    @pytest.mark.parametrize("key", ["blk.comfy_quant", "scaled_fp8"])
    def test_marker_keys_are_recognized(self, key: str) -> None:
        assert is_scale_metadata_key(key)

    @pytest.mark.parametrize("key", ["blk.weight", "blk.bias", "norm.scale", 0])
    def test_model_tensors_are_not_mistaken_for_metadata(self, key: object) -> None:
        # `norm.scale` is a real learned parameter in several architectures - stripping it would
        # delete weights, and it is why this cannot just match "scale" anywhere in the key.
        assert not is_scale_metadata_key(key)


def _fp8(rows: int = 4, cols: int = 4, value: float = 1.0) -> torch.Tensor:
    return (torch.ones(rows, cols) * value).to(FP8_DTYPE)


class _Linear(torch.nn.Module):
    def __init__(self, out_features: int = 4, in_features: int = 4) -> None:
        super().__init__()
        self.lin = torch.nn.Linear(in_features, out_features, bias=False)


class TestE5m2ScaleRecovery:
    """`float8_e5m2` may not stay quantized, but it must not lose its scale on the way to bf16."""

    def test_the_scale_is_recovered_and_folded(self) -> None:
        # Gating extraction on e4m3fn alone popped the scale key and then dropped it, so
        # `cast_state_dict` did a plain `.to(bf16)` and the weight came out off by 1/weight_scale.
        sd = {"lin.weight": (torch.ones(4, 4) * 2).to(torch.float8_e5m2), "lin.weight_scale": torch.tensor(0.25)}

        layers = extract_fp8_scaled_layers(sd)
        assert "lin" in layers

        dequantize_fp8_scaled(sd, layers, torch.bfloat16)
        assert torch.allclose(sd["lin.weight"].float(), torch.full((4, 4), 0.5))

    def test_it_is_never_left_quantized(self) -> None:
        """`scaled_mm_linear` cannot take e5m2 as the weight operand, so it must be widened."""
        sd = {"lin.weight": (torch.ones(4, 4) * 2).to(torch.float8_e5m2), "lin.weight_scale": torch.tensor(0.25)}

        kept = split_fp8_scaled_layers(sd, extract_fp8_scaled_layers(sd), torch.bfloat16, model=_Linear())

        assert kept == {}
        assert sd["lin.weight"].dtype is torch.bfloat16
        assert torch.allclose(sd["lin.weight"].float(), torch.full((4, 4), 0.5))


class TestFoldInSlabs:
    """A tensor larger than a slab is folded a slab of rows at a time, and must come out as the whole fold would.

    The expectations are the definition -- float32 product, one rounding to the target dtype -- with each weight
    element's scale picked out by index rather than by the expansion the fold uses. The slab is shrunk so a 39-row
    weight splits into slabs of two rows and an odd one at the end: the boundaries a row-indexing mistake would
    show at, and, against three-row scale blocks, slabs that start inside a block.
    """

    @staticmethod
    def _codes(shape: tuple[int, ...], contiguous: bool) -> torch.Tensor:
        if contiguous:
            return torch.randn(shape).to(FP8_DTYPE)
        if len(shape) == 1:
            return torch.randn(2 * shape[0]).to(FP8_DTYPE)[::2]
        # A transposed buffer, so each slab is a strided row slice.
        return torch.randn(shape[::-1]).to(FP8_DTYPE).permute(*reversed(range(len(shape))))

    @staticmethod
    def _per_element(scale: torch.Tensor, shape: torch.Size) -> torch.Tensor:
        """Each weight element's scale: along every axis the scale has, element ``i`` reads block ``i // size``."""
        for dim in range(scale.dim()):
            scale = scale.index_select(dim, torch.arange(shape[dim]) // (shape[dim] // scale.shape[dim]))
        return scale.reshape(*scale.shape, *[1] * (len(shape) - scale.dim()))

    @pytest.mark.parametrize(
        ("shape", "scale_shape"),
        [
            pytest.param((39, 48), (), id="per_tensor"),
            pytest.param((39, 48), (39,), id="per_row"),
            pytest.param((39, 48), (39, 3), id="column_blocks"),
            pytest.param((39, 48), (13, 3), id="row_and_column_blocks"),
            pytest.param((39, 48), (1, 3), id="one_scale_row"),
            pytest.param((300,), (300,), id="1d"),
            pytest.param((39, 4, 2, 3), (39,), id="4d_per_row"),
            pytest.param((39, 4, 2, 3), (13, 2, 2, 3), id="4d_blocks"),
        ],
    )
    @pytest.mark.parametrize("contiguous", [True, False], ids=["contiguous", "strided"])
    def test_slabs_fold_exactly_as_the_whole(
        self, monkeypatch: pytest.MonkeyPatch, shape: tuple[int, ...], scale_shape: tuple[int, ...], contiguous: bool
    ) -> None:
        monkeypatch.setattr(fp8_scaled, "_FOLD_SLAB_ELEMENTS", 100)
        torch.manual_seed(0)
        codes = self._codes(shape, contiguous)
        scale = torch.rand(scale_shape) + 0.5
        sd = {"lin.weight": codes}

        dequantize_fp8_scaled(sd, {"lin": Fp8ScaledLayer(weight_scale=scale)}, torch.bfloat16)

        expected = (codes.float() * self._per_element(scale, codes.shape)).to(torch.bfloat16)
        assert torch.equal(sd["lin.weight"], expected)

    def test_an_mxfp8_layer_folds_exactly_as_the_whole(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """Through the real decode: slabs of seven rows run across the 32-row tiles the grid is stored in."""
        monkeypatch.setattr(fp8_scaled, "_FOLD_SLAB_ELEMENTS", 7 * 128)
        torch.manual_seed(0)
        sd, decoded = mxfp8_tensors("lin", torch.randint(124, 131, (128, 4)))
        sd["lin.weight"] = codes = torch.randn(128, 4 * MX_BLOCK_SIZE).to(FP8_DTYPE)

        dequantize_fp8_scaled(sd, extract_fp8_scaled_layers(sd, layer_hints={"lin": mxfp8_marker()}), torch.bfloat16)

        expected = (codes.float() * self._per_element(decoded, codes.shape)).to(torch.bfloat16)
        assert torch.equal(sd["lin.weight"], expected)

    @pytest.mark.parametrize(
        "scale",
        [
            pytest.param(torch.tensor(0.37), id="per_tensor"),
            pytest.param(torch.rand(256) + 0.5, id="per_row"),
            pytest.param(torch.rand(16, 4) + 0.5, id="row_and_column_blocks"),
        ],
    )
    def test_nothing_the_size_of_the_weight_is_held_in_float32(
        self, monkeypatch: pytest.MonkeyPatch, scale: torch.Tensor
    ) -> None:
        """The point of the slabs: the largest allocation is the result itself.

        Folded whole, the weight's float32 copy and product are each twice the size of the bf16 result; so is a
        block-wise scale expanded whole before the slabs start, which bounded nothing for those layouts. Measured
        with the profiler's per-op allocation record, which sees every one of them.
        """
        from torch.profiler import ProfilerActivity, profile

        monkeypatch.setattr(fp8_scaled, "_FOLD_SLAB_ELEMENTS", 8 * 512)
        sd = {"lin.weight": torch.randn(256, 512).to(FP8_DTYPE)}
        result_bytes = 256 * 512 * torch.bfloat16.itemsize

        with profile(activities=[ProfilerActivity.CPU], profile_memory=True) as profiler:
            dequantize_fp8_scaled(sd, {"lin": Fp8ScaledLayer(weight_scale=scale)}, torch.bfloat16)

        assert max(event.cpu_memory_usage for event in profiler.events()) == result_bytes

    @pytest.mark.parametrize(
        "scale",
        [
            pytest.param(torch.tensor(0.37), id="per_tensor"),
            pytest.param(torch.rand(256) + 0.5, id="per_row"),
            pytest.param(torch.rand(16, 4) + 0.5, id="row_and_column_blocks"),
            pytest.param(torch.rand(256, 16) + 0.5, id="column_blocks"),
        ],
    )
    @pytest.mark.parametrize("slab_elements", [None, 8 * 512], ids=["whole", "slabs"])
    def test_the_transient_a_loader_reserves_for_covers_what_the_fold_holds(
        self, monkeypatch: pytest.MonkeyPatch, scale: torch.Tensor, slab_elements: int | None
    ) -> None:
        """`fold_transient_bytes` is what a loader adds to its reservation for the layer in flight, so it must not fall
        short of the live bytes the fold allocates beyond its result. Counted by storage, from allocation until the
        last tensor on it dies. In slabs it is exact; folded whole it is over by the result, which does not yet exist
        while the multiply holds its two float32 copies."""
        from torch.utils._python_dispatch import TorchDispatchMode
        from torch.utils._pytree import tree_leaves

        class LiveBytes(TorchDispatchMode):
            def __init__(self) -> None:
                super().__init__()
                self.live = 0
                self.peak = 0

            def _free(self, size: int) -> None:
                self.live -= size

            def __torch_dispatch__(self, func, types, args=(), kwargs=None):
                kwargs = kwargs or {}
                inputs = {t.untyped_storage().data_ptr() for t in tree_leaves((args, kwargs)) if torch.is_tensor(t)}
                out = func(*args, **kwargs)
                for tensor in tree_leaves(out):
                    storage = tensor.untyped_storage() if torch.is_tensor(tensor) else None
                    if storage is None or storage.nbytes() == 0 or storage.data_ptr() in inputs:
                        continue
                    inputs.add(storage.data_ptr())
                    self.live += storage.nbytes()
                    self.peak = max(self.peak, self.live)
                    weakref.finalize(tensor, self._free, storage.nbytes())
                return out

        if slab_elements is not None:
            monkeypatch.setattr(fp8_scaled, "_FOLD_SLAB_ELEMENTS", slab_elements)
        sd = {"lin.weight": torch.randn(256, 512).to(FP8_DTYPE)}
        bound = fold_transient_bytes(torch.empty(256, 512, dtype=FP8_DTYPE, device="meta"), scale)

        with LiveBytes() as live:
            dequantize_fp8_scaled(sd, {"lin": Fp8ScaledLayer(weight_scale=scale)}, torch.bfloat16)

        beyond_result = live.peak - 256 * 512 * torch.bfloat16.itemsize
        assert beyond_result > 0, "the tracker saw no allocation, so the comparison would pass vacuously"
        if slab_elements is None:
            assert beyond_result <= bound
        else:
            assert beyond_result == bound

    def test_a_block_scale_that_does_not_tile_the_rows_names_the_layer(self) -> None:
        # Left to the multiply: "The size of tensor a (39) must match the size of tensor b (36)", naming nothing.
        sd = {"blocks.0.lin.weight": torch.randn(39, 48).to(FP8_DTYPE)}

        with pytest.raises(ValueError, match=r"blocks\.0\.lin: a \(12, 3\) block-wise weight_scale"):
            dequantize_fp8_scaled(sd, {"blocks.0.lin": Fp8ScaledLayer(weight_scale=torch.ones(12, 3))})


class TestBlockWiseScale:
    """A 2-D ``weight_scale`` has one entry per *block* of weight elements, not per row."""

    def test_the_layout_survives_extraction(self) -> None:
        # Flattening it destroys the block geometry, and the multiply then fails on shape.
        sd = {"b.weight": _fp8(64, 128), "b.weight_scale": torch.full((64, 2), 0.5)}

        layers = extract_fp8_scaled_layers(sd)

        assert tuple(layers["b"].weight_scale.shape) == (64, 2)

    def test_it_is_expanded_rather_than_raising(self) -> None:
        # Before: `RuntimeError: The size of tensor a (64) must match the size of tensor b (128)`,
        # i.e. with fp8_compute off - the default - the model failed to load at all.
        sd = {"b.weight": _fp8(64, 128), "b.weight_scale": torch.full((64, 2), 0.5)}

        dequantize_fp8_scaled(sd, extract_fp8_scaled_layers(sd), torch.bfloat16)

        assert sd["b.weight"].shape == (64, 128)
        assert torch.allclose(sd["b.weight"].float(), torch.full((64, 128), 0.5))

    def test_it_is_never_left_quantized(self) -> None:
        """`scaled_mm_linear` can apply a per-tensor or per-row scale and nothing else.

        Left quantized, the mismatch surfaces mid-generation inside the kernel instead.
        Dequantizing here - before `predict_cast_state_dict_size` runs - also keeps the RAM
        reservation honest.
        """
        sd = {"lin.weight": _fp8(4, 4), "lin.weight_scale": torch.full((4, 2), 0.5)}

        kept = split_fp8_scaled_layers(sd, extract_fp8_scaled_layers(sd), torch.bfloat16, model=_Linear())

        assert kept == {}
        assert sd["lin.weight"].dtype is torch.bfloat16

    def test_a_per_row_scale_still_stays_quantized(self) -> None:
        sd = {"lin.weight": _fp8(4, 4), "lin.weight_scale": torch.full((4,), 0.5)}

        kept = split_fp8_scaled_layers(sd, extract_fp8_scaled_layers(sd), torch.bfloat16, model=_Linear())

        assert set(kept) == {"lin"}
        assert sd["lin.weight"].dtype is FP8_DTYPE


class TestSidechannelDetachReattach:
    """Key converters rename ``.weight``; the side channel has to be carried across separately."""

    def test_entries_follow_their_module_to_the_new_path(self) -> None:
        sd = {
            "old.linear.weight": _fp8(),
            "old.linear.weight_scale": torch.tensor(0.5),
            "old.linear.input_scale": torch.tensor(0.25),
        }

        detached = detach_layer_sidechannel(sd)
        assert list(sd) == ["old.linear.weight"], "scales must be out of the converter's way"

        converted = {"new.linear.weight": sd["old.linear.weight"]}
        orphaned = reattach_layer_sidechannel(converted, detached, {"old.linear": "new.linear"})

        assert orphaned == []
        layers = extract_fp8_scaled_layers(converted)
        assert set(layers) == {"new.linear"}
        assert layers["new.linear"].input_scale is not None

    def test_a_module_the_converter_drops_is_reported_not_swallowed(self) -> None:
        """A silently dropped scale is exactly the failure this pair exists to prevent."""
        sd = {"gone.weight": _fp8(), "gone.weight_scale": torch.tensor(0.5)}

        detached = detach_layer_sidechannel(sd)
        orphaned = reattach_layer_sidechannel({}, detached, {})

        assert orphaned == ["gone"]

    def test_both_scale_spellings_are_detached(self) -> None:
        sd = {
            "a.weight": _fp8(),
            "a.scale_weight": torch.tensor(0.5),
            "b.weight": _fp8(),
            "b.weight_scale": torch.tensor(0.5),
        }

        detached = detach_layer_sidechannel(sd)

        assert set(detached) == {"a", "b"}


class TestStripLayerPathPrefix:
    """`_quantization_metadata` is read from the file header, so its names keep the prefix."""

    def test_the_checkpoint_prefix_is_removed(self) -> None:
        hints = {"model.diffusion_model.blocks.0.attn.wq": {"full_precision_matrix_mult": True}}

        assert strip_layer_path_prefix(hints) == {"blocks.0.attn.wq": {"full_precision_matrix_mult": True}}

    def test_unprefixed_names_are_passed_through_not_dropped(self) -> None:
        """Running the names through a prefix *filter* truncated a partially-prefixed header, and
        the strict-zip that read the result back aborted the whole load with a ValueError."""
        hints = {"net.blocks.0.attn.q_proj": {}, "final_layer.linear": {}}

        assert set(strip_layer_path_prefix(hints)) == {"blocks.0.attn.q_proj", "final_layer.linear"}


class TestNonFloatTensorsAreNotCast:
    """Integer payloads are not weights; casting them to the compute dtype corrupts them."""

    def test_cast_state_dict_leaves_them_alone(self) -> None:
        sd = {"w.weight": torch.ones(2, 2), "ids": torch.arange(4, dtype=torch.int64)}

        cast_state_dict(sd, torch.bfloat16, keep_fp8=False)

        assert sd["w.weight"].dtype is torch.bfloat16
        assert sd["ids"].dtype is torch.int64

    def test_the_size_prediction_agrees(self) -> None:
        sd = {"ids": torch.arange(4, dtype=torch.int64)}

        assert predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=False) == 4 * 8


@contextlib.contextmanager
def _probe_on_cpu():
    """Let `_probe_fp8_matmul` run its allocations on CPU so the caching policy can be tested.

    The probe allocates real tensors on the device *before* it reaches `torch._scaled_mm`. On a
    CPU-only runner that allocation raises first, the mocked matmul never runs, and the test
    measures the wrong thing — it passed on a CUDA box and failed on CI for exactly this reason.
    float8 tensors are allocatable on CPU, so dropping the device argument is enough.
    """
    real_zeros, real_ones = torch.zeros, torch.ones

    def on_cpu(real):
        def alloc(*args, **kwargs):
            kwargs.pop("device", None)
            return real(*args, **kwargs)

        return alloc

    with (
        mock.patch("torch.cuda.is_available", return_value=True),
        mock.patch("torch.cuda.get_device_capability", return_value=(8, 9)),
        mock.patch("torch.cuda.current_device", return_value=0),
        mock.patch("torch.zeros", on_cpu(real_zeros)),
        mock.patch("torch.ones", on_cpu(real_ones)),
    ):
        yield


class TestProbeFailureCaching:
    """The probe runs during a model load, i.e. under real VRAM pressure."""

    def test_an_allocation_failure_is_not_cached(self) -> None:
        reset_fp8_matmul_support_cache()
        device = torch.device("cuda", 0)
        with _probe_on_cpu():
            with mock.patch("torch._scaled_mm", side_effect=torch.OutOfMemoryError("transient")):
                assert device_supports_fp8_matmul(device) is False
            # A momentary OOM must not disable fp8 for the rest of the process. The next *load* is
            # what gives it another go; the forwards in between are answered from the record.
            forget_inconclusive_fp8_matmul_probe(device)
            with mock.patch("torch._scaled_mm", return_value=torch.zeros(1)):
                assert device_supports_fp8_matmul(device) is True
        reset_fp8_matmul_support_cache()

    def test_a_genuine_unsupported_op_is_cached(self) -> None:
        reset_fp8_matmul_support_cache()
        device = torch.device("cuda", 0)
        with _probe_on_cpu():
            with mock.patch("torch._scaled_mm", side_effect=RuntimeError("not supported on this device")):
                assert device_supports_fp8_matmul(device) is False
            # No second probe: the answer cannot change at runtime.
            with mock.patch("torch._scaled_mm", side_effect=AssertionError("must not be probed again")):
                assert device_supports_fp8_matmul(device) is False
        reset_fp8_matmul_support_cache()


class TestMxfp8:
    """MXFP8 block scales are decoded when the file names them, and refused when it does not.

    Both halves matter. The grid is stored in cuBLAS tiles, so reading it row by row pairs blocks
    with the wrong rows -- an earlier attempt at this decode reached a correlation of 0.60 against a
    reference build and generated a pure-noise image, with the block-wise expansion making such a
    file *loadable* rather than raising. And a `uint8` tensor beside an fp8 weight is not
    self-evidently MXFP8: it is whatever its producer meant, so the layer has to say so.
    """

    @staticmethod
    def _mx_layer(exponents: torch.Tensor, marker: dict | None) -> tuple[dict, dict]:
        """One MXFP8 layer and its hints, the layout coming from the shared builder.

        The hints stay a parameter here because half these cells are about a layer that says the
        wrong thing about itself, or nothing at all.
        """
        tensors, _expected = mxfp8_tensors("lin", exponents)
        return dict(tensors), ({"lin": marker} if marker else {})

    @staticmethod
    def _mx_layer_with_expectation(exponents: torch.Tensor) -> tuple[dict, dict, torch.Tensor]:
        """The well-formed case, plus the decoded scale the builder says to expect."""
        tensors, expected = mxfp8_tensors("lin", exponents)
        return dict(tensors), {"lin": mxfp8_marker()}, expected

    def test_the_exponents_are_decoded_and_unswizzled(self) -> None:
        """Distinct exponents across the grid, so a read in the stored order lands the wrong one on
        all but a few entries.

        Against the expectation the shared builder returns, not a restatement of it here: that value
        is derived from the hand-written tile formula and the spec's bias, so it is the one statement
        of the decode that does not come from the decode.
        """
        exponents = torch.arange(128 * 4, dtype=torch.int64).reshape(128, 4) % 8 + 124

        sd, hints, expected = self._mx_layer_with_expectation(exponents)
        layers = extract_fp8_scaled_layers(sd, layer_hints=hints)

        assert torch.equal(layers["lin"].weight_scale, expected)

    def test_a_grid_no_marker_or_header_names_is_refused(self) -> None:
        """The dtype alone is not evidence. Reading an unknown producer's uint8 grid as MXFP8 is
        exactly the guess that generates noise without a log line."""
        sd, _ = self._mx_layer(torch.full((128, 4), 125, dtype=torch.int64), None)

        with pytest.raises(NotImplementedError) as excinfo:
            extract_fp8_scaled_layers(sd)

        message = str(excinfo.value)
        assert "lin" in message, "the failing layer must be named"
        assert "MXFP8" in message
        assert "noise" in message, "say what happens if it were loaded anyway"

    def test_the_header_names_it_too(self) -> None:
        """Comfy-Org's Krea-2 build ships no per-tensor markers at all: every layer is named only in
        `_quantization_metadata`, and without a `block_size`."""
        exponents = torch.full((128, 4), 129, dtype=torch.int64)

        sd, _ = self._mx_layer(exponents, None)
        layers = extract_fp8_scaled_layers(sd, layer_hints={"lin": {"format": "mxfp8"}})

        assert torch.equal(layers["lin"].weight_scale, torch.full((128, 4), 4.0))

    def test_a_declared_block_size_that_contradicts_the_shapes_is_refused(self) -> None:
        """The width is read off the shapes; the marker only cross-checks it. A producer that
        changed the block size would otherwise be decoded against the wrong blocks."""
        sd, hints = self._mx_layer(torch.full((128, 4), 127, dtype=torch.int64), {"format": "mxfp8", "block_size": 16})

        with pytest.raises(ValueError, match="blocks of 16"):
            extract_fp8_scaled_layers(sd, layer_hints=hints)

    def test_the_reserved_nan_exponent_is_refused(self) -> None:
        """0xFF is reserved in E8M0. Decoded as an exponent it is 2**128; propagated as NaN it
        poisons the layer just as quietly. Neither published build contains one."""
        exponents = torch.full((128, 4), 127, dtype=torch.int64)
        exponents[7, 2] = 0xFF

        sd, hints = self._mx_layer(exponents, {"format": "mxfp8", "block_size": 32})

        with pytest.raises(ValueError, match="reserved"):
            extract_fp8_scaled_layers(sd, layer_hints=hints)

    def test_an_mxfp8_layer_is_folded_rather_than_kept_for_the_matmul(self) -> None:
        """`_scaled_mm` cannot apply block scales before Blackwell and torch 2.8, so these layers
        have to be widened at load. Driven through the split that actually decides it, not through
        the predicate underneath: a decode that returned a per-tensor or per-row scale would satisfy
        the predicate test and still be wrong here."""
        exponents = torch.full((128, 4), 128, dtype=torch.int64)
        sd, hints = self._mx_layer(exponents, {"format": "mxfp8", "block_size": 32})
        layers = extract_fp8_scaled_layers(sd, layer_hints=hints)

        surviving = split_fp8_scaled_layers(sd, layers, torch.float32)

        assert surviving == {}
        assert sd["lin.weight"].dtype is torch.float32

    def test_the_decoded_scale_folds_into_the_weight(self) -> None:
        """End to end: the grid widens 32-fold across the row and multiplies the codes."""
        exponents = torch.tensor([[128, 129, 130, 131]] * 128, dtype=torch.int64)

        sd, hints = self._mx_layer(exponents, {"format": "mxfp8", "block_size": 32})
        layers = extract_fp8_scaled_layers(sd, layer_hints=hints)
        dequantize_fp8_scaled(sd, layers, torch.float32)

        expected = torch.cat([torch.full((128, 32), 2.0 ** (e - 127)) for e in (128, 129, 130, 131)], dim=1)
        assert torch.equal(sd["lin.weight"], expected)

    def test_every_exponent_byte_decodes_the_way_torch_reads_e8m0(self) -> None:
        """The bias is checked against the specification, not against a restatement of the code.

        `torch.float8_e8m0fnu` *is* the encoding: a byte is a biased exponent and nothing else. So
        every finite value is decoded here and compared with what torch makes of the same byte. An
        off-by-one bias, or reading the byte as a linear multiplier — the mistake that shipped once
        and inflated weights ~470,000x — cannot survive this.

        No checkpoint is needed for it, which matters: the two published MXFP8 builds are Krea-2's
        and NVIDIA's PiD, and neither is licensed for their weights to be carried in this repo. The
        de-swizzle is measured against them instead, and the numbers are in the class docstring.
        """
        finite = torch.arange(255, dtype=torch.uint8)

        sd, hints = self._mx_layer(finite.reshape(1, 255).expand(128, 255)[:, :4].contiguous().long(), None)
        decoded = decode_mx_block_scales("lin", sd["lin.weight"], sd["lin.weight_scale"], {"format": "mxfp8"})

        expected = unblock_scale_grid(sd["lin.weight_scale"]).view(torch.float8_e8m0fnu).float()
        assert torch.equal(decoded, expected)

    def test_a_scale_typed_as_e8m0_decodes_like_the_same_bytes_as_uint8(self) -> None:
        """safetensors has no E8M0 dtype so producers write `uint8`, but torch grew
        `float8_e8m0fnu` and another producer may use it. The two must decode identically: the code
        reinterprets the bytes rather than converting the values, and `scale.to(torch.int32)` on an
        e8m0 tensor silently yields zeros — every scale would become 2**-127."""
        exponents = torch.arange(128 * 4, dtype=torch.int64).reshape(128, 4) % 8 + 124

        as_bytes, hints = self._mx_layer(exponents, {"format": "mxfp8"})
        as_e8m0 = dict(as_bytes)
        as_e8m0["lin.weight_scale"] = as_bytes["lin.weight_scale"].view(torch.float8_e8m0fnu)

        assert torch.equal(
            extract_fp8_scaled_layers(as_bytes, layer_hints=hints)["lin"].weight_scale,
            extract_fp8_scaled_layers(as_e8m0, layer_hints=hints)["lin"].weight_scale,
        )

    def test_the_header_transport_names_it_too(self) -> None:
        """Comfy-Org's Krea-2 build — the one file this decode reaches today — ships no per-tensor
        markers at all: all 256 layers are named only in `_quantization_metadata`, and without a
        `block_size`. So the header is read here rather than hints being handed in ready-made."""
        exponents = torch.full((128, 4), 129, dtype=torch.int64)
        sd, _ = self._mx_layer(exponents, None)
        metadata = {QUANT_METADATA_KEY: json.dumps({"layers": {"lin": {"format": "mxfp8"}}})}

        layers = extract_fp8_scaled_layers(sd, metadata=metadata)

        assert torch.equal(layers["lin"].weight_scale, torch.full((128, 4), 4.0))

    def test_a_grid_whose_rows_do_not_match_the_weight_is_refused(self) -> None:
        """Left to `expand_weight_scale`, a short grid is repeat_interleaved along the row axis
        instead — every row scaled by another row's exponent, loading cleanly."""
        sd, hints = self._mx_layer(torch.full((128, 4), 127, dtype=torch.int64), {"format": "mxfp8"})
        sd["lin.weight"] = torch.ones(256, 128).to(FP8_DTYPE)

        with pytest.raises(ValueError, match="matching the weight"):
            extract_fp8_scaled_layers(sd, layer_hints=hints)

    def test_an_inferred_block_width_the_standard_does_not_use_is_refused(self) -> None:
        """Krea-2's build declares no `block_size`, so for the one reachable file the width is
        inferred outright. MX is 32 by definition; anything else is a producer this decode has not
        seen, and guessing at it is what this whole change exists to stop."""
        exponents = torch.full((128, 8), 127, dtype=torch.int64)
        sd, hints = self._mx_layer(exponents, {"format": "mxfp8"})
        sd["lin.weight"] = torch.ones(128, 128).to(FP8_DTYPE)

        # The message names the width MX is defined at, which is the one the file failed to meet.
        with pytest.raises(ValueError, match="blocks of 32"):
            extract_fp8_scaled_layers(sd, layer_hints=hints)

    def test_a_padded_block_axis_cannot_pass_as_a_wider_block(self) -> None:
        """`to_blocked` pads the *block* axis up to a multiple of 4, which is what makes the tile
        check pass -- so the tile check can never see that padding. An `in_features` of 192 gives 6
        real blocks padded to 8, and 192/8 divides evenly at 24, so nothing but the block width
        itself stands between a padded grid and every scale landing on the wrong 24 elements."""
        weight = torch.ones(128, 192).to(FP8_DTYPE)
        grid = torch.full((128, 8), 127, dtype=torch.uint8)

        with pytest.raises(ValueError, match="blocks of 32"):
            decode_mx_block_scales("lin", weight, grid, {"format": "mxfp8"})

    def test_a_loader_that_folds_without_decoding_refuses_rather_than_multiplying_bytes(self) -> None:
        """Several loaders fold `weight_scale` into the weight directly instead of going through
        the extraction. There the exponent bytes would be used as linear multipliers -- ~120-135
        instead of 2**(v-127) -- which loads cleanly and generates noise."""
        with pytest.raises(NotImplementedError, match="folds weight scales"):
            expand_weight_scale(torch.ones(128, 128), torch.full((128, 4), 127, dtype=torch.uint8))

    def test_float_scales_are_unaffected(self) -> None:
        """The decode keys off the dtype, so ordinary scaled-fp8 checkpoints must still load."""
        sd = {
            "lin.weight": torch.full((4, 64), 2.0).to(FP8_DTYPE),
            "lin.weight_scale": torch.full((4, 2), 0.25),
        }

        layers = extract_fp8_scaled_layers(sd)

        assert tuple(layers["lin"].weight_scale.shape) == (4, 2)


class TestNonScalarInputScale:
    """A multi-element ``input_scale`` is dropped, not raised on.

    `scaled_mm_linear` scales activations by one value, so there is nothing to do with a
    per-channel or per-block activation scale -- but reshaping it to a scalar unconditionally
    turned such a checkpoint into a shape error at load time. Every other malformed side-channel in
    this module is either skipped or refused with an actionable message; this one was neither.
    """

    def _sd(self, input_scale: torch.Tensor) -> dict[str, torch.Tensor]:
        return {
            "blocks.0.attn.weight": torch.zeros(16, 16, dtype=torch.float32).to(FP8_DTYPE),
            "blocks.0.attn.weight_scale": torch.tensor(2.0),
            "blocks.0.attn.input_scale": input_scale,
        }

    def test_a_per_channel_input_scale_does_not_abort_the_load(self) -> None:
        layers = extract_fp8_scaled_layers(self._sd(torch.full((16,), 0.5)))

        assert set(layers) == {"blocks.0.attn"}
        # Dropped, so the forward falls back to the dynamic amax path -- which is correct.
        assert layers["blocks.0.attn"].input_scale is None
        # The weight scale is unaffected by the unusable activation scale.
        assert layers["blocks.0.attn"].weight_scale is not None

    def test_a_scalar_input_scale_is_still_kept(self) -> None:
        layers = extract_fp8_scaled_layers(self._sd(torch.tensor(0.5)))

        assert layers["blocks.0.attn"].input_scale is not None


class TestMatmulUsableScale:
    """`is_matmul_usable_scale` decides what may reach `_scaled_mm` un-dequantized."""

    def test_a_per_tensor_scale_is_usable(self) -> None:
        assert is_matmul_usable_scale(torch.zeros(32, 16), torch.tensor(2.0)) is True

    def test_a_per_output_channel_scale_is_usable(self) -> None:
        assert is_matmul_usable_scale(torch.zeros(32, 16), torch.full((32,), 2.0)) is True

    def test_a_one_dimensional_scale_of_the_wrong_length_is_not(self) -> None:
        """The branch that keeps a mislabelled scale out of the kernel.

        A 1-D scale whose length is not the row count is neither per-tensor nor
        per-output-channel. Letting it through raises inside `_scaled_mm` mid-generation instead of
        dequantizing the layer up front, which is the whole point of asking.
        """
        assert is_matmul_usable_scale(torch.zeros(32, 16), torch.full((16,), 2.0)) is False

    def test_a_block_wise_scale_is_not(self) -> None:
        assert is_matmul_usable_scale(torch.zeros(32, 16), torch.full((4, 2), 2.0)) is False


class TestReattachToNonWeightDestination:
    """The reattach guard must not assume every module stores its parameter as ``weight``.

    A producer that quantizes norms -- the "quantizes everything" class this module documents --
    writes ``<path>.scale``. Testing for ``<destination>.weight`` rejects such a destination for the
    wrong reason and drops a scale it could have placed, with only a log line to show for it.
    """

    def test_a_destination_whose_tensor_is_not_named_weight_still_receives_its_scale(self) -> None:
        sd = {"blocks.0.norm_q.scale": torch.zeros(16, dtype=torch.float32).to(FP8_DTYPE)}
        detached = {"blocks.0.qnorm": [(".weight_scale", torch.tensor(2.0))]}

        orphaned = reattach_layer_sidechannel(sd, detached, {"blocks.0.qnorm": "blocks.0.norm_q"})

        assert orphaned == []
        assert "blocks.0.norm_q.weight_scale" in sd

    def test_a_destination_absent_from_the_state_dict_is_still_reported(self) -> None:
        sd = {"blocks.0.attn.weight": torch.zeros(4, 4)}
        detached = {"blocks.0.dropped": [(".weight_scale", torch.tensor(2.0))]}

        orphaned = reattach_layer_sidechannel(sd, detached, {})

        assert orphaned == ["blocks.0.dropped"]
        assert not [k for k in sd if k.endswith(".weight_scale")]


class TestProbeTransientFailures:
    """Only a definitively unsupported device may be cached; everything else re-probes.

    Listing the transient wordings is the wrong way round: an OOM and a cuBLAS workspace failure
    are two of the ways a loaded machine can fail this call, and any wording not on the list would
    be cached as permanent -- reproducing the failure the OOM branch was written to avoid.
    """

    def test_an_unrecognized_runtime_error_is_not_cached(self) -> None:
        reset_fp8_matmul_support_cache()
        device = torch.device("cuda", 0)
        with _probe_on_cpu():
            with mock.patch("torch._scaled_mm", side_effect=RuntimeError("CUDA driver reset")):
                assert device_supports_fp8_matmul(device) is False
            # Inconclusive, so the next *load* re-probes rather than the process losing fp8.
            forget_inconclusive_fp8_matmul_probe(device)
            with mock.patch("torch._scaled_mm", return_value=torch.zeros(1)):
                assert device_supports_fp8_matmul(device) is True
        reset_fp8_matmul_support_cache()

    def test_an_inconclusive_probe_is_not_repeated_on_the_forward_path(self) -> None:
        """`CustomLinear._can_use_fp8_matmul` asks once per quantized fp8 Linear per step.

        Leaving an inconclusive answer unrecorded meant re-probing every one of them, every step,
        for the whole generation -- measured on a 4090 at 32 probes/step for 32 Linears, against 1
        in total once it is recorded. Each probe allocates three device tensors and launches a
        `_scaled_mm` that is about to fail.
        """
        reset_fp8_matmul_support_cache()
        device = torch.device("cuda", 0)
        with _probe_on_cpu():
            with mock.patch("torch._scaled_mm", side_effect=RuntimeError("CUDA driver reset")) as probe:
                for _ in range(5):
                    assert device_supports_fp8_matmul(device) is False
            assert probe.call_count == 1, "the forward path re-probed"
        reset_fp8_matmul_support_cache()

    def test_a_load_gives_an_inconclusive_probe_another_go(self) -> None:
        """The retry belongs to the load, which can afford one probe, and not to the forward."""
        reset_fp8_matmul_support_cache()
        device = torch.device("cuda", 0)
        set_fp8_matmul_enabled(True)
        try:
            with _probe_on_cpu():
                with mock.patch("torch._scaled_mm", side_effect=torch.OutOfMemoryError("transient")):
                    assert should_keep_fp8_weights(device) is False
                with mock.patch("torch._scaled_mm", return_value=torch.zeros(1)) as probe:
                    assert should_keep_fp8_weights(device) is True
                assert probe.call_count == 1
        finally:
            set_fp8_matmul_enabled(None)
            reset_fp8_matmul_support_cache()

    def test_a_capability_error_is_cached(self) -> None:
        reset_fp8_matmul_support_cache()
        device = torch.device("cuda", 0)
        message = "torch._scaled_mm is only supported on CUDA devices with compute capability >= 8.9"
        with _probe_on_cpu():
            with mock.patch("torch._scaled_mm", side_effect=RuntimeError(message)):
                assert device_supports_fp8_matmul(device) is False
            with mock.patch("torch._scaled_mm", side_effect=AssertionError("must not be probed again")):
                assert device_supports_fp8_matmul(device) is False
        reset_fp8_matmul_support_cache()


class TestExpandWeightScaleAxis:
    """A per-output-channel scale must scale rows, not columns.

    `(out, in) * (out,)` broadcasts on the *last* axis, so a bare multiply scales input channels --
    wrong on a square weight, a shape error on any other. Both legacy folds used to do exactly that.
    """

    def test_a_per_channel_scale_lines_up_with_the_rows(self) -> None:
        weight = torch.ones(3, 2)
        scale = torch.tensor([1.0, 2.0, 3.0])

        result = weight * expand_weight_scale(weight, scale)

        assert torch.equal(result, torch.tensor([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]))

    def test_a_non_square_weight_no_longer_raises(self) -> None:
        weight = torch.ones(4, 2)

        # Without the expansion this is a broadcast error, not merely a wrong number.
        assert (weight * expand_weight_scale(weight, torch.arange(4, dtype=torch.float32))).shape == (4, 2)


class TestPredictionIsSplitAware:
    """`make_room` runs before `split_fp8_scaled_layers`, so the prediction has to account for what
    the split widens — not just for what the cast keeps.

    `can_stay_quantized` knows nothing about the scale *layout*. A block-wise-scaled 2-D
    `nn.Linear.weight` passes it, gets predicted at 1 byte/element, and is then dequantized to
    `dtype` by the split at 2 — the reservation is half the truth, and on a real checkpoint using
    that layout the shortfall is the whole quantized-Linear set.
    """

    def _model(self):
        model = torch.nn.Sequential()
        model.add_module("lin", torch.nn.Linear(16, 32))
        return model

    @pytest.mark.parametrize(
        "label, scale, stays_fp8",
        [
            ("per-tensor", torch.tensor(2.0), True),
            ("per-output-channel", torch.full((32,), 2.0), True),
            ("block-wise", torch.full((8, 2), 2.0), False),
        ],
    )
    def test_prediction_matches_the_bytes_left_after_split_and_cast(self, label, scale, stays_fp8):
        model = self._model()
        sd = {"lin.weight": torch.zeros(32, 16).to(FP8_DTYPE), "lin.weight_scale": scale}
        layers = extract_fp8_scaled_layers(sd)

        predicted = predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=True, model=model, scaled_layers=layers)
        remaining = split_fp8_scaled_layers(sd, layers, torch.bfloat16, model=model)
        cast_state_dict(sd, torch.bfloat16, keep_fp8=True, model=model)
        actual = sum(t.nelement() * t.element_size() for t in sd.values())

        assert predicted == actual, f"{label}: reserved {predicted}, needed {actual}"
        assert bool(remaining) is stays_fp8
        assert (sd["lin.weight"].dtype is FP8_DTYPE) is stays_fp8

    def test_without_the_mapping_the_block_wise_layer_is_under_counted(self):
        """The regression itself: omitting `scaled_layers` reverts to the weaker predicate."""
        model = self._model()
        sd = {"lin.weight": torch.zeros(32, 16).to(FP8_DTYPE), "lin.weight_scale": torch.full((8, 2), 2.0)}
        layers = extract_fp8_scaled_layers(sd)

        blind = predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=True, model=model)
        aware = predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=True, model=model, scaled_layers=layers)
        assert aware == 2 * blind

    def test_raw_fp8_without_any_scale_is_unaffected(self):
        """A weight with no `weight_scale` has no layout to reject; it stays fp8 either way."""
        model = self._model()
        sd = {"lin.weight": torch.zeros(32, 16).to(FP8_DTYPE)}
        with_map = predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=True, model=model, scaled_layers={})
        without = predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=True, model=model)
        assert with_map == without == 32 * 16


class TestQwen3VlStyleSplit:
    """Pins the helper contract the Qwen3-VL encoder switched to.

    Its old per-key `if dtype is not FP8_DTYPE` loop kept *every* fp8 tensor quantized. The three
    things that got wrong are all visible without any `transformers` dependency, against a stub
    module tree.
    """

    def _model(self):
        model = torch.nn.Module()
        model.add_module("norm", torch.nn.LayerNorm(32))
        model.add_module("lin", torch.nn.Linear(16, 32))
        model.add_module("plain", torch.nn.Linear(16, 32))
        return model

    def test_scaled_norm_is_folded_and_block_wise_linear_is_dropped(self):
        model = self._model()
        norm_scale = torch.tensor(4.0)
        sd = {
            # A 1-D fp8 norm carrying a scale: not a matmul weight, so it must be folded, not kept.
            "norm.weight": torch.ones(32).to(FP8_DTYPE),
            "norm.weight_scale": norm_scale,
            # A Linear whose scale layout `scaled_mm` cannot apply: folded too.
            "lin.weight": torch.ones(32, 16).to(FP8_DTYPE),
            "lin.weight_scale": torch.full((8, 2), 2.0),
            # A Linear the matmul can take: survives, and is what `attach_fp8_scales` gets.
            "plain.weight": torch.ones(32, 16).to(FP8_DTYPE),
            "plain.weight_scale": torch.tensor(3.0),
        }
        layers = extract_fp8_scaled_layers(sd)
        assert set(layers) == {"norm", "lin", "plain"}

        remaining = split_fp8_scaled_layers(sd, layers, torch.bfloat16, model=model)
        cast_state_dict(sd, torch.bfloat16, keep_fp8=True, model=model)

        assert set(remaining) == {"plain"}, "only the matmul-usable Linear may keep its scale"
        # The norm was dequantized *with* its scale applied — the old loop left it as raw fp8 codes.
        assert sd["norm.weight"].dtype is torch.bfloat16
        assert torch.allclose(sd["norm.weight"], torch.full((32,), 4.0, dtype=torch.bfloat16))
        assert sd["lin.weight"].dtype is torch.bfloat16
        assert sd["plain.weight"].dtype is FP8_DTYPE

    def test_e5m2_scaled_weight_keeps_its_scale_on_the_way_down(self):
        """Only e4m3fn may stay quantized, so an e5m2 weight must be folded rather than cast bare."""
        model = self._model()
        sd = {
            "plain.weight": torch.ones(32, 16).to(torch.float8_e5m2),
            "plain.weight_scale": torch.tensor(3.0),
        }
        layers = extract_fp8_scaled_layers(sd)
        remaining = split_fp8_scaled_layers(sd, layers, torch.bfloat16, model=model)

        assert remaining == {}
        assert torch.allclose(sd["plain.weight"], torch.full((32, 16), 3.0, dtype=torch.bfloat16))


class TestMalformedWeightScale:
    def test_a_1d_scale_of_the_wrong_length_is_reported_not_broadcast(self):
        """Neither per-tensor nor per-output-channel. Left to torch, the multiply raises "size of
        tensor a (32) must match tensor b (7)" from inside the fold, naming neither layer nor file."""
        with pytest.raises(ValueError, match="neither per-tensor nor per-output-channel"):
            expand_weight_scale(torch.ones(32, 16), torch.full((7,), 2.0))


class TestTheDecodeWidensTheGridOutsideThisPrediction:
    """Why this predictor cannot answer about an MXFP8 build, and what does.

    `extract_fp8_scaled_layers` pops every scale key and hands back a mapping, and for an MXFP8 layer
    it does more than move them: `decode_mx_block_scales` replaces the stored `uint8` exponent grid
    with a float32 one four times the size. That happens before the caller's `make_room`, so those
    bytes are resident while the cache decides what to evict -- and this function cannot see them,
    because it walks the state dict they have left.

    That is a property of its contract rather than a defect in it: it answers about the dict, exactly,
    and `TestPredictionIsSplitAware` pins the equality. Adding the mapping's bytes here would turn the
    answer into a peak and redden all three of those parametrizations. The question belongs to
    `reserve_for_load`, which is handed both halves; `tests/backend/quantization/test_load_plan.py`
    is where the grid is asserted to be inside the reservation.

    Kept here because the measurement is what motivates that function: on the released 12.6 GiB Krea-2
    build the grids total 1.455 GiB across 256 layers, more than the ~1.21 GB fold transient of its
    largest layer, and every seam used to omit them.
    """

    @staticmethod
    def _mx_layer(rows: int = 512, blocks: int = 16) -> tuple[dict, dict, torch.nn.Module]:
        exponents = (torch.arange(rows * blocks, dtype=torch.int64).reshape(rows, blocks) % 8) + 124
        sd, hints, _expected = TestMxfp8._mx_layer_with_expectation(exponents)
        model = torch.nn.Module()
        model.add_module("lin", torch.nn.Linear(blocks * MX_BLOCK_SIZE, rows, bias=False))
        return sd, hints, model

    def test_the_decode_widens_the_grid_and_this_prediction_does_not_see_it(self) -> None:
        """The stored grid leaves the state dict and a four-times-larger one takes its place in a
        mapping this function is handed but, by its contract, does not weigh."""
        sd, hints, model = self._mx_layer()
        stored_bytes = sd["lin.weight_scale"].nelement() * sd["lin.weight_scale"].element_size()

        layers = extract_fp8_scaled_layers(sd, layer_hints=hints)
        grid = layers["lin"].weight_scale

        assert "lin.weight_scale" not in sd
        assert grid.dtype is torch.float32 and stored_bytes * 4 == grid.nelement() * grid.element_size()
        # `keep_fp8=True`, the branch where `scaled_layers` and `model` are consulted at all. The MX
        # layer is charged 2 B/element either way, because a block-wise scale is never matmul-usable.
        assert (
            predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=True, model=model, scaled_layers=layers)
            == sd["lin.weight"].nelement() * 2
        )


def _torchao_layer(layer: str, out_f: int = 32, in_f: int = 16) -> tuple[dict[str, torch.Tensor], torch.Tensor]:
    """One layer as unsloth's torchao FP8 build stores it, and the weight it means (`q.float() * scale`)."""
    torch.manual_seed(0)
    q = (torch.randn(out_f, in_f) * 40).to(FP8_DTYPE)
    scale = torch.rand(out_f, 1) + 0.5
    return {f"{layer}._weight_qdata": q, f"{layer}._weight_scale": scale}, q.float() * scale


class TestTorchaoFp8:
    def test_a_translated_layer_dequantizes_to_the_weight_the_file_means(self) -> None:
        sd, meant = _torchao_layer("lin")

        assert translate_torchao_fp8_layers(sd) == 1

        assert set(sd) == {"lin.weight", "lin.weight_scale"}
        layers = extract_fp8_scaled_layers(sd)
        dequantize_fp8_scaled(sd, layers, torch.float32)
        assert torch.equal(sd["lin.weight"], meant)

    def test_a_translated_layer_stays_fp8_with_a_flat_scale_through_the_split(self) -> None:
        """The `[out, 1]` scale reads as block-wise unless flattened; the split would then fold the layer to bf16."""
        sd, meant = _torchao_layer("lin")
        model = torch.nn.Sequential()
        model.add_module("lin", torch.nn.Linear(16, 32))
        translate_torchao_fp8_layers(sd)
        layers = extract_fp8_scaled_layers(sd)

        assert layers["lin"].weight_scale.shape == (32,)
        kept = split_fp8_scaled_layers(sd, layers, torch.bfloat16, model=model)

        assert set(kept) == {"lin"}
        assert sd["lin.weight"].dtype is FP8_DTYPE
        assert torch.equal(dequantize_weight(sd["lin.weight"], kept["lin"].weight_scale, torch.float32), meant)

    @pytest.mark.parametrize("scale_shape", [(32, 1), (32,)])
    def test_a_per_row_scale_is_accepted_flat_or_not(self, scale_shape: tuple[int, ...]) -> None:
        sd, _ = _torchao_layer("lin")
        sd["lin._weight_scale"] = sd["lin._weight_scale"].reshape(scale_shape)
        assert find_torchao_fp8_problem(sd) is None

    def test_a_state_dict_without_torchao_keys_has_nothing_to_translate(self) -> None:
        sd = {"lin.weight": torch.zeros(4, 4), 7: torch.zeros(1)}
        assert find_torchao_fp8_problem(sd) is None
        assert translate_torchao_fp8_layers(sd) == 0
        assert set(sd) == {"lin.weight", 7}

    @pytest.mark.parametrize(
        ("label", "edit", "fragment"),
        [
            ("int8 payload", lambda sd: sd.update({"b._weight_qdata": torch.zeros(32, 16, dtype=torch.int8)}), "int8"),
            (
                "zero point",
                lambda sd: sd.update({"b._weight_zero_point": torch.zeros(32, 1, dtype=torch.int8)}),
                "zero point",
            ),
            ("per-tensor scale", lambda sd: sd.update({"b._weight_scale": torch.ones(1)}), "per-tensor"),
            ("block-wise scale", lambda sd: sd.update({"b._weight_scale": torch.ones(32, 4)}), "block-wise"),
            ("half precision scale", lambda sd: sd.update({"b._weight_scale": torch.ones(32, 1).half()}), "float32"),
            ("orphaned payload", lambda sd: sd.pop("b._weight_scale"), "no torchao scale"),
            ("orphaned scale", lambda sd: sd.pop("b._weight_qdata"), "no quantized weight"),
            (
                "torchao tensor this decode does not apply",
                lambda sd: sd.update({"b._weight_act_pre_scale": torch.ones(16)}),
                "_weight_act_pre_scale",
            ),
            ("plain weight beside it", lambda sd: sd.update({"b.weight": torch.zeros(32, 16)}), "plain weight"),
            (
                "3-D payload",
                lambda sd: sd.update({"b._weight_qdata": torch.zeros(32, 4, 4).to(FP8_DTYPE)}),
                "3-D",
            ),
        ],
    )
    def test_anything_but_the_per_row_fp8_layout_is_refused_by_name(self, label: str, edit, fragment: str) -> None:
        # A good layer sorts before the bad one: a check that stops at the first layer would pass.
        sd = {**_torchao_layer("a")[0], **_torchao_layer("b")[0]}
        edit(sd)
        before = dict(sd)

        problem = find_torchao_fp8_problem(sd)

        assert problem is not None and "'b'" in problem and fragment in problem, label
        with pytest.raises(ValueError, match="torchao"):
            translate_torchao_fp8_layers(sd)
        assert sd == before, "a refused state dict must be left as it was"

    def test_identification_can_check_meta_tensors(self) -> None:
        sd, _ = _torchao_layer("a")
        meta = {key: value.to("meta") for key, value in sd.items()}
        assert find_torchao_fp8_problem(meta) is None
        meta["a._weight_scale"] = torch.empty(32, 4, device="meta")
        assert find_torchao_fp8_problem(meta) is not None