Prhub

#49149 [Bugfix][MiniMax-M3] Fix token-major top-k buffer handling in Triton …

原始 PR 作者 lengrongfu 合并时间 2026-07-25 21:45 文件变更 2 提交数 4 评论 14 代码增减 +13 / -3

执行摘要

修复 MiniMax-M3 Triton 路径 top-k 缓冲区布局不一致

修复 issue #49147: MiniMax-M3 推理崩溃。根因是 NVIDIA 模型在 model.py:790topk_indices_buffer 设为 [token, head, topk] 布局,但共用 indexer 和 sparse_attention 仍按 [head, token, topk] 访问,导致数据错位。

值得精读,尤其是布局不一致的调试思路和平台隔离技巧。后续可考虑统一布局,消除平台特判。

讨论亮点

核心讨论围绕 AMD 兼容性:

  • yewentao256 提出转置是否会破坏 ROCm。
  • lengrongfu 指出 AMD 使用 [H,T,K] 布局,有两种方案:修改 AMD 模型或添加平台判断。最终采纳第二种,通过 current_platform.is_rocm() 隔离改动。
  • Fangzhou-Ai 建议将修复仅限 NVIDIA 平台,避免意外影响 AMD。
  • yewentao256 同意并批准 PR。

实现拆解

  1. sparse_attention.py:将 topk = layer.topk_indices_buffer 替换为平台判断:AMD 保持原布局;NVIDIA 通过 transpose(0, 1) 将 [token, head, topk] 转置为 [head, token, topk],供后续 decode/prefill 使用。
  2. indexer.py:类似地,在 indexer forward 中引入 buf_htk:若平台非 ROCm 且 buffer 非 None,则转置后再传给 decode 和 topk kernel,确保写入和读取布局一致。
  3. 平台隔离:通过 current_platform.is_rocm() 判断,仅 NVIDIA 路径执行转置,AMD 路径不受影响。
  4. 测试:新增 test_triton_indexer_mixed_batch_token_major_topk_buffer 单元测试验证混合批处理场景。
文件 模块 状态 重要度
vllm/models/minimax_m3/common/sparse_attention.py 模型层 modified 6.38
vllm/models/minimax_m3/common/indexer.py 模型层 modified 6.16
tests/kernels/attention/test_minimax_m3.py 测试 modified 4.0

关键符号

MiniMaxM3SparseTritonImpl.forward MiniMaxM3IndexerTritonImpl.forward

关键源码片段

vllm/models/minimax_m3/common/sparse_attention.py data-contract

核心修复:对 topk 缓冲区进行转置,对齐 NVIDIA 的 [T,H,K] 布局与 Triton 期望的 [H,T,K]。

# vllm/models/minimax_m3/common/sparse_attention.py
class MiniMaxM3SparseTritonImpl(MiniMaxM3SparseImpl):
    def forward(self, layer, query, kv_cache, output):
        ...
        topk_buffer = layer.topk_indices_buffer
        assert topk_buffer is not None
​
        # 平台隔离:AMD 保持 [H,T,K];NVIDIA 从 [T,H,K] 转置以对齐 Triton 期望
        topk = (
            topk_buffer
            if current_platform.is_rocm()
            else topk_buffer[:num_tokens].transpose(0, 1)
        )
        assert topk is not None
        ...
        # decode 和 prefill 使用已转置的 topk
        if main_md.num_decodes > 0:
            minimax_m3_sparse_attn_decode(
                q[:nd], kv_cache, topk[:, :nd, :], ...
            )
vllm/models/minimax_m3/common/indexer.py data-contract

辅助修复:在 indexer 中类似地转置缓冲区,确保写入和读取布局一致。

# vllm/models/minimax_m3/common/indexer.py
class MiniMaxM3IndexerTritonImpl(MiniMaxM3IndexerImpl):
    def forward(self, index_query):
        ...
        buf = self.topk_indices_buffer
        # 平台隔离:AMD 不转置,NVIDIA 从 [T,H,K] 转置为 [H,T,K]
        buf_htk = (
            buf
            if buf is None or current_platform.is_rocm()
            else buf.transpose(0, 1)
        )
        decode_topk = None
        prefill_topk = None
        if index_md.num_decodes > 0:
            decode_topk = minimax_m3_index_decode(
                ..., out=buf_htk, # 使用转置后缓冲区
            )
        if index_md.num_prefills > 0:
            prefill_topk = minimax_m3_index_topk(
                ..., out=buf_htk[:, nd:, :] if buf_htk is not None else None,
            )
        return decode_topk, prefill_topk

评论区精华

ROCm 兼容性风险 设计

yewentao256 询问转置是否会破坏 ROCm;lengrongfu 提出两种方案:修改 AMD 模型或添加平台判断;Fangzhou-Ai 建议仅限 NVIDIA 平台。

结论:采用平台判断:仅 NVIDIA 路径执行转置,AMD 路径保持不变。 · 已解决

测试必要性 测试

yewentao256 认为不需要专用单元测试,但 lengrongfu 仍添加了参数化测试。

结论:测试被保留,作为回归保护。 · 已解决

风险与影响

风险较低,因改动仅涉及布局转置,且通过 is_rocm() 显式隔离,AMD 路径不变。主要风险:

  • 未来若 AMD 也切换到 [T,H,K] 布局,需同步移除平台判断。
  • 测试覆盖仅限于单元测试,缺少端到端回归测试。

直接影响:修复 MiniMax-M3 在 NVIDIA GPU 上的推理崩溃(issue #49147)。
间接影响:无,因转置仅在 NVIDIA 路径生效,AMD 行为不变。
影响范围:使用 MiniMax-M3 模型的用户(NVIDIA 平台)。

平台特判需维护

关联 Issue

#49147 [Bug]: Minimax-M3 running crase

完整报告

参与讨论