Merged
feat(indexing): Support A5 SIMT gather and index put #5404
bynshardAdd on Aug 11
feat(indexing): Support A5 SIMT gather and index put #5404
Merged
bynshardAdd on Aug 11
bynshard
bynshardCollaborators
Aug 11

gather indexput 支持的simt 实现,在离散场景下提升性能

当 Gather 的 source 位于 GM、indices 位于 UB 时,A5 Codegen 会选择 SIMT MGATHER 实现:

  1. 使用 SIMD 批量计算 Gather 的元素偏移。
  2. 使用 SIMT MGATHER 根据偏移直接从 GM source 读取数据。
  3. source 位于 UB 时继续使用原有实现。
  4. int64 indices 暂不使用 SIMT,回退到原有 Scalar 实现。

同时补充了 SIMT Kernel 的端到端识别与下发机制:

  1. Pass 识别需要 SIMT 的 Gather,并设置 requiresSimt 属性。
  2. 将 requiresSimt 信息传递到 Device Program 和编译流程。
  3. 为包含 SIMT 的 A5 Kernel 生成相应的 ELF metadata。
  4. Runtime 根据 Kernel 是否包含 SIMT,选择对应的 UB 配置。
  5. SIMT Kernel 配置 216 KB 动态 UB,并为 DCache 预留 32 KB。
  6. 纯 SIMD Kernel 保持原有 Runtime 下发方式和 UB 配置。
    此外,完善了 GM GatherElement 在内存类型推导、动态参数注册、动态 valid shape 处理和 OoO 调度中的支持。

Related Issue: #3162


更多描述信息见:[Commit Message 规范](../docs/zh/contribute/pull-request.md#commit-message)
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for bynshard's contribution.)
bynshardbynshardCollaborators
Aug 11 Created pull request, commit 108c466d
atomgit-bot
atomgit-bot
Aug 11 Comments:

变更摘要

此 PR 新增了 ScatterGm 操作,实现将数据从 UB(Unified Buffer)直接散列写入全局内存(GM)的功能。该操作支持 A5 架构(DAV_3510)的 SIMT 混合模式,利用硬件 MSCATTER 指令加速写入,并在 V220 架构上提供逐元素回退实现。同时引入了一套 A5 混合模式内核元数据机制:在源码中嵌入 SimtKernelMeta 结构体标记内核类型与 UB 大小,并在编译链接后对 ELF 目标文件中的 .ascend.meta.* 段进行补丁,确保运行时能正确识别混合模式内核。此外,为 Opcode 注册新增 requiresSimt 标志位,并在运行时任务配置中为 A5 平台设置 localMemorySize 为 216KB。

主要改动

  • 新增 OP_SCATTER_GM 操作码: 在 Opcode 枚举、OpcodeManager::RegisterInfo、shape 推断 (ScatterGmInferFunc)、代码生成 (GenScatterGmOp)、tile 函数注册等全链路注册新操作码,标记为 requiresSimt=true,使用 PIPE_MTE3 管道,输入内存含 MEM_DEVICE_DDR,输出含 MEM_DEVICE_DDR。

  • 新增 TScatterGm tile 级实现: 在 scatter.h 中实现两个版本——A5 平台(TScatterGmA5)使用 pto::MSCATTER 指令批量写入 GM 并支持原子加和越界跳过;V220 平台通过逐元素 TSTORE 配合 PIPE_MTE3/PIPE_S 流水线同步实现,同时将已有的 Tscatter/TscatterElementS 中的事件 ID 从 EVENT_ID0 改为 EVENT_ID7。

  • 新增 ScatterGm 前端 API 与 tiling 逻辑: 在 indexing.cpp 中添加 ScatterGm 函数(含数据类型校验、inplace 语义),以及 TiledScatterGm/TensorScatterGm 分 tile 展开逻辑,通过 ScatterGmExpandFunc 生成 OP_SCATTER_GM 操作节点,并缓存 dst/self tile 以避免跨 tile 重复创建。

  • A5 混合模式元数据机制: 在 gen_aicore_code.cpp 中生成源码级 g_simt_kernel_meta(含 aivType=4、staticUbSize=8KB);在 aicore_compiler.cpp 中新增 PatchA5MixedMetadata,解析 ELF 文件的 .ascend.meta.* 段中 TLV 条目并覆写为混合模式值。

  • 运行时 localMemorySize 配置: 在 runtime_utils.h 中定义 GetAicoreLocalMemorySize,A5 返回 216*1024,并在 kernel_bundle_launch.cpp、eslmodel_launcher.cpp、kernel_binary.cpp 中设置 rtTaskCfg.localMemorySize。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 11 Comments:

代码审查

审查总结

我对所有 8 个变更文件逐一进行了审查,结果如下:

文件 审查结论
framework/include/tilefwk/tilefwk_op.h 无问题 — 新增 ScatterGm 声明,API 设计意图明确(in-place 操作,Tensor& self)
framework/src/codegen/npu/codegen_op_npu.cpp 无问题 — 在 gather/scatter 分发表中新增 OP_SCATTER_GM 条目
framework/src/codegen/npu/codegen_op_npu.h 无问题 — 新增 GenScatterGmOp 方法声明
framework/src/codegen/npu/codegen_vector_gather_scatter.cpp 无问题 — GenScatterGmOp 实现与同文件中其他 TileTensor codegen 模式一致
framework/src/interface/interpreter/calc_vector.cpp 无问题 — 复用 ExecuteOpScatter 做计算,与 OP_SCATTER 语义一致
framework/src/interface/operation/op_infer_shape_impl.cpp 发现 1 个 P3 问题:ScatterGmInferFunc 中 idxValidShape 空向量时存在越界访问
framework/src/interface/operation/opcode.cpp 无问题 — 注册信息、SUPPORT_TILETENSOR_OPS 等变更一致
framework/src/interface/operation/opcode.h 无问题 — 枚举、OpcodeInfo 结构体、IsCopyOut、统计集合等变更均一致且完整

发现统计:P3 × 1

整体风险评估:低风险。 这是一个新增 ScatterGm 算子的功能 PR,代码结构与既有 Scatter 算子高度一致,8 个文件的变更形成了完整的注册链(枚举 → 注册信息 → 推理形状 → 计算 → 代码生成 → API 声明)。唯一发现的问题是一个防御性编程方面的边界情况(indices 为空时越界),实际触发概率极低。


我已完成了所有关键问题的分析。现在让我写出最终总结。

审查总结

已审查的文件(共 8 个):

文件 审查结论
framework/src/interface/operation/operation.h 无问题 — 仅新增 OP_SCATTER_GM 到列表中
framework/src/interface/operation/tile_shape_verifier.h 无问题 — 新增 OP_SCATTER_GM 的校验配置
framework/src/interface/operation/vector/indexing.cpp 无问题 — 新增 ScatterGm 相关函数、tiling 逻辑和注册,逻辑自洽
framework/src/interface/tileop/vector/scatter.h P2 发现:Tscatter 在 A5 上移除了两种 dtype 组合的标量退化条件
framework/src/machine/compile/aicore_compiler.cpp P1 发现:PatchMetaSection 第一轮遍历错误校验 TLV 值,导致 Bisheng 段不被修补
framework/src/machine/compile/gen_aicore_code.cpp 无问题 — 新增 SIMT 内核元数据结构体及段属性,代码正确
framework/src/machine/runtime/bundle/kernel_bundle_launch.cpp 无问题 — 新增 localMemorySize 设置,与 kernel_binary.cpp 已有模式一致
framework/src/machine/runtime/launcher/eslmodel_launcher.cpp 无问题 — 新增 localMemorySize 设置,与 kernel_binary.cpp 已有模式一致

发现数量按严重度:

  • P0: 0
  • P1: 1 — PatchMetaSection 第一轮校验逻辑错误,导致 Bisheng 生成段中错误 TLV 值不被修补,Runtime 将使用错误元数据
  • P2: 1 — Tscatter 在 A5 上移除了 int8+int32 和 int32+int32 的标量退化,可能暴露不支持的 dtype 组合给 TSCATTER
  • P3: 0

整体风险判断:

中等风险。P1 缺陷直接影响 A5 混合模式(SIMT)编译产物的正确性——若 Bisheng 编译器确实生成与源码定义不同的元数据值,Runtime 将得到错误的 AIV type 和 static UB size 配置,可能导致内核启动失败或 UB 越界。P2 缺陷影响 A5 上特定 dtype 组合的 scatter 计算正确性,取决于 TSCATTER 对这些组合的实际支持情况。其余变更(ScatterGm 算子框架、tiling 逻辑、内核实现、localMemorySize 设置)结构完整、逻辑自洽。


发现 6 个问题,均为中低优先级建议。


审查总结

按优先级分类的问题统计:

优先级 数量
P0 0
P1 1
P2 0
P3 1

各文件审查结果:

  • framework/tests/ut/machine/src/test_aicore_compiler.cpp — 1 个 P3 可选建议(测试字符串脆断),无阻塞性问题。
  • python/pypto/op/indexing.py — 1 个 P1 问题:scatter_gm 使用 input.Update() 而所有其他 inplace 操作均使用 input.Move(),Update 方法在 Python 层未找到定义,可能导致 AttributeError。
  • python/src/bindings/operation.cpp — 无问题,绑定代码与现有模式一致。
  • python/tests/st/operation/vector/test_a5_mixed_metadata_simd_smoke.py — 无问题,测试结构与现有测试一致。

整体风险评估:中等风险。 核心风险在于 scatter_gm 的 Update/Move 不一致 —— 如果 Update 在 C++ pybind 层未导出,scatter_gm 将在运行时直接崩溃。建议在合入前确认 Update 方法的可用性,或统一改为 Move。

类型 数量
🔴 阻塞 1
🟡 建议 4

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 11 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 11 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
framework/src/codegen ✅ 尹杰, lululu40888 (2/2) ✅ 尹杰 (1/1)
framework/src/interface/function ✅ 尹杰, lululu40888 (2/2) ✅ 尹杰 (1/1)
framework/src/interface/operation ✅ 尹杰, lululu40888 (2/2) ✅ 尹杰 (1/1)
framework/src/interface/tileop/vector ✅ 尹杰, jason_yuan_ye, lululu40888 (3/2) ✅ 尹杰, jason_yuan_ye (2/1)
framework/src/interface/utils ✅ 尹杰, lululu40888 (2/2) ✅ 尹杰 (1/1)
framework/src/machine ✅ 尹杰, jason_yuan_ye, lululu40888 (3/2) ✅ 尹杰, jason_yuan_ye (2/1)
framework/src/passes ✅ lululu40888, zhangyuebing, 尹杰 (3/2) ✅ zhangyuebing, 尹杰 (2/1)
repo-cann/pypto ✅ jason_yuan_ye, 尹杰, lululu40888, zhangyuebing (4/2) ✅ zhangyuebing, jason_yuan_ye, 尹杰 (3/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
287 messages are collapsed here。 See more
Jie Yin
Jie YinCollaborators
Sep 1 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 1 Added label: lgtmapproved
CANN-robot
CANN-robotCollaborators
Sep 1 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
Jie YinJie YinCollaborators
Sep 1 Resolved final issue
CANN-robotCANN-robotCollaborators
Sep 1 Merged pull request