已开启
【代码侦探·Challenge 09】Student_ZZX 补全 MatmulAddRelu 算子 #4322
【代码侦探·Challenge 09】Student_ZZX 补全 MatmulAddRelu 算子 #4322
已开启
Student_ZZX创建于 7 天前
Student_ZZX
7 天前

当前 PR 是否有 AI 参与

  1. AI Agent 平台:Codex
  2. AI 模型:GPT-6
  3. Prompt 上下文:依据 Challenge 09 任务书、官方模板、CATLASS API 文档与 HiDevLab 实机结果,补全 MatmulAddRelu 五处 TODO 并验证。

功能描述

在 2026/CANN-Code-Detective/Challenge09-MatmulAddRelu/PR_code/Student_ZZX/ 提交个人实现与原版验证脚本。完成 GM→L1、L1→L0、四段 K=64 的 Cube MMAD、L0C→双 AIV 的 SPLIT_M 搬运,以及 Vector Add→ReLU。

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

测试信息

检查清单

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

变更摘要

本 PR 在 Challenge09-MatmulAddRelu/PR_code/Student_ZZX/ 目录下新增两个文件,补全 Challenge 09 官方模板中五处 TODO,实现固定 256×256×256 的 MatmulAddRelu 融合算子及其验证脚本。算子基于 catlass.tla 构造成 cube 与 vector 两段执行流程:Cube 侧完成 GM→L1、L1→L0、四段 L0_K=64 的 tla.mmad 矩阵乘累加以及 L0C→UB 的 SPLIT_M 搬运,并借助 mutex 与 cross_flag/cross_core_wait_flag 管理缓冲访问与跨核同步;Vector 侧在 UB 上分块搬入 X 后执行 Vector Add 与 ReLU,再写回 GM 的 Y。验证脚本 test_matmul_add_relu.py 使用 torch/torch_npu 生成输入与参考结果,通过 tla.compile 与 from_dlpack 运行算子并比对容差。

主要改动

  • 补全 GM→L1 与 L1→L0 搬运(TODO 1、TODO 2):在 mutex_l1、mutex_l0 约束下按 A、B 顺序调用 tla.copy,将 gm_a/gm_b 搬到 zN 布局的 l1_a/l1_b,再将 K 分片 l1_a_k/l1_b_k 搬到 l0_a(zN)与 l0_b(nZ)。
  • 补全四段 K 分片的矩阵乘累加(TODO 3):在 K // L0_K 循环中以 tla.mmad(l0_c, l0_a, l0_b, init_c=(k_tile == 0)) 将 FP16 输入累加为 L0C 中的 FP32 结果,仅首段初始化、后续保留累加值。
  • 补全 L0C→UB 的跨核搬运与同步(TODO 4):用 tla.copy 配合 tla.params.CopyL0C2DstParams(l0c2ub_mode=tla.params.L0C2UBMode.SPLIT_M) 将完整 l0_c 拆分到两侧 AIV 的 128×256 累加缓冲,并通过 cross_core_set_flag/cross_core_wait_flag 通知 Vector 侧。
  • 补全 Vector Add + ReLU(TODO 5):在 simd 模式下逐行按 SIMD_LANES=64 分块 load 累加值与 X,以 tla.max(tla.add(acc_value, x_value), 0.0) 计算并 store 回 ub_acc_chunk,随后由 tla.copy 写回 GM。
  • 新增实机验证脚本:test_matmul_add_relu.py 定义 --device 参数(禁止负数),用 torch.manual_seed(2026) 生成输入并计算 torch.relu(a @ b + x) 参考值,经 tla.compile(..., options="--npu-arch 3510") 与 block_num=1 执行后按相对容差判定 PASS/FAIL。
likedislike
不准确?
atomgit-bot
atomgit-bot
7 天前 评论:

代码审查

✅ 未发现问题

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

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: shaoyf, 傅涛, yanhf, Carolina_yuan, luzx66) ❌ (0/1)(You can also ask: 王艳秋, jxlang, yanhf, gcw_tgPdOLZT, luzx66)

💡 Tip:

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

CLA Signature Pass

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

likedislike
SStudent_ZZX
5 天前 修改了pull request 的描述
SStudent_ZZX
5 天前 修改了pull request 的描述
SStudent_ZZX
3 天前 预合并成功(commit_id: b555d72f5aec4ca18f33b60baa3b1923e472976e)
SStudent_ZZX
3 天前 强制推送  51 个提交:4b1168d4-50 commits from branch master,46800cff-【代码侦探·Challenge 09】Student_ZZX 补全 MatmulAddRelu 算子
SStudent_ZZX
3 天前 预合并成功(commit_id: 99ff0d13b4cf7b2bb2fe724d0dcbbb5b978209c2)
CANN-robotCANN-robot成员
3 天前 删除了label:cann-cla/yes
CANN-robotCANN-robot成员
3 天前 添加了label:cann-cla/yes
SStudent_ZZX
3 天前 修改了pull request 的描述
Student_ZZX
3 天前 评论:

已完成提交整理:分支已同步最新 master 并压为单一提交 46800cff;commit 已补充 Assisted-by: Codex:GPT-6,PR 描述已补齐 AI 参与声明。HiDevLab 实机验证通过,Catlass 与 PyTorch 结果一致,输出包含 tensor、M/N/K 与 blocks 信息。

likedislike