Description
This PR resolves two separate crashing issues that occur when loading 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 Traceback: Exception: Missing keys: ['lm_head.weight']
- Root Cause: Many quantized FP8 checkpoints intentionally drop the
lm_head.weightkey to save disk space, as it is tied tomodel.embed_tokens.weight. However, whenfast_load_transformers_modelis called,mmgp/offload.pyperforms a strict key check and crashes. - Solution: Implemented a
unified_preprocessor(with a_fix_qwen_fp8_sdmodule-level helper) that intercepts the state dictionary as it loads. It dynamically aliases themodel.embed_tokens.weightmemory reference into thelm_head.weightslot before the strict check occurs, bypassing the error without wasting VRAM on duplicated tensors.
Issue 2: SDPA Dtype Mismatch in GGUF
Error Traceback: RuntimeError: Expected query, key, and value to have the same dtype, but got query.dtype: float key.dtype: float and value.dtype: struct c10::BFloat16 instead.
- Root Cause: When using GGUF text encoders (e.g.,
Q4_K_M), the underlying integration provides theValuetensor inbfloat16, while Hugging Facetransformerscomputes the RoPE forQueryandKeyinfloat32. PyTorch's native SDPA strictly requires all three to share the exact same data type, causing an immediate crash. - Solution: Forced the text encoder to a uniform precision state directly after loading by calling
text_encoder.to(dtype). This aligns all attention projections, allowing HF's native SDPA to run seamlessly without needing to alter the core attention backend.
Changes Made in models/z_image/z_image_main.py
- Injected a state dictionary preprocessor to handle missing tied embedding weights on the fly.
- Added a
.to(dtype)cast immediately afteroffload.fast_load_transformers_modelcompletes to enforce strict dtype uniformity.
Fix: resolve Qwen3 text encoder loading issues for FP8 and GGUF formats
#1904
This PR resolves two separate crashing issues that occur when loading the Qwen3 text encoder in different quantized formats (FP8 Safetensors and GGUF) alongside the Z-Image model.
Error Traceback: Exception: Missing keys: ['lm_head.weight']
-
Root Cause: Many quantized FP8 checkpoints intentionally drop the
lm_head.weightkey to save disk space, as it is tied tomodel.embed_tokens.weight. However, whenfast_load_transformers_modelis called,mmgp/offload.pyperforms a strict key check and crashes. -
Solution: Implemented a
unified_preprocessor(with a_fix_qwen_fp8_sdmodule-level helper) that intercepts the state dictionary as it loads. It dynamically aliases themodel.embed_tokens.weightmemory reference into thelm_head.weightslot before the strict check occurs, bypassing the error without wasting VRAM on duplicated tensors.
Error Traceback: RuntimeError: Expected query, key, and value to have the same dtype, but got query.dtype: float key.dtype: float and value.dtype: struct c10::BFloat16 instead.
-
Root Cause: When using GGUF text encoders (e.g.,
Q4_K_M), the underlying integration provides theValuetensor inbfloat16, while Hugging Facetransformerscomputes the RoPE forQueryandKeyinfloat32. PyTorch's native SDPA strictly requires all three to share the exact same data type, causing an immediate crash. -
Solution: Forced the text encoder to a uniform precision state directly after loading by calling
text_encoder.to(dtype). This aligns all attention projections, allowing HF's native SDPA to run seamlessly without needing to alter the core attention backend.
- Injected a state dictionary preprocessor to handle missing tied embedding weights on the fly.
- Added a
.to(dtype)cast immediately afteroffload.fast_load_transformers_modelcompletes to enforce strict dtype uniformity.
Top comments (0)