From 20def35183bc51719cbb03f7cef95d2ed1a665c4 Mon Sep 17 00:00:00 2001 From: Piotr Wilkin Date: Fri, 10 Jul 2026 15:37:20 +0200 Subject: [PATCH] fix: avoid f16 overflow in Z-Image quantized matmuls on ROCm Z-Image DiT activations exceed the f16 range (up to ~1e6) in the attention qkv/out and feed_forward w2 projections. On HIP for RDNA3.x, ggml routes large-batch quantized matmuls to the hipBLAS path, which converts activations to f16, saturating them to inf; RMS_NORM then turns the inf into NaN and the decoded image comes out blank. Force f32 precision on those three projections so they take the f32 GEMM path, matching the existing Vulkan handling of w2. Co-Authored-By: Claude Fable 5 --- src/model/diffusion/z_image.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/model/diffusion/z_image.hpp b/src/model/diffusion/z_image.hpp index 362192f19..9fb333cbb 100644 --- a/src/model/diffusion/z_image.hpp +++ b/src/model/diffusion/z_image.hpp @@ -150,6 +150,8 @@ namespace ZImage { if (sd_backend_is(ctx->backend, "ROCm")) { out_proj->set_scale(1.f / 16.f); + out_proj->set_force_prec_f32(true); + qkv_proj->set_force_prec_f32(true); } auto qkv = qkv_proj->forward(ctx, x); // [N, n_token, (num_heads + num_kv_heads*2)*head_dim] @@ -227,7 +229,7 @@ namespace ZImage { auto w2 = std::dynamic_pointer_cast(blocks["w2"]); auto w3 = std::dynamic_pointer_cast(blocks["w3"]); - if (sd_backend_is(ctx->backend, "Vulkan")) { + if (sd_backend_is(ctx->backend, "Vulkan") || sd_backend_is(ctx->backend, "ROCm")) { w2->set_force_prec_f32(true); }