Merged
fix(mc2): 补充A5非量化scales的窗口与CCL buffer核算 #15295
fix(mc2): 补充A5非量化scales的窗口与CCL buffer核算 #15295
Merged
zhang_xin_yuAdd on 11 days ago
zhang_xin_yu
zhang_xin_yu
11 days ago

描述

关联的Issue

测试

文档更新

类型标签

PR #15295 全量代码检视报告

  • PR: https://gitcode.com/cann/ops-transformer/pull/15295
  • 检视分支: docs/mc2-a5-nonquant-scales-win
  • 检视提交: b8c0ed98f(用户指定 5b5719750,该提交已被 amend 为 b8c0ed98f)
  • 检视基线: origin/master
  • 变更规模: 12 个文件,+307 / -65
  • 检视方式: 按首次检视,重新对照 host tiling、helper 实现和文档公式

一、结论

核心逻辑与 host 侧口径一致,未发现会导致 A5 窗口少算的功能性错误。主要剩余问题集中在测试覆盖、保守预留策略和文档同步,均不阻塞合入,但建议处理。

二、问题与建议

1. [P2] Python helper 新增逻辑没有单元测试

  • 文件: mc2/common/torch_extension/moe_distribute_buffer.py
  • 变更: 新增 A5 设备判定、world_size/num_moe_expert/local*ep 上限、scales 最大占位、combine 480 对齐、rank offset。
  • 风险: 这些分支直接影响 HCCL_BUFFSIZE,缺少测试时后续修改容易回归。
  • 建议: 增加 mock get_device_name() 的单测,覆盖 A3/A5、fullmesh_v1/v2、有无 scales、rank offset 边界。

2. [P2] A5 所有场景都按 scales 最大值预留,量化场景会偏大

  • 文件: mc2/common/torch_extension/moe_distribute_buffer.py:153-160
  • 现象: max_scale_bytes = hidden * 4 对所有 A5 调用生效;host 侧只在 NON_QUANT + scales 时增加该占位。
  • 影响: 不会导致越界,但 A5 量化场景会多申请 HCCL buffer。
  • 建议: 若这是有意的保守策略,在文档/注释中明确;否则增加 quant_mode/scales 参数区分。

3. [P3] 英文文档未同步

  • 文件: mc2/moe_distribute_dispatch_v2/docs/aclnnMoeDistributeDispatchV2_en.md、V3_en、V4_en,以及 combine V2/V3/V4 的 _en.md。
  • 现象: 中文文档已补充 480 对齐、fullmesh_v2 变体和 scales 说明,英文文档仍是旧公式。
  • 建议: 同步英文文档,或由英文文档维护流程跟进。

4. [P3][历史遗留] Dispatch V2 950 的 commAlg 参数约束仍写不支持

  • 文件: mc2/moe_distribute_dispatch_v2/docs/aclnnMoeDistributeDispatchV2.md:500
  • 现象: 同文档 950 HCCL 公式已包含 fullmesh_v1/fullmesh_v2,但参数约束仍写“commAlg 当前版本不支持”。
  • 说明: 该描述早于本 PR 存在,代码侧 A5 fullmesh_v2 支持可追到 49af78d0a(2026-02-03,MR !1234),不是本 PR 引入。
  • 建议: 单独提 PR 修正,避免本 PR 扩大范围。

5. [P3][文档清晰度] Combine 950 的 HCCL 公式使用了 commAlg,但参数约束写不支持

  • 文件: mc2/moe_distribute_combine_v2/docs/aclnnMoeDistributeCombineV2.md、V3、V4 的 950 段。
  • 现象: HCCL 公式按 dispatch 侧 commAlg 的 fullmesh_v1/v2 展开,但 combine 的 commAlg 参数约束写不支持。
  • 说明: combine 本身可能不使用该属性,公式描述的是共享 buffer 的 dispatch 侧需求,但读者容易误解。
  • 建议: 在公式处注明“此处的 commAlg 指 dispatch 侧配置”,或单独修正 combine 的参数约束。

三、核对通过项

  • A5 非量化 scales:helper 使用 max_scale_bytes = hidden * 4,与 host CalcA5DispatchTokenBytes 的上界一致。
  • dispatch token 对齐:fullmesh_v1 用 512、fullmesh_v2 用 480,与 host tokenDataAlign 一致。
  • combine 对齐:A5 使用 480,A3 使用 512,与 host combineDataAlign 一致。
  • rank offset:helper 增加 world_size * 1024,与 host EP_RANK_OFFSET_STEP=1024 一致。
  • A5 上限:world_size≤1024、num_moe_expert≤2048、local*ep≤4096,与 host 常量一致。
  • 文档 950 段已补齐 fullmesh_v1/v2 公式,并将 combine 项统一为 480Align512(2 * H)。
  • _is_a5_device() 已消除重复判定;注释与常量命名已整理。

四、验证建议

  1. 用 mock 设备名的 Python 单测对比 helper 返回值与 host tiling 的最小窗口要求。
  2. 覆盖 fullmesh_v1/fullmesh_v2、NON_QUANT + scales、量化无 scales 三类场景。
  3. 取 H=7168、较大 world_size 和 local_moe_expert_num 的边界组合,确认 rank offset 和 2MB 对齐没有少算。
  4. 修改文档后执行 pre-commit run --files ...。
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for zhang_xin_yu's contribution.)
zhang_xin_yuzhang_xin_yu
11 days ago Created pull request, commit 888d38b8
atomgit-bot
atomgit-bot
11 days ago Comments:

变更摘要

本 PR 针对 mc2/common/torch_extension/moe_distribute_buffer.py 中的 MoeDistributeBuffer 缓冲区核算逻辑,补充了 A5(Ascend950)平台非量化 scales 的窗口占用与 CCL buffer 核算。改动在原有 A3 参数上限的基础上按设备类型区分上限值,并在 Dispatch/Combine 的 token 尺寸计算中纳入 scales 数据长度与对齐规则。

主要改动

  • 接口参数扩展:MoeDistributeBuffer 相关核算方法的入参新增 scales_col 与 scales_type_size,用于将 scales 的列数与元素字节数纳入缓冲区估算。
  • 设备差异化参数上限:新增基于 torch_npu.npu.get_device_name() 判断是否含 Ascend950 的 is_a5 标识,并据此将 world_size、num_moe_expert、local_moe_expert_num * world_size 的上限分别放宽到 1024、2048、4096,非 A5 保持 768、1024、2048;相应 torch._check 校验与报错文案改为引用这些上限变量。
  • Dispatch 侧 token 长度核算:在 A5 场景下新增 dispatch_token_bytes,按 inline_align(token_align + scales_col * scales_type_size, ub_align) + 3 * 4 + 4 计算,并与原 token_actual_len 取较大值,使 scales 占用被计入窗口需求。
  • Combine 侧对齐规则调整:引入 combine_data_align(A5 取 full_mesh_data_align,否则取 win_addr_align),先按该对齐粒度对齐 hidden * max_out_dtype_size,再折算为 win_addr_align 的整数倍,从而影响 token_need_size_combine 及最终 minimum_buffer_size 的估算。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
11 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
11 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
*/*/README.md ✅ zzzzzzyf_, wang-minbo, 刘丹, macech (4/2) ✅ zzzzzzyf_ (1/1)
*/*/docs/acl*.md ✅ zzzzzzyf_, wang-minbo, 刘丹, macech (4/2) ✅ zzzzzzyf_ (1/1)
mc2 ✅ 刘丹, macech, wang-minbo (3/2) ✅ 刘丹, macech, wang-minbo (3/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

zhang_xin_yu, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
11 days ago Set zzg_code,monologue815,zhu-mingzhe71,jiang-lirui,Allan_Yu,yayahello,gitee-duhuiping,wang-minbo,claire8090,tgwsakiko_,liudan12,chenjunjian11,yangzeheng,liuboxi,gitcode-chenjiao,ryan_li,luobaiqing,zhong-zixin,Ww7012,mabing1118,dingwei002,macech,chuguowei,fengying555682,huangshuai59,adamlwang,zzzzzzyf_,caiwenwen,yanglu-1 as approver
50 messages are collapsed here。 See more
周云飞
周云飞Collaborators
10 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
10 days ago Added label: approved
CANN-robot
CANN-robotCollaborators
10 days ago Comments:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
Wwang-minboCollaborators
10 days ago Resolved final issue
CANN-robotCANN-robotCollaborators
10 days ago Merged pull request