Skip to content

perf(gemma4): fold NVFP4 global scales into fused MLP kernel #698

Closed

Description

@inureyes

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.csv
  • docs/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) and compiled_per_layer_input_gate accept 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

Related

Activity

  1. added
    type:performance
    priority:low
    area:core
    status:ready
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Jul 8, 2026
  2. inureyes commented on Jul 9, 2026

    @inureyes
    MemberAuthor

    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/ and docs/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=1 restores 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.

  3. added
    status:done
    and removed
    status:review
    on Jul 9, 2026
  4. added a commit that references this issue on Jul 30, 2026
    d437bc7
  5. self-assigned this
    on Aug 31, 2026
  6. added a commit that references this issue on Oct 27, 2026
    5309f91
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corepriority:lowstatus:donetype:performance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions