Repository navigation
Description
Problem / Background
PR #697 (merged 21db6fe, closes #693) direct-transcodes ModelOpt NVFP4 triplets and preserves weight_scale_2 as a per-linear global_scale sidecar, applied as a scalar multiply on the matmul output in UnifiedLinear::forward (src/lib/mlxcel-core/src/layers.rs).
The compiled fused paths cannot apply this sidecar. As a result, MLP::forward and compiled_per_layer_input_gate in src/models/gemma4.rs bypass the fused compiled_gelu_approx_mlp_forward kernel whenever any participating weight carries a sidecar, falling back to op-at-a-time projections.
Measured cost on GB10 with gemma-4-31b-it-nvfp4: decode is roughly 7% slower than the dense-repack baseline (4.84 vs 5.26 tok/s at short prompt, 5.01 vs 5.38 tok/s at 2048). The gap is amplified because gemma4 decode runs with CUDA graphs disabled (#688), so every extra element-wise dispatch is paid at full launch cost.
Benchmarks:
benchmarks/cuda_gb10_issue693_nvfp4_direct_vs_dense_2026-07-08.csvdocs/benchmark_results/nvfp4-direct-transcode-gb10-2026-07-08.md
Proposed Solution
Teach the fused MLP path (and the per-layer-input gate fused path) to accept up to three per-linear f32 global scales, one each for the gate, up, and down projections, and apply them inside the compiled kernel:
- Fold the gate and up scales into their respective pre-activation matmul products.
- Apply the down scale on the fused output.
This restores fused execution for sidecar-carrying NVFP4 weights instead of falling back to op-at-a-time projections.
Mathematical care (GeGLU is nonlinear): the MLP computes down(gelu(gate(x)) * up(x)). Because gelu is nonlinear, the gate projection's global scale must be applied to the gate product BEFORE the activation: gelu(s_gate * (x @ W_gate)). It cannot be factored out past the activation, i.e. s_gate * gelu(x @ W_gate) is wrong. The up scale multiplies the up product (linear, so placement before or after the elementwise product is equivalent), and the down scale can be applied to the final output since the down projection is linear.
Acceptance Criteria
- Fused MLP path (
compiled_gelu_approx_mlp_forward) andcompiled_per_layer_input_gateaccept sidecar-carrying weights, with each per-projection global scale applied at the mathematically correct point (gate scale before the activation, up scale on the up product, down scale on the output) - Greedy output parity vs the current op-at-a-time sidecar path on gemma-4-31b-it-nvfp4 is token-exact
- Decode throughput on GB10 recovers to within ~2% of the dense-repack baseline (short prompt and 2048)
- Guard tests updated: the fused path now accepts sidecar weights, while non-gemma4 paths remain unaffected
- Benchmark CSV and
docs/benchmark_results/summary updated with the recovered numbers
Technical Considerations
- The sidecar is produced by the NVFP4 direct transcode in PR perf(nvfp4): direct-transcode ModelOpt triplets to MLX native #697;
UnifiedLinear::forwardcurrently applies it as a scalar multiply on the matmul output, which is what the fused kernel must reproduce internally. - With CUDA graphs disabled for gemma4 decode (bug(gemma4/cuda): decode collapses to <pad> after 1-3 tokens (single-query maskless attention + quantized GEMV); mlx_lm decodes coherently #688), dispatch count dominates; folding the scalar multiplies into the compiled kernel removes the per-projection element-wise launches that the op-at-a-time fallback incurs.
- Keep the scale inputs optional (default 1.0 or absent) so dense-repack and non-sidecar weights compile to the same kernel behavior as today.
Related
- perf(nvfp4): direct-transcode ModelOpt triplets to MLX native #693 (NVFP4 direct transcode)
- PR perf(nvfp4): direct-transcode ModelOpt triplets to MLX native #697 (introduced the
global_scalesidecar and the fused-path bypass) - perf(cuda/gemma4): decode is bound by 8-bit MLP weight bytes plus a near-saturated dual CPU/GPU pipeline; qmm kernels are healthy #683 (CUDA decode kernel profiling)
- bug(gemma4/cuda): decode collapses to <pad> after 1-3 tokens (single-query maskless attention + quantized GEMV); mlx_lm decodes coherently #688 (gemma4 CUDA graphs disabled for decode)
- perf(metal): evaluate native NVFP4 path after direct transcode #694 (Metal native NVFP4 path)
Activity
fix(gemma4): gate NVFP4 fused scale fold to single-token decode
inureyes commentedon Jul 9, 2026
Disposition note before merge: PR #701 implements the fold and closes this issue with one acceptance criterion consciously unmet, recorded here for transparency.
Met: scales folded at the mathematically correct points (gate before GeGLU, up on the product, down on the output), byte-identical greedy parity vs the op-at-a-time path on the short prompt (long-generation divergence sits inside the backend's own run-to-run nondeterminism envelope, demonstrated by baseline-vs-baseline divergence), guard tests updated, benchmarks recorded under
benchmarks/anddocs/benchmark_results/.Not met, with cause: decode did not recover to within ~2% of the dense-repack baseline (fused 4.90/5.07 vs dense 5.25/5.38 tok/s short/2048). GB10 gemma-4 decode is weight-bandwidth-bound (~94% GPU-busy), so removing per-layer scale dispatches is throughput-neutral there; the fold improves decode only ~1% over the bypass. The residual dense-vs-direct decode difference is not conclusively isolated (the current comparison is confounded by unequal generated-token counts and a cross-day dense 2048 measurement) and is documented in
docs/benchmark_results/nvfp4-direct-transcode-gb10-2026-07-08.md. If that residual ever matters, it should be a narrow same-day measurement task, not a fold change.Review-driven correction: the initial fold routed multi-token prefill through an uncompiled eager fold and regressed 2048-token prefill 8.3%; the merged version gates the fused fold to single-token decode, restoring prefill to 447.2 tok/s (within 0.6% of the bypass) while keeping the decode win.
MLXCEL_DISABLE_FUSED_GLOBAL_SCALE=1restores the full bypass.Net effect of the merged change: fused-execution invariant restored for sidecar weights, three FFI crossings and three dispatches removed per MLP layer at decode, no prefill regression, bit-consistent output.
perf(gemma4): fold NVFP4 global scales into fused MLP kernel (lablup#701
perf(gemma4): fold NVFP4 global scales into fused MLP kernel (lablup#701