Skip to content

perf: default Metal NVFP4 to native transcode (#705) - #706

Merged
inureyes merged 2 commits into
mainfrom
update/issue-705-nvfp4-prefill
Jul 9, 2026
Merged

inureyes merged 2 commits into
mainfrom
update/issue-705-nvfp4-prefill

Conversation

@inureyes

@inureyes inureyes commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Make direct ModelOpt-triplet to MLX native NVFP4 the default on Metal/non-CUDA, matching the CUDA default while keeping MLXCEL_NVFP4_DENSE_REPACK=1 as the dense-affine rollback path.
  • Add a native-NVFP4 prefill-friendly Gemma 4 scaled MLP path: multi-token group_size=16/bits=4/mode=nvfp4 now uses a shape-specific compiled graph that folds the weight_scale_2 sidecars and GeGLU activation without using the decode-oriented shapeless qmm graph.
  • Add a sidecar-aware quantized linear C++ helper so standalone native-NVFP4 UnifiedLinear projections can preserve apply_global_scale semantics through one C++ boundary, with MLXCEL_DISABLE_FUSED_GLOBAL_SCALE still restoring the op-at-a-time rollback.
  • Document the 512-16384 token M1 Ultra sweep and update the NVFP4 environment-variable semantics for the new native default.

Validation scope

Issue #705 follows the #694 / #702 native-vs-affine Metal comparison on the local models/gemma-4-31b-it-nvfp4 ModelOpt NVFP4 checkpoint. The earlier issue wording that named Llama 4 Scout has been corrected in the issue body; this PR's full sweep uses the same local Gemma 4 ModelOpt NVFP4 validation target rather than a reduced-size substitute.

Benchmark summary

Prompt tokens Affine prefill tok/s Native prefill tok/s Native prefill gap Native decode speedup Peak memory delta Load speedup
512 123.38 118.49 -4.0% 2.67x -3.27 GB 2.70x
1024 126.71 117.46 -7.3% 2.68x -4.44 GB 2.68x
2048 126.50 117.12 -7.4% 2.65x -7.66 GB 2.67x
4096 118.07 112.88 -4.4% 2.75x -10.15 GB 2.66x
8192 116.88 111.39 -4.7% 2.64x -10.61 GB 2.72x
16384 115.86 108.10 -6.7% 2.51x -12.32 GB 2.71x

The prefill gap is reduced on the mid-length cases that previously showed the largest regression, but it is not eliminated; native remains about 4-7% slower than affine for prefill on this M1 Ultra run. The default switch is based on the overall trade-off requested in #705: native load is about 2.7x faster, decode is 2.5-2.75x faster, and peak memory is 3.3-12.3 GB lower across the sweep. The dense-affine rollback remains available for prefill-sensitive A/B runs.

Activation-path follow-up

Because native prefill still trails affine, this PR also rechecked the #701 path choice on the current Metal/MLX 0.32.1 baseline. At 2048 prompt tokens, the default shape-specific scaled MLP measured 116.10 tok/s, the op-at-a-time sidecar path with compiled GeGLU activation (MLXCEL_DISABLE_FUSED_GLOBAL_SCALE=1) measured 116.11 tok/s, and the C++ eager fold (MLXCEL_COMPILED_QGELU_MLP=0) measured 113.35 tok/s. At 8192 prompt tokens, the same paths measured 110.66, 109.99, and 108.10 tok/s respectively. This does not support switching native prefill back to the compiled-activation fallback; the #701 eager-fold warning still holds on this Metal baseline.

Test plan

  • cargo test --release -p mlxcel-core quantized_linear_forward_global_scale_matches_manual_sidecar_path -- --nocapture
  • cargo test --release -p mlxcel-core compiled_qgelu_mlp_global_scale -- --nocapture
  • cargo test --release -p mlxcel gemma4::quant_scheme_tests::dense_mlp_scaled_fused_path_eligible -- --nocapture
  • cargo test --release -p mlxcel sanitize::tests::nvfp4_repack_strategy_matrix -- --nocapture
  • cargo test --release -p mlxcel load_and_sanitize_weights_repacks_nvfp4_gemma4_checkpoint -- --nocapture
  • cargo test --release -p mlxcel load_and_sanitize_weights_dense_repack_env_keeps_non_cuda_affine_rollback -- --nocapture
  • cargo build --release --bin mlxcel-bench-decode
  • git diff --check
  • python3 scripts/ci/check_cross_repo_refs.py
  • mlxcel-bench-decode native-default vs MLXCEL_NVFP4_DENSE_REPACK=1 sweep at 512, 1024, 2048, 4096, 8192, and 16384 prompt tokens
  • mlxcel-bench-decode native default vs MLXCEL_DISABLE_FUSED_GLOBAL_SCALE=1 vs MLXCEL_COMPILED_QGELU_MLP=0 at 2048 and 8192 prompt tokens

Closes #705

Use the direct ModelOpt-triplet to MLX native NVFP4 transcode by default on non-CUDA builds, keeping MLXCEL_NVFP4_DENSE_REPACK=1 as the dense-affine rollback path and preserving MLXCEL_NVFP4_NATIVE_REPACK=1 as the dense-native selector when both overrides are set.

Reduce the native prefill dispatch overhead by routing sidecar-carrying Gemma 4 dense MLP prefill through a shape-specific scaled compiled graph for the native group_size=16/bits=4/nvfp4 layout, and add a sidecar-aware quantized linear helper that folds qmm + global scale + dense bias through one C++ boundary while preserving apply_global_scale semantics. MLXCEL_DISABLE_FUSED_GLOBAL_SCALE still restores the op-at-a-time sidecar path.

Validation:

- cargo test --release -p mlxcel-core quantized_linear_forward_global_scale_matches_manual_sidecar_path -- --nocapture
- cargo test --release -p mlxcel-core compiled_qgelu_mlp_global_scale -- --nocapture
- cargo test --release -p mlxcel gemma4::quant_scheme_tests::dense_mlp_scaled_fused_path_eligible -- --nocapture
- cargo test --release -p mlxcel sanitize::tests::nvfp4_repack_strategy_matrix -- --nocapture
- cargo build --release --bin mlxcel-bench-decode
- git diff --check
- python3 scripts/ci/check_cross_repo_refs.py
- mlxcel-bench-decode native-default vs MLXCEL_NVFP4_DENSE_REPACK=1 sweep at 512, 1024, 2048, 4096, 8192, and 16384 prompt tokens

Closes #705
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:high High priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific area:benchmark Benchmark harness and performance measurement (bench_*.sh, /update-benchmarks) labels Jul 9, 2026
Update the Gemma 4 NVFP4 sanitize regression test so the default expectation now matches the issue #705 native-direct policy on every backend, and add an explicit non-CUDA rollback test that pins `MLXCEL_NVFP4_DENSE_REPACK=1` to the previous dense-affine route.

Document the corrected Gemma 4 ModelOpt NVFP4 validation scope and the focused activation-path A/B showing that the op-at-a-time compiled-GeGLU fallback is not faster than the current default while the C++ eager fold remains slower.

Refs #705
@inureyes inureyes added status:done Completed and removed status:review Under review labels Jul 9, 2026
@inureyes
inureyes merged commit c259d23 into main Jul 9, 2026
4 checks passed
@inureyes
inureyes deleted the update/issue-705-nvfp4-prefill branch July 9, 2026 05:19
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
## Summary
- Make direct ModelOpt-triplet to MLX native NVFP4 the default on Metal/non-CUDA, matching the CUDA default while keeping `MLXCEL_NVFP4_DENSE_REPACK=1` as the dense-affine rollback path.
- Add a native-NVFP4 prefill-friendly Gemma 4 scaled MLP path: multi-token group_size=16/bits=4/mode=nvfp4 now uses a shape-specific compiled graph that folds the `weight_scale_2` sidecars and GeGLU activation without using the decode-oriented shapeless qmm graph.
- Add a sidecar-aware quantized linear C++ helper so standalone native-NVFP4 `UnifiedLinear` projections can preserve `apply_global_scale` semantics through one C++ boundary, with `MLXCEL_DISABLE_FUSED_GLOBAL_SCALE` still restoring the op-at-a-time rollback.
- Document the 512-16384 token M1 Ultra sweep and update the NVFP4 environment-variable semantics for the new native default.

## Validation scope

Issue lablup#705 follows the lablup#694 / lablup#702 native-vs-affine Metal comparison on the local `models/gemma-4-31b-it-nvfp4` ModelOpt NVFP4 checkpoint. The earlier issue wording that named Llama 4 Scout has been corrected in the issue body; this PR's full sweep uses the same local Gemma 4 ModelOpt NVFP4 validation target rather than a reduced-size substitute.

## Benchmark summary
| Prompt tokens | Affine prefill tok/s | Native prefill tok/s | Native prefill gap | Native decode speedup | Peak memory delta | Load speedup |
|---:|---:|---:|---:|---:|---:|---:|
| 512 | 123.38 | 118.49 | -4.0% | 2.67x | -3.27 GB | 2.70x |
| 1024 | 126.71 | 117.46 | -7.3% | 2.68x | -4.44 GB | 2.68x |
| 2048 | 126.50 | 117.12 | -7.4% | 2.65x | -7.66 GB | 2.67x |
| 4096 | 118.07 | 112.88 | -4.4% | 2.75x | -10.15 GB | 2.66x |
| 8192 | 116.88 | 111.39 | -4.7% | 2.64x | -10.61 GB | 2.72x |
| 16384 | 115.86 | 108.10 | -6.7% | 2.51x | -12.32 GB | 2.71x |

The prefill gap is reduced on the mid-length cases that previously showed the largest regression, but it is not eliminated; native remains about 4-7% slower than affine for prefill on this M1 Ultra run. The default switch is based on the overall trade-off requested in lablup#705: native load is about 2.7x faster, decode is 2.5-2.75x faster, and peak memory is 3.3-12.3 GB lower across the sweep. The dense-affine rollback remains available for prefill-sensitive A/B runs.

## Activation-path follow-up

Because native prefill still trails affine, this PR also rechecked the lablup#701 path choice on the current Metal/MLX 0.32.1 baseline. At 2048 prompt tokens, the default shape-specific scaled MLP measured 116.10 tok/s, the op-at-a-time sidecar path with compiled GeGLU activation (`MLXCEL_DISABLE_FUSED_GLOBAL_SCALE=1`) measured 116.11 tok/s, and the C++ eager fold (`MLXCEL_COMPILED_QGELU_MLP=0`) measured 113.35 tok/s. At 8192 prompt tokens, the same paths measured 110.66, 109.99, and 108.10 tok/s respectively. This does not support switching native prefill back to the compiled-activation fallback; the lablup#701 eager-fold warning still holds on this Metal baseline.

## Test plan
- [x] `cargo test --release -p mlxcel-core quantized_linear_forward_global_scale_matches_manual_sidecar_path -- --nocapture`
- [x] `cargo test --release -p mlxcel-core compiled_qgelu_mlp_global_scale -- --nocapture`
- [x] `cargo test --release -p mlxcel gemma4::quant_scheme_tests::dense_mlp_scaled_fused_path_eligible -- --nocapture`
- [x] `cargo test --release -p mlxcel sanitize::tests::nvfp4_repack_strategy_matrix -- --nocapture`
- [x] `cargo test --release -p mlxcel load_and_sanitize_weights_repacks_nvfp4_gemma4_checkpoint -- --nocapture`
- [x] `cargo test --release -p mlxcel load_and_sanitize_weights_dense_repack_env_keeps_non_cuda_affine_rollback -- --nocapture`
- [x] `cargo build --release --bin mlxcel-bench-decode`
- [x] `git diff --check`
- [x] `python3 scripts/ci/check_cross_repo_refs.py`
- [x] `mlxcel-bench-decode` native-default vs `MLXCEL_NVFP4_DENSE_REPACK=1` sweep at 512, 1024, 2048, 4096, 8192, and 16384 prompt tokens
- [x] `mlxcel-bench-decode` native default vs `MLXCEL_DISABLE_FUSED_GLOBAL_SCALE=1` vs `MLXCEL_COMPILED_QGELU_MLP=0` at 2048 and 8192 prompt tokens

Closes lablup#705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:benchmark Benchmark harness and performance measurement (bench_*.sh, /update-benchmarks) area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) platform:macos macOS (Apple Silicon) specific priority:high High priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(metal): close native NVFP4 prefill gap

1 participant