From 3a0579b94ebddf21b6a945c0eaa0119646a507af Mon Sep 17 00:00:00 2001 From: sakasegawa Date: Sun, 29 Nov 2026 19:24:08 +0900 Subject: [PATCH 1/3] Keep the activations of every language model's FFN down projections in float32's range on every device Metal and Vulkan stage the activations of a matrix product in half precision, and the CPU those of an F16 matrix, and a value past 65504 becomes inf. The inputs of the FFN down projections of a transformer pretrained as a language model, or of one that predicts audio tokens one at a time, reach that range at some tokens: Drama-3-Japanese, a fine-tune of fishaudio/drama-3, reaches 86,682 in its experts' down projection and 66,927 in its shared expert's, so that its prompts gave logits that were not finite on Metal (issue 237), and Qwen3-TTS 0.6B's code predictor reaches 188,823. Measured on the CPU over each family's own inputs (2026-11-29), those inputs reach 585 to 188,823 in the language models and token decoders, and the FFN down projections of the audio encoders, codecs and DiTs stay below 240. mul_mat_f32_activations() and mul_mat_id_f32_activations() in src/common/graph.h ask for GGML_PREC_F32 on src1, as SEMamba's own helper did, which they replace. The FFN down projections of Qwen3.5's decoder (Drama-3's experts, shared expert and Haru's dense layers), Qwen3's (Qwen3-TTS's talker and code predictor, Qwen3-ASR's decoder), Drama-3's audio decoder, Irodori-TTS's text encoders and Nemotron 3.5 Transcribe's experts and shared expert take them. The ggml fork's commit makes every backend keep such activations' range for every matrix type. The add-model skill says which products take them. Checked: Drama-3-Japanese Q4_K's twelve cases of the issue, without a voice and with one, speak on Metal (Apple M5), where five gave logits that were not finite before; the fork's cases and every MUL_MAT and MUL_MAT_ID case pass on Metal with and without the tensor API and on Vulkan through MoltenVK. Co-Authored-By: Claude Opus 5.5 --- ggml | 2 +- skills/add-model/SKILL.md | 4 ++++ src/common/graph.h | 22 +++++++++++++++++++ src/common/qwen3-decoder.cpp | 2 +- src/common/qwen35-decoder.cpp | 8 +++---- src/families/drama3/audio-decoder.cpp | 2 +- src/families/irodori-tts/text-encoder.cpp | 4 ++-- .../nemotron-transcribe/language-model.cpp | 4 ++-- src/families/semamba/network.cpp | 20 +++-------------- 9 files changed, 40 insertions(+), 28 deletions(-) diff --git a/ggml b/ggml index 79b50d23..b23c4bfc 160000 --- a/ggml +++ b/ggml @@ -1 +1 @@ -Subproject commit 79b50d239d0b88852814c873db0d85d43ed66d9b +Subproject commit b23c4bfc7f242a172816f1fc1ad87cf7f36d9e03 diff --git a/skills/add-model/SKILL.md b/skills/add-model/SKILL.md index a9e83fee..4e64b99b 100644 --- a/skills/add-model/SKILL.md +++ b/skills/add-model/SKILL.md @@ -44,6 +44,10 @@ names keys, files and layouts. - The engine (`src/engines/-engine.cpp`) declares the options the model takes, with defaults and ranges read from the file. An option without effect, or a value the model cannot compute, is refused with the option named, never ignored. +- The FFN down projections of a transformer pretrained as a language model, or of one that predicts audio tokens one at + a time, multiply through `mul_mat_f32_activations()` (`mul_mat_id_f32_activations()` for experts): their inputs pass + half precision's 65504 at some tokens, in the official weights or in a fine-tune of them, and the GPUs' matrix kernels + stage activations in half precision otherwise. Any other product whose inputs reach that range does the same. ## 4. Checks diff --git a/src/common/graph.h b/src/common/graph.h index 7d51cd29..c67fdb6a 100644 --- a/src/common/graph.h +++ b/src/common/graph.h @@ -25,6 +25,28 @@ inline ggml_tensor * mul_mat_id(ggml_context * ctx, ggml_tensor * experts, ggml_ return t; } +/** + * mul_mat() of activations that keep float32's range on every device: Metal and Vulkan otherwise stage the activations + * of their matrix kernels in half precision, and the CPU those of an F16 matrix, which turns a value past 65504 into + * inf. The FFN down projections of a transformer pretrained as a language model, or one that predicts audio tokens one + * at a time, take it, since their inputs reach that range at some tokens: 19,103 in Drama-3's experts, 86,682 in + * Drama-3-Japanese's, 14,904 in Qwen3-ASR 1.7B's decoder and 188,823 in Qwen3-TTS 0.6B's code predictor, where the + * FFN down projections of the audio encoders, codecs and DiTs stay below 240 (CPU, 2026-11-29); as do the projections of + * RE-USE's Mamba blocks, whose inputs reach 7.07e4. + */ +inline ggml_tensor * mul_mat_f32_activations(ggml_context * ctx, ggml_tensor * a, ggml_tensor * b) { + ggml_tensor * t = mul_mat(ctx, a, b); + ggml_prec_set_src(t, GGML_PREC_F32, 1); + return t; +} + +/** mul_mat_id() of activations that keep float32's range on every device, as mul_mat_f32_activations() keeps them. */ +inline ggml_tensor * mul_mat_id_f32_activations(ggml_context * ctx, ggml_tensor * experts, ggml_tensor * b, ggml_tensor * ids) { + ggml_tensor * t = mul_mat_id(ctx, experts, b, ids); + ggml_prec_set_src(t, GGML_PREC_F32, 1); + return t; +} + /** * Computes `gf`, whose tensors have their memory, on `backend`. A backend that runs out of memory while it computes * throws an Error of Fault::OutOfMemory, and one that fails otherwise an Error of Fault::Device. diff --git a/src/common/qwen3-decoder.cpp b/src/common/qwen3-decoder.cpp index 833f56eb..edb7e117 100644 --- a/src/common/qwen3-decoder.cpp +++ b/src/common/qwen3-decoder.cpp @@ -407,7 +407,7 @@ ggml_tensor * Qwen3Decoder::layers(Graph & g, ggml_tensor * x, int64_t rows, boo ggml_tensor * up = ggml_mul_mat(ctx, m.tensor(b + "ffn_up"), h); g.expand(gate); g.expand(up); - x = ggml_add(ctx, x, ggml_mul_mat(ctx, m.tensor(b + "ffn_down"), ggml_swiglu_split(ctx, gate, up))); + x = ggml_add(ctx, x, mul_mat_f32_activations(ctx, m.tensor(b + "ffn_down"), ggml_swiglu_split(ctx, gate, up))); } return x; } diff --git a/src/common/qwen35-decoder.cpp b/src/common/qwen35-decoder.cpp index 6a5d684b..e735890a 100644 --- a/src/common/qwen35-decoder.cpp +++ b/src/common/qwen35-decoder.cpp @@ -203,15 +203,15 @@ ggml_tensor * Qwen35Decoder::experts(Graph & g, int l, ggml_tensor * x, ggml_ten ggml_tensor * rows = ggml_reshape_3d(c, x, h_, 1, n); ggml_tensor * gate = mul_mat_id(c, m_.tensor(p + "ffn_gate_exps"), rows, chosen); ggml_tensor * up = mul_mat_id(c, m_.tensor(p + "ffn_up_exps"), rows, chosen); - ggml_tensor * down = mul_mat_id(c, m_.tensor(p + "ffn_down_exps"), ggml_mul(c, ggml_silu(c, gate), up), chosen); + ggml_tensor * down = mul_mat_id_f32_activations(c, m_.tensor(p + "ffn_down_exps"), ggml_mul(c, ggml_silu(c, gate), up), chosen); down = ggml_mul(c, down, weights); ggml_tensor * sum = nullptr; for (int e = 0; e < used_; e++) { ggml_tensor * one = ggml_view_2d(c, down, h_, n, down->nb[2], (size_t) e * down->nb[1]); sum = sum ? ggml_add(c, sum, one) : one; } - ggml_tensor * shared = mul_mat(c, m_.tensor(p + "ffn_down_shexp"), - ggml_mul(c, ggml_silu(c, mul_mat(c, m_.tensor(p + "ffn_gate_shexp"), x)), mul_mat(c, m_.tensor(p + "ffn_up_shexp"), x))); + ggml_tensor * shared = mul_mat_f32_activations(c, m_.tensor(p + "ffn_down_shexp"), + ggml_mul(c, ggml_silu(c, mul_mat(c, m_.tensor(p + "ffn_gate_shexp"), x)), mul_mat(c, m_.tensor(p + "ffn_up_shexp"), x))); shared = ggml_mul(c, shared, ggml_sigmoid(c, mul_mat(c, m_.tensor(p + "ffn_gate_inp_shexp"), x))); return ggml_add(c, sum, shared); } @@ -219,7 +219,7 @@ ggml_tensor * Qwen35Decoder::experts(Graph & g, int l, ggml_tensor * x, ggml_ten ggml_tensor * Qwen35Decoder::dense(Graph & g, int l, ggml_tensor * x) { ggml_context * c = g.ctx(); const std::string p = layer_name(l); - return mul_mat(c, m_.tensor(p + "ffn_down"), ggml_mul(c, ggml_silu(c, mul_mat(c, m_.tensor(p + "ffn_gate"), x)), mul_mat(c, m_.tensor(p + "ffn_up"), x))); + return mul_mat_f32_activations(c, m_.tensor(p + "ffn_down"), ggml_mul(c, ggml_silu(c, mul_mat(c, m_.tensor(p + "ffn_gate"), x)), mul_mat(c, m_.tensor(p + "ffn_up"), x))); } std::vector Qwen35Decoder::run(const std::vector & input, int64_t n, Qwen35Stages * stages) { diff --git a/src/families/drama3/audio-decoder.cpp b/src/families/drama3/audio-decoder.cpp index b27dcc05..a4fa15eb 100644 --- a/src/families/drama3/audio-decoder.cpp +++ b/src/families/drama3/audio-decoder.cpp @@ -92,7 +92,7 @@ ggml_tensor * AudioDecoder::layer(Graph & g, int l, ggml_tensor * hidden, ggml_t h = norm(x, "ffn_norm"); h = ggml_mul(c, ggml_silu(c, mul_mat(c, m_.tensor(p + "ffn_gate"), h)), mul_mat(c, m_.tensor(p + "ffn_up"), h)); - x = ggml_add(c, x, mul_mat(c, m_.tensor(p + "ffn_down"), h)); + x = ggml_add(c, x, mul_mat_f32_activations(c, m_.tensor(p + "ffn_down"), h)); return norm(x, "final_norm"); } diff --git a/src/families/irodori-tts/text-encoder.cpp b/src/families/irodori-tts/text-encoder.cpp index 62cc6d16..c74aaf2c 100644 --- a/src/families/irodori-tts/text-encoder.cpp +++ b/src/families/irodori-tts/text-encoder.cpp @@ -96,7 +96,7 @@ ggml_tensor * TextEncoder::modernbert(Graph & g, const std::vector & id h = layer_norm(x, b + "ffn_norm"); ggml_tensor * act = ggml_gelu_erf(ctx, mul_mat(ctx, m_.tensor(b + "ffn_act"), h)); h = ggml_mul(ctx, act, mul_mat(ctx, m_.tensor(b + "ffn_gate"), h)); - x = ggml_add(ctx, x, mul_mat(ctx, m_.tensor(b + "ffn_down"), h)); + x = ggml_add(ctx, x, mul_mat_f32_activations(ctx, m_.tensor(b + "ffn_down"), h)); if (layers && l + 1 < layers_) layers->push_back(x); } x = layer_norm(x, "text.final_norm"); @@ -164,7 +164,7 @@ ggml_tensor * TextEncoder::t5gemma2(Graph & g, const std::vector & ids, h = rms_norm(x, b + "ffn_norm"); h = ggml_mul(ctx, gelu_tanh(mul_mat(ctx, m_.tensor(b + "ffn_gate"), h)), mul_mat(ctx, m_.tensor(b + "ffn_up"), h)); - x = ggml_add(ctx, x, rms_norm(mul_mat(ctx, m_.tensor(b + "ffn_down"), h), b + "ffn_post_norm")); + x = ggml_add(ctx, x, rms_norm(mul_mat_f32_activations(ctx, m_.tensor(b + "ffn_down"), h), b + "ffn_post_norm")); if (layers && l + 1 < layers_) layers->push_back(x); } x = rms_norm(x, "text.final_norm"); diff --git a/src/families/nemotron-transcribe/language-model.cpp b/src/families/nemotron-transcribe/language-model.cpp index f8b972ec..a326dbeb 100644 --- a/src/families/nemotron-transcribe/language-model.cpp +++ b/src/families/nemotron-transcribe/language-model.cpp @@ -201,13 +201,13 @@ ggml_tensor * LanguageModel::experts(Graph & g, int l, ggml_tensor * x, ggml_ten weights = ggml_scale(c, weights, routed_scale_); const auto relu2 = [&](ggml_tensor * t) { return ggml_sqr(c, ggml_relu(c, t)); }; ggml_tensor * up = mul_mat_id(c, m_.tensor(p + "ffn_up_exps.weight"), ggml_reshape_3d(c, x, h_, 1, n), chosen); - ggml_tensor * down = ggml_mul(c, mul_mat_id(c, m_.tensor(p + "ffn_down_exps.weight"), relu2(up), chosen), weights); + ggml_tensor * down = ggml_mul(c, mul_mat_id_f32_activations(c, m_.tensor(p + "ffn_down_exps.weight"), relu2(up), chosen), weights); ggml_tensor * sum = nullptr; for (int e = 0; e < used_; e++) { ggml_tensor * one = ggml_view_2d(c, down, h_, n, down->nb[2], (size_t) e * down->nb[1]); sum = sum ? ggml_add(c, sum, one) : one; } - ggml_tensor * shared = mul_mat(c, m_.tensor(p + "ffn_down_shexp.weight"), relu2(mul_mat(c, m_.tensor(p + "ffn_up_shexp.weight"), x))); + ggml_tensor * shared = mul_mat_f32_activations(c, m_.tensor(p + "ffn_down_shexp.weight"), relu2(mul_mat(c, m_.tensor(p + "ffn_up_shexp.weight"), x))); return ggml_add(c, sum, shared); } diff --git a/src/families/semamba/network.cpp b/src/families/semamba/network.cpp index eaaf64b9..a4df0540 100644 --- a/src/families/semamba/network.cpp +++ b/src/families/semamba/network.cpp @@ -6,20 +6,6 @@ namespace semamba { -namespace { - -/** - * A matrix product whose operands stay in float32 on every device: RE-USE's activations reach 7.07e4 before its output - * projection, past the 65504 of half precision, in which Metal and Vulkan otherwise stage the operands of an F32 matrix. - */ -ggml_tensor * mul_mat_f32(ggml_context * ctx, ggml_tensor * a, ggml_tensor * b) { - ggml_tensor * t = mul_mat(ctx, a, b); - ggml_prec_set_src(t, GGML_PREC_F32, 1); - return t; -} - -} // namespace - Network::Network(const ModelFile & m) : m_(m), hidden_((int) m.width("enc.in.conv.weight", 3)), @@ -70,17 +56,17 @@ ggml_tensor * Network::encode(ggml_context * ctx, ggml_tensor * input) const { } ggml_tensor * Network::mamba(ggml_context * ctx, const std::string & prefix, ggml_tensor * x, bool reverse) const { - ggml_tensor * xz = mul_mat_f32(ctx, w(prefix + ".in_proj.weight"), x); + ggml_tensor * xz = mul_mat_f32_activations(ctx, w(prefix + ".in_proj.weight"), x); ggml_tensor * a = ggml_neg(ctx, ggml_exp(ctx, w(prefix + ".A_log"))); ggml_tensor * y = ggml_mamba1(ctx, xz, w(prefix + ".conv1d.weight"), w(prefix + ".conv1d.bias"), w(prefix + ".x_proj.weight"), w(prefix + ".dt_proj.weight"), w(prefix + ".dt_proj.bias"), a, w(prefix + ".D"), reverse); - return mul_mat_f32(ctx, w(prefix + ".out_proj.weight"), y); + return mul_mat_f32_activations(ctx, w(prefix + ".out_proj.weight"), y); } ggml_tensor * Network::bidirectional(ggml_context * ctx, const std::string & prefix, ggml_tensor * x) const { ggml_tensor * forward = ggml_add(ctx, mamba(ctx, prefix + ".fw", x, false), x); ggml_tensor * backward = ggml_add(ctx, mamba(ctx, prefix + ".bw", x, true), x); - ggml_tensor * y = mul_mat_f32(ctx, w(prefix + ".proj.weight"), ggml_concat(ctx, forward, backward, 0)); + ggml_tensor * y = mul_mat_f32_activations(ctx, w(prefix + ".proj.weight"), ggml_concat(ctx, forward, backward, 0)); y = ggml_add(ctx, y, w(prefix + ".proj.bias")); return ggml_add(ctx, ggml_mul(ctx, ggml_norm(ctx, y, layer_eps_), w(prefix + ".norm.weight")), w(prefix + ".norm.bias")); } From c3696f8ea6a9ec27267f8c850884b69fbdb4b001 Mon Sep 17 00:00:00 2001 From: sakasegawa Date: Sun, 29 Nov 2026 20:48:34 +0900 Subject: [PATCH 2/3] Record why the FFN down projections of language models keep their activations in float32's range The new record states the rule, the measurements behind it and the designs turned down: asking only on the reported products, asking on every product, scaling a family's activations in speech.cpp, turning fp16 off on Vulkan, and choosing the products by measuring each model's weights. The ggml fork's record gets the defect, the decision for every matrix type and backend, its row in the table, and the two designs of the fix turned down. Co-Authored-By: Claude Opus 5.5 --- ...its-upstream-tag-and-measured-speed-ups.md | 30 ++++++-- ...tions-in-float32s-range-on-every-device.md | 71 +++++++++++++++++++ 2 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 docs/adr/the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md diff --git a/docs/adr/ggml-comes-from-a-fork-that-carries-tested-fixes-for-the-defects-of-its-upstream-tag-and-measured-speed-ups.md b/docs/adr/ggml-comes-from-a-fork-that-carries-tested-fixes-for-the-defects-of-its-upstream-tag-and-measured-speed-ups.md index 40871b1b..da0449ed 100644 --- a/docs/adr/ggml-comes-from-a-fork-that-carries-tested-fixes-for-the-defects-of-its-upstream-tag-and-measured-speed-ups.md +++ b/docs/adr/ggml-comes-from-a-fork-that-carries-tested-fixes-for-the-defects-of-its-upstream-tag-and-measured-speed-ups.md @@ -70,6 +70,12 @@ that fail on the tag on an Apple M5, an RTX 2080 or MoltenVK: became inf. NVIDIA NemotronLabs RE-USE's activations reach 7.07e4 before its output projection, so its network's output held NaN on Metal and lay at -4.9 to -21 dB from the official's on Vulkan through MoltenVK, where the CPU gave 109 dB (Apple M5, 2026-11-01), and its convolutions lay 51 to 61 dB from the official's on an RTX 2080. +- **Products of F16 and quantized matrices staged in half.** The same request left Metal's `kernel_mul_mm` staging the + F32 activations of an F16 or quantized matrix in half precision on both of its paths, and Vulkan's fp16, coopmat and + coopmat2 shaders likewise, and Vulkan, as CUDA, refused a MUL_MAT_ID that asks for it; only Metal's MUL_MAT_ID scaled + such activations, by a power of two. The CPU converted the activations of an F16 matrix to F16 and, on NEON with FP16 + arithmetic, summed the products in half precision. The inputs of a language model's FFN down projections pass 65504: + Drama-3-Japanese's prompts, whose inputs reach 86,682, gave logits that were not finite on Metal (2026-11-29). - **Vulkan's row-wise operations past 512 rows.** Vulkan dispatches NORM, L2_NORM, SUM_ROWS, MEAN, CUMSUM and RMS_NORM_BACK 512 rows to a row of workgroups once a tensor has more rows, and their shaders wrote the rows past the tensor's last into the memory after the output whenever the rows were no multiple of 512. A driver that drops writes @@ -142,12 +148,18 @@ in 32.0 s on Metal; with MAMBA1 in 15.1 s, and with the convolution below as wel 4.95 against 5.01, and 69 s recognized by Qwen3-ASR 0.6B Q8_0 in 2.41 against 2.39 s and by parakeet-tdt_ctc-0.6b-ja F16 in 1.44 against 1.45 s. On the RTX 5090, a run of the twenty sentences took about 24 s with robustness kept, as without it. -- **Metal and Vulkan multiply an F32 matrix by F32 activations in float where the activations ask for it** with - ggml's own `ggml_prec_set_src(t, GGML_PREC_F32, 1)`, staging both operands as float on Metal's simdgroup and - tensor-API paths and in Vulkan's scalar shader on every device, as the CPU multiplies them; every other product is - computed as in ggml. A family whose activations pass what half precision holds asks for it on its products, as - SEMamba does. On an Apple M5 (2026-11-01) such a product takes 1.5 to 2 times as long on the tensor API (1024^3: 0.48 - to 0.98 ms), 0.84 to 0.92 times on the simdgroup path and 1.07 to 1.22 times on Vulkan through MoltenVK. +- **Every backend keeps F32 activations in their range where they ask for it** with ggml's own + `ggml_prec_set_src(t, GGML_PREC_F32, 1)`, for every matrix type; every other product is computed as in ggml. An F32 + matrix is multiplied in float, both operands staged as float on Metal's simdgroup and tensor-API paths and in Vulkan's + scalar shader on every device, as the CPU multiplies them. Metal scales the activations of a matrix it stages in half + precision, F16 or quantized, by the power of two that brings them within 32768, as ggml's MUL_MAT_ID does, and the + products back, which is exact and leaves activations within 32768 as they were. Vulkan keeps them on q8_1 for a matrix + whose q8_1 kernel does not read the blocks' sums, and otherwise multiplies them in the scalar shaders that stage both + operands in float, for MUL_MAT and MUL_MAT_ID. The CPU multiplies an F16 matrix by them in float. A family whose + activations pass what half precision holds asks for it on its products, as + `the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md` + says. On an Apple M5 (2026-11-01) an F32 product that asks takes 1.5 to 2 times as long on the tensor API (1024^3: + 0.48 to 0.98 ms), 0.84 to 0.92 times on the simdgroup path and 1.07 to 1.22 times on Vulkan through MoltenVK. - **Vulkan convolves with an F32 kernel in float, in the scalar shader on every device**, as it convolves on a device without cooperative matrices; an F16 kernel keeps the cooperative matrix shaders. A convolution has nothing its operands can ask with, and speech.cpp's one F32 kernel, SEMamba's, needs float. On an RTX 2080 (2026-11-01) SEMamba's @@ -194,6 +206,7 @@ in 32.0 s on Metal; with MAMBA1 in 15.1 s, and with the convolution below as wel | the allocator plans a graph wired otherwise again | `test-gallocr-topology`: two graphs of as many nodes and leafs and the same sizes, wired otherwise, allocated one after the other with one allocator, on the CPU and Metal | the tag's `ggml_gallocr_alloc_graph()` plans such a graph again | | a depthwise convolution of one position per channel takes the kernel's layout | `test-backend-ops -o CONV_2D_DW`: inputs [1, 1, 64] by 3x3 and [1, 1, 1024] by 9x9 with a channel-fastest kernel, in F32 and F16, on Metal and Vulkan | the tag passes those cases | | F32 products in float where the activations ask for it, on Metal and Vulkan | `test-backend-ops -o MUL_MAT`: F32 by F32 whose src1 asks for `GGML_PREC_F32`, with values of 6.6e4 to 7.2e4 in the activations (128 to 64 at 151*81 and 251*161 positions, 129x77x1000, 1024x192x288) and in the matrix (129x77x1000), on Metal with and without the tensor API and on Vulkan | the tag passes those cases | + | activations that ask for F32 keep their range in products of F16 and quantized matrices, on Metal, Vulkan and the CPU | `test-backend-ops -o MUL_MAT -p src1_prec=f32` and `-o MUL_MAT_ID -p amax=100000`: F16, Q8_0, Q6_K, Q5_K, Q4_K, IQ4_NL and IQ3_XXS matrices of 2048x512 by activations of 6.6e4 to 1e5 at 1 to 363 rows, and 8 of 32 experts of them over 64 tokens, on Metal with and without the tensor API and on Vulkan | the tag passes those cases on Metal, Vulkan and the CPU | | Vulkan's convolutions of an F32 kernel in float | `test-backend-ops -o CONV_2D`: an F32 kernel with values of 6.6e4 to 7.2e4 in the input (64 to 64 channels over 323 by 403, 8 to 17 over 19 by 17) and in the kernel (the latter), on Vulkan on a device with cooperative matrices, such as the RTX 2080 | the tag passes those cases | | Vulkan's row-wise operations bounded | `test-backend-ops` NORM, RMS_NORM_BACK, L2_NORM, SUM_ROWS, MEAN and CUMSUM of rows of 64 with 513 rows, 513 over three dimensions and 40411, on Vulkan under a driver that writes outside a descriptor's range, such as MoltenVK | the tag passes those cases | | Mamba-1 as one operation (speed-up) | `test-backend-ops -o MAMBA1`: d_inner 256 at 251 tokens of 161 and 481 sequences and 161 and 481 tokens of 251, each way, sequences of 1 and 5 steps, and d_inner 128 and 512 on Metal, on Metal and Vulkan | the tag has an operation of Mamba-1 that SEMamba's graph can take, as fast on Metal and Vulkan | @@ -236,6 +249,11 @@ The alternatives were turned down: product for it, for Vulkan's accumulation, so it would be every product again. - F32 products staged in half everywhere, for the speed of the tensor API and the cooperative matrices. Half loses the values past 65504, which RE-USE's activations reach. +- Metal staging the operands of an F16 or quantized matrix in float where the activations ask, as for an F32 matrix. + The scale keeps the speed of half precision on the tensor API, where float staging took 1.5 to 2 times as long, and + loses nothing the CPU keeps: its products of quantized matrices quantize the activations to eight bits. +- Vulkan scaling the activations as Metal does. Its matrix shaders read B in a dozen variants, against one switch to the + scalar shaders that already stage float for an F32 matrix. - RE-USE's activations scaled down before its projection in speech.cpp. The defect would stay for every other graph. - Mamba-1 written with ggml's operations, with an SSM_SCAN of Mamba-1 on Vulkan and a faster one on Metal. The operations would still pass every step's activations through memory between them, and the backward direction would diff --git a/docs/adr/the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md b/docs/adr/the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md new file mode 100644 index 00000000..401328e3 --- /dev/null +++ b/docs/adr/the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md @@ -0,0 +1,71 @@ +# The FFN down projections of language models and audio-token decoders keep their activations in float32's range on every device + +## Context + +Metal's matrix kernel for many rows, `kernel_mul_mm`, stages the activations of every matrix type but BF16 in half +precision; Vulkan's matrix shaders do the same on every GPU with fp16, outside its q8_1 kernels, which a GPU with +NVIDIA's cooperative matrix 2 does not take, so that an RTX 2080 stages even a Q8_0 matrix's activations in half; and +the CPU converts the activations of an F16 matrix to F16 and, on NEON with FP16 arithmetic, sums the products in half +precision. The kernels of one row to a few, which every request's decoding steps take, read the activations in float. A value past 65504 becomes +inf there, and a graph's logits are no longer finite. ggml keeps a product's activations in their range only where +`ggml_prec_set_src(t, GGML_PREC_F32, 1)` asks for it, on the backends that honor that request. + +The inputs of an FFN's down projection, the output of its gated activation, take values that no normalization bounds, +and in a transformer pretrained as a language model, or one that predicts audio tokens one at a time, they reach that +range at some tokens: the common prefix of every prompt, a closing 「。」, a tag. The largest of those inputs over a +family's own requests, on the CPU (2026-11-29): + +| Transformer | Largest input of an FFN down projection | +|---|---| +| Qwen3-TTS 0.6B's code predictor (Q8_0) | 188,823 | +| Drama-3-Japanese's experts (StudioYebisu's Q4_K, a fine-tune of fishaudio/drama-3) | 86,682 | +| its shared expert | 66,927 | +| Drama-3's experts (Q4_K) | 19,103 | +| Qwen3-ASR 1.7B's decoder (Q8_0) | 14,904 | +| Drama-3's audio decoder | 9,543 | +| Haru-4B (F16) | 3,894 | +| Qwen3-TTS 0.6B's talker | 1,404 | +| Irodori-TTS v4.1's text encoder (F16) | 585 | +| the audio encoders, codecs, DiTs and flow heads of every family | below 240 | + +Every other product's inputs stayed below 1,000, but for RE-USE's Mamba projections, which reach 7.07e4 and ask for +float already. Drama-3-Japanese's prompts gave logits that were not finite on Metal wherever an input passed 65504, and +only there: of fifteen requests, the seven whose largest input lay between 36,345 and 58,070 spoke and the eight above +65504 failed. Its distributor tells Vulkan users to turn fp16 off with environment variables. The official weights of the +same model stay at 19,103, so a fine-tune of a model that passes today can pass that range tomorrow. + +## Decision + +- **The FFN down projections of every transformer pretrained as a language model, or that predicts audio tokens one at + a time, ask for their activations in float32's range**, through `mul_mat_f32_activations()` and + `mul_mat_id_f32_activations()`: Qwen3.5's (Drama-3's experts and shared expert, Haru's layers), Qwen3's (Qwen3-TTS's + talker and code predictor, Qwen3-ASR's decoder), Drama-3's audio decoder, Irodori-TTS's text encoders (ModernBERT and + T5Gemma 2's, both pretrained as language models) and Nemotron 3.5 Transcribe's experts and shared expert. The rule + goes by what a transformer is, not by what its weights measure, and a new family's port follows it. +- **The audio encoders, codecs, DiTs and flow heads do not ask**, whose FFN inputs stay two to three orders of magnitude + below the range; nor does any other product of the language models, whose inputs a normalization or an attention + bounds. +- **Any other product whose inputs reach that range asks too**, as RE-USE's Mamba projections do. +- **Every backend honors the request for every matrix type**, through the ggml fork, as the fork's record says: Metal + scales the activations by a power of two into half precision's range and the products back, Vulkan keeps them on + q8_1 or multiplies them in float, and the CPU multiplies an F16 matrix in float. + +The alternatives were turned down: + +- Asking only on the two products the issue reported. Every other language model's down projections would stay one + fine-tune away from the same failure, as Qwen3-TTS's code predictor is one batched product away. +- Asking on every product of every graph. No product but the down projections and RE-USE's reaches a hundredth of the + range, and each request costs Metal two small kernels before the product and Vulkan its cooperative matrices outside + q8_1. +- Scaling a family's activations down before a product and back after it, in speech.cpp. The defect stays for every + product the scale does not cover, and the CPU's result changes with it. +- Turning fp16 off on Vulkan (`GGML_VK_DISABLE_F16` and the cooperative matrix variables), as the distributor of + Drama-3-Japanese advises. It changes the environment of the host's process and every product of every model. +- Choosing the products by measuring each model's weights. A fine-tune of measured weights passes the range unmeasured. + +## Consequences + +Products that ask keep their results on Metal bit for bit while their activations stay within 32768, which covers every +official model above; past it, they are scaled where they overflowed before. On Vulkan they move off the cooperative +matrix kernels to the scalar shaders where no q8_1 kernel takes their matrix. On the CPU an F16 model's down projections +are computed in float, which changes their results by rounding. From 09a8cbd5054e26f72a4093a39d7b4f7271184656 Mon Sep 17 00:00:00 2001 From: sakasegawa Date: Sun, 29 Nov 2026 21:29:10 +0900 Subject: [PATCH 3/3] Size Vulkan's float-operand tiles by the BK its shader takes, and record the costs measured on Metal and Vulkan The fork's commit counted the shared memory of the F32 and F16 pipelines that keep their operands in float with a BK of 16, where mul_mm.comp takes 32 for those matrices whatever its constant says, so that a 128x128 tile needing 34,816 bytes was counted 18,432 and a device of 32 KiB could take a tile it cannot hold; it now counts 32 for every matrix. An RTX 2080's 48 KiB and MoltenVK's medium tiles hold every tile either way, so the pipelines there are the same. The records get the costs: on Metal the audio of Drama-3, Qwen3-TTS 0.6B and Irodori-TTS v4.1 stays the same bit for bit, and on an RTX 2080 the prompts of Qwen3-ASR 1.7B and Qwen3-TTS 0.6B slow by about a tenth, as their down projections leave the cooperative matrices. Checked: every MUL_MAT (2814) and MUL_MAT_ID (950) case passes on Vulkan0 through MoltenVK with the change. Found by the review of the ggml fork's commit. Co-Authored-By: Claude Opus 5.5 --- ...ects-of-its-upstream-tag-and-measured-speed-ups.md | 6 ++++-- ...r-activations-in-float32s-range-on-every-device.md | 11 ++++++++--- ggml | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/adr/ggml-comes-from-a-fork-that-carries-tested-fixes-for-the-defects-of-its-upstream-tag-and-measured-speed-ups.md b/docs/adr/ggml-comes-from-a-fork-that-carries-tested-fixes-for-the-defects-of-its-upstream-tag-and-measured-speed-ups.md index da0449ed..ec22312f 100644 --- a/docs/adr/ggml-comes-from-a-fork-that-carries-tested-fixes-for-the-defects-of-its-upstream-tag-and-measured-speed-ups.md +++ b/docs/adr/ggml-comes-from-a-fork-that-carries-tested-fixes-for-the-defects-of-its-upstream-tag-and-measured-speed-ups.md @@ -252,8 +252,10 @@ The alternatives were turned down: - Metal staging the operands of an F16 or quantized matrix in float where the activations ask, as for an F32 matrix. The scale keeps the speed of half precision on the tensor API, where float staging took 1.5 to 2 times as long, and loses nothing the CPU keeps: its products of quantized matrices quantize the activations to eight bits. -- Vulkan scaling the activations as Metal does. Its matrix shaders read B in a dozen variants, against one switch to the - scalar shaders that already stage float for an F32 matrix. +- Vulkan scaling the activations as Metal does, which would keep the cooperative matrices. Its matrix shaders read B in + a dozen variants, against one switch to the scalar shaders that already stage float for an F32 matrix, which costs + NVIDIA's GPUs about a tenth of a prompt's speed: on an RTX 2080 Qwen3-ASR 1.7B Q8_0 recognized 12.8 s in 0.55 s + against 0.49 s (2026-11-29). - RE-USE's activations scaled down before its projection in speech.cpp. The defect would stay for every other graph. - Mamba-1 written with ggml's operations, with an SSM_SCAN of Mamba-1 on Vulkan and a faster one on Metal. The operations would still pass every step's activations through memory between them, and the backward direction would diff --git a/docs/adr/the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md b/docs/adr/the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md index 401328e3..67a1fa1a 100644 --- a/docs/adr/the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md +++ b/docs/adr/the-ffn-down-projections-of-language-models-and-audio-token-decoders-keep-their-activations-in-float32s-range-on-every-device.md @@ -66,6 +66,11 @@ The alternatives were turned down: ## Consequences Products that ask keep their results on Metal bit for bit while their activations stay within 32768, which covers every -official model above; past it, they are scaled where they overflowed before. On Vulkan they move off the cooperative -matrix kernels to the scalar shaders where no q8_1 kernel takes their matrix. On the CPU an F16 model's down projections -are computed in float, which changes their results by rounding. +official model above: the audio of Drama-3 Q4_K, Qwen3-TTS 0.6B Q8_0 and Irodori-TTS v4.1 F16 and Q8_0 stayed the +same bit for bit (Apple M5, 2026-11-29); past it, they are scaled where they overflowed before. On Vulkan they move off +the cooperative matrix kernels to the scalar shaders where no q8_1 kernel takes their matrix, which on a GPU with +NVIDIA's cooperative matrix 2 is every quantized type, and its prompts slow by about a tenth: on an RTX 2080, +Qwen3-ASR 1.7B Q8_0 recognized 12.8 s in 0.55 s against 0.49 s, Qwen3-TTS 0.6B Q8_0 gave its first audio in 31 ms +against 28 ms, and Irodori-TTS v4.1 F16's first audio stayed at 0.78 s (2026-11-29). A device's first run compiles the +new pipelines, which took Drama-3-Japanese's first audio there to 16.7 s once. On the CPU an F16 model's down +projections are computed in float, which changes their results by rounding. diff --git a/ggml b/ggml index b23c4bfc..2e032120 160000 --- a/ggml +++ b/ggml @@ -1 +1 @@ -Subproject commit b23c4bfc7f242a172816f1fc1ad87cf7f36d9e03 +Subproject commit 2e0321200c448d94b3528ff69e432125967a8430