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,348 @@
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 softmax_v2_ar_small_r.h
13
+ * \brief
14
+ */
15
+
16
+ #ifndef SOFTMAX_V2_AR_SMALL_R_H
17
+ #define SOFTMAX_V2_AR_SMALL_R_H
18
+
19
+ #include "kernel_tiling/kernel_tiling.h"
20
+
21
+ #include "kernel_operator.h"
22
+ #include "softmax_v2_base.h"
23
+
24
+ #ifndef INFINITY
25
+ #define INFINITY (__builtin_inff())
26
+ #endif
27
+
28
+ namespace SoftmaxV2Ops
29
+ {
30
+ using namespace AscendC;
31
+
32
+ using AscendC::MicroAPI::MaskReg;
33
+ using AscendC::MicroAPI::RegTensor;
34
+
35
+ template <typename Tx, typename Ty>
36
+ class SoftmaxV2ArSmallR
37
+ {
38
+ static constexpr int32_t BUFFER_NUM = 2;
39
+ static constexpr int32_t BUFFER_DEPTH = 1;
40
+ static constexpr int64_t DATA_BLOCK_COUNT = 16;
41
+ static constexpr int64_t DATA_BLOCK_COUNT_HALF = 8;
42
+ static constexpr uint32_t VL_FP32 = GetVRegSize() / sizeof(float);
43
+ static constexpr int64_t BLOCK_SIZE = GetUbBlockSize();
44
+
45
+ public:
46
+ __aicore__ inline SoftmaxV2ArSmallR(TPipe* pipe)
47
+ {
48
+ pipe_ = pipe;
49
+ };
50
+
51
+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, const SoftmaxV2ArSmallRTilingData* tilingDataIn)
52
+ {
53
+ tl_ = tilingDataIn;
54
+
55
+ xGm_.SetGlobalBuffer((__gm__ Tx*)x);
56
+ yGm_.SetGlobalBuffer((__gm__ Ty*)y);
57
+
58
+ int64_t xShapeLen = tl_->tileA0Len * tl_->rAligned;
59
+ pipe_->InitBuffer(xQueue_, BUFFER_NUM, xShapeLen * sizeof(Tx));
60
+ pipe_->InitBuffer(yQueue_, BUFFER_NUM, xShapeLen * sizeof(Ty));
61
+
62
+ pipe_->InitBuffer(tmpBuf_, BUFFER_NUM * xShapeLen * sizeof(float) + tl_->tileA0Len * sizeof(float));
63
+ }
64
+
65
+ __aicore__ inline void Process()
66
+ {
67
+ int64_t blockIdx = GetBlockIdx();
68
+ int64_t beginIdx = blockIdx * tl_->tilesPerCore;
69
+ int64_t endIdx = beginIdx + tl_->tilesPerCore;
70
+ endIdx = endIdx > tl_->totalTiles ? tl_->totalTiles : endIdx;
71
+ LocalTensor<float> totalTmpLocal = tmpBuf_.Get<float>();
72
+ tmpLocal_ = totalTmpLocal[0];
73
+ sumLocal_ = totalTmpLocal[BUFFER_NUM * tl_->tileA0Len * tl_->rAligned];
74
+
75
+ // preload
76
+ int64_t xOffset = beginIdx * tl_->tileA0Len * tl_->totalRLen;
77
+ CopyInAndTransPose(xOffset, beginIdx == (tl_->totalTiles - 1) ? tl_->tileA0Tail : tl_->tileA0Len, tl_->totalRLen);
78
+
79
+ int curIdx;
80
+ uint32_t curTileA0Len = tl_->tileA0Len;
81
+ uint32_t nextTileA0Len = tl_->tileA0Len;
82
+ for (curIdx = beginIdx; curIdx < endIdx - 1; curIdx++) {
83
+ if (curIdx + 1 == (tl_->totalTiles - 1)) {
84
+ nextTileA0Len = tl_->tileA0Tail;
85
+ }
86
+ int64_t xOffsetPreLoad = (curIdx + 1) * tl_->tileA0Len * tl_->totalRLen;
87
+ xOffset = curIdx * tl_->tileA0Len * tl_->totalRLen;
88
+
89
+ CalcMaxSubExp(curTileA0Len, tl_->totalRLen);
90
+ CopyInAndTransPose(xOffsetPreLoad, nextTileA0Len, tl_->totalRLen); // (curA0, R) -> (R, curA0)
91
+ CalcReduceSum(curTileA0Len, tl_->totalRLen);
92
+ CalcOutput(curTileA0Len, tl_->totalRLen);
93
+ CalcTranspose(curTileA0Len, tl_->rAligned);
94
+ CopyOutY(xOffset, curTileA0Len);
95
+ }
96
+
97
+ if (curIdx == (tl_->totalTiles - 1)) {
98
+ curTileA0Len = tl_->tileA0Tail;
99
+ }
100
+ xOffset = curIdx * tl_->tileA0Len * tl_->totalRLen;
101
+ CalcMaxSubExp(curTileA0Len, tl_->totalRLen);
102
+ CalcReduceSum(curTileA0Len, tl_->totalRLen);
103
+ CalcOutput(curTileA0Len, tl_->totalRLen);
104
+ CalcTranspose(curTileA0Len, tl_->rAligned);
105
+ CopyOutY(xOffset, curTileA0Len);
106
+ }
107
+
108
+ private:
109
+ __aicore__ inline void CalcMaxSubExp(uint32_t curTileA0Len, uint32_t totalRLen)
110
+ {
111
+ LocalTensor<Tx> xLocal_ = xQueue_.DeQue<Tx>();
112
+ __local_mem__ Tx* xAddr = (__local_mem__ Tx*)xLocal_.GetPhyAddr();
113
+ __local_mem__ float* tmpAddr = (__local_mem__ float*)tmpLocal_.GetPhyAddr();
114
+ __local_mem__ float* tmpAddr2 = (__local_mem__ float*)tmpLocal_[tl_->tileA0Len * tl_->rAligned].GetPhyAddr();
115
+
116
+ uint16_t aLoopTimes = ops::CeilDiv(curTileA0Len, VL_FP32);
117
+ uint16_t rLoopTimes = static_cast<uint16_t>(totalRLen);
118
+
119
+ __VEC_SCOPE__
120
+ {
121
+ MicroAPI::RegTensor<Tx> reg0;
122
+ MicroAPI::RegTensor<float> reg1, reg2, reg3, maxReg;
123
+ MicroAPI::MaskReg mask;
124
+ uint32_t width = curTileA0Len;
125
+ uint32_t tileA0LenLocal = tl_->tileA0Len;
126
+
127
+ for (uint16_t j = 0; j < aLoopTimes; j++) {
128
+ mask = MicroAPI::UpdateMask<uint32_t>(width);
129
+ MicroAPI::Duplicate<float>(maxReg, static_cast<float>(-INFINITY));
130
+
131
+ for (uint16_t i = 0; i < rLoopTimes; i++) {
132
+ uint32_t offset = j * VL_FP32 + i * tileA0LenLocal;
133
+ LoadTensorForDtypeT(xAddr, reg1, mask, offset);
134
+ MicroAPI::Max(maxReg, maxReg, reg1, mask);
135
+ }
136
+
137
+ for (uint16_t i = 0; i < rLoopTimes; i++) {
138
+ uint32_t offset = j * VL_FP32 + i * tileA0LenLocal;
139
+ LoadTensorForDtypeT(xAddr, reg2, mask, offset);
140
+ MicroAPI::Sub(reg2, reg2, maxReg, mask);
141
+ MicroAPI::Exp(reg2, reg2, mask);
142
+ MicroAPI::DataCopy(tmpAddr + offset, reg2, mask);
143
+ MicroAPI::DataCopy(tmpAddr2 + offset, reg2, mask);
144
+ }
145
+ }
146
+ }
147
+
148
+ xQueue_.FreeTensor(xLocal_);
149
+ }
150
+
151
+ __aicore__ inline void CalcReduceSum(uint32_t curTileA0Len, uint32_t totalRLen)
152
+ {
153
+ uint32_t srcShape[2] = {uint32_t(totalRLen), uint32_t(tl_->tileA0Len)};
154
+ AscendC::ReduceSum<float, Pattern::Reduce::RA, true>(sumLocal_, tmpLocal_, srcShape, false);
155
+ }
156
+
157
+ __aicore__ inline void CalcOutput(uint32_t curTileA0Len, uint32_t totalRLen)
158
+ {
159
+ __local_mem__ float* sumAddr = (__local_mem__ float*)sumLocal_.GetPhyAddr();
160
+ __local_mem__ float* tmpAddr2 = (__local_mem__ float*)tmpLocal_[tl_->tileA0Len * tl_->rAligned].GetPhyAddr();
161
+ tmpLocalTy_ = tmpLocal_.template ReinterpretCast<Ty>();
162
+ __local_mem__ Ty* tmpAddrTy = (__local_mem__ Ty*)tmpLocalTy_.GetPhyAddr();
163
+
164
+ uint16_t aLoopTimes = static_cast<uint16_t>(ops::CeilDiv(curTileA0Len, VL_FP32));
165
+ uint16_t rLoopTimes = static_cast<uint16_t>(tl_->totalRLen);
166
+
167
+ __VEC_SCOPE__
168
+ {
169
+ MicroAPI::RegTensor<float> reg1;
170
+ MicroAPI::RegTensor<float> sumReg;
171
+ MicroAPI::MaskReg mask;
172
+
173
+ uint32_t sreg = curTileA0Len;
174
+ uint32_t tileA0LenLocal = tl_->tileA0Len;
175
+
176
+ for (uint16_t j = 0; j < aLoopTimes; j++) { // 列
177
+ mask = MicroAPI::UpdateMask<float>(sreg);
178
+ MicroAPI::DataCopy<float, MicroAPI::LoadDist::DIST_NORM>(sumReg, (__local_mem__ float*)sumAddr + j * VL_FP32);
179
+
180
+ for (uint16_t i = 0; i < rLoopTimes; i++) { // 行
181
+ uint32_t offset = j * VL_FP32 + i * tileA0LenLocal;
182
+
183
+ MicroAPI::DataCopy(reg1, tmpAddr2 + offset);
184
+ MicroAPI::Div(reg1, reg1, sumReg, mask);
185
+
186
+ if constexpr (yToFp32_) {
187
+ MicroAPI::DataCopy(tmpAddrTy + offset, reg1, mask);
188
+ } else { // fp16、bf16
189
+ MicroAPI::RegTensor<Ty> xFp16;
190
+ MicroAPI::Cast<Ty, float, castTraitFp32ToFp16>(xFp16, reg1, mask);
191
+ MicroAPI::DataCopy<Ty, MicroAPI::StoreDist::DIST_PACK_B32>(tmpAddrTy + offset, xFp16, mask);
192
+ }
193
+ }
194
+ }
195
+ }
196
+ }
197
+
198
+ __aicore__ inline void CalcTransposeFp32(const LocalTensor<Ty>& yLocal, uint32_t curTileA0Len, uint32_t rAligned)
199
+ {
200
+ int rRepeartTimes = ops::CeilDiv(static_cast<int64_t>(rAligned), DATA_BLOCK_COUNT_HALF);
201
+ for (int i = 0; i < rRepeartTimes; i++) {
202
+ TransDataTo5HDParams params;
203
+ LocalTensor<Ty> srcLocalList[DATA_BLOCK_COUNT];
204
+ LocalTensor<Ty> dstLocalList[DATA_BLOCK_COUNT];
205
+
206
+ uint32_t aRepeartTimes = ops::CeilDiv(curTileA0Len, uint32_t(DATA_BLOCK_COUNT));
207
+ params.repeatTimes = aRepeartTimes;
208
+ params.srcRepStride = aRepeartTimes == 1 ? 0 : CONST_TWO;
209
+ params.dstRepStride = aRepeartTimes == 1 ? 0 : DATA_BLOCK_COUNT * rRepeartTimes;
210
+ for (int j = 0; j < DATA_BLOCK_COUNT_HALF; j++) {
211
+ uint32_t offset = DATA_BLOCK_COUNT_HALF * tl_->tileA0Len * i + tl_->tileA0Len * j;
212
+ srcLocalList[j] = tmpLocalTy_[offset];
213
+ srcLocalList[j + DATA_BLOCK_COUNT_HALF] = tmpLocalTy_[offset + DATA_BLOCK_COUNT_HALF];
214
+ }
215
+ for (int j = 0; j < DATA_BLOCK_COUNT_HALF; j++) {
216
+ uint32_t offset = DATA_BLOCK_COUNT_HALF * i + DATA_BLOCK_COUNT_HALF * rRepeartTimes * j;
217
+ dstLocalList[j * CONST_TWO] = yLocal[offset];
218
+ dstLocalList[j * CONST_TWO + 1] = yLocal[offset + DATA_BLOCK_COUNT_HALF * DATA_BLOCK_COUNT_HALF * rRepeartTimes];
219
+ }
220
+
221
+ AscendC::TransDataTo5HD(dstLocalList, srcLocalList, params);
222
+ }
223
+ }
224
+
225
+ __aicore__ inline void CalcTransposeFp16(const LocalTensor<Ty>& yLocal, uint32_t curTileA0Len, uint32_t rAligned)
226
+ {
227
+ int rRepeartTimes = ops::CeilDiv(static_cast<int64_t>(rAligned), tl_->rTileBase);
228
+ for (int i = 0; i < rRepeartTimes; i++) {
229
+ TransDataTo5HDParams params;
230
+ int64_t aRepeartTimes = ops::CeilDiv(static_cast<int64_t>(curTileA0Len), tl_->rTileBase);
231
+ params.repeatTimes = aRepeartTimes;
232
+ params.srcRepStride = aRepeartTimes == 1 ? 0 : 1;
233
+ params.dstRepStride = aRepeartTimes == 1 ? 0 : (tl_->rTileBase * rRepeartTimes);
234
+ LocalTensor<Ty> srcLocalList[DATA_BLOCK_COUNT];
235
+ LocalTensor<Ty> dstLocalList[DATA_BLOCK_COUNT];
236
+ for (int j = 0; j < DATA_BLOCK_COUNT; j++) {
237
+ uint32_t offset = tl_->rTileBase * tl_->tileA0Len * i + tl_->tileA0Len * j;
238
+ srcLocalList[j] = tmpLocalTy_[offset];
239
+ }
240
+ for (int j = 0; j < DATA_BLOCK_COUNT; j++) {
241
+ uint32_t offset = tl_->rTileBase * i + rAligned * j;
242
+ dstLocalList[j] = yLocal[offset];
243
+ }
244
+
245
+ AscendC::TransDataTo5HD<Ty>(dstLocalList, srcLocalList, params);
246
+ }
247
+ }
248
+
249
+ __aicore__ inline void CalcTranspose(uint32_t curTileA0Len, uint32_t rAligned)
250
+ {
251
+ LocalTensor<Ty> yLocal = yQueue_.AllocTensor<Ty>();
252
+
253
+ if constexpr (yToFp32_) {
254
+ CalcTransposeFp32(yLocal, curTileA0Len, rAligned);
255
+ } else {
256
+ CalcTransposeFp16(yLocal, curTileA0Len, rAligned);
257
+ }
258
+
259
+ yQueue_.EnQue(yLocal);
260
+ }
261
+
262
+ __aicore__ inline void LoadTensorForDtypeT(const __local_mem__ Tx* src, RegTensor<float>& dst, MaskReg& preg,
263
+ uint32_t offset)
264
+ {
265
+ if constexpr (xToFp32_) {
266
+ MicroAPI::RegTensor<Tx> xFp16;
267
+ MicroAPI::DataCopy<Tx, MicroAPI::LoadDist::DIST_UNPACK_B16>(xFp16, ((__local_mem__ Tx*)src + offset));
268
+ MicroAPI::Cast<float, Tx, castTraitFp16ToFp32>(dst, xFp16, preg);
269
+ } else {
270
+ MicroAPI::DataCopy<float, MicroAPI::LoadDist::DIST_NORM>(dst, (__local_mem__ float*)src + offset);
271
+ }
272
+ }
273
+
274
+ __aicore__ inline void CopyInAndTransPose(int64_t xGmOffset, uint32_t curTileA0Len, uint32_t totalRLen)
275
+ {
276
+ static constexpr MultiCopyConfig config = {false};
277
+ MultiCopyLoopInfo<CONST_TWO> copyLoopInfo;
278
+ copyLoopInfo.loopSrcStride[0] = 1;
279
+ copyLoopInfo.loopSrcStride[1] = totalRLen;
280
+ copyLoopInfo.loopDstStride[0] = tl_->tileA0Len;
281
+ copyLoopInfo.loopDstStride[1] = 1;
282
+ copyLoopInfo.loopSize[0] = totalRLen;
283
+ copyLoopInfo.loopSize[1] = curTileA0Len;
284
+ MultiCopyParams<Tx, CONST_TWO> params = {copyLoopInfo, 0};
285
+
286
+ LocalTensor<Tx> xLocal_ = xQueue_.AllocTensor<Tx>();
287
+ DataCopy<Tx, CONST_TWO, config>(xLocal_, xGm_[xGmOffset], params);
288
+ xQueue_.EnQue(xLocal_);
289
+ }
290
+
291
+ __aicore__ inline void CopyOutFp32(const LocalTensor<Ty>& yLocal, int64_t yGmOffset, uint32_t curTileA0Len)
292
+ {
293
+ DataCopyParams copyOutParams;
294
+ copyOutParams.blockCount = curTileA0Len;
295
+ copyOutParams.blockLen = tl_->totalRLen * sizeof(Ty);
296
+ copyOutParams.dstStride = 0;
297
+ copyOutParams.srcStride = 0;
298
+ DataCopyPad(yGm_[yGmOffset], yLocal[0], copyOutParams);
299
+ }
300
+
301
+ __aicore__ inline void CopyOutFp16(const LocalTensor<Ty>& yLocal, int64_t yGmOffset, uint32_t curTileA0Len)
302
+ {
303
+ DataCopyParams copyOutParams;
304
+ copyOutParams.blockCount = curTileA0Len;
305
+ copyOutParams.blockLen = tl_->totalRLen * sizeof(Ty);
306
+ copyOutParams.srcStride = 0;
307
+ copyOutParams.dstStride = 0;
308
+ DataCopyPad(yGm_[yGmOffset], yLocal[0], copyOutParams);
309
+ }
310
+
311
+ __aicore__ inline void CopyOutY(int64_t yGmOffset, uint32_t curTileA0Len)
312
+ {
313
+ LocalTensor<Ty> yLocal = yQueue_.DeQue<Ty>();
314
+
315
+ if constexpr (yToFp32_) {
316
+ CopyOutFp32(yLocal, yGmOffset, curTileA0Len);
317
+ } else {
318
+ CopyOutFp16(yLocal, yGmOffset, curTileA0Len);
319
+ }
320
+
321
+ yQueue_.FreeTensor(yLocal);
322
+ }
323
+
324
+ private:
325
+ const SoftmaxV2ArSmallRTilingData* tl_;
326
+
327
+ TPipe* pipe_;
328
+
329
+ TQue<QuePosition::VECIN, BUFFER_DEPTH> xQueue_;
330
+ TQue<QuePosition::VECOUT, BUFFER_DEPTH> yQueue_;
331
+
332
+ GlobalTensor<Tx> xGm_;
333
+ GlobalTensor<Ty> yGm_;
334
+
335
+ TBuf<TPosition::VECCALC> tmpBuf_;
336
+
337
+ LocalTensor<float> sumLocal_;
338
+ LocalTensor<float> tmpLocal_;
339
+ LocalTensor<Ty> tmpLocalTy_;
340
+
341
+ static constexpr bool xToFp32_ = !IsSameType<Tx, float>::value;
342
+ static constexpr bool yToFp32_ = IsSameType<Ty, float>::value;
343
+ };
344
+
345
+
346
+ } // namespace SoftmaxV2Ops
347
+
348
+ #endif