Skip to content

fix(studio): dequant 1-D GGUF norms on no-conversion load path - #12449

Closed
Souravrajvi0 wants to merge 6 commits into
unslothai:mainfrom
Souravrajvi0:feat/qwen-gguf-1d-norm-dequant-8da5
Closed

Souravrajvi0 wants to merge 6 commits into
unslothai:mainfrom
Souravrajvi0:feat/qwen-gguf-1d-norm-dequant-8da5

Conversation

@Souravrajvi0

Copy link
Copy Markdown
Contributor

Fixes #12445

Pre-converted Qwen-Image-2.1 GGUF repos (e.g. abenzerps/Qwen-Image-2.1-Uncensored-GGUF) already use diffusers tensor names, so _qwen_image_21_checkpoint_to_diffusers never runs and packed 1-D norm weights reach inference unchanged. diffusers only dequantizes inside GGUFLinear, which breaks on the first norm (4096 vs 8192).

This PR runs the same 1-D dequant step in the existing GGUF meta-loader shim (_install_gguf_dim_restore), alongside trimmed-dimension restore, and reuses the helper from the Qwen mapping path.

Test plan

  • Added unit test for _dequantize_gguf_one_dimensional (BF16-packed 1-D norm)
  • Studio: load abenzerps/Qwen-Image-2.1-Uncensored-GGUF and generate (manual)

Pre-converted Qwen-Image-2.1 GGUFs skip checkpoint mapping but still ship
packed norm weights; diffusers only dequantizes inside GGUFLinear layers.
Run the same 1-D dequant step in the GGUF meta-loader shim used for every load.

Copy link
Copy Markdown
Member

I checked this using norm weights from both reported Q8_0 and Q6_K files, the base loader reproduces the exact 4096 vs 8192 error and this fix resolves it
a reduced transformer also passes through from_single_file with conversion skipped, outputs match the decoded reference and 2-D weights stay quantized
one small test fix before merging, the random BF16 bytes can contain NaNs and make torch.equal fail even when the output is correct, reproducible with torch.manual_seed(13), a known finite BF16 vector would avoid that

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed that pre-converted Qwen-Image-2.1 GGUFs skip the mapping function in diffusion.py, so their packed 1-D norms are never dequantized. Moving that step into the meta-loader shim covers it, and I will get this reviewed.

@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@Imagineer99 Thanks for validating on Q8_0/Q6_K and for catching the flaky test — good call on torch.manual_seed(13) / random BF16 NaNs.

Pushed a follow-up commit that builds the packed norm from a known finite torch.linspace(..., dtype=bfloat16) vector instead of torch.randint, so torch.equal is stable. Happy to adjust anything else before merge.

@Souravrajvi0

Copy link
Copy Markdown
Contributor Author

@danielhanchen @Imagineer99 Quick status update:

  • Addressed the flaky unit test (finite bfloat16 linspace instead of random bytes) in the latest commit.
  • Merged current main into the branch so the PR is up to date.
  • pre-commit.ci is green on the latest SHA; full CI is still running on the merge commit.

From my side this is ready for review/merge whenever you have a moment. Thanks again for the thorough validation on Q8_0/Q6_K.

@LeoBorcherding

LeoBorcherding commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

tested at bd57691 on Windows 11 (CPU, the step runs before device placement). a BF16-packed 1-D norm sent through the patched meta loader arrives still packed at base (uint8, twice the length) and as the right-length float32 at head, equal to the source. on the GGUFs that already worked (z-image-turbo Q4_1, flux-2-klein-4b Q4_K_M) the new step changes 0 tensors and costs 0.1 ms. didn't run the reporter's 20B model. details: LeoBorcherding#98 (comment)

read the diff too and it looks right to me. it moves the existing 1-D dequant out of _qwen_image_21_checkpoint_to_diffusers into a helper unchanged and calls it in _restoring_loader, which every GGUF load goes through, so pre-converted GGUFs that skip the mapping fn get it now. anything without a quant_shape comes back as the same object, so non-GGUF loads and tensors the mapping fn already unpacked aren't touched again. the test builds a real BF16 GGUFParameter and checks against diffusers' own dequantize_gguf_tensor.

one small nit, not a blocker: the new call shares the try/except: pass with _restore_gguf_trimmed_dims, so if dequant ever raised it would also skip the dims fix and fail later as a shape mismatch. giving it its own try keeps the two independent.

@iMonZ

iMonZ commented Oct 3, 2026

Copy link
Copy Markdown

This actually resolves the issue for me.

Exercise the installed loader wrapper instead of the helper alone, so removing the wiring fails the test. Covers a BF16 and a Q8_0 vector, a packed matrix and a plain tensor that must pass through untouched, and a packed vector that also needs its leading axis restored. Simplify the state-dict loop and tighten the docstrings.
@danielhanchen danielhanchen self-assigned this Oct 8, 2026
@Lyxot

Lyxot commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Pushed to this branch:

  • Merged latest main (the branch was 559 commits behind; no conflicts).
  • Replaced the unit test with one that goes through the installed loader wrapper. The earlier test called the helper directly, so it still passed with the wiring line in _install_gguf_dim_restore removed. The new one covers a BF16 and a Q8_0 vector, a packed matrix and a plain tensor that must pass through untouched, and a packed vector that also needs its leading axis restored.
  • Simplified the state-dict loop and tightened the docstrings. No change to the fix itself.

I also read the GGUF headers of the Q8_0, Q6_K and BF16 files from the repo in #12445: 297 tensors in diffusers names, 65 of them 1-D BF16, which matches the diagnosis. FLUX.1, FLUX.2-klein, Z-Image, Qwen-Image, Wan2.2 and SD3.5 GGUFs have no packed 1-D tensors, so the new step does nothing for them.

@Lyxot

Lyxot commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

Follow-up after re-checking against current main: this PR is no longer needed for #12445.

#12775 (in v0.1.904-beta) added _dequantize_gguf_outside_linears, which runs after every GGUF from_single_file load in the image path and dequantizes every packed parameter outside GGUFLinear. That already covers the 65 BF16 norms in the pre-converted Qwen-Image-2.1 files, whether or not checkpoint conversion ran.

I confirmed it with a tiny QwenImage21Transformer2DModel written to a GGUF in diffusers names (2-D Q8_0, 1-D BF16) and loaded through the real from_single_file: with this PR's loader step switched off and only the main post-load step applied, all packed 1-D params are dequantized and the txt_in forward passes with values equal to the source. This matches the maintainer's check on the real Q6_K file in the issue.

What this PR would still add on top of main is a packed bias inside a GGUFLinear, and a packed 1-D tensor that also needs its leading axis restored. Neither appears in any GGUF I checked (Qwen-Image 2.1 / v1 / Edit, FLUX.1, FLUX.2-klein, Z-Image, Wan2.2, SD3.5), so it would be a second dequant path for the same job.

My earlier comment here was written before I re-checked against the merged main; the commits I pushed (merge of main, test through the loader wrapper) are harmless but do not change this conclusion. Codex found no issues on the current head, and the CI failures seen on it (test_npu_gradient_checkpointing_initializes in unsloth_zoo, seven tests in test_hub_token_caller_identity.py) also fail without this change.

Thanks @Souravrajvi0 for the fix and the quick follow-ups. The diagnosis was right; the same step just landed through another PR first.

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Thanks for this, @Souravrajvi0, the fix was correct. Since then #12775 (shipped in v0.1.904-beta) added a post-load step that dequantizes every packed GGUF parameter outside GGUFLinear, on both the converted and the pre-converted paths. That already covers these BF16 norms, so this loader step would duplicate it. Lyxot confirmed it above with a real from_single_file load, and #12445 is closed. Closing as superseded by #12775.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Unsloth Bug] Studio: Qwen-Image-2.1 GGUF generation fails - 1-D norm weights never dequantized in no-conversion load path (4096 vs 8192)

6 participants