Repository navigation
Description
Problem / Background
#269 (PR #399) characterized the Apertus dense decode gap. The bulk of the original 0.83x gap had already been recovered by the earlier #290 fix (the bf16-scale quant decode regression), so Apertus now runs at roughly 0.98x of mlx-lm. PR #399 additionally trimmed one bit-exact elementwise op in apertus_xielu (src/models/apertus.rs).
Profiling the forward path showed the per-token decode step is GPU-bound: host-side graph build is around 0.7ms while GPU eval is around 13ms. With that ratio, further host-side op-count reductions have very little headroom. The implementer of #269 (PR #399) recommended this as the one remaining meaningful decode lever, left explicitly as a documented follow-up.
Proposed Solution
Fuse or compile the xIELU activation for Apertus. apertus_xielu currently issues roughly 11-12 separate elementwise MLX ops per layer per token (beta*x, x^2, mul_scalar, add, min, expm1, sub, mul_scalar, add, greater, where). A single mx::compile window over that elementwise graph, or a custom Metal kernel covering it, could cut per-op launch and epilogue overhead.
This was intentionally not implemented in #269 because it is not a "cheap" recovery: it touches the C++ FFI bridge / Metal kernel path and risks f16 reassociation/jitter.
Caveats
- Modest expected gain. Apertus is already near parity (~0.98x) and the remaining decode gap is diffuse GPU-bound overhead with no single high-value lever. The project's prior AsType reduction campaign capped around +2.6%. Triage should weigh whether the realized gain justifies the FFI/kernel complexity before committing to this work.
- Native bf16 precision. Apertus runs NATIVE bf16. A fused kernel must either preserve greedy temp-0 output as byte-identical, or be explicitly accepted as f16/bf16 jitter-class with documented RMS bounds. This mirrors the fused-MoE kernel's "byte-identical OR jitter-class" parity policy.
- Scope. This touches the C++ FFI bridge / Metal kernel path, not just
src/models/apertus.rs.
Acceptance Criteria
- A fused or compiled xIELU path for Apertus that measurably improves decode tok/s on Apple Silicon.
- Greedy temp-0 output is kept byte-identical, OR documented as jitter-class within stated RMS bounds (mirroring the fused-MoE parity policy).
- The new path is gated and benched against the current elementwise implementation.
- No regression for other models sharing any touched code (FFI bridge / Metal kernel / shared layers).
Technical Considerations
- Implementation lives across
src/models/apertus.rs(apertus_xielu) and the C++ FFI / Metal kernel path. - Follow-up to perf(models): apertus and seed_oss dense decode at ~0.83x of mlx-lm #269 and PR perf(models): profile apertus/seed_oss decode and trim xIELU op #399.
Activity
perf(models): default MLXCEL_FUSED_XIELU on after M5 Max validation (#…
perf(models): default MLXCEL_FUSED_XIELU on after M5 Max validation (l…