Repository navigation
refactor(model-manager): own the quantized single-file load sequence in one module - #10443
Conversation
…leCheckpoint `recover` takes nvfp4 payloads, int8 markers and fp8 scales out of a state dict in the order they need, with one key-conversion point after the nvfp4 pop. `install` reserves once before anything widens, folds or keeps, casts, installs packed layers, loads, clears the dict and attaches the kept scales.
…eCheckpoint FLUX.2 converts keys at the shared conversion point for both schemes and reserves once, after the model is built, instead of once before the fold and once after. The loaded modules are unchanged.
The native key conversion carries the side channel across the rename for both schemes, so int8 and int4 markers are read under diffusers names like the fp8 scales. A native int8 layer named per tensor or only in the header is tested.
…it decodes `SingleFileCheckpoint.recover` takes `int8` and `scaled_fp8` beside `accept_int4` and `nvfp4`. Without `int8` no marker is read and every int8 weight is refused; without `scaled_fp8` a file carrying a quantization side channel is refused before any room is reserved.
…gleFileCheckpoint The encoder's key remap runs at the shared conversion point, so its int8 refusals name transformers keys. Loaded modules and reservations are unchanged.
…gleFileCheckpoint Loaded modules and reservations are unchanged. Anima gains a scaled fp8 load test, kept and folded, through a `net.`-prefixed header.
…h SingleFileCheckpoint The native conversion and the bundled-key filter run at the shared conversion point. The Qwen3 encoder drops its LM head before the side channels are read, so the head is no longer charged to the reservation.
A ModelOpt nvfp4 payload carries its layout and is re-laid out as each layer is installed, so the copies are still made after the reservation that counts them.
…gleFileCheckpoint The LTX-2 Gemma-4 encoder refuses a stray weight scale before reserving room instead of at the strict assign. Loaded modules and reservations are unchanged.
…Checkpoint Covers T5, CLIP, the Qwen3-VL encoder and vision tower, the Qwen3.5 encoder and the Qwen3.5 text LLM. T5 and CLIP refuse an int8 file, and CLIP any quantization side channel, before reserving room; T5's fold reservation now counts the recovered scales.
…d its format capabilities
Pfannkuchensack
left a comment
There was a problem hiding this comment.
I ran this against every quantized single file we have locally, comparing the merge base (ed418f3) with 899ad33. No blockers from my side.
Load diff. 64 files went through each side's registered loader on the CPU. For each one I hashed every module of the result (class, parameters with requires_grad, buffers with their persistence flag, tensor attributes such as kept scales) and recorded the sizes passed to make_room.
- Transformers:
- FLUX.1: int8, Krea fp8_scaled, Fill bf16
- FLUX.2: Klein 4B fp8/int8/bf16, 9B fp8, Dev fp8mixed
- FLUX.3: bf16, FP8, int8, nvfp4
- Krea-2: fp8_scaled ×2, int4 mixed, int8, nvfp4
- Qwen-Image: Edit-2511 fp8, 2512 int8, Edit-2511 int8
- Qwen-Image-2.1: FP8, int8, bf16
- Z-Image: fp8 ×2, int8 mixed, nvfp4
- Anima: 2.9B int8, base bf16/fp8, 3.8B
- Ideogram 4: fp8_scaled, int8
- MOSS-Image: FP8, int8, bf16
- LTX-2.5 22B: int8, nvfp4
- Encoders:
- LTX Gemma-4
- T5 ×3, CLIP ×2
- Qwen3 ×6, Qwen2.5-VL ×4 (nvfp4 included), Qwen3-VL ×6
- Qwen3.5 encoder ×2, Qwen3.5 as text LLM ×2
All 64 produce byte-identical modules. Two files differ only in their reservations, and both match the description:
| File | Before | This PR |
|---|---|---|
| FLUX.2 Klein base 4B | [7751089152, 7771536384] |
[7771536384] |
| Qwen3 8B int8 | 9441306624 |
8196646912 (the unread lm_head, 151936 × 4096 × bf16) |
Generation. FLUX.2 Klein 4B fp8 with its single-file Qwen3 4B encoder, 1024², 4 steps, seed 1234, on an RTX 4090 through the API (scripts/family_e2e, one warm-up and two measured runs per side). The images are pixel-identical. The VRAM peak is the same (20,488 MB). Median time is 2.20 s before and 2.34 s here, within the run-to-run spread I see between server starts.
The tests pass locally apart from the known LFS-pointer fixture (test_16_channel_vae_loader).
Low
-
An nvfp4 file in a family that doesn't decode nvfp4 isn't refused in
recover. The module docstring says a format the family does not decode "is refused here, before anything is reserved". Withnvfp4=False, scaled_fp8=True(Anima, FLUX.1 Krea/Fill, FLUX.2, Ideogram 4, Qwen-Image-2.1), the packed payload goes through:scaled_fp8drops its scale keys as stray side channel, the reservation is made, and the file fails inload_state_dictwith a size mismatch on the packed weight. Withscaled_fp8=False, the side-channel refusal catches it as intended. Checking for nvfp4 payloads whennvfp4=False(pop_nvfp4_layersalready knows how to find them) would make the docstring true and give a readable error. I haven't checked whether the old per-family loaders refused this earlier; either way it's the new contract that promises it. -
consumed_byis only exercised for Krea-2. FLUX.2 (flux.py:964) and Qwen3.5 (qwen3_5_encoder.py:135) pass it too, but dropping it there leaves their tests green. A file with an unmarked int8 tensor the model doesn't consume (the case that motivated the argument) would pin it per family. -
Loader-level refusals lost their tests. The Qwen-Image transformer and encoder tests for a header entry that doesn't say whether the weight was rotated are gone (
test_qwen_image_int8_convrot_loader.py,test_qwen_image_nvfp4_loader.py). The config-level refusal is still tested, butint8_in_header=Trueroutes the loader through a different path.test_z_image_fp8_wiring.pyalso losttest_state_dict_is_released_before_the_fp8_castandtest_assign_true_really_aliases_the_state_dict.test_single_file_checkpoint.pycovers the generic order, but not that Z-Image'sloadcallback still loads withassign=Trueinto the dictinstallclears. -
Behaviour changes without a test:
- the Krea-2
.scaleint8 norm - the FLUX.2 marker refusal and the stale header
- Qwen3's unmarked
lm_head - CLIP's stray-marker refusal
The load diff above confirms the real files behave the same, but nothing keeps them that way.
- the Krea-2
-
One refusal isn't in the description: a dense Gemma-4 file with a stray
.input_scaleor a non-int8.comfy_quantis now refused (ltx2.py:655,scaled_fp8=False). That's reasonable, it just deserves a line in the list.
Nits
- Some capability flags work as policy switches. LTX-2 sets
scaled_fp8=Trueonly to shed leftover side-channel keys (the comment atltx2.py:355says so). MOSS tiesscaled_fp8totorchao_fp8. T5 setsint8_in_headerwithint8=False. A separate "drop stray side-channel keys" flag would keepscaled_fp8meaning "decodes scaled fp8". - Some refusals still sit in the families' conversion or
loadhooks (LTX-2, the Krea-2 vision tower, Z-Image'stie_weights) rather than inrecover. Fine where they need the built model, but worth a sentence in the module docstring. install's docstring says one reservation covers everything "before anything in the dict is widened". MXFP8 block-scale grids are already decoded to float32 inrecover(decode_mx_block_scales, about 1.45 GiB on the Comfy Krea-2 MXFP8 build perload_plan.py). The reservation counts them, but they are resident before it is made.
Scope the NVFP4 check to transformer weights and ignore bundled FLUX components. Separate LTX-2 metadata cleanup from scaled FP8 decoding and cover loader edge cases.
Pfannkuchensack
left a comment
There was a problem hiding this comment.
Rechecked at 9c6f94f. I ran the same 64 single files through the loaders again. The result is unchanged from the previous head: 62 byte-identical to the merge base, and the same two reservation differences (FLUX.2 Klein base 4B, Qwen3 8B int8). That includes the FLUX.2 files with the bundled-component filter and the three LTX-2 files with drop_stray_fp8_sidecars.
The follow-ups look good to me:
recovernow refuses nvfp4 in a family that doesn't decode it, before the conversion and again on the converted keys, so the docstring holds. Turning the refusal off failstest_a_family_without_nvfp4_support_refuses_packed_layers_before_conversion.consumed_byis pinned per family. Dropping it fails the FLUX.1 test (unmarked_int8) and the Qwen3.5 test.- The loader-level refusals and the Z-Image
assign=True/ release tests are back. The new behaviour (Krea-2.scalenorm, FLUX.2 stale header and marker refusal, Qwen3lm_head, CLIP stray keys) now has tests. - LTX-2 no longer borrows
scaled_fp8to shed stray keys, and the MXFP8 timing is documented.
A correction to my previous comment: flux.py:964 is in the FLUX.1 loader, not FLUX.2. The point was about FLUX.1, and the new FLUX.1 test covers it.
One nit: reject_unsupported_nvfp4_layers runs before the int8/int4 markers are read and treats any uint8 .weight with a weight_scale as nvfp4. A packed int4 file in a family that decodes int8 but not int4 (FLUX.1, FLUX.2, …) is therefore refused with the nvfp4 message instead of the int4 one. It's still refused, only the text points at the wrong format.
Tests pass locally apart from the known LFS-pointer fixture (test_16_channel_vae_loader). No further findings from my side.
A uint8 weight with a block scale but no global scale whose marker or header entry names another format, such as int4_tensorwise, is now refused under that format's name instead of as nvfp4. A global scale still means nvfp4 whatever the marker says, and the set of refused files is unchanged.
Pfannkuchensack
left a comment
There was a problem hiding this comment.
Rechecked f5e7279 against 9c6f94f. The refusal now names the declared format: a packed layer with only a block scale is refused under its marker's or header's format, while a global scale or an nvfp4 declaration still means nvfp4. The set of refused layers is the same as before, only the message changes, so the load results from my last pass still hold. The new test pins the int4 message. CI is green. Approving.
Optional nit, not reachable today: recover runs this check for every family with nvfp4=False, so a family that later takes int4 without nvfp4 would have its int4 layers refused here before extract_int8_convrot_markers sees them. Krea-2, the only accept_int4=True loader, decodes nvfp4, so nothing hits this now.
The unsupported-nvfp4 refusal reads only uint8 weights, and packed int4 codes are int8, so a family that decodes int4 but not nvfp4 still recovers its int4 layers. The test keeps it that way.
Summary
Loading a quantized single-file checkpoint is an ordered sequence (recover nvfp4 payloads, int8 markers and fp8 scales; reserve RAM before anything widens; keep or fold; cast; install; load; attach scales). Every family loader assembled it by hand, and the order lived only in comments repeated per loader.
invokeai/backend/quantization/single_file_checkpoint.pynow owns it:SingleFileCheckpoint.recover(sd, ...)takes the side channels out in the order they need. It has one key-conversion point, right after the nvfp4 pop; everything after it reads the model's key space.checkpoint.install(model, dtype, ...)makes the one reservation, folds or keeps, casts, installs packed layers, calls the family'sload, clears the dict and attaches the kept scales.A family supplies only what is its own: reading the file, its key conversion, building the model, skip patterns, its
load_state_dictpolicy and its log lines. It states which formats it decodes (int8,accept_int4,nvfp4,scaled_fp8); there is no per-family parameter.Loaders now behind the module:
Not moved, on purpose: Wan (it streams payloads against header metadata), the GGUF paths (
unpack_ggml_at_loadalready owns their reservation), MiniMax H3 (it never casts), LTX-2's dense components (they cast before a model exists) and Mistral (its own fold has to be unified onto the shared one first, which changes behaviour).Loaded modules are byte-identical to
mainfor every moved path. Behaviour differences:load_state_dict, after the reservation)..scale(it used to refuse the file), and drops a marker on a module with no weight with an INFO line.lm_head(unmarked int8, unreadable scale) instead of refusing the file; the head is tied and never loaded..input_scale, or a non-int8.comfy_quantmarker before reserving room.Related Issues / Discussions
Part of #10302. What remains of it is Mistral, which needs its own fold replaced by the shared one first. The three
_storage_formatcopies named in the issue were already merged by #10171.Follow-up noted during review: MXFP8 scale grids are still decoded before the reservation, as on
main; the module could now fix that.QA Instructions
[8128056, 8390144] -> [8390200]; T5[23360] -> [23416]).pytest -n 6ontests/backend/model_manager/load,tests/backend/quantizationand the family directories (anima,flux,flux2,flux3,ideogram4,krea2,ltx2,moss_image,qwen2_5_vl,qwen3,qwen3_5,qwen3_vl,qwen_image_2_1,t5,text_llm,z_image): 3999 passed, 5 failed. The same 5 fail onmainon this machine:test_16_channel_vae_loader.py::test_an_ldm_layout_file_is_converted_and_loses_no_tensorand fourtest_qwen_vl_bnb_lora.pycases.tests/backend/quantization/test_single_file_checkpoint.py: format × keep/fold on small CPU tensors, the conversion contract, and each format capability. Mutating the module (reserve after the fold, drop a reservation term, flip hint precedence, pop nvfp4 after the conversion, skip the dict clear, keep side channels on the object, ignore a capability flag) fails it.ruff@0.11.2 checkandformat --checkon the changed files: clean.Review
Material findings resolved:
installkept every folded scale grid alive through the fold.installnow empties the dicts in place.main.int8as a capability, and T5 loads such a file as before.weight_scale_inv) and Krea-2's header-only native int8 route had lost their tests; both are restored. Loader-level tests were added where a scheme had none (Anima scaled fp8, Qwen-Image-2.1 int8, a dense Gemma-4 load).Remaining risks and limits:
Checklist
What's Newcopy (if doing a release after this PR)