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

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,688 @@
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_with_gather.h
13
+ * \brief transpose_with_gather
14
+ */
15
+ #ifndef KERNEL_TRANSPOSE_WITH_GATHER_H_
16
+ #define KERNEL_TRANSPOSE_WITH_GATHER_H_
17
+
18
+ #include <type_traits>
19
+
20
+ #include "reg_compute/kernel_reg_compute_datacopy_intf.h"
21
+ #include "op_kernel/platform_util.h"
22
+ #include "transpose_base.h"
23
+
24
+ namespace Transpose {
25
+ using namespace AscendC;
26
+ using AscendC::Reg::CreateMask;
27
+ //using AscendC::Reg::DataCopyGather;
28
+ using AscendC::Reg::MaskReg;
29
+ using AscendC::Reg::RegTensor;
30
+ using AscendC::Reg::UpdateMask;
31
+
32
+ constexpr int8_t UB_MAX_DIM_NUM = 6;
33
+ constexpr int8_t NUM_TWO = 2;
34
+ constexpr int8_t NUM_THREE = 3;
35
+
36
+ template <typename T>
37
+ class TransposeWithGather {
38
+ public:
39
+ __aicore__ inline TransposeWithGather(){};
40
+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, const GatherTransposeTilingData* tilingData, TPipe* pipe);
41
+ __aicore__ inline void Process();
42
+
43
+ private:
44
+ __aicore__ inline void GetCoreLoopRange();
45
+ __aicore__ inline void InitAxes();
46
+ __aicore__ inline int32_t CalcUbAxesInOffset(int8_t begIdx);
47
+ __aicore__ inline int32_t CalcUbAxesOutOffset(int8_t begIdx);
48
+ __aicore__ inline int64_t CalcBlkAxesOffset(int8_t begIdx);
49
+ __aicore__ inline void GenIndex4OneDim(int32_t goffset);
50
+ __aicore__ inline void GenIndex4TwoDim(int32_t goffset);
51
+ __aicore__ inline void GenIndex4ThreeDim(int32_t goffset);
52
+ __aicore__ inline void GenGatherIndex(int32_t goffset);
53
+ __aicore__ inline void GenGatherIndex4AllPhase();
54
+ __aicore__ inline void CalcBlkAddr(int64_t idx);
55
+ __aicore__ inline void SetCopyInParams();
56
+ __aicore__ inline void CopyDataIn();
57
+ __aicore__ inline void GetOutLoopAxes();
58
+ __aicore__ inline void SetCopyOutParams();
59
+ __aicore__ inline void CopyDataOut();
60
+ __aicore__ inline void UpdateUbAxes(int64_t blkLpIdx);
61
+ __aicore__ inline void GatherData();
62
+
63
+ private:
64
+ int64_t blockIdx_ = 0;
65
+ const GatherTransposeTilingData* td_ = nullptr;
66
+ TQue<QuePosition::VECIN, 1> xInQue_;
67
+ TQue<QuePosition::VECOUT, 1> xOutQue_;
68
+ TBuf<QuePosition::VECCALC> idxBuf_;
69
+ GlobalTensor<T> xGM_;
70
+ GlobalTensor<T> yGM_;
71
+
72
+ int64_t blkBeg_ = 0;
73
+ int64_t blkEnd_ = 0;
74
+ int64_t blkInAddr_ = 0;
75
+ int64_t blkOutAddr_ = 0;
76
+ int64_t blkInCutROffset_ = 0;
77
+ int64_t blkOutCutROffset_ = 0;
78
+ int64_t blkInCutAxisSize_ = 0;
79
+ int64_t blkOutCutAxisSize_ = 0;
80
+ int32_t inUbAxes_[UB_MAX_DIM_NUM] = {1, 1, 1, 1, 1, 1};
81
+ int32_t outUbAxes_[UB_MAX_DIM_NUM] = {1, 1, 1, 1, 1, 1};
82
+ /* 0: out_cut=false, in_cut=false
83
+ * 1: out_cut=false, in_cut=true
84
+ * 2: out_cut=true, in_cut=false
85
+ * 3: out_cut=true, in_cut=true
86
+ */
87
+ int32_t gIndexId_ = 0; // control index offset of gather
88
+
89
+ using RangeType_ = std::conditional_t<sizeof(T) <= sizeof(int16_t), int16_t, int32_t>;
90
+ using IdxType_ = std::conditional_t<sizeof(T) <= sizeof(int16_t), uint16_t, uint32_t>;
91
+ using CastType_ = std::conditional_t<sizeof(T) == 1,
92
+ std::conditional_t<std::is_same_v<T, uint8_t>, uint16_t, int16_t>, T>;
93
+ uint32_t vlSize_ = static_cast<uint32_t>(Ops::Base::GetVRegSize() / sizeof(CastType_));
94
+ uint32_t idxVLSize_ = static_cast<uint32_t>(Ops::Base::GetVRegSize() / sizeof(RangeType_));
95
+ LocalTensor<RangeType_> idxLocal_;
96
+ int32_t gElemPerBlock_ = static_cast<int32_t>(BLOCK_SIZE_BYTE / sizeof(RangeType_));
97
+ int32_t elemPerBlock_ = static_cast<int32_t>(BLOCK_SIZE_BYTE / sizeof(T));
98
+ int32_t gIdxOffset_ = 0;
99
+
100
+ LoopModeParams lpModeInParams_ = {1, 1, 0, 0, 0, 0};
101
+ LoopModeParams lpModeOutParams_ = {1, 1, 0, 0, 0, 0};
102
+ DataCopyExtParams copyInParams_;
103
+ DataCopyExtParams copyOutParams_;
104
+ uint32_t outUbAxis0_ = 1;
105
+ uint32_t outUbAxis1_ = 1;
106
+ uint32_t outUbAxis2_ = 1;
107
+ uint32_t outUbAxis0InROffset_ = 0;
108
+ uint32_t outUbAxis1InROffset_ = 0;
109
+ uint32_t outUbAxis2InROffset_ = 0;
110
+ int32_t inUbTailFactor_ = 0;
111
+ int32_t outUbTailFactor_ = 0;
112
+ };
113
+
114
+ template <typename T>
115
+ __aicore__ inline void TransposeWithGather<T>::Init(GM_ADDR x, GM_ADDR y, const GatherTransposeTilingData* tilingData,
116
+ TPipe* pipe)
117
+ {
118
+ blockIdx_ = GetBlockIdx();
119
+ td_ = tilingData;
120
+
121
+ pipe->InitBuffer(xInQue_, BUFFER_NUM, td_->dataTensorSize);
122
+ pipe->InitBuffer(xOutQue_, BUFFER_NUM, td_->dataTensorSize);
123
+ pipe->InitBuffer(idxBuf_, td_->indexTensorSize);
124
+ idxLocal_ = idxBuf_.Get<RangeType_>();
125
+
126
+ xGM_.SetGlobalBuffer((__gm__ T*)x);
127
+ yGM_.SetGlobalBuffer((__gm__ T*)y);
128
+ }
129
+
130
+ template <typename T>
131
+ __aicore__ inline void TransposeWithGather<T>::Process()
132
+ {
133
+ if (blockIdx_ >= td_->usedCoreCnt) {
134
+ return;
135
+ }
136
+ GetCoreLoopRange();
137
+ InitAxes();
138
+ GenGatherIndex4AllPhase();
139
+ for (int64_t blkLpIdx = blkBeg_; blkLpIdx < blkEnd_; ++blkLpIdx) {
140
+ CalcBlkAddr(blkLpIdx);
141
+ UpdateUbAxes(blkLpIdx);
142
+ CopyDataIn();
143
+ GetOutLoopAxes();
144
+ GatherData();
145
+ CopyDataOut();
146
+ }
147
+ }
148
+
149
+ template <typename T>
150
+ __aicore__ inline void TransposeWithGather<T>::GetCoreLoopRange()
151
+ {
152
+ blkBeg_ = blockIdx_ * td_->blkFactor;
153
+ if (blockIdx_ != td_->usedCoreCnt - 1) {
154
+ blkEnd_ = blkBeg_ + td_->blkFactor;
155
+ } else {
156
+ blkEnd_ = blkBeg_ + td_->blkTailFactor;
157
+ }
158
+ }
159
+
160
+ template <typename T>
161
+ __aicore__ inline void TransposeWithGather<T>::InitAxes()
162
+ {
163
+ for (int8_t i = 0; i < td_->ubAxesCnt; ++i) {
164
+ inUbAxes_[i] = td_->inUbAxes[i];
165
+ outUbAxes_[i] = td_->outUbAxes[i];
166
+ }
167
+ if (td_->blkInUbCutPos != -1) {
168
+ blkInCutROffset_ = CalcBlkAxesOffset(td_->blkInUbCutPos);
169
+ blkInCutAxisSize_ = td_->blkAxes[td_->blkInUbCutPos];
170
+ }
171
+ if (td_->blkOutUbCutPos != -1) {
172
+ blkOutCutROffset_ = CalcBlkAxesOffset(td_->blkOutUbCutPos);
173
+ blkOutCutAxisSize_ = td_->blkAxes[td_->blkOutUbCutPos];
174
+ }
175
+ }
176
+
177
+ template <typename T>
178
+ __aicore__ inline void TransposeWithGather<T>::UpdateUbAxes(int64_t blkLpIdx)
179
+ {
180
+ bool isInSwitch = false;
181
+ bool isOutSwitch = false;
182
+
183
+ if (td_->blkInUbCutPos != -1) {
184
+ if (blkLpIdx / blkInCutROffset_ % blkInCutAxisSize_ == blkInCutAxisSize_ - 1) {
185
+ inUbAxes_[td_->inUbInCutPos] = inUbTailFactor_;
186
+ outUbAxes_[td_->outUbInCutPos] = inUbTailFactor_;
187
+ isInSwitch = true;
188
+ } else {
189
+ inUbAxes_[td_->inUbInCutPos] = td_->inUbCutAxisFactor;
190
+ outUbAxes_[td_->outUbInCutPos] = td_->inUbCutAxisFactor;
191
+ isInSwitch = false;
192
+ }
193
+ }
194
+ if (td_->blkOutUbCutPos != -1) {
195
+ if (blkLpIdx / blkOutCutROffset_ % blkOutCutAxisSize_ == blkOutCutAxisSize_ - 1) {
196
+ inUbAxes_[td_->inUbOutCutPos] = outUbTailFactor_;
197
+ outUbAxes_[td_->outUbOutCutPos] = outUbTailFactor_;
198
+ isOutSwitch = true;
199
+ } else {
200
+ inUbAxes_[td_->inUbOutCutPos] = td_->outUbCutAxisFactor;
201
+ outUbAxes_[td_->outUbOutCutPos] = td_->outUbCutAxisFactor;
202
+ isOutSwitch = false;
203
+ }
204
+ }
205
+
206
+ if (!isOutSwitch) {
207
+ if (!isInSwitch) {
208
+ gIndexId_ = 0;
209
+ } else {
210
+ gIndexId_ = 1;
211
+ }
212
+ } else {
213
+ if (!isInSwitch) {
214
+ gIndexId_ = NUM_TWO;
215
+ } else {
216
+ gIndexId_ = NUM_THREE;
217
+ }
218
+ }
219
+ }
220
+
221
+ template <typename T>
222
+ __aicore__ inline int32_t TransposeWithGather<T>::CalcUbAxesInOffset(int8_t begIdx)
223
+ {
224
+ int32_t res = 1;
225
+
226
+ // DataCopyPad blockCount and blockLen are consecutive
227
+ if (begIdx + 1 >= td_->inUbInCutPos) {
228
+ for (int8_t i = begIdx + 1; i < td_->ubAxesCnt; ++i) {
229
+ res *= inUbAxes_[i];
230
+ }
231
+ } else {
232
+ for (int8_t i = td_->inUbInCutPos - 1; i < td_->ubAxesCnt; ++i) {
233
+ res *= inUbAxes_[i];
234
+ }
235
+ // DataCopyPad loop2, loop1 are block align
236
+ res = Ops::Base::CeilAlign(res, elemPerBlock_);
237
+ for (int8_t j = begIdx + 1; j < td_->inUbInCutPos - 1; ++j) {
238
+ res *= inUbAxes_[j];
239
+ }
240
+ }
241
+ return res;
242
+ }
243
+
244
+ template <typename T>
245
+ __aicore__ inline int32_t TransposeWithGather<T>::CalcUbAxesOutOffset(int8_t begIdx)
246
+ {
247
+ int32_t res = 1;
248
+ for (int8_t i = begIdx + 1; i < td_->ubAxesCnt; ++i) {
249
+ res *= outUbAxes_[i];
250
+ }
251
+ return res;
252
+ }
253
+
254
+ template <typename T>
255
+ __aicore__ inline int64_t TransposeWithGather<T>::CalcBlkAxesOffset(int8_t begIdx)
256
+ {
257
+ int32_t res = 1;
258
+ for (int8_t i = begIdx + 1; i < td_->blkAxesCnt; ++i) {
259
+ res *= td_->blkAxes[i];
260
+ }
261
+ return res;
262
+ }
263
+
264
+ template <typename T>
265
+ __aicore__ inline void TransposeWithGather<T>::GatherData()
266
+ {
267
+ uint32_t outLen = static_cast<uint32_t>(CalcUbAxesOutOffset(td_->outUbOutCutPos - 1));
268
+ uint32_t outLenAlign = Ops::Base::CeilAlign(outLen, static_cast<uint32_t>(elemPerBlock_));
269
+ uint16_t burstLpCnt = Ops::Base::CeilDiv(outLen, vlSize_);
270
+ uint32_t maskValue = outLen;
271
+ if constexpr (sizeof(T) == sizeof(int8_t)) {
272
+ maskValue *= sizeof(int16_t);
273
+ }
274
+
275
+ auto xInLocal = xInQue_.DeQue<T>();
276
+ auto xOutLocal = xOutQue_.AllocTensor<T>();
277
+ __local_mem__ T* xInAddr = (__local_mem__ T*)xInLocal.GetPhyAddr();
278
+ __local_mem__ T* xOutAddr = (__local_mem__ T*)xOutLocal.GetPhyAddr();
279
+ __local_mem__ RangeType_* idxAddr = (__local_mem__ RangeType_*)idxLocal_.GetPhyAddr();
280
+ idxAddr += static_cast<uint32_t>(gIndexId_ * gIdxOffset_);
281
+
282
+ __VEC_SCOPE__
283
+ {
284
+ RegTensor<T> xReg;
285
+ RegTensor<RangeType_> idxReg;
286
+ RegTensor<RangeType_> idxOriReg;
287
+ MaskReg maskIdx = CreateMask<RangeType_, MicroAPI::MaskPattern::ALL>();
288
+ MaskReg mask;
289
+
290
+ for (uint16_t lpIdx = 0; lpIdx < burstLpCnt; ++lpIdx) {
291
+ mask = UpdateMask<T>(maskValue);
292
+ MicroAPI::DataCopy(idxOriReg, idxAddr + lpIdx * vlSize_);
293
+ uint32_t outIdx = 0;
294
+ for (uint16_t axis2Idx = 0; axis2Idx < static_cast<uint16_t>(outUbAxis2_); ++axis2Idx) {
295
+ RangeType_ axis2Update = static_cast<RangeType_>(axis2Idx * outUbAxis2InROffset_);
296
+ for (uint16_t axis1Idx = 0; axis1Idx < static_cast<uint16_t>(outUbAxis1_); ++axis1Idx) {
297
+ RangeType_ axis1Update = static_cast<RangeType_>(axis1Idx * outUbAxis1InROffset_);
298
+ for (uint16_t axis0Idx = 0; axis0Idx < static_cast<uint16_t>(outUbAxis0_); ++axis0Idx) {
299
+ RangeType_ axis0Update = static_cast<RangeType_>(axis0Idx * outUbAxis0InROffset_);
300
+ RangeType_ idxUpdate = axis2Update + axis1Update + axis0Update;
301
+ MicroAPI::Adds(idxReg, idxOriReg, idxUpdate, maskIdx);
302
+ DataCopyGatherImpl((MicroAPI::RegTensor<CastType_>&)xReg, xInAddr,
303
+ (MicroAPI::RegTensor<IdxType_>&)idxReg, mask);
304
+ //vgather2(xReg, xInAddr, idxReg, mask);
305
+ if constexpr (sizeof(T) != sizeof(int8_t)) {
306
+ MicroAPI::DataCopy(xOutAddr + outIdx * outLenAlign + lpIdx * vlSize_, xReg, mask);
307
+ } else {
308
+ __local_mem__ CastType_* xOutAddrB16 = reinterpret_cast<__local_mem__ CastType_*>(
309
+ xOutAddr + outIdx * outLenAlign + lpIdx * vlSize_);
310
+ MicroAPI::DataCopy<CastType_, MicroAPI::StoreDist::DIST_PACK_B16>(
311
+ xOutAddrB16, (MicroAPI::RegTensor<CastType_>&)xReg, mask);
312
+ }
313
+ ++outIdx;
314
+ }
315
+ }
316
+ }
317
+ }
318
+ }
319
+ xInQue_.FreeTensor(xInLocal);
320
+ xOutQue_.EnQue(xOutLocal);
321
+ }
322
+
323
+ template <typename T>
324
+ __aicore__ inline void TransposeWithGather<T>::GenIndex4OneDim(int32_t goffset)
325
+ {
326
+ int8_t lastPerm = td_->ubPerm[td_->ubAxesCnt - 1];
327
+ uint32_t lastDimSize = static_cast<uint32_t>(outUbAxes_[td_->ubAxesCnt - 1]);
328
+ RangeType_ lastDimInOffset = static_cast<RangeType_>(CalcUbAxesInOffset(lastPerm));
329
+
330
+ __local_mem__ RangeType_* idxAddr = (__local_mem__ RangeType_*)idxLocal_.GetPhyAddr();
331
+ idxAddr += goffset;
332
+ uint16_t loopCnt = static_cast<uint16_t>(Ops::Base::CeilDiv(lastDimSize, idxVLSize_));
333
+
334
+ __VEC_SCOPE__
335
+ {
336
+ RegTensor<RangeType_> srcReg;
337
+ RegTensor<RangeType_> dstReg;
338
+ MaskReg mask;
339
+ MicroAPI::Arange(srcReg, 0);
340
+ for (uint16_t lpIdx = 0; lpIdx < loopCnt; ++lpIdx) {
341
+ mask = UpdateMask<RangeType_>(lastDimSize);
342
+ MicroAPI::Muls(dstReg, srcReg, lastDimInOffset, mask);
343
+ MicroAPI::DataCopy(idxAddr + lpIdx * idxVLSize_, dstReg, mask);
344
+ MicroAPI::Adds(srcReg, srcReg, idxVLSize_, mask);
345
+ }
346
+ }
347
+ }
348
+
349
+ template <typename T>
350
+ __aicore__ inline void TransposeWithGather<T>::GenIndex4TwoDim(int32_t goffset)
351
+ {
352
+ int8_t lastPerm = td_->ubPerm[td_->ubAxesCnt - 1];
353
+ uint32_t lastDimSize = static_cast<uint32_t>(outUbAxes_[td_->ubAxesCnt - 1]);
354
+ RangeType_ lastDimInOffset = static_cast<RangeType_>(CalcUbAxesInOffset(lastPerm));
355
+ int8_t last2ndPerm = td_->ubPerm[td_->ubAxesCnt - NUM_TWO];
356
+ uint32_t last2ndDimSize = static_cast<uint32_t>(outUbAxes_[td_->ubAxesCnt - NUM_TWO]);
357
+ RangeType_ last2ndDimInOffset = static_cast<RangeType_>(CalcUbAxesInOffset(last2ndPerm));
358
+ auto totalDimSize = lastDimSize * last2ndDimSize;
359
+ RangeType_ lastDimInc = static_cast<RangeType_>(idxVLSize_ % lastDimSize);
360
+ RangeType_ last2ndDimInc = static_cast<RangeType_>(idxVLSize_ / lastDimSize);
361
+
362
+ __local_mem__ RangeType_* idxAddr = (__local_mem__ RangeType_*)idxLocal_.GetPhyAddr();
363
+ idxAddr += goffset;
364
+ uint16_t loopCnt = 0;
365
+ uint32_t leftDimSize = 0;
366
+ if (totalDimSize > idxVLSize_) {
367
+ leftDimSize = totalDimSize - idxVLSize_;
368
+ loopCnt = static_cast<uint16_t>(Ops::Base::CeilDiv(leftDimSize, idxVLSize_));
369
+ }
370
+
371
+ /* input: (a, b, c, d)
372
+ * output: (d, c, b, a)
373
+ * generate index for a, b
374
+ */
375
+ __VEC_SCOPE__
376
+ {
377
+ RegTensor<RangeType_> idxReg;
378
+ RegTensor<RangeType_> dim0Reg;
379
+ RegTensor<RangeType_> tmpReg;
380
+ RegTensor<RangeType_> dim1Reg;
381
+ RegTensor<RangeType_> dstReg;
382
+ MaskReg mask = CreateMask<RangeType_, MicroAPI::MaskPattern::ALL>();
383
+ // vec_a: VL % a
384
+ MicroAPI::Arange(idxReg, 0);
385
+ MicroAPI::Duplicate(dim0Reg, lastDimSize);
386
+ MicroAPI::Div(tmpReg, idxReg, dim0Reg, mask);
387
+ MicroAPI::Copy(dim1Reg, tmpReg); // vec_b: VL / a
388
+ MicroAPI::Mul(tmpReg, tmpReg, dim0Reg, mask);
389
+ MicroAPI::Sub(dim0Reg, idxReg, tmpReg, mask);
390
+ // index: vec_a * a_in_offset + vec_b * b_in_offset
391
+ MicroAPI::Muls(tmpReg, dim0Reg, lastDimInOffset, mask);
392
+ MicroAPI::Muls(dstReg, dim1Reg, last2ndDimInOffset, mask);
393
+ MicroAPI::Add(dstReg, dstReg, tmpReg, mask);
394
+ MicroAPI::DataCopy(idxAddr, dstReg, mask);
395
+
396
+ MaskReg lpMask;
397
+ MaskReg selMask;
398
+ RegTensor<RangeType_> zeroReg;
399
+ RegTensor<RangeType_> oneReg;
400
+ RegTensor<RangeType_> cmpReg;
401
+ MicroAPI::Duplicate(zeroReg, 0);
402
+ MicroAPI::Duplicate(oneReg, 1);
403
+ for (uint16_t lpIdx = 0; lpIdx < loopCnt; ++lpIdx) {
404
+ lpMask = UpdateMask<RangeType_>(leftDimSize);
405
+ /* vec_a += a_inc
406
+ * cmp_a = vec_a >= a
407
+ * vec_a = vec_a - cmp_a * a
408
+ */
409
+ MicroAPI::Adds(dim0Reg, dim0Reg, lastDimInc, lpMask);
410
+ MicroAPI::CompareScalar<RangeType_, CMPMODE::GE>(selMask, dim0Reg, lastDimSize, lpMask);
411
+ MicroAPI::Select(cmpReg, oneReg, zeroReg, selMask);
412
+ MicroAPI::Muls(tmpReg, cmpReg, lastDimSize, lpMask);
413
+ MicroAPI::Sub(dim0Reg, dim0Reg, tmpReg, lpMask);
414
+ // vec_b += (b_inc + cmp_a)
415
+ MicroAPI::Adds(dim1Reg, dim1Reg, last2ndDimInc, lpMask);
416
+ MicroAPI::Add(dim1Reg, dim1Reg, cmpReg, lpMask);
417
+ // index: vec_a * a_in_offset + vec_b * b_in_offset
418
+ MicroAPI::Muls(tmpReg, dim0Reg, lastDimInOffset, lpMask);
419
+ MicroAPI::Muls(dstReg, dim1Reg, last2ndDimInOffset, lpMask);
420
+ MicroAPI::Add(dstReg, dstReg, tmpReg, lpMask);
421
+ MicroAPI::DataCopy(idxAddr + (lpIdx + 1) * idxVLSize_, dstReg, lpMask);
422
+ }
423
+ }
424
+ }
425
+
426
+ template <typename T>
427
+ __aicore__ inline void TransposeWithGather<T>::GenIndex4ThreeDim(int32_t goffset)
428
+ {
429
+ int8_t lastPerm = td_->ubPerm[td_->ubAxesCnt - 1];
430
+ uint32_t lastDimSize = static_cast<uint32_t>(outUbAxes_[td_->ubAxesCnt - 1]);
431
+ RangeType_ lastDimInOffset = static_cast<RangeType_>(CalcUbAxesInOffset(lastPerm));
432
+ int8_t last2ndPerm = td_->ubPerm[td_->ubAxesCnt - NUM_TWO];
433
+ uint32_t last2ndDimSize = static_cast<uint32_t>(outUbAxes_[td_->ubAxesCnt - NUM_TWO]);
434
+ RangeType_ last2ndDimInOffset = static_cast<RangeType_>(CalcUbAxesInOffset(last2ndPerm));
435
+ int8_t last3rdPerm = td_->ubPerm[td_->ubAxesCnt - NUM_THREE];
436
+ uint32_t last3rdDimSize = static_cast<uint32_t>(outUbAxes_[td_->ubAxesCnt - NUM_THREE]);
437
+ RangeType_ last3rdDimInOffset = static_cast<RangeType_>(CalcUbAxesInOffset(last3rdPerm));
438
+ auto totalDimSize = lastDimSize * last2ndDimSize * last3rdDimSize;
439
+ RangeType_ lastDimInc = static_cast<RangeType_>(idxVLSize_ % lastDimSize);
440
+ RangeType_ last2ndDimInc = static_cast<RangeType_>(idxVLSize_ / lastDimSize % last2ndDimSize);
441
+ RangeType_ last3rdDimInc = static_cast<RangeType_>(idxVLSize_ / (lastDimSize * last2ndDimSize));
442
+
443
+ __local_mem__ RangeType_* idxAddr = (__local_mem__ RangeType_*)idxLocal_.GetPhyAddr();
444
+ idxAddr += goffset;
445
+ uint16_t loopCnt = 0;
446
+ uint32_t leftDimSize = 0;
447
+ if (totalDimSize > idxVLSize_) {
448
+ leftDimSize = totalDimSize - idxVLSize_;
449
+ loopCnt = static_cast<uint16_t>(Ops::Base::CeilDiv(leftDimSize, idxVLSize_));
450
+ }
451
+
452
+ /* input: (a, b, c, d)
453
+ * output: (d, c, b, a)
454
+ * generate index for a, b, c
455
+ */
456
+ __VEC_SCOPE__
457
+ {
458
+ RegTensor<RangeType_> idxReg;
459
+ RegTensor<RangeType_> dim0Reg;
460
+ RegTensor<RangeType_> tmpReg;
461
+ RegTensor<RangeType_> dim1Reg;
462
+ RegTensor<RangeType_> tmp1Reg;
463
+ RegTensor<RangeType_> dim2Reg;
464
+ RegTensor<RangeType_> dstReg;
465
+ MaskReg mask = CreateMask<RangeType_, MicroAPI::MaskPattern::ALL>();
466
+ // vec_a: VL % a
467
+ MicroAPI::Arange(idxReg, 0);
468
+ MicroAPI::Duplicate(dim0Reg, lastDimSize);
469
+ MicroAPI::Copy(dim2Reg, dim0Reg); // backup a
470
+ MicroAPI::Div(tmpReg, idxReg, dim0Reg, mask);
471
+ MicroAPI::Copy(dim1Reg, tmpReg); // backup VL / a
472
+ MicroAPI::Mul(tmpReg, tmpReg, dim0Reg, mask);
473
+ MicroAPI::Sub(dim0Reg, idxReg, tmpReg, mask);
474
+ // vec_b: VL / a % b
475
+ MicroAPI::Duplicate(tmp1Reg, last2ndDimSize);
476
+ MicroAPI::Mul(dim2Reg, dim2Reg, tmp1Reg, mask); // backup b
477
+ MicroAPI::Div(tmpReg, dim1Reg, tmp1Reg, mask);
478
+ MicroAPI::Mul(tmpReg, tmpReg, tmp1Reg, mask);
479
+ MicroAPI::Sub(dim1Reg, dim1Reg, tmpReg, mask);
480
+ // vec_c: VL / (a * b)
481
+ MicroAPI::Div(dim2Reg, idxReg, dim2Reg, mask);
482
+ // index: vec_a * a_in_offset + vec_b * b_in_offset + vec_c * c_in_offset
483
+ MicroAPI::Muls(tmpReg, dim0Reg, lastDimInOffset, mask);
484
+ MicroAPI::Muls(tmp1Reg, dim1Reg, last2ndDimInOffset, mask);
485
+ MicroAPI::Muls(dstReg, dim2Reg, last3rdDimInOffset, mask);
486
+ MicroAPI::Add(dstReg, dstReg, tmpReg, mask);
487
+ MicroAPI::Add(dstReg, dstReg, tmp1Reg, mask);
488
+ MicroAPI::DataCopy(idxAddr, dstReg, mask);
489
+
490
+ MaskReg lpMask;
491
+ MaskReg selMask;
492
+ RegTensor<RangeType_> zeroReg;
493
+ RegTensor<RangeType_> oneReg;
494
+ RegTensor<RangeType_> cmpReg;
495
+ MicroAPI::Duplicate(zeroReg, 0);
496
+ MicroAPI::Duplicate(oneReg, 1);
497
+ for (uint16_t lpIdx = 0; lpIdx < loopCnt; ++lpIdx) {
498
+ lpMask = UpdateMask<RangeType_>(leftDimSize);
499
+ /* vec_a += a_inc
500
+ * cmp_a = vec_a >= a
501
+ * vec_a = vec_a - cmp_a * a
502
+ */
503
+ MicroAPI::Adds(dim0Reg, dim0Reg, lastDimInc, lpMask);
504
+ MicroAPI::CompareScalar<RangeType_, CMPMODE::GE>(selMask, dim0Reg, lastDimSize, lpMask);
505
+ MicroAPI::Select(cmpReg, oneReg, zeroReg, selMask);
506
+ MicroAPI::Muls(tmpReg, cmpReg, lastDimSize, lpMask);
507
+ MicroAPI::Sub(dim0Reg, dim0Reg, tmpReg, lpMask);
508
+ /* vec_b += (b_inc + cmp_a)
509
+ * cmp_b = vec_b >= b
510
+ * vec_b = vec_b - cmp_b * b
511
+ */
512
+ MicroAPI::Adds(cmpReg, cmpReg, last2ndDimInc, lpMask);
513
+ MicroAPI::Add(dim1Reg, dim1Reg, cmpReg, lpMask);
514
+ MicroAPI::CompareScalar<RangeType_, CMPMODE::GE>(selMask, dim1Reg, last2ndDimSize, lpMask);
515
+ MicroAPI::Select(cmpReg, oneReg, zeroReg, selMask);
516
+ MicroAPI::Muls(tmpReg, cmpReg, last2ndDimSize, lpMask);
517
+ MicroAPI::Sub(dim1Reg, dim1Reg, tmpReg, lpMask);
518
+ // vec_c += (c_inc + cmp_b)
519
+ MicroAPI::Adds(dim2Reg, dim2Reg, last3rdDimInc, lpMask);
520
+ MicroAPI::Add(dim2Reg, dim2Reg, cmpReg, lpMask);
521
+ // index: vec_a * a_in_offset + vec_b * b_in_offset + vec_c * c_in_offset
522
+ MicroAPI::Muls(tmpReg, dim0Reg, lastDimInOffset, lpMask);
523
+ MicroAPI::Muls(tmp1Reg, dim1Reg, last2ndDimInOffset, lpMask);
524
+ MicroAPI::Muls(dstReg, dim2Reg, last3rdDimInOffset, lpMask);
525
+ MicroAPI::Add(dstReg, dstReg, tmpReg, lpMask);
526
+ MicroAPI::Add(dstReg, dstReg, tmp1Reg, lpMask);
527
+ MicroAPI::DataCopy(idxAddr + (lpIdx + 1) * idxVLSize_, dstReg, lpMask);
528
+ }
529
+ }
530
+ }
531
+
532
+ template <typename T>
533
+ __aicore__ inline void TransposeWithGather<T>::GenGatherIndex(int32_t goffset)
534
+ {
535
+ int8_t dimNum = td_->ubAxesCnt - td_->outUbOutCutPos;
536
+ // max borrowed axes count is 3
537
+ if (dimNum == 1) {
538
+ GenIndex4OneDim(goffset);
539
+ } else if (dimNum == NUM_TWO) {
540
+ GenIndex4TwoDim(goffset);
541
+ } else {
542
+ GenIndex4ThreeDim(goffset);
543
+ }
544
+ }
545
+
546
+ template <typename T>
547
+ __aicore__ inline void TransposeWithGather<T>::GenGatherIndex4AllPhase()
548
+ {
549
+ GenGatherIndex(0);
550
+ gIdxOffset_ = Ops::Base::CeilAlign(CalcUbAxesOutOffset(td_->outUbOutCutPos - 1), gElemPerBlock_);
551
+ inUbTailFactor_ = td_->inUbCutAxisSize % td_->inUbCutAxisFactor;
552
+ outUbTailFactor_ = td_->outUbCutAxisSize % td_->outUbCutAxisFactor;
553
+ if (td_->blkOutUbCutPos == -1 && td_->blkInUbCutPos != -1) {
554
+ inUbAxes_[td_->inUbInCutPos] = inUbTailFactor_;
555
+ outUbAxes_[td_->outUbInCutPos] = inUbTailFactor_;
556
+ GenGatherIndex(gIdxOffset_);
557
+ } else if (td_->blkOutUbCutPos != -1 && td_->blkInUbCutPos == -1) {
558
+ inUbAxes_[td_->inUbOutCutPos] = outUbTailFactor_;
559
+ outUbAxes_[td_->outUbOutCutPos] = outUbTailFactor_;
560
+ GenGatherIndex(gIdxOffset_ * NUM_TWO);
561
+ } else if (td_->blkOutUbCutPos != -1 && td_->blkInUbCutPos != -1) {
562
+ inUbAxes_[td_->inUbInCutPos] = inUbTailFactor_;
563
+ outUbAxes_[td_->outUbInCutPos] = inUbTailFactor_;
564
+ GenGatherIndex(gIdxOffset_);
565
+ inUbAxes_[td_->inUbOutCutPos] = outUbTailFactor_;
566
+ outUbAxes_[td_->outUbOutCutPos] = outUbTailFactor_;
567
+ inUbAxes_[td_->inUbInCutPos] = td_->inUbCutAxisFactor;
568
+ outUbAxes_[td_->outUbInCutPos] = td_->inUbCutAxisFactor;
569
+ GenGatherIndex(gIdxOffset_ * NUM_TWO);
570
+ inUbAxes_[td_->inUbInCutPos] = inUbTailFactor_;
571
+ outUbAxes_[td_->outUbInCutPos] = inUbTailFactor_;
572
+ GenGatherIndex(gIdxOffset_ * NUM_THREE);
573
+ }
574
+ // recover ub axes for next process
575
+ inUbAxes_[td_->inUbOutCutPos] = td_->outUbCutAxisFactor;
576
+ inUbAxes_[td_->inUbInCutPos] = td_->inUbCutAxisFactor;
577
+ outUbAxes_[td_->outUbInCutPos] = td_->inUbCutAxisFactor;
578
+ outUbAxes_[td_->outUbOutCutPos] = td_->outUbCutAxisFactor;
579
+ }
580
+
581
+ template <typename T>
582
+ __aicore__ inline void TransposeWithGather<T>::CalcBlkAddr(int64_t idx)
583
+ {
584
+ int64_t tmpIdx = 0;
585
+ blkInAddr_ = 0;
586
+ blkOutAddr_ = 0;
587
+ for (int8_t i = 0; i < td_->blkAxesCnt; ++i) {
588
+ tmpIdx = idx / CalcBlkAxesOffset(i) % td_->blkAxes[i];
589
+ blkInAddr_ += tmpIdx * td_->blkAxesInAOffset[i];
590
+ blkOutAddr_ += tmpIdx * td_->blkAxesOutAOffset[i];
591
+ }
592
+ }
593
+
594
+ template <typename T>
595
+ __aicore__ inline void TransposeWithGather<T>::SetCopyInParams()
596
+ {
597
+ int32_t inUbAxis0 = 1;
598
+ int32_t inUbAxis1 = 1;
599
+ int32_t inUbAxis2 = 1;
600
+ if (td_->inUbInCutPos == 1) {
601
+ inUbAxis0 = inUbAxes_[0];
602
+ } else if (td_->inUbInCutPos == NUM_TWO) {
603
+ inUbAxis0 = inUbAxes_[1];
604
+ inUbAxis1 = inUbAxes_[0];
605
+ } else if (td_->inUbInCutPos == NUM_THREE) {
606
+ inUbAxis0 = inUbAxes_[NUM_TWO];
607
+ inUbAxis1 = inUbAxes_[1];
608
+ inUbAxis2 = inUbAxes_[0];
609
+ }
610
+
611
+ uint32_t inCube = static_cast<size_t>(CalcUbAxesInOffset(td_->inUbInCutPos - 1)) * sizeof(T);
612
+ copyInParams_.blockCount = inUbAxis0;
613
+ copyInParams_.blockLen = inCube;
614
+ copyInParams_.srcStride = td_->axis0InSrcStride * sizeof(T) - inCube;
615
+ copyInParams_.dstStride = 0;
616
+ lpModeInParams_.loop1Size = inUbAxis1;
617
+ lpModeInParams_.loop2Size = inUbAxis2;
618
+ lpModeInParams_.loop1SrcStride = td_->axis1InSrcStride * sizeof(T);
619
+ lpModeInParams_.loop2SrcStride = td_->axis2InSrcStride * sizeof(T);
620
+ lpModeInParams_.loop1DstStride = Ops::Base::CeilAlign(
621
+ static_cast<int64_t>(copyInParams_.blockCount * copyInParams_.blockLen), BLOCK_SIZE_BYTE);
622
+ lpModeInParams_.loop2DstStride = lpModeInParams_.loop1Size * lpModeInParams_.loop1DstStride;
623
+ }
624
+
625
+ template <typename T>
626
+ __aicore__ inline void TransposeWithGather<T>::CopyDataIn()
627
+ {
628
+ SetCopyInParams();
629
+ DataCopyPadExtParams<T> padParams{false, 0, 0, 0};
630
+ auto xInLocal = xInQue_.AllocTensor<T>();
631
+ SetLoopModePara(lpModeInParams_, DataCopyMVType::OUT_TO_UB);
632
+ DataCopyPad<T, PaddingMode::Compact>(xInLocal, xGM_[blkInAddr_], copyInParams_, padParams);
633
+ ResetLoopModePara(DataCopyMVType::OUT_TO_UB);
634
+ xInQue_.EnQue(xInLocal);
635
+ }
636
+
637
+ template <typename T>
638
+ __aicore__ inline void TransposeWithGather<T>::GetOutLoopAxes()
639
+ {
640
+ if (td_->outUbOutCutPos == 1) {
641
+ outUbAxis0_ = outUbAxes_[0];
642
+ outUbAxis0InROffset_ = CalcUbAxesInOffset(td_->ubPerm[0]);
643
+ } else if (td_->outUbOutCutPos == NUM_TWO) {
644
+ outUbAxis0_ = outUbAxes_[1];
645
+ outUbAxis1_ = outUbAxes_[0];
646
+ outUbAxis0InROffset_ = CalcUbAxesInOffset(td_->ubPerm[1]);
647
+ outUbAxis1InROffset_ = CalcUbAxesInOffset(td_->ubPerm[0]);
648
+ } else if (td_->outUbOutCutPos == NUM_THREE) {
649
+ outUbAxis0_ = outUbAxes_[NUM_TWO];
650
+ outUbAxis1_ = outUbAxes_[1];
651
+ outUbAxis2_ = outUbAxes_[0];
652
+ outUbAxis0InROffset_ = CalcUbAxesInOffset(td_->ubPerm[NUM_TWO]);
653
+ outUbAxis1InROffset_ = CalcUbAxesInOffset(td_->ubPerm[1]);
654
+ outUbAxis2InROffset_ = CalcUbAxesInOffset(td_->ubPerm[0]);
655
+ }
656
+ }
657
+
658
+ template <typename T>
659
+ __aicore__ inline void TransposeWithGather<T>::SetCopyOutParams()
660
+ {
661
+ uint32_t outCube = static_cast<size_t>(CalcUbAxesOutOffset(td_->outUbOutCutPos - 1)) * sizeof(T);
662
+ uint32_t outCubeAlign = Ops::Base::CeilAlign(outCube, static_cast<uint32_t>(BLOCK_SIZE_BYTE));
663
+ copyOutParams_.blockCount = outUbAxis0_;
664
+ copyOutParams_.blockLen = outCube;
665
+ copyOutParams_.srcStride = 0;
666
+ copyOutParams_.dstStride = td_->axis0OutDstStride * sizeof(T) - outCube;
667
+ lpModeOutParams_.loop1Size = outUbAxis1_;
668
+ lpModeOutParams_.loop2Size = outUbAxis2_;
669
+ lpModeOutParams_.loop1SrcStride = outUbAxis0_ * outCubeAlign;
670
+ lpModeOutParams_.loop2SrcStride = outUbAxis1_ * lpModeOutParams_.loop1SrcStride;
671
+ lpModeOutParams_.loop1DstStride = td_->axis1OutDstStride * sizeof(T);
672
+ lpModeOutParams_.loop2DstStride = td_->axis2OutDstStride * sizeof(T);
673
+ }
674
+
675
+ template <typename T>
676
+ __aicore__ inline void TransposeWithGather<T>::CopyDataOut()
677
+ {
678
+ SetCopyOutParams();
679
+ auto xOutLocal = xOutQue_.DeQue<T>();
680
+ SetLoopModePara(lpModeOutParams_, DataCopyMVType::UB_TO_OUT);
681
+ DataCopyPad(yGM_[blkOutAddr_], xOutLocal, copyOutParams_);
682
+ ResetLoopModePara(DataCopyMVType::UB_TO_OUT);
683
+ xOutQue_.FreeTensor(xOutLocal);
684
+ }
685
+
686
+ } // namespace Transpose
687
+
688
+ #endif // TRANSPOSE_CUT_TWO_AXIS