已合并
【代码侦探Challenge09】完成 MatmulAddRelu 融合算子实现 #4363
acam2adam创建于 17 天前
【代码侦探Challenge09】完成 MatmulAddRelu 融合算子实现 #4363
已合并
Pull Request已成功合入, 合并人@CANN-robot
(感谢 acam2adam 的贡献)atomgit-bot
17 天前 评论:
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校验,同时检查结果非负且有限。


不准确?
atomgit-bot
17 天前 评论:
17 天前 评论:
17 天前 添加了label:cann-cla/yes
CANN-robot
17 天前 评论:
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
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


CANN-robot
17 天前 评论:
17 天前 评论:
1 天前 添加了label:lgtmapproved
1 天前 合入了pull request
当前PR是否有AI参与:
[x] 否
[ ] 是
__1. AI Agent 平台:
__2. AI 模型:
__3. Prompt上下文 :
PR功能描述 / 为什么需要这个合入**:
该PR关联的issue
(格式为fixes #<issue号>, 或者resolves #<issue号>): fixes #
希望检视人员了解:
改动类型 / Change Type
测试信息 / Testing
检查清单 / Checklist