Skip to content

fix(cuda/quant): repair nvfp4 execution path on CUDA #630

Closed

Description

@inureyes

Part of #623. Depends on #625: confirm the collapse persists on the upgraded pin before starting.

Context

nvfp4 checkpoints are effectively broken on CUDA: gemma-4-31b-it-nvfp4 decodes at 0.9 tok/s on GB10 vs 8.9 tok/s on Metal M1 Ultra, and prefills at 16.3 vs 38.9 (benchmarks/cuda_gb10_2026-06-17.csv). The GB10 roofline for a ~17 GB model is ~15 tok/s, so 0.9 tok/s means the path is not doing a quantized GEMV at all; the signature is a per-token dequantize-then-dense-matmul fallback.

Upstream context: nvfp4/mxfp8 support landed Metal/CPU-first in MLX (v0.30.x, "Metal/CPU nvfp4 and mxfp8"). The CUDA quantized dispatch in our overlay (src/lib/mlx-cpp/patches/mlx/backend/cuda/quantized/quantized.cpp:53-113) selects qmm/qmv kernels for affine quantization; nvfp4 (fp4 values, fp8 block scales) likely has no CUDA kernel and falls back. Ironically Blackwell (GB10, sm_121) has native fp4 tensor-core support, so long-term nvfp4 should be a CUDA strength, not a gap.

Scope

Make nvfp4 models run at a sane fraction of roofline on CUDA. Full tensor-core fp4 exploitation belongs to #637; this issue is about eliminating the pathological fallback.

Implementation plan

  1. Reproduce and characterize on GB10: MLXCEL_PROFILE_PIPELINE_DETAIL=1 decode run plus an nsys trace on gemma-4-31b-it-nvfp4. Confirm where the time goes (expect: dequantize kernels plus f16 GEMM every step, possibly re-dequantizing weights each token because the dequantized tensors are not retained).
  2. Check the current upstream (post pin-upgrade) for CUDA nvfp4/mxfp8 qmv/qmm kernels; the quantized-kernel area was rewritten to JIT compilation in feat: Integrate mlxcel prefill and decode workers with llm-d #3706/fix(tokenizer): build a tokenizer from GPT-2 slow files #3576, which may include mode coverage. If upstream has it, wire/verify and measure; done.
  3. If upstream lacks it, pick the pragmatic path:
    a. Preferred: implement an fp4-GEMV (fp_qmv) variant handling nvfp4 layout (16-value blocks, fp8-e4m3 scales) in our overlay patches/mlx/backend/cuda/quantized/qmm/qmv.cu, following the existing fp_qmv structure. Decode is GEMV-shaped and bandwidth-bound, so even a scalar-core implementation should approach roofline (~15 tok/s for the 31B model).
    b. Stopgap acceptable as a first PR: load-time repack nvfp4 -> affine 4-bit (gs=16 or 32) on CUDA with a clear log line, documenting the numerical delta on a perplexity spot check. This trades exactness for a 10x+ speedup and can ship while (a) is developed.
  4. Prefill: after the decode fix, measure prefill at 2048 tokens; if still degenerate, extend the same mode coverage to the qmm (matrix) path or route prefill through a one-time dequantized GEMM per layer (weights dequantized once per forward, not per token).
  5. Parity: greedy continuation vs the Metal output for the same checkpoint (tolerance: identical tokens for >= 38 of 40 steps, given fp4 accumulation-order differences).

Acceptance criteria

  • gemma-4-31b-it-nvfp4 decode >= 9 tok/s on GB10 (Metal M1 Ultra parity; roofline is ~15).
  • Prefill at 2048-token prompt >= 39 tok/s (Metal parity).
  • The chosen mechanism documented (upstream adoption / new kernel / repack stopgap) in a docs/benchmark_results/ note with the nsys before/after.
  • Any other nvfp4/mxfp8 checkpoint in the local model set spot-checked (at minimum gpt-oss-20b-mxfp4, which is mxfp8-family and currently at 126 vs 282 prefill).
  • Parity check passes; cargo test --features cuda passes.

Validation

cargo build --release --features cuda
MLXCEL_PROFILE_PIPELINE_DETAIL=1 ./target/release/mlxcel-bench-decode --model ./models/gemma-4-31b-it-nvfp4 --max-tokens 32
nsys profile -o nvfp4_decode ./target/release/mlxcel-bench-decode --model ./models/gemma-4-31b-it-nvfp4 --max-tokens 8

References

  • Dispatch: src/lib/mlx-cpp/patches/mlx/backend/cuda/quantized/quantized.cpp:53-113; kernels patches/.../qmm/qmv.cu.
  • Explicit dequantize fallback call sites in models: src/models/deepseek_v3.rs:1137, src/models/kimi_linear.rs:1279 (pattern to look for).
  • Baseline: benchmarks/cuda_gb10_2026-06-17.csv (gemma-4-31b-it-nvfp4 row).

Activity

  1. added
    type:bug
    status:ready
    priority:medium
    area:core
    on Jul 2, 2026
  2. inureyes commented on Jul 2, 2026

    @inureyes
    MemberAuthor

    Phase 1 (#625) triage update (see docs/benchmark_results/mlx-pin-upgrade-2026-07-03.md, PR #642).

    The gemma-4-31b-it-nvfp4 outlier that motivated this issue (0.89 tok/s decode) is a NVIDIA ModelOpt-packed checkpoint (config.quantization_config.quant_method = "modelopt"), an external format mlxcel intentionally rejects. The baseline number was degenerate output on an unsupported model; mlxcel now errors cleanly instead of running it. Separately, the genuine MLX-native nvfp4 dequant bug (F16 block-scales misparsed as zeros on CUDA) was fixed in #625 (src/models/sanitize.rs), and the sibling block-float format mxfp4 is healthy on GB10 (gpt-oss-20b-mxfp4: 78 decode / 127 prefill tok/s).

    Re-scoping this issue accordingly (still open): validate the MLX-native nvfp4/mxfp8 dequant path end-to-end on a genuine MLX-native nvfp4 checkpoint (none is available in the local model set, so acquiring/exporting one is part of the work), add a dequant guard/regression test, and document that ModelOpt/AWQ/GPTQ-packed checkpoints are out of scope and must be re-exported to MLX affine/block-float first.

  3. added
    status:in-progress
    and removed
    status:ready
    on Jul 8, 2026
  4. inureyes commented on Jul 8, 2026

    @inureyes
    MemberAuthor

    Opened PR #692 for the ModelOpt NVFP4 CUDA repair path:

    #692

    Status:

    • Gemma 4 local ModelOpt NVFP4 checkpoints now load by repacking {weight, weight_scale, weight_scale_2} triplets to MLX affine 4-bit tensors at load time.
    • 2048-token prefill now exceeds the target: 116.44 tok/s.
    • GPT-OSS MXFP4 control remains healthy: 70.23 decode tok/s.
    • Decode improves from the historical 0.89 tok/s baseline to 4.48 tok/s on the comparable short prompt, but still misses the requested 9 tok/s target.

    The PR uses Refs #630 rather than a closing keyword because the decode target is not yet satisfied. The remaining gap appears structural for the local checkpoint: it is about 31 GB and keeps many non-MLP tensors dense, while the fully affine Gemma 4 31B IT 4-bit reference checkpoint is about 18 GB and measures 8.54 decode tok/s on the same GB10 line.

  5. inureyes commented on Jul 8, 2026

    @inureyes
    MemberAuthor

    PR #692 has been updated to keep this repair CUDA-targeted and to use MLX native NVFP4 for the CUDA repack path. The final GB10 measurements are 75.36 prefill / 5.24 decode tok/s on the short prompt and 392.71 prefill / 5.42 decode tok/s at 2048 prompt tokens. This clears the prefill side by a wide margin but still does not meet the original 9 tok/s decode target, so the PR continues to use Refs #630 rather than a closing keyword.

    Follow-up work is split as discussed: #693 tracks direct ModelOpt triplet -> MLX native NVFP4 transcode with a preserved global-scale sidecar for weight_scale_2, and #694 tracks Apple Silicon native-path enablement after #693 exists and can be benchmarked on Metal.

  6. added
    status:done
    and removed
    status:in-progress
    on Jul 8, 2026
  7. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corepriority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions