已合并
【代码侦探Challenge09】完成 MatmulAddRelu 融合算子实现 #4363
【代码侦探Challenge09】完成 MatmulAddRelu 融合算子实现 #4363
已合并
acam2adam创建于 17 天前
acam2adam
acam2adam
17 天前

当前PR是否有AI参与:

[x] 否

[ ] 是
__1. AI Agent 平台:
__2. AI 模型:
__3. Prompt上下文 :

PR功能描述 / 为什么需要这个合入**:

该PR关联的issue

(格式为fixes #<issue号>, 或者resolves #<issue号>): fixes #

希望检视人员了解:

改动类型 / Change Type

测试信息 / Testing

检查清单 / Checklist

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 acam2adam 的贡献)
acam2adamacam2adam
17 天前 创建了 pull request,commit 82ebc5ca
atomgit-bot
atomgit-bot
17 天前 评论:

变更摘要

本 PR 为「代码侦探 Challenge09」新增了固定 M=N=K=256 的 Catlass Python 融合算子实现,完成 Y = ReLU(A @ B + X) 的计算。核心文件 matmul_add_relu.py 基于 catlass.tla 定义 matmul_add_relu_kernel,采用 tla.cube() 与 tla.vector() 双核协作流程:Cube 侧负责 GM→L1→L0 的数据搬运与 tla.mmad 矩阵乘累加,并经 FixPipe 将结果搬入 UB;Vector 侧完成加 X 与 ReLU 后写回 GM。配套的 test_matmul_add_relu.py 在 Ascend 950 上以 PyTorch 结果为参考进行正确性校验。

主要改动

  • 融合核函数与多核同步框架: 新增 matmul_add_relu_kernel,通过 tla.mutex 管理 l1_ab、l0_ab、l0_c、ub_acc、ub_x 等缓冲的访问顺序,并用 tla.cross_flag、tla.cross_core_set_flag、tla.cross_core_wait_flag 实现 Cube 与 AIV 之间的跨核通知。
  • Cube 侧矩阵乘流程: 在 tla.cube() 中按 K 分片(L0_K=64)执行 GM→L1 搬运、L1→L0A/L0B 搬运及 tla.mmad 累加,其中 init_c = k_tile == 0 控制首个分片覆盖写入、其余分片累加。
  • L0C→UB 结果下发: 使用 tla.copy 配合 tla.params.CopyL0C2DstParams(l0c2ub_mode=tla.params.L0C2UBMode.SPLIT_M),将 256 行按 M 均分给两个 AIV,各获得 128×256 的 FP32 结果。
  • Vector 侧 Add+ReLU 计算: 在 tla.vector() 中以 X_CHUNK_M=64 分块搬运 gm_x,并在 tla.vec.func(mode="simd") 内按 64 个 FP32 的 SIMD 宽度执行 tla.max(tla.add(acc_val, x_val), 0.0) 后原位写回,结果再经 MTE3 搬回 gm_y。
  • 测试脚本与校验逻辑: 新增 test_matmul_add_relu.py,在 Ascend 950 上调用 tla.compile(--npu-arch 3510)运行内核,并以 PyTorch 参考结果按相对容差 max(1, |expected|)/256 校验,同时检查结果非负且有限。
likedislike
不准确?
atomgit-bot
atomgit-bot
17 天前 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
17 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
17 天前 评论:

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
repo-cann/cann-outreach ✅ shaoyf, 傅涛 (2/2) ✅ shaoyf, 傅涛 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robot
CANN-robot成员
17 天前 评论:

CLA Signature Pass

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

likedislike
shaoyf成员
1 天前 评论:

/approve
/lgtm

likedislike
fulltower成员
1 天前 评论:

/approve
/lgtm

likedislike
CANN-robotCANN-robot成员
1 天前 添加了label:lgtmapproved
CANN-robotCANN-robot成员
1 天前 合入了pull request