Merged
[GBSAG] Add A2/A3 Op generic_block_sparse_attention_grad and generic_block_sparse_attention_grad_metadata #11499
tramp-llAdd on Sep 8
[GBSAG] Add A2/A3 Op generic_block_sparse_attention_grad and generic_block_sparse_attention_grad_metadata #11499
Merged
Merged fromtramp-ll/ops-transformer9.2.0intocann/ops-transformer9.2.0
Total: 47 files changed+9806-17
| @@ -43,5 +43,6 @@ if(NOT BUILD_OPS_RTY_KERNEL) | |||
| 43 | add_tiling_modules() | 43 | add_tiling_modules() |
| 44 | target_sources(${OPHOST_NAME}_tiling_obj PRIVATE | 44 | target_sources(${OPHOST_NAME}_tiling_obj PRIVATE |
| 45 | ./arch35/generic_block_sparse_attention_grad_tiling_arch35.cpp | 45 | ./arch35/generic_block_sparse_attention_grad_tiling_arch35.cpp |
| 46 | + ./arch22/generic_block_sparse_attention_grad_tiling_arch22.cpp | ||
| 46 | ) | 47 | ) |
| 47 | endif() | 48 | endif() |
Mattention/generic_block_sparse_attention_grad/op_host/generic_block_sparse_attention_grad_def.cpp+2-0
| @@ -102,6 +102,8 @@ public: | |||
| 102 | this->Attr("win_right").AttrType(OPTIONAL).Int(-1); | 102 | this->Attr("win_right").AttrType(OPTIONAL).Int(-1); |
| 103 | 103 | ||
| 104 | this->AICore().AddConfig("ascend950"); | 104 | this->AICore().AddConfig("ascend950"); |
| 105 | + this->AICore().AddConfig("ascend910b"); | ||
| 106 | + this->AICore().AddConfig("ascend910_93"); | ||
| 105 | } | 107 | } |
| 106 | }; | 108 | }; |
| 107 | 109 | ||
Mattention/generic_block_sparse_attention_grad/op_host/generic_block_sparse_attention_grad_tiling.h+59-0
| @@ -50,7 +50,66 @@ TILING_DATA_FIELD_DEF(int32_t, winRight); | |||
| 50 | TILING_DATA_FIELD_DEF_STRUCT(SoftMaxTiling, softmaxGradFrontTilingData); | 50 | TILING_DATA_FIELD_DEF_STRUCT(SoftMaxTiling, softmaxGradFrontTilingData); |
| 51 | END_TILING_DATA_DEF; | 51 | END_TILING_DATA_DEF; |
| 52 | 52 | ||
| 53 | +BEGIN_TILING_DATA_DEF(GenericBlockSparseAttentionGradTilingData) | ||
| 54 | +// 基础参数 | ||
| 55 | +TILING_DATA_FIELD_DEF(uint32_t, batch); | ||
| 56 | +TILING_DATA_FIELD_DEF(uint32_t, numHeads); | ||
| 57 | +TILING_DATA_FIELD_DEF(uint32_t, kvHeads); | ||
| 58 | +TILING_DATA_FIELD_DEF(uint32_t, headDim); | ||
| 59 | + | ||
| 60 | +TILING_DATA_FIELD_DEF(uint32_t, maskType); | ||
| 61 | +TILING_DATA_FIELD_DEF(float, scaleValue); | ||
| 62 | +TILING_DATA_FIELD_DEF(uint32_t, isPackedGQA); | ||
| 63 | +TILING_DATA_FIELD_DEF(uint32_t, softmaxPrecision); | ||
| 64 | +TILING_DATA_FIELD_DEF(int64_t, winLeft); | ||
| 65 | +TILING_DATA_FIELD_DEF(int64_t, winRight); | ||
| 66 | + | ||
| 67 | +// 稀疏分块参数 (block_shape 属性, 默认 {1, 128}) | ||
| 68 | +TILING_DATA_FIELD_DEF(uint64_t, blockShapeX); // block的x维度(Q方向) | ||
| 69 | +TILING_DATA_FIELD_DEF(uint64_t, blockShapeY); // block的y维度(KV方向) | ||
| 70 | + | ||
| 71 | +// selectIdx相关参数 | ||
| 72 | + | ||
| 73 | +// Layout: 0=TND, 1=BNSD, 2=BSND | ||
| 74 | +TILING_DATA_FIELD_DEF(uint32_t, inputLayout); | ||
| 75 | + | ||
| 76 | +// BNSD格式的最大序列长度(用于计算stride) | ||
| 77 | +// 当actualSeqLengths为nullptr时,maxQSeqlen也用作统一的qseqlen值 | ||
| 78 | +TILING_DATA_FIELD_DEF(uint32_t, maxQSeqlen); // BNSD格式Q的第三维(S维度),或统一的qseqlen值 | ||
| 79 | +// 当actualSeqLengthsKv为nullptr时,maxKvSeqlen也用作统一的kvseqlen值 | ||
| 80 | +TILING_DATA_FIELD_DEF(uint32_t, maxKvSeqlen); // BNSD/BSND格式KV的S维度 | ||
| 81 | +TILING_DATA_FIELD_DEF(uint32_t, useUniformQSeqlen); | ||
| 82 | +TILING_DATA_FIELD_DEF(uint32_t, useUniformKvSeqlen); | ||
| 83 | + | ||
| 84 | +// TilingKey for kernel dispatch (生成在tiling层) | ||
| 85 | +TILING_DATA_FIELD_DEF(uint64_t, tilingKey); | ||
| 86 | +TILING_DATA_FIELD_DEF(uint64_t, gradSize); | ||
| 87 | + | ||
| 88 | +// Workspace大小 | ||
| 89 | +TILING_DATA_FIELD_DEF(uint64_t, sOutSize); | ||
| 90 | +TILING_DATA_FIELD_DEF(uint64_t, dPOutSize); | ||
| 91 | +TILING_DATA_FIELD_DEF(uint64_t, dQOutSize); | ||
| 92 | +TILING_DATA_FIELD_DEF(uint64_t, dKOutSize); | ||
| 93 | +TILING_DATA_FIELD_DEF(uint64_t, dVOutSize); | ||
| 94 | + | ||
| 95 | +TILING_DATA_FIELD_DEF(uint32_t, basicKVBlockSize); | ||
| 96 | +TILING_DATA_FIELD_DEF(uint32_t, usedVecCoreNum); | ||
| 97 | +TILING_DATA_FIELD_DEF(uint64_t, dqSize); | ||
| 98 | +TILING_DATA_FIELD_DEF(uint64_t, dkvSize); | ||
| 99 | +TILING_DATA_FIELD_DEF(uint64_t, postUbBaseSize); | ||
| 100 | +TILING_DATA_FIELD_DEF(uint64_t, ubSize); | ||
| 101 | +TILING_DATA_FIELD_DEF_STRUCT(SoftMaxTiling, softmaxGradTilingData); | ||
| 102 | +// packetWorkspaceSize:所有实际 AIC core 的 packet 双缓冲总字节数。 | ||
| 103 | +TILING_DATA_FIELD_DEF(uint64_t, packetWorkspaceSize); | ||
| 104 | +END_TILING_DATA_DEF; | ||
| 105 | + | ||
| 53 | REGISTER_TILING_DATA_CLASS(GenericBlockSparseAttentionGrad, GenericBlockSparseAttentionGradTilingDataArch35) | 106 | REGISTER_TILING_DATA_CLASS(GenericBlockSparseAttentionGrad, GenericBlockSparseAttentionGradTilingDataArch35) |
| 107 | +REGISTER_TILING_DATA_CLASS(GenericBlockSparseAttentionGrad_100, GenericBlockSparseAttentionGradTilingData) | ||
| 108 | +REGISTER_TILING_DATA_CLASS(GenericBlockSparseAttentionGrad_101, GenericBlockSparseAttentionGradTilingData) | ||
| 109 | +REGISTER_TILING_DATA_CLASS(GenericBlockSparseAttentionGrad_102, GenericBlockSparseAttentionGradTilingData) | ||
| 110 | +REGISTER_TILING_DATA_CLASS(GenericBlockSparseAttentionGrad_110, GenericBlockSparseAttentionGradTilingData) | ||
| 111 | +REGISTER_TILING_DATA_CLASS(GenericBlockSparseAttentionGrad_111, GenericBlockSparseAttentionGradTilingData) | ||
| 112 | +REGISTER_TILING_DATA_CLASS(GenericBlockSparseAttentionGrad_112, GenericBlockSparseAttentionGradTilingData) | ||
| 54 | 113 | ||
| 55 | struct GenericBlockSparseAttentionGradCompileInfo { | 114 | struct GenericBlockSparseAttentionGradCompileInfo { |
| 56 | uint32_t inputDataByte = 2; | 115 | uint32_t inputDataByte = 2; |
| @@ -0,0 +1,55 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_ARCH_HPP | ||
| 12 | +#define GBSAG_ARCH_HPP | ||
| 13 | + | ||
| 14 | +#include "../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | + | ||
| 16 | +namespace NpuArch::Arch { | ||
| 17 | + | ||
| 18 | +struct AtlasA2 { | ||
| 19 | + static constexpr uint32_t BIAS_SIZE = 1024; | ||
| 20 | + static constexpr uint32_t FIXBUF_SIZE = 7U * 1024U; | ||
| 21 | + // AscendC user-addressable UB window on this target is 192 KiB. | ||
| 22 | + static constexpr uint32_t UB_SIZE = 192U * 1024U; | ||
| 23 | + static constexpr uint32_t L1_SIZE = 512U * 1024U; | ||
| 24 | + static constexpr uint32_t L0A_SIZE = 64U * 1024U; | ||
| 25 | + static constexpr uint32_t L0B_SIZE = 64U * 1024U; | ||
| 26 | + static constexpr uint32_t L0C_SIZE = 128U * 1024U; | ||
| 27 | +}; | ||
| 28 | + | ||
| 29 | +struct PositionGM { | ||
| 30 | + static constexpr AscendC::TPosition POSITION = AscendC::TPosition::GM; | ||
| 31 | +}; | ||
| 32 | + | ||
| 33 | +struct PositionL1 { | ||
| 34 | + static constexpr AscendC::TPosition POSITION = AscendC::TPosition::A1; | ||
| 35 | +}; | ||
| 36 | + | ||
| 37 | +struct PositionL0A { | ||
| 38 | + static constexpr AscendC::TPosition POSITION = AscendC::TPosition::A2; | ||
| 39 | +}; | ||
| 40 | + | ||
| 41 | +struct PositionL0B { | ||
| 42 | + static constexpr AscendC::TPosition POSITION = AscendC::TPosition::B2; | ||
| 43 | +}; | ||
| 44 | + | ||
| 45 | +struct PositionL0C { | ||
| 46 | + static constexpr AscendC::TPosition POSITION = AscendC::TPosition::CO1; | ||
| 47 | +}; | ||
| 48 | + | ||
| 49 | +struct PositionUB { | ||
| 50 | + static constexpr AscendC::TPosition POSITION = AscendC::TPosition::VECCALC; | ||
| 51 | +}; | ||
| 52 | + | ||
| 53 | +} // namespace NpuArch::Arch | ||
| 54 | + | ||
| 55 | +#endif // ARCH_ARCH_HPP | ||
| @@ -0,0 +1,217 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_LOCAL_TENSOR_BUFFER_HPP | ||
| 12 | +#define GBSAG_LOCAL_TENSOR_BUFFER_HPP | ||
| 13 | + | ||
| 14 | +#include "../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | +#include "../../attn_infra/arch/gbsag_arch.hpp" | ||
| 16 | + | ||
| 17 | +namespace NpuArch::Arch { | ||
| 18 | + | ||
| 19 | +struct LocalTensorBufferBase { | ||
| 20 | +public: | ||
| 21 | + template <class Element = half> | ||
| 22 | + __aicore__ inline AscendC::LocalTensor<Element> GetBufferByByte(const uint32_t offset) const | ||
| 23 | + { | ||
| 24 | + return tensor[offset].template ReinterpretCast<Element>(); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | +protected: | ||
| 28 | + __aicore__ inline LocalTensorBufferBase() = default; | ||
| 29 | + | ||
| 30 | + AscendC::LocalTensor<uint8_t> tensor; | ||
| 31 | +}; | ||
| 32 | + | ||
| 33 | +template <class ArchTag, AscendC::TPosition Position> | ||
| 34 | +struct LocalTensorBuffer { | ||
| 35 | + static_assert(DEPENDENT_FALSE<ArchTag>, "Unsupported local tensor buffer, can not find the specialization."); | ||
| 36 | +}; | ||
| 37 | + | ||
| 38 | +/// Partial specialization for TPosition::A1 | ||
| 39 | +template <class ArchTag> | ||
| 40 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::A1> : LocalTensorBufferBase { | ||
| 41 | +public: | ||
| 42 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::A1; | ||
| 43 | + | ||
| 44 | + __aicore__ inline LocalTensorBuffer() | ||
| 45 | + { | ||
| 46 | + AscendC::TBuf<AscendC::TPosition::A1> tbufA1; | ||
| 47 | + GetTPipePtr()->InitBuffer(tbufA1, ArchTag::L1_SIZE); | ||
| 48 | + tensor = tbufA1.Get<uint8_t>(); | ||
| 49 | + } | ||
| 50 | +}; | ||
| 51 | + | ||
| 52 | +/////////////////////////////////////////////////////////// | ||
| 53 | + | ||
| 54 | +/// Partial specialization for TPosition::A2 | ||
| 55 | +template <class ArchTag> | ||
| 56 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::A2> : LocalTensorBufferBase { | ||
| 57 | +public: | ||
| 58 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::A2; | ||
| 59 | + | ||
| 60 | + __aicore__ inline LocalTensorBuffer() | ||
| 61 | + { | ||
| 62 | + AscendC::TBuf<AscendC::TPosition::A2> tbufA2; | ||
| 63 | + GetTPipePtr()->InitBuffer(tbufA2, ArchTag::L0A_SIZE); | ||
| 64 | + tensor = tbufA2.Get<uint8_t>(); | ||
| 65 | + } | ||
| 66 | +}; | ||
| 67 | + | ||
| 68 | +/////////////////////////////////////////////////////////// | ||
| 69 | + | ||
| 70 | +/// Partial specialization for TPosition::B1 | ||
| 71 | +template <class ArchTag> | ||
| 72 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::B1> : LocalTensorBufferBase { | ||
| 73 | +public: | ||
| 74 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::B1; | ||
| 75 | + | ||
| 76 | + __aicore__ inline LocalTensorBuffer() | ||
| 77 | + { | ||
| 78 | + AscendC::TBuf<AscendC::TPosition::B1> tbufB1; | ||
| 79 | + GetTPipePtr()->InitBuffer(tbufB1, ArchTag::L1_SIZE); | ||
| 80 | + tensor = tbufB1.Get<uint8_t>(); | ||
| 81 | + } | ||
| 82 | +}; | ||
| 83 | + | ||
| 84 | +/////////////////////////////////////////////////////////// | ||
| 85 | + | ||
| 86 | +/// Partial specialization for AtlasA2, TPosition::B2 | ||
| 87 | +template <class ArchTag> | ||
| 88 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::B2> : LocalTensorBufferBase { | ||
| 89 | +public: | ||
| 90 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::B2; | ||
| 91 | + | ||
| 92 | + __aicore__ inline LocalTensorBuffer() | ||
| 93 | + { | ||
| 94 | + AscendC::TBuf<AscendC::TPosition::B2> tbufB2; | ||
| 95 | + GetTPipePtr()->InitBuffer(tbufB2, ArchTag::L0B_SIZE); | ||
| 96 | + tensor = tbufB2.Get<uint8_t>(); | ||
| 97 | + } | ||
| 98 | +}; | ||
| 99 | + | ||
| 100 | +/////////////////////////////////////////////////////////// | ||
| 101 | + | ||
| 102 | +/// Partial specialization for AtlasA2, TPosition::C1 | ||
| 103 | +template <> | ||
| 104 | +struct LocalTensorBuffer<Arch::AtlasA2, AscendC::TPosition::C1> : LocalTensorBufferBase { | ||
| 105 | +public: | ||
| 106 | + using ArchTag = Arch::AtlasA2; | ||
| 107 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::C1; | ||
| 108 | + | ||
| 109 | + __aicore__ inline LocalTensorBuffer() | ||
| 110 | + { | ||
| 111 | + AscendC::TBuf<AscendC::TPosition::C1> tbufC1; | ||
| 112 | + GetTPipePtr()->InitBuffer(tbufC1, ArchTag::L1_SIZE); | ||
| 113 | + tensor = tbufC1.Get<uint8_t>(); | ||
| 114 | + } | ||
| 115 | +}; | ||
| 116 | + | ||
| 117 | +/////////////////////////////////////////////////////////// | ||
| 118 | + | ||
| 119 | +/// Partial specialization for AtlasA2, TPosition::C2 | ||
| 120 | +template <> | ||
| 121 | +struct LocalTensorBuffer<Arch::AtlasA2, AscendC::TPosition::C2> : LocalTensorBufferBase { | ||
| 122 | +public: | ||
| 123 | + using ArchTag = Arch::AtlasA2; | ||
| 124 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::C2; | ||
| 125 | + | ||
| 126 | + __aicore__ inline LocalTensorBuffer() | ||
| 127 | + { | ||
| 128 | + AscendC::TBuf<AscendC::TPosition::C2> tbufC2; | ||
| 129 | + GetTPipePtr()->InitBuffer(tbufC2, ArchTag::BIAS_SIZE); | ||
| 130 | + tensor = tbufC2.Get<uint8_t>(); | ||
| 131 | + } | ||
| 132 | +}; | ||
| 133 | + | ||
| 134 | +/////////////////////////////////////////////////////////// | ||
| 135 | + | ||
| 136 | +/// Partial specialization for TPosition::CO1 | ||
| 137 | +template <class ArchTag> | ||
| 138 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::CO1> : LocalTensorBufferBase { | ||
| 139 | +public: | ||
| 140 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::CO1; | ||
| 141 | + | ||
| 142 | + __aicore__ inline LocalTensorBuffer() | ||
| 143 | + { | ||
| 144 | + AscendC::TBuf<AscendC::TPosition::CO1> tbufCO1; | ||
| 145 | + GetTPipePtr()->InitBuffer(tbufCO1, ArchTag::L0C_SIZE); | ||
| 146 | + tensor = tbufCO1.Get<uint8_t>(); | ||
| 147 | + } | ||
| 148 | +}; | ||
| 149 | + | ||
| 150 | +/////////////////////////////////////////////////////////// | ||
| 151 | + | ||
| 152 | +/// Partial specialization for AtlasA2, TPosition::C2PIPE2GM | ||
| 153 | +template <> | ||
| 154 | +struct LocalTensorBuffer<Arch::AtlasA2, AscendC::TPosition::C2PIPE2GM> : LocalTensorBufferBase { | ||
| 155 | +public: | ||
| 156 | + using ArchTag = Arch::AtlasA2; | ||
| 157 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::C2PIPE2GM; | ||
| 158 | + | ||
| 159 | + __aicore__ inline LocalTensorBuffer() | ||
| 160 | + { | ||
| 161 | + AscendC::TBuf<AscendC::TPosition::C2PIPE2GM> tbufC2PIPE2GM; | ||
| 162 | + GetTPipePtr()->InitBuffer(tbufC2PIPE2GM, ArchTag::FIXBUF_SIZE); | ||
| 163 | + tensor = tbufC2PIPE2GM.Get<uint8_t>(); | ||
| 164 | + } | ||
| 165 | +}; | ||
| 166 | + | ||
| 167 | +/////////////////////////////////////////////////////////// | ||
| 168 | + | ||
| 169 | +/// Partial specialization for TPosition::VECIN | ||
| 170 | +template <class ArchTag> | ||
| 171 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::VECIN> : LocalTensorBufferBase { | ||
| 172 | +public: | ||
| 173 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::VECIN; | ||
| 174 | + | ||
| 175 | + __aicore__ inline LocalTensorBuffer() | ||
| 176 | + { | ||
| 177 | + AscendC::TBuf<AscendC::TPosition::VECIN> tbufVECIN; | ||
| 178 | + GetTPipePtr()->InitBuffer(tbufVECIN, ArchTag::UB_SIZE); | ||
| 179 | + tensor = tbufVECIN.Get<uint8_t>(); | ||
| 180 | + } | ||
| 181 | +}; | ||
| 182 | + | ||
| 183 | +/////////////////////////////////////////////////////////// | ||
| 184 | + | ||
| 185 | +/// Partial specialization for TPosition::VECOUT | ||
| 186 | +template <class ArchTag> | ||
| 187 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::VECOUT> : LocalTensorBufferBase { | ||
| 188 | +public: | ||
| 189 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::VECOUT; | ||
| 190 | + | ||
| 191 | + __aicore__ inline LocalTensorBuffer() | ||
| 192 | + { | ||
| 193 | + AscendC::TBuf<AscendC::TPosition::VECOUT> tbufVECOUT; | ||
| 194 | + GetTPipePtr()->InitBuffer(tbufVECOUT, ArchTag::UB_SIZE); | ||
| 195 | + tensor = tbufVECOUT.Get<uint8_t>(); | ||
| 196 | + } | ||
| 197 | +}; | ||
| 198 | + | ||
| 199 | +/////////////////////////////////////////////////////////// | ||
| 200 | + | ||
| 201 | +/// Partial specialization for TPosition::VECCALC | ||
| 202 | +template <class ArchTag> | ||
| 203 | +struct LocalTensorBuffer<ArchTag, AscendC::TPosition::VECCALC> : LocalTensorBufferBase { | ||
| 204 | +public: | ||
| 205 | + static constexpr AscendC::TPosition Position = AscendC::TPosition::VECCALC; | ||
| 206 | + | ||
| 207 | + __aicore__ inline LocalTensorBuffer() | ||
| 208 | + { | ||
| 209 | + AscendC::TBuf<AscendC::TPosition::VECCALC> tbufVECCALC; | ||
| 210 | + GetTPipePtr()->InitBuffer(tbufVECCALC, ArchTag::UB_SIZE); | ||
| 211 | + tensor = tbufVECCALC.Get<uint8_t>(); | ||
| 212 | + } | ||
| 213 | +}; | ||
| 214 | + | ||
| 215 | +} // namespace NpuArch::Arch | ||
| 216 | + | ||
| 217 | +#endif // INCLUDE_ARCH_MEMORY_H | ||
Aattention/generic_block_sparse_attention_grad/op_kernel/arc22/attn_infra/arch/gbsag_resource.hpp+41-0
| @@ -0,0 +1,41 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_RESOURCE_HPP | ||
| 12 | +#define GBSAG_RESOURCE_HPP | ||
| 13 | + | ||
| 14 | +#include "../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | +#include "../../attn_infra/arch/gbsag_local_tensor_buffer.hpp" | ||
| 16 | + | ||
| 17 | +namespace NpuArch::Arch { | ||
| 18 | + | ||
| 19 | +template <class ArchTag> | ||
| 20 | +struct Resource { | ||
| 21 | +public: | ||
| 22 | + AscendC::TPipe pipe; | ||
| 23 | + | ||
| 24 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::A1> l1Buf; | ||
| 25 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::A2> l0ABuf; | ||
| 26 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::B2> l0BBuf; | ||
| 27 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::C2> btBuf; | ||
| 28 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::CO1> l0CBuf; | ||
| 29 | + LocalTensorBuffer<ArchTag, AscendC::TPosition::VECCALC> ubBuf; | ||
| 30 | + | ||
| 31 | + __aicore__ inline Resource() | ||
| 32 | + { | ||
| 33 | + // The initialization of AscendC::Tpipe will insert some synchronization interfaces, | ||
| 34 | + // which may conflict with the usage by users. Therefore, the "destroy" interface is used for releasing. | ||
| 35 | + pipe.Destroy(); | ||
| 36 | + } | ||
| 37 | +}; | ||
| 38 | + | ||
| 39 | +} // namespace NpuArch::Arch | ||
| 40 | + | ||
| 41 | +#endif // INCLUDE_ARCH_RESOURCE_HPP | ||
Aattention/generic_block_sparse_attention_grad/op_kernel/arc22/attn_infra/detail/gbsag_alignment.hpp+74-0
| @@ -0,0 +1,74 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_ALIGNMENT_HPP | ||
| 12 | +#define GBSAG_ALIGNMENT_HPP | ||
| 13 | + | ||
| 14 | +#include <limits> | ||
| 15 | + | ||
| 16 | +#include "../../attn_infra/detail/gbsag_macros.hpp" | ||
| 17 | + | ||
| 18 | +template <uint32_t ALIGN, typename T> | ||
| 19 | +HOST_DEVICE constexpr T RoundDown(const T val) | ||
| 20 | +{ | ||
| 21 | + static_assert(ALIGN != 0U, "ALIGN must not be 0"); | ||
| 22 | + return val / ALIGN * ALIGN; | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +template <class T> | ||
| 26 | +HOST_DEVICE constexpr T RoundDown(const T val, const T align) | ||
| 27 | +{ | ||
| 28 | + if (align == 0) { | ||
| 29 | + return val; | ||
| 30 | + } | ||
| 31 | + return val / align * align; | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +template <uint32_t ALIGN, typename T = uint32_t> | ||
| 35 | +HOST_DEVICE constexpr T RoundUp(const T val) | ||
| 36 | +{ | ||
| 37 | + static_assert(ALIGN != 0U, "ALIGN must not be 0"); | ||
| 38 | + T align = ALIGN; | ||
| 39 | + if (val + align - 1 < val) { | ||
| 40 | + return val; | ||
| 41 | + } | ||
| 42 | + return (val + align - 1) / align * align; | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +template <class T> | ||
| 46 | +HOST_DEVICE constexpr T RoundUp(const T val, const T align) | ||
| 47 | +{ | ||
| 48 | + if (align == 0 || val + align - 1 < val) { | ||
| 49 | + return val; | ||
| 50 | + } | ||
| 51 | + return (val + align - 1) / align * align; | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +template <uint32_t DIVISOR, typename T = uint32_t> | ||
| 55 | +HOST_DEVICE constexpr T CeilDiv(const T dividend) | ||
| 56 | +{ | ||
| 57 | + static_assert(DIVISOR != 0U, "DIVISOR must not be 0"); | ||
| 58 | + T divisor = DIVISOR; | ||
| 59 | + if (dividend + divisor - 1 < dividend) { | ||
| 60 | + return dividend; | ||
| 61 | + } | ||
| 62 | + return (dividend + divisor - 1) / divisor; | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +template <class T> | ||
| 66 | +HOST_DEVICE constexpr T CeilDiv(const T dividend, const T divisor) | ||
| 67 | +{ | ||
| 68 | + if (divisor == 0 || dividend + divisor - 1 < dividend) { | ||
| 69 | + return std::numeric_limits<T>::max(); | ||
| 70 | + } | ||
| 71 | + return (dividend + divisor - 1) / divisor; | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +#endif // ALIGNMENT_HPP | ||
| @@ -0,0 +1,20 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_DEPENDENT_FALSE_HPP | ||
| 12 | +#define GBSAG_DEPENDENT_FALSE_HPP | ||
| 13 | + | ||
| 14 | +template <bool VALUE, class... Args> | ||
| 15 | +constexpr bool DEPENDENT_BOOL_VALUE = VALUE; | ||
| 16 | + | ||
| 17 | +template <class... Args> | ||
| 18 | +constexpr bool DEPENDENT_FALSE = DEPENDENT_BOOL_VALUE<false, Args...>; | ||
| 19 | + | ||
| 20 | +#endif // DETAIL_DEPENDENT_FALSE_HPP | ||
Aattention/generic_block_sparse_attention_grad/op_kernel/arc22/attn_infra/detail/gbsag_macros.hpp+16-0
| @@ -0,0 +1,16 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_MACROS_HPP | ||
| 12 | +#define GBSAG_MACROS_HPP | ||
| 13 | + | ||
| 14 | +#define HOST_DEVICE __forceinline__[host, aicore] | ||
| 15 | + | ||
| 16 | +#endif // DETAIL_MACROS_HPP | ||
| @@ -0,0 +1,26 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_EPILOGUE_HPP | ||
| 12 | +#define GBSAG_EPILOGUE_HPP | ||
| 13 | + | ||
| 14 | +#include "../../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | + | ||
| 16 | +namespace NpuArch::Epilogue::Block { | ||
| 17 | + | ||
| 18 | +template <class DispatchPolicy, class... Args> | ||
| 19 | +class BlockEpilogue { | ||
| 20 | + static_assert(DEPENDENT_FALSE<DispatchPolicy>, "Could not find an epilogue specialization"); | ||
| 21 | +}; | ||
| 22 | + | ||
| 23 | +} // namespace NpuArch::Epilogue::Block | ||
| 24 | + | ||
| 25 | +#include "../../../attn_infra/epilogue/block/gbsag_epilogue_fag_pre.hpp" | ||
| 26 | +#endif // EPILOGUE_BLOCK_GBSAG_EPILOGUE_HPP | ||
| @@ -0,0 +1,171 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 gbsag_epilogue_fag_pre.hpp | ||
| 13 | + * \brief GBSAG Block Epliogue Fag Pre Kernel Implementation | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_FAG_PRE_HPP | ||
| 17 | +#define CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_FAG_PRE_HPP | ||
| 18 | + | ||
| 19 | +#include "../../../attn_infra/arch/gbsag_resource.hpp" | ||
| 20 | +#include "../../../attn_infra/epilogue/gbsag_epilogue_dispatch_policy.hpp" | ||
| 21 | +#include "kernel_operator.h" | ||
| 22 | +#include "lib/matmul_intf.h" | ||
| 23 | +#include "lib/matrix/matmul/tiling.h" | ||
| 24 | + | ||
| 25 | +using namespace AscendC; | ||
| 26 | +namespace NpuArch::Epilogue::Block { | ||
| 27 | + | ||
| 28 | +template <class OutputType_, class UpdateType_, class InputType_> | ||
| 29 | +class BlockEpilogue<EpilogueAtlasA2FAGPre, OutputType_, UpdateType_, InputType_> { | ||
| 30 | +public: | ||
| 31 | + using DispatchPolicy = EpilogueAtlasA2FAGPre; | ||
| 32 | + using ArchTag = typename DispatchPolicy::ArchTag; | ||
| 33 | + | ||
| 34 | + struct Params { | ||
| 35 | + GM_ADDR dqWrk; | ||
| 36 | + GM_ADDR dkWrk; | ||
| 37 | + GM_ADDR dvWrk; | ||
| 38 | + GM_ADDR tilingData; | ||
| 39 | + | ||
| 40 | + // Methods | ||
| 41 | + __aicore__ inline Params() {} | ||
| 42 | + | ||
| 43 | + __aicore__ inline Params(GM_ADDR dqWrk_, GM_ADDR dkWrk_, GM_ADDR dvWrk_, GM_ADDR tilingData_) | ||
| 44 | + : dqWrk(dqWrk_), | ||
| 45 | + dkWrk(dkWrk_), | ||
| 46 | + dvWrk(dvWrk_), | ||
| 47 | + tilingData(tilingData_) | ||
| 48 | + {} | ||
| 49 | + }; | ||
| 50 | + | ||
| 51 | + NpuArch::Arch::Resource<ArchTag> resource; | ||
| 52 | + GlobalTensor<float> dqWorkSpaceGm, dkWorkSpaceGm, dvWorkSpaceGm; | ||
| 53 | + LocalTensor<float> zeroTensor; | ||
| 54 | + uint64_t cBlockIdx; | ||
| 55 | + uint64_t BLOCK_BYTE = 32; | ||
| 56 | + | ||
| 57 | + uint64_t qPreBlockFactor = 0; | ||
| 58 | + uint64_t qPreBlockTotal = 0; | ||
| 59 | + uint64_t qPreTailNumTmp = 0; | ||
| 60 | + uint64_t qPreTailNum = 0; | ||
| 61 | + uint64_t qSizeAlign = 0; | ||
| 62 | + uint64_t initdqSize = 0; | ||
| 63 | + uint64_t dqOffset = 0; | ||
| 64 | + | ||
| 65 | + uint64_t kvPreBlockFactor = 0; | ||
| 66 | + uint64_t kvPreBlockTotal = 0; | ||
| 67 | + uint64_t kvPreTailNumTmp = 0; | ||
| 68 | + uint64_t kvPreTailNum = 0; | ||
| 69 | + uint64_t kvSizeAlign = 0; | ||
| 70 | + uint64_t initdkSize = 0; | ||
| 71 | + uint64_t dkvOffset = 0; | ||
| 72 | + | ||
| 73 | + uint64_t usedCoreNum = 0; | ||
| 74 | + uint64_t ubsize = 0; | ||
| 75 | + | ||
| 76 | + __aicore__ inline BlockEpilogue(Params const ¶ms) | ||
| 77 | + { | ||
| 78 | + cBlockIdx = GetBlockIdx(); | ||
| 79 | + __gm__ GenericBlockSparseAttentionGradTilingData *tilingData = | ||
| 80 | + reinterpret_cast<__gm__ GenericBlockSparseAttentionGradTilingData *>(params.tilingData); | ||
| 81 | + usedCoreNum = tilingData->usedVecCoreNum; // 先按这个把,得适配 | ||
| 82 | + | ||
| 83 | + qSizeAlign = tilingData->dqSize; | ||
| 84 | + kvSizeAlign = tilingData->dkvSize; | ||
| 85 | + ubsize = tilingData->ubSize / BLOCK_BYTE * BLOCK_BYTE; // 32字节对齐 | ||
| 86 | + | ||
| 87 | + qPreBlockFactor = (qSizeAlign + usedCoreNum - 1) / usedCoreNum; // 每个vec 处理的元素数量 向上取整 | ||
| 88 | + qPreBlockTotal = | ||
| 89 | + (qSizeAlign + qPreBlockFactor - 1) / qPreBlockFactor; // 一共需要处理次数 向上取整, 也理解需要的核数 | ||
| 90 | + qPreTailNumTmp = qSizeAlign % qPreBlockFactor; // remain量, 尾核数量 | ||
| 91 | + qPreTailNum = qPreTailNumTmp == 0 ? qPreBlockFactor : qPreTailNumTmp; | ||
| 92 | + | ||
| 93 | + kvPreBlockFactor = (kvSizeAlign + usedCoreNum - 1) / usedCoreNum; | ||
| 94 | + kvPreBlockTotal = (kvSizeAlign + kvPreBlockFactor - 1) / kvPreBlockFactor; | ||
| 95 | + kvPreTailNumTmp = kvSizeAlign % kvPreBlockFactor; | ||
| 96 | + kvPreTailNum = kvPreTailNumTmp == 0 ? kvPreBlockFactor : kvPreTailNumTmp; | ||
| 97 | + | ||
| 98 | + dqWorkSpaceGm.SetGlobalBuffer((__gm__ float *)params.dqWrk); | ||
| 99 | + dkWorkSpaceGm.SetGlobalBuffer((__gm__ float *)params.dkWrk); | ||
| 100 | + dvWorkSpaceGm.SetGlobalBuffer((__gm__ float *)params.dvWrk); | ||
| 101 | + | ||
| 102 | + initdqSize = cBlockIdx == qPreBlockTotal - 1 ? qPreTailNum : qPreBlockFactor; | ||
| 103 | + dqOffset = ((uint64_t)cBlockIdx) * qPreBlockFactor; | ||
| 104 | + initdkSize = cBlockIdx == kvPreBlockTotal - 1 ? kvPreTailNum : kvPreBlockFactor; | ||
| 105 | + dkvOffset = ((uint64_t)cBlockIdx) * kvPreBlockFactor; | ||
| 106 | + | ||
| 107 | + zeroTensor = resource.ubBuf.template GetBufferByByte<float>(0); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + __aicore__ inline ~BlockEpilogue() {} | ||
| 111 | + | ||
| 112 | + template <int32_t CORE_TYPE = g_coreType> | ||
| 113 | + __aicore__ inline void operator()(); | ||
| 114 | + | ||
| 115 | + template <> | ||
| 116 | + __aicore__ inline void operator()<AscendC::AIC>() | ||
| 117 | + {} | ||
| 118 | + | ||
| 119 | + template <> | ||
| 120 | + __aicore__ inline void operator()<AscendC::AIV>() | ||
| 121 | + { | ||
| 122 | + if (cBlockIdx >= usedCoreNum) { | ||
| 123 | + return; | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + uint64_t maxDataCount = (ubsize) / sizeof(float); | ||
| 127 | + Duplicate(zeroTensor, (float)0.0, maxDataCount); | ||
| 128 | + | ||
| 129 | + // dq | ||
| 130 | + uint64_t cOutElement = initdqSize; // currenr out ele | ||
| 131 | + uint64_t totalLoop = cOutElement / maxDataCount; | ||
| 132 | + uint64_t remainOutNum = cOutElement % maxDataCount; | ||
| 133 | + | ||
| 134 | + auto eventID1 = EVENT_ID2; | ||
| 135 | + set_flag(PIPE_V, PIPE_MTE3, eventID1); | ||
| 136 | + wait_flag(PIPE_V, PIPE_MTE3, eventID1); | ||
| 137 | + | ||
| 138 | + if (cBlockIdx < qPreBlockTotal) { | ||
| 139 | + processZero(dqWorkSpaceGm[dqOffset], initdqSize, maxDataCount); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + if (cBlockIdx < kvPreBlockTotal) { | ||
| 143 | + processZero(dkWorkSpaceGm[dkvOffset], initdkSize, maxDataCount); | ||
| 144 | + processZero(dvWorkSpaceGm[dkvOffset], initdkSize, maxDataCount); | ||
| 145 | + } | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + __aicore__ inline void processZero(GlobalTensor<float> outGm, uint64_t outCount, uint64_t maxDataCount) | ||
| 149 | + { | ||
| 150 | + uint64_t cOutElement = outCount; // currenr out ele | ||
| 151 | + uint64_t totalLoop = cOutElement / maxDataCount; | ||
| 152 | + uint64_t remainOutNum = cOutElement % maxDataCount; | ||
| 153 | + | ||
| 154 | + for (uint32_t i = 0; i < totalLoop; i++) { | ||
| 155 | + DataCopy(outGm[i * maxDataCount], zeroTensor, maxDataCount); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + if (remainOutNum > 0) { | ||
| 159 | + if (remainOutNum * sizeof(float) % BLOCK_BYTE == 0) { | ||
| 160 | + DataCopy(outGm[totalLoop * maxDataCount], zeroTensor, remainOutNum); | ||
| 161 | + } else { | ||
| 162 | + AscendC::DataCopyExtParams copyParams{1, static_cast<uint32_t>(remainOutNum * sizeof(float)), 0, 0, 0}; | ||
| 163 | + AscendC::DataCopyPad(outGm[totalLoop * maxDataCount], zeroTensor, copyParams); | ||
| 164 | + } | ||
| 165 | + } | ||
| 166 | + } | ||
| 167 | +}; | ||
| 168 | + | ||
| 169 | +} // namespace NpuArch::Epilogue::Block | ||
| 170 | + | ||
| 171 | +#endif // CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_FAG_PRE_HPP | ||
| @@ -0,0 +1,46 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 gbsag_epilogue_packet_ub_layout.hpp | ||
| 13 | + * \brief packet softmax ub layout | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef CATLASS_EPILOGUE_BLOCK_PACKET_UB_LAYOUT_HPP | ||
| 17 | +#define CATLASS_EPILOGUE_BLOCK_PACKET_UB_LAYOUT_HPP | ||
| 18 | + | ||
| 19 | +#include <cstdint> | ||
| 20 | + | ||
| 21 | +namespace NpuArch::Epilogue::Block { | ||
| 22 | + | ||
| 23 | +// Keep softmax and prepD buffers in disjoint UB regions. | ||
| 24 | +// | ||
| 25 | +// Softmax uses [0, 104 KiB); prepD uses [104 KiB, 192 KiB). | ||
| 26 | +// CAST storage is sized for 32-row chunks because the full two-buffer | ||
| 27 | +// representation does not fit in the 192 KiB user-addressable UB window. | ||
| 28 | +// | ||
| 29 | +// 注意:本算子 host 侧锁死 headDim=128(EZ1001 参数校验),输入/CAST 按 128 | ||
| 30 | +// 定容;若将来解除该限制,本布局需按新 headDim 重排。 | ||
| 31 | +struct PacketSoftmaxUbLayout { | ||
| 32 | + static constexpr uint64_t MAX_ROWS_PER_AIV = 64; | ||
| 33 | + static constexpr uint64_t INPUT_BUFFER_BYTES = MAX_ROWS_PER_AIV * 128 * 2; // 16KB, fp16 | ||
| 34 | + static constexpr uint64_t CAST_BUFFER_BYTES = (MAX_ROWS_PER_AIV / 2) * 128 * 4; // 16KB, fp32 半行块 | ||
| 35 | + static constexpr uint64_t INPUT_COUNT = 2; | ||
| 36 | + static constexpr uint64_t D_WIDTH = 8; | ||
| 37 | + static constexpr uint64_t D_BYTES = MAX_ROWS_PER_AIV * D_WIDTH * sizeof(float); // 2KB | ||
| 38 | + static constexpr uint64_t PREPD_BASE = 104 * 1024; | ||
| 39 | + static constexpr uint64_t D_OFFSET = PREPD_BASE + (INPUT_BUFFER_BYTES + CAST_BUFFER_BYTES) * INPUT_COUNT; // 168KB | ||
| 40 | + static constexpr uint64_t TEMP_OFFSET = 172 * 1024; | ||
| 41 | + static constexpr uint64_t TEMP_BUFFER_BYTES = 20 * 1024; // front 按此自适应分裂 | ||
| 42 | +}; | ||
| 43 | + | ||
| 44 | +} // namespace NpuArch::Epilogue::Block | ||
| 45 | + | ||
| 46 | +#endif // CATLASS_EPILOGUE_BLOCK_PACKET_UB_LAYOUT_HPP | ||
| @@ -0,0 +1,466 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 gbsag_epilogue_post.hpp | ||
| 13 | + * \brief Block Epliogue Post Kernel Implementation | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_POST_HPP | ||
| 17 | +#define CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_POST_HPP | ||
| 18 | + | ||
| 19 | +#include "../../../attn_infra/arch/gbsag_resource.hpp" | ||
| 20 | +#include "../../../attn_infra/epilogue/gbsag_epilogue_dispatch_policy.hpp" | ||
| 21 | +#include "kernel_operator.h" | ||
| 22 | + | ||
| 23 | +using namespace AscendC; | ||
| 24 | + | ||
| 25 | +namespace NpuArch::Epilogue::Block { | ||
| 26 | + | ||
| 27 | +struct ShapeBnsd { | ||
| 28 | + uint64_t b; | ||
| 29 | + uint64_t n; | ||
| 30 | + uint64_t s; | ||
| 31 | + uint64_t d; | ||
| 32 | +}; | ||
| 33 | + | ||
| 34 | +template <uint32_t INPUT_LAYOUT, typename OutputDtype_, typename UpdateType_, typename InputType_> | ||
| 35 | +class BlockPost { | ||
| 36 | +public: | ||
| 37 | + using DispatchPolicy = EpilogueAtlasA2FAGPre; | ||
| 38 | + using ArchTag = typename DispatchPolicy::ArchTag; | ||
| 39 | + | ||
| 40 | + struct Params { | ||
| 41 | + GM_ADDR dq; | ||
| 42 | + GM_ADDR dk; | ||
| 43 | + GM_ADDR dv; | ||
| 44 | + GM_ADDR dqWrk; | ||
| 45 | + GM_ADDR dkWrk; | ||
| 46 | + GM_ADDR dvWrk; | ||
| 47 | + GM_ADDR tilingData; | ||
| 48 | + GM_ADDR cuSeqQ; | ||
| 49 | + GM_ADDR cuSeqKv; | ||
| 50 | + | ||
| 51 | + // Methods | ||
| 52 | + __aicore__ inline Params() {} | ||
| 53 | + | ||
| 54 | + __aicore__ inline Params(GM_ADDR dq_, GM_ADDR dk_, GM_ADDR dv_, GM_ADDR dqWrk_, GM_ADDR dkWrk_, GM_ADDR dvWrk_, | ||
| 55 | + GM_ADDR tilingData_, GM_ADDR cuSeqQ_, GM_ADDR cuSeqKv_) | ||
| 56 | + : dq(dq_), | ||
| 57 | + dk(dk_), | ||
| 58 | + dv(dv_), | ||
| 59 | + dqWrk(dqWrk_), | ||
| 60 | + dkWrk(dkWrk_), | ||
| 61 | + dvWrk(dvWrk_), | ||
| 62 | + tilingData(tilingData_), | ||
| 63 | + cuSeqQ(cuSeqQ_), | ||
| 64 | + cuSeqKv(cuSeqKv_) | ||
| 65 | + {} | ||
| 66 | + }; | ||
| 67 | + | ||
| 68 | + NpuArch::Arch::Resource<ArchTag> resource; | ||
| 69 | + constexpr static uint32_t BUFFER_NUM = 2; | ||
| 70 | + constexpr static uint64_t INPUT_NUM = 2; | ||
| 71 | + constexpr static uint64_t BNSD = 1; // [B, N, S, D] | ||
| 72 | + constexpr static uint64_t BSND = 2; // [B, S, N, D] | ||
| 73 | + constexpr static uint64_t TND = 0; | ||
| 74 | + constexpr static uint32_t WORKSPACE_NUM_ALIGN = 256; | ||
| 75 | + constexpr static uint32_t BASE_BLOCK_BYTE = 32; | ||
| 76 | + constexpr static int32_t DQ = 1; | ||
| 77 | + constexpr static int32_t DK = -1; | ||
| 78 | + GM_ADDR cuSeqQ; | ||
| 79 | + GM_ADDR cuSeqKv; | ||
| 80 | + constexpr static int32_t DV = -2; | ||
| 81 | + | ||
| 82 | + GlobalTensor<OutputDtype_> dqGm, dkGm, dvGm; | ||
| 83 | + GlobalTensor<float> dqWorkSpaceGm, dkWorkSpaceGm, dvWorkSpaceGm; | ||
| 84 | + LocalTensor<float> input[BUFFER_NUM]; | ||
| 85 | + LocalTensor<OutputDtype_> output[BUFFER_NUM]; | ||
| 86 | + | ||
| 87 | + uint64_t usedCoreNum = 0; | ||
| 88 | + uint64_t cBlockIdx = 0; | ||
| 89 | + float scaleValue = 0; | ||
| 90 | + | ||
| 91 | + uint64_t computeS1 = 0; | ||
| 92 | + uint64_t computeS2 = 0; | ||
| 93 | + uint64_t actualCol = 0; | ||
| 94 | + uint64_t curBatch1 = 0; | ||
| 95 | + uint64_t curBatch2 = 0; // k | ||
| 96 | + uint64_t curN1Idx = 0; // q_n | ||
| 97 | + uint64_t curN2Idx = 0; // k_n | ||
| 98 | + uint64_t curS1Idx = 0; // q_s | ||
| 99 | + uint64_t curS2Idx = 0; // v_s | ||
| 100 | + | ||
| 101 | + uint64_t curT1Idx = 0; | ||
| 102 | + uint64_t transpseQStride = 0; | ||
| 103 | + uint64_t transpseKvStride = 0; | ||
| 104 | + | ||
| 105 | + uint64_t dqOffset = 0; | ||
| 106 | + uint64_t dkvOffset = 0; | ||
| 107 | + | ||
| 108 | + uint64_t ubBaseSize = 0; | ||
| 109 | + uint64_t ubBasePreBufferSize = 0; | ||
| 110 | + uint64_t b = 0; | ||
| 111 | + uint64_t n1 = 0; | ||
| 112 | + uint64_t n2 = 0; | ||
| 113 | + uint64_t s1 = 0; | ||
| 114 | + uint64_t s2 = 0; | ||
| 115 | + uint64_t d = 0; | ||
| 116 | + | ||
| 117 | + __aicore__ inline BlockPost(Params const ¶ms) | ||
| 118 | + { | ||
| 119 | + cBlockIdx = GetBlockIdx(); | ||
| 120 | + __gm__ GenericBlockSparseAttentionGradTilingData *tilingData = | ||
| 121 | + reinterpret_cast<__gm__ GenericBlockSparseAttentionGradTilingData *>(params.tilingData); | ||
| 122 | + usedCoreNum = tilingData->usedVecCoreNum; | ||
| 123 | + if (cBlockIdx >= usedCoreNum) { | ||
| 124 | + return; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + b = tilingData->batch; | ||
| 128 | + n1 = tilingData->numHeads; | ||
| 129 | + n2 = tilingData->kvHeads; | ||
| 130 | + s1 = tilingData->maxQSeqlen; | ||
| 131 | + s2 = tilingData->maxKvSeqlen; | ||
| 132 | + d = tilingData->headDim; | ||
| 133 | + scaleValue = tilingData->scaleValue; | ||
| 134 | + ubBaseSize = tilingData->postUbBaseSize / BUFFER_NUM * BUFFER_NUM / WORKSPACE_NUM_ALIGN * WORKSPACE_NUM_ALIGN; | ||
| 135 | + | ||
| 136 | + uint64_t qPostSize = tilingData->dqSize / d; | ||
| 137 | + uint64_t kvPostSize = tilingData->dkvSize / d; | ||
| 138 | + | ||
| 139 | + uint64_t qPostBlockTotal = qPostSize; // 把d前面合洲,总共的行数 | ||
| 140 | + uint64_t qPostBlockEeachCore = qPostBlockTotal / usedCoreNum; // 每个核处理的行数 | ||
| 141 | + uint64_t qPostBlockNumEeachCore = qPostBlockEeachCore * d; // 每个核处理的元素数量 | ||
| 142 | + uint64_t qPostTailNum = qPostBlockTotal % usedCoreNum; // 剩余的行数,给尾核处理 | ||
| 143 | + | ||
| 144 | + uint64_t kvPostBlockTotal = kvPostSize; // 把d前面合洲,总共的行数 | ||
| 145 | + uint64_t kvPostBlockEeachCore = kvPostBlockTotal / usedCoreNum; // 每个核处理的行数 | ||
| 146 | + uint64_t kvPostBlockNumEeachCore = kvPostBlockEeachCore * d; // 每个核处理的元素数量 | ||
| 147 | + uint64_t kvPostTailNum = kvPostBlockTotal % usedCoreNum; // 剩余的行数,给尾核处理 | ||
| 148 | + | ||
| 149 | + cuSeqQ = params.cuSeqQ; | ||
| 150 | + cuSeqKv = params.cuSeqKv; | ||
| 151 | + if constexpr (INPUT_LAYOUT == TND) { | ||
| 152 | + transpseQStride = (n1 * d - d) * sizeof(OutputDtype_); | ||
| 153 | + transpseKvStride = (n2 * d - d) * sizeof(OutputDtype_); | ||
| 154 | + } else if constexpr (INPUT_LAYOUT == BNSD || INPUT_LAYOUT == BSND) { | ||
| 155 | + transpseQStride = 0; | ||
| 156 | + transpseKvStride = 0; | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + computeS1 = cBlockIdx == usedCoreNum - 1 ? (qPostTailNum + qPostBlockEeachCore) : | ||
| 160 | + qPostBlockEeachCore; // 当前核需要计算的dq的行数 | ||
| 161 | + dqOffset = ((uint64_t)cBlockIdx) * qPostBlockNumEeachCore; // 当前核dq的偏移元素个数 | ||
| 162 | + computeS2 = cBlockIdx == usedCoreNum - 1 ? (kvPostBlockEeachCore + kvPostTailNum) : | ||
| 163 | + kvPostBlockEeachCore; // 当前核需要计算的dkv的行数 | ||
| 164 | + dkvOffset = ((uint64_t)cBlockIdx) * kvPostBlockNumEeachCore; // 当前核dkv的偏移元素个数 | ||
| 165 | + | ||
| 166 | + dqWorkSpaceGm.SetGlobalBuffer((__gm__ float *)params.dqWrk + dqOffset); | ||
| 167 | + dkWorkSpaceGm.SetGlobalBuffer((__gm__ float *)params.dkWrk + dkvOffset); | ||
| 168 | + dvWorkSpaceGm.SetGlobalBuffer((__gm__ float *)params.dvWrk + dkvOffset); | ||
| 169 | + | ||
| 170 | + if constexpr (INPUT_LAYOUT == TND) { | ||
| 171 | + dqGm.SetGlobalBuffer((__gm__ OutputDtype_ *)params.dq + dqOffset); | ||
| 172 | + dkGm.SetGlobalBuffer((__gm__ OutputDtype_ *)params.dk + dkvOffset); | ||
| 173 | + dvGm.SetGlobalBuffer((__gm__ OutputDtype_ *)params.dv + dkvOffset); | ||
| 174 | + } else if constexpr (INPUT_LAYOUT == BNSD || INPUT_LAYOUT == BSND) { | ||
| 175 | + dqGm.SetGlobalBuffer((__gm__ OutputDtype_ *)params.dq); | ||
| 176 | + dkGm.SetGlobalBuffer((__gm__ OutputDtype_ *)params.dk); | ||
| 177 | + dvGm.SetGlobalBuffer((__gm__ OutputDtype_ *)params.dv); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + ubBasePreBufferSize = ubBaseSize / BUFFER_NUM / BASE_BLOCK_BYTE * BASE_BLOCK_BYTE; // double buffer | ||
| 181 | + for (uint64_t i = 0; i < BUFFER_NUM; i++) { | ||
| 182 | + input[i] = resource.ubBuf.template GetBufferByByte<float>(ubBasePreBufferSize * 3 * i); | ||
| 183 | + output[i] = resource.ubBuf.template GetBufferByByte<OutputDtype_>(ubBasePreBufferSize * 2 + | ||
| 184 | + ubBasePreBufferSize * 3 * i); | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + // 获取当前核的dq dk dv 对应的索引序号 | ||
| 188 | + struct ShapeBnsd qShape { | ||
| 189 | + b, n1, s1, d | ||
| 190 | + }; | ||
| 191 | + InitIndex(dqOffset, curS1Idx, cuSeqQ, curBatch1, curN1Idx, curS1Idx, qShape); | ||
| 192 | + struct ShapeBnsd kvShape { | ||
| 193 | + b, n2, s2, d | ||
| 194 | + }; | ||
| 195 | + InitIndex(dkvOffset, curS2Idx, cuSeqKv, curBatch2, curN2Idx, curS2Idx, kvShape); | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + /* | ||
| 199 | + * 根据当前 Core 的起始偏移量(startIdx),计算要处理的维度索引(bIdx/nIdx/sIdx); | ||
| 200 | + * | ||
| 201 | + * startIdx : input 元素开始序号 | ||
| 202 | + * curS: 当前batch的seqlen, 主要要针对tnd格式,s不等场景 | ||
| 203 | + * seqS: actual seqlen list, s 在list是累加的,例如s1 2, s2 3, s3 10, seqS[0, 2, 5, 15] | ||
| 204 | + * bIdx, nIdx, sIdx : 当前元素索引 | ||
| 205 | + * shape : 矩阵的shape | ||
| 206 | + */ | ||
| 207 | + __aicore__ inline uint64_t GetSeqLen(GM_ADDR cuS, uint64_t batchIdx) const | ||
| 208 | + { | ||
| 209 | + return static_cast<uint64_t>(((__gm__ int64_t *)cuS)[batchIdx + 1] - ((__gm__ int64_t *)cuS)[batchIdx]); | ||
| 210 | + } | ||
| 211 | + | ||
| 212 | + __aicore__ inline uint64_t GetSeqPrefix(GM_ADDR cuS, uint64_t batchIdx) const | ||
| 213 | + { | ||
| 214 | + return static_cast<uint64_t>(((__gm__ int64_t *)cuS)[batchIdx]); | ||
| 215 | + } | ||
| 216 | + __aicore__ inline void InitIndex(uint64_t startIdx, uint64_t &curS, GM_ADDR cuS, uint64_t &bIdx, uint64_t &nIdx, | ||
| 217 | + uint64_t &sIdx, struct ShapeBnsd shape) | ||
| 218 | + { | ||
| 219 | + if constexpr (INPUT_LAYOUT == TND) { | ||
| 220 | + // batch 边界直接取 cuSeq 前缀和(cuS[b+1]*n*d):每轮仅一次 GM 读, | ||
| 221 | + // 免差分、免累加(原实现每轮双 GM 读 + prefixSum 累加)。 | ||
| 222 | + // 变长 batch 的定位无法闭式,线性搜索已是最简;b 较大时可再改二分。 | ||
| 223 | + const __gm__ int64_t *cu = (__gm__ int64_t *)cuS; | ||
| 224 | + for (uint64_t bDimIdx = 0; bDimIdx < shape.b; bDimIdx++) { | ||
| 225 | + const uint64_t batchEndElements = static_cast<uint64_t>(cu[bDimIdx + 1]) * shape.n * shape.d; | ||
| 226 | + if (startIdx < batchEndElements) { | ||
| 227 | + bIdx = bDimIdx; | ||
| 228 | + curS = static_cast<uint64_t>(cu[bDimIdx + 1] - cu[bDimIdx]); | ||
| 229 | + | ||
| 230 | + const uint64_t bTail = startIdx - static_cast<uint64_t>(cu[bDimIdx]) * shape.n * shape.d; | ||
| 231 | + nIdx = bTail / (curS * shape.d); | ||
| 232 | + const uint64_t nTail = bTail % (curS * shape.d); | ||
| 233 | + sIdx = nTail / shape.d; | ||
| 234 | + break; | ||
| 235 | + } | ||
| 236 | + } | ||
| 237 | + } else if constexpr (INPUT_LAYOUT == BSND) { | ||
| 238 | + bIdx = startIdx / (shape.s * shape.n * shape.d); | ||
| 239 | + uint64_t bTail = startIdx % (shape.s * shape.n * shape.d); | ||
| 240 | + sIdx = bTail / (shape.n * shape.d); | ||
| 241 | + nIdx = (bTail % (shape.n * shape.d)) / shape.d; | ||
| 242 | + } else { | ||
| 243 | + bIdx = startIdx / (shape.n * shape.s * shape.d); | ||
| 244 | + uint64_t bTail = startIdx % (shape.n * shape.s * shape.d); | ||
| 245 | + nIdx = bTail / (shape.s * shape.d); | ||
| 246 | + uint64_t nTail = bTail % (shape.s * shape.d); | ||
| 247 | + sIdx = nTail / shape.d; | ||
| 248 | + } | ||
| 249 | + } | ||
| 250 | + | ||
| 251 | + __aicore__ inline ~BlockPost() {} | ||
| 252 | + | ||
| 253 | + template <int32_t CORE_TYPE = g_coreType> | ||
| 254 | + __aicore__ inline void operator()(); | ||
| 255 | + | ||
| 256 | + template <> | ||
| 257 | + __aicore__ inline void operator()<AscendC::AIC>() | ||
| 258 | + {} | ||
| 259 | + | ||
| 260 | + template <> | ||
| 261 | + __aicore__ inline void operator()<AscendC::AIV>() | ||
| 262 | + { | ||
| 263 | + if (cBlockIdx >= usedCoreNum) { | ||
| 264 | + return; | ||
| 265 | + } | ||
| 266 | + Process(); | ||
| 267 | + } | ||
| 268 | + | ||
| 269 | + /* | ||
| 270 | + * 求每个当前core 起始元素所在位置 b n s | ||
| 271 | + * | ||
| 272 | + GM_ADDR cuS = (qkvFlag > 0) ? cuSeqQ : cuSeqKv; | ||
| 273 | + * leftNburst : 当前需要搬运的s的行数 | ||
| 274 | + * curS: 当前batch的seqlen, 主要要针对tnd格式,s不等场景 | ||
| 275 | + * seqS: actual seqlen list, s 在list是累加的,例如s1 2, s2 3, s3 10, seqS[0, 2, 5, 15] | ||
| 276 | + */ | ||
| 277 | + __aicore__ inline void CopyOutPost(uint64_t leftNburst, LocalTensor<OutputDtype_> output, int32_t qkvFlag) | ||
| 278 | + { | ||
| 279 | + uint64_t n = (qkvFlag > 0) ? n1 : n2; | ||
| 280 | + uint64_t s = (qkvFlag > 0) ? s1 : s2; | ||
| 281 | + uint64_t dstOffset = 0; | ||
| 282 | + uint64_t &nIdx = (qkvFlag > 0) ? curN1Idx : curN2Idx; | ||
| 283 | + GM_ADDR cuS = (qkvFlag > 0) ? cuSeqQ : cuSeqKv; | ||
| 284 | + uint64_t &sIdx = (qkvFlag > 0) ? curS1Idx : curS2Idx; | ||
| 285 | + uint64_t &bIdx = (qkvFlag > 0) ? curBatch1 : curBatch2; | ||
| 286 | + | ||
| 287 | + // 要兼容BNSD没有seqList的情况,得从tiling传个参数过来 | ||
| 288 | + uint64_t curS = s; | ||
| 289 | + uint64_t count = 0; | ||
| 290 | + | ||
| 291 | + if constexpr (INPUT_LAYOUT == BSND) { | ||
| 292 | + while (leftNburst > 0) { | ||
| 293 | + Copy2Out(1, output, dstOffset, qkvFlag); | ||
| 294 | + dstOffset += d; | ||
| 295 | + --leftNburst; | ||
| 296 | + ++nIdx; | ||
| 297 | + if (nIdx == n) { | ||
| 298 | + nIdx = 0; | ||
| 299 | + ++sIdx; | ||
| 300 | + if (sIdx == s) { | ||
| 301 | + sIdx = 0; | ||
| 302 | + ++bIdx; | ||
| 303 | + } | ||
| 304 | + } | ||
| 305 | + } | ||
| 306 | + return; | ||
| 307 | + } | ||
| 308 | + | ||
| 309 | + while (leftNburst > 0) { // 当前leftNburst 搬运量不会到下一个n | ||
| 310 | + uint64_t curNburst = 0; | ||
| 311 | + count++; | ||
| 312 | + if (curS - sIdx < leftNburst) { // 需要借N或借B | ||
| 313 | + curNburst = curS - sIdx; | ||
| 314 | + Copy2Out(curNburst, output, dstOffset, qkvFlag); | ||
| 315 | + leftNburst = leftNburst - curNburst; | ||
| 316 | + sIdx = 0; | ||
| 317 | + if (nIdx < n - 1) { // 需要借N | ||
| 318 | + nIdx += 1; | ||
| 319 | + } else { | ||
| 320 | + nIdx = 0; | ||
| 321 | + if (bIdx < b - 1) { // 需要借B | ||
| 322 | + bIdx += 1; | ||
| 323 | + if constexpr (INPUT_LAYOUT == TND) { | ||
| 324 | + curS = GetSeqLen(cuS, bIdx); | ||
| 325 | + } else { | ||
| 326 | + curS = s; | ||
| 327 | + } | ||
| 328 | + } else { // 没有轴可以借了,end | ||
| 329 | + leftNburst = 0; | ||
| 330 | + } | ||
| 331 | + } | ||
| 332 | + } else { // 当前leftNburst 搬运量不会到下一个n | ||
| 333 | + curNburst = leftNburst; | ||
| 334 | + Copy2Out(curNburst, output, dstOffset, qkvFlag); | ||
| 335 | + sIdx = sIdx + leftNburst; | ||
| 336 | + leftNburst = 0; | ||
| 337 | + } | ||
| 338 | + dstOffset = dstOffset + curNburst * d; | ||
| 339 | + } | ||
| 340 | + } | ||
| 341 | + | ||
| 342 | + __aicore__ inline void Copy2Out(uint64_t sCount, LocalTensor<OutputDtype_> output, uint64_t srcOffset, | ||
| 343 | + int32_t qkvFlag) | ||
| 344 | + { | ||
| 345 | + uint64_t n = (qkvFlag > 0) ? n1 : n2; | ||
| 346 | + uint64_t nIdx = (qkvFlag > 0) ? curN1Idx : curN2Idx; | ||
| 347 | + GM_ADDR cuLen = (qkvFlag > 0) ? cuSeqQ : cuSeqKv; | ||
| 348 | + uint64_t s = (qkvFlag > 0) ? s1 : s2; | ||
| 349 | + uint64_t curS = (qkvFlag > 0) ? curS1Idx : curS2Idx; | ||
| 350 | + uint64_t curBatch = (qkvFlag > 0) ? curBatch1 : curBatch2; | ||
| 351 | + GlobalTensor<OutputDtype_> outGm = dqGm; | ||
| 352 | + uint64_t transpseStride = (qkvFlag > 0) ? transpseQStride : transpseKvStride; | ||
| 353 | + if (qkvFlag == DK) { | ||
| 354 | + outGm = dkGm; | ||
| 355 | + } else if (qkvFlag == DV) { | ||
| 356 | + outGm = dvGm; | ||
| 357 | + } | ||
| 358 | + | ||
| 359 | + uint64_t bOffset = 0; | ||
| 360 | + uint64_t outOffset = 0; | ||
| 361 | + if constexpr (INPUT_LAYOUT == TND) { | ||
| 362 | + // Compute the prefix only for the current output batch. | ||
| 363 | + uint64_t prefixSum = GetSeqPrefix(cuLen, curBatch); | ||
| 364 | + bOffset = prefixSum * n * d; | ||
| 365 | + outOffset = bOffset + (curS * n + nIdx) * d; | ||
| 366 | + } else if constexpr (INPUT_LAYOUT == BSND) { | ||
| 367 | + outOffset = curBatch * (s * n * d) + curS * (n * d) + nIdx * d; | ||
| 368 | + } else { | ||
| 369 | + outOffset = curBatch * (n * s * d) + nIdx * (s * d) + curS * d; | ||
| 370 | + } | ||
| 371 | + DataCopyExtParams copyParams{static_cast<uint16_t>(sCount), static_cast<uint32_t>(d * sizeof(OutputDtype_)), 0, | ||
| 372 | + static_cast<uint32_t>(transpseStride / BASE_BLOCK_BYTE), | ||
| 373 | + 0}; // 结构体DataCopyExtParams最后一个参数是rsv保留位 | ||
| 374 | + DataCopyPad(outGm[outOffset], output[srcOffset], copyParams); | ||
| 375 | + } | ||
| 376 | + | ||
| 377 | + __aicore__ inline void ProcessOut(uint64_t conputeS, int32_t qkvFlag) | ||
| 378 | + { | ||
| 379 | + uint64_t ubBaseSizeNum = ubBasePreBufferSize / sizeof(OutputDtype_); // 一块buffer处理的元素数量 | ||
| 380 | + uint64_t singleLoopSCount = ubBaseSizeNum / d; | ||
| 381 | + uint64_t loopTimes = static_cast<uint64_t>(CeilDiv(conputeS, singleLoopSCount)); | ||
| 382 | + uint64_t tailS = conputeS % singleLoopSCount; | ||
| 383 | + uint64_t curSIdx = (qkvFlag > 0) ? curS1Idx : curS2Idx; | ||
| 384 | + uint64_t curBatchIdx = (qkvFlag > 0) ? curBatch1 : curBatch2; | ||
| 385 | + GlobalTensor<float> inGm = dqWorkSpaceGm; | ||
| 386 | + if (qkvFlag == -1) { | ||
| 387 | + inGm = dkWorkSpaceGm; | ||
| 388 | + } else if (qkvFlag == -2) { | ||
| 389 | + inGm = dvWorkSpaceGm; | ||
| 390 | + } | ||
| 391 | + | ||
| 392 | + int64_t ping = 0; | ||
| 393 | + set_flag(PIPE_MTE3, PIPE_MTE2, EVENT_ID0); | ||
| 394 | + set_flag(PIPE_MTE3, PIPE_MTE2, EVENT_ID1); | ||
| 395 | + for (uint64_t i = 0; i < loopTimes; i++) { | ||
| 396 | + auto event_id = ping ? EVENT_ID0 : EVENT_ID1; | ||
| 397 | + uint64_t sCount = singleLoopSCount; | ||
| 398 | + uint64_t totalSCout = i * singleLoopSCount; | ||
| 399 | + uint64_t gmOffset = totalSCout * d; | ||
| 400 | + if (i == loopTimes - 1 && tailS != 0) { | ||
| 401 | + sCount = tailS; | ||
| 402 | + } | ||
| 403 | + | ||
| 404 | + wait_flag(PIPE_MTE3, PIPE_MTE2, event_id); | ||
| 405 | + | ||
| 406 | + DataCopy(input[ping], inGm[gmOffset], sCount * d); // d 为32b 对齐场景 | ||
| 407 | + | ||
| 408 | + set_flag(PIPE_MTE2, PIPE_V, event_id); | ||
| 409 | + wait_flag(PIPE_MTE2, PIPE_V, event_id); | ||
| 410 | + | ||
| 411 | + if (qkvFlag != DV) { | ||
| 412 | + Muls(input[ping], input[ping], (float)scaleValue, sCount * d); | ||
| 413 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 414 | + } | ||
| 415 | + | ||
| 416 | + AscendC::LocalTensor<float> srcLocal = input[ping]; | ||
| 417 | + AscendC::LocalTensor<OutputDtype_> dstLocal = output[ping]; | ||
| 418 | + Cast(dstLocal, srcLocal, AscendC::RoundMode::CAST_ROUND, sCount * d); | ||
| 419 | + | ||
| 420 | + set_flag(PIPE_V, PIPE_MTE3, event_id); | ||
| 421 | + wait_flag(PIPE_V, PIPE_MTE3, event_id); | ||
| 422 | + | ||
| 423 | + if constexpr (INPUT_LAYOUT == TND) { | ||
| 424 | + GlobalTensor<OutputDtype_> outGm = dqGm; | ||
| 425 | + if (qkvFlag == DK) { | ||
| 426 | + outGm = dkGm; | ||
| 427 | + } else if (qkvFlag == DV) { | ||
| 428 | + outGm = dvGm; | ||
| 429 | + } | ||
| 430 | + DataCopy(outGm[gmOffset], dstLocal, sCount * d); | ||
| 431 | + } else { | ||
| 432 | + CopyOutPost(sCount, output[ping], qkvFlag); | ||
| 433 | + } | ||
| 434 | + | ||
| 435 | + set_flag(PIPE_MTE3, PIPE_MTE2, event_id); | ||
| 436 | + | ||
| 437 | + if (BUFFER_NUM == 2) { | ||
| 438 | + ping = 1 - ping; | ||
| 439 | + } | ||
| 440 | + } | ||
| 441 | + wait_flag(PIPE_MTE3, PIPE_MTE2, EVENT_ID0); | ||
| 442 | + wait_flag(PIPE_MTE3, PIPE_MTE2, EVENT_ID1); | ||
| 443 | + } | ||
| 444 | + | ||
| 445 | + __aicore__ inline void Process() | ||
| 446 | + { | ||
| 447 | + // dq | ||
| 448 | + ProcessOut(computeS1, DQ); | ||
| 449 | + // dk | ||
| 450 | + ProcessOut(computeS2, DK); | ||
| 451 | + // dv | ||
| 452 | + // 重新初始化索引 | ||
| 453 | + curS2Idx = 0; | ||
| 454 | + curBatch2 = 0; | ||
| 455 | + curN2Idx = 0; | ||
| 456 | + struct ShapeBnsd kvShape { | ||
| 457 | + b, n2, s2, d | ||
| 458 | + }; | ||
| 459 | + InitIndex(dkvOffset, curS2Idx, cuSeqKv, curBatch2, curN2Idx, curS2Idx, kvShape); | ||
| 460 | + ProcessOut(computeS2, DV); | ||
| 461 | + } | ||
| 462 | +}; | ||
| 463 | + | ||
| 464 | +} // namespace NpuArch::Epilogue::Block | ||
| 465 | + | ||
| 466 | +#endif // CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_POST_HPP | ||
| @@ -0,0 +1,149 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 gbsag_epilogue_softmaxgrad.hpp | ||
| 13 | + * \brief Packet-local SoftmaxGrad implementation for GBSAG K_OUT. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_SOFTAXGRAD_HPP | ||
| 17 | +#define CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_SOFTAXGRAD_HPP | ||
| 18 | + | ||
| 19 | +#include "../../../attn_infra/arch/gbsag_resource.hpp" | ||
| 20 | +#include "../../../attn_infra/epilogue/gbsag_epilogue_dispatch_policy.hpp" | ||
| 21 | +#include "gbsag_epilogue_packet_ub_layout.hpp" | ||
| 22 | +#include "kernel_operator.h" | ||
| 23 | + | ||
| 24 | +using namespace AscendC; | ||
| 25 | + | ||
| 26 | +namespace NpuArch::Epilogue::Block { | ||
| 27 | + | ||
| 28 | +template <typename InputDType, typename OutputDtype, uint32_t INPUT_LAYOUT> | ||
| 29 | +class SoftmaxGrad { | ||
| 30 | +public: | ||
| 31 | + using DispatchPolicy = EpilogueAtlasA2FAGPre; | ||
| 32 | + using ArchTag = typename DispatchPolicy::ArchTag; | ||
| 33 | + | ||
| 34 | + static constexpr uint64_t BLOCK_BYTE_SIZE = 32; | ||
| 35 | + static constexpr uint64_t BLOCK_SIZE = BLOCK_BYTE_SIZE / sizeof(float); | ||
| 36 | + static constexpr uint64_t SFMG_HIGH_PERF_N_FACTOR = 8; | ||
| 37 | + static constexpr uint64_t SFMG_HIGH_PERF_D_FACTOR = 64; | ||
| 38 | + static constexpr uint64_t STAGES = 1; | ||
| 39 | + static constexpr uint64_t INPUT_NUM = PacketSoftmaxUbLayout::INPUT_COUNT; | ||
| 40 | + static constexpr uint64_t INPUT_BUFFER_LEN = PacketSoftmaxUbLayout::INPUT_BUFFER_BYTES; | ||
| 41 | + static constexpr uint64_t CAST_BUFFER_LEN = PacketSoftmaxUbLayout::CAST_BUFFER_BYTES; | ||
| 42 | + static constexpr uint64_t OUTPUT_BUFFER_LEN = PacketSoftmaxUbLayout::D_BYTES; | ||
| 43 | + | ||
| 44 | + __aicore__ inline explicit SoftmaxGrad(NpuArch::Arch::Resource<ArchTag> &resource_) | ||
| 45 | + : resource(resource_) | ||
| 46 | + { | ||
| 47 | + // prepD 整体迁入 [PREPD_BASE, 192K) 私有区,与 | ||
| 48 | + // softmax 的 [0,104K) 区域彻底分离(布局见 PacketSoftmaxUbLayout)。 | ||
| 49 | + // 输入按 headDim=128 全量定容(host 侧 EZ1001 锁死 128); | ||
| 50 | + // CAST 按半行块(32 行)定容,ProcessPacket 分块处理。 | ||
| 51 | + constexpr uint64_t inputBufferLenEachStage = INPUT_BUFFER_LEN / STAGES; | ||
| 52 | + constexpr uint64_t castBufferLenEachStage = CAST_BUFFER_LEN / STAGES; | ||
| 53 | + constexpr uint64_t outputBufferLenEachStage = OUTPUT_BUFFER_LEN / STAGES; | ||
| 54 | + for (uint64_t i = 0; i < STAGES; ++i) { | ||
| 55 | + doutTensor[i] = resource.ubBuf.template GetBufferByByte<InputDType>(PacketSoftmaxUbLayout::PREPD_BASE + | ||
| 56 | + inputBufferLenEachStage * i); | ||
| 57 | + outTensor[i] = resource.ubBuf.template GetBufferByByte<InputDType>( | ||
| 58 | + PacketSoftmaxUbLayout::PREPD_BASE + INPUT_BUFFER_LEN + inputBufferLenEachStage * i); | ||
| 59 | + doutFp32Tensor[i] = resource.ubBuf.template GetBufferByByte<float>( | ||
| 60 | + PacketSoftmaxUbLayout::PREPD_BASE + INPUT_BUFFER_LEN * INPUT_NUM + castBufferLenEachStage * i); | ||
| 61 | + outFp32Tensor[i] = resource.ubBuf.template GetBufferByByte<float>( | ||
| 62 | + PacketSoftmaxUbLayout::PREPD_BASE + INPUT_BUFFER_LEN * INPUT_NUM + CAST_BUFFER_LEN + | ||
| 63 | + castBufferLenEachStage * i); | ||
| 64 | + softmaxGradTensor[i] = resource.ubBuf.template GetBufferByByte<float>(PacketSoftmaxUbLayout::D_OFFSET + | ||
| 65 | + outputBufferLenEachStage * i); | ||
| 66 | + } | ||
| 67 | + tempBuffer = resource.ubBuf.template GetBufferByByte<uint8_t>(PacketSoftmaxUbLayout::TEMP_OFFSET); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + __aicore__ inline void ProcessPacket(GlobalTensor<InputDType> doutPack, GlobalTensor<InputDType> outPack, | ||
| 71 | + uint32_t rows, uint32_t headDim, GM_ADDR tilingDataAddr) | ||
| 72 | + { | ||
| 73 | + if (rows == 0) { | ||
| 74 | + return; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + GET_TILING_DATA_WITH_STRUCT(GenericBlockSparseAttentionGradTilingData, tilingData, tilingDataAddr); | ||
| 78 | + constexpr uint32_t ping = 0; | ||
| 79 | + auto eventId = EVENT_ID2; | ||
| 80 | + const uint64_t calcSize = static_cast<uint64_t>(rows) * headDim; | ||
| 81 | + | ||
| 82 | + // prepD 私有区([PREPD_BASE,192K),见 PacketSoftmaxUbLayout)。 | ||
| 83 | + // 此处 MTE3→MTE2 等待是 pack GM 的 RAW 闸(GatherOut 刚写完 pack), | ||
| 84 | + // 与 UB 分区无关,必须保留。 | ||
| 85 | + set_flag(PIPE_MTE3, PIPE_MTE2, eventId); | ||
| 86 | + wait_flag(PIPE_MTE3, PIPE_MTE2, eventId); | ||
| 87 | + | ||
| 88 | + DataCopy(doutTensor[ping], doutPack, calcSize); | ||
| 89 | + DataCopy(outTensor[ping], outPack, calcSize); | ||
| 90 | + | ||
| 91 | + set_flag(PIPE_MTE2, PIPE_V, eventId); | ||
| 92 | + wait_flag(PIPE_MTE2, PIPE_V, eventId); | ||
| 93 | + | ||
| 94 | + // 192K UB 窗口放不下全量 CAST(2×32K),CAST 区按半行块 | ||
| 95 | + // (CAST_BUFFER_BYTES=16K → 32 行)定容,64 行拆块逐块 cast + front。 | ||
| 96 | + // 逐行规约(D=Σ_dout·out)行独立,分块结果与全量逐位一致。 | ||
| 97 | + constexpr uint32_t CAST_CHUNK_ROWS = static_cast<uint32_t>(CAST_BUFFER_LEN / (128 * sizeof(float))); // 32 | ||
| 98 | + uint32_t rowBegin = 0; | ||
| 99 | + while (rowBegin < rows) { | ||
| 100 | + uint32_t chunk = rows - rowBegin; | ||
| 101 | + if (chunk > CAST_CHUNK_ROWS) { | ||
| 102 | + // 非末块按 8 行对齐,尽量保住 front 的 basicBlock 高性能路径。 | ||
| 103 | + chunk = CAST_CHUNK_ROWS / SFMG_HIGH_PERF_N_FACTOR * SFMG_HIGH_PERF_N_FACTOR; | ||
| 104 | + } | ||
| 105 | + const uint32_t calcChunk = chunk * headDim; | ||
| 106 | + const uint32_t outChunk = chunk * BLOCK_SIZE; | ||
| 107 | + | ||
| 108 | + Cast(doutFp32Tensor[ping], doutTensor[ping][rowBegin * headDim], RoundMode::CAST_NONE, calcChunk); | ||
| 109 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 110 | + Cast(outFp32Tensor[ping], outTensor[ping][rowBegin * headDim], RoundMode::CAST_NONE, calcChunk); | ||
| 111 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 112 | + | ||
| 113 | + LocalTensor<float> dChunk = softmaxGradTensor[ping][rowBegin * BLOCK_SIZE]; | ||
| 114 | + Duplicate<float>(dChunk, 0.0f, outChunk); | ||
| 115 | + AscendC::PipeBarrier<PIPE_V>(); | ||
| 116 | + | ||
| 117 | + uint32_t inputShape[] = {chunk, headDim}; | ||
| 118 | + doutFp32Tensor[ping].SetShapeInfo(ShapeInfo(2, inputShape, DataFormat::ND)); | ||
| 119 | + outFp32Tensor[ping].SetShapeInfo(ShapeInfo(2, inputShape, DataFormat::ND)); | ||
| 120 | + uint32_t outputShape[] = {chunk, static_cast<uint32_t>(BLOCK_SIZE)}; | ||
| 121 | + dChunk.SetShapeInfo(ShapeInfo(2, outputShape, DataFormat::ND)); | ||
| 122 | + | ||
| 123 | + const bool isBasicBlock = chunk % SFMG_HIGH_PERF_N_FACTOR == 0 && headDim % SFMG_HIGH_PERF_D_FACTOR == 0; | ||
| 124 | + if (likely(isBasicBlock)) { | ||
| 125 | + SoftmaxGradFront<float, true>(dChunk, doutFp32Tensor[ping], outFp32Tensor[ping], tempBuffer, | ||
| 126 | + tilingData.softmaxGradTilingData); | ||
| 127 | + } else { | ||
| 128 | + SoftmaxGradFront<float, false>(dChunk, doutFp32Tensor[ping], outFp32Tensor[ping], tempBuffer, | ||
| 129 | + tilingData.softmaxGradTilingData); | ||
| 130 | + } | ||
| 131 | + rowBegin += chunk; | ||
| 132 | + } | ||
| 133 | + // D 交接:front 写 D(V)→ CalDs 读 D(V),V 按序流水天然安全,无需屏障。 | ||
| 134 | + // 后续 softmax 的 MTE2 载入与本私有区无任何 RAW/WAR(分区分离)。 | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | +private: | ||
| 138 | + NpuArch::Arch::Resource<ArchTag> &resource; | ||
| 139 | + LocalTensor<InputDType> doutTensor[STAGES]; | ||
| 140 | + LocalTensor<InputDType> outTensor[STAGES]; | ||
| 141 | + LocalTensor<float> doutFp32Tensor[STAGES]; | ||
| 142 | + LocalTensor<float> outFp32Tensor[STAGES]; | ||
| 143 | + LocalTensor<float> softmaxGradTensor[STAGES]; | ||
| 144 | + LocalTensor<uint8_t> tempBuffer; | ||
| 145 | +}; | ||
| 146 | + | ||
| 147 | +} // namespace NpuArch::Epilogue::Block | ||
| 148 | + | ||
| 149 | +#endif // CATLASS_EPILOGUE_BLOCK_GBSAG_EPILOGUE_SOFTAXGRAD_HPP | ||
| @@ -0,0 +1,612 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 gbsag_gather_and_scatter.hpp | ||
| 13 | + * \brief Gbsag Gather and Scatterx Kernel Implementation | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef GBSAG_EPILOGUE_GBSAG_GATHER_AND_SCATTER_HPP | ||
| 17 | +#define GBSAG_EPILOGUE_GBSAG_GATHER_AND_SCATTER_HPP | ||
| 18 | + | ||
| 19 | +#include "../../../attn_infra/arch/gbsag_resource.hpp" | ||
| 20 | +#include "kernel_operator.h" | ||
| 21 | + | ||
| 22 | +namespace GBSAG { | ||
| 23 | + | ||
| 24 | +// 单个 Packet 最多聚合的有效 Q 行数,与 Cube M 方向上限保持一致。 | ||
| 25 | +constexpr uint32_t Q_PACKET_AGGREGATE_M = 128; | ||
| 26 | +// Packet 内最多记录的 tile 数量;tile 至少 1 行,因此不超过 128。 | ||
| 27 | +constexpr uint32_t Q_PACKET_MAX_SEGMENTS = 128; | ||
| 28 | + | ||
| 29 | +/** | ||
| 30 | + * @brief tile 行数:G≤128 时一个 tile 覆盖整个 GQA group;G>128 时按 128 head 分片。 | ||
| 31 | + */ | ||
| 32 | +__aicore__ inline uint32_t GetQPacketTileRows(uint32_t groupSize) | ||
| 33 | +{ | ||
| 34 | + return groupSize < Q_PACKET_AGGREGATE_M ? groupSize : Q_PACKET_AGGREGATE_M; | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +/** | ||
| 38 | + * @brief tileRows 的 log2 值;非 2 的幂(G=3/5 等)返回 0xFF 表示走除法回退。 | ||
| 39 | + * | ||
| 40 | + * AIV 标量核无硬件除法,`/ tileRows` 会展开为软件例程(数十周期 + 调用开销); | ||
| 41 | + * 逐拍求段号时 2 的幂 tileRows(G∈{1,2,4,8,...,128},全部 perf 档)必须用移位。 | ||
| 42 | + */ | ||
| 43 | +__aicore__ inline uint32_t GetQPacketTileShift(uint32_t tileRows) | ||
| 44 | +{ | ||
| 45 | + if ((tileRows & (tileRows - 1)) != 0) { | ||
| 46 | + return 0xFF; | ||
| 47 | + } | ||
| 48 | + uint32_t shift = 0; | ||
| 49 | + while ((1u << shift) < tileRows) { | ||
| 50 | + ++shift; | ||
| 51 | + } | ||
| 52 | + return shift; | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +/** | ||
| 56 | + * @brief 段号计算:shift 有效时移位,否则除法回退(与 v / tileRows 严格等价)。 | ||
| 57 | + */ | ||
| 58 | +__aicore__ inline uint32_t SegIndexByTileRows(uint32_t v, uint32_t tileRows, uint32_t tileShift) | ||
| 59 | +{ | ||
| 60 | + return tileShift != 0xFF ? (v >> tileShift) : v / tileRows; | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +/** | ||
| 64 | + * @brief 每 Packet 的 tile 数(整 token 切分:floor(128 / tileRows),恒 >= 1)。 | ||
| 65 | + */ | ||
| 66 | +__aicore__ inline uint32_t GetQPacketTilesPerPacket(uint32_t groupSize) | ||
| 67 | +{ | ||
| 68 | + return Q_PACKET_AGGREGATE_M / GetQPacketTileRows(groupSize); | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +/** | ||
| 72 | + * @brief 组聚合 Packet 的精简元数据。 | ||
| 73 | + * | ||
| 74 | + * 整 token 切分下 tile 布局恒均匀,消费端按闭式推导: | ||
| 75 | + * packStart[s] = s * tileRows | ||
| 76 | + * segmentRows[s] = tileRows | ||
| 77 | + * headStart[s] = headStart0(G≤128 恒 0;G>128 为 head 分片偏移) | ||
| 78 | + * 仅 token 号需要数组存储(qStart,AIV 侧顺序填充);AIC 只读标量字段。 | ||
| 79 | + */ | ||
| 80 | +struct QPacket { | ||
| 81 | + uint32_t rows; // 当前 Packet 的实际有效行数(= tileCount * tileRows)。 | ||
| 82 | + uint32_t tileCount; // 当前 Packet 包含的 tile 数量。 | ||
| 83 | + uint32_t headStart0; // tile 首 head 偏移(G>128 分片时非 0)。 | ||
| 84 | + uint32_t qStart[Q_PACKET_MAX_SEGMENTS]; // tile 的 token 号(Q 序列内行号)。 | ||
| 85 | +}; | ||
| 86 | + | ||
| 87 | +/** | ||
| 88 | + * @brief 当前 AIV 在一个 Packet 内负责的连续行范围。 | ||
| 89 | + */ | ||
| 90 | +struct QPacketAivRange { | ||
| 91 | + uint32_t begin; // 本 AIV 负责的 Packet 起始行,包含。 | ||
| 92 | + uint32_t end; // 本 AIV 负责的 Packet 结束行,不包含。 | ||
| 93 | + uint32_t rows; // 本 AIV 实际负责的行数。 | ||
| 94 | +}; | ||
| 95 | + | ||
| 96 | +/** | ||
| 97 | + * @brief 将 Packet 行沿 M 方向分配给一对 AIV。 | ||
| 98 | + * @param packetRows 当前 Packet 的实际有效行数。 | ||
| 99 | + * @return 当前 AIV 对应的半开行区间。 | ||
| 100 | + */ | ||
| @@ -0,0 +1,46 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_EPILOGUE_DISPATCH_POLICY_HPP | ||
| 12 | +#define GBSAG_EPILOGUE_DISPATCH_POLICY_HPP | ||
| 13 | + | ||
| 14 | +#include "../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | +#include "../../attn_infra/arch/gbsag_arch.hpp" | ||
| 16 | + | ||
| 17 | +namespace NpuArch::Epilogue { | ||
| 18 | + | ||
| 19 | +enum class LseMode { | ||
| 20 | + NONE = 0, | ||
| 21 | + OUT_ONLY = 1 | ||
| 22 | +}; | ||
| 23 | +// For AtlasA2, FA Infer online Softmax | ||
| 24 | +template <LseMode LSE_MODE_, typename SM_DTYPE_> | ||
| 25 | +struct EpilogueAtlasA2OnlineSoftmax { | ||
| 26 | + using ArchTag = Arch::AtlasA2; | ||
| 27 | + using IntermPrec = SM_DTYPE_; | ||
| 28 | + static constexpr LseMode LSE_MODE = LSE_MODE_; | ||
| 29 | +}; | ||
| 30 | + | ||
| 31 | +// For AtlasA2, FA Infer RescaleO | ||
| 32 | +template <LseMode LSE_MODE_, typename SM_DTYPE_> | ||
| 33 | +struct EpilogueAtlasA2RescaleO { | ||
| 34 | + using ArchTag = Arch::AtlasA2; | ||
| 35 | + using IntermPrec = SM_DTYPE_; | ||
| 36 | + static constexpr LseMode LSE_MODE = LSE_MODE_; | ||
| 37 | +}; | ||
| 38 | + | ||
| 39 | +// For AtlasA2, GBSAG Pre | ||
| 40 | +struct EpilogueAtlasA2FAGPre { | ||
| 41 | + using ArchTag = Arch::AtlasA2; | ||
| 42 | +}; | ||
| 43 | + | ||
| 44 | +} // namespace NpuArch::Epilogue | ||
| 45 | + | ||
| 46 | +#endif // EPILOGUE_DISPATCH_POLICY_HPP | ||
| @@ -0,0 +1,39 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 base_defs.hpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef GBSAG_BASE_DEFS_HPP | ||
| 17 | +#define GBSAG_BASE_DEFS_HPP | ||
| 18 | + | ||
| 19 | +#include <cstdint> | ||
| 20 | + | ||
| 21 | +#include <kernel_operator.h> | ||
| 22 | + | ||
| 23 | +#include "../attn_infra/detail/gbsag_alignment.hpp" | ||
| 24 | +#include "../attn_infra/detail/gbsag_dependent_false.hpp" | ||
| 25 | +#include "../attn_infra/detail/gbsag_macros.hpp" | ||
| 26 | + | ||
| 27 | +namespace NpuArch { | ||
| 28 | + | ||
| 29 | +constexpr uint32_t BYTE_PER_C0 = 32; | ||
| 30 | +constexpr uint32_t C0_NUM_PER_FRACTAL = 16; | ||
| 31 | +constexpr uint32_t BYTE_PER_FRACTAL = BYTE_PER_C0 * C0_NUM_PER_FRACTAL; | ||
| 32 | + | ||
| 33 | +constexpr uint32_t BYTE_PER_BLK = 32; | ||
| 34 | + | ||
| 35 | +constexpr uint32_t STRIDE_LIMIT = 65536; | ||
| 36 | + | ||
| 37 | +} // namespace NpuArch | ||
| 38 | + | ||
| 39 | +#endif // GBSAG_BASE_DEFS_HPP | ||
| @@ -0,0 +1,314 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 coord.hpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef GBSAG_COORD_HPP | ||
| 17 | +#define GBSAG_COORD_HPP | ||
| 18 | + | ||
| 19 | +#include "../attn_infra/gbsag_base_defs.hpp" | ||
| 20 | + | ||
| 21 | +namespace NpuArch { | ||
| 22 | + | ||
| 23 | +/// Statically-sized array specifying Coords within a tensor | ||
| 24 | +template <int RANK_, ///< Logical rank of coordinate | ||
| 25 | + class Index_ = uint32_t, ///< Index type used for each dimension | ||
| 26 | + class LongIndex_ = int64_t ///< Long index type used for linear offsets | ||
| 27 | + > | ||
| 28 | +struct Coord { | ||
| 29 | +public: | ||
| 30 | + // Number of elements in Coord | ||
| 31 | + static const int RANK = RANK_; | ||
| 32 | + | ||
| 33 | + // Index typen used to store elements | ||
| 34 | + using Index = Index_; | ||
| 35 | + | ||
| 36 | + // Type used to represent linear offsets | ||
| 37 | + using LongIndex = LongIndex_; | ||
| 38 | + | ||
| 39 | + // Default ctor initializes uniformly | ||
| 40 | + HOST_DEVICE constexpr explicit Coord(Index value = Index(0)) | ||
| 41 | + { | ||
| 42 | + for (int i = 0; i < RANK; ++i) { | ||
| 43 | + idx[i] = value; | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + // Constructs from an array of integers | ||
| 48 | + HOST_DEVICE constexpr Coord(Index const (&idx_)[RANK]) | ||
| 49 | + { | ||
| 50 | + for (int i = 0; i < RANK; ++i) { | ||
| 51 | + idx[i] = idx_[i]; | ||
| 52 | + } | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + // Constructs frrom an array of integers | ||
| 56 | + HOST_DEVICE | ||
| 57 | + int Argmin() const | ||
| 58 | + { | ||
| 59 | + int i = 0; | ||
| 60 | + for (int j = 1; j < RANK; ++j) { | ||
| 61 | + if (idx[j] < idx[i]) { | ||
| 62 | + i = j; | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + return i; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + // Returns the index of the dimension with greatest value | ||
| 69 | + HOST_DEVICE | ||
| 70 | + int Argmax() const | ||
| 71 | + { | ||
| 72 | + int i = 0; | ||
| 73 | + for (int j = 1; j < RANK; ++j) { | ||
| 74 | + if (idx[j] > idx[i]) { | ||
| 75 | + i = j; | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + return i; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + // Returns true if Coord is non-zero | ||
| 82 | + HOST_DEVICE | ||
| 83 | + explicit operator bool() const | ||
| 84 | + { | ||
| 85 | + for (int i = 0; i < RANK; ++i) { | ||
| 86 | + if (idx[i]) { | ||
| 87 | + return true; | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + return false; | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + // Return true if Coord is uniformly zero. | ||
| 94 | + HOST_DEVICE | ||
| 95 | + bool operator!() const | ||
| 96 | + { | ||
| 97 | + for (int i = 0; i < RANK; ++i) { | ||
| 98 | + if (idx[i]) { | ||
| 99 | + return false; | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + return true; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + // Element-wise addition | ||
| 106 | + HOST_DEVICE | ||
| 107 | + Coord operator+(Coord const &b) const | ||
| 108 | + { | ||
| 109 | + Coord c; | ||
| 110 | + for (int i = 0; i < RANK; ++i) { | ||
| 111 | + c.idx[i] = idx[i] + b.idx[i]; | ||
| 112 | + } | ||
| 113 | + return c; | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + // Add a scalar to each element | ||
| 117 | + HOST_DEVICE | ||
| 118 | + Coord operator+(const Index val) const | ||
| 119 | + { | ||
| 120 | + Coord c; | ||
| 121 | + for (int i = 0; i < RANK; ++i) { | ||
| 122 | + c.idx[i] = idx[i] + val; | ||
| 123 | + } | ||
| 124 | + return c; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + // Element-wise subtraction | ||
| 128 | + HOST_DEVICE | ||
| 129 | + Coord operator-(Coord const &b) const | ||
| 130 | + { | ||
| 131 | + Coord c; | ||
| 132 | + for (int i = 0; i < RANK; i++) { | ||
| 133 | + c.idx[i] = idx[i] - b.idx[i]; | ||
| 134 | + } | ||
| 135 | + return c; | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + // Subtract a scalar from each element | ||
| 139 | + HOST_DEVICE | ||
| 140 | + Coord operator-(Index const val) const | ||
| 141 | + { | ||
| 142 | + Coord c; | ||
| 143 | + for (int i = 0; i < RANK; ++i) { | ||
| 144 | + c.idx[i] = idx[i] - val; | ||
| 145 | + } | ||
| 146 | + return c; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + // Element-wise multiply | ||
| 150 | + HOST_DEVICE | ||
| 151 | + Coord operator*(Coord const &b) const | ||
| 152 | + { | ||
| 153 | + Coord c; | ||
| 154 | + for (int i = 0; i < RANK; i++) { | ||
| 155 | + c.idx[i] = idx[i] * b.idx[i]; | ||
| 156 | + } | ||
| 157 | + return c; | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + // Element-wise division | ||
| 161 | + HOST_DEVICE | ||
| 162 | + Coord operator/(Coord const &b) const | ||
| 163 | + { | ||
| 164 | + Coord c; | ||
| 165 | + for (int i = 0; i < RANK; i++) { | ||
| 166 | + c.idx[i] = idx[i] / b.idx[i]; | ||
| 167 | + } | ||
| 168 | + return c; | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + // Element-wise mod | ||
| 172 | + HOST_DEVICE | ||
| 173 | + Coord operator%(Coord const &b) const | ||
| 174 | + { | ||
| 175 | + Coord c; | ||
| 176 | + for (int i = 0; i < RANK; i++) { | ||
| 177 | + c.idx[i] = idx[i] % b.idx[i]; | ||
| 178 | + } | ||
| 179 | + return c; | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + // In-place addition | ||
| 183 | + HOST_DEVICE | ||
| 184 | + Coord &operator+=(Coord const &b) | ||
| 185 | + { | ||
| 186 | + for (int i = 0; i < RANK; ++i) { | ||
| 187 | + idx[i] += b.idx[i]; | ||
| 188 | + } | ||
| 189 | + return *this; | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + // In-place equal | ||
| 193 | + HOST_DEVICE | ||
| 194 | + bool operator==(Coord const &b) const | ||
| 195 | + { | ||
| 196 | + for (int i = 0; i < RANK; ++i) { | ||
| 197 | + if (idx[i] != b.idx[i]) { | ||
| 198 | + return false; | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | + return true; | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + // In-place equal | ||
| 205 | + HOST_DEVICE | ||
| 206 | + bool operator==(Index const val) const | ||
| 207 | + { | ||
| 208 | + for (int i = 0; i < RANK; ++i) { | ||
| 209 | + if (idx[i] != val) { | ||
| 210 | + return false; | ||
| 211 | + } | ||
| 212 | + } | ||
| 213 | + return true; | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + // Member acces operator | ||
| 217 | + HOST_DEVICE | ||
| 218 | + Index &operator[](int dim) | ||
| 219 | + { | ||
| 220 | + return idx[dim]; | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + // Member access operator | ||
| 224 | + HOST_DEVICE | ||
| 225 | + Index const &operator[](int dim) const | ||
| 226 | + { | ||
| 227 | + return idx[dim]; | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + // Gets the index of a given Coord element | ||
| 231 | + template <int DIM> | ||
| 232 | + HOST_DEVICE Index &At() | ||
| 233 | + { | ||
| 234 | + return idx[DIM]; | ||
| 235 | + } | ||
| 236 | + | ||
| 237 | + // Access via index; may limit unrolling potential | ||
| 238 | + HOST_DEVICE | ||
| 239 | + Index &At(int dim) | ||
| 240 | + { | ||
| 241 | + return idx[dim]; | ||
| 242 | + } | ||
| 243 | + | ||
| 244 | + // Gets the index of a given Coord element | ||
| 245 | + template <int DIM> | ||
| 246 | + HOST_DEVICE Index const &At() const | ||
| 247 | + { | ||
| 248 | + return idx[DIM]; | ||
| 249 | + } | ||
| 250 | + | ||
| 251 | + // Access via index; may limit unrolling potential | ||
| 252 | + HOST_DEVICE | ||
| 253 | + Index const &At(int dim) const | ||
| 254 | + { | ||
| 255 | + return idx[dim]; | ||
| 256 | + } | ||
| 257 | + | ||
| 258 | + template <int... Is> | ||
| 259 | + HOST_DEVICE auto GetCoordByAxis() const | ||
| 260 | + { | ||
| 261 | + Index idx_[sizeof...(Is)]{idx[Is]...}; | ||
| 262 | + return Coord<sizeof...(Is), Index, LongIndex>{idx_}; | ||
| 263 | + } | ||
| 264 | + | ||
| 265 | + HOST_DEVICE | ||
| 266 | + static Coord Min(Coord const &a, Coord const &b) | ||
| 267 | + { | ||
| 268 | + Coord res; | ||
| 269 | + for (int i = 0; i < RANK; ++i) { | ||
| 270 | + res[i] = a[i] < b[i] ? a[i] : b[i]; | ||
| 271 | + } | ||
| 272 | + return res; | ||
| 273 | + } | ||
| 274 | + | ||
| 275 | +private: | ||
| 276 | + // Indices | ||
| 277 | + Index idx[RANK]; | ||
| 278 | +}; | ||
| 279 | + | ||
| 280 | +// Helper to make a 1-element coordinate | ||
| 281 | +template <class T> | ||
| 282 | +HOST_DEVICE constexpr Coord<1, T> MakeCoord(T dim0) | ||
| 283 | +{ | ||
| 284 | + T values[1] = {dim0}; | ||
| 285 | + return Coord<1, T>(values); | ||
| 286 | +} | ||
| 287 | + | ||
| 288 | +/// Helper to make a 2-element coordinate | ||
| 289 | +template <class T> | ||
| 290 | +HOST_DEVICE constexpr Coord<2, T> MakeCoord(T dim0, T dim1) | ||
| 291 | +{ | ||
| 292 | + T values[2] = {dim0, dim1}; | ||
| 293 | + return Coord<2, T>(values); | ||
| 294 | +} | ||
| 295 | + | ||
| 296 | +/// Helper to make a 3-element coordinate | ||
| 297 | +template <class T> | ||
| 298 | +HOST_DEVICE constexpr Coord<3, T> MakeCoord(T dim0, T dim1, T dim2) | ||
| 299 | +{ | ||
| 300 | + T values[3] = {dim0, dim1, dim2}; | ||
| 301 | + return Coord<3, T>(values); | ||
| 302 | +} | ||
| 303 | + | ||
| 304 | +/// Helper to make a 4-element coordinate | ||
| 305 | +template <class T> | ||
| 306 | +HOST_DEVICE constexpr Coord<4, T> MakeCoord(T dim0, T dim1, T dim2, T dim3) | ||
| 307 | +{ | ||
| 308 | + T values[4] = {dim0, dim1, dim2, dim3}; | ||
| 309 | + return Coord<4, T>(values); | ||
| 310 | +} | ||
| 311 | + | ||
| 312 | +} // namespace NpuArch | ||
| 313 | + | ||
| 314 | +#endif // COORD_HPP | ||
Aattention/generic_block_sparse_attention_grad/op_kernel/arc22/attn_infra/gbsag_gemm_coord.hpp+166-0
| @@ -0,0 +1,166 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 gemm_coord.hpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef GBSAG_GEMM_COORD_HPP | ||
| 17 | +#define GBSAG_GEMM_COORD_HPP | ||
| 18 | + | ||
| 19 | +#include "../attn_infra/gbsag_coord.hpp" | ||
| 20 | + | ||
| 21 | +namespace NpuArch { | ||
| 22 | + | ||
| 23 | +/// Shape of a matrix multiply-add operation | ||
| 24 | +template < | ||
| 25 | + /// Rows of matrix product | ||
| 26 | + uint32_t M_ = 1, | ||
| 27 | + /// Columns of matrix product | ||
| 28 | + uint32_t N_ = 1, | ||
| 29 | + /// Inner dimension of matrix product | ||
| 30 | + uint32_t K_ = 1> | ||
| 31 | +struct GemmShape { | ||
| 32 | + static constexpr uint32_t M = M_; | ||
| 33 | + static constexpr uint32_t N = N_; | ||
| 34 | + static constexpr uint32_t K = K_; | ||
| 35 | + | ||
| 36 | + static constexpr int64_t MN = M * N; | ||
| 37 | + static constexpr int64_t MK = M * K; | ||
| 38 | + static constexpr int64_t KN = N * K; | ||
| 39 | + static constexpr int64_t MNK = M * N * K; | ||
| 40 | + | ||
| 41 | + static constexpr int64_t COUNT = MNK; | ||
| 42 | + | ||
| 43 | + /// Returns a Coord object | ||
| 44 | + HOST_DEVICE | ||
| 45 | + static Coord<3> ToCoord() | ||
| 46 | + { | ||
| 47 | + return MakeCoord(M, N, K); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + HOST_DEVICE | ||
| 51 | + static Coord<2> ToCoordMN() | ||
| 52 | + { | ||
| 53 | + return MakeCoord(M, N); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + HOST_DEVICE | ||
| 57 | + static Coord<2> ToCoordMK() | ||
| 58 | + { | ||
| 59 | + return MakeCoord(M, K); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + HOST_DEVICE | ||
| 63 | + static Coord<2> ToCoordKN() | ||
| 64 | + { | ||
| 65 | + return MakeCoord(K, N); | ||
| 66 | + } | ||
| 67 | +}; | ||
| 68 | + | ||
| 69 | +/// GemmCoord is a structure derived from Coord<3> that specifies a location within the | ||
| 70 | +/// coordinate space of a Gemm problem. | ||
| 71 | +struct GemmCoord : public Coord<3, uint32_t> { | ||
| 72 | + /// Integer-valued index | ||
| 73 | + using Index = uint32_t; | ||
| 74 | + | ||
| 75 | + /// Base type is a Coord of rank=3 | ||
| 76 | + using Base = Coord<3, Index>; | ||
| 77 | + | ||
| 78 | + /// Gemm M dimension - rows of the output C matrix | ||
| 79 | + static constexpr int M_INDEX = 0; | ||
| 80 | + | ||
| 81 | + /// Gemm N dimension - columns of the output C matrix | ||
| 82 | + static constexpr int N_INDEX = 1; | ||
| 83 | + | ||
| 84 | + /// Gemm K dimension - inner dimension of the Gemm problem | ||
| 85 | + static constexpr int K_INDEX = 2; | ||
| 86 | + | ||
| 87 | + /// Default ctor | ||
| 88 | + HOST_DEVICE | ||
| 89 | + GemmCoord() {} | ||
| 90 | + | ||
| 91 | + /// Constructs from Coord<3> and a batch | ||
| 92 | + HOST_DEVICE | ||
| 93 | + GemmCoord(Coord<3, Index> const &coord) | ||
| 94 | + : Base(coord) | ||
| 95 | + {} | ||
| 96 | + | ||
| 97 | + /// Helper to construct from a K, N, M, batch variables | ||
| 98 | + HOST_DEVICE | ||
| 99 | + GemmCoord(Index m, Index n, Index k) | ||
| 100 | + : Base(MakeCoord(m, n, k)) | ||
| 101 | + {} | ||
| 102 | + | ||
| 103 | + /// Returns the Gemm M coordinate | ||
| 104 | + HOST_DEVICE | ||
| 105 | + Index const &m() const | ||
| 106 | + { | ||
| 107 | + return this->At(M_INDEX); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /// Returns reference to the Gemm M coordinate | ||
| 111 | + HOST_DEVICE | ||
| 112 | + Index &m() | ||
| 113 | + { | ||
| 114 | + return this->At(M_INDEX); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + /// Returns the Gemm N coordinate | ||
| 118 | + HOST_DEVICE | ||
| 119 | + Index const &n() const | ||
| 120 | + { | ||
| 121 | + return this->At(N_INDEX); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /// Returns reference to the Gemm N coordinate | ||
| 125 | + HOST_DEVICE | ||
| 126 | + Index &n() | ||
| 127 | + { | ||
| 128 | + return this->At(N_INDEX); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + /// Returns the Gemm K coordinate | ||
| 132 | + HOST_DEVICE | ||
| 133 | + Index const &k() const | ||
| 134 | + { | ||
| 135 | + return this->At(K_INDEX); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + /// Returns reference to the Gemm K coordinate | ||
| 139 | + HOST_DEVICE | ||
| 140 | + Index &k() | ||
| 141 | + { | ||
| 142 | + return this->At(K_INDEX); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + HOST_DEVICE | ||
| 146 | + auto GetCoordMN() const | ||
| 147 | + { | ||
| 148 | + return this->GetCoordByAxis<M_INDEX, N_INDEX>(); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + HOST_DEVICE | ||
| 152 | + auto GetCoordMK() const | ||
| 153 | + { | ||
| 154 | + return this->GetCoordByAxis<M_INDEX, K_INDEX>(); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + HOST_DEVICE | ||
| 158 | + auto GetCoordKN() const | ||
| 159 | + { | ||
| 160 | + return this->GetCoordByAxis<K_INDEX, N_INDEX>(); | ||
| 161 | + } | ||
| 162 | +}; | ||
| 163 | + | ||
| 164 | +} // namespace NpuArch | ||
| 165 | + | ||
| 166 | +#endif // GEMM_COORD_HPP | ||
Aattention/generic_block_sparse_attention_grad/op_kernel/arc22/attn_infra/gbsag_matrix_coord.hpp+123-0
| @@ -0,0 +1,123 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 matrix_coord.hpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef GBSAG_MATRIX_COORD_HPP | ||
| 17 | +#define GBSAG_MATRIX_COORD_HPP | ||
| 18 | + | ||
| 19 | +#include "../attn_infra/gbsag_coord.hpp" | ||
| 20 | + | ||
| 21 | +namespace NpuArch { | ||
| 22 | + | ||
| 23 | +template <uint32_t ROW_ = 1, uint32_t COLUMN_ = 1> | ||
| 24 | +struct MatrixShape { | ||
| 25 | + static constexpr uint32_t ROW = ROW_; | ||
| 26 | + static constexpr uint32_t COLUMN = COLUMN_; | ||
| 27 | + | ||
| 28 | + static constexpr int64_t COUNT = ROW * COLUMN; | ||
| 29 | + | ||
| 30 | + HOST_DEVICE | ||
| 31 | + static Coord<2> ToCoord() | ||
| 32 | + { | ||
| 33 | + return MakeCoord(ROW, COLUMN); | ||
| 34 | + } | ||
| 35 | +}; | ||
| 36 | + | ||
| 37 | +/// MatrixCoord wraps Coord<2, uint32_t> to provide a helper for accessing named dimensions. Classes | ||
| 38 | +/// expecting a coordinate in the rank=2 index space of a matrix should use MatrixCoord. | ||
| 39 | +struct MatrixCoord : public Coord<2, uint32_t> { | ||
| 40 | + /// Integer-valued index | ||
| 41 | + using Index = uint32_t; | ||
| 42 | + | ||
| 43 | + /// Base type is a Coord of rank=2 | ||
| 44 | + using Base = Coord<2, Index>; | ||
| 45 | + | ||
| 46 | + /// LongIndex type | ||
| 47 | + using LongIndex = typename Base::LongIndex; | ||
| 48 | + | ||
| 49 | + /// Rows dimension | ||
| 50 | + static constexpr uint32_t ROW_INDEX = 0; | ||
| 51 | + | ||
| 52 | + /// Columns dimension | ||
| 53 | + static constexpr uint32_t COLUMN_INDEX = 1; | ||
| 54 | + | ||
| 55 | + /// Default ctor | ||
| 56 | + HOST_DEVICE | ||
| 57 | + MatrixCoord() {} | ||
| 58 | + | ||
| 59 | + /// Constructs from Coord<2> | ||
| 60 | + HOST_DEVICE | ||
| 61 | + MatrixCoord(Coord<2, Index> const &coord) | ||
| 62 | + : Base(coord) | ||
| 63 | + {} | ||
| 64 | + | ||
| 65 | + /// Helper to construct from a row and column | ||
| 66 | + HOST_DEVICE | ||
| 67 | + MatrixCoord(Index row, Index column) | ||
| 68 | + : Base(MakeCoord(row, column)) | ||
| 69 | + {} | ||
| 70 | + | ||
| 71 | + /// Helper to construct from a row and column, which are LongIndex based | ||
| 72 | + HOST_DEVICE | ||
| 73 | + MatrixCoord(LongIndex row, LongIndex column) | ||
| 74 | + : Base(MakeCoord(Index(row), Index(column))) | ||
| 75 | + {} | ||
| 76 | + | ||
| 77 | + /// Returns the row of the coordinate | ||
| 78 | + HOST_DEVICE | ||
| 79 | + Index const &row() const | ||
| 80 | + { | ||
| 81 | + return this->At(ROW_INDEX); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + /// Returns the row of the coordinate | ||
| 85 | + HOST_DEVICE | ||
| 86 | + Index &row() | ||
| 87 | + { | ||
| 88 | + return this->At(ROW_INDEX); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + /// Returns the column of the coordinate | ||
| 92 | + HOST_DEVICE | ||
| 93 | + Index const &column() const | ||
| 94 | + { | ||
| 95 | + return this->At(COLUMN_INDEX); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + /// Returns the column of the coordinate | ||
| 99 | + HOST_DEVICE | ||
| 100 | + Index &column() | ||
| 101 | + { | ||
| 102 | + return this->At(COLUMN_INDEX); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + /// Element-wise addition | ||
| 106 | + HOST_DEVICE | ||
| 107 | + MatrixCoord operator+(Base const &b) const | ||
| 108 | + { | ||
| 109 | + return MatrixCoord(Base::operator+(b)); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + /// In-place addition | ||
| 113 | + HOST_DEVICE | ||
| 114 | + MatrixCoord &operator+=(Base const &b) | ||
| 115 | + { | ||
| 116 | + Base::operator+=(b); | ||
| 117 | + return *this; | ||
| 118 | + } | ||
| 119 | +}; | ||
| 120 | + | ||
| 121 | +} // namespace NpuArch | ||
| 122 | + | ||
| 123 | +#endif | ||
| @@ -0,0 +1,178 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GEMM_BLOCK_MMAD_GBSAG_2_HPP | ||
| 12 | +#define GEMM_BLOCK_MMAD_GBSAG_2_HPP | ||
| 13 | + | ||
| 14 | +#include "../../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | +#include "../../../attn_infra/arch/gbsag_resource.hpp" | ||
| 16 | +#include "../../../attn_infra/gbsag_coord.hpp" | ||
| 17 | +#include "../../../attn_infra/gemm/gbsag_gemm_dispatch_policy.hpp" | ||
| 18 | +#include "../../../attn_infra/gemm/gbsag_helper.hpp" | ||
| 19 | +#include "../../../attn_infra/gbsag_gemm_coord.hpp" | ||
| 20 | +#include "../../../attn_infra/gemm/tile_common/gbsag_gemm_tile_copy.hpp" | ||
| 21 | +#include "../../../attn_infra/gemm/tile_common/gbsag_tile_mmad.hpp" | ||
| 22 | + | ||
| 23 | +//////////////////////////////////////////////////////////////////// | ||
| 24 | + | ||
| 25 | +namespace NpuArch::Gemm::Block { | ||
| 26 | +//////////////////////////////////////////////////////////////////// | ||
| 27 | + | ||
| 28 | +template <class L1TileShape_, class L0TileShape_, class AType_, class BType_, class CType_, class BiasType_, | ||
| 29 | + class TileCopy_, class TileMmad_> | ||
| 30 | +struct BlockMmad<MmadAtlasA2GBSAG2, L1TileShape_, L0TileShape_, AType_, BType_, CType_, BiasType_, TileCopy_, | ||
| 31 | + TileMmad_> { | ||
| 32 | +public: | ||
| 33 | + using DispatchPolicy = MmadAtlasA2GBSAG2; | ||
| 34 | + using ArchTag = typename DispatchPolicy::ArchTag; | ||
| 35 | + | ||
| 36 | + using L1TileShape = L1TileShape_; | ||
| 37 | + using L0TileShape = L0TileShape_; | ||
| 38 | + | ||
| 39 | + using ElementA = typename AType_::Element; | ||
| 40 | + using LayoutA = typename AType_::Layout; | ||
| 41 | + using ElementB = typename BType_::Element; | ||
| 42 | + using LayoutB = typename BType_::Layout; | ||
| 43 | + using ElementC = typename CType_::Element; | ||
| 44 | + using LayoutC = typename CType_::Layout; | ||
| 45 | + | ||
| 46 | + using TileMmad = TileMmad_; | ||
| 47 | + using CopyGmToL1A = typename TileCopy_::CopyGmToL1A; | ||
| 48 | + using CopyGmToL1B = typename TileCopy_::CopyGmToL1B; | ||
| 49 | + using CopyL1ToL0A = typename TileCopy_::CopyL1ToL0A; | ||
| 50 | + using CopyL1ToL0B = typename TileCopy_::CopyL1ToL0B; | ||
| 51 | + using CopyL0CToGm = typename TileCopy_::CopyL0CToGm; | ||
| 52 | + | ||
| 53 | + using ElementAccumulator = | ||
| 54 | + typename Gemm::helper::ElementAccumulatorSelector<ElementA, ElementB>::ElementAccumulator; | ||
| 55 | + | ||
| 56 | + using LayoutAInL1 = typename CopyL1ToL0A::LayoutSrc; | ||
| 57 | + using LayoutBInL1 = typename CopyL1ToL0B::LayoutSrc; | ||
| 58 | + using LayoutAInL0 = typename CopyL1ToL0A::LayoutDst; | ||
| 59 | + using LayoutBInL0 = typename CopyL1ToL0B::LayoutDst; | ||
| 60 | + using LayoutCInL0 = layout::zN; | ||
| 61 | + | ||
| 62 | + using L1AAlignHelper = Gemm::helper::L1AlignHelper<ElementA, LayoutA>; | ||
| 63 | + using L1BAlignHelper = Gemm::helper::L1AlignHelper<ElementB, LayoutB>; | ||
| 64 | + | ||
| 65 | + static constexpr uint32_t STAGES = DispatchPolicy::STAGES; | ||
| 66 | + | ||
| 67 | + static constexpr uint32_t L1A_SIZE = L1TileShape::M * L1TileShape::K * sizeof(ElementA); | ||
| 68 | + static constexpr uint32_t L1B_SIZE = L1TileShape::N * L1TileShape::K * sizeof(ElementB); | ||
| 69 | + | ||
| 70 | + static constexpr uint32_t L0A_SIZE = ArchTag::L0A_SIZE; | ||
| 71 | + static constexpr uint32_t L0B_SIZE = ArchTag::L0B_SIZE; | ||
| 72 | + static constexpr uint32_t L0C_SIZE = ArchTag::L0C_SIZE; | ||
| 73 | + | ||
| 74 | + static constexpr uint32_t L0A_PINGPONG_BUF_SIZE = L0A_SIZE / STAGES; | ||
| 75 | + static constexpr uint32_t L0B_PINGPONG_BUF_SIZE = L0B_SIZE / STAGES; | ||
| 76 | + static constexpr uint32_t L0C_PINGPONG_BUF_SIZE = L0C_SIZE / STAGES; | ||
| 77 | + | ||
| 78 | + static constexpr uint32_t BLOCK_SIZE = 16; | ||
| 79 | + | ||
| 80 | + static_assert(std::is_same_v<LayoutC, layout::RowMajor>, "LayoutC only support RowMajor yet!"); | ||
| 81 | + | ||
| 82 | + __aicore__ inline BlockMmad(Arch::Resource<ArchTag> &resource, uint32_t l1BufAddrStart = 0, | ||
| 83 | + uint32_t pingpongFlagOffset = 0, bool isAtomicAdd_ = false) | ||
| 84 | + { | ||
| 85 | + isAtomicAdd = isAtomicAdd_; | ||
| 86 | + PINGPONG_FLAG_OFFSET = pingpongFlagOffset; | ||
| 87 | + for (uint32_t i = 0; i < STAGES; i++) { | ||
| 88 | + l1ATensor[i] = resource.l1Buf.template GetBufferByByte<ElementA>(l1BufAddrStart + L1A_SIZE * i); | ||
| 89 | + l1BTensor[i] = | ||
| 90 | + resource.l1Buf.template GetBufferByByte<ElementB>(l1BufAddrStart + L1A_SIZE * 2 + L1B_SIZE * i); | ||
| 91 | + l0ATensor[i] = resource.l0ABuf.template GetBufferByByte<ElementA>(L0A_PINGPONG_BUF_SIZE * i); | ||
| 92 | + l0BTensor[i] = resource.l0BBuf.template GetBufferByByte<ElementB>(L0B_PINGPONG_BUF_SIZE * i); | ||
| 93 | + l0CTensor[i] = resource.l0CBuf.template GetBufferByByte<ElementAccumulator>(L0C_PINGPONG_BUF_SIZE * i); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + __aicore__ inline ~BlockMmad() {} | ||
| 98 | + | ||
| 99 | + __aicore__ inline void operator()(AscendC::GlobalTensor<ElementA> gA, AscendC::GlobalTensor<ElementB> gB, | ||
| 100 | + AscendC::GlobalTensor<ElementC> gC, LayoutA layoutA, LayoutB layoutB, | ||
| 101 | + LayoutC layoutC, GemmCoord actualShape, uint32_t &pingpongFlag) | ||
| 102 | + { | ||
| 103 | + LayoutAInL1 layoutAInL1 = LayoutAInL1::template MakeLayout<ElementA>(L1TileShape::M, L1TileShape::K); | ||
| 104 | + LayoutBInL1 layoutBInL1 = LayoutBInL1::template MakeLayout<ElementB>(L1TileShape::K, L1TileShape::N); | ||
| 105 | + | ||
| 106 | + uint32_t mRound = RoundUp<L1AAlignHelper::M_ALIGNED>(actualShape.m()); | ||
| 107 | + uint32_t nRound = RoundUp<L1BAlignHelper::N_ALIGNED>(actualShape.n()); | ||
| 108 | + uint32_t kRound = RoundUp<L1BAlignHelper::K_ALIGNED>(actualShape.k()); | ||
| 109 | + | ||
| 110 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_MTE2>(pingpongFlag + PINGPONG_FLAG_OFFSET); | ||
| 111 | + auto layoutTileB = layoutB.GetTileLayout(MakeCoord(actualShape.k(), actualShape.n())); | ||
| 112 | + copyGmToL1B(l1BTensor[pingpongFlag], gB, layoutBInL1, layoutTileB); | ||
| 113 | + AscendC::SetFlag<AscendC::HardEvent::MTE2_MTE1>(EVENT_ID0); | ||
| 114 | + AscendC::WaitFlag<AscendC::HardEvent::MTE2_MTE1>(EVENT_ID0); | ||
| 115 | + | ||
| 116 | + AscendC::WaitFlag<AscendC::HardEvent::M_MTE1>(pingpongFlag); | ||
| 117 | + LayoutBInL0 layoutBInL0 = LayoutBInL0::template MakeLayout<ElementB>(kRound, nRound); | ||
| 118 | + copyL1ToL0B(l0BTensor[pingpongFlag], l1BTensor[pingpongFlag], layoutBInL0, layoutBInL1); | ||
| 119 | + | ||
| 120 | + auto layoutTileA = layoutA.GetTileLayout(MakeCoord(actualShape.m(), actualShape.k())); | ||
| 121 | + copyGmToL1A(l1ATensor[pingpongFlag], gA, layoutAInL1, layoutTileA); | ||
| 122 | + AscendC::SetFlag<AscendC::HardEvent::MTE2_MTE1>(EVENT_ID0); | ||
| 123 | + AscendC::WaitFlag<AscendC::HardEvent::MTE2_MTE1>(EVENT_ID0); | ||
| 124 | + LayoutAInL0 layoutAInL0 = LayoutAInL0::template MakeLayout<ElementA>(mRound, kRound); | ||
| 125 | + copyL1ToL0A(l0ATensor[pingpongFlag], l1ATensor[pingpongFlag], layoutAInL0, layoutAInL1); | ||
| 126 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_MTE2>(pingpongFlag + PINGPONG_FLAG_OFFSET); | ||
| 127 | + | ||
| 128 | + AscendC::SetFlag<AscendC::HardEvent::MTE1_M>(EVENT_ID0); | ||
| 129 | + AscendC::WaitFlag<AscendC::HardEvent::MTE1_M>(EVENT_ID0); | ||
| 130 | + | ||
| 131 | + AscendC::WaitFlag<AscendC::HardEvent::FIX_M>(pingpongFlag); | ||
| 132 | + tileMmad(l0CTensor[pingpongFlag], l0ATensor[pingpongFlag], l0BTensor[pingpongFlag], mRound, nRound, | ||
| 133 | + actualShape.k()); | ||
| 134 | + AscendC::SetFlag<AscendC::HardEvent::M_MTE1>(pingpongFlag); | ||
| 135 | + | ||
| 136 | + AscendC::SetFlag<AscendC::HardEvent::M_FIX>(EVENT_ID0); | ||
| 137 | + AscendC::WaitFlag<AscendC::HardEvent::M_FIX>(EVENT_ID0); | ||
| 138 | + | ||
| 139 | + // copy block out | ||
| 140 | + auto blockShape = MakeCoord(actualShape.m(), actualShape.n()); | ||
| 141 | + auto layoutInL0C = LayoutCInL0::MakeLayoutInL0C(blockShape); | ||
| 142 | + if (isAtomicAdd) { | ||
| 143 | + AscendC::SetAtomicAdd<ElementC>(); | ||
| 144 | + copyL0CToGm(gC, l0CTensor[pingpongFlag], layoutC, layoutInL0C); | ||
| 145 | + AscendC::SetAtomicNone(); | ||
| 146 | + } else { | ||
| 147 | + copyL0CToGm(gC, l0CTensor[pingpongFlag], layoutC, layoutInL0C); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + AscendC::SetFlag<AscendC::HardEvent::FIX_M>(pingpongFlag); | ||
| 151 | + | ||
| 152 | + pingpongFlag = 1 - pingpongFlag; | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | +protected: | ||
| 156 | + AscendC::LocalTensor<ElementA> l1ATensor[STAGES]; | ||
| 157 | + AscendC::LocalTensor<ElementB> l1BTensor[STAGES]; | ||
| 158 | + | ||
| 159 | + AscendC::LocalTensor<ElementA> l0ATensor[STAGES]; | ||
| 160 | + AscendC::LocalTensor<ElementB> l0BTensor[STAGES]; | ||
| 161 | + AscendC::LocalTensor<ElementAccumulator> l0CTensor[STAGES]; | ||
| 162 | + | ||
| 163 | + TileMmad tileMmad; | ||
| 164 | + CopyGmToL1A copyGmToL1A; | ||
| 165 | + CopyGmToL1B copyGmToL1B; | ||
| 166 | + CopyL1ToL0A copyL1ToL0A; | ||
| 167 | + CopyL1ToL0B copyL1ToL0B; | ||
| 168 | + CopyL0CToGm copyL0CToGm; | ||
| 169 | + | ||
| 170 | + uint32_t PINGPONG_FLAG_OFFSET = 0; | ||
| 171 | + bool isAtomicAdd = false; | ||
| 172 | +}; | ||
| 173 | + | ||
| 174 | +//////////////////////////////////////////////////////////////////// | ||
| 175 | + | ||
| 176 | +} // namespace NpuArch::Gemm::Block | ||
| 177 | + | ||
| 178 | +#endif // GEMM_BLOCK_MMAD_GBSAG_2_HPP | ||
| @@ -0,0 +1,32 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_BLOCK_MMAD_HPP | ||
| 12 | +#define GBSAG_BLOCK_MMAD_HPP | ||
| 13 | + | ||
| 14 | +#include "../../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | +#include "../../../attn_infra/gemm/tile_common/gbsag_gemm_tile_copy.hpp" | ||
| 16 | +#include "../../../attn_infra/gemm/tile_common/gbsag_tile_mmad.hpp" | ||
| 17 | + | ||
| 18 | +namespace NpuArch::Gemm::Block { | ||
| 19 | + | ||
| 20 | +template <class DispatchPolicy, class L1TileShape, class L0TileShape, class AType, class BType, class CType, | ||
| 21 | + class BiasType = void, | ||
| 22 | + class TileCopy = Gemm::Tile::TileCopy<typename DispatchPolicy::ArchTag, AType, BType, CType, BiasType>, | ||
| 23 | + class TileMmad = Gemm::Tile::TileMmad<typename DispatchPolicy::ArchTag, AType, BType, BiasType> > | ||
| 24 | +struct BlockMmad { | ||
| 25 | + static_assert(DEPENDENT_FALSE<DispatchPolicy>, "BlockMmad is not implemented for this DispatchPolicy"); | ||
| 26 | +}; | ||
| 27 | + | ||
| 28 | +} // namespace NpuArch::Gemm::Block | ||
| 29 | + | ||
| 30 | +#include "../../../attn_infra/gemm/block/block_mmad_gbsag2.hpp" | ||
| 31 | + | ||
| 32 | +#endif // GEMM_BLOCK_BLOCK_MMAD_HPP | ||
| @@ -0,0 +1,85 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_DISPATCH_POLICY_HPP | ||
| 12 | +#define GBSAG_DISPATCH_POLICY_HPP | ||
| 13 | + | ||
| 14 | +#include "../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | +#include "../../attn_infra/arch/gbsag_arch.hpp" | ||
| 16 | + | ||
| 17 | +namespace NpuArch::Gemm { | ||
| 18 | + | ||
| 19 | +// Block Mmad Policies | ||
| 20 | + | ||
| 21 | +template <bool ASYNC_ = false> | ||
| 22 | +struct MmadAtlasA2Base { | ||
| 23 | + using ArchTag = Arch::AtlasA2; | ||
| 24 | + static constexpr uint32_t ASYNC = ASYNC_; | ||
| 25 | +}; | ||
| 26 | + | ||
| 27 | +using MmadAtlasA2 = MmadAtlasA2Base<false>; | ||
| 28 | + | ||
| 29 | +template <bool PAGED_CACHE_FLAG_ = false, bool ENABLE_UNIT_FLAG_ = false> | ||
| 30 | +struct MmadAtlasA2FAIQK : public MmadAtlasA2 { | ||
| 31 | + static constexpr uint32_t STAGES = 2; | ||
| 32 | + static constexpr bool PAGED_CACHE_FLAG = PAGED_CACHE_FLAG_; | ||
| 33 | + static constexpr bool ENABLE_UNIT_FLAG = ENABLE_UNIT_FLAG_; | ||
| 34 | +}; | ||
| 35 | + | ||
| 36 | +template <bool PAGED_CACHE_FLAG_ = false, bool ENABLE_UNIT_FLAG_ = false> | ||
| 37 | +struct MmadAtlasA2FAIPV : public MmadAtlasA2 { | ||
| 38 | + static constexpr uint32_t STAGES = 2; | ||
| 39 | + static constexpr bool PAGED_CACHE_FLAG = PAGED_CACHE_FLAG_; | ||
| 40 | + static constexpr bool ENABLE_UNIT_FLAG = ENABLE_UNIT_FLAG_; | ||
| 41 | +}; | ||
| 42 | + | ||
| 43 | +template <bool PAGED_CACHE_FLAG_ = false, bool ENABLE_UNIT_FLAG_ = false> | ||
| 44 | +struct MmadAtlasA2FAITailQK : public MmadAtlasA2 { | ||
| 45 | + static constexpr uint32_t STAGES = 2; | ||
| 46 | + static constexpr bool PAGED_CACHE_FLAG = PAGED_CACHE_FLAG_; | ||
| 47 | + static constexpr bool ENABLE_UNIT_FLAG = ENABLE_UNIT_FLAG_; | ||
| 48 | +}; | ||
| 49 | + | ||
| 50 | +template <bool PAGED_CACHE_FLAG_ = false, bool ENABLE_UNIT_FLAG_ = false> | ||
| 51 | +struct MmadAtlasA2FAITailPV : public MmadAtlasA2 { | ||
| 52 | + static constexpr uint32_t STAGES = 2; | ||
| 53 | + static constexpr bool PAGED_CACHE_FLAG = PAGED_CACHE_FLAG_; | ||
| 54 | + static constexpr bool ENABLE_UNIT_FLAG = ENABLE_UNIT_FLAG_; | ||
| 55 | +}; | ||
| 56 | + | ||
| 57 | +template <bool PAGED_CACHE_FLAG_ = false, bool ENABLE_UNIT_FLAG_ = false> | ||
| 58 | +struct MmadAtlasA2SFAIQK : public MmadAtlasA2 { | ||
| 59 | + static constexpr uint32_t STAGES = 2; | ||
| 60 | + static constexpr bool PAGED_CACHE_FLAG = PAGED_CACHE_FLAG_; | ||
| 61 | + static constexpr bool ENABLE_UNIT_FLAG = ENABLE_UNIT_FLAG_; | ||
| 62 | +}; | ||
| 63 | + | ||
| 64 | +template <bool PAGED_CACHE_FLAG_ = false, bool ENABLE_UNIT_FLAG_ = false> | ||
| 65 | +struct MmadAtlasA2SFAIPV : public MmadAtlasA2 { | ||
| 66 | + static constexpr uint32_t STAGES = 2; | ||
| 67 | + static constexpr bool PAGED_CACHE_FLAG = PAGED_CACHE_FLAG_; | ||
| 68 | + static constexpr bool ENABLE_UNIT_FLAG = ENABLE_UNIT_FLAG_; | ||
| 69 | +}; | ||
| 70 | + | ||
| 71 | +struct MmadAtlasA2GBSAG1 : public MmadAtlasA2 { | ||
| 72 | + static constexpr uint32_t STAGES = 2; | ||
| 73 | +}; | ||
| 74 | + | ||
| 75 | +struct MmadAtlasA2GBSAG2 : public MmadAtlasA2 { | ||
| 76 | + static constexpr uint32_t STAGES = 2; | ||
| 77 | +}; | ||
| 78 | + | ||
| 79 | +struct MmadAtlasA2GBSAG3 : public MmadAtlasA2 { | ||
| 80 | + static constexpr uint32_t STAGES = 2; | ||
| 81 | +}; | ||
| 82 | + | ||
| 83 | +} // namespace NpuArch::Gemm | ||
| 84 | + | ||
| 85 | +#endif // GEMM_DISPATCH_POLICY_HPP | ||
Aattention/generic_block_sparse_attention_grad/op_kernel/arc22/attn_infra/gemm/gbsag_gemm_type.hpp+26-0
| @@ -0,0 +1,26 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 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 | +#ifndef GBSAG_GEMM_TYPE_HPP | ||
| 12 | +#define GBSAG_GEMM_TYPE_HPP | ||
| 13 | + | ||
| 14 | +#include "../../attn_infra/gbsag_base_defs.hpp" | ||
| 15 | + | ||
| 16 | +namespace NpuArch::Gemm { | ||
| 17 | +template <class Element_, class Layout_, AscendC::TPosition POSITION_ = AscendC::TPosition::GM> | ||
| 18 | +struct GemmType { | ||
| 19 | + using Element = Element_; | ||
| 20 | + using Layout = Layout_; | ||
| 21 | + static constexpr AscendC::TPosition POSITION = POSITION_; | ||
| 22 | +}; | ||
| 23 | + | ||
| 24 | +} // namespace NpuArch::Gemm | ||
| 25 | + | ||
| 26 | +#endif // GEMM_GEMM_TYPE_HPP | ||