Repository navigation
feat(quant): load ModelOpt MIXED_PRECISION FP8 layers as mxfp8 - #3678
Merged
Merged
Conversation
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
force-pushed
the
feature/issue-3201-modelopt-mixed-precision
branch
from
December 2, 2026 03:01
fc9c572 to
20546ad
Compare
inureyes
marked this pull request as ready for review
December 2, 2026 03:01
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
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.
ModelOpt
MIXED_PRECISIONcheckpoints such asnvidia/Qwen3.6-35B-A3B-NVFP4mix NVFP4 linears with per-tensor FP8 linears.is_modelopt_nvfp4rejected the FP8 entries andfp8_ctrefused 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 consultGpuBackendKind::quant_mode_support, so a backend that reportsConvertToorUnsupportedfails by name instead of aborting at the first forward pass.Changes:
mixed_precision_layersaccepts per-tensorFP8, classifies the declaration, and carriesquantized_layersthrough thehf_quant_config.jsonsynthesis.weight_scale, noweight_scale_2), so per-expertexpertsentries and renamed VLM keys are covered. It reconstructs one tensor at a time through the shared FP8 decoder and quantizes tomxfp8with an eval per tensor.fp8_ctaccepts compressed-tensorsstrategy: "tensor"with a scalar scale and shares the reconstruction and quantization helpers with the ModelOpt route.UnifiedLinear::from_weightsreads 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.SwitchLinearalready worked this way.Validation on
PhaniPendela/Qwen3.5-4B-W4A16-NVFP4(104 FP8 and 97 NVFP4 linears, Apple M1 Ultra):mlxcel generateruns it. An independent numpy E4M3 decode of the same safetensors matches the producedmxfp8planes 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%.MLXCEL_NVFP4_DENSE_REPACK=1 MLXCEL_NVFP4_NATIVE_REPACK=1), which exercises themxfp8FP8 planes through the whole model.nvfp4kernel overflow, not this change: with the FP8 half bypassed entirely (MLXCEL_FP8_DENSE_DEBUGin a local debug build),quantized_matmulon the checkpoint's own planes still returnsinffor 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.