fix(gguf): honor ComfyUI's comfy.gguf.orig_shape metadata - #9564
Open
Pfannkuchensack wants to merge 2 commits into
Open
fix(gguf): honor ComfyUI's comfy.gguf.orig_shape metadata#9564Pfannkuchensack wants to merge 2 commits into
Pfannkuchensack wants to merge 2 commits into
Conversation
ComfyUI's GGUF converter can only quantize 2-D tensors, so it reshapes any
tensor the quantizer rejects and records the native shape under a
`comfy.gguf.orig_shape.<tensor name>` KV entry. `gguf_sd_loader` ignored those
entries and used the stored shape, so such a checkpoint failed at load with a
size mismatch.
Concretely, Krea-2's `first.weight` is (6144, 64) but is stored as (1536, 256),
which produced:
size mismatch for img_in.weight: copying a param with shape
torch.Size([1536, 256]) from checkpoint, the shape in current model is
torch.Size([6144, 64])
The loader now reads the metadata and uses the declared native shape, rejecting
an entry whose element count doesn't match the stored tensor and warning on a
malformed one. This is architecture-agnostic, not a Krea-2 special case.
Verified end-to-end: the affected checkpoint from the issue installs, loads and
generates a coherent image.
Closes invoke-ai#9537
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Pfannkuchensack
requested review from
JPPhoto,
blessedcoolant,
dunkeroni and
lstein
as code owners
August 31, 2026 21:36
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
Kind of change: fix (backend, GGUF model loading).
Why: Certain ComfyUI-produced GGUF checkpoints install fine but fail at generation with a weight shape mismatch, e.g. for a Krea-2 Q4_K_M checkpoint:
ComfyUI's GGUF converter can only quantize 2-D tensors, so it reshapes any tensor whose native shape the quantizer rejects and records the native shape in a
comfy.gguf.orig_shape.<tensor name>KV entry.gguf_sd_loaderignored those entries and used the stored shape instead, soload_state_dictsaw a wrongly-shaped tensor.The affected file's header shows exactly this — note that both shapes have the same element count, so it is purely a reshape:
This is not Krea-2 specific; other Krea-2 GGUFs load only because their conversion did not have to reshape this tensor.
How:
gguf_sd_loadernow reads thecomfy.gguf.orig_shape.*metadata and uses the declared native shape for theGGMLTensor. An entry whose element count does not match the stored tensor raises with an explicit message rather than producing a silently wrong view; a malformed entry is logged and ignored.Related Issues / Discussions
Closes #9537
QA Instructions
Unit tests:
They cover all three paths: metadata present (shape is applied), metadata absent (unchanged behaviour), metadata inconsistent (raises).
Manual, with the checkpoint from #9537 (Krea-2 SAT-IOR v2 Q4_K_M GGUF from CivitAI):
main / krea-2 / gguf_quantized.load_state_dictreports no missing or unexpected keys, and generation completes with a coherent image (verified at 512x512, 4 steps).Also worth a quick regression check that an unaffected GGUF (e.g. an existing FLUX or Krea-2 GGUF without
comfy.gguf.orig_shapekeys) still loads — those files take the unchanged code path.Merge Plan
Nothing special — self-contained backend change, no schema or frontend impact.
Checklist
What's Newcopy (if doing a release after this PR)