Repository navigation
perf: default Metal NVFP4 to native transcode (#705) - #706
Merged
Merged
Conversation
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
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
4 tasks
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MLXCEL_NVFP4_DENSE_REPACK=1as the dense-affine rollback path.weight_scale_2sidecars and GeGLU activation without using the decode-oriented shapeless qmm graph.UnifiedLinearprojections can preserveapply_global_scalesemantics through one C++ boundary, withMLXCEL_DISABLE_FUSED_GLOBAL_SCALEstill restoring the op-at-a-time rollback.Validation scope
Issue #705 follows the #694 / #702 native-vs-affine Metal comparison on the local
models/gemma-4-31b-it-nvfp4ModelOpt 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
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 -- --nocapturecargo test --release -p mlxcel-core compiled_qgelu_mlp_global_scale -- --nocapturecargo test --release -p mlxcel gemma4::quant_scheme_tests::dense_mlp_scaled_fused_path_eligible -- --nocapturecargo test --release -p mlxcel sanitize::tests::nvfp4_repack_strategy_matrix -- --nocapturecargo test --release -p mlxcel load_and_sanitize_weights_repacks_nvfp4_gemma4_checkpoint -- --nocapturecargo test --release -p mlxcel load_and_sanitize_weights_dense_repack_env_keeps_non_cuda_affine_rollback -- --nocapturecargo build --release --bin mlxcel-bench-decodegit diff --checkpython3 scripts/ci/check_cross_repo_refs.pymlxcel-bench-decodenative-default vsMLXCEL_NVFP4_DENSE_REPACK=1sweep at 512, 1024, 2048, 4096, 8192, and 16384 prompt tokensmlxcel-bench-decodenative default vsMLXCEL_DISABLE_FUSED_GLOBAL_SCALE=1vsMLXCEL_COMPILED_QGELU_MLP=0at 2048 and 8192 prompt tokensCloses #705