GeezerVison is showing github.com — fix: avoid f16 overflow in Z-Image quantized matmuls on CUDA by losewayy · Pull Request #2074 · leejet/stable-diffusion.cpp
Conversation
Summary
Fixes white image generation for Z-Image base models with k-quant diffusion weights on CUDA.
The attention output projection already gets scale = 1/16 on ROCm (extended in #1771) to keep the activation of that matmul inside f16 range. That mitigation is gated on the backend name, but the trigger is the quantized matmul itself: with a quantized weight the kernel converts the activation to a low precision internally, and a large enough activation overflows there regardless of the backend. On CUDA the force_prec_f32 override that the ROCm branch sets does not prevent it (measured: forcing fp32 alone does not recover the image, scaling the activation alone does).
This change applies the scale whenever the attention output weight is quantized, and leaves the ROCm branch untouched:
Linear::has_quantized_weight() reports whether the weight is a ggml quantized type.
ZImage::JointAttention sets scale = 1/16 on the attention output projection for quantized weights outside the ROCm path.
Verification
Local, Z-Image, 512x512, 20 steps, same seed, CUDA (RTX 5070 Ti, 12 GB):
| model |
before |
after |
z-image-Q4_K_M.gguf (base) |
pure white (std 0.00) |
normal image (std ~57-65) |
z_image_turbo-Q4_K.gguf |
normal |
normal |
z-image-BF16.gguf |
normal |
bit-identical to before |
- Before/after was measured on the same build (patch stashed and rebuilt), so the only variable is this change.
--preview proj shows the latent is already broken at the first step, so it is not a VAE decoding issue.
- Per-layer bisection: scaling only the attention output projection recovers the image; scaling the embedders, attention qkv, feed_forward, adaLN or final_layer alone does not.
- On CPU the two builds produce byte-identical output (quantized model, 256x256, 2 steps, all modules on cpu): the CPU matmul does not have the f16 activation quantization this overflow comes from, and 1/16 is a power of two, so the rescale cancels exactly.
- Performance (interleaved A/B, same revision, quantized, 8 steps): 3.12s vs 3.13s per sampling run, no measurable difference.
set_scale is an identity rescale in exact arithmetic (x*scale before the matmul and x/scale after it, see ggml_ext_linear), so it only shifts intermediate magnitudes. Quantized configs on CUDA do change numerically (a working quantized config moved by ~37 dB PSNR); BF16 is bit-identical because the new branch is not taken.
Known limitations
- This is a mitigation, not a root-cause fix: the overflow is inside ggml's CUDA quantized matmul, and CONTRIBUTING keeps ggml changes with the maintainer. It makes the configuration above work; it does not guarantee that no activation can ever overflow.
- It covers one matmul (the attention output projection). The other quantized matmuls in the same block are still unscaled.
- Legacy quant types are not fixed: with
z-image-Q4_0.gguf and z-image-Q5_0.gguf the output is still pure white after this change. On the unpatched build, --linear-scale 1/256 does not help either while 1/4096 recovers the image, and no single layer group is sufficient on its own, so those files need a much larger and broader reduction - a quality/perf trade-off that is yours to decide rather than something to fold in here.
Additional Information
- Same kind of mitigation as fix: avoid f16 overflow in Z-Image quantized matmuls on ROCm #1771 ("avoid f16 overflow in Z-Image quantized matmuls on ROCm") and Fix qwen-image NaN (black image) #1249, applied through the weight type instead of a backend name.
- The same "Vulkan/ROCm only" pattern appears in
qwen_image.hpp, qwen_image_2_1.hpp, krea2.hpp, boogu.hpp and block.hpp. Some of those sites only force fp32 without the activation scale, and on CUDA we measured that the precision override alone is not enough, so they may be under-mitigated in the same way. This is a suspicion only - I have no way to validate those paths locally, which is why this change stays limited to the Z-Image path I could reproduce.
- The
split_qkv branch of the same class (used by Ming-Image, not by Z-Image itself) is deliberately left untouched for the same reason.
- Not reproduced on Vulkan on this machine (it renders fine before the change), so this does not claim [BUG] ZImage + VULKAN create a blank image #1031; [Bug] Qwen-Image-2512 produces black images with Q4_K_M and other k-quant types (activation dequantization overflow) #1385 is a Qwen-Image/ROCm report and is not claimed either.
- fp8 and INT8 weights are excluded from
has_quantized_weight() on purpose: they take separate paths, and int8 convrot requires scale == 1.
- Passing
--linear-scale overrides the per-layer scale and therefore also overrides this mitigation (existing behavior).
Related Issue
Related to #1488 and #1253 (Z-Image base with quantized weights). I could not test the reporters' GPUs; the failure reproduced and fixed here is Z-Image base + a k-quant GGUF (Q4_K_M) on CUDA, so please retest on the affected machine.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|