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,429 @@
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_full_load.h
13
+ * \brief
14
+ */
15
+
16
+ #ifndef SOFTMAX_V2_AR_FULL_LOAD_H
17
+ #define SOFTMAX_V2_AR_FULL_LOAD_H
18
+
19
+ #include "kernel_tiling/kernel_tiling.h"
20
+ #include "kernel_operator.h"
21
+
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
+ constexpr static uint32_t DOUBLE_BUFFER = 2;
33
+ constexpr static uint32_t BLOCK_SIZE = 32; // 32B
34
+ constexpr static uint32_t AR_FULL_LOAD_BINARY_TMP_BYTES = 512;
35
+
36
+ template <typename T_in, typename T_out>
37
+ class SoftmaxV2AR : public SoftmaxV2OpsBase
38
+ {
39
+ public:
40
+ __aicore__ inline SoftmaxV2AR(TPipe* pipe)
41
+ {
42
+ pipe_ = pipe;
43
+ };
44
+
45
+ __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, const SoftmaxV2ARTilingData* tilingData);
46
+ __aicore__ inline void Process();
47
+
48
+ private:
49
+ __aicore__ inline void ProcessUB(int64_t ubA, int64_t aOffset);
50
+
51
+ __aicore__ inline void FirstNormCompute(int64_t ubA, __local_mem__ T_in* xInAddr,
52
+ __local_mem__ float* xTmpLocalAddr);
53
+ __aicore__ inline void SecondNormCompute(const LocalTensor<T_out>& dstTensor, const LocalTensor<float>& srcTensor,
54
+ const LocalTensor<float>& reduceSumTempTensor, const int64_t aSize,
55
+ const int64_t rSize, const int64_t stride);
56
+ __aicore__ inline void SecondNormComputePost(const LocalTensor<T_out>& dstTensor,
57
+ const LocalTensor<float>& srcTensor,
58
+ const LocalTensor<float>& oriSrcTensor, const int64_t aSize,
59
+ const int64_t rSize, const int64_t stride);
60
+
61
+ __aicore__ inline void CopyInX(const LocalTensor<T_in>& xInUb, int64_t ubA, int64_t offset);
62
+ __aicore__ inline void CopyOutY(const LocalTensor<T_out>& yOutUb, int64_t ubA, int64_t offset);
63
+ __aicore__ inline void LoadTensorForDtypeTIn(__local_mem__ T_in* src, AscendC::MicroAPI::RegTensor<float>& dst,
64
+ AscendC::MicroAPI::MaskReg& preg, uint32_t offset);
65
+ __aicore__ inline void StoreTensorForDtypeTOut(__local_mem__ T_out* dst, AscendC::MicroAPI::RegTensor<float>& src,
66
+ AscendC::MicroAPI::MaskReg& preg, uint32_t offset);
67
+
68
+ private:
69
+ /* global memory address */
70
+ GlobalTensor<T_in> xGm_;
71
+ GlobalTensor<T_out> yGm_;
72
+
73
+ /* ascendc variable */
74
+ TPipe* pipe_ = nullptr;
75
+ TQue<QuePosition::VECIN, 1> xQueue_;
76
+ TQue<QuePosition::VECOUT, 1> yQueue_;
77
+
78
+ TBuf<> tmpLocalBuffer_;
79
+
80
+ int64_t blockA_ = 0; // 获取分块操作中的单个块的大小
81
+ const SoftmaxV2ARTilingData* tl_ = nullptr;
82
+ };
83
+
84
+ template <typename T_in, typename T_out>
85
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::Init(GM_ADDR x, GM_ADDR y, const SoftmaxV2ARTilingData* tilingData)
86
+ {
87
+ this->tl_ = tilingData;
88
+ // GM not need align.
89
+ // 获取分块操作中的单个块的大小。判断是否是最后一块,是最后一块,则等于剩余元素的数量,否则等于固定的单核处理的行数
90
+ this->blockA_ = (AscendC::GetBlockIdx() == AscendC::GetBlockNum() - 1)
91
+ ? (tl_->a - tl_->aBlockFactor * (AscendC::GetBlockNum() - 1))
92
+ : tl_->aBlockFactor;
93
+ int64_t aGmOffset = tl_->aBlockFactor * AscendC::GetBlockIdx() * tl_->r;
94
+ // 初始化GM Tensor
95
+ xGm_.SetGlobalBuffer((__gm__ T_in*)x + aGmOffset);
96
+ yGm_.SetGlobalBuffer((__gm__ T_out*)y + aGmOffset);
97
+
98
+ // 初始化Pipe
99
+ int64_t ubBufferSize = tl_->ubFactor * tl_->rAligned;
100
+ pipe_->InitBuffer(this->xQueue_, DOUBLE_BUFFER, ubBufferSize * sizeof(T_in));
101
+ pipe_->InitBuffer(this->yQueue_, DOUBLE_BUFFER, ubBufferSize * sizeof(T_out));
102
+ pipe_->InitBuffer(this->tmpLocalBuffer_, ubBufferSize * sizeof(float) + AR_FULL_LOAD_BINARY_TMP_BYTES);
103
+ }
104
+
105
+ template <typename T_in, typename T_out>
106
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::Process()
107
+ {
108
+ // ubLoop: 表示需要多少个子块来覆盖singleA大小的数据
109
+ int64_t ubLoop = ops::CeilDiv(this->blockA_, tl_->ubFactor);
110
+ int64_t lastUbFactor = this->blockA_ - tl_->ubFactor * (ubLoop - 1);
111
+ // 循环处理每个子块
112
+ for (int64_t ubLoopIdx = 0; ubLoopIdx < ubLoop; ubLoopIdx++) {
113
+ // aOffset:计算当前子块的偏移量
114
+ int64_t aOffset = ubLoopIdx * tl_->ubFactor * tl_->r;
115
+ int64_t ubA = (ubLoopIdx == (ubLoop - 1)) ? lastUbFactor : tl_->ubFactor;
116
+ ProcessUB(ubA, aOffset);
117
+ }
118
+ }
119
+
120
+ template <typename T_in, typename T_out>
121
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::ProcessUB(int64_t ubA, int64_t aOffset)
122
+ {
123
+ LocalTensor<T_in> xInUb = xQueue_.AllocTensor<T_in>();
124
+ CopyInX(xInUb, ubA, aOffset);
125
+ xQueue_.EnQue(xInUb);
126
+ xInUb = xQueue_.DeQue<T_in>();
127
+
128
+ LocalTensor<float> tmpLocalTensor = tmpLocalBuffer_.AllocTensor<float>();
129
+ LocalTensor<float> binaryTmpLocalTensor = tmpLocalTensor[tl_->ubFactor * tl_->rAligned];
130
+ LocalTensor<float> xTmpLocalTensor = tmpLocalTensor[0];
131
+
132
+ __local_mem__ T_in* xInUbAddr = (__local_mem__ T_in*)xInUb.GetPhyAddr();
133
+ __local_mem__ float* xTmpLocalAddr = (__local_mem__ float*)xTmpLocalTensor.GetPhyAddr();
134
+ __local_mem__ float* binaryTmpLocalAddr = (__local_mem__ float*)binaryTmpLocalTensor.GetPhyAddr();
135
+
136
+ FirstNormCompute(ubA, xInUbAddr, xTmpLocalAddr);
137
+ xQueue_.FreeTensor<T_in>(xInUb);
138
+ LocalTensor<T_out> yInUb = yQueue_.AllocTensor<T_out>();
139
+ SecondNormCompute(yInUb, xTmpLocalTensor, binaryTmpLocalTensor, ubA, tl_->r, tl_->rAligned);
140
+ yQueue_.EnQue(yInUb);
141
+
142
+ LocalTensor<T_out> yOutUb = yQueue_.DeQue<T_out>();
143
+ CopyOutY(yOutUb, ubA, aOffset);
144
+ yQueue_.FreeTensor(yOutUb);
145
+ }
146
+
147
+ template <typename T_in, typename T_out>
148
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::FirstNormCompute(int64_t ubA, __local_mem__ T_in* xInAddr,
149
+ __local_mem__ float* xTmpLocalAddr)
150
+ {
151
+ int64_t rAligned = tl_->rAligned;
152
+ int64_t ubActualR = tl_->r;
153
+ uint16_t ubActualA = static_cast<uint16_t>(ubA);
154
+ int64_t tailUbBlockSize = tl_->r - VL_FP32 * (tl_->rLoopCount - 1);
155
+ uint16_t rLoopCount = static_cast<uint16_t>(tl_->rLoopCount);
156
+ uint16_t rLoopCountTmp = rLoopCount - 1;
157
+
158
+ __VEC_SCOPE__
159
+ {
160
+ AscendC::MicroAPI::RegTensor<float> vreg0;
161
+ AscendC::MicroAPI::RegTensor<float> vreg1;
162
+ AscendC::MicroAPI::RegTensor<float> vreg2;
163
+ AscendC::MicroAPI::RegTensor<float> vreg3;
164
+ AscendC::MicroAPI::RegTensor<float> vreg4;
165
+ AscendC::MicroAPI::RegTensor<float> vreg5;
166
+
167
+ AscendC::MicroAPI::MaskReg maskTail;
168
+ AscendC::MicroAPI::MaskReg maskSub;
169
+ AscendC::MicroAPI::MaskReg maskMax;
170
+ AscendC::MicroAPI::MaskReg maskAll;
171
+
172
+ uint32_t sreg0 = tailUbBlockSize;
173
+ uint32_t sreg1 = VL_FP32;
174
+ maskTail = AscendC::MicroAPI::UpdateMask<uint32_t>(sreg0);
175
+ maskAll = AscendC::MicroAPI::UpdateMask<uint32_t>(sreg1);
176
+
177
+ for (uint16_t k = 0; k < ubActualA; k++) {
178
+ uint32_t tailAddrPtr = k * rAligned + VL_FP32 * (rLoopCount - 1);
179
+ AscendC::MicroAPI::Duplicate(vreg0, static_cast<float>(-INFINITY), maskAll);
180
+ LoadTensorForDtypeTIn(xInAddr, vreg1, maskTail, tailAddrPtr);
181
+ AscendC::MicroAPI::Max(vreg1, vreg0, vreg1, maskTail);
182
+ AscendC::MicroAPI::Copy<float, AscendC::MicroAPI::MaskMergeMode::MERGING>(vreg0, vreg1, maskTail);
183
+
184
+ uint32_t sreg2 = static_cast<uint32_t>(ubActualR - tailUbBlockSize);
185
+ for (uint16_t i = 0; i < rLoopCountTmp; i++) {
186
+ maskMax = AscendC::MicroAPI::UpdateMask<float>(sreg2);
187
+ uint32_t addrPtr = i * VL_FP32 + k * rAligned;
188
+ LoadTensorForDtypeTIn(xInAddr, vreg1, maskMax, addrPtr);
189
+ AscendC::MicroAPI::Max(vreg0, vreg0, vreg1, maskMax);
190
+ }
191
+ AscendC::MicroAPI::ReduceMax(vreg2, vreg0, maskAll);
192
+ AscendC::MicroAPI::Duplicate(vreg3, vreg2, maskAll);
193
+
194
+ // 求sub和exp
195
+ uint32_t sreg3 = ubActualR;
196
+ for (uint16_t i = 0; i < rLoopCount; i++) {
197
+ maskSub = AscendC::MicroAPI::UpdateMask<float>(sreg3);
198
+ uint32_t addrPtr = i * VL_FP32 + k * rAligned;
199
+ LoadTensorForDtypeTIn(xInAddr, vreg1, maskSub, addrPtr);
200
+ AscendC::MicroAPI::Sub(vreg4, vreg1, vreg3, maskSub);
201
+ AscendC::MicroAPI::Exp(vreg5, vreg4, maskSub);
202
+ AscendC::MicroAPI::DataCopy(((__local_mem__ float*)xTmpLocalAddr + addrPtr), vreg5, maskSub);
203
+ }
204
+ }
205
+ }
206
+ }
207
+
208
+ template <typename T_in, typename T_out>
209
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::SecondNormComputePost(const LocalTensor<T_out>& dstTensor,
210
+ const LocalTensor<float>& srcTensor,
211
+ const LocalTensor<float>& oriSrcTensor,
212
+ const int64_t aSize, const int64_t rSize,
213
+ const int64_t stride)
214
+ {
215
+ if (aSize <= 0) {
216
+ return;
217
+ }
218
+ if (rSize <= 0) {
219
+ return;
220
+ }
221
+ if (rSize > CONST_TWO * VL_FP32) {
222
+ return;
223
+ }
224
+
225
+ uint16_t loopTimes = aSize;
226
+ uint16_t rLoopCount = tl_->rLoopCount;
227
+ uint16_t oriR = tl_->r;
228
+ uint16_t oriRAligned = tl_->rAligned;
229
+
230
+ if (rSize <= VL_FP32) {
231
+ __local_mem__ T_out* dst = (__local_mem__ T_out*)dstTensor.GetPhyAddr();
232
+ __local_mem__ float* src = (__local_mem__ float*)srcTensor.GetPhyAddr();
233
+ __local_mem__ float* oriSrc = (__local_mem__ float*)oriSrcTensor.GetPhyAddr();
234
+
235
+ __VEC_SCOPE__
236
+ {
237
+ uint32_t count = static_cast<uint32_t>(rSize);
238
+ AscendC::MicroAPI::RegTensor<float> aReg, bReg, cReg, dReg;
239
+ AscendC::MicroAPI::MaskReg pMask = AscendC::MicroAPI::UpdateMask<float>(count);
240
+ AscendC::MicroAPI::MaskReg pFull =
241
+ AscendC::MicroAPI::CreateMask<float, AscendC::MicroAPI::MaskPattern::ALL>();
242
+ AscendC::MicroAPI::MaskReg maskOri;
243
+ for (uint16_t i = 0; i < loopTimes; ++i) {
244
+ DataCopy(aReg, (__local_mem__ float*)src + i * static_cast<uint32_t>(stride));
245
+ ReduceSum(bReg, aReg, pMask);
246
+ Duplicate(cReg, bReg, pFull);
247
+ uint32_t sreg0 = static_cast<uint32_t>(oriR);
248
+ for (uint16_t j = 0; j < rLoopCount; ++j) {
249
+ maskOri = AscendC::MicroAPI::UpdateMask<float>(sreg0);
250
+ uint32_t addrPtr = j * VL_FP32 + i * oriRAligned;
251
+ DataCopy(dReg, ((__local_mem__ float*)oriSrc + addrPtr));
252
+ Div(dReg, dReg, cReg, maskOri);
253
+ StoreTensorForDtypeTOut(dst, dReg, maskOri, addrPtr);
254
+ }
255
+ }
256
+ }
257
+ } else {
258
+ __local_mem__ T_out* dst = (__local_mem__ T_out*)dstTensor.GetPhyAddr();
259
+ __local_mem__ float* src0 = (__local_mem__ float*)srcTensor.GetPhyAddr();
260
+ __local_mem__ float* src1 = (__local_mem__ float*)srcTensor.GetPhyAddr() + VL_FP32;
261
+ __local_mem__ float* oriSrc = (__local_mem__ float*)oriSrcTensor.GetPhyAddr();
262
+
263
+ __VEC_SCOPE__
264
+ {
265
+ uint32_t count = static_cast<uint32_t>(rSize - VL_FP32);
266
+ AscendC::MicroAPI::RegTensor<float> aReg, bReg, cReg, dReg, eReg;
267
+ AscendC::MicroAPI::MaskReg pMask = AscendC::MicroAPI::UpdateMask<float>(count);
268
+ AscendC::MicroAPI::MaskReg pFull =
269
+ AscendC::MicroAPI::CreateMask<float, AscendC::MicroAPI::MaskPattern::ALL>();
270
+ AscendC::MicroAPI::MaskReg maskOri;
271
+ for (uint16_t i = 0; i < loopTimes; ++i) {
272
+ DataCopy(aReg, (__local_mem__ float*)src0 + i * static_cast<uint32_t>(stride));
273
+ DataCopy(bReg, (__local_mem__ float*)src1 + i * static_cast<uint32_t>(stride));
274
+ Add<float, AscendC::MicroAPI::MaskMergeMode::ZEROING>(cReg, aReg, bReg, pMask);
275
+ Copy<float, AscendC::MicroAPI::MaskMergeMode::MERGING>(aReg, cReg, pMask);
276
+ ReduceSum(bReg, aReg, pFull);
277
+ Duplicate(dReg, bReg, pFull);
278
+ uint32_t sreg0 = static_cast<uint32_t>(oriR);
279
+ for (uint16_t j = 0; j < rLoopCount; ++j) {
280
+ maskOri = AscendC::MicroAPI::UpdateMask<float>(sreg0);
281
+ uint32_t addrPtr = j * VL_FP32 + i * oriRAligned;
282
+ DataCopy(eReg, ((__local_mem__ float*)oriSrc + addrPtr));
283
+ Div(eReg, eReg, dReg, maskOri);
284
+ StoreTensorForDtypeTOut(dst, eReg, maskOri, addrPtr);
285
+ }
286
+ }
287
+ }
288
+ }
289
+ }
290
+
291
+ template <typename T_in, typename T_out>
292
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::SecondNormCompute(const LocalTensor<T_out>& dstTensor,
293
+ const LocalTensor<float>& srcTensor,
294
+ const LocalTensor<float>& reduceSumTempTensor,
295
+ const int64_t aSize, const int64_t rSize,
296
+ const int64_t stride)
297
+ {
298
+ if (aSize <= 0) {
299
+ return;
300
+ }
301
+ if (rSize <= 0) {
302
+ return;
303
+ }
304
+ if (rSize <= CONST_TWO * VL_FP32) {
305
+ SecondNormComputePost(dstTensor, srcTensor, srcTensor, aSize, rSize, stride);
306
+ return;
307
+ }
308
+
309
+ int64_t ceilVLCount =
310
+ ops::CeilDiv(static_cast<int64_t>(rSize * sizeof(float)), static_cast<int64_t>(GetVRegSize()));
311
+ int64_t floorVLCount =
312
+ ops::FloorDiv(static_cast<int64_t>(rSize * sizeof(float)), static_cast<int64_t>(GetVRegSize()));
313
+ int64_t foldPoint = FindNearestPower2(ceilVLCount);
314
+
315
+ uint16_t outerLoopTimes = aSize;
316
+ uint16_t tailFoldLoopTimes = ceilVLCount - floorVLCount;
317
+ uint32_t tailFoldElemCount = static_cast<uint32_t>(rSize - floorVLCount * VL_FP32);
318
+ uint16_t mainFoldLoopTimes = floorVLCount - foldPoint;
319
+ uint16_t unFoldLoopTimes = foldPoint + foldPoint - ceilVLCount;
320
+ uint32_t outerLoopStride = stride;
321
+ uint32_t innerLoopStride = VL_FP32;
322
+ uint32_t outerLoopDstStride =
323
+ ops::Aligned(static_cast<int64_t>(foldPoint), static_cast<int64_t>(GetUbBlockSize() / sizeof(float)));
324
+
325
+ int64_t foldSrcBOffset = foldPoint * VL_FP32;
326
+ int64_t tailSrcAOffset = mainFoldLoopTimes * VL_FP32;
327
+ int64_t tailSrcBOffset = floorVLCount * VL_FP32;
328
+ int64_t unFoldSrcOffset = (mainFoldLoopTimes + tailFoldLoopTimes) * VL_FP32;
329
+
330
+ __local_mem__ float* dst = (__local_mem__ float*)reduceSumTempTensor.GetPhyAddr();
331
+ __local_mem__ float* foldSrcA = (__local_mem__ float*)srcTensor.GetPhyAddr();
332
+ __local_mem__ float* foldSrcB = (__local_mem__ float*)srcTensor.GetPhyAddr() + foldSrcBOffset;
333
+ __local_mem__ float* tailSrcA = (__local_mem__ float*)srcTensor.GetPhyAddr() + tailSrcAOffset;
334
+ __local_mem__ float* tailSrcB = (__local_mem__ float*)srcTensor.GetPhyAddr() + tailSrcBOffset;
335
+ __local_mem__ float* unFoldSrc = (__local_mem__ float*)srcTensor.GetPhyAddr() + unFoldSrcOffset;
336
+
337
+ __VEC_SCOPE__
338
+ {
339
+ AscendC::MicroAPI::MaskReg pFull = AscendC::MicroAPI::CreateMask<float, AscendC::MicroAPI::MaskPattern::ALL>();
340
+ AscendC::MicroAPI::UnalignReg UReg;
341
+
342
+ for (uint16_t i = 0; i < outerLoopTimes; ++i) {
343
+ dst = (__local_mem__ float*)reduceSumTempTensor.GetPhyAddr() + i * outerLoopDstStride;
344
+ for (uint16_t j = 0; j < mainFoldLoopTimes; ++j) {
345
+ AscendC::MicroAPI::RegTensor<float> aReg, bReg, cReg, dReg;
346
+ DataCopy(aReg, (__local_mem__ float*)foldSrcA + i * outerLoopStride + j * innerLoopStride);
347
+ DataCopy(bReg, (__local_mem__ float*)foldSrcB + i * outerLoopStride + j * innerLoopStride);
348
+ Add<float, AscendC::MicroAPI::MaskMergeMode::ZEROING>(cReg, aReg, bReg, pFull);
349
+ ReduceSum(dReg, cReg, pFull);
350
+ AscendC::MicroAPI::DataCopyUnAlign((__local_mem__ float*&)dst, dReg, UReg, 1);
351
+ }
352
+ for (uint16_t j = 0; j < tailFoldLoopTimes; ++j) {
353
+ uint32_t count = static_cast<uint32_t>(tailFoldElemCount);
354
+ AscendC::MicroAPI::RegTensor<float> aReg, bReg, cReg;
355
+ AscendC::MicroAPI::MaskReg pMask = AscendC::MicroAPI::UpdateMask<float>(count);
356
+ DataCopy(aReg, (__local_mem__ float*)tailSrcA + i * outerLoopStride + j * innerLoopStride);
357
+ DataCopy(bReg, (__local_mem__ float*)tailSrcB + i * outerLoopStride + j * innerLoopStride);
358
+ Add<float, AscendC::MicroAPI::MaskMergeMode::ZEROING>(cReg, aReg, bReg, pMask);
359
+ Copy<float, AscendC::MicroAPI::MaskMergeMode::MERGING>(aReg, cReg, pMask);
360
+ ReduceSum(bReg, aReg, pFull);
361
+ AscendC::MicroAPI::DataCopyUnAlign((__local_mem__ float*&)dst, bReg, UReg, 1);
362
+ }
363
+ for (uint16_t j = 0; j < unFoldLoopTimes; ++j) {
364
+ AscendC::MicroAPI::RegTensor<float> aReg, bReg;
365
+ DataCopy(aReg, (__local_mem__ float*)unFoldSrc + i * outerLoopStride + j * innerLoopStride);
366
+ ReduceSum(bReg, aReg, pFull);
367
+ AscendC::MicroAPI::DataCopyUnAlign((__local_mem__ float*&)dst, bReg, UReg, 1);
368
+ }
369
+ AscendC::MicroAPI::DataCopyUnAlignPost((__local_mem__ float*&)dst, UReg, 0);
370
+ }
371
+ }
372
+ SecondNormComputePost(dstTensor, reduceSumTempTensor, srcTensor, aSize, foldPoint, outerLoopDstStride);
373
+ }
374
+
375
+ template <typename T_in, typename T_out>
376
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::LoadTensorForDtypeTIn(__local_mem__ T_in* src,
377
+ AscendC::MicroAPI::RegTensor<float>& dst,
378
+ AscendC::MicroAPI::MaskReg& preg,
379
+ uint32_t offset)
380
+ {
381
+ if constexpr (IsSameType<T_in, float>::value) {
382
+ DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_NORM>(dst, src + offset);
383
+ } else {
384
+ AscendC::MicroAPI::RegTensor<T_in> xFp16;
385
+ DataCopy<T_in, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>(xFp16, src + offset);
386
+ Cast<float, T_in, castTraitFp16ToFp32>(dst, xFp16, preg);
387
+ }
388
+ }
389
+
390
+ template <typename T_in, typename T_out>
391
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::StoreTensorForDtypeTOut(__local_mem__ T_out* dst,
392
+ AscendC::MicroAPI::RegTensor<float>& src,
393
+ AscendC::MicroAPI::MaskReg& preg,
394
+ uint32_t offset)
395
+ {
396
+ if constexpr (IsSameType<T_out, float>::value) {
397
+ DataCopy<T_out, AscendC::MicroAPI::StoreDist::DIST_NORM>(dst + offset, src, preg);
398
+ } else {
399
+ AscendC::MicroAPI::RegTensor<T_out> xFp16;
400
+ Cast<T_out, float, castTraitFp32ToFp16>(xFp16, src, preg);
401
+ DataCopy<T_out, AscendC::MicroAPI::StoreDist::DIST_PACK_B32>(dst + offset, xFp16, preg);
402
+ }
403
+ }
404
+
405
+ template <typename T_in, typename T_out>
406
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::CopyInX(const LocalTensor<T_in>& xInUb, int64_t ubA, int64_t offset)
407
+ {
408
+ DataCopyPadParams padParams{false, 0, 0, 0};
409
+ DataCopyParams copyInParams;
410
+ copyInParams.blockCount = ubA;
411
+ copyInParams.blockLen = tl_->r * sizeof(T_in);
412
+ copyInParams.srcStride = 0;
413
+ copyInParams.dstStride = (tl_->rAligned - tl_->r) * sizeof(T_in) / BLOCK_SIZE;
414
+ DataCopyPad(xInUb, xGm_[offset], copyInParams, padParams);
415
+ }
416
+
417
+ template <typename T_in, typename T_out>
418
+ __aicore__ inline void SoftmaxV2AR<T_in, T_out>::CopyOutY(const LocalTensor<T_out>& yOutUb, int64_t ubA, int64_t offset)
419
+ {
420
+ DataCopyParams copyOutParams;
421
+ copyOutParams.blockCount = ubA;
422
+ copyOutParams.blockLen = tl_->r * sizeof(T_out);
423
+ copyOutParams.srcStride = (tl_->rAligned - tl_->r) * sizeof(T_out) / BLOCK_SIZE;
424
+ copyOutParams.dstStride = 0;
425
+ DataCopyPad(yGm_[offset], yOutUb, copyOutParams);
426
+ }
427
+
428
+ } // namespace SoftmaxV2Ops
429
+ #endif // SOFTMAX_V2_AR_FULL_LOAD_H