Merged
feat(transpose): 新增 2D perm=[1,0] TransDataTo5HD 全 tile 转置模板(TilingKey 10011) #7382
yefeicodingAdd on 6 days ago
feat(transpose): 新增 2D perm=[1,0] TransDataTo5HD 全 tile 转置模板(TilingKey 10011) #7382
Merged
yefeicodingAdd on 6 days ago
Total: 5 files changed+680-32
@@ -40,6 +40,7 @@
40 40 
41#include <sstream>41#include <sstream>
42#include <set>42#include <set>
43+#include <limits>
43#include "util/platform_util.h"44#include "util/platform_util.h"
44#include "transpose_tiling_base.h"45#include "transpose_tiling_base.h"
45#include "transpose_tiling_arch35.h"46#include "transpose_tiling_arch35.h"
@@ -103,6 +104,11 @@ ge::graphStatus TransposeNddmaTiling::RunTranposelTiling()
103 CHECK_RET_SUCC(ret);104 CHECK_RET_SUCC(ret);
104 105 
105 CalcTotalVolumeActual();106 CalcTotalVolumeActual();
107+ // 2D perm=[1,0] 特化模板优先: 满足条件直接走 10011
108+ if (!isRelatedTranspose_ && Is2DPermutation() && Fill2DTransDataTilingData() == ge::GRAPH_SUCCESS) {
109+ OP_LOGD(tilingContext_->GetNodeName(), "2D TransData tiling done.");
110+ return ge::GRAPH_SUCCESS;
111+ }
106 OP_CHECK_IF(TryVCONVTiling() == ge::GRAPH_SUCCESS, OP_LOGD(tilingContext_->GetNodeName(), "Do convTiling success"),112 OP_CHECK_IF(TryVCONVTiling() == ge::GRAPH_SUCCESS, OP_LOGD(tilingContext_->GetNodeName(), "Do convTiling success"),
107 return ge::GRAPH_SUCCESS);113 return ge::GRAPH_SUCCESS);
108 114 
@@ -212,6 +218,130 @@ ge::graphStatus TransposeNddmaTiling::TryVCONVTiling()
212 return ge::GRAPH_FAILED;218 return ge::GRAPH_FAILED;
213}219}
214 220 
221+/**
222+ * @brief 判断是否为 2D perm=[1,0] 转置
223+ *
224+ * RemoveAxis/MergeAxis 后 identity 场景已归并为 1 维,因此 dim==2 且
225+ * perm=[1,0] 即为真正的二维转置。
226+ */
227+bool TransposeNddmaTiling::Is2DPermutation()
228+{
229+ if (shapeInfo_.dim != DIM_TWO) {
230+ return false;
231+ }
232+ return shapeInfo_.reducedPerm[0] == DIM_ONE && shapeInfo_.reducedPerm[1] == DIM_ZERO;
233+}
234+ 
235+/**
236+ * @brief 填充 2D perm=[1,0] 特化模板(10011 TRANSPOSE_2D_TRANSDATA)的 TilingData
237+ *
238+ * 准入条件:
239+ * 1. dtype 为 16bit/32bit(kernel 仅实例化 int16_t/int32_t)
240+ * 2. m、n 均 16 对齐(TransDataTo5HD 按 16x16 块转置)
241+ * 3. 总数据量达到门槛:DAV_3510 为 512KB(实测对 SIMT/gather 交叉点),
242+ * 其它代际保持 4MB
243+ *
244+ * @return ge::GRAPH_SUCCESS 命中并使用 10011;ge::GRAPH_FAILED 不满足条件,回落主流程
245+ */
246+ge::graphStatus TransposeNddmaTiling::Fill2DTransDataTilingData()
zhanw_coding
zhanw_codingzhanw_coding2 days ago

这里并不是为了给tilingdata赋值,而是尝试走5hd模板,建议改为相同风格命名Try5hdTiling()

likedislike
247+{
248+ static constexpr int64_t TRANSDATA_TILE = 16;
249+ static constexpr int64_t TRANSDATA_UB_BLOCK = 32;
250+ static constexpr int64_t TRANSDATA_UB_RESERVE = 4096;
251+ static constexpr int64_t TRANSDATA_MAX_M_16BIT = 256;
252+ static constexpr int64_t TRANSDATA_MAX_M_32BIT = 128;
253+ static constexpr int64_t TRANSDATA_MAX_REPEAT = 255;
254+ static constexpr int64_t TRANSDATA_MIN_BYTES = 512 * 1024;
255+ static constexpr int64_t TRANSDATA_MIN_BYTES_LEGACY = 4000000;
256+ // TilingData 中 m/n 为 int32, 准入时拒绝超范围 shape, 避免下发时静默截断
257+ static constexpr int64_t TRANSDATA_MAX_DIM_ELEMS = std::numeric_limits<int32_t>::max();
258+ 
259+ int64_t m = shapeInfo_.reducedInShape[0];
260+ int64_t n = shapeInfo_.reducedInShape[1];
261+ int64_t eleLen = shapeInfo_.eleLenInBytes;
262+ int64_t totalBytes = m * n * eleLen;
zhanw_coding
zhanw_codingzhanw_coding2 days ago

这些计算与下面的退出条件无关,建议挪到下面去

likedislike
263+ 
264+ auto ascendcPlatform = platform_ascendc::PlatformAscendC(tilingContext_->GetPlatformInfo());
265+ auto arch = ascendcPlatform.GetCurNpuArch();
266+ if (arch != NpuArch::DAV_3510 && arch != NpuArch::DAV_5102) {
267+ return ge::GRAPH_FAILED;
268+ }
269+ int64_t minBytes = (arch == NpuArch::DAV_3510) ? TRANSDATA_MIN_BYTES : TRANSDATA_MIN_BYTES_LEGACY;
270+ if (eleLen < static_cast<int64_t>(B16_BYTES) || eleLen > static_cast<int64_t>(B32_BYTES) ||
271+ m > TRANSDATA_MAX_DIM_ELEMS || n > TRANSDATA_MAX_DIM_ELEMS || m % TRANSDATA_TILE != 0 ||
272+ n % TRANSDATA_TILE != 0 || totalBytes < minBytes) {
273+ return ge::GRAPH_FAILED;
274+ }
atomgit-bot
atomgit-botatomgit-bot6 days ago

🟡 Medium Priority

变更行:transpose_tiling_arch35.cpp:257-265(Fill2DTransDataTilingData 的准入条件只有 arch、bit 宽、16 对齐、总字节数,完全没有使用 ubSize_)。

affected contract:kernel 侧 transpose_2d_transdata.h:112-117 用 LocalMemAllocator<Hardware::UB> 静态分配 in(1)+out(2) 三块 tile,尺寸与 ubSize 无关:

failure mode:当 compilerInfo->ubSize 小于 208KB 时,LocalMemAllocator::Alloc 仍会返回越界的 UB 地址,MTE2/V/MTE3 会踩到 UB 合法区之外,表现为精度错乱或 AIC Error。可触发证据:本 PR 新增/修改的所有 UT 都用 compileInfo.ubSize = 196608(192KB < 208KB),此时 tiling 仍返回 GRAPH_SUCCESS 并下发 10011;同文件其它策略(EntryTilingTemplate、CalcUBSplitInfo、B4、10007/10008 的 PlatInfo{coreNum_, ubSize_})全部按 ubSize_ 推导预算,只有新路径是固定 tile,形成与既有约定的不一致。

likedislike
Incorrect?
275+ 
276+ // tile 几何自适应:tileM 不超过 m(m<=cap 时保证整行搬出,不把连续输出行切碎),
277+ // tileN 按 UB 预算(tileM*(tileN+pad) + 2*tileN*(tileM+pad) 个元素)确定,
278+ // 避免 m 很小时 tile 退化成 16x128 的 4KB 小块。
279+ int64_t pad = TRANSDATA_UB_BLOCK / eleLen;
280+ int64_t maxTileM = (eleLen == static_cast<int64_t>(B16_BYTES)) ? TRANSDATA_MAX_M_16BIT : TRANSDATA_MAX_M_32BIT;
281+ int64_t tileM = std::min(m, maxTileM);
282+ int64_t ubBudgetElems = (ubSize_ - TRANSDATA_UB_RESERVE) / eleLen;
283+ int64_t maxTileN = (ubBudgetElems - tileM * pad) / (3 * tileM + 2 * pad);
284+ maxTileN = std::min(maxTileN, TRANSDATA_TILE * TRANSDATA_MAX_REPEAT);
285+ if (maxTileN < TRANSDATA_TILE) {
286+ return ge::GRAPH_FAILED;
287+ }
288+ // tileM 达到单块行数上限 (m 需要按 cap 行分块) 时, tileN 取 2 的幂次:
289+ // 此时 tile 已足够大, 2 的幂次行宽对齐收益更高 (实测 m=256/1024 更快);
290+ // 小 m 场景则取 UB 预算允许的最大 16 倍数, 用更少的 tile 摊薄固定开销。
291+ int64_t tileN = TRANSDATA_TILE;
292+ if (tileM == maxTileM) {
293+ while (tileN * 2 <= maxTileN) {
294+ tileN *= 2;
295+ }
296+ } else {
297+ tileN = (maxTileN / TRANSDATA_TILE) * TRANSDATA_TILE;
298+ }
299+ // 窄列场景 (n < tileN 预算, 如 131072x16): 每 tile 实际只用 n 列, 按 n 收紧
300+ // tileN, 改用行方向切分。tileM 取能满足 "行块数≈k*coreNum" 的最大值 (k 从小到大),
301+ // 既放大单 tile 又保证多核负载均衡 (避免行块数落在 coreNum 与 2*coreNum 之间)。
302+ if (n < tileN) {
303+ tileN = n;
304+ // 列宽只有一个 32B block 的整数倍时无需 padding 即可让 16 个并发源地址
305+ // 落在不同 bank; 此时去掉源侧 padding, tile 在 UB 内保持连续, 搬入可退化为
306+ // 单次连续 DataCopyPad。
307+ int64_t blockElems = TRANSDATA_UB_BLOCK / eleLen;
308+ int64_t srcPad = ((tileN / blockElems) % 2 == 1) ? 0 : pad;
309+ int64_t bestTileM = tileM;
310+ for (int64_t k = 1; k <= coreNum_; ++k) {
311+ int64_t cand = Ops::Base::CeilDiv(m, k * coreNum_);
312+ cand = Ops::Base::CeilAlign(cand, TRANSDATA_TILE);
313+ cand = std::max(cand, tileM);
314+ cand = std::min(cand, m);
315+ int64_t inElems = cand * (tileN + srcPad);
316+ int64_t outElems = tileN * (cand + pad);
317+ if (inElems + 2 * outElems <= ubBudgetElems) {
318+ bestTileM = cand;
319+ break;
320+ }
321+ }
322+ tileM = bestTileM;
323+ }
324+ 
325+ tilingData2D_.set_m(static_cast<int32_t>(m));
326+ tilingData2D_.set_n(static_cast<int32_t>(n));
327+ tilingData2D_.set_coreNum(static_cast<int32_t>(coreNum_));
328+ tilingData2D_.set_tileM(static_cast<int32_t>(tileM));
329+ tilingData2D_.set_tileN(static_cast<int32_t>(tileN));
330+ tilingData2D_.SaveToBuffer(tilingContext_->GetRawTilingData()->GetData(),
331+ tilingContext_->GetRawTilingData()->GetCapacity());
332+ tilingContext_->GetRawTilingData()->SetDataSize(tilingData2D_.GetDataSize());
333+ 
334+ tilingContext_->SetBlockDim(coreNum_);
335+ tilingContext_->SetTilingKey(static_cast<int64_t>(SplitMode::TRANSPOSE_2D_TRANSDATA));
336+ size_t* workspaces = tilingContext_->GetWorkspaceSizes(1);
337+ OP_CHECK_NULL_WITH_CONTEXT(tilingContext_, workspaces);
338+ workspaces[0] = 0;
339+ OP_LOGD(tilingContext_->GetNodeName(),
340+ "Fill2DTransDataTilingData success. m=%ld, n=%ld, coreNum=%ld, tileM=%ld, tileN=%ld", m, n, coreNum_, tileM,
341+ tileN);
342+ return ge::GRAPH_SUCCESS;
343+}
344+ 
215/**345/**
216 * @brief 判断 021 VCONV 路径是否有效346 * @brief 判断 021 VCONV 路径是否有效
217 *347 *
@@ -125,22 +125,35 @@ TILING_DATA_FIELD_DEF_STRUCT(TransposeOpTilingData, transposeOpTiling);
125END_TILING_DATA_DEF;125END_TILING_DATA_DEF;
126REGISTER_TILING_DATA_CLASS(Transpose, TransposeTilingData);126REGISTER_TILING_DATA_CLASS(Transpose, TransposeTilingData);
127 127 
128+/**
129+ * @brief 2D perm=[1,0] 特化模板的精简 Tiling 数据(10011 TRANSPOSE_2D_TRANSDATA 使用)
130+ */
131+BEGIN_TILING_DATA_DEF(Transpose2DTilingData)
132+TILING_DATA_FIELD_DEF(int32_t, m);
133+TILING_DATA_FIELD_DEF(int32_t, n);
134+TILING_DATA_FIELD_DEF(int32_t, coreNum);
atomgit-bot
atomgit-botatomgit-bot6 days ago

🟡 Medium Priority

新增的 Transpose2DTilingData 把二维 shape 参数 m、n 声明为 int32_t(coreNum 用 int32_t 无害,但 m/n 是 shape/偏移参数),与本文件既有 tiling 结构(TransposeOpTilingData 的 inputShape/outputShape/perm 等一律 int64_t)形成了新的一致性分歧。

证据链:

  1. 新增行 Transpose2DTilingData::set_m/set_n 的入参是 int64_t(transpose_tiling_arch35.cpp: int64_t m = shapeInfo_.reducedInShape[0]; int64_t n = ...,随后 set_m(static_cast<int32_t>(m))),即宿主侧在 int64 域做 shape 分析,下发时静默收窄到 32 位;
  2. 宿主侧准入条件只有下界(transpose_tiling_arch35.cpp:261-265 仅校验 eleLenInBytes∈[2,4]、m%16==0、n%16==0、totalBytes >= 512KB/4MB),没有 m、n 的上限校验,也没有 m*n 的乘积校验;
  3. 该结构没有任何 totalElements/元素字节数字段,kernel 侧必须自行用 m_、n_ 推导行偏移与总元素数(mIdx*n_ + nIdx 之类),一旦这些乘法在 32 位域完成,收窄后的字段会让 64 位语义失效。

失效模式:触发条件为「2D perm=[1,0] + 16bit + 大 shape」。例:65536×65536 fp16 共 4.3e9 元素(约 8.6GB,64GB 显存可容纳,且远大于 512KB 门槛),m*n 已超 INT32_MAX(2.1e9);若 kernel 侧基于 m_*n_ 在 32 位域计算总元素数/跨行偏移,会溢出为负值或错值,导致 GM 读写偏移错误(精度错乱或越界访问);单维超过 2^31 时 set_m 本身还会静默截断。若 kernel 内部已把 m_/n_ 提升为 int64 运算,则风险降为契约/可维护性问题,故 confidence 取 0.5。

建议:把 m、n 改为 int64_t(与 TransposeOpTilingData 中其他 shape 字段一致),并在 Fill2DTransDataTilingData 中增加 mn(或 mn*eleLenInBytes)的上限校验,不满足时返回 GRAPH_FAILED 回落到其他策略。

likedislike
Incorrect?
135+TILING_DATA_FIELD_DEF(int32_t, tileM);
136+TILING_DATA_FIELD_DEF(int32_t, tileN);
137+END_TILING_DATA_DEF;
138+REGISTER_TILING_DATA_CLASS(Transpose_10011, Transpose2DTilingData);
139+ 
128/**140/**
129 * @brief 9种 Tiling 策略的 Key 值枚举141 * @brief 9种 Tiling 策略的 Key 值枚举
130 *142 *
131 */143 */
132enum class SplitMode : int64_t {144enum class SplitMode : int64_t {
133- TENSOR_MOVE = 10000, // 仅1维,纯数据搬运145+ TENSOR_MOVE = 10000, // 仅1维,纯数据搬运
134- SMALL_SHAPE = 10001, // 小shape,SIMT模式GM→GM146+ SMALL_SHAPE = 10001, // 小shape,SIMT模式GM→GM
135- CUT_ONCE = 10002, // NDDMA单轴切分147+ CUT_ONCE = 10002, // NDDMA单轴切分
136- CUT_TWICE = 10003, // NDDMA双轴切分148+ CUT_TWICE = 10003, // NDDMA双轴切分
137- N_LAST_TRANSPOSE = 10004, // 尾轴不转置+连续行搬移149+ N_LAST_TRANSPOSE = 10004, // 尾轴不转置+连续行搬移
138- BIG_DIM = 10005, // >5维压缩到5维NDDMA150+ BIG_DIM = 10005, // >5维压缩到5维NDDMA
139- GATHER_TRANSPOSE = 10006, // DataCopyGather硬件加速151+ GATHER_TRANSPOSE = 10006, // DataCopyGather硬件加速
140- VCONV_TRANSPOSE = 10007, // TransDataTo5HD 2D转置(DAV_5102)152+ VCONV_TRANSPOSE = 10007, // TransDataTo5HD 2D转置(DAV_5102)
141- VCONV_021_TRANSPOSE = 10008, // TransDataTo5HD 3D 021转置(DAV_5102)153+ VCONV_021_TRANSPOSE = 10008, // TransDataTo5HD 3D 021转置(DAV_5102)
142- B4_DOUBLE_SPLIT = 10009, // B4 双切分154+ B4_DOUBLE_SPLIT = 10009, // B4 双切分
143- NDDMA_BASE = 90000 // NDDMA基础key(非实际使用的key,用于策略选型中间状态)155+ TRANSPOSE_2D_TRANSDATA = 10011, // 2D perm=[1,0] 特化,TransDataTo5HD 全tile转置
156+ NDDMA_BASE = 90000 // NDDMA基础key(非实际使用的key,用于策略选型中间状态)
144};157};
145 158 
146/**159/**
@@ -219,6 +232,8 @@ private:
219 ge::graphStatus AdjustShapeForB4();232 ge::graphStatus AdjustShapeForB4();
220 ge::graphStatus TryVCONVTiling();233 ge::graphStatus TryVCONVTiling();
221 bool Is021VConvValid();234 bool Is021VConvValid();
235+ bool Is2DPermutation();
236+ ge::graphStatus Fill2DTransDataTilingData();
222 void FlushBaseNumForBigDim();237 void FlushBaseNumForBigDim();
223 void EntryTilingTemplate();238 void EntryTilingTemplate();
224 void CalcUBSplitInfo();239 void CalcUBSplitInfo();
@@ -257,6 +272,7 @@ private:
257 272 
258private:273private:
259 TransposeTilingData tilingData_;274 TransposeTilingData tilingData_;
275+ Transpose2DTilingData tilingData2D_;
260 gert::TilingContext* tilingContext_ = nullptr;276 gert::TilingContext* tilingContext_ = nullptr;
261 277 
262 int64_t realCoreNum_ = 0;278 int64_t realCoreNum_ = 0;
@@ -0,0 +1,343 @@
1+/**
2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+/*!
12+ * \file transpose_2d_transdata.h
13+ * \brief tilingKey 10011 TRANSPOSE_2D_TRANSDATA: TransDataTo5HD 全 full-tile 转置
14+ *
15+ * 适用场景:2D perm=[1,0],dtype 为 16bit/32bit,且 m/n 均 16 对齐。
16+ * 核心机制:按 tile 搬入 UB 后使用 TransDataTo5HD 硬件指令完成 16x16 块转置,
17+ * 再按列搬出到输出 GM,MTE2/V/MTE3 通过 Mutex credit 组成三流水。
18+ */
19+#ifndef TRANSPOSE_2D_TRANSDATA_H
20+#define TRANSPOSE_2D_TRANSDATA_H
21+ 
22+#include "transpose_base.h"
23+ 
24+namespace Transpose {
25+using namespace AscendC;
26+ 
27+static constexpr uint32_t TD_TRANSELEM = 16;
28+ 
29+template <typename T>
30+class Transpose2DTransData {
31+public:
32+ __aicore__ inline Transpose2DTransData(){};
33+ __aicore__ inline ~Transpose2DTransData()
34+ {
35+ ReleaseMutexID(mutexIn0_);
36+ ReleaseMutexID(mutexOut0_);
37+ ReleaseMutexID(mutexOut1_);
38+ }
39+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, __tiling_data_ptr__ Transpose2DTilingData* tilingData);
40+ __aicore__ inline void Process();
41+ 
42+private:
43+ __aicore__ inline void ProcessTile(uint32_t inRow, uint32_t inCol, uint32_t rows, uint32_t cols,
44+ LocalTensor<T>& tin, LocalTensor<T>& tout, MutexID mIn, MutexID mOut);
45+ __aicore__ inline void ComputeTranspose(LocalTensor<T>& src, LocalTensor<T>& dst, uint32_t rows, uint32_t cols);
46+ 
47+ // tile 几何由 tiling 下发: tileM 跟随 m (m<=cap 时取 m, 保证整行搬出不被切碎),
48+ // tileN 由 UB 预算决定。避免 m 很小时 tile 退化成 4KB 小块。
49+ int32_t tileM_ = 0;
50+ int32_t tileN_ = 0;
51+ uint32_t tmcPad_ = 0;
52+ uint32_t tmrPad_ = 0;
53+ 
54+ GlobalTensor<T> inputGM_;
55+ GlobalTensor<T> outputGM_;
56+ int32_t blockIdx_;
57+ int32_t m_;
58+ int32_t n_;
59+ int32_t coreNum_;
60+ __tiling_data_ptr__ Transpose2DTilingData* tiling_;
61+ 
62+ // 静态Tensor: LocalMemAllocator分配, 不通过TPipe (in 单缓冲, out 双缓冲)
63+ LocalTensor<T> inPing;
64+ LocalTensor<T> outPing;
65+ LocalTensor<T> outPong;
66+ 
67+ // Mutex 按 buffer 生命周期拆分: mIn 门控 inbuf 复用 (MTE2写/V读交替),
68+ // mOut[b] 门控 outbuf[b] 复用 (V写/MTE3读交替), 各自独立归还, 消除跨段过度同步
69+ MutexID mutexIn0_{0};
70+ MutexID mutexOut0_{0};
71+ MutexID mutexOut1_{0};
72+ 
73+ // UB bank 冲突规避 (DAV_3510: 16bank x 512行, 8group x 2bank, 每组 2 读口+2 写口):
74+ // 行步长额外 +32B (PAD_EL=32/S 个元素) 拉开 16 个并发 sL/dL 地址的 bank 分布。
75+ static constexpr uint32_t PAD_EL = BLOCK_SIZE_BYTE / sizeof(T);
76+ 
77+ // Scalar hoisting: full-tile 预计算参数 (标量成员, 非数组间接读)
78+ DataCopyExtParams fullCopyIn_;
79+ DataCopyExtParams fullCopyOut_;
80+ uint8_t fullCB_ = 0; // full tile 的 TransDataTo5HD repeatTimes
81+};
82+ 
83+template <typename T>
84+__aicore__ inline void Transpose2DTransData<T>::Init(GM_ADDR x, GM_ADDR y,
85+ __tiling_data_ptr__ Transpose2DTilingData* tilingData)
86+{
87+ AscendC::InitSocState();
88+ blockIdx_ = GetBlockIdx();
89+ tiling_ = tilingData;
90+ m_ = tiling_->m;
91+ n_ = tiling_->n;
92+ coreNum_ = tiling_->coreNum;
93+ tileM_ = tiling_->tileM;
94+ tileN_ = tiling_->tileN;
95+ // 窄列且列宽为奇数个 32B block 时无需源侧 padding 即可避免 bank 冲突,
96+ // 此时 UB 内输入 tile 连续, 搬入可退化为单次连续 DataCopyPad。
97+ constexpr uint32_t BLOCK_ELEMS = static_cast<uint32_t>(BLOCK_SIZE_BYTE) / sizeof(T);
98+ uint32_t srcPad = PAD_EL;
99+ if (tileN_ == n_ && ((static_cast<uint32_t>(tileN_) / BLOCK_ELEMS) % 2U == 1U)) {
100+ srcPad = 0;
101+ }
102+ tmcPad_ = static_cast<uint32_t>(tileN_) + srcPad;
103+ tmrPad_ = static_cast<uint32_t>(tileM_) + PAD_EL;
104+ inputGM_.SetGlobalBuffer((__gm__ T*)x);
105+ outputGM_.SetGlobalBuffer((__gm__ T*)y);
106+ 
107+ mutexIn0_ = AllocMutexID();
108+ mutexOut0_ = AllocMutexID();
109+ mutexOut1_ = AllocMutexID();
110+ 
111+ // 静态Tensor: 用LocalMemAllocator分配UB, 行步长含 padding (bank 冲突规避)
112+ // 池深 in 1+out 2。tile 尺寸由 tiling 自适应给出, 预算约 248KB。
113+ // MTE3(慢侧)保留双缓冲; in 单缓冲下 MTE2(t+1) 仅等 V(t) 读完 (mIn 在 V 读后即归还),
114+ // 周期 = max(MTE2+V, MTE3)。
115+ const uint32_t inTileElems = static_cast<uint32_t>(tileM_) * tmcPad_;
116+ const uint32_t outTileElems = static_cast<uint32_t>(tileN_) * tmrPad_;
117+ LocalMemAllocator<Hardware::UB> ubAlloc;
118+ inPing = ubAlloc.Alloc<T>(inTileElems);
119+ outPing = ubAlloc.Alloc<T>(outTileElems);
120+ outPong = ubAlloc.Alloc<T>(outTileElems);
121+ 
122+ // Scalar hoisting: full-tile 常量参数预计算 (标量, Init 一次)
123+ constexpr uint32_t S = sizeof(T);
124+ if (tmcPad_ == static_cast<uint32_t>(tileN_)) {
125+ fullCopyIn_.blockCount = 1;
126+ fullCopyIn_.blockLen = static_cast<uint32_t>(tileM_) * static_cast<uint32_t>(tileN_) * S;
127+ fullCopyIn_.srcStride = 0;
128+ fullCopyIn_.dstStride = 0;
129+ } else {
130+ fullCopyIn_.blockCount = static_cast<uint16_t>(tileM_);
131+ fullCopyIn_.blockLen = static_cast<uint32_t>(tileN_) * S;
132+ fullCopyIn_.srcStride = static_cast<int64_t>(n_ - tileN_) * static_cast<int64_t>(S);
133+ fullCopyIn_.dstStride = static_cast<int64_t>(PAD_EL * S) / static_cast<int64_t>(BLOCK_SIZE_BYTE);
134+ }
135+ 
136+ fullCopyOut_.blockCount = static_cast<uint16_t>(tileN_);
137+ fullCopyOut_.blockLen = static_cast<uint32_t>(tileM_) * S;
138+ fullCopyOut_.srcStride = static_cast<int64_t>(PAD_EL * S) / static_cast<int64_t>(BLOCK_SIZE_BYTE);
139+ fullCopyOut_.dstStride = static_cast<int64_t>(m_ - tileM_) * static_cast<int64_t>(S);
140+ 
141+ fullCB_ = static_cast<uint8_t>(tileN_ / TD_TRANSELEM);
142+}
143+ 
144+template <typename T>
145+__aicore__ inline void Transpose2DTransData<T>::ComputeTranspose(LocalTensor<T>& src, LocalTensor<T>& dst,
146+ uint32_t rows, uint32_t cols)
147+{
148+ if constexpr (sizeof(T) <= sizeof(int32_t)) {
149+ constexpr uint32_t S = sizeof(T);
150+ constexpr uint32_t BS = static_cast<uint32_t>(BLOCK_SIZE_BYTE) / S;
151+ constexpr uint32_t halfBlocks = TD_TRANSELEM / BS;
152+ uint32_t rB = rows / TD_TRANSELEM;
153+ uint32_t cB = (cols == static_cast<uint32_t>(tileN_)) ? fullCB_ : static_cast<uint8_t>(cols / TD_TRANSELEM);
154+ 
155+ // Scalar hoisting: 增量地址计算 — 基址一次算好, 内循环只加常量步长
156+ // (消除 16 次乘法 + 16 次 GetPhyAddr 调用)
157+ const uint64_t srcPhysBase = reinterpret_cast<uint64_t>(src.GetPhyAddr());
158+ const uint64_t dstPhysBase = reinterpret_cast<uint64_t>(dst.GetPhyAddr());
159+ const uint64_t srcStep = static_cast<uint64_t>(tmcPad_) * S;
160+ const uint64_t dstStep = static_cast<uint64_t>(tmrPad_) * S;
161+ 
162+ // 列方向只有一个 16x16 block (窄列 n=16): 把 repeat 用在行块方向,
163+ // 一次指令覆盖最多 255 个行块, 减少 TransDataTo5HD 的发射与地址构造开销。
164+ if (cB == 1) {
165+ const uint16_t dstRepUnit = static_cast<uint16_t>((TD_TRANSELEM * S) / BLOCK_SIZE_BYTE);
166+ const uint16_t srcRepUnit = static_cast<uint16_t>((TD_TRANSELEM * tmcPad_ * S) / BLOCK_SIZE_BYTE);
167+ uint32_t a = 0;
168+ while (a < rB) {
169+ uint32_t chunk = (rB - a > 255U) ? 255U : (rB - a);
170+ for (uint32_t p = 0; p < halfBlocks; ++p) {
171+ uint64_t sL[16];
172+ uint64_t dL[16];
173+ uint64_t sa = srcPhysBase + (static_cast<uint64_t>(a) * TD_TRANSELEM * tmcPad_) * S +
174+ static_cast<uint64_t>(p * BS) * S;
175+ uint64_t da = dstPhysBase + (static_cast<uint64_t>(a) * TD_TRANSELEM) * S +
176+ static_cast<uint64_t>(p * BS) * tmrPad_ * S;
177+ for (int n = 0; n < 16; ++n) {
178+ sL[n] = sa;
179+ sa += srcStep;
180+ }
181+ if constexpr (halfBlocks == 1) {
182+ for (int n = 0; n < 16; ++n) {
183+ dL[n] = da;
184+ da += dstStep;
185+ }
186+ } else {
187+ constexpr uint64_t bsStep = static_cast<uint64_t>(BS) * S;
188+ for (int n = 0; n < 16; n += 2) {
189+ dL[n] = da;
190+ dL[n + 1] = da + bsStep;
191+ da += dstStep;
192+ }
193+ }
194+ PipeBarrier<PIPE_V>();
195+ const uint16_t dstRS = (chunk == 1) ? 0 : dstRepUnit;
196+ const uint16_t srcRS = (chunk == 1) ? 0 : srcRepUnit;
197+ TransDataTo5HDParams pr(false, false, static_cast<uint8_t>(chunk), dstRS, srcRS);
198+ TransDataTo5HD<T>(dL, sL, pr);
199+ }
200+ a += chunk;
201+ }
202+ return;
203+ }
204+ 
205+ for (uint32_t a = 0; a < rB; ++a) {
206+ uint64_t srcAddr = srcPhysBase + (static_cast<uint64_t>(a * TD_TRANSELEM) * tmcPad_) * S;
207+ uint64_t dstColBase = dstPhysBase + (static_cast<uint64_t>(a * TD_TRANSELEM)) * S;
208+ for (uint32_t p = 0; p < halfBlocks; ++p) {
209+ uint64_t sL[16];
210+ uint64_t dL[16];
211+ 
212+ // src: 基址 + p*BS*S, 16 个地址按 srcStep 递增
213+ uint64_t sa = srcAddr + p * BS * S;
214+ for (int n = 0; n < 16; ++n) {
215+ sL[n] = sa;
216+ sa += srcStep;
217+ }
218+ 
219+ // dst: 基址 + p*BS*tmrPad_*S, 按模式递增
220+ if constexpr (halfBlocks == 1) {
221+ uint64_t da = dstColBase + static_cast<uint64_t>(p * BS) * tmrPad_ * S;
222+ for (int n = 0; n < 16; ++n) {
223+ dL[n] = da;
224+ da += dstStep;
225+ }
226+ } else {
227+ uint64_t da = dstColBase + static_cast<uint64_t>(p * BS) * tmrPad_ * S;
228+ constexpr uint64_t bsStep = static_cast<uint64_t>(BS) * S;
229+ for (int n = 0; n < 16; n += 2) {
230+ dL[n] = da;
231+ dL[n + 1] = da + bsStep;
232+ da += dstStep;
233+ }
234+ }
235+ 
236+ PipeBarrier<PIPE_V>();
237+ // repeatTimes==1 时没有下一个 repeat,repStride 必须为 0;
238+ // 否则指令不写出(tail tile 恰好 16 列时输出全零)。
239+ const uint16_t dstRepStride = (cB == 1) ? 0 : static_cast<uint16_t>(tmrPad_ * TD_TRANSELEM / BS);
240+ const uint16_t srcRepStride = (cB == 1) ? 0 : static_cast<uint16_t>(TD_TRANSELEM / BS);
241+ TransDataTo5HDParams pr(false, false, static_cast<uint8_t>(cB), dstRepStride, srcRepStride);
242+ TransDataTo5HD<T>(dL, sL, pr);
243+ }
244+ }
245+ }
246+}
247+ 
248+template <typename T>
249+__aicore__ inline void Transpose2DTransData<T>::ProcessTile(uint32_t inRow, uint32_t inCol, uint32_t rows,
250+ uint32_t cols, LocalTensor<T>& tin, LocalTensor<T>& tout,
251+ MutexID mIn, MutexID mOut)
252+{
253+ constexpr uint32_t S = sizeof(T);
254+ DataCopyPadExtParams<T> padParams{false, 0, 0, 0};
255+ 
256+ // Scalar hoisting: full tile 用预计算参数, 尾 tile 现算
257+ DataCopyExtParams copyInParams;
258+ DataCopyExtParams copyOutParams;
259+ const bool isFull = (rows == static_cast<uint32_t>(tileM_)) && (cols == static_cast<uint32_t>(tileN_));
260+ if (isFull) {
261+ copyInParams = fullCopyIn_;
262+ copyOutParams = fullCopyOut_;
263+ } else {
264+ if (tmcPad_ == static_cast<uint32_t>(tileN_) && cols == static_cast<uint32_t>(tileN_)) {
265+ copyInParams.blockCount = 1;
266+ copyInParams.blockLen = rows * cols * S;
267+ copyInParams.srcStride = 0;
268+ copyInParams.dstStride = 0;
269+ } else {
270+ copyInParams.blockCount = static_cast<uint16_t>(rows);
271+ copyInParams.blockLen = cols * S;
272+ copyInParams.srcStride = static_cast<int64_t>((n_ - cols) * S);
273+ copyInParams.dstStride = static_cast<int64_t>((tmcPad_ - cols) * S) / static_cast<int64_t>(BLOCK_SIZE_BYTE);
274+ }
275+ copyOutParams.blockCount = static_cast<uint16_t>(cols);
276+ copyOutParams.blockLen = rows * S;
277+ copyOutParams.srcStride = static_cast<int64_t>((tmrPad_ - rows) * S) / static_cast<int64_t>(BLOCK_SIZE_BYTE);
278+ copyOutParams.dstStride = static_cast<int64_t>((m_ - rows) * S);
279+ }
280+ 
281+ // MTE2 搬入: mIn 门控 inbuf 复用 — 上一轮读者是 V(i-2), 其计算完成即归还
282+ Mutex::Lock<PIPE_MTE2>(mIn);
283+ DataCopyPad(tin, inputGM_[(uint64_t)inRow * n_ + inCol], copyInParams, padParams);
284+ Mutex::Unlock<PIPE_MTE2>(mIn);
285+ 
286+ // V 转置: 等 mIn (本轮搬入完成) + mOut (outbuf 空闲, 上一轮 MTE3(i-2) 搬出完已归还)
287+ Mutex::Lock<PIPE_V>(mIn);
288+ Mutex::Lock<PIPE_V>(mOut);
289+ ComputeTranspose(tin, tout, rows, cols);
290+ Mutex::Unlock<PIPE_V>(mIn); // inbuf 读完即归还 → MTE2(i+2) 可写, 不再等 MTE3
291+ Mutex::Unlock<PIPE_V>(mOut); // outbuf 写完 → MTE3(i) 可搬出
292+ 
293+ // V 转置 → MTE3 搬出, mOut 门控数据就绪
294+ Mutex::Lock<PIPE_MTE3>(mOut);
295+ DataCopyPad(outputGM_[(uint64_t)inCol * m_ + inRow], tout, copyOutParams);
296+ Mutex::Unlock<PIPE_MTE3>(mOut); // 搬出完 → outbuf 归还, V(i+2) 可写
297+}
298+ 
299+template <typename T>
300+__aicore__ inline void Transpose2DTransData<T>::Process()
301+{
302+ uint32_t bid = static_cast<uint32_t>(blockIdx_);
303+ uint32_t bnum = static_cast<uint32_t>(coreNum_);
304+ 
305+ uint32_t tileM = static_cast<uint32_t>(tileM_);
306+ uint32_t tileN = static_cast<uint32_t>(tileN_);
307+ uint32_t rowTiles = (static_cast<uint32_t>(m_) + tileM - 1) / tileM;
308+ uint32_t colTiles = (static_cast<uint32_t>(n_) + tileN - 1) / tileN;
309+ uint32_t total = rowTiles * colTiles;
310+ 
311+ // Scalar hoisting: 增量推进 (rowIdx, colIdx) 替代每 tile 的 t/colTiles 整数除法
312+ // t 步进 bnum: 分解 bnum = stepQ*colTiles + stepR, 每 tile 仅一次条件减法
313+ uint32_t stepQ = bnum / colTiles;
314+ uint32_t stepR = bnum % colTiles;
315+ uint32_t rowIdx = bid / colTiles;
316+ uint32_t colIdx = bid % colTiles;
317+ 
318+ uint32_t i = 0;
319+ for (uint32_t t = bid; t < total; t += bnum, ++i) {
320+ uint32_t inRow = rowIdx * tileM;
321+ uint32_t inCol = colIdx * tileN;
322+ uint32_t rows = (inRow + tileM <= static_cast<uint32_t>(m_)) ? tileM : (m_ - inRow);
323+ uint32_t cols = (inCol + tileN <= static_cast<uint32_t>(n_)) ? tileN : (n_ - inCol);
324+ // Ping/Pong 交替 (out 池): in 单缓冲下 MTE2(t+1) 与 MTE3(t) 并行,
325+ // 仅被 V(t) 读完门控
326+ uint32_t b = i & 1;
327+ MutexID curIn = mutexIn0_;
328+ MutexID curOut = b ? mutexOut1_ : mutexOut0_;
329+ LocalTensor<T>& tin = inPing;
330+ LocalTensor<T>& tout = b ? outPong : outPing;
331+ ProcessTile(inRow, inCol, rows, cols, tin, tout, curIn, curOut);
332+ colIdx += stepR;
333+ rowIdx += stepQ;
334+ if (colIdx >= colTiles) {
335+ colIdx -= colTiles;
336+ ++rowIdx;
337+ }
338+ }
339+}
340+ 
341+} // namespace Transpose
342+ 
343+#endif // TRANSPOSE_2D_TRANSDATA_H
@@ -39,6 +39,7 @@
39 * 10006 GATHER_TRANSPOSE - 尾轴转置+大shape,DataCopyGather硬件指令+RegBase39 * 10006 GATHER_TRANSPOSE - 尾轴转置+大shape,DataCopyGather硬件指令+RegBase
40 * 10007 VCONV_TRANSPOSE - 2D perm=[1,0]+16bit,TransDataTo5HD硬件指令40 * 10007 VCONV_TRANSPOSE - 2D perm=[1,0]+16bit,TransDataTo5HD硬件指令
41 * 10008 VCONV_021_TRANSPOSE - 3D perm=[0,2,1]+8/16/32bit,TransDataTo5HD硬件指令41 * 10008 VCONV_021_TRANSPOSE - 3D perm=[0,2,1]+8/16/32bit,TransDataTo5HD硬件指令
42+ * 10011 TRANSPOSE_2D_TRANSDATA - 2D perm=[1,0] 特化,TransDataTo5HD 全 tile 转置
42 */43 */
43 44 
44#include "arch35/transpose_big_dim.h"45#include "arch35/transpose_big_dim.h"
@@ -52,6 +53,7 @@
52#include "arch35/transpose_with_gather.h"53#include "arch35/transpose_with_gather.h"
53#include "arch35/transpose_transdata_5hd.h"54#include "arch35/transpose_transdata_5hd.h"
54#include "arch35/transpose_transdata_5hd_021.h"55#include "arch35/transpose_transdata_5hd_021.h"
56+#include "arch35/transpose_2d_transdata.h"
55#ifdef __CCE_AICORE_SUPPORT_SIMT__57#ifdef __CCE_AICORE_SUPPORT_SIMT__
56#include "arch35/transpose_b4.h"58#include "arch35/transpose_b4.h"
57#endif59#endif
@@ -65,8 +67,9 @@
65#define BIG_DIM 10005 // 维度>5,压缩到5维NDDMA格式进行搬运67#define BIG_DIM 10005 // 维度>5,压缩到5维NDDMA格式进行搬运
66#define GATHER_TRANSPOSE 10006 // 尾轴转置+大shape,使用DataCopyGather硬件指令(RegBase路径)68#define GATHER_TRANSPOSE 10006 // 尾轴转置+大shape,使用DataCopyGather硬件指令(RegBase路径)
67#define VCONV_TRANSPOSE 10007 // 2D perm=[1,0]+16bit(DAV_5102),TransDataTo5HD硬件指令69#define VCONV_TRANSPOSE 10007 // 2D perm=[1,0]+16bit(DAV_5102),TransDataTo5HD硬件指令
68-#define VCONV_021_TRANSPOSE 10008 // 3D perm=[0,2,1]+8/16/32bit(DAV_5102),TransDataTo5HD硬件指令70+#define VCONV_021_TRANSPOSE 10008 // 3D perm=[0,2,1]+8/16/32bit(DAV_5102),TransDataTo5HD硬件指令
69-#define B4_DUAL_CUT 10009 // B4 双切分71+#define TRANSPOSE_2D_TRANSDATA 10011 // 2D perm=[1,0] 特化,TransDataTo5HD 全 tile 转置
72+#define B4_DUAL_CUT 10009 // B4 双切分
70 73 
71using namespace Transpose;74using namespace Transpose;
72 75 
@@ -383,6 +386,29 @@ extern "C" __aicore__ inline void Transpose021VconvProcess(GM_ADDR x, GM_ADDR y,
383 }386 }
384}387}
385 388 
389+/**
390+ * @brief TRANSPOSE_2D_TRANSDATA(10011) 策略的 Process 函数
391+ * 2D perm=[1,0] 特化路径:dtype 为 16bit/32bit 且 m/n 均 16 对齐。
392+ * 按 tile 搬入 UB 后用 TransDataTo5HD 完成 16x16 块转置,再按列搬出。
393+ *
394+ * @param x 输入张量 GM 地址
395+ * @param y 输出张量 GM 地址
396+ * @param tilingData Tiling 数据指针(Transpose2DTilingData 结构)
397+ */
398+extern "C" __aicore__ inline void Transpose2DTransDataProcess(GM_ADDR x, GM_ADDR y,
399+ __tiling_data_ptr__ Transpose2DTilingData* tilingData)
400+{
401+ if constexpr (sizeof(DTYPE_X) == sizeof(int16_t)) {
402+ Transpose::Transpose2DTransData<int16_t> op;
403+ op.Init(x, y, tilingData);
404+ op.Process();
405+ } else if constexpr (sizeof(DTYPE_X) == sizeof(int32_t)) {
406+ Transpose::Transpose2DTransData<int32_t> op;
407+ op.Init(x, y, tilingData);
408+ op.Process();
409+ }
410+}
411+ 
386#ifdef __CCE_AICORE_SUPPORT_SIMT__412#ifdef __CCE_AICORE_SUPPORT_SIMT__
387extern "C" __aicore__ inline void TransposeHIF4DualCutProcess(GM_ADDR x, GM_ADDR y,413extern "C" __aicore__ inline void TransposeHIF4DualCutProcess(GM_ADDR x, GM_ADDR y,
388 const HIF4DualCutTilingData* tilingData, TPipe* pipe)414 const HIF4DualCutTilingData* tilingData, TPipe* pipe)
@@ -407,6 +433,7 @@ extern "C" __aicore__ inline void TransposeHIF4DualCutProcess(GM_ADDR x, GM_ADDR
407 * - TilingKey 10006 使用 GatherTransposeTilingData433 * - TilingKey 10006 使用 GatherTransposeTilingData
408 * - TilingKey 10007 使用 TransposeVCONVTilingData434 * - TilingKey 10007 使用 TransposeVCONVTilingData
409 * - TilingKey 10008 使用 Transpose021VCONVTilingData435 * - TilingKey 10008 使用 Transpose021VCONVTilingData
436+ * - TilingKey 10011 使用 Transpose2DTilingData
410 *437 *
411 * @param x 输入张量 GM 地址438 * @param x 输入张量 GM 地址
412 * @param perm perm 数组 GM 地址(Kernel 侧不直接使用,通过 TilingData 传递)439 * @param perm perm 数组 GM 地址(Kernel 侧不直接使用,通过 TilingData 传递)
@@ -449,6 +476,9 @@ extern "C" __global__ __aicore__ void transpose(GM_ADDR x, GM_ADDR perm, GM_ADDR
449 } else if (TILING_KEY_IS(VCONV_021_TRANSPOSE)) { // 10008: TransDataTo5HD 3D 021转置476 } else if (TILING_KEY_IS(VCONV_021_TRANSPOSE)) { // 10008: TransDataTo5HD 3D 021转置
450 GET_TILING_DATA_WITH_STRUCT(Transpose021VCONVTilingData, tilingData, tiling);477 GET_TILING_DATA_WITH_STRUCT(Transpose021VCONVTilingData, tilingData, tiling);
451 Transpose021VconvProcess(x, y, &tilingData, &pipe);478 Transpose021VconvProcess(x, y, &tilingData, &pipe);
479+ } else if (TILING_KEY_IS(TRANSPOSE_2D_TRANSDATA)) { // 10011: 2D TransDataTo5HD 全tile转置
480+ GET_TILING_DATA_PTR_WITH_STRUCT(Transpose2DTilingData, tilingDataPtr, tiling);
481+ Transpose2DTransDataProcess(x, y, tilingDataPtr);
452 }482 }
453#ifdef __CCE_AICORE_SUPPORT_SIMT__483#ifdef __CCE_AICORE_SUPPORT_SIMT__
454 else if (TILING_KEY_IS(B4_DUAL_CUT)) {484 else if (TILING_KEY_IS(B4_DUAL_CUT)) {
@@ -322,22 +322,18 @@ TEST_F(TransposeTiling, transpose_tiling_n_last_4d_no_last_transpose)
322 compileInfo.coreNum = 40;322 compileInfo.coreNum = 40;
323 compileInfo.ubSize = 196608;323 compileInfo.ubSize = 196608;
324 324 
325- // perm = {0, 1, 3, 2}: last axis (dim=3) is transposed, but the last reduced axis stays325+ // perm = {1, 0, 2, 3}: 只交换前两轴, 尾轴保持不转置。MergeAxis 后尾轴 (30*64=1920)
326- // After merge: perm {0,1,3,2} where axis 2 is transposed (isLastAxisTranspose=true since perm[2]=3!=2)326+ // 仍不参与转置且 >= 32; 总量 4.6MB 超过 SMALL 门槛 → 走 N_LAST。
327- // For N_LAST we need isLastAxisTranspose=false AND last axis >= 32327+ int64_t perm_value[4] = {1, 0, 2, 3};
328- // perm = {0, 3, 1, 2} -> reduced perm might become {0, 2, 1} after removing 1-dim axes328+ gert::TilingContextPara::TensorDescription x({{30, 20, 30, 64}, {30, 20, 30, 64}}, ge::DT_FLOAT, ge::FORMAT_ND);
329- // But simpler: {0, 1, 2} (identity, last axis not transposed)329+ gert::TilingContextPara::TensorDescription perm({{4}, {4}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
330- int64_t perm_value[3] = {2, 0, 1};330+ gert::TilingContextPara::TensorDescription out({{20, 30, 30, 64}, {20, 30, 30, 64}}, ge::DT_FLOAT, ge::FORMAT_ND);
331- gert::TilingContextPara::TensorDescription x({{100, 200, 64}, {100, 200, 64}}, ge::DT_FLOAT, ge::FORMAT_ND);
332- gert::TilingContextPara::TensorDescription perm({{3}, {3}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
333- gert::TilingContextPara::TensorDescription out({{64, 100, 200}, {64, 100, 200}}, ge::DT_FLOAT, ge::FORMAT_ND);
334 gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);331 gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);
335 332 
336 TilingInfo tilingInfo;333 TilingInfo tilingInfo;
337 bool success = ExecuteTiling(tilingContextPara, tilingInfo);334 bool success = ExecuteTiling(tilingContextPara, tilingInfo);
338 EXPECT_TRUE(success);335 EXPECT_TRUE(success);
339- // On ascend950 (DAV_3510), this goes through N_LAST, NDDMA_BASE, GATHER, SMALL_SHAPE, or CUT paths336+ EXPECT_EQ(tilingInfo.tilingKey, static_cast<uint64_t>(optiling::SplitMode::N_LAST_TRANSPOSE));
340- EXPECT_TRUE(tilingInfo.tilingKey >= 10000 && tilingInfo.tilingKey <= 10008);
341}337}
342 338 
343// 4D NDDMA_BASE CUT_ONCE/CUT_TWICE path (large shape, dim <= 5, last axis transposed, not n_last)339// 4D NDDMA_BASE CUT_ONCE/CUT_TWICE path (large shape, dim <= 5, last axis transposed, not n_last)
@@ -347,17 +343,19 @@ TEST_F(TransposeTiling, transpose_tiling_nddma_base_4d)
347 compileInfo.coreNum = 40;343 compileInfo.coreNum = 40;
348 compileInfo.ubSize = 196608;344 compileInfo.ubSize = 196608;
349 345 
346+ // 总量 4.6MB 超过 SMALL 门槛; MergeAxis 后输入等效行数 30*21*30=18900 非 16 对齐,
347+ // 不命中小 m/N_LAST/10011 特化, 落到 NDDMA_BASE (CUT_ONCE/CUT_TWICE)。
350 int64_t perm_value[4] = {3, 0, 1, 2};348 int64_t perm_value[4] = {3, 0, 1, 2};
351- gert::TilingContextPara::TensorDescription x({{10, 20, 30, 64}, {10, 20, 30, 64}}, ge::DT_FLOAT, ge::FORMAT_ND);349+ gert::TilingContextPara::TensorDescription x({{30, 21, 30, 64}, {30, 21, 30, 64}}, ge::DT_FLOAT, ge::FORMAT_ND);
352 gert::TilingContextPara::TensorDescription perm({{4}, {4}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);350 gert::TilingContextPara::TensorDescription perm({{4}, {4}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
353- gert::TilingContextPara::TensorDescription out({{64, 10, 20, 30}, {64, 10, 20, 30}}, ge::DT_FLOAT, ge::FORMAT_ND);351+ gert::TilingContextPara::TensorDescription out({{64, 30, 21, 30}, {64, 30, 21, 30}}, ge::DT_FLOAT, ge::FORMAT_ND);
354 gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);352 gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);
355 353 
356 TilingInfo tilingInfo;354 TilingInfo tilingInfo;
357 bool success = ExecuteTiling(tilingContextPara, tilingInfo);355 bool success = ExecuteTiling(tilingContextPara, tilingInfo);
358 EXPECT_TRUE(success);356 EXPECT_TRUE(success);
359- // Should hit NDDMA_BASE (CUT_ONCE or CUT_TWICE), GATHER, or N_LAST path357+ EXPECT_TRUE(tilingInfo.tilingKey == static_cast<uint64_t>(optiling::SplitMode::CUT_ONCE) ||
360- EXPECT_TRUE(tilingInfo.tilingKey >= 10000 && tilingInfo.tilingKey <= 10008);358+ tilingInfo.tilingKey == static_cast<uint64_t>(optiling::SplitMode::CUT_TWICE));
361}359}
362 360 
363// BIG_DIM path (dim > 5), expect tiling key 10005361// BIG_DIM path (dim > 5), expect tiling key 10005
@@ -838,6 +836,136 @@ TEST_F(TransposeTiling, transpose_tiling_big_dim_6d_no_consecutive_perm)
838 EXPECT_EQ(tilingInfo.tilingKey, static_cast<uint64_t>(optiling::SplitMode::BIG_DIM));836 EXPECT_EQ(tilingInfo.tilingKey, static_cast<uint64_t>(optiling::SplitMode::BIG_DIM));
839}837}
840 838 
839+// 2D perm [1, 0], fp16, m/n 均 16 对齐, 512KB (DAV_3510 门槛): 走 10011 TransData 特化模板
840+TEST_F(TransposeTiling, transpose_tiling_2d_transdata_route)
841+{
842+ optiling::TransposeCompilerInfo compileInfo;
843+ compileInfo.coreNum = 40;
844+ compileInfo.ubSize = 253952;
845+ 
846+ int64_t perm_value[2] = {1, 0};
847+ gert::TilingContextPara::TensorDescription x({{512, 512}, {512, 512}}, ge::DT_FLOAT16, ge::FORMAT_ND);
848+ gert::TilingContextPara::TensorDescription perm({{2}, {2}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
849+ gert::TilingContextPara::TensorDescription out({{512, 512}, {512, 512}}, ge::DT_FLOAT16, ge::FORMAT_ND);
850+ gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);
851+ 
852+ TilingInfo tilingInfo;
853+ bool success = ExecuteTiling(tilingContextPara, tilingInfo);
854+ EXPECT_TRUE(success);
855+ EXPECT_EQ(tilingInfo.tilingKey, static_cast<uint64_t>(optiling::SplitMode::TRANSPOSE_2D_TRANSDATA));
856+ EXPECT_EQ(tilingInfo.blockNum, 40U);
857+ ASSERT_GE(tilingInfo.tilingDataSize, 5 * sizeof(int32_t));
858+ int32_t tilingData[5] = {0};
859+ std::memcpy(tilingData, tilingInfo.tilingData.get(), sizeof(tilingData));
860+ EXPECT_EQ(tilingData[0], 512);
861+ EXPECT_EQ(tilingData[1], 512);
862+ EXPECT_EQ(tilingData[2], 40);
863+ // m=512 > cap(256): tileM 取 256; 248KB UB 预算下 tileN 取最大 2 的幂次 128
864+ EXPECT_EQ(tilingData[3], 256);
865+ EXPECT_EQ(tilingData[4], 128);
866+}
867+ 
868+// 2D perm [1, 0], fp16, m=16: tileM 跟随 m, 避免 tile 退化成 16x128 的 4KB 小块
869+TEST_F(TransposeTiling, transpose_tiling_2d_transdata_small_m_tile)
870+{
871+ optiling::TransposeCompilerInfo compileInfo;
872+ compileInfo.coreNum = 40;
873+ compileInfo.ubSize = 253952;
874+ 
875+ int64_t perm_value[2] = {1, 0};
876+ gert::TilingContextPara::TensorDescription x({{16, 262144}, {16, 262144}}, ge::DT_FLOAT16, ge::FORMAT_ND);
877+ gert::TilingContextPara::TensorDescription perm({{2}, {2}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
878+ gert::TilingContextPara::TensorDescription out({{262144, 16}, {262144, 16}}, ge::DT_FLOAT16, ge::FORMAT_ND);
879+ gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);
880+ 
881+ TilingInfo tilingInfo;
882+ bool success = ExecuteTiling(tilingContextPara, tilingInfo);
883+ EXPECT_TRUE(success);
884+ EXPECT_EQ(tilingInfo.tilingKey, static_cast<uint64_t>(optiling::SplitMode::TRANSPOSE_2D_TRANSDATA));
885+ ASSERT_GE(tilingInfo.tilingDataSize, 5 * sizeof(int32_t));
886+ int32_t tilingData[5] = {0};
887+ std::memcpy(tilingData, tilingInfo.tilingData.get(), sizeof(tilingData));
888+ EXPECT_EQ(tilingData[0], 16);
889+ EXPECT_EQ(tilingData[1], 262144);
890+ EXPECT_EQ(tilingData[3], 16);
891+ EXPECT_EQ(tilingData[4], 1552);
892+}
893+ 
894+// 2D perm [1, 0], fp16, 窄列 (m=131072, n=16): 收起多余 tileN, 改为行方向切分,
895+// tileM 按 "行块数≈k*coreNum" 放大以保证多核负载均衡
896+TEST_F(TransposeTiling, transpose_tiling_2d_transdata_narrow_n_tile)
897+{
898+ optiling::TransposeCompilerInfo compileInfo;
899+ compileInfo.coreNum = 40;
900+ compileInfo.ubSize = 253952;
901+ 
902+ int64_t perm_value[2] = {1, 0};
903+ gert::TilingContextPara::TensorDescription x({{131072, 16}, {131072, 16}}, ge::DT_FLOAT16, ge::FORMAT_ND);
904+ gert::TilingContextPara::TensorDescription perm({{2}, {2}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
905+ gert::TilingContextPara::TensorDescription out({{16, 131072}, {16, 131072}}, ge::DT_FLOAT16, ge::FORMAT_ND);
906+ gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);
907+ 
908+ TilingInfo tilingInfo;
909+ bool success = ExecuteTiling(tilingContextPara, tilingInfo);
910+ EXPECT_TRUE(success);
911+ EXPECT_EQ(tilingInfo.tilingKey, static_cast<uint64_t>(optiling::SplitMode::TRANSPOSE_2D_TRANSDATA));
912+ ASSERT_GE(tilingInfo.tilingDataSize, 5 * sizeof(int32_t));
913+ int32_t tilingData[5] = {0};
914+ std::memcpy(tilingData, tilingInfo.tilingData.get(), sizeof(tilingData));
915+ EXPECT_EQ(tilingData[0], 131072);
916+ EXPECT_EQ(tilingData[1], 16);
917+ EXPECT_EQ(tilingData[3], 1648);
918+ EXPECT_EQ(tilingData[4], 16);
919+}
920+ 
921+// 2D perm [1, 0], fp16, m/n 均 16 对齐但不是 tile 整数倍 (1040x1040):
922+// 行、列方向均产生尾块, 覆盖 kernel 非 full-tile 的搬运/转置路径
923+TEST_F(TransposeTiling, transpose_tiling_2d_transdata_tail_tile)
924+{
925+ optiling::TransposeCompilerInfo compileInfo;
926+ compileInfo.coreNum = 40;
927+ compileInfo.ubSize = 253952;
928+ 
929+ int64_t perm_value[2] = {1, 0};
930+ gert::TilingContextPara::TensorDescription x({{1040, 1040}, {1040, 1040}}, ge::DT_FLOAT16, ge::FORMAT_ND);
931+ gert::TilingContextPara::TensorDescription perm({{2}, {2}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
932+ gert::TilingContextPara::TensorDescription out({{1040, 1040}, {1040, 1040}}, ge::DT_FLOAT16, ge::FORMAT_ND);
933+ gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);
934+ 
935+ TilingInfo tilingInfo;
936+ bool success = ExecuteTiling(tilingContextPara, tilingInfo);
937+ EXPECT_TRUE(success);
938+ EXPECT_EQ(tilingInfo.tilingKey, static_cast<uint64_t>(optiling::SplitMode::TRANSPOSE_2D_TRANSDATA));
939+ ASSERT_GE(tilingInfo.tilingDataSize, 5 * sizeof(int32_t));
940+ int32_t tilingData[5] = {0};
941+ std::memcpy(tilingData, tilingInfo.tilingData.get(), sizeof(tilingData));
942+ EXPECT_EQ(tilingData[0], 1040);
943+ EXPECT_EQ(tilingData[1], 1040);
944+ EXPECT_EQ(tilingData[2], 40);
945+ // tileM=256 / tileN=128: 1040 既不是 256 也不是 128 的整数倍, 行/列均有尾块
946+ EXPECT_EQ(tilingData[3], 256);
947+ EXPECT_EQ(tilingData[4], 128);
948+}
949+ 
950+// 2D perm [1, 0] 但 n 非 16 对齐: 10011 不适用, 回落主流程
951+TEST_F(TransposeTiling, transpose_tiling_2d_transdata_not_aligned_fallback)
952+{
953+ optiling::TransposeCompilerInfo compileInfo;
954+ compileInfo.coreNum = 40;
955+ compileInfo.ubSize = 253952;
956+ 
957+ int64_t perm_value[2] = {1, 0};
958+ gert::TilingContextPara::TensorDescription x({{512, 510}, {512, 510}}, ge::DT_FLOAT16, ge::FORMAT_ND);
959+ gert::TilingContextPara::TensorDescription perm({{2}, {2}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
960+ gert::TilingContextPara::TensorDescription out({{510, 512}, {510, 512}}, ge::DT_FLOAT16, ge::FORMAT_ND);
961+ gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);
962+ 
963+ TilingInfo tilingInfo;
964+ bool success = ExecuteTiling(tilingContextPara, tilingInfo);
965+ EXPECT_TRUE(success);
966+ EXPECT_NE(tilingInfo.tilingKey, static_cast<uint64_t>(optiling::SplitMode::TRANSPOSE_2D_TRANSDATA));
967+}
968+ 
841// BIG_DIM path: 6D with isLastAxisTranspose=true, GATHER may fail then BIG_DIM969// BIG_DIM path: 6D with isLastAxisTranspose=true, GATHER may fail then BIG_DIM
842TEST_F(TransposeTiling, transpose_tiling_big_dim_6d_gather_fallback)970TEST_F(TransposeTiling, transpose_tiling_big_dim_6d_gather_fallback)
843{971{
@@ -1030,10 +1158,11 @@ TEST_F(TransposeTiling, transpose_tiling_cut_twice_in_tail_only)
1030 EXPECT_TRUE(success);1158 EXPECT_TRUE(success);
1031}1159}
1032 1160 
1033-// Small shape [8000, 256] perm [1, 0] with 248K UB: sqrt(UB) barely misses axis 1 (256),1161+// Small shape [8000, 255] perm [1, 0] with 248K UB: sqrt(UB) barely misses axis 1 (255),
1034// absorption strategy promotes the cut to axis 0, but the effective outUbFactor amplified1162// absorption strategy promotes the cut to axis 0, but the effective outUbFactor amplified
1035-// by inUbFactor would overflow UB (248*248*256 > ubElement), so the safety validation in1163+// by inUbFactor would overflow UB (248*248*255 > ubElement), so the safety validation in
1036// DoSplitUB reverts to the original strategy, yielding CUT_TWICE.1164// DoSplitUB reverts to the original strategy, yielding CUT_TWICE.
1165+// 注: n=255 非 16 对齐, 用于避开 10011 TransData 特化路由。
1037TEST_F(TransposeTiling, transpose_tiling_small_shape_absorb_revert_to_cut_twice)1166TEST_F(TransposeTiling, transpose_tiling_small_shape_absorb_revert_to_cut_twice)
1038{1167{
1039 optiling::TransposeCompilerInfo compileInfo;1168 optiling::TransposeCompilerInfo compileInfo;
@@ -1041,9 +1170,9 @@ TEST_F(TransposeTiling, transpose_tiling_small_shape_absorb_revert_to_cut_twice)
1041 compileInfo.ubSize = 253952; // 248K1170 compileInfo.ubSize = 253952; // 248K
1042 1171 
1043 int64_t perm_value[2] = {1, 0};1172 int64_t perm_value[2] = {1, 0};
1044- gert::TilingContextPara::TensorDescription x({{8000, 256}, {8000, 256}}, ge::DT_FLOAT, ge::FORMAT_ND);1173+ gert::TilingContextPara::TensorDescription x({{8000, 255}, {8000, 255}}, ge::DT_FLOAT, ge::FORMAT_ND);
1045 gert::TilingContextPara::TensorDescription perm({{2}, {2}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);1174 gert::TilingContextPara::TensorDescription perm({{2}, {2}}, ge::DT_INT64, ge::FORMAT_ND, true, &perm_value);
1046- gert::TilingContextPara::TensorDescription out({{256, 8000}, {256, 8000}}, ge::DT_FLOAT, ge::FORMAT_ND);1175+ gert::TilingContextPara::TensorDescription out({{255, 8000}, {255, 8000}}, ge::DT_FLOAT, ge::FORMAT_ND);
1047 gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);1176 gert::TilingContextPara tilingContextPara("Transpose", {x, perm}, {out}, &compileInfo);
1048 1177 
1049 TilingInfo tilingInfo;1178 TilingInfo tilingInfo;