已开启
【代码侦探·Challenge 09】电子科技大学 - xhf_A2_1 - MatmulAddRelu #4395
【代码侦探·Challenge 09】电子科技大学 - xhf_A2_1 - MatmulAddRelu #4395
已开启
xhf_A2_1创建于 5 天前
xhf_A2_1
5 天前

当前PR是否有AI参与:

__1. AI Agent 平台: OpenAI Codex
__2. AI 模型: GPT-5
__3. Prompt上下文: 协助分析 Challenge 09 任务、补全 TODO、准备验证脚本,并在本人 HiDevLab Ascend 950 环境执行实机验证。

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

完成 Challenge 09 MatmulAddRelu 融合算子:

  • 使用 TLA 描述 Matmul、Add 与 ReLU 数据流;
  • 使用多核切分与双缓冲,blockFactor=20480;
  • 将结果写回 GM,并补充可直接执行的自验证脚本;
  • 个人目录:PR_code/xhf_A2_1/。

关联活动 Issue

https://gitcode.com/cann/cann-learning-hub/issues/777

希望检视人员了解

已在本人 HiDevLab Ascend950PR_9579 真机环境验证通过。

改动类型 / Change Type

测试信息 / Testing

  • 环境:CANN 9.1.0,CATLASS 2.1.0

  • 芯片:Ascend950PR_9579

  • torch:2.9.0+cpu

  • torch_npu:2.9.0.post6

  • 命令:python PR_code/xhf_A2_1/test_matmul_add_relu.py --device 0

  • 结果:PASS: M=N=K=256, blocks=1

  • 退出状态:0

检查清单 / Checklist

likedislike
合并受阻
Xxhf_A2_1
5 天前 创建了 pull request,commit 0134ead2
Xxhf_A2_1
5 天前 关联了issue:🔍 代码侦探·第九期(CATLASS实操)|补全算子代码有奖,周周赢取定制礼品!
atomgit-bot
atomgit-bot
5 天前 评论:

变更摘要

本 PR 在 PR_code/xhf_A2_1/ 目录下新增了 Challenge 09 的 MatmulAddRelu 融合算子实现与自验证脚本。matmul_add_relu.py 基于 catlass.tla 描述固定 256×256×256 的 Matmul、Add、ReLU 数据流,通过多级缓冲、mutex 同步与 Cube/Vector 双流水实现多核切分与计算;test_matmul_add_relu.py 构造随机输入,调用 tla.compile 并执行核函数,与 PyTorch 参考结果对比完成验证。

主要改动

  • 融合算子骨架与常量定义: 新增 matmul_add_relu_kernel,固定 M=N=K=256,定义 L0_K=64、AIV_M=M//2、X_CHUNK_M=64、SIMD_LANES=64,并通过 tla.allocate 在 l1、l0a、l0b、l0c、ub 上分配 A/B/累加/输入缓冲。
  • Cube 端多级搬运与矩阵乘累加: 在 tla.cube() 中完成 GM→L1 的 tla.copy、按 k_tile 的 L1→L0 搬运,并用 tla.mmad(l0_c, l0_a, l0_b, init_c=(k_tile == 0)) 将 FP16 乘积累加到 L0C 的 FP32 结果,mutex_l1/mutex_l0/mutex_l0c 管理各 Pipe 访问顺序。
  • L0C→UB 拆分与跨核同步: 使用带 tla.params.CopyL0C2DstParams(l0c2ub_mode=SPLIT_M) 的 tla.copy 将完整累加结果分片到两个 AIV 的 UB,并通过 tla.cross_core_set_flag / tla.cross_core_wait_flag(fix_done)实现 Cube 到 Vector 的跨核通知。
  • Vector 端 Add + ReLU 计算与写回: 在 tla.vector() 中按 aiv_id 和 chunk 切分数据,使用 tla.vec.func(mode="simd") 对每行按 SIMD_LANES 切向量,执行 tla.max(tla.add(acc_reg.load(), x_reg.load()), 0.0) 原位写回,再由 tla.copy 输出到 gm_y。
  • 新增自验证测试脚本: test_matmul_add_relu.py 支持 --device 参数并延迟导入 catlass.tla、torch、torch_npu,构造 A/B/X 输入与 torch.relu(a @ b + x) 参考值,经 tla.compile(..., options="--npu-arch 3510") 编译后执行,按容差校验并打印 PASS/FAIL。

说明:我按要求对代码进行了只读分析,并尝试运行 ast-grep、grep、read_file 等工具辅助理解,但所有调用均因当前环境限制未能实际返回结果;以上总结完全基于 PR 标题、描述、改动文件列表及内嵌补丁内容,未对文件做额外探查。

likedislike
不准确?
atomgit-bot
atomgit-bot
5 天前 评论:

代码审查

✅ 未发现问题

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

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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/cann-outreach ❌ (0/2)(You can also ask: 傅涛, 田晓亮, luzx66, yanhf, shaoyf) ❌ (0/1)(You can also ask: 王艳秋, 傅涛, luzx66, jxlang, Carolina_yuan)

💡 Tip:

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

CLA Signature Pass

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

likedislike
Xxhf_A2_1
3 天前 修改了pull request 的描述
CANN-robotCANN-robot成员
3 天前 添加了label:ai-includes-code
CANN-robotCANN-robot成员
3 天前 添加了label:ai-compliance-failed
CANN-robot
CANN-robot成员
3 天前 评论:

AI Declaration Check Failed

@xhf_A2_1, AI declaration is not compliant. AI tool: OpenAI Codex

Please check:

  1. The AI tool and model declared in the PR description must be real.
  2. Commit messages must include the AI model, and it must be consistent with the PR description. ⚠️
likedislike
Xxhf_A2_1
3 天前 预合并成功(commit_id: 84de84a5d0b77459449b60891a15630b5971f7ed)
此处折叠了5条事件消息 查看更多
CANN-robotCANN-robot成员
3 天前 添加了label:ai-compliance-successful
xhf_A2_1
3 天前 评论:

/retrigger

likedislike
xhf_A2_1
3 天前 评论:

/compile

likedislike
xhf_A2_1
3 天前 评论:

@傅涛 @luzx66 您好,AI 声明与提交信息已按规范修正,当前已通过 ai-compliance-successful 与 CLA,且保持单提交;实现已在 HiDevLab Ascend 950 真机验证通过,并已重新触发 /compile。烦请有空时协助检视并给予 /lgtm、/approve,谢谢!

likedislike