已开启
【代码侦探·Challenge 09】 CATLASS DSL MatmulAddRelu 算子练习题 #4270
【代码侦探·Challenge 09】 CATLASS DSL MatmulAddRelu 算子练习题 #4270
已开启
JeffDing创建于 23 天前
JeffDing
JeffDing
23 天前

当前PR是否有AI参与:

[x] 否

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

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

提交 Challenge09-MatmulAddRelu(CATLASS DSL)的个人答案。补全 PR_code/JeffDing/matmul_add_relu.py 中五处 TODO:

  • L1 装载完整的 A/B:tla.copy 将 GM 中行优先 FP16 的 A/B 搬入 L1 中 zN 布局的缓冲区;
  • L1→L0A/L0B:按 K=64 将当前分片搬入 L0A(zN)/L0B(nZ);
  • 矩阵乘累加:K=256 分为四个长度为 64 的分片,每个分片调用一次 tla.mmad,首次 init_c=True 覆盖写入 L0C,后三次累加,最终在 L0C 得到 256×256 FP32 结果;
  • L0C→UB:CopyL0C2DstParams(l0c2ub_mode=L0C2UBMode.SPLIT_M) 将 L0C 分给两个 AIV,各处理 128 行;
  • Vector 后处理:每个 AIV 将 X 分为两个 64×256 分片,在存放累加结果的 UB 中原位执行 Add→ReLU(先 tla.add 加 X,再 tla.max 与 0.0 取最大),最后写回 GM 的 Y。

该PR关联的issue

fixes #

希望检视人员了解:

  • 改动仅新增 PR_code/JeffDing/ 个人目录下的 matmul_add_relu.py 与 test_matmul_add_relu.py,未改动题目 Template/、PR_code/README.md 及他人目录;
  • 核内缓冲访问顺序沿用模板给出的 tla.mutex lock/unlock 框架,未修改同步结构与提交框架;ReLU 严格在加 X 之后执行;
  • 已在本地 Ascend 950 NPU 实际运行验证脚本:python PR_code/JeffDing/test_matmul_add_relu.py --device 0,输出 PASS: M=N=K=256, blocks=1。

改动类型 / Change Type

测试信息 / Testing

检查清单 / Checklist

likedislike
合并受阻
JeffDingJeffDing
23 天前 创建了 pull request,commit 39562161
atomgit-bot
atomgit-bot
23 天前 评论:

变更摘要

本 PR 为 Challenge09-MatmulAddRelu(CATLASS DSL)提交个人答案,新增 matmul_add_relu.py 与 test_matmul_add_relu.py 两个文件,仅存在于 PR_code/JeffDing/ 个人目录下,补全模板中五处 TODO,实现固定 M=N=K=256 的 A @ B + X 后接 ReLU 的融合算子。实现按 Cube 与 Vector 两个核分工:Cube 侧完成 A/B 的 GM→L1→L0A/L0B 搬运、按 K=64 分片用 tla.mmad 累加到 L0C,再经 L0C→UB 将结果拆给两个 AIV;Vector 侧在 UB 中逐分片执行 Add→ReLU 并写回 Y。测试脚本在 Ascend 950 上通过 tla.compile 编译执行并与 PyTorch 参考结果比对。

主要改动

  • 补全 GM→L1 的 A/B 搬运:用 tla.copy 将行优先 FP16 的 gm_a/gm_b 搬入 L1 中 zN 布局的 l1_a/l1_b,并以 mutex_l1 的 lock/unlock 约束 MTE2 的搬运顺序。
  • 补全 L1→L0A/L0B 的分片搬运:在 k_tile 循环内用 tla.tile_view 切出 K=64 分片,按 A、B 顺序将 l1_a_k/l1_b_k 分别搬入 L0A(zN)与 L0B(nZ),由 mutex_l1/mutex_l0 保证 Cube 读取前完成搬运。
  • 补全矩阵乘累加:以 init_c = k_tile == 0 控制 tla.mmad,首次覆盖写入 L0C、后续累加,将 K=256 的四个分片累加成 256×256 FP32 的 l0_c,期间不提前启用 ReLU。
  • 补全 L0C→UB 拆分搬运:调用 tla.copy 并传入 CopyL0C2DstParams(l0c2ub_mode=L0C2UBMode.SPLIT_M),使两个 AIV 各接收 128×256 的累加结果,并用 cross_core_set_flag(fix_done)/cross_core_wait_flag 做跨核同步。
  • 补全 Vector Add+ReLU 与写回:在每个 AIV 的 simd 函数内按行/列载入 FP32 向量,先后执行 tla.add 与 tla.max(..., 0.0) 并原位写回 acc_reg,最后用 tla.copy 将处理结果写回 Y;新增的 test_matmul_add_relu.py 负责构造输入、调用 tla.compile/artifact 并比对结果输出 PASS/FAIL。
likedislike
不准确?
atomgit-bot
atomgit-bot
23 天前 评论:

代码审查

✅ 未发现问题

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

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, yanyawen, jxlang, luzx66) ❌ (0/1)(You can also ask: yanhf, shaoyf, 田晓亮, 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

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

likedislike
JeffDing
JeffDing
23 天前 评论:

/compile

likedislike
JeffDingJeffDing
23 天前 预合并成功(commit_id: d346a74d9fcc85ab6f035cf4c348c6429a7b7c8e)
JeffDingJeffDing
23 天前 强制推送  1 个提交:80dd6fd0-提交算子 challenge09_matmuladdrelu - 开发者
JeffDingJeffDing
23 天前 预合并成功(commit_id: 4ce899d0d635600e5e8d28fe745bf488c848b57e)
CANN-robotCANN-robot成员
23 天前 删除了label:cann-cla/no
CANN-robotCANN-robot成员
23 天前 添加了label:cann-cla/yes
JeffDingJeffDing
23 天前 关联了issue:[Feature]: 提交算子 challenge09_matmuladdrelu - 开发者
JeffDingJeffDing
23 天前 修改标题为 “【代码侦探·Challenge 09】 challenge09_matmuladdrelu”,原标题为“提交算子 challenge09_matmuladdrelu - 开发者”
JeffDingJeffDing
23 天前 修改标题为 “【代码侦探·Challenge 09】 matmuladdrelu”,原标题为“【代码侦探·Challenge 09】 challenge09_matmuladdrelu”
JeffDingJeffDing
23 天前 修改标题为 “【代码侦探·Challenge 09】 CATLASS DSL MatmulAddRelu 算子练习题”,原标题为“【代码侦探·Challenge 09】 matmuladdrelu”