ops-tune-workbench-plugin 0.0.0-stage → 0.0.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/NOTICE.md +14 -0
  2. package/README.md +39 -2
  3. package/cordis.patch.yml +20 -0
  4. package/lib/client.js +4253 -0
  5. package/lib/host.js +2518 -0
  6. package/package.json +53 -4
  7. package/skills/tool-ops-tune/INSTALL.md +52 -0
  8. package/skills/tool-ops-tune/README.md +167 -0
  9. package/skills/tool-ops-tune/SKILL.md +93 -0
  10. package/skills/tool-ops-tune/agent/AGENTS.md +195 -0
  11. package/skills/tool-ops-tune/agent/agents/perf-implementer.md +68 -0
  12. package/skills/tool-ops-tune/agent/agents/perf-reviewer.md +86 -0
  13. package/skills/tool-ops-tune/agent/workflows/task-prompts.md +362 -0
  14. package/skills/tool-ops-tune/agent/workflows/troubleshooting.md +43 -0
  15. package/skills/tool-ops-tune/docs/figures/opstune-architecture.png +0 -0
  16. package/skills/tool-ops-tune/docs/figures/opstune-architecture.svg +253 -0
  17. package/skills/tool-ops-tune/docs/ops-tune-design.md +404 -0
  18. package/skills/tool-ops-tune/docs//347/224/250/344/276/213/346/236/204/345/273/272.md +714 -0
  19. package/skills/tool-ops-tune/docs//347/237/245/350/257/206/345/272/223/350/247/204/350/214/203.md +103 -0
  20. package/skills/tool-ops-tune/evals/evals.json +242 -0
  21. package/skills/tool-ops-tune/ops-collect/SKILL.md +146 -0
  22. package/skills/tool-ops-tune/ops-collect/references/tool-routing.md +86 -0
  23. package/skills/tool-ops-tune/ops-collect/scripts/collect_board.sh +444 -0
  24. package/skills/tool-ops-tune/ops-collect/scripts/collect_cases.py +368 -0
  25. package/skills/tool-ops-tune/ops-collect/scripts/collect_sim.sh +134 -0
  26. package/skills/tool-ops-tune/ops-collect/scripts/csv_to_db.py +314 -0
  27. package/skills/tool-ops-tune/ops-collect/scripts/db_bench.py +545 -0
  28. package/skills/tool-ops-tune/ops-collect/scripts/facts_bridge.py +1063 -0
  29. package/skills/tool-ops-tune/ops-collect/scripts/isolation_test.py +454 -0
  30. package/skills/tool-ops-tune/ops-collect/scripts/jsonl_to_cases.py +163 -0
  31. package/skills/tool-ops-tune/ops-family/SKILL.md +76 -0
  32. package/skills/tool-ops-tune/ops-family/references/broadcast/advanced_tiling.md +92 -0
  33. package/skills/tool-ops-tune/ops-family/references/broadcast/broadcast_design.md +160 -0
  34. package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_add_kernel.cpp +126 -0
  35. package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_add_tiling.cpp +144 -0
  36. package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_tiling_common.h +401 -0
  37. package/skills/tool-ops-tune/ops-family/references/broadcast/code/onedim_add_kernel.cpp +107 -0
  38. package/skills/tool-ops-tune/ops-family/references/broadcast/code/onedim_add_tiling.cpp +64 -0
  39. package/skills/tool-ops-tune/ops-family/references/broadcast/datacopypad_design.md +46 -0
  40. package/skills/tool-ops-tune/ops-family/references/broadcast/guide.md +14 -0
  41. package/skills/tool-ops-tune/ops-family/references/broadcast/nddma_design.md +53 -0
  42. package/skills/tool-ops-tune/ops-family/references/broadcast/onedim_design.md +114 -0
  43. package/skills/tool-ops-tune/ops-family/references/broadcast/ub_broadcast_design.md +67 -0
  44. package/skills/tool-ops-tune/ops-family/references/common/datacopy_optimization_design.md +351 -0
  45. package/skills/tool-ops-tune/ops-family/references/common/index.md +9 -0
  46. package/skills/tool-ops-tune/ops-family/references/common/tail_block_design.md +268 -0
  47. package/skills/tool-ops-tune/ops-family/references/common/timing_harness/index.md +38 -0
  48. package/skills/tool-ops-tune/ops-family/references/common/timing_harness/timing_harness_aclrt_event.cpp +199 -0
  49. package/skills/tool-ops-tune/ops-family/references/common/timing_harness/timing_harness_torch_event.py +130 -0
  50. package/skills/tool-ops-tune/ops-family/references/common/ub_resident_design.md +342 -0
  51. package/skills/tool-ops-tune/ops-family/references/conversion/guide.md +69 -0
  52. package/skills/tool-ops-tune/ops-family/references/conversion/index.md +4 -0
  53. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_base.template +81 -0
  54. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_big_dim.template +217 -0
  55. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_cut_one_axis.template +336 -0
  56. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_cut_two_axis.template +781 -0
  57. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_n_last.template +340 -0
  58. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_small_shape.template +321 -0
  59. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_tensor_move.template +102 -0
  60. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_transdata_5hd.template +480 -0
  61. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_transdata_5hd_021axis.template +533 -0
  62. package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_with_gather.template +688 -0
  63. package/skills/tool-ops-tune/ops-family/references/conversion/templates/transpose_tiling_data.template +192 -0
  64. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_big_dim.md +220 -0
  65. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_cut_one_axis.md +175 -0
  66. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_cut_two_axis.md +177 -0
  67. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_fusion_design.md +398 -0
  68. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_gather.md +191 -0
  69. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_n_last.md +276 -0
  70. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_small_shape.md +315 -0
  71. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_tensor_move.md +203 -0
  72. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_tiling_data.md +176 -0
  73. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_vconv_021.md +206 -0
  74. package/skills/tool-ops-tune/ops-family/references/conversion/transpose_vconv_5hd.md +179 -0
  75. package/skills/tool-ops-tune/ops-family/references/elementwise/double_buffer_design.md +288 -0
  76. package/skills/tool-ops-tune/ops-family/references/elementwise/guide.md +46 -0
  77. package/skills/tool-ops-tune/ops-family/references/elementwise/index.md +10 -0
  78. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/CMakeLists.txt +131 -0
  79. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/README.md +55 -0
  80. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/add_custom_torch.cpp +80 -0
  81. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/ops.h +23 -0
  82. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/register.cpp +30 -0
  83. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_host/add_custom.asc +149 -0
  84. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_host/data_utils.h +97 -0
  85. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_kernel/add_custom_kernel.asc +136 -0
  86. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_kernel/add_custom_tiling.h +31 -0
  87. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/run.sh +86 -0
  88. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/gen_data.py +70 -0
  89. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/golden.py +34 -0
  90. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/test_torch.py +99 -0
  91. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/verify_result.py +56 -0
  92. package/skills/tool-ops-tune/ops-family/references/elementwise/templates/usage_guide.md +61 -0
  93. package/skills/tool-ops-tune/ops-family/references/elementwise/vector_efficiency_design.md +203 -0
  94. package/skills/tool-ops-tune/ops-family/references/fa/fa_mixed_kernel_design.md +174 -0
  95. package/skills/tool-ops-tune/ops-family/references/fa/fa_roofline_design.md +141 -0
  96. package/skills/tool-ops-tune/ops-family/references/fa/guide.md +87 -0
  97. package/skills/tool-ops-tune/ops-family/references/knowledge_gaps.json +56 -0
  98. package/skills/tool-ops-tune/ops-family/references/matmul/best-practices-guide.md +14 -0
  99. package/skills/tool-ops-tune/ops-family/references/matmul/fullload_design.md +344 -0
  100. package/skills/tool-ops-tune/ops-family/references/matmul/guide.md +52 -0
  101. package/skills/tool-ops-tune/ops-family/references/matmul/mte2_preload_design.md +320 -0
  102. package/skills/tool-ops-tune/ops-family/references/matmul/pingpong_design.md +254 -0
  103. package/skills/tool-ops-tune/ops-family/references/matmul/scale_coalescing_design.md +282 -0
  104. package/skills/tool-ops-tune/ops-family/references/matmul/streamk_design.md +374 -0
  105. package/skills/tool-ops-tune/ops-family/references/matmul/swat_design.md +423 -0
  106. package/skills/tool-ops-tune/ops-family/references/matmul/templates/matmul_basic_blaze.template +165 -0
  107. package/skills/tool-ops-tune/ops-family/references/matmul/templates/usage_guide.md +29 -0
  108. package/skills/tool-ops-tune/ops-family/references/mc2/best-practices-guide.md +43 -0
  109. package/skills/tool-ops-tune/ops-family/references/mc2/guide.md +42 -0
  110. package/skills/tool-ops-tune/ops-family/references/mc2/local_matmul_design.md +130 -0
  111. package/skills/tool-ops-tune/ops-family/references/mc2/pipeline_balancing_design.md +502 -0
  112. package/skills/tool-ops-tune/ops-family/references/reduce/guide.md +178 -0
  113. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/kernel_utils.template +75 -0
  114. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_full_load.template +429 -0
  115. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_recompute.template +485 -0
  116. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_small_r.template +348 -0
  117. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_full_load.template +574 -0
  118. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_online.template +324 -0
  119. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_recompute.template +436 -0
  120. package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_base.template +950 -0
  121. package/skills/tool-ops-tune/ops-family/references/reduce/templates/fused_attention_online_softmax_design.md +428 -0
  122. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_full_load.md +137 -0
  123. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_recompute.md +135 -0
  124. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_small_r.md +122 -0
  125. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_full_load.md +144 -0
  126. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_online.md +185 -0
  127. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_recompute.md +163 -0
  128. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_tiling.template +460 -0
  129. package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_tiling_data.template +278 -0
  130. package/skills/tool-ops-tune/ops-family/references/reduce/templates/state_resident_design.md +337 -0
  131. package/skills/tool-ops-tune/ops-family/references/reduce/templates/usage_guide.md +253 -0
  132. package/skills/tool-ops-tune/ops-family/references/scalar/coding_principles.md +476 -0
  133. package/skills/tool-ops-tune/ops-family/references/scalar/conv_scalar.md +163 -0
  134. package/skills/tool-ops-tune/ops-family/references/scalar/guide.md +30 -0
  135. package/skills/tool-ops-tune/ops-family/references/simt/guide.md +91 -0
  136. package/skills/tool-ops-tune/ops-family/references/simt/optimization-guide.md +110 -0
  137. package/skills/tool-ops-tune/ops-family/references/sort/guide.md +83 -0
  138. package/skills/tool-ops-tune/ops-family/references/sort/radix_sort.md +354 -0
  139. package/skills/tool-ops-tune/ops-family/scripts/family_asset_audit.py +68 -0
  140. package/skills/tool-ops-tune/ops-family/scripts/tiling_candidates.py +419 -0
  141. package/skills/tool-ops-tune/ops-mechanism/SKILL.md +23 -0
  142. package/skills/tool-ops-tune/ops-mechanism/cases/ascendc/case_groupedmatmul_pipeline.md +23 -0
  143. package/skills/tool-ops-tune/ops-mechanism/cases/ascendc/case_triton_atomic_revert.md +19 -0
  144. package/skills/tool-ops-tune/ops-mechanism/cases/mc2/case_mc2_fusion.md +19 -0
  145. package/skills/tool-ops-tune/ops-mechanism/references/optimize/api-usage.md +70 -0
  146. package/skills/tool-ops-tune/ops-mechanism/references/optimize/ascendc-regbase-vf.md +45 -0
  147. package/skills/tool-ops-tune/ops-mechanism/references/optimize/ascendc-tiling.md +51 -0
  148. package/skills/tool-ops-tune/ops-mechanism/references/optimize/data-copy.md +67 -0
  149. package/skills/tool-ops-tune/ops-mechanism/references/optimize/index.md +45 -0
  150. package/skills/tool-ops-tune/ops-mechanism/references/optimize/mc2-balancing.md +77 -0
  151. package/skills/tool-ops-tune/ops-mechanism/references/optimize/mc2.md +103 -0
  152. package/skills/tool-ops-tune/ops-mechanism/references/optimize/memory-hierarchy.md +40 -0
  153. package/skills/tool-ops-tune/ops-mechanism/references/optimize/pipeline.md +95 -0
  154. package/skills/tool-ops-tune/ops-mechanism/scripts/case_backfill.py +170 -0
  155. package/skills/tool-ops-tune/ops-mechanism/scripts/log_gap.py +145 -0
  156. package/skills/tool-ops-tune/ops-mechanism/scripts/mc2_search.py +367 -0
  157. package/skills/tool-ops-tune/ops-report/SKILL.md +77 -0
  158. package/skills/tool-ops-tune/ops-verdict/evals/run_evals.py +837 -0
  159. package/skills/tool-ops-tune/ops-verdict/evals/smoke/install-hooks.sh +65 -0
  160. package/skills/tool-ops-tune/ops-verdict/evals/smoke/run_smoke.sh +405 -0
  161. package/skills/tool-ops-tune/ops-verdict/fixtures/core_tail_a5.csv +25 -0
  162. package/skills/tool-ops-tune/ops-verdict/fixtures/few_cores_a5.csv +5 -0
  163. package/skills/tool-ops-tune/ops-verdict/fixtures/mte2_bound_a2.csv +17 -0
  164. package/skills/tool-ops-tune/ops-verdict/fixtures/multi_kernel_mix_a5.csv +7 -0
  165. package/skills/tool-ops-tune/ops-verdict/fixtures/no_bound_a5.csv +9 -0
  166. package/skills/tool-ops-tune/ops-verdict/fixtures/serial_no_overlap_a2.csv +9 -0
  167. package/skills/tool-ops-tune/ops-verdict/fixtures/vec_bound_a5.csv +9 -0
  168. package/skills/tool-ops-tune/ops-verdict/references/io-contracts.md +94 -0
  169. package/skills/tool-ops-tune/ops-verdict/references/rule-schema.md +130 -0
  170. package/skills/tool-ops-tune/ops-verdict/references/scope-schema.md +61 -0
  171. package/skills/tool-ops-tune/ops-verdict/references/soc-spec.yaml +140 -0
  172. package/skills/tool-ops-tune/ops-verdict/rules/L0-instruction.yaml +67 -0
  173. package/skills/tool-ops-tune/ops-verdict/rules/L1-single-core.yaml +316 -0
  174. package/skills/tool-ops-tune/ops-verdict/rules/L2-inter-core.yaml +173 -0
  175. package/skills/tool-ops-tune/ops-verdict/rules/L3-inter-card.yaml +156 -0
  176. package/skills/tool-ops-tune/ops-verdict/scripts/batch_pipeline.py +444 -0
  177. package/skills/tool-ops-tune/ops-verdict/scripts/calibrate.py +249 -0
  178. package/skills/tool-ops-tune/ops-verdict/scripts/delta_compare.py +244 -0
  179. package/skills/tool-ops-tune/ops-verdict/scripts/render_html.py +274 -0
  180. package/skills/tool-ops-tune/ops-verdict/scripts/snapshot.py +200 -0
  181. package/skills/tool-ops-tune/ops-verdict/scripts/trace_bubble.py +331 -0
  182. package/skills/tool-ops-tune/ops-verdict/scripts/verdict.py +1072 -0
  183. package/skills/tool-ops-tune/scripts/diff_audit.py +281 -0
@@ -0,0 +1,781 @@
1
+ /**
2
+ * Copyright (c) 2025 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_cut_two_axis.h
13
+ * \brief transpose_cut_two_axis
14
+ */
15
+ #ifndef TRANSPOSE_CUT_TWO_AXIS_H
16
+ #define TRANSPOSE_CUT_TWO_AXIS_H
17
+
18
+ #include "transpose_base.h"
19
+
20
+ namespace Transpose {
21
+ using namespace AscendC;
22
+
23
+ template <typename T>
24
+ class TransposeCutTwoAxis : public TransposeBase<T> {
25
+ public:
26
+ __aicore__ inline TransposeCutTwoAxis(){};
27
+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, const TransposeOpTilingData* tilingData, TPipe* pipe);
28
+ __aicore__ inline void Process();
29
+
30
+ private:
31
+ int64_t blockIdx_;
32
+ TQueBind<QuePosition::VECIN, QuePosition::VECOUT, 1> vecQue_;
33
+ GlobalTensor<T> inputGM_;
34
+ GlobalTensor<T> outputGM_;
35
+
36
+ // tiling params
37
+ const TransposeOpTilingData* tiling_;
38
+
39
+ // expand input and output init
40
+ int64_t expandedInputCutIndex_;
41
+ int64_t expandedOutputCutIndex_;
42
+
43
+ // core params
44
+ int64_t blkProcessNum_ = 0;
45
+ int64_t srcOffset_ = 0;
46
+
47
+ // copyOut params
48
+ int64_t mainLoopSize_[NDDMA_MAX_DIM_NUM] = {1, 1, 1, 1, 1};
49
+ int64_t mainLoopSrcStride_[NDDMA_MAX_DIM_NUM] = {0, 0, 0, 0, 0};
50
+ int64_t inputTailLoopSize_[NDDMA_MAX_DIM_NUM] = {1, 1, 1, 1, 1};
51
+ int64_t inputTailLoopSrcStride_[NDDMA_MAX_DIM_NUM] = {0, 0, 0, 0, 0};
52
+ int64_t outputTailLoopSize_[NDDMA_MAX_DIM_NUM] = {1, 1, 1, 1, 1};
53
+ int64_t outputTailLoopSrcStride_[NDDMA_MAX_DIM_NUM] = {0, 0, 0, 0, 0};
54
+ int64_t tailLoopSize_[NDDMA_MAX_DIM_NUM] = {1, 1, 1, 1, 1};
55
+ int64_t tailLoopSrcStride_[NDDMA_MAX_DIM_NUM] = {0, 0, 0, 0, 0};
56
+ int64_t loopDstStride_[NDDMA_MAX_DIM_NUM] = {0, 0, 0, 0, 0};
57
+
58
+ int64_t inputTailSrcAddressOffsetBase_ = 1;
59
+ int64_t inputTailDstAddressOffsetBase_ = 1;
60
+ int64_t outputTailSrcAddressOffsetBase_ = 1;
61
+ int64_t outputTailDstAddressOffsetBase_ = 1;
62
+
63
+ int64_t inputOutputCutIndex_;
64
+ int64_t outputInputCutIndex_;
65
+ int64_t maxCutIndex_;
66
+ int64_t minCutIndex_;
67
+ int64_t outUbLoop_ = 1;
68
+
69
+ struct Interval_ {
70
+ int64_t start = 0;
71
+ int64_t end = 0;
72
+ };
73
+
74
+ __aicore__ inline void ParseTilingData();
75
+ __aicore__ inline int64_t FindPermIndex(int64_t j);
76
+ __aicore__ inline void GetLoopAddressOffsetImpl(bool isSrc, int64_t loopNumArray[], int64_t loopShapeSizeArray[],
77
+ int64_t cutIndex1, int64_t cutIndex2, const int64_t expandedShape[],
78
+ const int64_t inUbShape[]);
79
+ __aicore__ inline void GetMainLoopAddressOffset(bool isSrc, int64_t loopNumArray[], int64_t loopShapeSizeArray[],
80
+ const int64_t expandedShape[], const int64_t inUbShape[]);
81
+ __aicore__ inline void GetTailLoopAddressOffset(bool isSrc, int64_t loopNumArray[], int64_t loopShapeSizeArray[],
82
+ int64_t cutIndex, const int64_t expandedShape[],
83
+ const int64_t inUbShape[]);
84
+ __aicore__ inline void GetTailTailLoopAddressOffset(bool isSrc, int64_t loopNumArray[],
85
+ int64_t loopShapeSizeArray[], int64_t inputCutIndex,
86
+ int64_t outputCutIndex, const int64_t expandedShape[],
87
+ const int64_t inUbShape[]);
88
+ __aicore__ inline MultiCopyLoopInfo<NDDMA_MAX_DIM_NUM> SetupLoopInfo(const int64_t inUbSrcShape[],
89
+ const int64_t inUbDstShape[]);
90
+ __aicore__ inline void ProcessMain(int64_t loopidxStart, int64_t loopidxEnd);
91
+ __aicore__ inline void ProcessOutputTail(int64_t loopidxStart, int64_t loopidxEnd);
92
+ __aicore__ inline void ProcessInputTail(int64_t loopidxStart, int64_t loopidxEnd);
93
+ __aicore__ inline void ProcessTail(int64_t loopidxStart, int64_t loopidxEnd);
94
+ __aicore__ inline void PorcessOffsetRange(int64_t start, int64_t end, Interval_& offsetRangeRes);
95
+ __aicore__ inline void GetCopyOutParams();
96
+ __aicore__ inline void GetTailAddressOffsetBase();
97
+ __aicore__ inline void ProcessPerCore();
98
+ __aicore__ inline void ProcessBlock();
99
+ };
100
+
101
+ template <typename T>
102
+ __aicore__ inline void TransposeCutTwoAxis<T>::Init(GM_ADDR x, GM_ADDR y, const TransposeOpTilingData* tilingData,
103
+ TPipe* pipe)
104
+ {
105
+ blockIdx_ = GetBlockIdx();
106
+ tiling_ = tilingData;
107
+ ParseTilingData();
108
+ inputGM_.SetGlobalBuffer((__gm__ T*)x);
109
+ outputGM_.SetGlobalBuffer((__gm__ T*)y);
110
+ pipe->InitBuffer(vecQue_, 1, tiling_->ubSize);
111
+ }
112
+
113
+ template <typename T>
114
+ __aicore__ inline void TransposeCutTwoAxis<T>::Process()
115
+ {
116
+ if (blockIdx_ >= tiling_->realCoreNum) {
117
+ return;
118
+ }
119
+
120
+ blkProcessNum_ = tiling_->blkFactor;
121
+ srcOffset_ = blockIdx_ * tiling_->blkFactor;
122
+ if (blockIdx_ < tiling_->blkTailFactor) {
123
+ blkProcessNum_ += 1;
124
+ srcOffset_ += blockIdx_;
125
+ } else {
126
+ srcOffset_ += tiling_->blkTailFactor;
127
+ }
128
+
129
+ ProcessPerCore();
130
+ }
131
+
132
+ __aicore__ inline void DecimalToMixedBase(int64_t num, int64_t bases[], int64_t mixedBase[])
133
+ {
134
+ if (num == 0) {
135
+ return;
136
+ }
137
+ for (int64_t i = 0; i < TRANSPOSE_MAX_AXIS_NUM; ++i) {
138
+ mixedBase[i] = num % bases[i];
139
+ num /= bases[i];
140
+ if (num == 0) {
141
+ break;
142
+ }
143
+ }
144
+ }
145
+
146
+ __aicore__ inline int64_t AlignToMultiple(int64_t a, int64_t b)
147
+ {
148
+ if (b <= 0) {
149
+ return a;
150
+ }
151
+ return CeilDivision(a, b) * b;
152
+ }
153
+
154
+ template <typename T>
155
+ __aicore__ inline void TransposeCutTwoAxis<T>::PorcessOffsetRange(int64_t start, int64_t end, Interval_& offsetRangeRes)
156
+ {
157
+ if ((srcOffset_ < start) && (srcOffset_ + blkProcessNum_ - 1 > end)) {
158
+ return;
159
+ }
160
+ if ((srcOffset_ >= start) && (srcOffset_ <= end)) {
161
+ offsetRangeRes.start = srcOffset_;
162
+ if ((srcOffset_ + blkProcessNum_ - 1 >= start) && (srcOffset_ + blkProcessNum_ - 1 <= end)) {
163
+ offsetRangeRes.end = srcOffset_ + blkProcessNum_ - 1;
164
+ }
165
+ } else {
166
+ if ((srcOffset_ + blkProcessNum_ - 1 >= start) && (srcOffset_ + blkProcessNum_ - 1 <= end)) {
167
+ offsetRangeRes.end = srcOffset_ + blkProcessNum_ - 1;
168
+ } else {
169
+ offsetRangeRes.start = 0;
170
+ offsetRangeRes.end = 0;
171
+ }
172
+ }
173
+ }
174
+
175
+ template <typename T>
176
+ __aicore__ inline void TransposeCutTwoAxis<T>::ParseTilingData()
177
+ {
178
+ expandedOutputCutIndex_ = tiling_->outCutIndex + NDDMA_MAX_DIM_NUM - tiling_->permSize;
179
+ expandedInputCutIndex_ = tiling_->inCutIndex + NDDMA_MAX_DIM_NUM - tiling_->permSize;
180
+ }
181
+
182
+ template <typename T>
183
+ __aicore__ inline int64_t TransposeCutTwoAxis<T>::FindPermIndex(int64_t j)
184
+ {
185
+ for (int64_t permIndex = 0; permIndex < NDDMA_MAX_DIM_NUM; permIndex++) {
186
+ if (tiling_->expandedPerm[permIndex] == j) {
187
+ return permIndex;
188
+ }
189
+ }
190
+ return j;
191
+ }
192
+
193
+ template <typename T>
194
+ __aicore__ inline void TransposeCutTwoAxis<T>::GetLoopAddressOffsetImpl(bool isSrc, int64_t loopNumArray[],
195
+ int64_t loopShapeSizeArray[], int64_t cutIndex1,
196
+ int64_t cutIndex2,
197
+ const int64_t expandedShape[],
198
+ const int64_t inUbShape[])
199
+ {
200
+ int64_t startIndex = NDDMA_MAX_DIM_NUM - 1;
201
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
202
+ for (int64_t j = startIndex; j >= 0; j--) {
203
+ int64_t idx = j;
204
+ if (!isSrc) {
205
+ idx = FindPermIndex(j);
206
+ }
207
+ if (idx == cutIndex1 || idx == cutIndex2) {
208
+ continue;
209
+ }
210
+ int64_t loopNum = expandedShape[idx] / inUbShape[idx];
211
+ int64_t loopSize = inUbShape[idx];
212
+ for (int64_t k = idx + 1; k < NDDMA_MAX_DIM_NUM; k++) {
213
+ loopSize *= expandedShape[k];
214
+ }
215
+ if (loopNum > 1) {
216
+ startIndex = j - 1;
217
+ loopNumArray[i] = loopNum;
218
+ loopShapeSizeArray[i] = loopSize;
219
+ break;
220
+ }
221
+ }
222
+ }
223
+ }
224
+
225
+ template <typename T>
226
+ __aicore__ inline void TransposeCutTwoAxis<T>::GetMainLoopAddressOffset(bool isSrc, int64_t loopNumArray[],
227
+ int64_t loopShapeSizeArray[],
228
+ const int64_t expandedShape[],
229
+ const int64_t inUbShape[])
230
+ {
231
+ GetLoopAddressOffsetImpl(isSrc, loopNumArray, loopShapeSizeArray, -1, -1, expandedShape, inUbShape);
232
+ }
233
+
234
+ template <typename T>
235
+ __aicore__ inline void TransposeCutTwoAxis<T>::GetTailLoopAddressOffset(bool isSrc, int64_t loopNumArray[],
236
+ int64_t loopShapeSizeArray[], int64_t cutIndex,
237
+ const int64_t expandedShape[],
238
+ const int64_t inUbShape[])
239
+ {
240
+ GetLoopAddressOffsetImpl(isSrc, loopNumArray, loopShapeSizeArray, cutIndex, -1, expandedShape, inUbShape);
241
+ }
242
+
243
+ template <typename T>
244
+ __aicore__ inline void TransposeCutTwoAxis<T>::GetTailTailLoopAddressOffset(
245
+ bool isSrc, int64_t loopNumArray[], int64_t loopShapeSizeArray[], int64_t inputCutIndex, int64_t outputCutIndex,
246
+ const int64_t expandedShape[], const int64_t inUbShape[])
247
+ {
248
+ GetLoopAddressOffsetImpl(isSrc, loopNumArray, loopShapeSizeArray, inputCutIndex, outputCutIndex, expandedShape,
249
+ inUbShape);
250
+ }
251
+
252
+ template <typename T>
253
+ __aicore__ inline MultiCopyLoopInfo<NDDMA_MAX_DIM_NUM> TransposeCutTwoAxis<T>::SetupLoopInfo(
254
+ const int64_t inUbSrcShape[], const int64_t inUbDstShape[])
255
+ {
256
+ MultiCopyLoopInfo<NDDMA_MAX_DIM_NUM> loopInfo;
257
+
258
+ int64_t loopDstStrideTmp[NDDMA_MAX_DIM_NUM] = {0};
259
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
260
+ int64_t recumMultiSrc = 1;
261
+ int64_t recumMultiDst = 1;
262
+ loopInfo.loopSize[i] = inUbSrcShape[i];
263
+ for (int64_t j = i + 1; j < NDDMA_MAX_DIM_NUM; j++) {
264
+ recumMultiSrc *= tiling_->expandedInputShape[j];
265
+ recumMultiDst *= inUbDstShape[j];
266
+ }
267
+ loopInfo.loopSrcStride[i] = recumMultiSrc;
268
+ loopDstStrideTmp[i] = recumMultiDst;
269
+ loopInfo.loopLpSize[i] = 0;
270
+ loopInfo.loopRpSize[i] = 0;
271
+ }
272
+ loopInfo.loopSrcStride[NDDMA_MAX_DIM_NUM - 1] = 1;
273
+ loopDstStrideTmp[NDDMA_MAX_DIM_NUM - 1] = 1;
274
+
275
+ int64_t alignDstStride = 1;
276
+ if (maxCutIndex_ - 1 >= 0) {
277
+ alignDstStride = (loopDstStrideTmp[maxCutIndex_ - 1] * sizeof(T) + BLOCK_SIZE_BYTE - 1) / BLOCK_SIZE_BYTE;
278
+ alignDstStride = alignDstStride * BLOCK_SIZE_BYTE / sizeof(T);
279
+ loopDstStrideTmp[maxCutIndex_ - 1] = alignDstStride;
280
+ } else {
281
+ alignDstStride = (loopDstStrideTmp[0] * sizeof(T) + BLOCK_SIZE_BYTE - 1) / BLOCK_SIZE_BYTE;
282
+ alignDstStride = alignDstStride * BLOCK_SIZE_BYTE / sizeof(T);
283
+ loopDstStrideTmp[0] = alignDstStride;
284
+ }
285
+
286
+ for (int64_t i = maxCutIndex_ - 2; i >= 0; i--) {
287
+ loopDstStrideTmp[i] = inUbDstShape[i + 1] * loopDstStrideTmp[i + 1];
288
+ }
289
+
290
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
291
+ loopInfo.loopDstStride[tiling_->expandedPerm[i]] = loopDstStrideTmp[i];
292
+ }
293
+
294
+ this->reverseArray(loopInfo.loopSize);
295
+ this->reverseArray(loopInfo.loopSrcStride);
296
+ this->reverseArray(loopInfo.loopDstStride);
297
+
298
+ return loopInfo;
299
+ }
300
+
301
+ template <typename T>
302
+ __aicore__ inline void TransposeCutTwoAxis<T>::ProcessMain(int64_t loopidxStart, int64_t loopidxEnd)
303
+ {
304
+ // main
305
+ T constValue = 0;
306
+ MultiCopyLoopInfo<NDDMA_MAX_DIM_NUM> loopInfoMain = SetupLoopInfo(tiling_->inUbMainSrcShape,
307
+ tiling_->inUbMainDstShape);
308
+ MultiCopyParams<T, NDDMA_MAX_DIM_NUM> paramsMain = {loopInfoMain, constValue};
309
+
310
+ DataCopyExtParams copyOutParamsMain;
311
+ copyOutParamsMain.blockLen = mainLoopSize_[0] * sizeof(T);
312
+ copyOutParamsMain.blockCount = mainLoopSize_[1];
313
+ copyOutParamsMain.srcStride = 0;
314
+ copyOutParamsMain.dstStride = (loopDstStride_[1] - mainLoopSize_[0]) * sizeof(T);
315
+
316
+ LoopModeParams loopParams;
317
+ loopParams.loop1Size = mainLoopSize_[2];
318
+ loopParams.loop1SrcStride = mainLoopSrcStride_[2] * sizeof(T);
319
+ loopParams.loop1DstStride = loopDstStride_[2] * sizeof(T);
320
+ loopParams.loop2Size = mainLoopSize_[3];
321
+ loopParams.loop2SrcStride = mainLoopSrcStride_[3] * sizeof(T);
322
+ loopParams.loop2DstStride = loopDstStride_[3] * sizeof(T);
323
+
324
+ int64_t srcLoopNumArray[NDDMA_MAX_DIM_NUM] = {0};
325
+ int64_t srcLoopShapeSizeArray[NDDMA_MAX_DIM_NUM] = {0};
326
+ GetMainLoopAddressOffset(true, srcLoopNumArray, srcLoopShapeSizeArray, tiling_->expandedInputShape,
327
+ tiling_->inUbMainSrcShape);
328
+
329
+ int64_t dstLoopNumArray[NDDMA_MAX_DIM_NUM] = {0};
330
+ int64_t dstLoopShapeSizeArray[NDDMA_MAX_DIM_NUM] = {0};
331
+ GetMainLoopAddressOffset(false, dstLoopNumArray, dstLoopShapeSizeArray, tiling_->expandedOutputShape,
332
+ tiling_->inUbMainDstShape);
333
+
334
+ // multicore cycle align main blocks
335
+ for (int64_t loopidx = loopidxStart; loopidx <= loopidxEnd; loopidx++) {
336
+ int64_t srcAddressOffset = 0;
337
+ int64_t dstAddressOffset = 0;
338
+ int64_t ubAddressOffset = 0;
339
+
340
+ int64_t srcAddressOffsetMixedBase[NDDMA_MAX_DIM_NUM] = {0};
341
+ DecimalToMixedBase(loopidx, srcLoopNumArray, srcAddressOffsetMixedBase);
342
+
343
+ int64_t dstAddressOffsetMixedBase[NDDMA_MAX_DIM_NUM] = {0};
344
+ DecimalToMixedBase(loopidx, dstLoopNumArray, dstAddressOffsetMixedBase);
345
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
346
+ if (srcLoopShapeSizeArray[i] == 0) {
347
+ break;
348
+ }
349
+ srcAddressOffset += srcAddressOffsetMixedBase[i] * srcLoopShapeSizeArray[i];
350
+ dstAddressOffset += dstAddressOffsetMixedBase[i] * dstLoopShapeSizeArray[i];
351
+ }
352
+ LocalTensor<T> bindLocalIn = vecQue_.AllocTensor<T>();
353
+ DataCopy<T, NDDMA_MAX_DIM_NUM, config>(bindLocalIn, inputGM_[srcAddressOffset], paramsMain);
354
+ vecQue_.EnQue(bindLocalIn);
355
+ SetLoopModePara(loopParams, DataCopyMVType::UB_TO_OUT);
356
+ LocalTensor<T> bindLocalOut = vecQue_.DeQue<T>();
357
+ for (int64_t i = 0; i < mainLoopSize_[4]; i++) {
358
+ DataCopyPad(outputGM_[dstAddressOffset + i * loopDstStride_[4]], bindLocalOut[i * mainLoopSrcStride_[4]],
359
+ copyOutParamsMain);
360
+ }
361
+ ResetLoopModePara(DataCopyMVType::UB_TO_OUT);
362
+ vecQue_.FreeTensor(bindLocalOut);
363
+ }
364
+ }
365
+
366
+ template <typename T>
367
+ __aicore__ inline void TransposeCutTwoAxis<T>::ProcessInputTail(int64_t loopidxStart, int64_t loopidxEnd)
368
+ {
369
+ // NDDMA loopInfo init
370
+ T constValue = 0;
371
+ MultiCopyLoopInfo<NDDMA_MAX_DIM_NUM> loopInfoInputTail = SetupLoopInfo(tiling_->inUbInputTailSrcShape,
372
+ tiling_->inUbInputTailDstShape);
373
+ MultiCopyParams<T, NDDMA_MAX_DIM_NUM> paramsInputTail = {loopInfoInputTail, constValue};
374
+
375
+ DataCopyExtParams copyOutParamsInputTail;
376
+ copyOutParamsInputTail.blockLen = inputTailLoopSize_[0] * sizeof(T);
377
+ copyOutParamsInputTail.blockCount = inputTailLoopSize_[1];
378
+ copyOutParamsInputTail.srcStride = 0;
379
+ copyOutParamsInputTail.dstStride = (loopDstStride_[1] - inputTailLoopSize_[0]) * sizeof(T);
380
+
381
+ LoopModeParams loopParams;
382
+ loopParams.loop1Size = inputTailLoopSize_[2];
383
+ loopParams.loop1SrcStride = inputTailLoopSrcStride_[2] * sizeof(T);
384
+ loopParams.loop1DstStride = loopDstStride_[2] * sizeof(T);
385
+ loopParams.loop2Size = inputTailLoopSize_[3];
386
+ loopParams.loop2SrcStride = inputTailLoopSrcStride_[3] * sizeof(T);
387
+ loopParams.loop2DstStride = loopDstStride_[3] * sizeof(T);
388
+
389
+ // inputTail
390
+ int64_t inputTailSrcLoopNumArray[NDDMA_MAX_DIM_NUM] = {0};
391
+ int64_t inputTailSrcLoopShapeSizeArray[NDDMA_MAX_DIM_NUM] = {0};
392
+ GetTailLoopAddressOffset(true, inputTailSrcLoopNumArray, inputTailSrcLoopShapeSizeArray, expandedInputCutIndex_,
393
+ tiling_->expandedInputShape, tiling_->inUbInputTailSrcShape);
394
+
395
+ int64_t inputTailDstLoopNumArray[NDDMA_MAX_DIM_NUM] = {0};
396
+ int64_t inputTailDstLoopShapeSizeArray[NDDMA_MAX_DIM_NUM] = {0};
397
+ GetTailLoopAddressOffset(false, inputTailDstLoopNumArray, inputTailDstLoopShapeSizeArray, outputInputCutIndex_,
398
+ tiling_->expandedOutputShape, tiling_->inUbInputTailDstShape);
399
+
400
+ for (int64_t loopidx = loopidxStart; loopidx <= loopidxEnd; loopidx++) {
401
+ int64_t srcAddressOffset = inputTailSrcAddressOffsetBase_;
402
+ int64_t dstAddressOffset = inputTailDstAddressOffsetBase_;
403
+ int64_t ubAddressOffset = 0;
404
+
405
+ int64_t srcAddressOffsetMixedBase[NDDMA_MAX_DIM_NUM] = {0};
406
+ DecimalToMixedBase(loopidx - ((tiling_->expandedInputShape[expandedInputCutIndex_] /
407
+ tiling_->inUbMainSrcShape[expandedInputCutIndex_]) *
408
+ (tiling_->expandedInputShape[inputOutputCutIndex_] /
409
+ tiling_->inUbMainSrcShape[inputOutputCutIndex_]) *
410
+ outUbLoop_),
411
+ inputTailSrcLoopNumArray, srcAddressOffsetMixedBase);
412
+
413
+ int64_t dstAddressOffsetMixedBase[NDDMA_MAX_DIM_NUM] = {0};
414
+ DecimalToMixedBase(loopidx - ((tiling_->expandedInputShape[expandedInputCutIndex_] /
415
+ tiling_->inUbMainSrcShape[expandedInputCutIndex_]) *
416
+ (tiling_->expandedInputShape[inputOutputCutIndex_] /
417
+ tiling_->inUbMainSrcShape[inputOutputCutIndex_]) *
418
+ outUbLoop_),
419
+ inputTailDstLoopNumArray, dstAddressOffsetMixedBase);
420
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
421
+ if (inputTailSrcLoopShapeSizeArray[i] == 0) {
422
+ break;
423
+ }
424
+ srcAddressOffset += srcAddressOffsetMixedBase[i] * inputTailSrcLoopShapeSizeArray[i];
425
+ dstAddressOffset += dstAddressOffsetMixedBase[i] * inputTailDstLoopShapeSizeArray[i];
426
+ }
427
+ LocalTensor<T> bindLocalIn = vecQue_.AllocTensor<T>();
428
+ DataCopy<T, NDDMA_MAX_DIM_NUM, config>(bindLocalIn, inputGM_[srcAddressOffset], paramsInputTail);
429
+ vecQue_.EnQue(bindLocalIn);
430
+ SetLoopModePara(loopParams, DataCopyMVType::UB_TO_OUT);
431
+ LocalTensor<T> bindLocalOut = vecQue_.DeQue<T>();
432
+ for (int64_t i = 0; i < inputTailLoopSize_[4]; i++) {
433
+ DataCopyPad(outputGM_[dstAddressOffset + i * loopDstStride_[4]],
434
+ bindLocalOut[i * inputTailLoopSrcStride_[4]], copyOutParamsInputTail);
435
+ }
436
+ ResetLoopModePara(DataCopyMVType::UB_TO_OUT);
437
+ vecQue_.FreeTensor(bindLocalOut);
438
+ }
439
+ }
440
+
441
+ template <typename T>
442
+ __aicore__ inline void TransposeCutTwoAxis<T>::ProcessOutputTail(int64_t loopidxStart, int64_t loopidxEnd)
443
+ {
444
+ // NDDMA loopInfo init
445
+ T constValue = 0;
446
+ MultiCopyLoopInfo<NDDMA_MAX_DIM_NUM> loopInfoOutputTail = SetupLoopInfo(tiling_->inUbOutputTailSrcShape,
447
+ tiling_->inUbOutputTailDstShape);
448
+ MultiCopyParams<T, NDDMA_MAX_DIM_NUM> paramsOutputTail = {loopInfoOutputTail, constValue};
449
+
450
+ DataCopyExtParams copyOutParamsOutputTail;
451
+ copyOutParamsOutputTail.blockLen = outputTailLoopSize_[0] * sizeof(T);
452
+ copyOutParamsOutputTail.blockCount = outputTailLoopSize_[1];
453
+ copyOutParamsOutputTail.srcStride = 0;
454
+ copyOutParamsOutputTail.dstStride = (loopDstStride_[1] - outputTailLoopSize_[0]) * sizeof(T);
455
+
456
+ LoopModeParams loopParams;
457
+ loopParams.loop1Size = outputTailLoopSize_[2];
458
+ loopParams.loop1SrcStride = outputTailLoopSrcStride_[2] * sizeof(T);
459
+ loopParams.loop1DstStride = loopDstStride_[2] * sizeof(T);
460
+ loopParams.loop2Size = outputTailLoopSize_[3];
461
+ loopParams.loop2SrcStride = outputTailLoopSrcStride_[3] * sizeof(T);
462
+ loopParams.loop2DstStride = loopDstStride_[3] * sizeof(T);
463
+
464
+ // outputTail
465
+ int64_t outputTailSrcLoopNumArray[NDDMA_MAX_DIM_NUM] = {0};
466
+ int64_t outputTailSrcLoopShapeSizeArray[NDDMA_MAX_DIM_NUM] = {0};
467
+ GetTailLoopAddressOffset(true, outputTailSrcLoopNumArray, outputTailSrcLoopShapeSizeArray, inputOutputCutIndex_,
468
+ tiling_->expandedInputShape, tiling_->inUbOutputTailSrcShape);
469
+
470
+ int64_t outputTailDstLoopNumArray[NDDMA_MAX_DIM_NUM] = {0};
471
+ int64_t outputTailDstLoopShapeSizeArray[NDDMA_MAX_DIM_NUM] = {0};
472
+ GetTailLoopAddressOffset(false, outputTailDstLoopNumArray, outputTailDstLoopShapeSizeArray, expandedOutputCutIndex_,
473
+ tiling_->expandedOutputShape, tiling_->inUbOutputTailDstShape);
474
+
475
+ int64_t idxOffset = 0;
476
+ if (tiling_->inTailFactor == 0) {
477
+ idxOffset = (tiling_->expandedInputShape[expandedInputCutIndex_] /
478
+ tiling_->inUbMainSrcShape[expandedInputCutIndex_]) *
479
+ (tiling_->expandedInputShape[inputOutputCutIndex_] /
480
+ tiling_->inUbMainSrcShape[inputOutputCutIndex_]) *
481
+ outUbLoop_;
482
+ } else {
483
+ idxOffset = (tiling_->expandedInputShape[expandedInputCutIndex_] /
484
+ tiling_->inUbMainSrcShape[expandedInputCutIndex_]) *
485
+ (tiling_->expandedInputShape[inputOutputCutIndex_] /
486
+ tiling_->inUbMainSrcShape[inputOutputCutIndex_]) *
487
+ outUbLoop_ +
488
+ (tiling_->expandedInputShape[inputOutputCutIndex_] /
489
+ tiling_->inUbMainSrcShape[inputOutputCutIndex_]) *
490
+ outUbLoop_;
491
+ }
492
+
493
+ for (int64_t loopidx = loopidxStart; loopidx <= loopidxEnd; loopidx++) {
494
+ int64_t srcAddressOffset = outputTailSrcAddressOffsetBase_;
495
+ int64_t dstAddressOffset = outputTailDstAddressOffsetBase_;
496
+ int64_t ubAddressOffset = 0;
497
+
498
+ int64_t srcAddressOffsetMixedBase[NDDMA_MAX_DIM_NUM] = {0};
499
+ DecimalToMixedBase(loopidx - idxOffset, outputTailSrcLoopNumArray, srcAddressOffsetMixedBase);
500
+ int64_t dstAddressOffsetMixedBase[NDDMA_MAX_DIM_NUM] = {0};
501
+ DecimalToMixedBase(loopidx - idxOffset, outputTailDstLoopNumArray, dstAddressOffsetMixedBase);
502
+
503
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
504
+ if (outputTailSrcLoopShapeSizeArray[i] == 0) {
505
+ break;
506
+ }
507
+ srcAddressOffset += srcAddressOffsetMixedBase[i] * outputTailSrcLoopShapeSizeArray[i];
508
+ dstAddressOffset += dstAddressOffsetMixedBase[i] * outputTailDstLoopShapeSizeArray[i];
509
+ }
510
+ LocalTensor<T> bindLocalIn = vecQue_.AllocTensor<T>();
511
+ DataCopy<T, NDDMA_MAX_DIM_NUM, config>(bindLocalIn, inputGM_[srcAddressOffset], paramsOutputTail);
512
+ vecQue_.EnQue(bindLocalIn);
513
+ SetLoopModePara(loopParams, DataCopyMVType::UB_TO_OUT);
514
+ LocalTensor<T> bindLocalOut = vecQue_.DeQue<T>();
515
+ for (int64_t i = 0; i < outputTailLoopSize_[4]; i++) {
516
+ DataCopyPad(outputGM_[dstAddressOffset + i * loopDstStride_[4]],
517
+ bindLocalOut[i * outputTailLoopSrcStride_[4]], copyOutParamsOutputTail);
518
+ }
519
+ ResetLoopModePara(DataCopyMVType::UB_TO_OUT);
520
+ vecQue_.FreeTensor(bindLocalOut);
521
+ }
522
+ }
523
+
524
+ template <typename T>
525
+ __aicore__ inline void TransposeCutTwoAxis<T>::ProcessTail(int64_t loopidxStart, int64_t loopidxEnd)
526
+ {
527
+ // NDDMA loopInfo init
528
+ T constValue = 0;
529
+ MultiCopyLoopInfo<NDDMA_MAX_DIM_NUM> loopInfoTail = SetupLoopInfo(tiling_->inUbTailSrcShape,
530
+ tiling_->inUbTailDstShape);
531
+ MultiCopyParams<T, NDDMA_MAX_DIM_NUM> paramsTail = {loopInfoTail, constValue};
532
+
533
+ DataCopyExtParams copyOutParamsTail;
534
+ copyOutParamsTail.blockLen = tailLoopSize_[0] * sizeof(T);
535
+ copyOutParamsTail.blockCount = tailLoopSize_[1];
536
+ copyOutParamsTail.srcStride = 0;
537
+ copyOutParamsTail.dstStride = (loopDstStride_[1] - tailLoopSize_[0]) * sizeof(T);
538
+
539
+ LoopModeParams loopParams;
540
+ loopParams.loop1Size = tailLoopSize_[2];
541
+ loopParams.loop1SrcStride = tailLoopSrcStride_[2] * sizeof(T);
542
+ loopParams.loop1DstStride = loopDstStride_[2] * sizeof(T);
543
+ loopParams.loop2Size = tailLoopSize_[3];
544
+ loopParams.loop2SrcStride = tailLoopSrcStride_[3] * sizeof(T);
545
+ loopParams.loop2DstStride = loopDstStride_[3] * sizeof(T);
546
+
547
+ int64_t tailSrcAddressOffsetBase = inputTailSrcAddressOffsetBase_ + outputTailSrcAddressOffsetBase_;
548
+ int64_t tailDstAddressOffsetBase = inputTailDstAddressOffsetBase_ + outputTailDstAddressOffsetBase_;
549
+ int64_t ubAddressOffset = 0;
550
+
551
+ int64_t tailSrcLoopNumArray[NDDMA_MAX_DIM_NUM] = {0};
552
+ int64_t tailSrcLoopShapeSizeArray[NDDMA_MAX_DIM_NUM] = {0};
553
+ GetTailTailLoopAddressOffset(true, tailSrcLoopNumArray, tailSrcLoopShapeSizeArray, expandedInputCutIndex_,
554
+ inputOutputCutIndex_, tiling_->expandedInputShape, tiling_->inUbTailSrcShape);
555
+
556
+ int64_t tailDstLoopNumArray[NDDMA_MAX_DIM_NUM] = {0};
557
+ int64_t tailDstLoopShapeSizeArray[NDDMA_MAX_DIM_NUM] = {0};
558
+ GetTailTailLoopAddressOffset(false, tailDstLoopNumArray, tailDstLoopShapeSizeArray, expandedOutputCutIndex_,
559
+ outputInputCutIndex_, tiling_->expandedOutputShape, tiling_->inUbTailDstShape);
560
+
561
+ for (int64_t loopidx = loopidxStart; loopidx <= loopidxEnd; loopidx++) {
562
+ int64_t srcAddressOffset = tailSrcAddressOffsetBase;
563
+ int64_t dstAddressOffset = tailDstAddressOffsetBase;
564
+
565
+ int64_t tailLoopLocalIdx = loopidx -
566
+ ((tiling_->expandedInputShape[expandedInputCutIndex_] /
567
+ tiling_->inUbMainSrcShape[expandedInputCutIndex_]) *
568
+ (tiling_->expandedInputShape[inputOutputCutIndex_] /
569
+ tiling_->inUbMainSrcShape[inputOutputCutIndex_]) *
570
+ outUbLoop_) -
571
+ ((tiling_->expandedInputShape[expandedInputCutIndex_] /
572
+ tiling_->inUbMainSrcShape[expandedInputCutIndex_]) *
573
+ outUbLoop_) -
574
+ ((tiling_->expandedInputShape[inputOutputCutIndex_] /
575
+ tiling_->inUbMainSrcShape[inputOutputCutIndex_]) *
576
+ outUbLoop_);
577
+
578
+ int64_t srcAddressOffsetMixedBase[NDDMA_MAX_DIM_NUM] = {0};
579
+ DecimalToMixedBase(tailLoopLocalIdx, tailSrcLoopNumArray, srcAddressOffsetMixedBase);
580
+
581
+ int64_t dstAddressOffsetMixedBase[NDDMA_MAX_DIM_NUM] = {0};
582
+ DecimalToMixedBase(tailLoopLocalIdx, tailDstLoopNumArray, dstAddressOffsetMixedBase);
583
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
584
+ if (tailSrcLoopShapeSizeArray[i] == 0) {
585
+ break;
586
+ }
587
+ srcAddressOffset += srcAddressOffsetMixedBase[i] * tailSrcLoopShapeSizeArray[i];
588
+ dstAddressOffset += dstAddressOffsetMixedBase[i] * tailDstLoopShapeSizeArray[i];
589
+ }
590
+ LocalTensor<T> bindLocalIn = vecQue_.AllocTensor<T>();
591
+ DataCopy<T, NDDMA_MAX_DIM_NUM, config>(bindLocalIn, inputGM_[srcAddressOffset], paramsTail);
592
+ vecQue_.EnQue(bindLocalIn);
593
+ SetLoopModePara(loopParams, DataCopyMVType::UB_TO_OUT);
594
+ LocalTensor<T> bindLocalOut = vecQue_.DeQue<T>();
595
+ for (int64_t i = 0; i < tailLoopSize_[4]; i++) {
596
+ DataCopyPad(outputGM_[dstAddressOffset + i * loopDstStride_[4]], bindLocalOut[i * tailLoopSrcStride_[4]],
597
+ copyOutParamsTail);
598
+ }
599
+ ResetLoopModePara(DataCopyMVType::UB_TO_OUT);
600
+ vecQue_.FreeTensor(bindLocalOut);
601
+ }
602
+ }
603
+
604
+ template <typename T>
605
+ __aicore__ inline void TransposeCutTwoAxis<T>::GetCopyOutParams()
606
+ {
607
+ // calculate the main and tail loopSize and loopSrc/DstStirde of copyOut
608
+ int64_t idx = 0;
609
+ int64_t loopSize = 1;
610
+ int64_t cutIdx[NDDMA_MAX_DIM_NUM] = {-1, -1, -1, -1, -1};
611
+ for (int64_t i = NDDMA_MAX_DIM_NUM - 1; i >= 0; i--) {
612
+ if (tiling_->inUbMainDstShape[i] != tiling_->expandedOutputShape[i]) {
613
+ mainLoopSize_[idx] = loopSize * tiling_->inUbMainDstShape[i];
614
+ inputTailLoopSize_[idx] = loopSize * tiling_->inUbInputTailDstShape[i];
615
+ outputTailLoopSize_[idx] = loopSize * tiling_->inUbOutputTailDstShape[i];
616
+ tailLoopSize_[idx] = loopSize * tiling_->inUbTailDstShape[i];
617
+ cutIdx[idx] = NDDMA_MAX_DIM_NUM - 1 - i;
618
+ idx++;
619
+ loopSize = 1;
620
+ } else {
621
+ loopSize *= tiling_->inUbMainDstShape[i];
622
+ }
623
+ if (i == 0) {
624
+ mainLoopSize_[idx] = loopSize;
625
+ inputTailLoopSize_[idx] = loopSize;
626
+ outputTailLoopSize_[idx] = loopSize;
627
+ tailLoopSize_[idx] = loopSize;
628
+ }
629
+ }
630
+ mainLoopSrcStride_[1] = Ops::Base::CeilDiv(static_cast<int64_t>(mainLoopSize_[0] * sizeof(T)), BLOCK_SIZE_BYTE) *
631
+ BLOCK_SIZE_BYTE / sizeof(T);
632
+ inputTailLoopSrcStride_[1] = Ops::Base::CeilDiv(static_cast<int64_t>(inputTailLoopSize_[0] * sizeof(T)),
633
+ BLOCK_SIZE_BYTE) *
634
+ BLOCK_SIZE_BYTE / sizeof(T);
635
+ outputTailLoopSrcStride_[1] = Ops::Base::CeilDiv(static_cast<int64_t>(outputTailLoopSize_[0] * sizeof(T)),
636
+ BLOCK_SIZE_BYTE) *
637
+ BLOCK_SIZE_BYTE / sizeof(T);
638
+ tailLoopSrcStride_[1] = Ops::Base::CeilDiv(static_cast<int64_t>(tailLoopSize_[0] * sizeof(T)), BLOCK_SIZE_BYTE) *
639
+ BLOCK_SIZE_BYTE / sizeof(T);
640
+ for (int64_t i = 2; i < NDDMA_MAX_DIM_NUM; i++) {
641
+ mainLoopSrcStride_[i] = mainLoopSize_[i - 1] * mainLoopSrcStride_[i - 1];
642
+ inputTailLoopSrcStride_[i] = inputTailLoopSize_[i - 1] * inputTailLoopSrcStride_[i - 1];
643
+ outputTailLoopSrcStride_[i] = outputTailLoopSize_[i - 1] * outputTailLoopSrcStride_[i - 1];
644
+ tailLoopSrcStride_[i] = tailLoopSize_[i - 1] * tailLoopSrcStride_[i - 1];
645
+ }
646
+
647
+ int64_t loopDstStride[NDDMA_MAX_DIM_NUM] = {0, 0, 0, 0, 0};
648
+ loopDstStride[1] = tiling_->expandedOutputShape[NDDMA_MAX_DIM_NUM - 1];
649
+ for (int64_t i = 2; i < NDDMA_MAX_DIM_NUM; i++) {
650
+ loopDstStride[i] = tiling_->expandedOutputShape[NDDMA_MAX_DIM_NUM - i] * loopDstStride[i - 1];
651
+ }
652
+ idx = 1;
653
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
654
+ if (cutIdx[i] < 0) {
655
+ break;
656
+ }
657
+ if (cutIdx[i] + 1 >= NDDMA_MAX_DIM_NUM) {
658
+ break;
659
+ }
660
+ loopDstStride_[idx] = loopDstStride[cutIdx[i] + 1];
661
+ idx++;
662
+ }
663
+ }
664
+
665
+ template <typename T>
666
+ __aicore__ inline void TransposeCutTwoAxis<T>::GetTailAddressOffsetBase()
667
+ {
668
+ // inputTail base address offset
669
+ inputTailSrcAddressOffsetBase_ = 1;
670
+ for (int64_t i = NDDMA_MAX_DIM_NUM - 1; i >= expandedInputCutIndex_; i--) {
671
+ if (i == expandedInputCutIndex_) {
672
+ inputTailSrcAddressOffsetBase_ *= (tiling_->expandedInputShape[i] - tiling_->inTailFactor);
673
+ } else {
674
+ inputTailSrcAddressOffsetBase_ *= tiling_->expandedInputShape[i];
675
+ }
676
+ }
677
+ inputTailDstAddressOffsetBase_ = 1;
678
+ for (int64_t i = NDDMA_MAX_DIM_NUM - 1; i >= outputInputCutIndex_; i--) {
679
+ if (i == outputInputCutIndex_) {
680
+ inputTailDstAddressOffsetBase_ *= (tiling_->expandedOutputShape[i] - tiling_->inTailFactor);
681
+ } else {
682
+ inputTailDstAddressOffsetBase_ *= tiling_->expandedOutputShape[i];
683
+ }
684
+ }
685
+
686
+ // outputTail base address offset
687
+ outputTailSrcAddressOffsetBase_ = 1;
688
+ for (int64_t i = NDDMA_MAX_DIM_NUM - 1; i >= inputOutputCutIndex_; i--) {
689
+ if (i == inputOutputCutIndex_) {
690
+ outputTailSrcAddressOffsetBase_ *= (tiling_->expandedInputShape[i] - tiling_->outTailFactor);
691
+ } else {
692
+ outputTailSrcAddressOffsetBase_ *= tiling_->expandedInputShape[i];
693
+ }
694
+ }
695
+ outputTailDstAddressOffsetBase_ = 1;
696
+ for (int64_t i = NDDMA_MAX_DIM_NUM - 1; i >= expandedOutputCutIndex_; i--) {
697
+ if (i == expandedOutputCutIndex_) {
698
+ outputTailDstAddressOffsetBase_ *= (tiling_->expandedOutputShape[i] - tiling_->outTailFactor);
699
+ } else {
700
+ outputTailDstAddressOffsetBase_ *= tiling_->expandedOutputShape[i];
701
+ }
702
+ }
703
+ }
704
+
705
+ template <typename T>
706
+ __aicore__ inline void TransposeCutTwoAxis<T>::ProcessBlock()
707
+ {
708
+ // 初始化四个结果数据区间
709
+ Interval_ offsetRangeMainRes;
710
+ offsetRangeMainRes.start = 0;
711
+ offsetRangeMainRes.end = tiling_->rangeMainEnd;
712
+
713
+ Interval_ offsetRangeInputTailRes;
714
+ offsetRangeInputTailRes.start = tiling_->rangeInputTailStart;
715
+ offsetRangeInputTailRes.end = tiling_->rangeInputTailEnd;
716
+
717
+ Interval_ offsetRangeOutputTailRes;
718
+ offsetRangeOutputTailRes.start = tiling_->rangeOutputTailStart;
719
+ offsetRangeOutputTailRes.end = tiling_->rangeOutputTailEnd;
720
+
721
+ Interval_ offsetRangeTailRes;
722
+ offsetRangeTailRes.start = tiling_->rangeTailStart;
723
+ offsetRangeTailRes.end = tiling_->rangeTailEnd;
724
+
725
+ // 计算本核上的循环区间
726
+ PorcessOffsetRange(0, tiling_->rangeMainEnd, offsetRangeMainRes);
727
+ PorcessOffsetRange(tiling_->rangeInputTailStart, tiling_->rangeInputTailEnd, offsetRangeInputTailRes);
728
+ PorcessOffsetRange(tiling_->rangeOutputTailStart, tiling_->rangeOutputTailEnd, offsetRangeOutputTailRes);
729
+ PorcessOffsetRange(tiling_->rangeTailStart, tiling_->rangeTailEnd, offsetRangeTailRes);
730
+
731
+ if (offsetRangeMainRes.end != 0 || blockIdx_ == 0) {
732
+ ProcessMain(offsetRangeMainRes.start, offsetRangeMainRes.end);
733
+ }
734
+
735
+ if (offsetRangeInputTailRes.end != 0) {
736
+ ProcessInputTail(offsetRangeInputTailRes.start, offsetRangeInputTailRes.end);
737
+ }
738
+
739
+ if (offsetRangeOutputTailRes.end != 0) {
740
+ ProcessOutputTail(offsetRangeOutputTailRes.start, offsetRangeOutputTailRes.end);
741
+ }
742
+
743
+ if (offsetRangeTailRes.end != 0) {
744
+ ProcessTail(offsetRangeTailRes.start, offsetRangeTailRes.end);
745
+ }
746
+ }
747
+
748
+ template <typename T>
749
+ __aicore__ inline void TransposeCutTwoAxis<T>::ProcessPerCore()
750
+ {
751
+ // cut two axis
752
+ // 对于输入,输出切分轴的index和对于输出,输入切分轴的index
753
+ inputOutputCutIndex_ = tiling_->expandedPerm[expandedOutputCutIndex_];
754
+ for (int64_t i = 0; i < NDDMA_MAX_DIM_NUM; i++) {
755
+ if (tiling_->expandedPerm[i] == expandedInputCutIndex_) {
756
+ outputInputCutIndex_ = i;
757
+ break;
758
+ }
759
+ }
760
+ maxCutIndex_ = expandedOutputCutIndex_ > outputInputCutIndex_ ? expandedOutputCutIndex_ : outputInputCutIndex_;
761
+ minCutIndex_ = expandedOutputCutIndex_ > outputInputCutIndex_ ? outputInputCutIndex_ : expandedOutputCutIndex_;
762
+
763
+ GetCopyOutParams();
764
+ GetTailAddressOffsetBase();
765
+
766
+ // 该核总循环数:blkProcessNum_
767
+ // 该核循环的起始数:srcOffset_
768
+ // 该核的终止数:srcOffset_ + blkProcessNum_ - 1
769
+ // 四个数据区间,初始化
770
+ outUbLoop_ = 1;
771
+ for (int64_t i = NDDMA_MAX_DIM_NUM - 1; i >= 0; i--) {
772
+ if (i != expandedInputCutIndex_ && i != inputOutputCutIndex_) {
773
+ outUbLoop_ = outUbLoop_ * (tiling_->expandedInputShape[i] / tiling_->inUbMainSrcShape[i]);
774
+ }
775
+ }
776
+ ProcessBlock();
777
+ }
778
+
779
+ } // namespace Transpose
780
+
781
+ #endif // TRANSPOSE_CUT_TWO_AXIS