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.
- package/NOTICE.md +14 -0
- package/README.md +39 -2
- package/cordis.patch.yml +20 -0
- package/lib/client.js +4253 -0
- package/lib/host.js +2518 -0
- package/package.json +53 -4
- package/skills/tool-ops-tune/INSTALL.md +52 -0
- package/skills/tool-ops-tune/README.md +167 -0
- package/skills/tool-ops-tune/SKILL.md +93 -0
- package/skills/tool-ops-tune/agent/AGENTS.md +195 -0
- package/skills/tool-ops-tune/agent/agents/perf-implementer.md +68 -0
- package/skills/tool-ops-tune/agent/agents/perf-reviewer.md +86 -0
- package/skills/tool-ops-tune/agent/workflows/task-prompts.md +362 -0
- package/skills/tool-ops-tune/agent/workflows/troubleshooting.md +43 -0
- package/skills/tool-ops-tune/docs/figures/opstune-architecture.png +0 -0
- package/skills/tool-ops-tune/docs/figures/opstune-architecture.svg +253 -0
- package/skills/tool-ops-tune/docs/ops-tune-design.md +404 -0
- package/skills/tool-ops-tune/docs//347/224/250/344/276/213/346/236/204/345/273/272.md +714 -0
- package/skills/tool-ops-tune/docs//347/237/245/350/257/206/345/272/223/350/247/204/350/214/203.md +103 -0
- package/skills/tool-ops-tune/evals/evals.json +242 -0
- package/skills/tool-ops-tune/ops-collect/SKILL.md +146 -0
- package/skills/tool-ops-tune/ops-collect/references/tool-routing.md +86 -0
- package/skills/tool-ops-tune/ops-collect/scripts/collect_board.sh +444 -0
- package/skills/tool-ops-tune/ops-collect/scripts/collect_cases.py +368 -0
- package/skills/tool-ops-tune/ops-collect/scripts/collect_sim.sh +134 -0
- package/skills/tool-ops-tune/ops-collect/scripts/csv_to_db.py +314 -0
- package/skills/tool-ops-tune/ops-collect/scripts/db_bench.py +545 -0
- package/skills/tool-ops-tune/ops-collect/scripts/facts_bridge.py +1063 -0
- package/skills/tool-ops-tune/ops-collect/scripts/isolation_test.py +454 -0
- package/skills/tool-ops-tune/ops-collect/scripts/jsonl_to_cases.py +163 -0
- package/skills/tool-ops-tune/ops-family/SKILL.md +76 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/advanced_tiling.md +92 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/broadcast_design.md +160 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_add_kernel.cpp +126 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_add_tiling.cpp +144 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/code/broadcast_tiling_common.h +401 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/code/onedim_add_kernel.cpp +107 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/code/onedim_add_tiling.cpp +64 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/datacopypad_design.md +46 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/guide.md +14 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/nddma_design.md +53 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/onedim_design.md +114 -0
- package/skills/tool-ops-tune/ops-family/references/broadcast/ub_broadcast_design.md +67 -0
- package/skills/tool-ops-tune/ops-family/references/common/datacopy_optimization_design.md +351 -0
- package/skills/tool-ops-tune/ops-family/references/common/index.md +9 -0
- package/skills/tool-ops-tune/ops-family/references/common/tail_block_design.md +268 -0
- package/skills/tool-ops-tune/ops-family/references/common/timing_harness/index.md +38 -0
- package/skills/tool-ops-tune/ops-family/references/common/timing_harness/timing_harness_aclrt_event.cpp +199 -0
- package/skills/tool-ops-tune/ops-family/references/common/timing_harness/timing_harness_torch_event.py +130 -0
- package/skills/tool-ops-tune/ops-family/references/common/ub_resident_design.md +342 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/guide.md +69 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/index.md +4 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_base.template +81 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_big_dim.template +217 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_cut_one_axis.template +336 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_cut_two_axis.template +781 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_n_last.template +340 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_small_shape.template +321 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_tensor_move.template +102 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_transdata_5hd.template +480 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_transdata_5hd_021axis.template +533 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/dav3510/transpose_with_gather.template +688 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/templates/transpose_tiling_data.template +192 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_big_dim.md +220 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_cut_one_axis.md +175 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_cut_two_axis.md +177 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_fusion_design.md +398 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_gather.md +191 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_n_last.md +276 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_small_shape.md +315 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_tensor_move.md +203 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_tiling_data.md +176 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_vconv_021.md +206 -0
- package/skills/tool-ops-tune/ops-family/references/conversion/transpose_vconv_5hd.md +179 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/double_buffer_design.md +288 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/guide.md +46 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/index.md +10 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/CMakeLists.txt +131 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/README.md +55 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/add_custom_torch.cpp +80 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/ops.h +23 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_extension/register.cpp +30 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_host/add_custom.asc +149 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_host/data_utils.h +97 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_kernel/add_custom_kernel.asc +136 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/op_kernel/add_custom_tiling.h +31 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/run.sh +86 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/gen_data.py +70 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/golden.py +34 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/test_torch.py +99 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/add_custom/scripts/verify_result.py +56 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/templates/usage_guide.md +61 -0
- package/skills/tool-ops-tune/ops-family/references/elementwise/vector_efficiency_design.md +203 -0
- package/skills/tool-ops-tune/ops-family/references/fa/fa_mixed_kernel_design.md +174 -0
- package/skills/tool-ops-tune/ops-family/references/fa/fa_roofline_design.md +141 -0
- package/skills/tool-ops-tune/ops-family/references/fa/guide.md +87 -0
- package/skills/tool-ops-tune/ops-family/references/knowledge_gaps.json +56 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/best-practices-guide.md +14 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/fullload_design.md +344 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/guide.md +52 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/mte2_preload_design.md +320 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/pingpong_design.md +254 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/scale_coalescing_design.md +282 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/streamk_design.md +374 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/swat_design.md +423 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/templates/matmul_basic_blaze.template +165 -0
- package/skills/tool-ops-tune/ops-family/references/matmul/templates/usage_guide.md +29 -0
- package/skills/tool-ops-tune/ops-family/references/mc2/best-practices-guide.md +43 -0
- package/skills/tool-ops-tune/ops-family/references/mc2/guide.md +42 -0
- package/skills/tool-ops-tune/ops-family/references/mc2/local_matmul_design.md +130 -0
- package/skills/tool-ops-tune/ops-family/references/mc2/pipeline_balancing_design.md +502 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/guide.md +178 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/kernel_utils.template +75 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_full_load.template +429 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_recompute.template +485 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ar_small_r.template +348 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_full_load.template +574 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_online.template +324 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_ara_recompute.template +436 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/dav310/softmax_v2_base.template +950 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/fused_attention_online_softmax_design.md +428 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_full_load.md +137 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_recompute.md +135 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ar_small_r.md +122 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_full_load.md +144 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_online.md +185 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_ara_recompute.md +163 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_tiling.template +460 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/softmax_v2_tiling_data.template +278 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/state_resident_design.md +337 -0
- package/skills/tool-ops-tune/ops-family/references/reduce/templates/usage_guide.md +253 -0
- package/skills/tool-ops-tune/ops-family/references/scalar/coding_principles.md +476 -0
- package/skills/tool-ops-tune/ops-family/references/scalar/conv_scalar.md +163 -0
- package/skills/tool-ops-tune/ops-family/references/scalar/guide.md +30 -0
- package/skills/tool-ops-tune/ops-family/references/simt/guide.md +91 -0
- package/skills/tool-ops-tune/ops-family/references/simt/optimization-guide.md +110 -0
- package/skills/tool-ops-tune/ops-family/references/sort/guide.md +83 -0
- package/skills/tool-ops-tune/ops-family/references/sort/radix_sort.md +354 -0
- package/skills/tool-ops-tune/ops-family/scripts/family_asset_audit.py +68 -0
- package/skills/tool-ops-tune/ops-family/scripts/tiling_candidates.py +419 -0
- package/skills/tool-ops-tune/ops-mechanism/SKILL.md +23 -0
- package/skills/tool-ops-tune/ops-mechanism/cases/ascendc/case_groupedmatmul_pipeline.md +23 -0
- package/skills/tool-ops-tune/ops-mechanism/cases/ascendc/case_triton_atomic_revert.md +19 -0
- package/skills/tool-ops-tune/ops-mechanism/cases/mc2/case_mc2_fusion.md +19 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/api-usage.md +70 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/ascendc-regbase-vf.md +45 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/ascendc-tiling.md +51 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/data-copy.md +67 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/index.md +45 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/mc2-balancing.md +77 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/mc2.md +103 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/memory-hierarchy.md +40 -0
- package/skills/tool-ops-tune/ops-mechanism/references/optimize/pipeline.md +95 -0
- package/skills/tool-ops-tune/ops-mechanism/scripts/case_backfill.py +170 -0
- package/skills/tool-ops-tune/ops-mechanism/scripts/log_gap.py +145 -0
- package/skills/tool-ops-tune/ops-mechanism/scripts/mc2_search.py +367 -0
- package/skills/tool-ops-tune/ops-report/SKILL.md +77 -0
- package/skills/tool-ops-tune/ops-verdict/evals/run_evals.py +837 -0
- package/skills/tool-ops-tune/ops-verdict/evals/smoke/install-hooks.sh +65 -0
- package/skills/tool-ops-tune/ops-verdict/evals/smoke/run_smoke.sh +405 -0
- package/skills/tool-ops-tune/ops-verdict/fixtures/core_tail_a5.csv +25 -0
- package/skills/tool-ops-tune/ops-verdict/fixtures/few_cores_a5.csv +5 -0
- package/skills/tool-ops-tune/ops-verdict/fixtures/mte2_bound_a2.csv +17 -0
- package/skills/tool-ops-tune/ops-verdict/fixtures/multi_kernel_mix_a5.csv +7 -0
- package/skills/tool-ops-tune/ops-verdict/fixtures/no_bound_a5.csv +9 -0
- package/skills/tool-ops-tune/ops-verdict/fixtures/serial_no_overlap_a2.csv +9 -0
- package/skills/tool-ops-tune/ops-verdict/fixtures/vec_bound_a5.csv +9 -0
- package/skills/tool-ops-tune/ops-verdict/references/io-contracts.md +94 -0
- package/skills/tool-ops-tune/ops-verdict/references/rule-schema.md +130 -0
- package/skills/tool-ops-tune/ops-verdict/references/scope-schema.md +61 -0
- package/skills/tool-ops-tune/ops-verdict/references/soc-spec.yaml +140 -0
- package/skills/tool-ops-tune/ops-verdict/rules/L0-instruction.yaml +67 -0
- package/skills/tool-ops-tune/ops-verdict/rules/L1-single-core.yaml +316 -0
- package/skills/tool-ops-tune/ops-verdict/rules/L2-inter-core.yaml +173 -0
- package/skills/tool-ops-tune/ops-verdict/rules/L3-inter-card.yaml +156 -0
- package/skills/tool-ops-tune/ops-verdict/scripts/batch_pipeline.py +444 -0
- package/skills/tool-ops-tune/ops-verdict/scripts/calibrate.py +249 -0
- package/skills/tool-ops-tune/ops-verdict/scripts/delta_compare.py +244 -0
- package/skills/tool-ops-tune/ops-verdict/scripts/render_html.py +274 -0
- package/skills/tool-ops-tune/ops-verdict/scripts/snapshot.py +200 -0
- package/skills/tool-ops-tune/ops-verdict/scripts/trace_bubble.py +331 -0
- package/skills/tool-ops-tune/ops-verdict/scripts/verdict.py +1072 -0
- package/skills/tool-ops-tune/scripts/diff_audit.py +281 -0
|
@@ -0,0 +1,485 @@
|
|
|
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_recompute.h
|
|
13
|
+
* \brief
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
#ifndef SOFTMAX_V2_AR_RECOMPUTE_H
|
|
17
|
+
#define SOFTMAX_V2_AR_RECOMPUTE_H
|
|
18
|
+
|
|
19
|
+
#include "kernel_tiling/kernel_tiling.h"
|
|
20
|
+
#include "kernel_operator.h"
|
|
21
|
+
|
|
22
|
+
#include "kernel_utils.h"
|
|
23
|
+
#include "softmax_v2_base.h"
|
|
24
|
+
|
|
25
|
+
namespace SoftmaxV2Ops
|
|
26
|
+
{
|
|
27
|
+
using namespace AscendC;
|
|
28
|
+
constexpr int64_t AR_RECOMPUTE_MAX_BUFFER_BTYES = 32;
|
|
29
|
+
constexpr int64_t AR_RECOMPUTE_SUM_BUFFER_BTYES = 32;
|
|
30
|
+
constexpr int64_t AR_RECOMPUTE_BINARY_CACHE_BTYES = 2048;
|
|
31
|
+
constexpr int64_t AR_RECOMPUTE_SUM_LEN = AR_RECOMPUTE_SUM_BUFFER_BTYES / sizeof(float);
|
|
32
|
+
constexpr static uint32_t TRIPLE_BUFFER = 3;
|
|
33
|
+
constexpr static float CONST_FP32_MIN = -(__builtin_inff());
|
|
34
|
+
constexpr int64_t A_IN_IN = 1;
|
|
35
|
+
|
|
36
|
+
template <typename Tx, typename Ty>
|
|
37
|
+
class SoftmaxV2ArRecompute : public SoftmaxV2OpsBase
|
|
38
|
+
{
|
|
39
|
+
public:
|
|
40
|
+
__aicore__ inline SoftmaxV2ArRecompute(TPipe* pipe)
|
|
41
|
+
{
|
|
42
|
+
pipe_ = pipe;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
__aicore__ inline void Init(GM_ADDR x, GM_ADDR y, const SoftmaxV2ArRecomputeTilingData* tilingData);
|
|
46
|
+
__aicore__ inline void Process();
|
|
47
|
+
|
|
48
|
+
private:
|
|
49
|
+
__aicore__ inline void CalculateMaxVF(__local_mem__ float*& xMaxPtr, __local_mem__ Tx*& xPtr, uint32_t aSize,
|
|
50
|
+
uint32_t ubFactor);
|
|
51
|
+
__aicore__ inline void CalculateOutVF(__local_mem__ Ty*& yPtr, __local_mem__ Tx*& xPtr,
|
|
52
|
+
__local_mem__ float*& xMaxPtr, __local_mem__ float*& xSumPtr, uint32_t a,
|
|
53
|
+
uint32_t ubFactor);
|
|
54
|
+
__aicore__ inline void MainBlockCastSubExpVF(__local_mem__ float*& xFp32Ptr, __local_mem__ Tx*& xPtr,
|
|
55
|
+
__local_mem__ float*& xMaxPtr, uint32_t a, uint32_t ubFactor);
|
|
56
|
+
__aicore__ inline void FoldBlockCastSubExpVF(__local_mem__ float*& dstPtr, __local_mem__ Tx*& xPtr,
|
|
57
|
+
__local_mem__ float*& xMaxPtr, uint32_t a, uint32_t ubFactor);
|
|
58
|
+
__aicore__ inline int64_t GetCacheId(const int64_t idx);
|
|
59
|
+
__aicore__ inline void UpdateCache(const LocalTensor<float>& dstTensor, const LocalTensor<float>& srcTensor,
|
|
60
|
+
const int64_t cacheId, const int64_t stride, const int64_t count);
|
|
61
|
+
|
|
62
|
+
protected:
|
|
63
|
+
GlobalTensor<Tx> xGm_;
|
|
64
|
+
GlobalTensor<Ty> yGm_;
|
|
65
|
+
|
|
66
|
+
const SoftmaxV2ArRecomputeTilingData* tl_ = nullptr;
|
|
67
|
+
TPipe* pipe_ = nullptr;
|
|
68
|
+
TQue<QuePosition::VECIN, 1> xQueue_;
|
|
69
|
+
TQue<QuePosition::VECOUT, 1> yQueue_;
|
|
70
|
+
|
|
71
|
+
TBuf<> xTmpBuffer;
|
|
72
|
+
TBuf<> xMaxBuffer;
|
|
73
|
+
TBuf<> xSumBuffer;
|
|
74
|
+
TBuf<> cachebuffer;
|
|
75
|
+
|
|
76
|
+
uint32_t blockIdx_ = GetBlockIdx();
|
|
77
|
+
uint64_t currentRowBlock_ = 0;
|
|
78
|
+
uint32_t resultCacheID_ = 0;
|
|
79
|
+
|
|
80
|
+
LocalTensor<float> totalSumLocal_;
|
|
81
|
+
|
|
82
|
+
static constexpr bool xToFp32_ = !IsSameType<Tx, float>::value;
|
|
83
|
+
static constexpr bool yToFp32_ = IsSameType<Ty, float>::value;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
template <typename Tx, typename Ty>
|
|
87
|
+
__aicore__ inline void SoftmaxV2ArRecompute<Tx, Ty>::Init(GM_ADDR x, GM_ADDR y,
|
|
88
|
+
const SoftmaxV2ArRecomputeTilingData* tilingData)
|
|
89
|
+
{
|
|
90
|
+
tl_ = tilingData;
|
|
91
|
+
|
|
92
|
+
int64_t rowBlockCount = ops::FloorDiv(tl_->a, tl_->aBlockFactor);
|
|
93
|
+
int64_t tailBlockFactor = tl_->a - rowBlockCount * tl_->aBlockFactor;
|
|
94
|
+
|
|
95
|
+
if (blockIdx_ < rowBlockCount) {
|
|
96
|
+
currentRowBlock_ = tl_->aBlockFactor;
|
|
97
|
+
} else {
|
|
98
|
+
currentRowBlock_ = tailBlockFactor;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (tl_->basicBlockLoop == 0) {
|
|
102
|
+
resultCacheID_ = 0;
|
|
103
|
+
} else {
|
|
104
|
+
resultCacheID_ = GetCacheId(tl_->basicBlockLoop - 1);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
xGm_.SetGlobalBuffer((__gm__ Tx*)x);
|
|
108
|
+
yGm_.SetGlobalBuffer((__gm__ Ty*)y);
|
|
109
|
+
|
|
110
|
+
pipe_->InitBuffer(xQueue_, TRIPLE_BUFFER, tl_->ubFactor * sizeof(Tx));
|
|
111
|
+
pipe_->InitBuffer(yQueue_, DOUBLE_BUFFER, tl_->ubFactor * sizeof(Ty));
|
|
112
|
+
|
|
113
|
+
pipe_->InitBuffer(xTmpBuffer, tl_->ubFactor * sizeof(float));
|
|
114
|
+
pipe_->InitBuffer(xMaxBuffer, AR_RECOMPUTE_MAX_BUFFER_BTYES);
|
|
115
|
+
pipe_->InitBuffer(xSumBuffer, AR_RECOMPUTE_SUM_BUFFER_BTYES);
|
|
116
|
+
pipe_->InitBuffer(cachebuffer, AR_RECOMPUTE_BINARY_CACHE_BTYES);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
template <typename Tx, typename Ty>
|
|
120
|
+
__aicore__ inline void SoftmaxV2ArRecompute<Tx, Ty>::Process()
|
|
121
|
+
{
|
|
122
|
+
DataCopyPadExtParams<Tx> padExtParams{false, 0, 0, 0};
|
|
123
|
+
|
|
124
|
+
int64_t xDimOffsetPerCore = tl_->aBlockFactor * blockIdx_; // 每个核按行的偏移
|
|
125
|
+
LocalTensor<float> xMaxLocal = xMaxBuffer.Get<float>();
|
|
126
|
+
LocalTensor<float> xSumLocal = xSumBuffer.Get<float>();
|
|
127
|
+
|
|
128
|
+
// 对每行循环
|
|
129
|
+
for (uint64_t rowIdx = 0; rowIdx < currentRowBlock_; rowIdx++) {
|
|
130
|
+
int64_t xDimOffset = (xDimOffsetPerCore + rowIdx) * tl_->r; // 每行的偏移量
|
|
131
|
+
|
|
132
|
+
AscendC::Duplicate(xMaxLocal, CONST_FP32_MIN, AR_RECOMPUTE_MAX_BUFFER_BTYES / sizeof(float));
|
|
133
|
+
|
|
134
|
+
LocalTensor<float> cacheLocal = cachebuffer.Get<float>();
|
|
135
|
+
|
|
136
|
+
DataCopyExtParams x1DataCopyExtParams;
|
|
137
|
+
x1DataCopyExtParams.blockCount = 1;
|
|
138
|
+
x1DataCopyExtParams.srcStride = 0;
|
|
139
|
+
x1DataCopyExtParams.dstStride = 0;
|
|
140
|
+
|
|
141
|
+
__local_mem__ float* xMaxPtr = (__local_mem__ float*)xMaxLocal.GetPhyAddr();
|
|
142
|
+
// step 1. 对R循环,求整行R的最大值
|
|
143
|
+
for (uint64_t ubIdx = 0; ubIdx < tl_->aLoopCountCeil; ubIdx++) {
|
|
144
|
+
int64_t xUbOffset = xDimOffset + tl_->ubFactor * ubIdx; // 每个UB循环的偏移量
|
|
145
|
+
int64_t ubFactor = tl_->ubFactor;
|
|
146
|
+
if (ubIdx == tl_->aLoopCountCeil - 1 && tl_->ubFactorTail > 0) {
|
|
147
|
+
ubFactor = tl_->ubFactorTail;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
LocalTensor<Tx> xLocal = xQueue_.AllocTensor<Tx>();
|
|
151
|
+
x1DataCopyExtParams.blockLen = ubFactor * sizeof(Tx);
|
|
152
|
+
DataCopyPad(xLocal[0], xGm_[xUbOffset], x1DataCopyExtParams, padExtParams);
|
|
153
|
+
xQueue_.EnQue<Tx>(xLocal);
|
|
154
|
+
xLocal = xQueue_.DeQue<Tx>();
|
|
155
|
+
|
|
156
|
+
__local_mem__ Tx* xPtr = (__local_mem__ Tx*)xLocal.GetPhyAddr();
|
|
157
|
+
CalculateMaxVF(xMaxPtr, xPtr, A_IN_IN, ubFactor);
|
|
158
|
+
xQueue_.FreeTensor(xLocal);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// step 2. UB间二分累加:计算每行的Σe^(x - max)
|
|
162
|
+
LocalTensor<float> xTmpLocal = xTmpBuffer.Get<float>();
|
|
163
|
+
__local_mem__ float* xTmpFp32Ptr = (__local_mem__ float*)xTmpLocal.GetPhyAddr();
|
|
164
|
+
|
|
165
|
+
x1DataCopyExtParams.blockLen = tl_->ubFactor * sizeof(Tx);
|
|
166
|
+
|
|
167
|
+
DataCopyExtParams x2DataCopyExtParams;
|
|
168
|
+
x2DataCopyExtParams.blockCount = 1;
|
|
169
|
+
x2DataCopyExtParams.blockLen = tl_->ubFactor * sizeof(Tx);
|
|
170
|
+
x2DataCopyExtParams.srcStride = 0;
|
|
171
|
+
x2DataCopyExtParams.dstStride = 0;
|
|
172
|
+
|
|
173
|
+
for (uint64_t basicBlockIdx = 0; basicBlockIdx < tl_->basicBlockLoop; basicBlockIdx++) {
|
|
174
|
+
int64_t xUbOffset1 = xDimOffset + tl_->ubFactor * basicBlockIdx; // 主块
|
|
175
|
+
int64_t xUbOffset2 = xDimOffset + tl_->ubFactor * (tl_->basicBlockLoop + basicBlockIdx); // 被折叠块
|
|
176
|
+
int64_t ubFactor = tl_->ubFactor;
|
|
177
|
+
|
|
178
|
+
LocalTensor<Tx> x1Local = xQueue_.AllocTensor<Tx>();
|
|
179
|
+
|
|
180
|
+
DataCopyPad(x1Local[0], xGm_[xUbOffset1], x1DataCopyExtParams, padExtParams);
|
|
181
|
+
xQueue_.EnQue<Tx>(x1Local);
|
|
182
|
+
x1Local = xQueue_.DeQue<Tx>();
|
|
183
|
+
|
|
184
|
+
__local_mem__ Tx* x1Ptr = (__local_mem__ Tx*)x1Local.GetPhyAddr();
|
|
185
|
+
MainBlockCastSubExpVF(xTmpFp32Ptr, x1Ptr, xMaxPtr, A_IN_IN, tl_->ubFactor);
|
|
186
|
+
xQueue_.FreeTensor(x1Local);
|
|
187
|
+
|
|
188
|
+
// 折叠部分:X2折叠到X1上
|
|
189
|
+
if (basicBlockIdx < tl_->mainFoldCount) {
|
|
190
|
+
LocalTensor<Tx> x2Local = xQueue_.AllocTensor<Tx>();
|
|
191
|
+
__local_mem__ Tx* x2Ptr = (__local_mem__ Tx*)x2Local.GetPhyAddr();
|
|
192
|
+
DataCopyPad(x2Local[0], xGm_[xUbOffset2], x2DataCopyExtParams, padExtParams);
|
|
193
|
+
xQueue_.EnQue<Tx>(x2Local);
|
|
194
|
+
x2Local = xQueue_.DeQue<Tx>();
|
|
195
|
+
|
|
196
|
+
FoldBlockCastSubExpVF(xTmpFp32Ptr, x2Ptr, xMaxPtr, A_IN_IN, tl_->ubFactor);
|
|
197
|
+
xQueue_.FreeTensor(x2Local);
|
|
198
|
+
} else if ((basicBlockIdx == tl_->mainFoldCount) && (tl_->ubFactorTail > 0)) {
|
|
199
|
+
LocalTensor<Tx> x2Local = xQueue_.AllocTensor<Tx>();
|
|
200
|
+
__local_mem__ Tx* x2Ptr = (__local_mem__ Tx*)x2Local.GetPhyAddr();
|
|
201
|
+
x2DataCopyExtParams.blockLen = tl_->ubFactorTail * sizeof(Tx); // 这里的x2为尾块
|
|
202
|
+
DataCopyPad(x2Local[0], xGm_[xUbOffset2], x2DataCopyExtParams, padExtParams);
|
|
203
|
+
xQueue_.EnQue<Tx>(x2Local);
|
|
204
|
+
x2Local = xQueue_.DeQue<Tx>();
|
|
205
|
+
|
|
206
|
+
FoldBlockCastSubExpVF(xTmpFp32Ptr, x2Ptr, xMaxPtr, A_IN_IN, tl_->ubFactorTail);
|
|
207
|
+
xQueue_.FreeTensor(x2Local);
|
|
208
|
+
}
|
|
209
|
+
// 不折叠的部分:仅拷贝X1到UB,不做预处理
|
|
210
|
+
|
|
211
|
+
// 计算UB内二分累加,并用UpdateCache计算UB间的和
|
|
212
|
+
int64_t cacheId = GetCacheId(basicBlockIdx);
|
|
213
|
+
uint32_t srcShape[2] = {uint32_t(A_IN_IN), uint32_t(tl_->ubFactor)};
|
|
214
|
+
AscendC::ReduceSum<float, AscendC::Pattern::Reduce::AR, true>(xSumLocal, xTmpLocal, srcShape, false);
|
|
215
|
+
UpdateCache(cacheLocal, xSumLocal, cacheId, A_IN_IN * AR_RECOMPUTE_SUM_LEN, A_IN_IN);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// R很小,不需要做UB间二分累加
|
|
219
|
+
if (tl_->basicBlockLoop == 0) {
|
|
220
|
+
LocalTensor<Tx> x1Local = xQueue_.AllocTensor<Tx>();
|
|
221
|
+
__local_mem__ Tx* x1Ptr = (__local_mem__ Tx*)x1Local.GetPhyAddr();
|
|
222
|
+
DataCopyPad(x1Local[0], xGm_[xDimOffset], x1DataCopyExtParams, padExtParams);
|
|
223
|
+
xQueue_.EnQue<Tx>(x1Local);
|
|
224
|
+
x1Local = xQueue_.DeQue<Tx>();
|
|
225
|
+
|
|
226
|
+
MainBlockCastSubExpVF(xTmpFp32Ptr, x1Ptr, xMaxPtr, A_IN_IN, tl_->ubFactor);
|
|
227
|
+
xQueue_.FreeTensor(x1Local);
|
|
228
|
+
uint32_t srcShape[2] = {uint32_t(A_IN_IN), uint32_t(tl_->ubFactor)};
|
|
229
|
+
AscendC::ReduceSum<float, AscendC::Pattern::Reduce::AR, true>(xSumLocal, xTmpLocal, srcShape, false);
|
|
230
|
+
}
|
|
231
|
+
// 不做UB间二分累加时直接获取结果
|
|
232
|
+
totalSumLocal_ = tl_->basicBlockLoop > 0 ? cacheLocal[resultCacheID_ * AR_RECOMPUTE_SUM_LEN] : xSumLocal;
|
|
233
|
+
|
|
234
|
+
DataCopyExtParams yDataCopyExtParams;
|
|
235
|
+
yDataCopyExtParams.blockCount = 1;
|
|
236
|
+
yDataCopyExtParams.srcStride = 0;
|
|
237
|
+
yDataCopyExtParams.dstStride = 0;
|
|
238
|
+
|
|
239
|
+
__local_mem__ float* xSumPtr = (__local_mem__ float*)totalSumLocal_.GetPhyAddr();
|
|
240
|
+
// step 3. 遍历UB块,计算除法
|
|
241
|
+
for (uint64_t ubIdx = 0; ubIdx < tl_->aLoopCountCeil; ubIdx++) {
|
|
242
|
+
int64_t xUbOffset = xDimOffset + tl_->ubFactor * ubIdx;
|
|
243
|
+
int64_t ubFactor = tl_->ubFactor;
|
|
244
|
+
if (ubIdx == tl_->aLoopCountCeil - 1 && tl_->ubFactorTail > 0) {
|
|
245
|
+
ubFactor = tl_->ubFactorTail;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
LocalTensor<Tx> xLocal = xQueue_.AllocTensor<Tx>();
|
|
249
|
+
LocalTensor<Ty> yLocal = yQueue_.AllocTensor<Ty>();
|
|
250
|
+
__local_mem__ Tx* xPtr = (__local_mem__ Tx*)xLocal.GetPhyAddr();
|
|
251
|
+
__local_mem__ Ty* yPtr = (__local_mem__ Ty*)yLocal.GetPhyAddr();
|
|
252
|
+
|
|
253
|
+
x1DataCopyExtParams.blockLen = ubFactor * sizeof(Tx);
|
|
254
|
+
DataCopyPad(xLocal[0], xGm_[xUbOffset], x1DataCopyExtParams, padExtParams);
|
|
255
|
+
xQueue_.EnQue<Tx>(xLocal);
|
|
256
|
+
xLocal = xQueue_.DeQue<Tx>();
|
|
257
|
+
|
|
258
|
+
CalculateOutVF(yPtr, xPtr, xMaxPtr, xSumPtr, A_IN_IN, ubFactor);
|
|
259
|
+
xQueue_.FreeTensor(xLocal);
|
|
260
|
+
yQueue_.EnQue<Ty>(yLocal);
|
|
261
|
+
yLocal = yQueue_.DeQue<Ty>();
|
|
262
|
+
|
|
263
|
+
yDataCopyExtParams.blockLen = ubFactor * sizeof(Ty);
|
|
264
|
+
DataCopyPad(yGm_[xUbOffset], yLocal[0], yDataCopyExtParams);
|
|
265
|
+
yQueue_.FreeTensor(yLocal);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
template <typename Tx, typename Ty>
|
|
271
|
+
__aicore__ inline void SoftmaxV2ArRecompute<Tx, Ty>::CalculateMaxVF(__local_mem__ float*& xMaxPtr,
|
|
272
|
+
__local_mem__ Tx*& xPtr, uint32_t aSize,
|
|
273
|
+
uint32_t ubFactor)
|
|
274
|
+
{
|
|
275
|
+
__VEC_SCOPE__
|
|
276
|
+
{
|
|
277
|
+
AscendC::MicroAPI::RegTensor<float> vreg1, vreg2, maxReg;
|
|
278
|
+
AscendC::MicroAPI::RegTensor<Tx> vreg3;
|
|
279
|
+
AscendC::MicroAPI::MaskReg maskTail, maskOne, maskFull;
|
|
280
|
+
|
|
281
|
+
uint32_t constOne = 1;
|
|
282
|
+
maskFull = AscendC::MicroAPI::CreateMask<float, AscendC::MicroAPI::MaskPattern::ALL>();
|
|
283
|
+
maskOne = AscendC::MicroAPI::UpdateMask<float>(constOne); // 用于读写1个元素
|
|
284
|
+
|
|
285
|
+
uint16_t repeatTimes = CeilDivision(ubFactor, VL_FP32);
|
|
286
|
+
uint16_t repeatTimesTmp = repeatTimes - 1;
|
|
287
|
+
|
|
288
|
+
// 尾块处理
|
|
289
|
+
uint32_t tail = static_cast<uint32_t>(ubFactor - VL_FP32 * (repeatTimes - 1));
|
|
290
|
+
maskTail = AscendC::MicroAPI::UpdateMask<float>(tail);
|
|
291
|
+
uint16_t j = repeatTimes - 1;
|
|
292
|
+
auto xAddr = xPtr + j * VL_FP32;
|
|
293
|
+
|
|
294
|
+
AscendC::MicroAPI::Duplicate(maxReg, CONST_FP32_MIN);
|
|
295
|
+
|
|
296
|
+
if constexpr (xToFp32_) {
|
|
297
|
+
AscendC::MicroAPI::DataCopy<Tx, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>(vreg3, xAddr);
|
|
298
|
+
AscendC::MicroAPI::Cast<float, Tx, castTraitFp16ToFp32>(vreg1, vreg3, maskTail);
|
|
299
|
+
} else {
|
|
300
|
+
AscendC::MicroAPI::DataCopy(vreg1, xAddr);
|
|
301
|
+
}
|
|
302
|
+
AscendC::MicroAPI::Max(vreg1, maxReg, vreg1, maskTail);
|
|
303
|
+
AscendC::MicroAPI::Copy<float, AscendC::MicroAPI::MaskMergeMode::MERGING>(maxReg, vreg1, maskTail);
|
|
304
|
+
|
|
305
|
+
// 整块处理
|
|
306
|
+
for (uint16_t j = 0; j < repeatTimesTmp; j++) {
|
|
307
|
+
auto xAddr = xPtr + j * VL_FP32;
|
|
308
|
+
if constexpr (xToFp32_) {
|
|
309
|
+
AscendC::MicroAPI::DataCopy<Tx, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>(vreg3, xAddr);
|
|
310
|
+
AscendC::MicroAPI::Cast<float, Tx, castTraitFp16ToFp32>(vreg1, vreg3, maskFull);
|
|
311
|
+
} else {
|
|
312
|
+
AscendC::MicroAPI::DataCopy(vreg1, xAddr);
|
|
313
|
+
}
|
|
314
|
+
AscendC::MicroAPI::Max(maxReg, maxReg, vreg1, maskFull);
|
|
315
|
+
}
|
|
316
|
+
AscendC::MicroAPI::DataCopy(vreg2, xMaxPtr);
|
|
317
|
+
|
|
318
|
+
AscendC::MicroAPI::ReduceMax(maxReg, maxReg, maskFull);
|
|
319
|
+
AscendC::MicroAPI::Max(maxReg, maxReg, vreg2, maskOne);
|
|
320
|
+
AscendC::MicroAPI::DataCopy(xMaxPtr, maxReg, maskOne);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
template <typename Tx, typename Ty>
|
|
325
|
+
__aicore__ inline void SoftmaxV2ArRecompute<Tx, Ty>::CalculateOutVF(__local_mem__ Ty*& yPtr, __local_mem__ Tx*& xPtr,
|
|
326
|
+
__local_mem__ float*& xMaxPtr,
|
|
327
|
+
__local_mem__ float*& xSumPtr, uint32_t a,
|
|
328
|
+
uint32_t ubFactor)
|
|
329
|
+
{
|
|
330
|
+
__VEC_SCOPE__
|
|
331
|
+
{
|
|
332
|
+
AscendC::MicroAPI::RegTensor<Tx> vreg0;
|
|
333
|
+
AscendC::MicroAPI::RegTensor<float> sumReg, maxReg, vreg1, vreg2, vreg3;
|
|
334
|
+
AscendC::MicroAPI::RegTensor<Ty> vreg4;
|
|
335
|
+
AscendC::MicroAPI::MaskReg mask;
|
|
336
|
+
|
|
337
|
+
uint32_t width = ubFactor;
|
|
338
|
+
uint16_t repeatTimes = CeilDivision(ubFactor, VL_FP32);
|
|
339
|
+
|
|
340
|
+
AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>(maxReg, xMaxPtr);
|
|
341
|
+
AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>(sumReg, xSumPtr);
|
|
342
|
+
|
|
343
|
+
for (uint16_t j = 0; j < repeatTimes; j++) {
|
|
344
|
+
mask = AscendC::MicroAPI::UpdateMask<float>(width);
|
|
345
|
+
auto xAddr = xPtr + j * VL_FP32;
|
|
346
|
+
auto yAddr = yPtr + j * VL_FP32;
|
|
347
|
+
|
|
348
|
+
if constexpr (xToFp32_) {
|
|
349
|
+
AscendC::MicroAPI::DataCopy<Tx, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>(vreg0, xAddr);
|
|
350
|
+
AscendC::MicroAPI::Cast<float, Tx, castTraitFp16ToFp32>(vreg1, vreg0, mask);
|
|
351
|
+
} else {
|
|
352
|
+
AscendC::MicroAPI::DataCopy(vreg1, xAddr);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
AscendC::MicroAPI::Sub(vreg2, vreg1, maxReg, mask);
|
|
356
|
+
AscendC::MicroAPI::Exp(vreg2, vreg2, mask);
|
|
357
|
+
AscendC::MicroAPI::Div(vreg3, vreg2, sumReg, mask);
|
|
358
|
+
|
|
359
|
+
if constexpr (yToFp32_) {
|
|
360
|
+
AscendC::MicroAPI::DataCopy(yAddr, vreg3, mask);
|
|
361
|
+
} else {
|
|
362
|
+
AscendC::MicroAPI::Cast<Ty, float, castTraitFp32ToFp16>(vreg4, vreg3, mask);
|
|
363
|
+
AscendC::MicroAPI::DataCopy<Ty, AscendC::MicroAPI::StoreDist::DIST_PACK_B32>(yAddr, vreg4, mask);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
template <typename Tx, typename Ty>
|
|
370
|
+
__aicore__ inline void SoftmaxV2ArRecompute<Tx, Ty>::MainBlockCastSubExpVF(__local_mem__ float*& xFp32Ptr,
|
|
371
|
+
__local_mem__ Tx*& xPtr,
|
|
372
|
+
__local_mem__ float*& xMaxPtr, uint32_t a,
|
|
373
|
+
uint32_t ubFactor)
|
|
374
|
+
{
|
|
375
|
+
__VEC_SCOPE__
|
|
376
|
+
{
|
|
377
|
+
AscendC::MicroAPI::RegTensor<Tx> vreg0;
|
|
378
|
+
AscendC::MicroAPI::RegTensor<float> vreg1, vreg2, vreg3, maxReg;
|
|
379
|
+
AscendC::MicroAPI::MaskReg mask;
|
|
380
|
+
|
|
381
|
+
uint32_t width = ubFactor;
|
|
382
|
+
uint16_t repeatTimes = CeilDivision(ubFactor, VL_FP32);
|
|
383
|
+
|
|
384
|
+
AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>(maxReg, xMaxPtr);
|
|
385
|
+
for (uint16_t j = 0; j < repeatTimes; j++) {
|
|
386
|
+
mask = AscendC::MicroAPI::UpdateMask<float>(width);
|
|
387
|
+
auto xAddr = xPtr + j * VL_FP32;
|
|
388
|
+
auto xFp32Addr = xFp32Ptr + j * VL_FP32;
|
|
389
|
+
|
|
390
|
+
if constexpr (xToFp32_) {
|
|
391
|
+
AscendC::MicroAPI::DataCopy<Tx, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>(vreg0, xAddr);
|
|
392
|
+
AscendC::MicroAPI::Cast<float, Tx, castTraitFp16ToFp32>(vreg1, vreg0, mask);
|
|
393
|
+
} else {
|
|
394
|
+
AscendC::MicroAPI::DataCopy(vreg1, xAddr);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
AscendC::MicroAPI::Sub(vreg2, vreg1, maxReg, mask);
|
|
398
|
+
AscendC::MicroAPI::Exp(vreg3, vreg2, mask);
|
|
399
|
+
|
|
400
|
+
AscendC::MicroAPI::DataCopy(xFp32Addr, vreg3, mask);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
template <typename Tx, typename Ty>
|
|
406
|
+
__aicore__ inline void SoftmaxV2ArRecompute<Tx, Ty>::FoldBlockCastSubExpVF(__local_mem__ float*& dstPtr,
|
|
407
|
+
__local_mem__ Tx*& xPtr,
|
|
408
|
+
__local_mem__ float*& xMaxPtr, uint32_t a,
|
|
409
|
+
uint32_t ubFactor)
|
|
410
|
+
{
|
|
411
|
+
__VEC_SCOPE__
|
|
412
|
+
{
|
|
413
|
+
AscendC::MicroAPI::RegTensor<Tx> vreg0;
|
|
414
|
+
AscendC::MicroAPI::RegTensor<float> vreg1, vreg2, vreg3, maxReg, dstReg;
|
|
415
|
+
AscendC::MicroAPI::MaskReg mask;
|
|
416
|
+
|
|
417
|
+
uint32_t width = ubFactor;
|
|
418
|
+
uint16_t repeatTimes = CeilDivision(ubFactor, VL_FP32);
|
|
419
|
+
|
|
420
|
+
AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_BRC_B32>(maxReg, xMaxPtr);
|
|
421
|
+
for (uint16_t j = 0; j < repeatTimes; j++) {
|
|
422
|
+
mask = AscendC::MicroAPI::UpdateMask<float>(width);
|
|
423
|
+
auto xAddr = xPtr + j * VL_FP32;
|
|
424
|
+
auto dstAddr = dstPtr + j * VL_FP32;
|
|
425
|
+
|
|
426
|
+
if constexpr (xToFp32_) {
|
|
427
|
+
AscendC::MicroAPI::DataCopy<Tx, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>(vreg0, xAddr);
|
|
428
|
+
AscendC::MicroAPI::Cast<float, Tx, castTraitFp16ToFp32>(vreg1, vreg0, mask);
|
|
429
|
+
} else {
|
|
430
|
+
AscendC::MicroAPI::DataCopy(vreg1, xAddr);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
AscendC::MicroAPI::Sub(vreg2, vreg1, maxReg, mask);
|
|
434
|
+
AscendC::MicroAPI::Exp(vreg3, vreg2, mask);
|
|
435
|
+
|
|
436
|
+
AscendC::MicroAPI::DataCopy(dstReg, dstAddr);
|
|
437
|
+
AscendC::MicroAPI::Add(dstReg, dstReg, vreg3, mask);
|
|
438
|
+
|
|
439
|
+
AscendC::MicroAPI::DataCopy(dstAddr, dstReg, mask);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
template <typename Tx, typename Ty>
|
|
445
|
+
__aicore__ inline int64_t SoftmaxV2ArRecompute<Tx, Ty>::GetCacheId(const int64_t idx)
|
|
446
|
+
{
|
|
447
|
+
return AscendC::ScalarGetCountOfValue<1>(idx ^ (idx + CONST_ONE)) - CONST_ONE;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
template <typename Tx, typename Ty>
|
|
451
|
+
__aicore__ inline void SoftmaxV2ArRecompute<Tx, Ty>::UpdateCache(const LocalTensor<float>& dstTensor,
|
|
452
|
+
const LocalTensor<float>& srcTensor,
|
|
453
|
+
const int64_t cacheId, const int64_t stride,
|
|
454
|
+
const int64_t count)
|
|
455
|
+
{
|
|
456
|
+
uint16_t outerLoopTimes =
|
|
457
|
+
ops::CeilDiv(static_cast<int64_t>(count * sizeof(float)), static_cast<int64_t>(GetVRegSize()));
|
|
458
|
+
uint16_t innerLoopTimes = cacheId;
|
|
459
|
+
uint32_t outerLoopStride = VL_FP32;
|
|
460
|
+
uint32_t innerLoopStride = stride;
|
|
461
|
+
|
|
462
|
+
__local_mem__ float* dst = (__local_mem__ float*)dstTensor.GetPhyAddr();
|
|
463
|
+
__local_mem__ float* cache = (__local_mem__ float*)dstTensor.GetPhyAddr() + cacheId * stride;
|
|
464
|
+
__local_mem__ float* src = (__local_mem__ float*)srcTensor.GetPhyAddr();
|
|
465
|
+
|
|
466
|
+
__VEC_SCOPE__
|
|
467
|
+
{
|
|
468
|
+
uint32_t sreg = static_cast<uint32_t>(count);
|
|
469
|
+
AscendC::MicroAPI::RegTensor<float> aReg, bReg;
|
|
470
|
+
AscendC::MicroAPI::MaskReg pMask;
|
|
471
|
+
for (uint16_t i = 0; i < outerLoopTimes; ++i) {
|
|
472
|
+
pMask = AscendC::MicroAPI::UpdateMask<float>(sreg);
|
|
473
|
+
AscendC::MicroAPI::DataCopy(aReg, (__local_mem__ float*)src + i * outerLoopStride);
|
|
474
|
+
for (uint16_t j = 0; j < innerLoopTimes; ++j) {
|
|
475
|
+
AscendC::MicroAPI::DataCopy(bReg,
|
|
476
|
+
(__local_mem__ float*)dst + i * outerLoopStride + j * innerLoopStride);
|
|
477
|
+
AscendC::MicroAPI::Add<float, AscendC::MicroAPI::MaskMergeMode::ZEROING>(aReg, aReg, bReg, pMask);
|
|
478
|
+
}
|
|
479
|
+
AscendC::MicroAPI::DataCopy((__local_mem__ float*)cache + i * outerLoopStride, aReg, pMask);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
} // namespace SoftmaxV2Ops
|
|
485
|
+
#endif // SOFTMAX_V2_AR_RECOMPUTE_H
|