Skip to content

Fix: resolve Qwen3 text encoder loading issues for FP8 and GGUF formats - #1904

Open
arifanchan wants to merge 8 commits into
deepbeepmeep:mainfrom
arifanchan:patch-1
Open

arifanchan wants to merge 8 commits into
deepbeepmeep:mainfrom
arifanchan:patch-1

Conversation

@arifanchan

@arifanchan arifanchan commented Jun 13, 2026

Copy link
Copy Markdown

Description

This PR resolves three sequential crashing issues that occur when loading and compiling the Qwen3 text encoder in different quantized formats (FP8 Safetensors and GGUF) alongside the Z-Image model.

Issue 1: Missing lm_head.weight in FP8 Safetensors

Error: Exception: Missing keys: ['lm_head.weight']

  • Root Cause: FP8 checkpoints intentionally drop the lm_head.weight key to save disk space, causing mmgp/offload.py strict key checks to fail.
  • Solution: Added _fix_qwen_fp8_sd preprocessor in z_image_main.py to dynamically alias the model.embed_tokens.weight memory reference into the lm_head.weight slot on the fly.

Issue 2: SDPA Dtype Mismatch in GGUF

Error: RuntimeError: Expected query, key, and value to have the same dtype

  • Root Cause: llama.cpp integration often provides the Value tensor in bfloat16, while HF transformers computes RoPE for Query/Key in float32. PyTorch SDPA strictly requires uniformity.
  • Solution: Added a .to(dtype) cast immediately after loading the text encoder in z_image_main.py to enforce strict dtype uniformity.

Issue 3: Dynamo RecursionError with Q8_0 GGUF

Error: torch._dynamo.exc.InternalTorchDynamoError: RecursionError: maximum recursion depth exceeded

  • Root Cause: The Q8_0 GGUF format lacks an embedding fast path, causing it to fall back to a custom Python __torch_function__ subclass wrapper. When these embeddings are passed to the torch.compile wrapped Z-Image transformer, Dynamo tries to trace the custom wrapper and enters an infinite loop.
  • Solution: Added an .as_subclass(torch.Tensor) cast to the embeddings in models/z_image/pipeline_z_image.py to strip the GGUF wrapper before they enter the compiled transformer. This has zero VRAM/performance penalty and acts as a safe bridge to eager mode.

Added a preprocessing function to fix tied weights for Qwen FP8 models during loading.
Copilot AI review requested due to automatic review settings June 13, 2026 14:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a state-dict preprocessing hook when loading the Qwen3 text encoder to handle FP8 checkpoints that omit lm_head.weight.

Changes:

  • Replaces direct fast_load_transformers_model call with a wrapper that injects missing lm_head.weight.
  • Introduces fix_qwen_fp8_sd to patch the loaded state dict before model instantiation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread models/z_image/z_image_main.py Outdated
Comment thread models/z_image/z_image_main.py Outdated
Added a function to restore lm_head.weight from model.embed_tokens.weight for Qwen FP8 models.

@arifanchan arifanchan left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the semantics. We specifically want the tensor alias here rather than a .clone() to preserve the tied weights and avoid re-allocating the vocab matrix in VRAM. Updated the comment to clarify.

Good call on keeping the scope clean. Even though the loading script only runs once per model initialization, moving it to the module level as a private helper _fix_qwen_fp8_sd is tidier. Updated!

arifanchan and others added 2 commits July 26, 2026 09:11
Added dtype conversion for text encoder to resolve SDPA mismatch.
@arifanchan arifanchan changed the title Fix: Resolve missing 'lm_head.weight' during Qwen3 FP8 text encoder loading Fix: resolve Qwen3 text encoder loading issues for FP8 and GGUF formats Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants