Repository navigation
ggml-cpu: speed up Mamba-1 and quantized-V flash attention; add an opt-in GELU erf approximation - #1342
Conversation
|
@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 Then you can extend 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 |
…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.
d280583 to
24da545
Compare
|
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:
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. |
Summary
Reworked after review.
ggml_vec_gelu_erf_f32andGGML_UNARY_OP_GELU_ERFare now identical to main, and the vectorized erf is a separate op that nothing selects yet.ssm_conv_causal_siluandssm_scan_mamba1process channels in blocks of 16 lanes, so the inner loops vectorize instead of walking one channel at a time. They callggml_vec_silu_f32instead of a local SiLU.ggml_vec_gelu_erf_approx_f32behind 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 afterRSQRT, so existing op ids keep their values. Only the CPU backend has a kernel for it.GeluApproximation::ApproxErf, whichGeluModulelowers to that op.ExactErfstays the default.No model selects
ApproxErfin 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
reuseandrealtime_reusemodels. 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: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/ggmlin between.test-backend-opshas no perf cases for the SSM ops, so the rest is the Voice Isolator's real-time factor (RTF, lower is faster) fromvoice_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-lineApproxErfopt-in.ApproxErfBy commit (BF16 weights):
ggml_vec_silu_f32in Mamba-1ApproxErfselected by the modelTesting
encoder_module_testhas a new case. It checks thatExactErfstill equals scalarerffbit for bit, and thatApproxErfstays 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.ApproxErfselected. The F32 path stays within 2.1e-7 of the Python reference in all three.fe6d1535: three tests do not pass, and none of them touches this code.lfm3_audio_blocks_testdoes not compile on main.granite_speech5_b1_units_testfails the same way on a clean main.supertonic_vector_convnext_exp_testasserts a 5% timing margin and fails about 1 run in 15 with and without this branch. The branch is now rebased on11aeb8d4, which does not touchexternal/ggmlor the framework; I rebuilt and re-ranencoder_module_testthere.test-backend-ops test -b CUDA0on an RTX 5090 checks CUDA against these CPU kernels, and FLASH_ATTN_EXT passes 2880/2880.tests/unittests/test_ssm_mamba1.cppis 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.