Skip to content

ggml-cpu: speed up Mamba-1 and quantized-V flash attention; add an opt-in GELU erf approximation - #1342

Merged
0xShug0 merged 4 commits into
0xShug0:mainfrom
Etherll:Etherll/ggml-cpu-ssm-fa-gelu
Nov 21, 2026
Merged

0xShug0 merged 4 commits into
0xShug0:mainfrom
Etherll:Etherll/ggml-cpu-ssm-fa-gelu

Conversation

@Etherll

@Etherll Etherll commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Reworked after review. ggml_vec_gelu_erf_f32 and GGML_UNARY_OP_GELU_ERF are now identical to main, and the vectorized erf is a separate op that nothing selects yet.

  • ssm_conv_causal_silu and ssm_scan_mamba1 process channels in blocks of 16 lanes, so the inner loops vectorize instead of walking one channel at a time. They call ggml_vec_silu_f32 instead of a local SiLU.
  • Flash attention with a quantized V cache scores a block of 64 keys at a time, then does one vectorized exp and one rescale per block and accumulates the dequantized V rows into a single F32 buffer. F16 and F32 V keep the existing path.
  • New ggml_vec_gelu_erf_approx_f32 behind a new op, ggml_gelu_erf_approx() / GGML_UNARY_OP_GELU_ERF_APPROX. On AVX-512 and on AVX2 with FMA it runs N. Juffa's float erf on 16 or 8 lanes; the tail, other targets and non-F32 tensors take the exact scalar path. The op is appended after RSQRT, so existing op ids keep their values. Only the CPU backend has a kernel for it.
  • New GeluApproximation::ApproxErf, which GeluModule lowers to that op. ExactErf stays the default.

No model selects ApproxErf in this PR. Once it is merged I will add the opt-in to the Voice Isolator (#1343), on the CPU backend only.

The Mamba-1 ops are used by the Voice Isolator and by the reuse and realtime_reuse models. The flash attention change applies to any CPU model with a quantized V cache.

Benchmarks

AMD Ryzen 9 9950X3D (AVX-512, AVX512-BF16), 16 threads, idle machine, best of 3.

Flash attention, test-backend-ops perf -b CPU -o FLASH_ATTN_EXT:

Case main This PR Speedup
q8_0 K/V, head 64, 8 heads, GQA 8, 7680 keys, 512 queries, mask 241.7 ms 193.3 ms 1.25x
q4_0 K/V, same shape 264.4 ms 207.9 ms 1.27x
f16 K/V, head 72, 16 heads, 5776 keys and queries 978 ms 1009 ms 0.97x

The f16 case runs unchanged code, so its difference is run-to-run variation. These three rows are from the first revision of this PR. The two kernel commits have the same patch id now as then, and main has not changed external/ggml in between.

test-backend-ops has no perf cases for the SSM ops, so the rest is the Voice Isolator's real-time factor (RTF, lower is faster) from voice_isolator_warm_bench, re-measured on this revision. "This PR" is #1343 built on this branch with exact GELU. The last column adds the two-line ApproxErf opt-in.

Case Weights main This PR This PR + ApproxErf
Offline, 30 s clip BF16 0.77 0.42 0.40
Offline, 30 s clip F32 0.83 0.46 0.44
Stream, 40 ms pushes BF16 0.76 0.51 0.48
Stream, 40 ms pushes F32 0.81 0.59 0.56
Stream, 10 ms pushes BF16 1.02 0.77 0.73
Stream, 10 ms pushes F32 1.21 0.95 0.92

By commit (BF16 weights):

Commit Offline 30 s Stream 10 ms
main 0.77 1.02
Mamba-1 and flash attention blocking 0.43 0.75
+ ggml_vec_silu_f32 in Mamba-1 0.42 0.77
+ ApproxErf selected by the model 0.40 0.73

Testing

  • encoder_module_test has a new case. It checks that ExactErf still equals scalar erff bit for bit, and that ApproxErf stays within 1e-6 * max(1, |x|) of it. The sweep covers ±0, denormals, both sides of the erf branch point, ±150 and a scalar tail.
  • The Voice Isolator parity test passes on main's kernels, on this branch, and on this branch with ApproxErf selected. The F32 path stays within 2.1e-7 of the Python reference in all three.
  • Full CPU ctest on this branch at fe6d1535: three tests do not pass, and none of them touches this code. lfm3_audio_blocks_test does not compile on main. granite_speech5_b1_units_test fails the same way on a clean main. supertonic_vector_convnext_exp_test asserts a 5% timing margin and fails about 1 run in 15 with and without this branch. The branch is now rebased on 11aeb8d4, which does not touch external/ggml or the framework; I rebuilt and re-ran encoder_module_test there.
  • From the first revision, unchanged since: test-backend-ops test -b CUDA0 on an RTX 5090 checks CUDA against these CPU kernels, and FLASH_ATTN_EXT passes 2880/2880. tests/unittests/test_ssm_mamba1.cpp is only built for Vulkan and Metal, so I built it by hand and ran it with --backend cpu; it passes against the legacy SSM ops and double-precision references.

@0xShug0

0xShug0 commented Nov 19, 2026

Copy link
Copy Markdown
Owner

@Etherll Thanks for the optimizations! The Mamba-1 and quantized-V flash-attention changes look narrowly scoped. However, GELU-erf affects many existing models and needs broader regression and parity testing, particularly because its numerical approximation can introduce rounding differences that accumulate across layers.

I'd suggest keeping ggml_vec_gelu_erf_f32() unchanged and placing the new implementation in ggml_vec_gelu_erf_approx_f32(), exposed through a separate GGML operation such as ggml_gelu_erf_approx().

Then you can extend GeluApproximation in GeluModule with a new value, such as ApproxErf, to expose this operation through the framework while keeping existing behavior unchanged. Only the Voice Isolator model would opt in initially.

Without evidence from a full regression sweep under various workloads, I'd prefer to opt in model by model rather than enable it unconditionally by default.

On interesting example: I've been working on AMBIT. It's highly sensitive to numerical drift. Changing y = x @ weight.T + bias to separate step dot = x @ weight.T, y = dot + bias dropped the cosine similarity against the controlled Python reference WAV to about 6% on CUDA. The model will probably never be merged.

…tention key loop

ssm_conv_causal_silu and ssm_scan_mamba1 now process channels in blocks of
SIMD lanes, so the inner loops vectorize instead of walking one channel at a
time. Flash attention with a quantized V cache dequantizes and accumulates
keys in blocks rather than one row per step.
The causal conv and scan kernels carried their own SiLU over a lane
block; ggml_vec_silu_f32 computes the same x / (1 + exp(-x)).
GGML_UNARY_OP_GELU_ERF_APPROX (ggml_gelu_erf_approx) computes GELU with a
vectorized erf on AVX2+FMA and AVX-512: a polynomial in x^2 below
|x| = 0.9277 and 1 - exp(polynomial) above. Its results can differ from
ggml_gelu_erf in the last bits, so it is a separate op appended to the unary
enum. ggml_vec_gelu_erf_f32 and GGML_UNARY_OP_GELU_ERF are unchanged, and
existing op ids keep their values. Only the CPU backend has a kernel; F16
input and targets without those instruction sets use the exact scalar loop.
GeluModule lowers ApproxErf to ggml_gelu_erf_approx. ExactErf stays the
default and no model selects the new value. The encoder module test checks
that ExactErf still equals scalar erf and bounds the difference between the
two.
@Etherll
Etherll force-pushed the Etherll/ggml-cpu-ssm-fa-gelu branch from d280583 to 24da545 Compare November 21, 2026 00:38
@Etherll Etherll changed the title ggml-cpu: speed up Mamba-1, quantized-V flash attention and GELU erf ggml-cpu: speed up Mamba-1 and quantized-V flash attention; add an opt-in GELU erf approximation Nov 21, 2026
@Etherll

Etherll commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and for the AMBIT example. Opting in model by model makes sense to me.

I reworked the PR the way you described:

  • ggml_vec_gelu_erf_f32() is back to main's code, and GGML_UNARY_OP_GELU_ERF behaves as before.
  • The vectorized version is now ggml_vec_gelu_erf_approx_f32(), behind a separate op, ggml_gelu_erf_approx() (GGML_UNARY_OP_GELU_ERF_APPROX, appended after RSQRT so existing op ids keep their values). Only the CPU backend has a kernel for it.
  • GeluApproximation::ApproxErf lowers to that op. Nothing selects it in this PR.

The Mamba-1 and flash attention commits are the same as before. I re-measured the numbers on this revision and updated the description. Once this is merged I'll add the opt-in to the Voice Isolator in #1343, on the CPU backend only.

@0xShug0
0xShug0 merged commit a85afd2 into 0xShug0:main Nov 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants