fix(fp8): never apply FP8 storage to already-quantized weights - #9416
Open
Pfannkuchensack wants to merge 5 commits into
Open
fix(fp8): never apply FP8 storage to already-quantized weights#9416Pfannkuchensack wants to merge 5 commits into
Pfannkuchensack wants to merge 5 commits into
Conversation
Z-Image was excluded from FP8 storage in invoke-ai#8945 because diffusers' enable_layerwise_casting() was called with the global torch dtype (fp16) while Z-Image loads in bf16: skipped modules stayed bf16, hooked ones produced fp16, and attention crashed. That root cause was fixed later in the same PR — the compute dtype now comes from the model's own parameters — so the exclusion is obsolete. Removing it alone is not enough. Our hook-based cast (invoke-ai#9231) dropped one thing diffusers' enable_layerwise_casting() did: honoring the model's declared _skip_layerwise_casting_patterns. Z-Image needs it, and not for quality — TimestepEmbedder.forward reads self.mlp[0].weight.dtype and casts its *input* to it. With an fp8 weight the input becomes float8 before our pre-hook restores the weight, and F.linear dies with: RuntimeError: "addmm_cuda" not implemented for 'Float8_e4m3fn' which is why ZImageTransformer2DModel declares ['t_embedder', 'cap_embedder']. _apply_fp8_to_nn_module now takes extra_skip_patterns and the caller passes the model's list. For other models this is a strict superset of our defaults (FLUX/SD3 pos_embed+norm, UNet norm, CogView4 also proj_out), so it only ever skips more. Also wire the cast into ZImageCheckpointModel: only the diffusers loader called it, so the toggle was a silent no-op for single-file Z-Image models even though both paths build the same ZImageTransformer2DModel. Tested end to end on CUDA: transformer resident VRAM drops from ~11.5GB to 5880MB for both Z-Image-Turbo (diffusers) and Z-Image-Turbo (checkpoint, 14.37GB file), with clean output images in both cases.
The fp8_storage toggle was shown for Anima main models but did nothing: AnimaCheckpointModel never called _apply_fp8_layerwise_casting. Wire it in — the state dict is cast to a single model_dtype before load_state_dict, so the layerwise cast has one unambiguous compute dtype to restore to. Wiring alone renders a heavily dithered image with no fine detail. The cause is t_embedder: it produces the adaln_lora conditioning consumed by every block, so casting it to FP8 corrupts every token everywhere. None of the generic skip patterns match it — they target diffusers' module names (norm, pos_embed, patch_embed, proj_in/out) and this architecture names things differently. AnimaTransformer now declares _skip_layerwise_casting_patterns, the same attribute diffusers models use, so the loader needs no special-casing. Measured on CUDA, same seed/steps/CFG each run: casting nothing = broken at 1994MB; t_embedder alone = clean at 2010MB; adding x_embedder and final_layer changes nothing further (2012MB) and is kept as margin on the I/O layers; adaln_modulation was tested too and is deliberately not listed — it costs 168MB and made no difference. Against a bf16 reference (3988MB) the FP8 result keeps the same composition and loses only a little micro-detail.
Every quantized-format loader reaches _apply_fp8_layerwise_casting, and the cast
there is not a no-op. Verified on real layers:
- GGUF raises "Operation changed the dtype of GGMLTensor unexpectedly" at load.
- bnb NF4 corrupts silently: bnb.nn.LinearNF4 subclasses nn.Linear, so the
isinstance check passes and the packed uint8 payload is cast to float8.
Inference still returns finite numbers and the model just produces garbage
(max abs deviation 50.4 against a reference forward pass).
Both are reachable today by enabling the fp8_storage toggle, which the UI offered
for these models.
Guard on two levels, because a format check alone is not enough — an externally
quantized checkpoint can carry a plain `diffusers` format (e.g. SDNQ):
- _should_use_fp8 rejects gguf_quantized and both bnb formats.
- _apply_fp8_to_nn_module skips any module whose params are non-floating-point
or a torch.Tensor subclass, regardless of the model's declared format.
Frontend hides the toggle for quantized formats, so the control is not shown for
something the backend refuses.
Verified end to end: with fp8_storage forced true in the DB (the legacy case the
UI no longer offers), a GGUF Z-Image model now loads cleanly with no FP8 casting
and no GGMLTensor error, while non-quantized models still show the toggle and
still get cast.
Pfannkuchensack
requested review from
JPPhoto,
blessedcoolant,
dunkeroni and
lstein
as code owners
July 31, 2026 08:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enabling the
fp8_storagetoggle on an already-quantized model breaks it. Every quantized-format loader reaches_apply_fp8_layerwise_casting— FLUX/FLUX2/Krea-2/Qwen-Image/WAN/Z-Image GGUF, FLUX bnb-NF4, and the T5/Qwen3/Gemma2 encoders — and the cast there is not a no-op. Verified on real layers:ValueError: Operation changed the dtype of GGMLTensor unexpectedlyat load time.bnb.nn.LinearNF4subclassesnn.Linear, so theisinstancecheck passes and the packed uint8 payload is cast to float8. Inference still returns finite numbers — the model just produces garbage.Reproduce the silent one directly (no model download needed):
Guarded on two levels, because a format check alone is not enough — externally quantized weights can ship under a plain
diffusersformat (e.g. an SDNQ-quantized Z-Image):_should_use_fp8rejectsgguf_quantized,bnb_quantized_nf4bandbnb_quantized_int8b._apply_fp8_to_nn_moduleskips any module whose params are non-floating-point (bnb's packed uint8) or atorch.Tensorsubclass (GGUF'sGGMLTensor), regardless of declared format. This also covers the case where a model already has a persistedfp8_storage=truefrom before this PR.Frontend hides the toggle for quantized formats, so the UI stops offering a control the backend refuses.
Related Issues / Discussions
Follow-up to #8945 (FP8 storage). Same series as the Z-Image and Anima FP8 PRs.
QA Instructions
Needs a CUDA GPU.
Reproduce the bug on
main(both cases needfp8_storageenabled in the model's Default Settings):Operation changed the dtype of GGMLTensor unexpectedly.Verify the fix:
UI — open a GGUF or bnb model in Model Manager → Default Settings. The FP8 Storage switch is gone. Open a non-quantized model (SDXL, FLUX checkpoint, Z-Image diffusers) → the switch is still there and still works.
Legacy DB value — the backend must not rely on the UI. Force the old value back in:
Then generate with that model. Expect: model loads at its normal size, no
FP8 layerwise casting enabledline in the log, noGGMLTensorerror.No regression on non-quantized models — a model with FP8 enabled must still log
FP8 layerwise casting enabled ...and load at roughly half its usual VRAM.Multiple dispatch failed for 'torch._ops.aten.where.self'(torch.where(mask, pad_token, feats_cat)with aGGMLTensorpad token). That is a pre-existing, unrelated GGUF/Z-Image incompatibility — it reproduces identically withfp8_storageoff, and onmain. Use a GGUF FLUX model if you want a run that completes.Unit tests:
433 passed, 129 skipped locally. The new tests cover the format check (parametrized over all three quantized formats) and the param-level guard (both signals: non-floating-point payload and Tensor subclass), plus a control assertion that an ordinary layer in the same model is still cast.
Merge Plan
Merge after the Anima PR to avoid conflicts in
load_default.py,MainModelDefaultSettings.tsxandtest_load_default_fp8.py. If the earlier PRs in the series are dropped, this one rebases ontomaincleanly on its own — it has no logical dependency on them.No DB schema, no redux slice, no API schema change. Existing
fp8_storage=truevalues persisted against quantized models stay in the DB and are simply ignored from now on; no migration needed.Checklist
What's Newcopy (if doing a release after this PR) — n/a