Repository navigation
perf(models): fused single-launch xIELU Metal kernel for Apertus - #414
Conversation
apertus_xielu issues ~11 elementwise MLX ops (square, multiply_scalar, full, minimum, expm1, subtract, where, add, ...) per layer per token. On the Apertus-8B decode path that is ~352 tiny dispatches per token (11 ops x 32 layers), all over the same small MLP intermediate buffer. Add a fused Metal kernel `fused_xielu` (mlx_cxx_kernels.cpp, wired through the cxx bridge header and lib.rs) that computes the whole xIELU activation in one launch, gated by MLXCEL_FUSED_XIELU (default-off, single read cached in a OnceLock) on the Apertus MLP forward, mirroring the fused decode-MoE rollout (#268). The kernel keeps every intermediate in the input dtype (bf16 for the native Apertus path) and uses one arithmetic op per statement, so each sub-expression rounds exactly where the elementwise reference rounds (one MLX op == one round) and the Metal compiler cannot contract a multiply and add into a single FMA. Metal's JIT metal_math has no expm1 and the metal_kernel preamble does not pull in MLX's expm1f.h, so the kernel header reproduces MLX's expm1f verbatim (Norbert Juffa's BSD-2-Clause routine carried in MLX, attributed in NOTICE) to match mlx::core::expm1 bit-for-bit. Non-Metal back-ends fall back to an equivalent elementwise graph, so the flag is safe everywhere. Parity: greedy temp-0 decode is byte-identical between the elementwise and fused paths on two prompts, and a new unit test (fused_xielu_matches_elementwise_bit_for_bit) asserts bit-for-bit equality across the full activation domain (positive, the x>0 boundary, near-zero negatives straddling eps, large negatives that saturate expm1). Decode (Apple M1 Ultra, apertus-8b-instruct-2509-4bit, interleaved A/B, best-of-3, temp 0, --no-chat-template, 100 tokens): flag off 83.25/83.44/83.80 tok/s vs flag on 85.70/85.61/86.08 tok/s, a repeatable +2.7% with no parity cost. The kernel is Apertus-only; other models and the flag-off Apertus path are unchanged, and the BitNet and fused-MoE kernels in the same translation unit were re-smoke-tested.
Implementation Review SummaryIntent
Verification1. Parity (byte-identical claim) — confirmed.
2. Flag gating — confirmed. 3. Non-Metal fallback — confirmed. 4. TU safety — confirmed. The change to 5. NOTICE / attribution — confirmed. The new NOTICE entry attributes MLX's 6. Conventions — confirmed. No shared function modified (no Findings
Observations (no fix required)
|
Add the MLXCEL_FUSED_XIELU entry to the hardware and kernel diagnostic table alongside MLXCEL_FUSED_MOE and MLXCEL_FUSED_QK_NORM. Covers what the flag does (routes Apertus MLP activation through the fused single-launch Metal xIELU kernel), default (off, presence-based), scope (Apertus only; non-Metal back-ends fall back to an equivalent elementwise graph), byte-identical guarantee, and the measured +2.7% decode gain on M1 Ultra.
PR finalizationOne change added: The PR body was updated to reference the new doc entry in both the "What changed" section and the "Default state and recommendation" paragraph. Checks:
Branch is pushed and clean. |
…417) Flip MLXCEL_FUSED_XIELU to default-on with `0`/`false`/`off`/`no` as the case-insensitive opt-out, mirroring the MLXCEL_FUSED_MOE default-on convention (#282). #409 (PR #414) shipped the fused single-launch Metal xIELU kernel for the Apertus MLP activation default-off, explicitly pending M5 Max validation; this completes that follow-up. M5 Max (Apple M5 Max, 128GB), Apertus-8B-Instruct-2509-4bit, mlxcel-bench-decode, 12 interleaved off/on rounds of 256-token greedy decode: off (elementwise): 112.0 tok/s on (fused): 114.2 tok/s delta: +1.9% mean (+2.05% median), 11/12 paired wins, paired t ~ 8.9 Greedy temp-0 decode is byte-identical between the two paths on M5 Max (end-to-end sha256 match over a full generation, plus the committed bit-for-bit unit test over the full activation domain). With the documented M1 Ultra result (+2.7%), the fused path is a byte-identical, positive-throughput, no-regression win on both validated Apple Silicon generations. Apertus-only; non-Metal back-ends keep the equivalent elementwise fallback. Set MLXCEL_FUSED_XIELU=0 to force the elementwise path. Adds fused_xielu_enabled_from() as a pure, unit-tested opt-out decision and updates docs/environment-variables.md.
…lup#414) * perf(models): fused single-launch xIELU Metal kernel for Apertus apertus_xielu issues ~11 elementwise MLX ops (square, multiply_scalar, full, minimum, expm1, subtract, where, add, ...) per layer per token. On the Apertus-8B decode path that is ~352 tiny dispatches per token (11 ops x 32 layers), all over the same small MLP intermediate buffer. Add a fused Metal kernel `fused_xielu` (mlx_cxx_kernels.cpp, wired through the cxx bridge header and lib.rs) that computes the whole xIELU activation in one launch, gated by MLXCEL_FUSED_XIELU (default-off, single read cached in a OnceLock) on the Apertus MLP forward, mirroring the fused decode-MoE rollout (lablup#268). The kernel keeps every intermediate in the input dtype (bf16 for the native Apertus path) and uses one arithmetic op per statement, so each sub-expression rounds exactly where the elementwise reference rounds (one MLX op == one round) and the Metal compiler cannot contract a multiply and add into a single FMA. Metal's JIT metal_math has no expm1 and the metal_kernel preamble does not pull in MLX's expm1f.h, so the kernel header reproduces MLX's expm1f verbatim (Norbert Juffa's BSD-2-Clause routine carried in MLX, attributed in NOTICE) to match mlx::core::expm1 bit-for-bit. Non-Metal back-ends fall back to an equivalent elementwise graph, so the flag is safe everywhere. Parity: greedy temp-0 decode is byte-identical between the elementwise and fused paths on two prompts, and a new unit test (fused_xielu_matches_elementwise_bit_for_bit) asserts bit-for-bit equality across the full activation domain (positive, the x>0 boundary, near-zero negatives straddling eps, large negatives that saturate expm1). Decode (Apple M1 Ultra, apertus-8b-instruct-2509-4bit, interleaved A/B, best-of-3, temp 0, --no-chat-template, 100 tokens): flag off 83.25/83.44/83.80 tok/s vs flag on 85.70/85.61/86.08 tok/s, a repeatable +2.7% with no parity cost. The kernel is Apertus-only; other models and the flag-off Apertus path are unchanged, and the BitNet and fused-MoE kernels in the same translation unit were re-smoke-tested. * docs: document MLXCEL_FUSED_XIELU in environment-variables.md Add the MLXCEL_FUSED_XIELU entry to the hardware and kernel diagnostic table alongside MLXCEL_FUSED_MOE and MLXCEL_FUSED_QK_NORM. Covers what the flag does (routes Apertus MLP activation through the fused single-launch Metal xIELU kernel), default (off, presence-based), scope (Apertus only; non-Metal back-ends fall back to an equivalent elementwise graph), byte-identical guarantee, and the measured +2.7% decode gain on M1 Ultra.
…ablup#417) Flip MLXCEL_FUSED_XIELU to default-on with `0`/`false`/`off`/`no` as the case-insensitive opt-out, mirroring the MLXCEL_FUSED_MOE default-on convention (lablup#282). lablup#409 (PR lablup#414) shipped the fused single-launch Metal xIELU kernel for the Apertus MLP activation default-off, explicitly pending M5 Max validation; this completes that follow-up. M5 Max (Apple M5 Max, 128GB), Apertus-8B-Instruct-2509-4bit, mlxcel-bench-decode, 12 interleaved off/on rounds of 256-token greedy decode: off (elementwise): 112.0 tok/s on (fused): 114.2 tok/s delta: +1.9% mean (+2.05% median), 11/12 paired wins, paired t ~ 8.9 Greedy temp-0 decode is byte-identical between the two paths on M5 Max (end-to-end sha256 match over a full generation, plus the committed bit-for-bit unit test over the full activation domain). With the documented M1 Ultra result (+2.7%), the fused path is a byte-identical, positive-throughput, no-regression win on both validated Apple Silicon generations. Apertus-only; non-Metal back-ends keep the equivalent elementwise fallback. Set MLXCEL_FUSED_XIELU=0 to force the elementwise path. Adds fused_xielu_enabled_from() as a pure, unit-tested opt-out decision and updates docs/environment-variables.md.
Summary
Fuse the Apertus xIELU activation into a single Metal kernel (
fused_xielu) to cut the per-token launch/epilogue overhead of the ~11-op elementwiseapertus_xielugraph. On the Apertus-8B decode path the elementwise activation is about 352 tiny dispatches per token (11 ops x 32 layers) over the same small MLP intermediate buffer. This is the one remaining meaningful Apertus decode lever flagged as a follow-up by #269 (PR #399); the bulk of the original #269 gap was already recovered by #290, leaving Apertus near parity.The result is byte-identical to the elementwise path and a repeatable +2.7% decode improvement on M1 Ultra, so it carries no parity cost.
What changed
src/lib/mlxcel-core/cpp/mlx_cxx_kernels.cpp: newfused_xieluplus itsxielu_fusedMetal kernel. Every intermediate is held in the input dtype (bf16 for the native Apertus path) and each sub-expression is one arithmetic op per statement, so the kernel rounds exactly where the elementwise reference rounds (one MLX op == one round) and the Metal compiler cannot contract a multiply and add into a single FMA. Metal's JITmetal_mathhas noexpm1and themetal_kernelpreamble does not include MLX'sexpm1f.h, so the kernelheaderreproduces MLX'sexpm1fverbatim to matchmlx::core::expm1bit-for-bit. Non-Metal back-ends use an equivalent elementwise fallback, so the path is safe everywhere.src/lib/mlxcel-core/cpp/mlx_cxx_bridge.handsrc/lib/mlxcel-core/src/lib.rs: declare thefused_xieluFFI entry.src/models/apertus.rs: routeMLP::forwardthroughmlxcel_core::fused_xieluwhenMLXCEL_FUSED_XIELUis set (read once, cached in aOnceLock, default-off), mirroring the fused decode-MoE rollout in #268. Adds thefused_xielu_matches_elementwise_bit_for_bitunit test asserting bit-for-bit equality across the full activation domain.NOTICE: attribute MLX'sexpm1fdevice routine (Apple, MIT) and the embedded Norbert Juffa BSD-2-Clause routine it carries.docs/environment-variables.md: documentMLXCEL_FUSED_XIELUin the hardware and kernel diagnostic table alongside the other fused-kernel flags (MLXCEL_FUSED_MOE,MLXCEL_FUSED_QK_NORM).Before/after (validation)
Decode, Apple M1 Ultra,
models/apertus-8b-instruct-2509-4bit, interleaved A/B (each pair flag-off then flag-on back-to-back to control thermal drift), best-of-3,--temp 0 --no-chat-template, 100 tokens:Median 83.44 -> 85.70 tok/s, a repeatable +2.7% (all three pairs agree, ~2.2-2.3 tok/s, no overlap). This is in the range of the prior AsType reduction campaign (about +2.6%) but achieved with one clean kernel and zero parity cost.
Parity (byte-identical)
Greedy temp-0 decode is byte-identical between the elementwise and fused paths:
diffempty (byte-identical).diffempty (byte-identical).cargo test --release -p mlxcel apertus:fused_xielu_matches_elementwise_bit_for_bitpasses (bit-for-bit over positive, the x>0 boundary, near-zero negatives straddling eps, and large negatives that saturate expm1), and the existing flag-offapertus_greedy_parity_matches_mlx_lmis unchanged.Bit-for-bit (rather than jitter-class) required keeping every intermediate in bf16 with one op per statement, and reproducing MLX's exact
expm1f. A first prototype using a single fused float expression andexp(c)-1was only jitter-class (near-tie token flips), which the per-op rounding and exactexpm1feliminated.No regression
mlx_cxx_kernels.cppis purely additive; the BitNet (bitlinear_matmul) and fused-MoE kernels in the same translation unit were re-smoke-tested and produce correct output.cargo fmt -p mlxcel -p mlxcel-core --checkclean.Default state and recommendation
Shipped default-off, matching the fused decode-MoE rollout (#268). Because the path is byte-identical, Apertus-only, and falls back cleanly on non-Metal back-ends, the risk of enabling it by default is essentially zero. Recommendation: enable
MLXCEL_FUSED_XIELUby default for Apertus. Left default-off here so the default-on flip is an explicit maintainer decision, consistent with #268.MLXCEL_FUSED_XIELUis documented indocs/environment-variables.md(hardware and kernel diagnostic table).Test plan
cargo build --release --features metal,accelerate --bin mlxcel --bin mlxcel-bench-decodecargo test --release -p mlxcel apertus(fused parity unit test + existing greedy parity)cargo fmt -p mlxcel -p mlxcel-core --checkCloses #409