feat(models): load the original MiniMax-M3.1-Flash mxfp8+NVFP4 export

Đang mở
#3,238 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
8/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
huggingface, rust
Lĩnh vực
machine-learning

Hướng nghiên cứu

Start with the loader entry points: load_minimax_m3_vl in src/loading/vlm_minimax_m3_vl.rs and MiniMaxM3Model::load with sanitize_weights in src/models/minimax_m3.rs, then the NVFP4 pass in src/models/sanitize.rs. The new prepare_original_checkpoint step goes in src/models/minimax_m3_checkpoint.rs, with tests in src/models/minimax_m3_checkpoint_tests.rs. Done means those tests pass, the router and e_score_correction_bias stay float32, and a logit trace matches the mlx-lm reference, which needs a 256 GB host.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

arch:moe area:models modelsize:xlarge modeltype:vlm priority:medium status:ready type:enhancement

Summary

mlxcel cannot load MiniMax's own release of MiniMax M3.1, MiniMaxAI/MiniMax-M3.1-Flash (model_type: minimax_m3_vl, about 224 GB). The checkpoint mixes MXFP8 (weight_scale_inv E8M0 group scales) for attention, the indexer and the shared expert, compressed-tensors NVFP4 for the routed experts, and an unquantized float32 router. The earlier expectation that #3169's compressed-tensors NVFP4 pass covers it does not hold, as traced below. This issue makes the original checkpoint load and run through both the text and VLM loaders, with the router kept in float32.

Upstream reference

ml-explore/mlx-lm#2003 (59995f1), now at https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/minimax_m3_vl/checkpoint.py and https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/minimax_m3_vl/model.py:

  • MXFP8: for every {p}.weight_scale_inv, rename it to {p}.scales and view {p}.weight (e4m3 bytes) as uint32; that is MLX's native mxfp8 layout (group 32, 8 bits). The loader sets quantization = {group_size: 32, bits: 8, mode: "mxfp8"} for quant_method: "mxfp8" (https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/utils.py), and per-layer modes are inferred from the scales (infer_quant_config: uint8 scales at 4 bits / group 16 are nvfp4).
  • Compressed-tensors NVFP4 experts: {p}.weight_packed viewed as uint32 becomes {p}.weight, {p}.weight_scale becomes {p}.scales, {p}.global_scale = 2688 / weight_global_scale, input_global_scale is dropped; per-expert global_scale is stacked with the expert weights.
  • cast_predicate keeps e_score_correction_bias and block_sparse_moe.gate.* out of dtype casting: "Keep the router in float32 so expert selection matches the original."

What the checkpoint holds

Hub API and safetensors headers, 2026-11-23. The id MiniMaxAI/MiniMax-M3.1 returns 401 (not public); the public release is MiniMaxAI/MiniMax-M3.1-Flash, architecture MiniMaxM3SparseForConditionalGeneration, 89632 tensors in 48 shards.

  • quantization_config: {"quant_method": "mxfp8", "activation_scheme": "dynamic", "weight_block_size": [1, 32], "group_size": 16, "moe_quant_algo": "NVFP4", "moe_quant_format": "nvfp4-pack-quantized"} with ignored_layers covering every block_sparse_moe.gate, lm_head, model.embed_tokens, vision_tower, multi_modal_projector and patch_merge_mlp.
  • MXFP8 layers (self_attn.{q,k,v,o}_proj, self_attn.index_{q,k}_proj, block_sparse_moe.shared_experts.{gate,up,down}_proj): weight F8_E4M3 [out, in], weight_scale_inv U8 [out, in/32].
  • Routed experts block_sparse_moe.experts.N.{w1,w2,w3}: weight_packed U8 [out, in/2], weight_scale F8_E4M3 [out, in/16], weight_global_scale and input_global_scale F32 scalars.
  • Router block_sparse_moe.gate.weight F32 [128, 6144]; block_sparse_moe.e_score_correction_bias F32 [128]; norms, embeddings, lm_head and the vision tower BF16.

Current mlxcel behavior

  • src/models/detection.rs:959 routes minimax_m3_vl to load_minimax_m3_vl (src/loading/vlm_minimax_m3_vl.rs:43), which reads weights through load_vlm_weights_common (:70). Its finishing pass finish_vlm_weights_common (src/loading/vlm.rs:383) applies the bf16-to-f16 policy and apply_native_global_scale_flags_to_subset, but never calls sanitize_modelopt_nvfp4_weights (src/models/sanitize.rs:1264), so weight_packed / weight_global_scale are never rewritten on the VLM path.
  • On the text path (MiniMaxM3Model::load, src/models/minimax_m3.rs:292-301), load_text_weights runs the NVFP4 pass only when is_modelopt_nvfp4_text_checkpoint (sanitize.rs:1443) accepts the config, and compressed_tensors_nvfp4_as_modelopt (sanitize.rs:1362) requires quant_method: "compressed-tensors" with format: "nvfp4-pack-quantized". This checkpoint declares quant_method: "mxfp8" with moe_quant_format, so #3169's detection returns None on both paths.
  • Nothing in src/models/minimax_m3*.rs handles .weight_scale_inv. fp8_block (src/models/fp8_block.rs:148) is Qwen-specific and accepts only quant_method: "fp8" with 128x128 blocks.
  • ModelArgs::group_size() / bits() (src/models/minimax_m3_config.rs:202-211) default to 64 / 4 when there is no quantization block, so every MXFP8 UnifiedLinear would be read with the wrong group geometry even after a rename.
  • minimax_m3::sanitize_weights (src/models/minimax_m3.rs:359) is called by both loaders and only drops vision keys, rewrites the language_model. prefix and drops MTP layers.
  • The parts that already work: the router input is cast to float32 (src/models/minimax_m3_moe.rs:166); the router UnifiedLinear (:205) stays a regular f32 linear when the plane has no scales; per-expert NVFP4 global_scale sidecars stack into inv_global_scale (stack_individual_experts_with_count, src/models/switch_layers.rs:1441); the bf16-to-f16 conversion touches only bf16 tensors, so the F32 router and bias survive it. is_model_quantized (vlm.rs:475) reads only quantization, so the BF16 norms, embeddings, lm_head and vision tower convert to f16 on Apple Silicon; that matches the non-quantized-weight rule, and there are no bf16 quantization scales in this checkpoint for the "keep bf16 scales" rule to protect.

#3201 (ModelOpt MIXED_PRECISION) is a different declaration (quant_method: "modelopt", per-layer FP8 / NVFP4 with hf_quant_config.json) and does not cover this one; share helpers with it rather than duplicating them if it lands first.

Required changes

  1. New src/models/minimax_m3_checkpoint.rs (keeps minimax_m3.rs focused), with pub(crate) fn prepare_original_checkpoint(weights: &mut WeightMap, config: &Value) -> Result<bool, String> that is a no-op (Ok(false)) unless the config (top level or text_config) declares quant_method: "mxfp8". When it does:
    • Every {p}.weight_scale_inv with a U8 / F8_E4M3 weight beside it: move it to {p}.scales and mlxcel_core::view the weight as UINT32 ([out, in/4]). Reject an in not divisible by 32, a scale shape other than [out, in/32], or a weight_scale_inv that is not uint8 (that would be the 128x128 float layout fp8_block handles), with an error naming the key.
    • Run the existing sanitize_modelopt_nvfp4_weights(weights, Some(config)) so compressed_tensors_nvfp4_to_modelopt_triplets and the repack turn the expert triplets into native NVFP4 planes with global_scale sidecars. Drop input_global_scale. Do not write a second NVFP4 converter.
    • Leave block_sparse_moe.gate.* and e_score_correction_bias untouched (float32).
    • Layout facts this relies on (index of the checkpoint): layers 0 to 2 are dense (moe_layer_freq starts [0, 0, 0, 1, ...]) with MXFP8 mlp.{gate,up,down}_proj under mlp., not under block_sparse_moe, so the rename covers them and no dense-under-MoE remap is needed; the routed experts are named w1/w2/w3, which expert_proj_names (src/models/minimax_m3_moe.rs:77) maps to gate/down/up once experts.0.w1.weight exists, so the NVFP4 rewrite (which turns weight_packed into weight) must run before MoeBlock::from_weights; and the expert planes' mode comes from their packed shapes (src/models/switch_layers.rs:1177-1195, words per group 2 is nvfp4 at group 16), not from the 32 / 8 default.
  2. Config: when the declaration is detected, insert quantization: {"group_size": 32, "bits": 8, "mode": "mxfp8"} into the text config before ModelArgs is parsed (both MiniMaxM3Model::load and load_minimax_m3_vl:49-58), following fp8_block::merge_fp8_block_quantization's rule that an explicit quantization wins. Per-plane mode inference (infer_plane_quantization_mode, src/lib/mlxcel-core/src/layers.rs) then reads the NVFP4 expert planes as group-16 nvfp4 from their scales. Confirm gate_bits() (minimax_m3_config.rs:217) cannot quantize the scale-less router.
  3. Call prepare_original_checkpoint from both loaders immediately before sanitize_weights (after the vision tower has been built from the raw keys in the VLM loader, so the BF16 tower is unaffected). Update sanitize_weights's doc comment to say which loader calls what.
  4. Do not change is_model_quantized or the global bf16-to-f16 policy here; record the decision in the module doc.

Acceptance criteria

  • src/models/minimax_m3_checkpoint_tests.rs: mxfp8_weight_scale_inv_becomes_native_scales builds a tiny [64, 64] E4M3 plane with a U8 [64, 2] weight_scale_inv and asserts {p}.scales exists, the weight is UINT32 [64, 16], and a UnifiedLinear loaded from it with group 32 / 8 bits reports mode mxfp8 and matches a dense dequantized reference within 1e-3. Fails today (the key is unknown).
  • Same file: compressed_tensors_experts_load_under_mxfp8_declaration builds two experts.N.w1 NVFP4 triplets under a quant_method: "mxfp8" config and asserts they become stacked native NVFP4 planes with a two-element inv_global_scale, and that input_global_scale is gone. Fails today on both paths.
  • Same file: router_and_bias_stay_f32 writes a synthetic minimax_m3_vl directory to a temp dir (pattern of src/loading/vlm_mage_vl_tests.rs) holding an F32 router, an F32 bias and a BF16 norm, runs the same sequence load_minimax_m3_vl runs (load_vlm_weights_common, then prepare_original_checkpoint, then sanitize_weights), and asserts the router weight and e_score_correction_bias are float32 afterwards while the norm followed the bf16-to-f16 policy.
  • Same file: a config without quant_method: "mxfp8" leaves the weight map unchanged (mlx-community conversions keep loading byte-identically).
  • Wired into the real flow: mlxcel generate, mlxcel-server and the minimax_m3 text route load the original checkpoint; no flag is needed.

Validation

Checkpoint: MiniMaxAI/MiniMax-M3.1-Flash, about 224 GB on disk (206.6 G U8 elements, 10.6 G F8, 3.3 G BF16, 46 M F32), so it runs on the 256 GB+ host, not a 128 GB box. Download model-*.safetensors and the configs (the repo also carries a dspark/ drafter directory that is not needed).

make verify-test
cargo clippy --workspace --all-targets -- -D warnings
cargo fmt --check
./target/release/mlxcel generate -m models/MiniMax-M3.1-Flash -p "Explain the difference between TCP and UDP." -n 256
./target/release/mlxcel generate -m models/MiniMax-M3.1-Flash --image tests/fixtures/test_image_shapes.png -p "Describe the image." -n 128

Loading the original planes changes which arithmetic runs, so per CLAUDE.md "Changes that move the numbers" report a teacher-forced trace (examples/logit_trace.rs, scripts/compare_logit_traces.py --decided 2) against an mlx-lm main reference that loads the same original checkpoint (scripts/mlx_lm_logit_trace.py, same arguments as logit_trace); upstream's own check was that long generations match. Record peak RSS and decode tok/s.

Related

#3169 (compressed-tensors NVFP4 text checkpoints; detection does not fire here), #3201 (ModelOpt MIXED_PRECISION), #764 (MiniMax-M3-VL port), #2411 (compressed-tensors FP8). Upstream: ml-explore/mlx-lm#2003.

Ngôn ngữ chính
Rust
Star
505
Fork
81
Merge trung bình
14 giờ 2 phút
Pull request đã merge (30 ngày)
448

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của lablup/mlxcel

Tất cả issue của lablup/mlxcel

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.