Merged
单核切K AL1全载模板 修复合入 #7504
llqx-1Add on Jul 14
单核切K AL1全载模板 修复合入 #7504
Merged
llqx-1Add on Jul 14
llqx-1
llqx-1Collaborators
Jul 14

描述

Summary

为本 PR 增加 MatMulV3 AL1 全载 + 单核切 K 模板(tilingKey 65569),面向 M 小、K/N 大 场景,通过核内 K 条带复用 L1 上的 A,降低左矩阵重复搬入量。同期包含 CopyInAL1 未初始化读修复,以及 block 参数结构体字段排布调整。

背景与收益

在 M 较小、K/N 较大时,基本块平铺会使左矩阵 A 随 N 的切分被反复从 GM 搬入。

  • 分核:按 N 方向(baseN 总块数)均分给各 AIC;
  • 核内:K 外循环;每一轮把当前 K Tile的 A 整块搬入 AL1,再对本核负责的 N 段做 matmul;A 在同一核内只按 K Tile总块数各搬一次,整个A矩阵所有数据量仅搬入一次。

相对基本块平铺,数据搬入量(设计目标)对比:

基本块平铺:
  D0 = dtype_size * ( ceil(M/mTile)*K*N + ceil(N/nTile)*M*K )

本模板(AL1 全载 + 单核切 K):
  D0 = dtype_size * ( ceil(M/mTile)*K*N + min(ceil(N/nTile), numCore)*M*K )

左矩阵 A 的重复搬入从约 ceil(N/nTile) 次降为至多 numCore 次量级,缓解带宽瓶颈。


算法流程

1. 准入与分核(Host Tiling)

准入(摘要)(IsSupportSingleCoreSplitKAL1FullLoad):

  • 910B / 支持 L0c2out;输入 ND;A 不转置;dtype 当前为 fp16/bf16;
  • K、N 按 256B 对齐;M ≤ 256;
  • 形状落入 K/N 收益区间(K、N 中等偏大,具体阈值见 host 代码)。

分核:

  • nCnt = ceil(N / baseN),usedCoreNum = min(nCnt, aicNum);
  • 各核分配若干连续的 N base 条带(头/尾核 ceil/floor),核内一次算完本核整段 curCoreN。

基本块与 L1:

  • baseM = CeilAlign(M, 16),baseN / baseK 在 L0A/L0B/L0C 约束下搜索优选;
  • stepKa * baseK 为 AL1 满载 K 条带尺寸;stepN / depthA1/depthB1 在 L1 容量内选择,溢出时回退调小 stepKa/stepN。

输出 tilingKey:LOADMODE=AL1_FULLLOAD + SPLITCOREMODE=SINGLE_CORE_SPLIT_K → 65569。

2. 核内计算(Device Kernel)

伪代码(与详设一致,K 外、本核 N 内):

InitBlockIndex()   // 得到本核 coreOffsetN、curCoreN
for kIndex in [0, kAL1LoopNum):
    UpdateBlockParamsK(kIndex)          // currentKBlockStart / Size
    CopyInAL1()                         // GM→AL1,Nd2Nz / NZ DataCopy
    CalcGMOffset(kIndex)                // offsetB / offsetC / offsetBias
    SetSingleShape(M, curCoreN, currentKBlockSize)
    SetTensorA(al1Local) + SetTensorB(bGM[offsetB])
    IterateAll → C(首轮 k 可带 bias;后续 k 原子累加)
    Free AL1
若多条 K 条带且输出非 fp32:AIV 再做 fp32 workspace → 输出 cast

要点:

  • A:手动搬入 TSCM(A_TYPE_NEW),MatmulImpl 从 L1 取 A;
  • B:当前仍由高阶 API 从 GM 设置地址搬入并参与计算;
  • 单 K 条带(stepKa*baseK >= Ka)且输出非 fp32:可 直接写 C GM(directOutput),跳过 fp32 workspace + vector cast;
  • 多 K 条带:AIC 写 float workspace,AIV Cast32to16 到输出(fp16/bf16)。

3. 算法实现模块

模块 路径 职责
Tiling 策略 op_host/op_tiling/matmul_v3_sc_splitk_al1_fullload_tiling.{h,cpp} base 块搜索、L1 step、校验
Tiling 挂接 matmul_v3_base_tiling.cpp/.h 准入 + DoSingleCoreSplitKAL1FullLoadTiling 接入选型链
Block op_kernel/mat_mul_sc_splitk_al1_fullload_block.h 分核索引、K 条带、CalcGMOffset
Kernel op_kernel/mat_mul_sc_splitk_al1_fullload_kernel.h AL1 CopyIn、IterateAll、cast / direct 双路径
Dispatch mat_mul_v3.cpp + mat_mul_v3_tiling_key.h MIX_AIC_1_2 下 65569 模板注册与分发

后续两笔修复(纳入本 PR)

CopyInAL1 未初始化读

Nd2Nz 的 nValue/dValue 若使用 baseM,在 M < baseM 时会多读行 → 内存检测报 GM 未初始化读。

修复:ND 路径按真实 matmulTiling.M 设置 nDim/dDim(与 AL1 全载模板 CopyInA1 一致);K 维仍用 currentKBlockSize。

BlockArgs 字段排布

将 isHf32、转置标志、atomicAddFlag、isSingleKStrip 等较小字段移到结构体尾部,便于对齐与后续扩展(无改语义)。


关联的Issue

测试

文档更新

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for llqx-1's contribution.)
llqx-1llqx-1Collaborators
Jul 14 Created pull request, commit 50903331
CANN-robotCANN-robotCollaborators
Jul 14 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Jul 14 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 14 Comments:

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


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
matmul ✅ 唐超, 唐玮玮 (2/2) ✅ 唐超, 唐玮玮 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
Jul 14 Set zengjuan,sxb154714,chaotang233,crystalhu,yangyang016,fanqirui,chenqi317,LinPX,liubo75,tangweiwei2,wangzitao_leo,xubinglin,liuyang_806 as approver
10 event messages are collapsed here。 See more
llqx-1llqx-1Collaborators
Jul 15 Draft state has been cancelled
llqx-1
llqx-1Collaborators
Jul 15 Comments:

compile

likedislike
llqx-1llqx-1Collaborators
Jul 15 Pre merge succeeded (commit_id: 8541e3a83a1d445da369b2539c12e989d223eb24)
CANN-robotCANN-robotCollaborators
Jul 15 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Jul 15 Comments:

流水线任务触发成功
任务链接 [52947f07eb7c44abb6a9d718ff756b24][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950 ✅ SUCCESS >>>>> >>>>>
Compile_Pre ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_classify ✅ SUCCESS >>>>>
UT_Test_ophost ✅ SUCCESS
UT_Test_opapi ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
UT_Test_opgraph ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_harmony-infer-chs-nn ✅ SUCCESS
UT_Test_report_lcov ⚠️ WARNING >>>>>

[2026-07-15 11:36:58]    CI执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Jul 15 Comments:

流水线任务触发成功
任务链接 [4370c125aba44ffc97f5dbc8d09782af][流水线指导]

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_checkpr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
codecheck_precommit ❌ FAILED >>>>> >>>>>
SCA ✅ SUCCESS >>>>>
likedislike
CANN-robotCANN-robotCollaborators
Jul 15 Added label: api-check-pass
5 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Jul 15 Removed label: stat/needs-squash
CANN-robot
CANN-robotCollaborators
Jul 15 Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): stat/needs-squash.

likedislike
llqx-1llqx-1Collaborators
Jul 15 Modify description of pull request
llqx-1
llqx-1Collaborators
Jul 15 Comments:

compile

likedislike
llqx-1llqx-1Collaborators
Jul 15 Pre merge succeeded (commit_id: b65e432ae3b758a0e6a1535ef4b860e2aa242e6a)
CANN-robotCANN-robotCollaborators
Jul 15 Removed label: ci-pipeline-failedapi-check-pass
CANN-robotCANN-robotCollaborators
Jul 15 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Jul 15 Comments:

流水线任务触发成功
任务链接 [c108b9a860f043f7895c600876a0d992][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950 ✅ SUCCESS >>>>> >>>>>
Compile_Pre ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_classify ✅ SUCCESS >>>>>
UT_Test_ophost ✅ SUCCESS
UT_Test_opapi ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
UT_Test_opgraph ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_harmony-infer-chs-nn ✅ SUCCESS
UT_Test_report_lcov ⚠️ WARNING >>>>>

[2026-07-15 14:59:23]    CI执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Jul 15 Comments:

流水线任务触发成功
任务链接 [7090e2df1b8c448fa2564588ea7c1c72][流水线指导]

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_checkpr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
codecheck_precommit ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>

[2026-07-15 14:13:18]    CI执行结束

likedislike
CANN-robotCANN-robotCollaborators
Jul 15 Added label: api-check-pass
CANN-robotCANN-robotCollaborators
Jul 15 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Jul 15 Added label: ci-pipeline-passed
llqx-1
llqx-1Collaborators
Jul 16 Comments:

/check-pr

likedislike
CANN-robot
CANN-robotCollaborators
Jul 16 Comments:

The following labels are not ready.

lgtm: Please wait for reviewers to review the code.

approved: Please wait for committers to review the code.

likedislike
tangweiwei2
tangweiwei2Collaborators
Jul 16 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 16 Added label: approved
唐超唐超Collaborators
Jul 17 Approval passed
唐超唐超Collaborators
Jul 17 Approval passed
唐超
唐超Collaborators
Jul 17 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 17 Added label: lgtm
CANN-robotCANN-robotCollaborators
Jul 17 Merged pull request