[Models]【Hackathon 10th Spring No.47】MiniMax-M1 model reproduction - #7333
Closed
r-cloudforge wants to merge 1 commit into
Closed
[Models]【Hackathon 10th Spring No.47】MiniMax-M1 model reproduction#7333r-cloudforge wants to merge 1 commit into
r-cloudforge wants to merge 1 commit into
Conversation
|
Thanks for your contribution! |
This was referenced Apr 10, 2026
Author
|
@luotao1 请问方便 review 一下吗?谢谢! |
Author
|
与 yTPl 线程为同一建议。MiniMax-M1 config.json 的 |
r-cloudforge
marked this pull request as ready for review
April 14, 2026 18:19
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 AI Code Review |
2025-04-15
📋 Review 摘要
PR 概述:新增 MiniMax-M1 混合注意力 MoE 模型支持(70 层线性注意力 + 10 层全注意力),包含 Lightning Attention Triton kernels 实现。
变更范围:model_executor/models/、model_executor/ops/triton_ops/、layers/、docs/、tests/
影响面 Tag:[Models] [OP]
📝 PR 规范检查
PR 标题和描述均符合规范,无需修改。
问题
未发现阻塞性问题。
总体评价
整体质量优秀,PR 实现了复杂的混合注意力架构(Linear Attention + Full Attention)和 6 种量化类型的 MoE 支持,代码结构清晰,测试覆盖全面(30/30 单元测试通过),文档完善。
亮点:
- ✅ 模型注册规范:双架构名注册(
MiniMaxM1ForCausalLM+MiniMaxText01ForCausalLM) - ✅ 组件复用正确:复用
FusedMoE、RMSNorm、SiluAndMul、QKVParallelLinear等现有组件 - ✅ RoPE 扩展合理:通过
architecture.startswith("MiniMaxM1")复用QwenRotaryEmbedding - ✅ Lightning Attention 实现:726 行 Triton kernels 正确适配 PaddlePaddle,包含 4 步分解算法
- ✅ DeepNorm 正确实现:postnorm 代码路径正确,返回
None作为 residual - ✅ 测试覆盖全面:30 个单元测试 + GPU 集成测试,包括纯 Python 参考算法验证
- ✅ 文档完善:中英文最佳实践文档和支持模型列表更新
已知限制(作者已在 PR 描述中说明):
- KV history 使用实例变量存储(
_kv_history),生产环境需迁移至 slot-based cache(TODO 已标注)
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.
Motivation
为 FastDeploy 增加部署 MiniMaxAI/MiniMax-M1-40k 系列模型的能力。
This PR adds support for deploying the MiniMax-M1 (456B MoE, 45.9B active) model family in FastDeploy, as required by Hackathon 10th Spring No.47.
MiniMax-M1 is a hybrid-attention Mixture-of-Experts LLM with:
MiniMaxM1ForCausalLMandMiniMaxText01ForCausalLMDesign document: community#1315
Reference approved RFC: community#1156 (@NKNaN)
Modifications
Model Code (
fastdeploy/model_executor/models/minimax_m1.py, 826 lines)9 classes implementing the full model:
MiniMaxM1MLP: Gate/up merged projection with SiLU activationMiniMaxM1MoE: FusedMoE with 32 experts, top-2 routing, renormalize=True, quantization-awareweight_key_map(w4a8, w4afp8 static/dynamic, tensor_wise_fp8, block_wise_fp8)MiniMaxM1FullAttention: Standard GQA with RoPE, used in 10 out of 80 layersMiniMaxM1LinearAttention: Lightning attention with SiLU-gated QKV, output_gate (sigmoid), RMSNorm, persistent KV state history. Forward: SiLU(QKV) → lightning_attn → RMSNorm → sigmoid(gate) × hidden → out_projMiniMaxM1DecoderLayer: Dispatches to linear/full attention based onattn_type_list, DeepNorm scaling with separate alpha/beta per attention type, postnorm supportMiniMaxM1Model: Full transformer with embedding and final RMSNormMiniMaxM1ForCausalLM: Causal LM wrapper with dual weight loading:set_state_dict(v0 loader): HF key preprocessing (w1→gate_proj, w3→up_proj, w2→down_proj, q/k/v→qkv_proj concatenation)load_weights(v1 loader): stacked_params_mapping + FusedMoE.make_expert_params_mappingMiniMaxM1PretrainedModel: Tensor parallel column/row split mappingsLightning Attention Kernels (
fastdeploy/model_executor/ops/triton_ops/lightning_attn.py, 726 lines)Triton kernels for O(n) linear attention with exponential decay:
_fwd_diag_kernel: Intra-block causal attention with exponential decay masking_fwd_kv_parallel+_fwd_kv_reduce: Inter-block KV state accumulation with block-level decay and prefix-sum reduction_fwd_none_diag_kernel: Non-diagonal block attention combining with diagonal results_linear_attn_decode_kernel: Single-token decode with slot-based KV cache updatelightning_attention(): Python wrapper dispatching to Triton with automatic block size, dtype management, and KV history persistenceDocumentation
docs/best_practices/MiniMax-M1.md+docs/zh/best_practices/MiniMax-M1.md: Bilingual usage guide with deployment examplesdocs/supported_models.md+docs/zh/supported_models.md: Added MiniMax-M1 to LLM model tableEngineering Highlights
This is the most architecturally complex model reproduction in this batch — the only FastDeploy model mixing two fundamentally different attention mechanisms within a single architecture:
Hybrid Attention Dispatch: The decoder layer dynamically dispatches to
MiniMaxM1LinearAttention(O(n) with persistent KV state history) orMiniMaxM1Attention(standard GQA with RoPE) per layer. This requires two completely different forward paths, KV cache strategies, and weight structures within one model.Lightning Attention Triton Adaptation (726 lines): Adapted from the Lightning Attention paper algorithm and vLLM reference to PaddlePaddle's Triton integration:
enable_compat_on_triton_kernelfor PaddlePaddle↔Triton compatibility_linear_attn_decode_kernel) with slot-based KV cache for batched inference — not present in upstream referencespaddle.empty,paddle.concat,.contiguous(), stride computation)DeepNorm Dual-Branch Scaling: Separate alpha/beta coefficients for linear vs full attention layers, with correct postnorm residual stream handling (residual carries normed output, differs from standard pre-norm).
6-Variant Quantization MoE:
weight_key_mapconstruction handles unquantized, w4a8, tensor_wise_fp8, block_wise_fp8, w4afp8-static, and w4afp8-dynamic — each with different key patterns for weight, scale, and activation tensors.Dual Weight Loader: Both v0 (
set_state_dict— full dict with q/k/v→qkv_proj concatenation, w1/w2/w3→gate/up/down expert remapping) and v1 (load_weights— streaming iterator viaFusedMoE.make_expert_params_mapping).Design Decisions
MiniMaxText01LinearAttentionreference, adapted for Paddleblock_sparse_moeattribute name matches HF config convention (notmlp)Usage or Command
See docs/best_practices/MiniMax-M1.md for full deployment guide.
Accuracy Tests
Unit Tests (30/30 passed — CI verified on H20 GPU)
tests/model_executor/test_minimax_m1.py(528 lines, 6 classes, 30 tests)TestBuildAttnTypeList(4 tests): 80-layer attention type dispatch validation, edge cases (short model, single layer, all-linear)TestBuildSlopeTensor(4 tests): Exponential decay slopes for power-of-2 and non-power-of-2 head counts, 64-head validation, positivity invariantTestModelRegistration(5 tests): Dual architecture registration (MiniMaxM1ForCausalLM+MiniMaxText01ForCausalLM), class identity, name method, pretrained nameTestDecoderLayerConstruction(9 tests): Linear/full attention dispatch, MoE vs dense MLP, postnorm config, fallback attention type, quantization weight_key_map (default/w4a8/w4afp8-dynamic)TestDecoderLayerForward(4 tests): Forward shape validation, DeepNorm scaling, postnorm code pathTestLightningAttentionPurePython(4 tests): Reference NumPy implementation, multi-token causal, KV history persistence, multi-head independenceCI Results (commit a76cb23)
28/30 checks passed — 2 failures are known infrastructure issues, unrelated to this PR:
run_tests_with_coveragetest_hopper_ll_precision.py— IBGDA transport init failure (nvshmemi_transport_init:275, exit code -6). Same test also fails on merged PRs #7087, #7088. Our 30/30 MiniMax-M1 tests passed (344 total, 343 passed, 1 unrelated failure).CI_HPUAttributeError: module 'paddle' has no attribute 'enable_compat'. Known flaky — also fails on merged PRs #7087, #7088.All other checks green: Pre Commit, Check PR Template, base_tests, run_ce_cases, stable_tests, 4-cards tests, logprob tests, iluvatar tests, XPU build + 4/8-card tests, FD-Build, CLA, diff_coverage_report.
Pre-commit Validation
All hooks passing: black, isort, flake8, ruff, clang-format, merge conflict check, trailing whitespace, large file check.
Checklist
minimax_m1.py, 826 lines) — 9 classes with full weight loading + quantization supportlightning_attn.py, 726 lines) — O(n) linear attentionset_state_dict) and v1 (load_weights) loader paths implementedMiniMaxM1ForCausalLM+MiniMaxText01ForCausalLMCompanion PR: #7347 — integration tests with multi-GPU validation script (≥3 GPUs + model weights)