Skip to content

feat(quant): load ModelOpt MIXED_PRECISION FP8 layers as mxfp8 - #3678

Merged
inureyes merged 2 commits into
mainfrom
feature/issue-3201-modelopt-mixed-precision
Dec 2, 2026
Merged

inureyes merged 2 commits into
mainfrom
feature/issue-3201-modelopt-mixed-precision

Conversation

@inureyes

@inureyes inureyes commented Dec 2, 2026

Copy link
Copy Markdown
Member

ModelOpt MIXED_PRECISION checkpoints such as nvidia/Qwen3.6-35B-A3B-NVFP4 mix NVFP4 linears with per-tensor FP8 linears. is_modelopt_nvfp4 rejected the FP8 entries and fp8_ct refused a tensor scale, so those checkpoints failed with "Unsupported packed weight" on the first attention projection.

The FP8 half is requantized to MLX mxfp8, not decoded to bf16 and not converted to NVFP4. It keeps the 8-bit tensor-core format and the e4m3 mantissa (NVFP4 keeps one mantissa bit), the e8m0 block multiplier is a power-of-two rescale, and it reuses the target and measured error bound the vendor block and compressed-tensors FP8 routes already ship. Decoding to bf16 would double the FP8 half's weight memory. NVFP4 layers stay on the existing triplet repack untouched, which is the native Blackwell path. Both halves now consult GpuBackendKind::quant_mode_support, so a backend that reports ConvertTo or Unsupported fails by name instead of aborting at the first forward pass.

Changes:

  • mixed_precision_layers accepts per-tensor FP8, classifies the declaration, and carries quantized_layers through the hf_quant_config.json synthesis.
  • The FP8 pass finds its tensors by layout (uint8 weight, scalar weight_scale, no weight_scale_2), so per-expert experts entries and renamed VLM keys are covered. It reconstructs one tensor at a time through the shared FP8 decoder and quantizes to mxfp8 with an eval per tensor.
  • fp8_ct accepts compressed-tensors strategy: "tensor" with a scalar scale and shares the reconstruction and quantization helpers with the ModelOpt route.
  • UnifiedLinear::from_weights reads a bias-free plane's mode, group size and bits from its packed-width to scale-group ratio, so one declared pair for the whole export no longer forces NVFP4 and MXFP8 planes onto the same algorithm. SwitchLinear already worked this way.

Validation on PhaniPendela/Qwen3.5-4B-W4A16-NVFP4 (104 FP8 and 97 NVFP4 linears, Apple M1 Ultra):

  • The checkpoint loads, and mlxcel generate runs it. An independent numpy E4M3 decode of the same safetensors matches the produced mxfp8 planes for the layer-0 linear-attention and layer-3 full-attention projections (7 tensors, 78,643,200 elements): every value is within half an E4M3 step of the exact reconstruction (worst 0.90x of that bound), with overall relative RMS 2.52% and worst-tensor relative RMS 3.30%.
  • Generation is coherent with the NVFP4 half on its existing dense-native route (MLXCEL_NVFP4_DENSE_REPACK=1 MLXCEL_NVFP4_NATIVE_REPACK=1), which exercises the mxfp8 FP8 planes through the whole model.
  • The default NVFP4 direct-transcode route degenerates to token 0 on this checkpoint. That is an existing Metal nvfp4 kernel overflow, not this change: with the FP8 half bypassed entirely (MLXCEL_FP8_DENSE_DEBUG in a local debug build), quantized_matmul on the checkpoint's own planes still returns inf for about a quarter of outputs at |x| <= 20, because the ModelOpt parameterization keeps block scales up to 448 and a 1e-4 per-tensor scale, and the f16 kernel multiplies before that scale is applied. Folding the per-tensor scale into the block scales makes the same checkpoint generate coherently on the default route. This PR leaves that NVFP4 route untouched and reports the overflow for a separate fix.

Closes #3201.

@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority status:done Completed labels Dec 2, 2026
inureyes added a commit that referenced this pull request Dec 2, 2026
ModelOpt MIXED_PRECISION exports mix NVFP4 linears with per-tensor FP8 linears. The detector rejected the FP8 entries and the compressed-tensors route refused a tensor scale, so nvidia/Qwen3.6-35B-A3B-NVFP4 and its siblings failed with "Unsupported packed weight" on the first attention projection.

Requantize the FP8 half to MLX mxfp8 rather than decoding it to bf16 or converting it to NVFP4: it keeps the 8-bit tensor-core format and the e4m3 mantissa, reuses the target and error bound of the existing FP8 routes, and does not double the FP8 half's memory. NVFP4 layers keep the existing triplet repack. Both halves consult GpuBackendKind::quant_mode_support.

The FP8 pass finds tensors by layout (uint8 weight, scalar weight_scale, no weight_scale_2), covering per-expert entries and renamed VLM keys, and reconstructs one tensor per eval. UnifiedLinear::from_weights now reads a bias-free plane's mode, group size and bits from its packed-width to scale-group ratio so one declared pair no longer forces every plane onto one algorithm.

Validated on PhaniPendela/Qwen3.5-4B-W4A16-NVFP4 (104 FP8 and 97 NVFP4 linears) with a numpy dequantization of the same safetensors as the independent reference.

Closes #3201.
@inureyes
inureyes force-pushed the feature/issue-3201-modelopt-mixed-precision branch from fc9c572 to 20546ad Compare December 2, 2026 03:01
@inureyes
inureyes marked this pull request as ready for review December 2, 2026 03:01
@inureyes
inureyes merged commit 8a8b499 into main Dec 2, 2026
15 checks passed
@inureyes
inureyes deleted the feature/issue-3201-modelopt-mixed-precision branch December 2, 2026 03:01
inureyes added a commit that referenced this pull request Dec 2, 2026
#3678 added two MXFP8 requantization tests that evaluate MLX arrays without taking mlx_test_guard, and the new coverage scan flags both. They now take the guard as their first statement; both pass on GB10 under the CUDA build.

Refs #3660
inureyes added a commit that referenced this pull request Dec 2, 2026
Parallel `cargo test --features cuda --lib -- --test-threads=4` aborted in most runs with "invalid memory registered when calling <op>::eval_gpu". The cause is a race in MLX's debug-only CUDA allocation tracker: `move_to_unified_memory` and `move_to_device_memory` freed the old pointer before calling `swap_allocation`, so another thread could be handed that address and track it, and the late swap then moved the live entry away and its next `verify_graph` aborted. A new overlay of `mlx/backend/cuda/allocator.cpp` (base 5a48f9cc) retargets the tracker before the free. Release and test-fast builds define NDEBUG and never ran this code, which is why the merge gates did not see it.

MLX-evaluating lib tests now take `mlx_test_guard()` as their first statement, and `tests/mlx_test_guard_coverage.rs` gains a source scan that fails when a test reaches an eval or host readback before taking the guard. Helpers that took their own guard or the device lock were adjusted so nothing deadlocks. CI clippy now also lints examples (about one second of extra time).

Main broke several times while this was open, and the branch carries the fixes the verification needed: the diffusing_interval settings classification, LLaVA-OneVision in FAMILY_ORDER, two clippy lints, the WebUI string fixture and bundle, and guards on tests added by #3654, #3657 and #3678.

Validation: five consecutive 4-thread CUDA lib runs on GB10 passed (14080 passed, 0 failed each) where the base aborted; the guard scan, family_order, fmt, clippy with examples, the scripts/ci contract checks, the WebUI suite and contract check pass. OpenXLA feature compile fails with the same RUSTFLAGS quoting error as on main.

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

Labels

priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(quant): load ModelOpt MIXED_PRECISION (NVFP4 + FP8) checkpoints such as nvidia/Qwen3.6-35B-A3B-NVFP4

1 participant