Fix: Resolve missing 'lm_head.weight' during Qwen3 FP8 text encoder loading#1904
Fix: Resolve missing 'lm_head.weight' during Qwen3 FP8 text encoder loading#1904arifanchan wants to merge 2 commits into
Conversation
Added a preprocessing function to fix tied weights for Qwen FP8 models during loading.
There was a problem hiding this comment.
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_modelcall with a wrapper that injects missinglm_head.weight. - Introduces
fix_qwen_fp8_sdto patch the loaded state dict before model instantiation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Added a function to restore lm_head.weight from model.embed_tokens.weight for Qwen FP8 models.
arifanchan
left a comment
There was a problem hiding this comment.
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!
This PR fixes a crash that occurs in the queue worker when attempting to load Qwen3 FP8 text encoder safetensor checkpoints alongside the Z-Image Turbo 6B model.
Root Cause
Many quantized FP8 checkpoints drop the
lm_head.weightkey to save disk space because it is tied to and mathematically identical tomodel.embed_tokens.weight. Whenz_image_main.pycallsoffload.fast_load_transformers_model, the underlying memory manager (mmgp/offload.py) performs a strict key check and raises an exception:Exception: Missing keys: ['lm_head.weight'].Solution
Rather than modifying the core
mmgp/offload.pylibrary to ignore missing keys (which could hide legitimate loading errors), this PR resolves the issue locally insidez_image_main.py:fix_qwen_fp8_sdhelper function.preprocess_sdcallback infast_load_transformers_model.model.embed_tokens.weightinto thelm_head.weightslot before the strict key check occurs.Changes Made
Updated
models/z_image/z_image_main.pyto inject the missing tied embedding weights on the fly during text encoder initialization.