Skip to content

perf(nvfp4): add opt-in native repack override for non-CUDA builds - #699

Merged
inureyes merged 3 commits into
mainfrom
perf/694-nvfp4-native-repack-optin
Jul 8, 2026
Merged

inureyes merged 3 commits into
mainfrom
perf/694-nvfp4-native-repack-optin

Conversation

@inureyes

@inureyes inureyes commented Jul 8, 2026

Copy link
Copy Markdown
Member

Summary

This PR implements the hardware-independent plumbing that issue #694's benchmark plan needs, so an Apple Silicon host can A/B test the direct NVFP4 transcode against the affine fallback without a code change. It does not run that benchmark and does not flip any default; this machine is GB10 Linux CUDA, not Apple Silicon, so both the M-series bench run and the default-flip decision remain explicitly out of scope here and stay open on #694.

What changed

  • src/models/sanitize.rs: the three cfg!(feature = "cuda") NVFP4 repack decision sites (the log label, the direct-transcode gate, and the dense-path quantize-mode choice) now all read from one pure function, nvfp4_repack_strategy(cuda_build, native_forced, dense_forced), so the three sites can never disagree.
  • Added MLXCEL_NVFP4_NATIVE_REPACK (same case-insensitive 1/true/on/yes parsing as the existing MLXCEL_NVFP4_DENSE_REPACK), which opts a non-CUDA build into the direct triplet transcode that CUDA already defaults to.
  • Precedence, documented on nvfp4_repack_strategy: MLXCEL_NVFP4_DENSE_REPACK wins over everything (forces the dense debug fallback on any build), otherwise MLXCEL_NVFP4_NATIVE_REPACK forces the direct transcode, otherwise the existing default applies (native on CUDA, affine off CUDA).
  • Added nvfp4_repack_strategy_matrix, exercising the full cuda-build x native-forced x dense-forced matrix (8 combinations, including both overrides forced at once, where dense wins the route) against the pure function, and nvfp4_native_repack_forced_matches_case_insensitively, matching the existing MLXCEL_NVFP4_DENSE_REPACK env-parsing test style.
  • docs/environment-variables.md: documented MLXCEL_NVFP4_NATIVE_REPACK next to MLXCEL_NVFP4_DENSE_REPACK with the precedence spelled out.
  • docs/benchmark_results/nvfp4-direct-transcode-gb10-2026-07-08.md: added an Apple Silicon A/B runbook section with the exact build and mlxcel-bench-decode commands from issue perf(metal): evaluate native NVFP4 path after direct transcode #694's benchmark plan (short-prompt and 2048-token runs, once with the affine default and once with MLXCEL_NVFP4_NATIVE_REPACK=1), what to record (cold-load wall time, MLX peak memory, prefill/decode tok/s, a 40+ token greedy parity spot-check), and the decision gate from the issue (enable native on Metal only if 2048 prefill improves at least 20% and decode regresses at most 5%).

Test plan

  • cargo check -p mlxcel --features cuda --lib
  • cargo test -p mlxcel --features cuda --lib nvfp4 (12 passed, including the two new tests)
  • cargo clippy -p mlxcel --features cuda --lib --tests -- -D warnings
  • cargo fmt -p mlxcel -- --check

Refs #694

Issue #694 asks whether Metal/non-CUDA should switch from the affine 4-bit fallback to the direct ModelOpt-triplet NVFP4 transcode that #693/#697 made the CUDA default. That decision needs an A/B benchmark on real M-series hardware, which is not available here, so this change only adds the plumbing the benchmark plan requires.

The three cfg!(feature = "cuda") decision sites in src/models/sanitize.rs (the log label, the direct-transcode gate, and the dense-path quantize-mode choice) are replaced by a single pure function, nvfp4_repack_strategy(cuda_build, native_forced, dense_forced), so the three sites can never disagree. A new env var, MLXCEL_NVFP4_NATIVE_REPACK (same truthy-value parsing as the existing MLXCEL_NVFP4_DENSE_REPACK), opts a non-CUDA build into the direct transcode. Precedence: MLXCEL_NVFP4_DENSE_REPACK wins over everything, otherwise MLXCEL_NVFP4_NATIVE_REPACK forces native, otherwise the existing default applies (native on CUDA, affine off CUDA).

Added nvfp4_repack_strategy_matrix, which exercises the full 2x2x2 build/native-forced/dense-forced matrix against the pure function, and nvfp4_native_repack_forced_matches_case_insensitively, matching the existing MLXCEL_NVFP4_DENSE_REPACK env-parsing test style.

docs/environment-variables.md documents MLXCEL_NVFP4_NATIVE_REPACK next to MLXCEL_NVFP4_DENSE_REPACK with the precedence spelled out. docs/benchmark_results/nvfp4-direct-transcode-gb10-2026-07-08.md gets an Apple Silicon A/B runbook section with the exact build and bench-decode commands from issue #694's benchmark plan, what to record, and the decision gate (2048 prefill improves >= 20% and decode regresses <= 5%).

Refs #694
@inureyes inureyes added type:performance Performance improvements priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers status:review Under review labels Jul 8, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Jul 8, 2026
@inureyes
inureyes merged commit 37b7e8f into main Jul 8, 2026
4 checks passed
@inureyes
inureyes deleted the perf/694-nvfp4-native-repack-optin branch July 8, 2026 14:49
@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
…ablup#699)

## Summary

This PR implements the hardware-independent plumbing that issue lablup#694's benchmark plan needs, so an Apple Silicon host can A/B test the direct NVFP4 transcode against the affine fallback without a code change. It does not run that benchmark and does not flip any default; this machine is GB10 Linux CUDA, not Apple Silicon, so both the M-series bench run and the default-flip decision remain explicitly out of scope here and stay open on lablup#694.

## What changed

- `src/models/sanitize.rs`: the three `cfg!(feature = "cuda")` NVFP4 repack decision sites (the log label, the direct-transcode gate, and the dense-path quantize-mode choice) now all read from one pure function, `nvfp4_repack_strategy(cuda_build, native_forced, dense_forced)`, so the three sites can never disagree.
- Added `MLXCEL_NVFP4_NATIVE_REPACK` (same case-insensitive 1/true/on/yes parsing as the existing `MLXCEL_NVFP4_DENSE_REPACK`), which opts a non-CUDA build into the direct triplet transcode that CUDA already defaults to.
- Precedence, documented on `nvfp4_repack_strategy`: `MLXCEL_NVFP4_DENSE_REPACK` wins over everything (forces the dense debug fallback on any build), otherwise `MLXCEL_NVFP4_NATIVE_REPACK` forces the direct transcode, otherwise the existing default applies (native on CUDA, affine off CUDA).
- Added `nvfp4_repack_strategy_matrix`, exercising the full cuda-build x native-forced x dense-forced matrix (8 combinations, including both overrides forced at once, where dense wins the route) against the pure function, and `nvfp4_native_repack_forced_matches_case_insensitively`, matching the existing `MLXCEL_NVFP4_DENSE_REPACK` env-parsing test style.
- `docs/environment-variables.md`: documented `MLXCEL_NVFP4_NATIVE_REPACK` next to `MLXCEL_NVFP4_DENSE_REPACK` with the precedence spelled out.
- `docs/benchmark_results/nvfp4-direct-transcode-gb10-2026-07-08.md`: added an Apple Silicon A/B runbook section with the exact build and `mlxcel-bench-decode` commands from issue lablup#694's benchmark plan (short-prompt and 2048-token runs, once with the affine default and once with `MLXCEL_NVFP4_NATIVE_REPACK=1`), what to record (cold-load wall time, MLX peak memory, prefill/decode tok/s, a 40+ token greedy parity spot-check), and the decision gate from the issue (enable native on Metal only if 2048 prefill improves at least 20% and decode regresses at most 5%).

## Test plan

- [x] `cargo check -p mlxcel --features cuda --lib`
- [x] `cargo test -p mlxcel --features cuda --lib nvfp4` (12 passed, including the two new tests)
- [x] `cargo clippy -p mlxcel --features cuda --lib --tests -- -D warnings`
- [x] `cargo fmt -p mlxcel -- --check`

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

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant