Merged
refactor: chunk_gated_delta_rule 拆分架构目录,支持 ascend950 #8318
refactor: chunk_gated_delta_rule 拆分架构目录,支持 ascend950 #8318
Merged
huang-chuhongAdd on Jul 6
Total: 13 files changed+1866-5
@@ -79,6 +79,16 @@ public:
79 .ExtendCfgInfo("softsync.flag", "true");79 .ExtendCfgInfo("softsync.flag", "true");
80 this->AICore().AddConfig("ascend910_93");80 this->AICore().AddConfig("ascend910_93");
81 this->AICore().AddConfig("ascend910b", aicoreConfig);81 this->AICore().AddConfig("ascend910b", aicoreConfig);
82+ 
83+ OpAICoreConfig config_950;
84+ config_950.DynamicCompileStaticFlag(true)
85+ .DynamicFormatFlag(true)
86+ .DynamicRankSupportFlag(true)
87+ .DynamicShapeSupportFlag(true)
88+ .NeedCheckSupportFlag(false)
89+ .ExtendCfgInfo("softsync.flag", "true")
90+ .ExtendCfgInfo("opFile.value", "chunk_gated_delta_rule_apt");
91+ this->AICore().AddConfig("ascend950", config_950);
82 }92 }
83};93};
84OP_ADD(ChunkGatedDeltaRule); // 添加算子信息库94OP_ADD(ChunkGatedDeltaRule); // 添加算子信息库
Rattention/chunk_gated_delta_rule/op_kernel/chunk_gated_delta_rule.h→attention/chunk_gated_delta_rule/op_kernel/arch22/chunk_gated_delta_rule.h+1-1
@@ -18,7 +18,7 @@
18#include "kernel_operator.h"18#include "kernel_operator.h"
19#include "lib/matmul_intf.h"19#include "lib/matmul_intf.h"
20#include "kernel_tiling/kernel_tiling.h"20#include "kernel_tiling/kernel_tiling.h"
21-#include "chunk_gated_delta_rule_tiling_data.h"21+#include "../chunk_gated_delta_rule_tiling_data.h"
22#include "chunk_gated_delta_rule_stage1.h"22#include "chunk_gated_delta_rule_stage1.h"
23#include "chunk_gated_delta_rule_stage2.h"23#include "chunk_gated_delta_rule_stage2.h"
24#include "chunk_gated_delta_rule_stage3.h"24#include "chunk_gated_delta_rule_stage3.h"
Rattention/chunk_gated_delta_rule/op_kernel/chunk_gated_delta_rule_stage1.h→attention/chunk_gated_delta_rule/op_kernel/arch22/chunk_gated_delta_rule_stage1.h+1-1
@@ -17,7 +17,7 @@
17 17 
18#include "kernel_tiling/kernel_tiling.h"18#include "kernel_tiling/kernel_tiling.h"
19#include "chunk_gated_delta_rule_utils.h"19#include "chunk_gated_delta_rule_utils.h"
20-#include "chunk_gated_delta_rule_tiling_data.h"20+#include "../chunk_gated_delta_rule_tiling_data.h"
21 21 
22namespace ChunkGatedDeltaRule {22namespace ChunkGatedDeltaRule {
23using namespace AscendC;23using namespace AscendC;
Rattention/chunk_gated_delta_rule/op_kernel/chunk_gated_delta_rule_stage2.h→attention/chunk_gated_delta_rule/op_kernel/arch22/chunk_gated_delta_rule_stage2.h+1-1
@@ -17,7 +17,7 @@
17 17 
18#include "kernel_tiling/kernel_tiling.h"18#include "kernel_tiling/kernel_tiling.h"
19#include "chunk_gated_delta_rule_utils.h"19#include "chunk_gated_delta_rule_utils.h"
20-#include "chunk_gated_delta_rule_tiling_data.h"20+#include "../chunk_gated_delta_rule_tiling_data.h"
21 21 
22namespace ChunkGatedDeltaRule {22namespace ChunkGatedDeltaRule {
23using namespace AscendC;23using namespace AscendC;
Rattention/chunk_gated_delta_rule/op_kernel/chunk_gated_delta_rule_stage3.h→attention/chunk_gated_delta_rule/op_kernel/arch22/chunk_gated_delta_rule_stage3.h+1-1
@@ -17,7 +17,7 @@
17 17 
18#include "kernel_tiling/kernel_tiling.h"18#include "kernel_tiling/kernel_tiling.h"
19#include "chunk_gated_delta_rule_utils.h"19#include "chunk_gated_delta_rule_utils.h"
20-#include "chunk_gated_delta_rule_tiling_data.h"20+#include "../chunk_gated_delta_rule_tiling_data.h"
21 21 
22namespace ChunkGatedDeltaRule {22namespace ChunkGatedDeltaRule {
23using namespace AscendC;23using namespace AscendC;
Rattention/chunk_gated_delta_rule/op_kernel/chunk_gated_delta_rule_utils.h→attention/chunk_gated_delta_rule/op_kernel/arch22/chunk_gated_delta_rule_utils.h+0-0
File renamed without changes.
@@ -0,0 +1,320 @@
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 chunk_gated_delta_rule.h
13+ * \brief
14+ */
15+#ifndef CHUNK_GATED_DELTA_RULE_H
16+#define CHUNK_GATED_DELTA_RULE_H
17+ 
18+#include "kernel_operator.h"
19+#include "lib/matmul_intf.h"
20+#include "kernel_tiling/kernel_tiling.h"
21+#include "../chunk_gated_delta_rule_tiling_data.h"
22+#include "chunk_gated_delta_rule_stage1.h"
23+#include "chunk_gated_delta_rule_stage2.h"
24+#include "chunk_gated_delta_rule_stage3.h"
25+#include "chunk_gated_delta_rule_utils.h"
26+ 
27+namespace ChunkGatedDeltaRule {
28+ 
29+using namespace AscendC;
30+ 
31+struct CGDRInitParams {
32+ GM_ADDR query;
33+ GM_ADDR key;
34+ GM_ADDR value;
35+ GM_ADDR beta;
36+ GM_ADDR initState;
37+ GM_ADDR seqlens;
38+ GM_ADDR gOptional;
39+ GM_ADDR attnOut;
40+ GM_ADDR finalState;
41+};
42+ 
43+template <typename srcType, typename dstType>
44+__aicore__ inline void CopyCast(
45+ const GlobalTensor<srcType>& src,
46+ const GlobalTensor<dstType>& dst,
47+ TPipe* pipe,
48+ const int64_t totalDataCount,
49+ const RoundMode& roundMode)
50+{
51+ if ASCEND_IS_AIV {
52+ int64_t blkNum = GetBlockNum();
53+ int64_t blkId = GetBlockIdx();
54+ int64_t dataPerCore = (totalDataCount + blkNum - 1) / blkNum;
55+ int64_t startPos = dataPerCore * blkId;
56+ int64_t endPos = startPos + dataPerCore;
57+ if (startPos >= totalDataCount) {
58+ return;
59+ }
60+ if (endPos > totalDataCount) {
61+ endPos = totalDataCount;
62+ }
63+ TQue<QuePosition::VECIN, TQUE_DEPTH_TWO> inQueue;
64+ TQue<QuePosition::VECOUT, TQUE_DEPTH_TWO> outQueue;
65+ pipe->InitBuffer(inQueue, BUFFER_NUM_TWO, TILE_LEN * sizeof(srcType)); // use 2 buffer
66+ pipe->InitBuffer(outQueue, BUFFER_NUM_TWO, TILE_LEN * sizeof(dstType)); // use 2 buffer
67+ for (int64_t i = startPos; i < endPos; i += TILE_LEN) {
68+ uint32_t blockLen = i + TILE_LEN > endPos ? endPos - i : TILE_LEN;
69+ // copy in
70+ DataCopyExtParams inParams{1, static_cast<uint32_t>(blockLen * sizeof(srcType)), 0, 0, 0};
71+ DataCopyPadExtParams<srcType> inPadParams{false, 0, 0, 0};
72+ auto inLocal = inQueue.AllocTensor<srcType>();
73+ DataCopyPad(inLocal, src[i], inParams, inPadParams);
74+ inQueue.EnQue(inLocal);
75+ // cast
76+ auto stateIn = inQueue.DeQue<srcType>();
77+ auto stateOut = outQueue.AllocTensor<dstType>();
78+ Cast(stateOut, stateIn, roundMode, blockLen);
79+ outQueue.EnQue(stateOut);
80+ inQueue.FreeTensor(stateIn);
81+ // copy out
82+ auto outLocal = outQueue.DeQue<dstType>();
83+ DataCopyExtParams outParams{1, static_cast<uint32_t>(blockLen * sizeof(dstType)), 0, 0, 0};
84+ DataCopyPad(dst[i], outLocal, outParams);
85+ outQueue.FreeTensor(outLocal);
86+ }
87+ PipeBarrier<PIPE_V>();
88+ }
89+}
90+ 
91+ 
92+template <typename lowType, typename highType>
93+class CGDR {
94+public:
95+ __aicore__ inline CGDR(TPipe *pipe, const ChunkGatedDeltaRuleTilingData *tilingData)
96+ : stageOneOp_(stage1MT_)
97+ {
98+ pipe_ = pipe;
99+ tiling_ = tilingData;
100+ };
101+ 
102+ __aicore__ inline void InitMatmul()
103+ {
104+ if ASCEND_IS_AIC {
105+ // 使用 tiling 中的 matmul tiling 数据初始化
106+ stage1MT_.Init(&tiling_->matmulTilingFp32, pipe_);
107+ stage2MT_.Init(&tiling_->matmulTilingFp32, pipe_);
108+ stage3MT_.Init(&tiling_->matmulTilingFp32, pipe_);
109+ }
110+ }
111+ 
112+ __aicore__ inline void InitMask()
113+ {
114+ if ASCEND_IS_AIV {
115+ // 初始化mask矩阵
116+ uint32_t cBlockSize = tiling_->chunkSize * tiling_->chunkSize;
117+ pipe_->InitBuffer(tmpBuff_, cBlockSize * sizeof(float));
118+ auto cCFloat_ = tmpBuff_.GetWithOffset<float>(static_cast<uint32_t>(cBlockSize), 0);
119+ Duplicate<float>(cCFloat_, 0, tiling_->chunkSize);
120+ int32_t eventID = static_cast<int32_t>(pipe_->FetchEventID(HardEvent::V_MTE3));
121+ SetFlag<HardEvent::V_MTE3>(eventID);
122+ WaitFlag<HardEvent::V_MTE3>(eventID);
123+ DataCopyExtParams copyParams;
124+ copyParams.blockCount = static_cast<uint16_t>(1);
125+ copyParams.blockLen = static_cast<uint32_t>(tiling_->chunkSize * sizeof(float));
126+ copyParams.srcStride = static_cast<uint32_t>(0);
127+ copyParams.dstStride = static_cast<uint32_t>((0) * sizeof(float));
128+ for (int i = 0; i < tiling_->chunkSize; ++i) {
129+ DataCopyPad(stageOneMask_[GetBlockIdx() * cBlockSize + i * tiling_->chunkSize], cCFloat_, copyParams);
130+ eventID = static_cast<int32_t>(pipe_->FetchEventID(HardEvent::MTE3_S));
131+ SetFlag<HardEvent::MTE3_S>(eventID);
132+ WaitFlag<HardEvent::MTE3_S>(eventID);
133+ cCFloat_.SetValue(i, 1);
134+ eventID = static_cast<int32_t>(pipe_->FetchEventID(HardEvent::S_MTE3));
135+ SetFlag<HardEvent::S_MTE3>(eventID);
136+ WaitFlag<HardEvent::S_MTE3>(eventID);
137+ DataCopyPad(stageThreeMask_[GetBlockIdx() * cBlockSize + i * tiling_->chunkSize], cCFloat_, copyParams);
138+ }
139+ eventID = static_cast<int32_t>(pipe_->FetchEventID(HardEvent::MTE3_MTE2));
140+ SetFlag<HardEvent::MTE3_MTE2>(eventID); // 这里必须同步, 否则会有提前拷出的问题
141+ WaitFlag<HardEvent::MTE3_MTE2>(eventID);
142+ pipe_->Reset();
143+ }
144+ }
145+ 
146+ __aicore__ inline void Init(const CGDRInitParams &initParams, GM_ADDR user)
147+ {
148+ uint64_t dataSize = tiling_->t * tiling_->nk * tiling_->dk;
149+ query_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(initParams.query), dataSize);
150+ key_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(initParams.key), dataSize);
151+ 
152+ dataSize = tiling_->t * tiling_->nv * tiling_->dv;
153+ value_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(initParams.value), dataSize);
154+ out_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(initParams.attnOut), dataSize);
155+ 
156+ dataSize = tiling_->t * tiling_->nv;
157+ beta_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(initParams.beta), dataSize);
158+ if (initParams.gOptional != nullptr) {
159+ g_.SetGlobalBuffer(reinterpret_cast<__gm__ highType *>(initParams.gOptional), dataSize);
160+ gFlag_ = true;
161+ }
162+ 
163+ dataSize = tiling_->b * tiling_->nv * tiling_->dv * tiling_->dk;
164+ initState_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(initParams.initState), dataSize);
165+ finalState_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(initParams.finalState), dataSize);
166+ 
167+ actualSeqLens_.SetGlobalBuffer(reinterpret_cast<__gm__ int32_t *>(initParams.seqlens), tiling_->b);
168+ 
169+ uint64_t offset = 0;
170+ gCum_.SetGlobalBuffer(reinterpret_cast<__gm__ highType *>(user + offset));
171+ offset += sizeof(highType) * tiling_->nv * tiling_->maxGroupLength;
172+ 
173+ kCumDecay_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(user + offset));
174+ offset += sizeof(lowType) * tiling_->nv * tiling_->maxGroupLength * tiling_->dk;
175+ 
176+ vInner_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(user + offset));
177+ offset += sizeof(lowType) * tiling_->nv * tiling_->maxGroupLength * tiling_->dv;
178+ 
179+ qPrime_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(user + offset));
180+ offset += sizeof(lowType) * tiling_->nv * tiling_->maxGroupLength * tiling_->dk;
181+ 
182+ attnInter_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(user + offset));
183+ offset += sizeof(lowType) * tiling_->nv * tiling_->maxGroupLength * tiling_->dv;
184+ 
185+ kg_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(user + offset));
186+ offset += sizeof(lowType) * tiling_->nv * tiling_->maxGroupLength * tiling_->dk;
187+ 
188+ qkt_.SetGlobalBuffer(reinterpret_cast<__gm__ lowType *>(user + offset));
189+ offset += sizeof(lowType) * tiling_->nv * tiling_->maxGroupLength * tiling_->chunkSize;
190+ 
191+ highState_.SetGlobalBuffer(reinterpret_cast<__gm__ highType *>(user + offset));
192+ offset += sizeof(highType) * tiling_->b * tiling_->nv * tiling_->dv * tiling_->dk;
193+ 
194+ stageOneMask_.SetGlobalBuffer(reinterpret_cast<__gm__ highType *>(user + offset));
195+ offset += sizeof(highType) * tiling_->chunkSize * tiling_->chunkSize * tiling_->aiCoreNum * TASK_RATIO;
196+ 
197+ stageThreeMask_.SetGlobalBuffer(reinterpret_cast<__gm__ highType *>(user + offset));
198+ offset += sizeof(highType) * tiling_->chunkSize * tiling_->chunkSize * tiling_->aiCoreNum * TASK_RATIO;
199+ 
200+ stageWsAddr_ = user + offset;
201+ 
202+ InitMatmul();
203+ InitMask();
204+ }
205+ 
206+ __aicore__ inline void Process()
207+ {
208+ SyncAll<false>();
209+ int64_t seqStart = 0;
210+ int64_t seqEnd = 0;
211+ ChunkGroup cg;
212+ cg.chunkSize = tiling_->chunkSize;
213+ for (int64_t bid = 0; bid < tiling_->b; bid++) {
214+ int32_t length = actualSeqLens_.GetValue(bid);
215+ seqStart = seqEnd;
216+ seqEnd = seqStart + (int64_t)length;
217+ for (int64_t pos = seqStart; pos < seqEnd; pos += tiling_->maxGroupLength) {
218+ // set chunk group
219+ cg.startPos = pos;
220+ if (pos + tiling_->maxGroupLength > seqEnd) {
221+ cg.length = seqEnd - pos;
222+ } else {
223+ cg.length = tiling_->maxGroupLength;
224+ }
225+ auto curState = (pos == seqStart) ?
226+ initState_[bid * tiling_->nv * tiling_->dv * tiling_->dk] :
227+ finalState_[bid * tiling_->nv * tiling_->dv * tiling_->dk];
228+ // compute this chunk group
229+ RunStage1(cg);
230+ SyncAll<false>();
231+ 
232+ RunStage2(cg, curState,
233+ finalState_[bid * tiling_->nv * tiling_->dv * tiling_->dk]);
234+ SyncAll<false>();
235+ 
236+ RunStage3(cg);
237+ SyncAll<false>();
238+ }
239+ }
240+ }
241+ 
242+private:
243+ __aicore__ inline void RunStage1(const ChunkGroup& cg)
244+ {
245+ GDRStageOneInitParams initStageOneParams {query_, key_, value_, beta_, g_,
246+ gCum_, kCumDecay_, vInner_, qPrime_, kg_, qkt_,
247+ stageWsAddr_, stageOneMask_, cg, gFlag_};
248+ stageOneOp_.Init(initStageOneParams, pipe_, tiling_);
249+ stageOneOp_.Process();
250+ pipe_->Reset();
251+ }
252+ 
253+ __aicore__ inline void RunStage2(ChunkGroup& cg, GlobalTensor<lowType> stateIn,
254+ GlobalTensor<lowType> stateOut)
255+ {
256+ Stage2 stageTwoOp;
257+ StageTwoParams initStageTwoParams{
258+ qPrime_, vInner_, gCum_, kCumDecay_, stateIn, stateOut, kg_,
259+ out_[cg.startPos * tiling_->nv * tiling_->dv], stageWsAddr_, &stage2MT_, pipe_, &cg,
260+ tiling_->nv, tiling_->nk, tiling_->dv, tiling_->dk, gFlag_};
261+ stageTwoOp.Init(&initStageTwoParams, tiling_->aiCoreNum);
262+ stageTwoOp.Process();
263+ pipe_->Reset();
264+ }
265+ 
266+ __aicore__ inline void RunStage3(ChunkGroup& cg)
267+ {
268+ Stage3 stageThreeOp;
269+ StageThreeParams initStageThreeParams{
270+ qkt_, gCum_, vInner_,
271+ stageThreeMask_[int(GetBlockIdx() / 2) * tiling_->chunkSize * tiling_->chunkSize],
272+ stageWsAddr_, out_[cg.startPos * tiling_->nv * tiling_->dv],
273+ &stage3MT_, pipe_, &cg, tiling_->scale,
274+ tiling_->nv, tiling_->nk, tiling_->dv, tiling_->dk, gFlag_};
275+ stageThreeOp.Init(&initStageThreeParams, tiling_->aiCoreNum);
276+ stageThreeOp.Process();
277+ pipe_->Reset();
278+ }
279+ 
280+private:
281+ TPipe *pipe_;
282+ const ChunkGatedDeltaRuleTilingData *tiling_;
283+ GlobalTensor<lowType> query_;
284+ GlobalTensor<lowType> key_;
285+ GlobalTensor<lowType> value_;
286+ GlobalTensor<lowType> beta_;
287+ GlobalTensor<highType> g_;
288+ GlobalTensor<lowType> out_;
289+ float scale_;
290+ GlobalTensor<lowType> finalState_;
291+ GlobalTensor<lowType> initState_;
292+ GlobalTensor<int32_t> actualSeqLens_;
293+ 
294+ GlobalTensor<highType> gCum_; // (Nv, maxGroupLength)
295+ GlobalTensor<lowType> kCumDecay_; // (Nv, maxGroupLength, Dk)
296+ GlobalTensor<lowType> vInner_; // (Nv, maxGroupLength, Dv)
297+ GlobalTensor<lowType> qPrime_; // (Nv, maxGroupLength, Dk)
298+ GlobalTensor<lowType> attnInter_; // (Nv, maxGroupLength, Dv)
299+ GlobalTensor<lowType> kg_; // (Nv, maxGroupLength, Dk)
300+ GlobalTensor<lowType> qkt_; // (Nv, maxGroupLength, C)
301+ GlobalTensor<highType> highState_;
302+ // mask矩阵
303+ GlobalTensor<highType> stageOneMask_; // (Nv, maxGroupLength, C)
304+ GlobalTensor<highType> stageThreeMask_; // (Nv, maxGroupLength, C)
305+ GM_ADDR stageWsAddr_; // temporary space addr for stages
306+ 
307+ TBuf<TPosition::VECCALC> tmpBuff_; // 构造mask矩阵
308+ 
309+ // Matmul objects
310+ StageOneMT stage1MT_;
311+ StageTwoMT stage2MT_;
312+ StageThreeMT stage3MT_;
313+ 
314+ // Stage operators
315+ Stage1 stageOneOp_;
316+ bool gFlag_ = false;
317+};
318+ 
319+} // namespace ChunkGatedDeltaRule
320+#endif // CHUNK_GATED_DELTA_RULE_H
@@ -0,0 +1,270 @@
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 chunk_gated_delta_rule_stage2.h
13+ * \brief
14+ */
15+#ifndef CHUNK_GATED_DELTA_RULE_STAGE2_H
16+#define CHUNK_GATED_DELTA_RULE_STAGE2_H
17+ 
18+#include "kernel_tiling/kernel_tiling.h"
19+#include "chunk_gated_delta_rule_utils.h"
20+#include "../chunk_gated_delta_rule_tiling_data.h"
21+ 
22+namespace ChunkGatedDeltaRule {
23+using namespace AscendC;
24+using namespace matmul;
25+ 
26+using aT2 = MatmulType<TPosition::GM, CubeFormat::ND, bfloat16_t, true>;
27+using bT2 = MatmulType<TPosition::GM, CubeFormat::ND, bfloat16_t, true>;
28+using cT2 = MatmulType<TPosition::GM, CubeFormat::ND, bfloat16_t>;
29+using StageTwoMT = matmul::MatmulImpl<aT2, bT2, cT2>;
30+ 
31+struct StageTwoParams {
32+ GlobalTensor<bfloat16_t> qPrime; // (Nv, Sp, Dk)
33+ GlobalTensor<bfloat16_t> vInner; // (Nv, Sp, Dv)
34+ GlobalTensor<float> gCum; // (Nv, Sp)
35+ GlobalTensor<bfloat16_t> kCumdecay; // (Nv, Sp, Dk)
36+ GlobalTensor<bfloat16_t> curState; // (Nv, Dv, Dk)
37+ GlobalTensor<bfloat16_t> finalState; // (Nv, Dv, Dk)
38+ GlobalTensor<bfloat16_t> kg;
39+ GlobalTensor<bfloat16_t> out;
40+ GM_ADDR ws;
41+ StageTwoMT *mm1;
42+ TPipe *pipe;
43+ ChunkGroup *cg;
44+ int64_t Nv;
45+ int64_t Nk;
46+ int64_t Dv;
47+ int64_t Dk;
48+ bool gOptional;
49+};
50+ 
51+class Stage2 {
52+public:
53+ __aicore__ inline void Init(StageTwoParams *initParams, int32_t coreNum)
54+ {
55+ sTP_ = initParams;
56+ pipe_ = sTP_->pipe;
57+ chunkSize_ = sTP_->cg->chunkSize;
58+ seqLength_ = sTP_->cg->length;
59+ Sp_ = (seqLength_ + chunkSize_ - 1) / chunkSize_ * chunkSize_;
60+ chunkNum_ = Sp_ / chunkSize_;
61+ coreNum_ = coreNum;
62+ Nv_ = sTP_->Nv;
63+ Nk_ = sTP_->Nk;
64+ Dv_ = sTP_->Dv;
65+ Dk_ = sTP_->Dk;
66+ curDk_ = Ceil(Dk_, BLOCK_SIZE / sizeof(bfloat16_t)) * (BLOCK_SIZE / sizeof(bfloat16_t));
67+ curChunkSize_ = chunkSize_;
68+ gOptional_ = sTP_->gOptional;
69+ InitLocalBuffers();
70+ }
71+ 
72+ __aicore__ inline void InitLocalBuffers()
73+ {
74+ if ASCEND_IS_AIC {
75+ return;
76+ }
77+ pipe_->InitBuffer(inQueue_, BUFFER_NUM_ONE, Std::max((uint64_t)chunkSize_,
78+ (uint64_t)Dv_) * curDk_ * sizeof(bfloat16_t));
79+ uint64_t outQueueSize = Std::max((uint64_t)chunkSize_ * chunkSize_ * sizeof(bfloat16_t),
80+ (uint64_t)Dv_ * curDk_ * sizeof(bfloat16_t));
81+ pipe_->InitBuffer(outQueue_, BUFFER_NUM_ONE, outQueueSize);
82+ pipe_->InitBuffer(tmpBuff_, (Std::max((uint64_t)chunkSize_, (uint64_t)Dv_) * curDk_ +
83+ BLOCK_FLOAT_NUM) *sizeof(float));
84+ uint32_t buffOffset = 0;
85+ tmpBuffer1_ = tmpBuff_.GetWithOffset<float>(static_cast<uint32_t>(Dv_ * curDk_), buffOffset);
86+ buffOffset += Ceil(Dv_ * curDk_ * sizeof(float), BLOCK_SIZE) * BLOCK_SIZE;
87+ // 暂存所取的最后一位数
88+ lastGCum_ = tmpBuff_.GetWithOffset<float>(static_cast<uint32_t>(NUM_ONE), buffOffset);
89+ }
90+ 
91+ __aicore__ inline void Process()
92+ {
93+ int64_t coreId = GetBlockIdx();
94+ if ASCEND_IS_AIV {
95+ coreId /= AIC_AIV_1_1;
96+ }
97+ int64_t nvPerCore = (Nv_ + coreNum_ - 1) / coreNum_;
98+ int64_t nvStart = coreId * nvPerCore;
99+ int64_t nvEnd = nvStart + nvPerCore;
100+ nvEnd = nvEnd > Nv_ ? Nv_ : nvEnd;
101+ int64_t lastChunkSize = seqLength_ % chunkSize_ == 0 ? chunkSize_ : seqLength_ % chunkSize_;
102+ for (int64_t nvId = nvStart; nvId < nvEnd; nvId++) {
103+ curChunkSize_ = chunkSize_;
104+ for (int64_t cId = 0; cId < chunkNum_; cId++) {
105+ auto curState = (cId == 0) ? sTP_->curState[nvId * Dv_ * Dk_] : sTP_->finalState[nvId * Dv_ * Dk_];
106+ auto finalState = sTP_->finalState[nvId * Dv_ * Dk_];
107+ int64_t length = cId * chunkSize_;
108+ if (cId == chunkNum_ - 1) {
109+ curChunkSize_ = lastChunkSize;
110+ }
111+ if ASCEND_IS_AIV {
112+ if (GetSubBlockIdx() == 0) {
113+ CopyIn(curState, Dv_, Dk_);
114+ CalGCumExp(sTP_->gCum[nvId * Sp_ + length]);
115+ }
116+ CrossCoreWaitFlag(0x2);
117+ if (GetSubBlockIdx() == 0) {
118+ CopyOutState(finalState);
119+ }
120+ CrossCoreSetFlag<0x2, PIPE_MTE3>(0x5);
121+ CrossCoreWaitFlag(0x4);
122+ }
123+ if ASCEND_IS_AIC {
124+ uint64_t mm_offset0 = nvId * Sp_ * Dk_ + length * Dk_;
125+ uint64_t mm_offset1 = nvId * Sp_ * Dv_ + length * Dv_;
126+ CalVPrime(sTP_->kCumdecay[mm_offset0], curState, sTP_->vInner[mm_offset1]);
127+ CalAttnInter(sTP_->qPrime[mm_offset0], curState,
128+ sTP_->out[nvId * Dv_ + cId * chunkSize_ * Nv_ * Dv_]);
129+ CrossCoreSetFlag<0x2, PIPE_FIX>(0x2); // 读完之前AIV不能写
130+ CrossCoreWaitFlag(0x5);
131+ CalStateNew(sTP_->vInner[mm_offset1], sTP_->kg[mm_offset0], finalState);
132+ SetFlag<HardEvent::FIX_MTE2>(FIX_MTE2_EVENT);
133+ WaitFlag<HardEvent::FIX_MTE2>(FIX_MTE2_EVENT);
134+ CrossCoreSetFlag<0x2, PIPE_FIX>(0x4);
135+ }
136+ }
137+ }
138+ }
139+ 
140+ __aicore__ inline void CalGCumExp(GlobalTensor<float> gCum)
141+ {
142+ if (gOptional_) {
143+ // 刷新cache
144+ DataCacheCleanAndInvalid<float,
145+ CacheLine::SINGLE_CACHE_LINE,
146+ DcciDst::CACHELINE_OUT>(gCum[curChunkSize_ - 1]);
147+ float tmpFloat = gCum.GetValue(curChunkSize_ - 1);
148+ lastGCum_.SetValue(0, tmpFloat);
149+ SetFlag<HardEvent::S_V>(S_V_EVENT);
150+ WaitFlag<HardEvent::S_V>(S_V_EVENT);
151+ Exp<float, 0, true>(lastGCum_, lastGCum_, 1);
152+ } else {
153+ lastGCum_.SetValue(0, 1.0f);
154+ }
155+ float tmpFloat = lastGCum_.GetValue(0);
156+ auto stateIn = inQueue_.DeQue<bfloat16_t>();
157+ auto stateOut = outQueue_.AllocTensor<bfloat16_t>();
158+ SetFlag<HardEvent::MTE2_V>(MTE2_V_EVENT);
159+ WaitFlag<HardEvent::MTE2_V>(MTE2_V_EVENT);
160+ Cast(tmpBuffer1_, stateIn, RoundMode::CAST_NONE, Dv_ * curDk_);
161+ SetFlag<HardEvent::S_V>(S_V_EVENT);
162+ WaitFlag<HardEvent::S_V>(S_V_EVENT);
163+ Muls(tmpBuffer1_, tmpBuffer1_, tmpFloat, Dv_ * curDk_);
164+ Cast(stateOut, tmpBuffer1_, RoundMode::CAST_RINT, Dv_ * curDk_);
165+ SetFlag<HardEvent::V_MTE3>(V_MTE3_EVENT);
166+ WaitFlag<HardEvent::V_MTE3>(V_MTE3_EVENT);
167+ outQueue_.EnQue(stateOut);
168+ inQueue_.FreeTensor(stateIn);
169+ }
170+ 
171+ __aicore__ inline void CalAttnInter(GlobalTensor<bfloat16_t> qPrime,
172+ GlobalTensor<bfloat16_t> state,
173+ GlobalTensor<bfloat16_t> out)
174+ {
175+ // q_prime @ state.transpose(0, 1)
176+ sTP_->mm1->SetOrgShape(curChunkSize_, Dv_, Dk_, Dk_, Nv_ * Dv_); // MNKaKbN
177+ sTP_->mm1->SetSingleShape(curChunkSize_, Dv_, Dk_); // SingleCoreMNK
178+ sTP_->mm1->SetTensorA(qPrime, false);
179+ sTP_->mm1->SetTensorB(state, true);
180+ sTP_->mm1->IterateAll(out);
181+ sTP_->mm1->End();
182+ }
183+ 
184+ __aicore__ inline void CalVPrime(GlobalTensor<bfloat16_t> kCumdecay,
185+ GlobalTensor<bfloat16_t> state,
186+ GlobalTensor<bfloat16_t> vPrime)
187+ {
188+ // v_inner += k_cumdecay @ state.transpose(0, 1)
189+ sTP_->mm1->SetOrgShape(curChunkSize_, Dv_, Dk_); // MNK
190+ sTP_->mm1->SetSingleShape(curChunkSize_, Dv_, Dk_); // SingleCoreMNK
191+ sTP_->mm1->SetTensorA(kCumdecay, false);
192+ sTP_->mm1->SetTensorB(state, true);
193+ sTP_->mm1->IterateAll(vPrime, 1);
194+ sTP_->mm1->End();
195+ }
196+ 
197+ __aicore__ inline void CalStateNew(GlobalTensor<bfloat16_t> vInner,
198+ GlobalTensor<bfloat16_t> kg,
199+ GlobalTensor<bfloat16_t> state)
200+ {
201+ // state_out = v_new.transpose(0, 1) @ kg
202+ sTP_->mm1->SetOrgShape(Dv_, Dk_, curChunkSize_); // MNK
203+ sTP_->mm1->SetSingleShape(Dv_, Dk_, curChunkSize_); // SingleCoreMNK
204+ sTP_->mm1->SetTensorA(vInner, true);
205+ sTP_->mm1->SetTensorB(kg, false);
206+ sTP_->mm1->IterateAll(state, 1);
207+ sTP_->mm1->End();
208+ }
209+ 
210+ template <typename inType>
211+ __aicore__ inline void CopyIn(GlobalTensor<inType> tmpGM, int32_t row, int32_t col)
212+ {
213+ LocalTensor<inType> inLocal = inQueue_.AllocTensor<inType>();
214+ DataCopyExtParams inParams{static_cast<uint16_t>(row),
215+ static_cast<uint32_t>(col * sizeof(inType)), // 非对齐情况需要补0
216+ static_cast<uint32_t>(0),
217+ 0, 0};
218+ int padding = Ceil(col, BLOCK_SIZE / sizeof(inType)) * (BLOCK_SIZE / sizeof(inType)) - col;
219+ DataCopyPadExtParams<inType> copyPadParams{true, 0, static_cast<uint8_t>(padding), 0};
220+ DataCopyPad(inLocal, tmpGM, inParams, copyPadParams);
221+ inQueue_.EnQue(inLocal);
222+ }
223+ 
224+ __aicore__ inline void CopyOutState(GlobalTensor<bfloat16_t> stateNew)
225+ {
226+ CopyOut<bfloat16_t>(stateNew, Dv_, Dk_, false);
227+ }
228+ 
229+ template <typename outType>
230+ __aicore__ inline void CopyOut(GlobalTensor<outType> tmpGM, int32_t row, int32_t col, bool setAtomic = false)
231+ {
232+ auto outLocal = outQueue_.DeQue<outType>();
233+ DataCopyExtParams copyParams;
234+ copyParams.blockCount = static_cast<uint16_t>(row);
235+ copyParams.blockLen = static_cast<uint32_t>(col * sizeof(outType));
236+ copyParams.srcStride = static_cast<uint32_t>(0);
237+ copyParams.dstStride = static_cast<uint32_t>(0);
238+ if (setAtomic) {
239+ SetAtomicAdd<outType>();
240+ }
241+ DataCopyPad(tmpGM, outLocal, copyParams);
242+ if (setAtomic) {
243+ SetAtomicNone();
244+ }
245+ outQueue_.FreeTensor(outLocal);
246+ }
247+ 
248+private:
249+ StageTwoParams *sTP_;
250+ TPipe *pipe_;
251+ TQue<QuePosition::VECIN, BUFFER_NUM_ONE> inQueue_;
252+ TQue<QuePosition::VECOUT, BUFFER_NUM_ONE> outQueue_;
253+ TBuf<TPosition::VECCALC> tmpBuff_;
254+ LocalTensor<float> tmpBuffer1_;
255+ LocalTensor<float> lastGCum_;
256+ int64_t Nk_;
257+ int64_t Nv_;
258+ int64_t Dk_;
259+ int64_t Dv_;
260+ int64_t seqLength_;
261+ int32_t chunkSize_;
262+ int32_t curChunkSize_;
263+ int32_t curDk_;
264+ int64_t Sp_;
265+ int32_t chunkNum_;
266+ int32_t coreNum_;
267+ bool gOptional_;
268+};
269+} // namespace ChunkGatedDeltaRule
270+#endif // CHUNK_GATED_DELTA_RULE_STAGE2_H
@@ -0,0 +1,242 @@
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 chunk_gated_delta_rule_stage3.h
13+ * \brief
14+ */
15+#ifndef CHUNK_GATED_DELTA_RULE_STAGE3_H
16+#define CHUNK_GATED_DELTA_RULE_STAGE3_H
17+ 
18+#include "kernel_tiling/kernel_tiling.h"
19+#include "chunk_gated_delta_rule_utils.h"
20+#include "../chunk_gated_delta_rule_tiling_data.h"
21+ 
22+namespace ChunkGatedDeltaRule {
23+using namespace AscendC;
24+using namespace matmul;
25+ 
26+using aT3 = MatmulType<TPosition::GM, CubeFormat::ND, bfloat16_t, true>;
27+using bT3 = MatmulType<TPosition::GM, CubeFormat::ND, bfloat16_t, true>;
28+using cT3 = MatmulType<TPosition::GM, CubeFormat::ND, bfloat16_t>;
29+using StageThreeMT = matmul::MatmulImpl<aT3, bT3, cT3>;
30+ 
31+struct StageThreeParams {
32+ GlobalTensor<bfloat16_t> qkt; // (Nv, Sp, Dk)
33+ GlobalTensor<float> gCumExp; // (Nv, Sp)
34+ GlobalTensor<bfloat16_t> vInner;
35+ GlobalTensor<float> maskTensor;
36+ GM_ADDR ws;
37+ GlobalTensor<bfloat16_t> attnOut;
38+ StageThreeMT *mm3;
39+ TPipe *pipe;
40+ ChunkGroup *cg;
41+ float scale;
42+ int64_t Nv;
43+ int64_t Nk;
44+ int64_t Dv;
45+ int64_t Dk;
46+ bool gOptional;
47+};
48+ 
49+class Stage3 {
50+public:
51+ __aicore__ inline void Init(StageThreeParams *initParams, int32_t coreNum)
52+ {
53+ coreId_ = GetBlockIdx();
54+ sTP_ = initParams;
55+ pipe_ = sTP_->pipe;
56+ chunkSize_ = sTP_->cg->chunkSize;
57+ seqLength_ = sTP_->cg->length;
58+ Sp_ = (seqLength_ + chunkSize_ - 1) / chunkSize_ * chunkSize_;
59+ chunkNum_ = (seqLength_ + chunkSize_ - 1) / chunkSize_ ;
60+ coreNum_ = coreNum;
61+ Nv_ = sTP_->Nv;
62+ Nk_ = sTP_->Nk;
63+ Dv_ = sTP_->Dv;
64+ Dk_ = sTP_->Dk;
65+ paddedDv_ = Ceil(Dv_, BLOCK_SIZE / sizeof(bfloat16_t)) * (BLOCK_SIZE / sizeof(bfloat16_t));
66+ gOptional_ = sTP_->gOptional;
67+ uint64_t workSpaceOffset = 0;
68+ tmpGM_.SetGlobalBuffer(reinterpret_cast<__gm__ bfloat16_t *>(
69+ initParams->ws + workSpaceOffset +
70+ coreNum_ * chunkSize_ * chunkSize_ * sizeof(float)));
71+ if ASCEND_IS_AIC {
72+ return;
73+ }
74+ coreId_ /= AIC_AIV_1_1;
75+ if (GetSubBlockIdx() == 1) {
76+ return;
77+ }
78+ uint64_t inQueueSize = static_cast<uint64_t>(chunkSize_) *
79+ AscendC::Std::max((int64_t)chunkSize_, paddedDv_) * sizeof(bfloat16_t);
80+ pipe_->InitBuffer(inQueue_, BUFFER_NUM_ONE, inQueueSize);
81+ pipe_->InitBuffer(outQueue_, BUFFER_NUM_ONE, chunkSize_ > paddedDv_ ?
82+ chunkSize_ * chunkSize_ * sizeof(float) : chunkSize_ * paddedDv_ * sizeof(bfloat16_t));
83+ pipe_->InitBuffer(tmpBuff_, (STAGE3_BUFFER_COUNT * chunkSize_ * chunkSize_ * sizeof(float)));
84+ uint64_t buffOffset = 0;
85+ uint64_t tmpOffset = chunkSize_ * chunkSize_;
86+ tmpBuffer1_ = tmpBuff_.GetWithOffset<float>(static_cast<uint32_t>(tmpOffset), buffOffset);
87+ buffOffset += tmpOffset * sizeof(float);
88+ tmpBuffer2_ = tmpBuff_.GetWithOffset<float>(static_cast<uint32_t>(tmpOffset), buffOffset);
89+ buffOffset += tmpOffset * sizeof(float);
90+ maskBuffer_ = tmpBuff_.GetWithOffset<float>(static_cast<uint32_t>(tmpOffset), buffOffset);
91+ 
92+ // 搬入mask
93+ DataCopyExtParams inParams{static_cast<uint16_t>(chunkSize_),
94+ static_cast<uint32_t>(chunkSize_ * sizeof(float)),
95+ 0, 0, 0};
96+ DataCopyPadExtParams<float> copyPadParams{false, 0, 0, 0};
97+ DataCopyPad(maskBuffer_, sTP_->maskTensor, inParams, copyPadParams);
98+ int32_t eventID = static_cast<int32_t>(pipe_->FetchEventID(HardEvent::MTE2_V));
99+ SetFlag<HardEvent::MTE2_V>(eventID);
100+ WaitFlag<HardEvent::MTE2_V>(eventID);
101+ }
102+ 
103+ __aicore__ inline void Process()
104+ {
105+ int64_t totalChunks = Nv_ * chunkNum_; // Nv Nc 融合
106+ int64_t chunksPerCore = (totalChunks + coreNum_ -1) / coreNum_;
107+ int64_t lastChunkSize = seqLength_ % chunkSize_ == 0 ? chunkSize_ : seqLength_ % chunkSize_;
108+ int64_t startChunk = coreId_ * chunksPerCore;
109+ int64_t endChunk = startChunk + chunksPerCore > totalChunks ? totalChunks : startChunk + chunksPerCore;
110+ for (int64_t idx = startChunk; idx < endChunk; idx++) {
111+ int64_t nvId = idx / chunkNum_;
112+ int64_t chunkId = idx % chunkNum_;
113+ int64_t chunkPos = chunkId * chunkSize_; // 当前chunk起始位置
114+ curChunkSize_ = (chunkId == chunkNum_ - 1) ? lastChunkSize : chunkSize_; // 尾块
115+ if ASCEND_IS_AIV {
116+ if (GetSubBlockIdx() == 0) {
117+ CalMaskedQKT(tmpGM_[coreId_ * chunkSize_ * chunkSize_], nvId, chunkPos);
118+ }
119+ CrossCoreSetFlag<0x2, PIPE_MTE3>(0x4);
120+ CrossCoreWaitFlag(0x3);
121+ }
122+ 
123+ if ASCEND_IS_AIC {
124+ CrossCoreWaitFlag(0x4);
125+ AICProcess(tmpGM_[coreId_ * chunkSize_ * chunkSize_],
126+ sTP_->vInner[nvId * Sp_ * Dv_ + chunkPos * Dv_],
127+ sTP_->attnOut[nvId * Dv_ + chunkPos * Nv_ * Dv_]);
128+ CrossCoreSetFlag<0x2, PIPE_FIX>(0x3);
129+ }
130+ }
131+ }
132+
133+ __aicore__ inline void CalMaskedQKT(GlobalTensor<bfloat16_t> outGM, int nvId, int chunkPos)
134+ {
135+ // chunkSize 大小进行自动补齐
136+ if (gOptional_) {
137+ AlignedCopyIn(sTP_->gCumExp[nvId * Sp_ + chunkPos], 1, curChunkSize_); // 自动补齐
138+ auto g_cum = inQueue_.DeQue<float>();
139+ const uint32_t srcShape1[] = {static_cast<uint32_t>(chunkSize_), static_cast<uint32_t>(1)};
140+ const uint32_t srcShape2[] = {static_cast<uint32_t>(1), static_cast<uint32_t>(chunkSize_)};
141+ const uint32_t dstShape[] = {static_cast<uint32_t>(chunkSize_),
142+ static_cast<uint32_t>(chunkSize_)};
143+ Broadcast<float, BROADCAST_AXIS, 1>(tmpBuffer1_, g_cum, dstShape, srcShape1);
144+ Broadcast<float, BROADCAST_AXIS, 0>(tmpBuffer2_, g_cum, dstShape, srcShape2);
145+ PipeBarrier<PIPE_V>();
146+ Sub(tmpBuffer1_, tmpBuffer1_, tmpBuffer2_, curChunkSize_ * chunkSize_);
147+ PipeBarrier<PIPE_V>();
148+ Mul(tmpBuffer1_, tmpBuffer1_, maskBuffer_, curChunkSize_ * chunkSize_);
149+ PipeBarrier<PIPE_V>();
150+ Exp<float, 0, true>(tmpBuffer1_, tmpBuffer1_, curChunkSize_ * chunkSize_);
151+ PipeBarrier<PIPE_V>();
152+ inQueue_.FreeTensor(g_cum);
153+ } else {
154+ Duplicate(tmpBuffer1_, static_cast<float>(1.0f), curChunkSize_ * chunkSize_);
155+ PipeBarrier<PIPE_V>();
156+ }
157+
158+ // qkt
159+ AlignedCopyIn(sTP_->qkt[nvId * Sp_ * chunkSize_ + chunkPos * chunkSize_], curChunkSize_, curChunkSize_);
160+ auto qkt = inQueue_.DeQue<bfloat16_t>();
161+ auto scale_qkt = outQueue_.AllocTensor<bfloat16_t>();
162+ Cast(tmpBuffer2_, qkt, RoundMode::CAST_NONE, curChunkSize_ * chunkSize_);
163+ Muls(tmpBuffer2_, tmpBuffer2_, sTP_->scale, curChunkSize_ * chunkSize_);
164+ Mul(tmpBuffer2_, tmpBuffer2_, tmpBuffer1_, curChunkSize_ * chunkSize_);
165+ // mask
166+ Mul(tmpBuffer2_, tmpBuffer2_, maskBuffer_, curChunkSize_ * chunkSize_);
167+ Cast(scale_qkt, tmpBuffer2_, RoundMode::CAST_RINT, curChunkSize_ * chunkSize_);
168+ outQueue_.EnQue(scale_qkt);
169+ AlignedCopyOut(outGM, curChunkSize_, curChunkSize_);
170+ inQueue_.FreeTensor(qkt);
171+ }
172+ 
173+ __aicore__ inline void AICProcess(GlobalTensor<bfloat16_t>tmpGM,
174+ GlobalTensor<bfloat16_t>vInner,
175+ GlobalTensor<bfloat16_t>attnInter)
176+ {
177+ // masked_qkt @ v_inner
178+ sTP_->mm3->SetOrgShape(curChunkSize_, Dv_, curChunkSize_, curChunkSize_, Nv_ * Dv_); // MNK
179+ sTP_->mm3->SetSingleShape(curChunkSize_, Dv_, curChunkSize_); // SingleCoreMNK
180+ sTP_->mm3->SetTensorA(tmpGM);
181+ sTP_->mm3->SetTensorB(vInner);
182+ sTP_->mm3->IterateAll(attnInter, 1);
183+ sTP_->mm3->End();
184+ }
185+ 
186+ template <typename inType>
187+ __aicore__ inline void AlignedCopyIn(GlobalTensor<inType> tmpGM, int32_t row, int32_t col)
188+ {
189+ LocalTensor<inType> inLocal = inQueue_.AllocTensor<inType>();
190+ // 非对齐拷入会自动对齐, 然后离散拷入UB
191+ int paddingCol = Ceil(col, BLOCK_SIZE / sizeof(inType)) * (BLOCK_SIZE / sizeof(inType));
192+ DataCopyExtParams inParams{static_cast<uint16_t>(row),
193+ static_cast<uint32_t>(col * sizeof(inType)),
194+ static_cast<uint32_t>(0),
195+ static_cast<uint32_t>((chunkSize_ - paddingCol) * sizeof(inType) / BLOCK_SIZE),
196+ 0};
197+ DataCopyPadExtParams<inType> copyPadParams{false, 0, 0, 0};
198+ DataCopyPad(inLocal, tmpGM, inParams, copyPadParams);
199+ inQueue_.EnQue(inLocal);
200+ }
201+ 
202+ template <typename outType>
203+ __aicore__ inline void AlignedCopyOut(GlobalTensor<outType> tmpGM, int32_t row, int32_t col)
204+ {
205+ auto outLocal = outQueue_.DeQue<outType>();
206+ int paddingCol = Ceil(col, BLOCK_SIZE / sizeof(outType)) * (BLOCK_SIZE / sizeof(outType));
207+ DataCopyExtParams copyParams;
208+ copyParams.blockCount = static_cast<uint16_t>(row);
209+ copyParams.blockLen = static_cast<uint32_t>(col * sizeof(outType));
210+ copyParams.srcStride = static_cast<uint32_t>((chunkSize_ - paddingCol) * sizeof(outType) / BLOCK_SIZE);
211+ copyParams.dstStride = static_cast<uint32_t>((0) * sizeof(outType));
212+ DataCopyPad(tmpGM, outLocal, copyParams);
213+ outQueue_.FreeTensor(outLocal);
214+ }
215+ 
216+private:
217+ StageThreeParams *sTP_;
218+ TPipe *pipe_;
219+ TQue<QuePosition::VECIN, BUFFER_NUM_ONE> inQueue_;
220+ TQue<QuePosition::VECOUT, BUFFER_NUM_ONE> outQueue_;
221+ TBuf<TPosition::VECCALC> tmpBuff_;
222+ GlobalTensor<bfloat16_t> tmpGM_;
223+ LocalTensor<float> tmpBuffer1_;
224+ LocalTensor<float> tmpBuffer2_;
225+ LocalTensor<float> maskBuffer_;
226+ int64_t seqLength_;
227+ int64_t Sp_;
228+ int64_t Nv_;
229+ int64_t Nk_;
230+ int64_t Dv_;
231+ int64_t Dk_;
232+ int64_t paddedDv_;
233+ int32_t chunkNum_;
234+ int32_t coreNum_;
235+ int32_t curChunkSize_;
236+ int32_t chunkSize_;
237+ int32_t coreId_;
238+ bool gOptional_;
239+};
240+ 
241+} // namespace ChunkGatedDeltaRule
242+#endif // CHUNK_GATED_DELTA_RULE_STAGE3_H
@@ -0,0 +1,50 @@
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 chunk_gated_delta_rule_utils.h
13+ * \brief Common constants for chunk_gated_delta_rule kernels
14+ */
15+ 
16+#ifndef CHUNK_GATED_DELTA_RULE_UTILS_H__
17+#define CHUNK_GATED_DELTA_RULE_UTILS_H__
18+ 
19+#include "kernel_vec_intf.h"
20+#include "kernel_cube_intf.h"
21+#include "kernel_operator_list_tensor_intf.h"
22+#include "kernel_tiling/kernel_tiling.h"
23+ 
24+namespace ChunkGatedDeltaRule {
25+ // 同步信号
26+ constexpr uint64_t V_MTE3_EVENT = 0;
27+ constexpr uint64_t V_S_EVENT = 1;
28+ constexpr uint64_t MTE2_V_EVENT = 2;
29+ constexpr uint64_t S_V_EVENT = 3;
30+ constexpr uint64_t MTE3_MTE2_EVENT = 4;
31+ constexpr uint64_t MTE3_S_EVENT = 5;
32+ constexpr uint64_t FIX_MTE2_EVENT = 6;
33+ constexpr uint64_t S_MTE3_EVENT = 6;
34+ 
35+ constexpr uint64_t NUM_ONE = 1;
36+ constexpr uint64_t BUFFER_NUM_ONE = 1;
37+ constexpr uint64_t BUFFER_NUM_TWO = 2;
38+ constexpr uint64_t TQUE_DEPTH_TWO = 2;
39+ constexpr uint64_t AIC_AIV_1_1 = 2;
40+ constexpr uint64_t BROADCAST_AXIS = 2;
41+ constexpr uint64_t TASK_RATIO = 2;
42+ constexpr uint64_t STAGE3_BUFFER_COUNT = 4;
43+ constexpr uint32_t MAX_L0_SIZE = 64 * 1024; // 64KB
44+ constexpr uint32_t BLOCK_SIZE = 32; // copypad对齐块大小
45+ constexpr uint32_t BLOCK_FLOAT_NUM = 8;
46+ constexpr uint32_t BLOCK_BF16_NUM = 16;
47+ constexpr uint32_t TILE_LEN = 1024; // 1024 = 1kb,经测试1kb和10kb性能差异很小
48+} // ChunkGatedDeltaRule
49+ 
50+#endif // __CHUNK_GATED_DELTA_RULE_UTILS_H__
@@ -13,7 +13,7 @@
13 * \brief13 * \brief
14 */14 */
15 15 
16-#include "chunk_gated_delta_rule.h"16+#include "arch22/chunk_gated_delta_rule.h"
17#include "chunk_gated_delta_rule_tiling_data.h"17#include "chunk_gated_delta_rule_tiling_data.h"
18 18 
19using namespace AscendC;19using namespace AscendC;
@@ -0,0 +1,40 @@
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 chunk_gated_delta_rule.cpp
13+ * \brief
14+ */
15+ 
16+#include "arch35/chunk_gated_delta_rule.h"
17+#include "chunk_gated_delta_rule_tiling_data.h"
18+ 
19+using namespace AscendC;
20+using namespace matmul;
21+using namespace ChunkGatedDeltaRule;
22+ 
23+extern "C" __global__ __aicore__ void chunk_gated_delta_rule(
24+ GM_ADDR query, GM_ADDR key, GM_ADDR value, GM_ADDR beta, GM_ADDR initialState, GM_ADDR seqlens, GM_ADDR gOptional,
25+ GM_ADDR out, GM_ADDR finalState, GM_ADDR workspaceGM, GM_ADDR tilingGM)
26+{
27+ REGISTER_TILING_DEFAULT(ChunkGatedDeltaRuleTilingData);
28+ GET_TILING_DATA(tilingData, tilingGM);
29+ KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2);
30+ TPipe pipe;
31+ 
32+ __gm__ uint8_t *user = GetUserWorkspace(workspaceGM);
33+
34+ CGDR<bfloat16_t, float> op(&pipe, &tilingData);
35+ CGDRInitParams initParams{
36+ query, key, value, beta, initialState, seqlens, gOptional,
37+ out, finalState};
38+ op.Init(initParams, user);
39+ op.Process();
40+}