Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
feat(quantization): reserve for the side channel a load is still holding
Alexander Eichhornda55c6ee2203
diff --git a/invokeai/backend/quantization/load_plan.py b/invokeai/backend/quantization/load_plan.pynew file mode 100644index 0000000000..cd58c63c57--- /dev/null+++ b/invokeai/backend/quantization/load_plan.py@@ -0,0 +1,103 @@+"""Reserve for what a load is holding, not for half of it.++A single-file load recovers a quantization side channel out of its state dict -- scaled-fp8 scales+via `extract_fp8_scaled_layers`, nvfp4 packed payloads via `pop_nvfp4_layers` -- and then asks the+RAM cache for room. Those were separate steps, and the number was assembled at each seam:+`predict_cast_state_dict_size(...)` plus, where the loader remembered, `predict_nvfp4_install_size(...)`.+Fourteen call sites of the first and seven of the second, across eight loaders.++The gap is structural rather than careless. `predict_cast_state_dict_size` answers about the *state+dict*, exactly and by contract -- bytes it will occupy once the split and the cast have run. The+extraction has already popped the scale keys out of that dict, so that function cannot count them and+must not: tying the two together breaks the equality its own cells pin, because whether a scale+survives the split depends on whether its layer stayed fp8.++So the composition belongs to one function that is handed both halves. On Comfy-Org's released MXFP8+Krea-2 build the term nobody could add is 1.455 GiB: `decode_mx_block_scales` replaces each stored+`uint8` exponent grid with a float32 one four times the size, one entry per 32 weight elements, and+they stay resident from the extraction until the split consumes them.++A function rather than a value object, deliberately. An earlier draft was a frozen dataclass, and+each of its extra affordances turned into a defect: the generated constructor defaulted the two+mappings and so silently under-reserved, `frozen` generated `__eq__`/`__hash__` over dicts of tensors+that raise when called, and an instance bound to a local outlives the fold that frees the grids. No+caller wanted the decomposition -- they all want the reservation made.++`reserve` is a callable rather than the cache itself, matching `install_int8_convrot_layers(...,+reserve=...)` next door. That is not only for tests: it keeps `backend/quantization` from acquiring an+import edge into `backend/model_manager/load/model_cache`, which nothing in this package has today.+"""++from collections.abc import Callable, Iterable, Mapping+from typing import Any++import torch++from invokeai.backend.quantization.fp8_scaled import Fp8ScaledLayer, predict_cast_state_dict_size+from invokeai.backend.quantization.nvfp4 import NVFP4Payload, predict_nvfp4_install_size+++def side_channel_bytes(fp8_layers: Mapping[str, Fp8ScaledLayer]) -> int:+ """The recovered scales, resident from extraction until the split consumes them.++ `extract_fp8_scaled_layers` pops each scale key out of the state dict, so from that point on no+ prediction over the dict can see them. Small for a per-tensor scale, and not small for an MXFP8+ grid -- 1.455 GiB across 256 layers on the released 12.6 GiB Krea-2 build.++ `input_scale` is `None` where the producer shipped none, or where `_usable_input_scale` rejected an+ uncalibrated placeholder; that is the only absence counted as nothing. Anything else lacking the+ tensor interface raises rather than being charged zero, because a silent zero here is the failure+ this module exists to remove.+ """+ total = 0+ for layer in fp8_layers.values():+ for scale in (layer.weight_scale, layer.input_scale):+ if scale is not None:+ total += scale.nelement() * scale.element_size()+ return total+++def reserve_for_load(+ reserve: Callable[[int], None],+ state_dict: Mapping[str, Any],+ dtype: torch.dtype,+ *,+ keep_fp8: bool,+ fp8_layers: Mapping[str, Fp8ScaledLayer],+ nvfp4_payloads: Mapping[str, NVFP4Payload],+ model: torch.nn.Module | None = None,+ skip_patterns: Iterable[str] = (),+) -> int:+ """Ask for room for the state dict, the payloads it will install, and the scales it took out.++ Both side-channel mappings are required rather than defaulted. A seam that recovered nothing+ passes `{}` and says so; a default would let a seam that *did* recover something omit it by+ silence and under-reserve by exactly the amount this function exists to count. That is weaker than+ making the ordering unrepresentable -- nothing here can stop a caller reserving before it has+ recovered -- and it is what a function taking the results can honestly give.++ Returns the bytes asked for, so a caller can log or assert on them.+ """+ patterns = tuple(skip_patterns)+ if nvfp4_payloads and model is None:+ # `predict_nvfp4_install_size` needs the model to decide which payloads stay packed. Absorbing+ # that into a zero would drop a GiB-scale term without a word, which is this module's subject.+ raise ValueError(+ f"Cannot size {len(nvfp4_payloads)} nvfp4 payload(s) without the model they are installed "+ "into; reserve after the model is built, or pass no payloads."+ )++ needed = (+ predict_cast_state_dict_size(+ state_dict,+ dtype,+ keep_fp8=keep_fp8,+ model=model,+ skip_patterns=patterns,+ scaled_layers=fp8_layers,+ )+ + (predict_nvfp4_install_size(model, nvfp4_payloads, dtype, patterns) if nvfp4_payloads else 0)+ + side_channel_bytes(fp8_layers)+ )+ reserve(needed)+ return neededdiff --git a/tests/backend/quantization/test_load_plan.py b/tests/backend/quantization/test_load_plan.pynew file mode 100644index 0000000000..bc55cdb5cc--- /dev/null+++ b/tests/backend/quantization/test_load_plan.py@@ -0,0 +1,182 @@+"""`reserve_for_load` asks for what the whole load is holding, not for half of it.++The reservation a seam makes has to cover what the load is holding. It used to be assembled at each+seam -- fourteen `predict_cast_state_dict_size` call sites and seven `predict_nvfp4_install_size`+ones -- and the term nobody could add was the side channel, because `extract_fp8_scaled_layers` pops+it out of the state dict that every prediction walks.++Each cell below isolates one term by making the others zero, so a total that came out right through+two compensating errors would still fail somewhere. The one thing not asserted here is the wiring:+whether a given loader passes the right mappings is pinned at the seams, in+`test_ideogram4_checkpoint_loader.py` and `test_qwen_image_nvfp4_loader.py`.+"""++import pytest+import torch++from invokeai.backend.quantization.fp8_scaled import extract_fp8_scaled_layers, predict_cast_state_dict_size+from invokeai.backend.quantization.load_plan import reserve_for_load, side_channel_bytes+from invokeai.backend.quantization.nvfp4 import pop_nvfp4_layers+from tests.fixtures.quantized_payloads import mxfp8_marker, mxfp8_tensors, nvfp4_signed_tensors, quantize_scaled_fp8+++def _linear_model(in_features: int, out_features: int) -> torch.nn.Module:+ model = torch.nn.Module()+ model.add_module("lin", torch.nn.Linear(in_features, out_features, bias=False))+ return model+++def _asked() -> tuple[list[int], object]:+ """A `reserve` callable and the list it records into."""+ asked: list[int] = []+ return asked, asked.append+++class TestTheSideChannelIsCounted:+ """The term the state dict cannot report, because it is no longer in the state dict."""++ def test_a_decoded_mx_grid_is_inside_the_reservation(self) -> None:+ """The finding this module was written for.++ `decode_mx_block_scales` replaces the stored `uint8` grid with a float32 one four times the+ size, inside the extraction that runs before the reservation. Measured on the released 12.6+ GiB Krea-2 build that is 1.455 GiB across 256 layers; here it is 32768 bytes beside a 524288+ byte weight, and the reservation is their sum.+ """+ exponents = (torch.arange(512 * 16, dtype=torch.int64).reshape(512, 16) % 8) + 124+ tensors, _expected = mxfp8_tensors("lin", exponents)+ sd = dict(tensors)+ model = _linear_model(16 * 32, 512)+ layers = extract_fp8_scaled_layers(sd, layer_hints={"lin": mxfp8_marker()})+ grid = layers["lin"].weight_scale+ asked, reserve = _asked()++ needed = reserve_for_load(+ reserve, sd, torch.bfloat16, keep_fp8=True, model=model, fp8_layers=layers, nvfp4_payloads={}+ )++ assert "lin.weight_scale" not in sd+ assert grid.dtype is torch.float32+ assert needed == sd["lin.weight"].nelement() * 2 + grid.nelement() * grid.element_size()+ assert asked == [needed]++ def test_a_per_tensor_scale_is_counted_too(self) -> None:+ """Four bytes, counted for the same reason. A reservation that special-cased MXFP8 would be+ answering about one scheme rather than about the mapping it was handed."""+ payload = quantize_scaled_fp8(torch.arange(32, dtype=torch.float32).reshape(4, 8))+ sd = {"lin.weight": payload.codes, "lin.weight_scale": payload.scale}+ layers = extract_fp8_scaled_layers(sd)+ _asked_list, reserve = _asked()++ needed = reserve_for_load(+ reserve, sd, torch.bfloat16, keep_fp8=False, model=_linear_model(8, 4), fp8_layers=layers, nvfp4_payloads={}+ )++ assert side_channel_bytes(layers) == payload.scale.nelement() * payload.scale.element_size()+ assert needed == 4 * 8 * torch.bfloat16.itemsize + side_channel_bytes(layers)++ def test_an_activation_scale_is_counted_beside_its_weight_scale(self) -> None:+ """Encoder builds ship both halves, and both are resident from extraction onward."""+ payload = quantize_scaled_fp8(torch.arange(32, dtype=torch.float32).reshape(4, 8))+ layers = extract_fp8_scaled_layers(+ {+ "lin.weight": payload.codes,+ "lin.weight_scale": payload.scale,+ "lin.input_scale": torch.tensor(0.5),+ }+ )++ assert layers["lin"].input_scale is not None+ assert side_channel_bytes(layers) == payload.scale.nelement() * payload.scale.element_size() + 4++ def test_a_scale_the_producer_did_not_ship_costs_nothing_and_nothing_else_is_absorbed(self) -> None:+ """`None` is the one absence charged as zero -- an uncalibrated `input_scale` is dropped by+ `_usable_input_scale` and really is not held. Anything else without the tensor interface has+ to raise, because a silent zero is the failure this module removes."""+ payload = quantize_scaled_fp8(torch.arange(32, dtype=torch.float32).reshape(4, 8))+ layers = extract_fp8_scaled_layers(+ {"lin.weight": payload.codes, "lin.weight_scale": payload.scale, "lin.input_scale": torch.tensor(1.0)}+ )++ assert layers["lin"].input_scale is None+ assert side_channel_bytes(layers) == payload.scale.nelement() * payload.scale.element_size()++ layers["lin"].__dict__["weight_scale"] = 0.25 # a float, not a tensor+ with pytest.raises(AttributeError):+ side_channel_bytes(layers)+++class TestTheDictAndThePayloads:+ def test_a_load_with_no_side_channel_asks_for_what_the_predictor_alone_says(self) -> None:+ """A dense checkpoint must not pay for this function. It is a superset, not a surcharge."""+ sd = {"lin.weight": torch.zeros(4, 8)}+ model = _linear_model(8, 4)+ _asked_list, reserve = _asked()++ needed = reserve_for_load(+ reserve, sd, torch.bfloat16, keep_fp8=False, model=model, fp8_layers={}, nvfp4_payloads={}+ )++ assert needed == predict_cast_state_dict_size(sd, torch.bfloat16, keep_fp8=False, model=model)++ def test_nvfp4_payloads_are_counted_although_the_dict_no_longer_holds_them(self) -> None:+ """`pop_nvfp4_layers` takes them out before any prediction, which is why every seam that has+ them used to add a second term by hand."""+ tensors, _expected = nvfp4_signed_tensors("lin", torch.randint(0, 2, (128, 64), dtype=torch.bool))+ sd = dict(tensors)+ dense = torch.zeros(4, 4)+ sd["other.weight"] = dense+ model = _linear_model(64, 128)+ payloads = pop_nvfp4_layers(sd, {"lin": {"format": "nvfp4"}})+ packed = sum(t.nelement() * t.element_size() for t in (payloads["lin"].weight, payloads["lin"].weight_scale))+ _asked_list, reserve = _asked()++ needed = reserve_for_load(+ reserve, sd, torch.bfloat16, keep_fp8=False, model=model, fp8_layers={}, nvfp4_payloads=payloads+ )++ # The dense remainder is still in the dict and the packed layer is not, so the two terms are+ # separable: a reservation that dropped either would miss this by a different amount.+ assert needed > dense.nelement() * torch.bfloat16.itemsize+ assert needed >= packed++ def test_payloads_without_the_model_that_sizes_them_are_refused(self) -> None:+ """`predict_nvfp4_install_size` needs the model to decide which payloads stay packed.++ Absorbing its absence into a zero would drop a GiB-scale term silently -- the exact shape of+ defect this module exists to remove -- and a model-less reservation is a real pattern in this+ tree (`flux.py`, where the architecture is read off the keys before any model exists).+ """+ tensors, _expected = nvfp4_signed_tensors("lin", torch.randint(0, 2, (128, 64), dtype=torch.bool))+ sd = dict(tensors)+ payloads = pop_nvfp4_layers(sd, {"lin": {"format": "nvfp4"}})+ _asked_list, reserve = _asked()++ with pytest.raises(ValueError, match="without the model"):+ reserve_for_load(reserve, sd, torch.bfloat16, keep_fp8=False, fp8_layers={}, nvfp4_payloads=payloads)+++class TestKeepFp8IsCarried:+ """The flag decides what stays at one byte per element, and it reaches the prediction from here."""++ @pytest.mark.parametrize("keep_fp8", [True, False], ids=["kept", "folded"])+ def test_a_matmul_usable_scale_is_charged_one_byte_when_kept_and_two_when_folded(self, keep_fp8: bool) -> None:+ """A per-tensor scale *is* matmul-usable, so the two branches differ -- unlike an MX grid,+ which is charged the widened size either way and so cannot see this flag at all."""+ payload = quantize_scaled_fp8(torch.arange(32, dtype=torch.float32).reshape(4, 8))+ sd = {"lin.weight": payload.codes, "lin.weight_scale": payload.scale}+ layers = extract_fp8_scaled_layers(sd)+ _asked_list, reserve = _asked()++ needed = reserve_for_load(+ reserve,+ sd,+ torch.bfloat16,+ keep_fp8=keep_fp8,+ model=_linear_model(8, 4),+ fp8_layers=layers,+ nvfp4_payloads={},+ )++ weight_bytes = 4 * 8 * (1 if keep_fp8 else torch.bfloat16.itemsize)+ assert needed == weight_bytes + side_channel_bytes(layers)