Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 |
Expand Down Expand Up @@ -236,6 +249,13 @@ 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, 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# 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: 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.
4 changes: 4 additions & 0 deletions skills/add-model/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ names keys, files and layouts.
- The engine (`src/engines/<family>-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

Expand Down
22 changes: 22 additions & 0 deletions src/common/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/common/qwen3-decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/common/qwen35-decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,23 @@ 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);
}

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<float> Qwen35Decoder::run(const std::vector<float> & input, int64_t n, Qwen35Stages * stages) {
Expand Down
2 changes: 1 addition & 1 deletion src/families/drama3/audio-decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Loading
Loading