Merged
新增batch一致性需求的reduce模版 #519
sunzhongwen1Add on Jun 11
新增batch一致性需求的reduce模版 #519
Merged
sunzhongwen1Add on Jun 11
Total: 10 files changed+1013-80
@@ -22,6 +22,7 @@
22#include "reduce_operator.h"22#include "reduce_operator.h"
23#include "reduce_tensor_empty.h"23#include "reduce_tensor_empty.h"
24#include "reduce_tensor_move.h"24#include "reduce_tensor_move.h"
25+#include "reduce_sch_batch_invariant.h"
25 26 
26namespace Ops {27namespace Ops {
27namespace Base {28namespace Base {
@@ -36,8 +37,11 @@ struct EleSclar<ElemOp, true> {
36 using T = void;37 using T = void;
37};38};
38 39 
39-template <bool isContiguous, uint32_t PatternID, uint32_t LoopARCount, uint32_t LoopInnerARCount, class OpDag>40+template <
40-class ReduceSch {41+ bool isContiguous, bool batchInvariant, uint32_t PatternID, uint32_t LoopARCount, uint32_t LoopInnerARCount,
42+ class OpDag>
43+class ReduceSch
44+{
41public:45public:
42 constexpr static ReduceSchLoopInfo SchLoopInfo = GetSchLoopInfo<PatternID, LoopARCount, LoopInnerARCount>();46 constexpr static ReduceSchLoopInfo SchLoopInfo = GetSchLoopInfo<PatternID, LoopARCount, LoopInnerARCount>();
43 using Pattern = typename __reducePattern::GetPattern<SchLoopInfo.patternID>::T;47 using Pattern = typename __reducePattern::GetPattern<SchLoopInfo.patternID>::T;
@@ -244,19 +248,30 @@ public:
244 constexpr static ReduceSchLoopInfo groupSchLoopInfo = GetGroupSchLoopInfo();248 constexpr static ReduceSchLoopInfo groupSchLoopInfo = GetGroupSchLoopInfo();
245 ReduceOpTilingData groupTiling;249 ReduceOpTilingData groupTiling;
246 SetGroupTiling(groupTiling);250 SetGroupTiling(groupTiling);
247- if constexpr (isContiguous) {251+ if constexpr (isContiguous && !batchInvariant) {
248 using SchTypeR = ReduceSchAux<&SchLoopInfo, std::remove_reference_t<decltype(*this)>, true, OpDag>;252 using SchTypeR = ReduceSchAux<&SchLoopInfo, std::remove_reference_t<decltype(*this)>, true, OpDag>;
249 SchTypeR op(this, input_, &workspace_, &workspace_, tiling_);253 SchTypeR op(this, input_, &workspace_, &workspace_, tiling_);
250 using SchTypeA = ReduceSchAux<&groupSchLoopInfo, std::remove_reference_t<decltype(*this)>, false, OpDag>;254 using SchTypeA = ReduceSchAux<&groupSchLoopInfo, std::remove_reference_t<decltype(*this)>, false, OpDag>;
251 SchTypeA groupOp(this, input_, output_, &workspace_, &groupTiling);255 SchTypeA groupOp(this, input_, output_, &workspace_, &groupTiling);
252 256 
257+ op.Process(args...);
258+ SyncAll();
259+ groupOp.Process(args...);
260+ } else if constexpr (isContiguous && batchInvariant) {
261+ groupTiling.ubFactorA = tiling_->resultBlock / sizeof(DataType);
262+ groupTiling.factorACntPerCore = Ops::Base::CeilDiv(groupTiling.factorATotalCnt,
263+ static_cast<uint64_t>(tiling_->realCoreNum));
atomgit-bot

🟡 Medium Priority

变更行 260-263:ProcessGroup 中先调用 SetGroupTiling(groupTiling)(行 250),内部根据 ubFactorA = ELEMENT_ONE_REPEAT 计算了 factorATotalCnt = CeilDiv(outSize, ELEMENT_ONE_REPEAT)(参见 reduce_sch.h 行 306-321)。然后 batch-invariant 分支(行 261)覆盖 groupTiling.ubFactorA = tiling_->resultBlock,但未重新计算 factorATotalCnt。行 262-263 使用旧的 factorATotalCnt 来计算 factorACntPerCore。

ELEMENT_ONE_REPEAT 通常仅为数十字节,而 resultBlock 通常是数百字节,导致 factorATotalCnt 和 factorACntPerCore 均远大于合理值,进而导致 device 端 SetLoopRange 中 loopAEndIndex_ clamp 边界失效、核心工作负载分配错误。

Suggestions
263
+ groupTiling.ubFactorA = tiling_->resultBlock;
264
+ groupTiling.factorATotalCnt = Ops::Base::CeilDiv(groupTiling.shape[1], groupTiling.ubFactorA);
265
+ groupTiling.factorACntPerCore = Ops::Base::CeilDiv(groupTiling.factorATotalCnt,
263
266
  static_cast<uint64_t>(tiling_->realCoreNum));
Apply suggestion
likedislike
Incorrect?
264+ using SchTypeR = ReduceSchAuxBatchInvariant<&SchLoopInfo, std::remove_reference_t<decltype(*this)>, true, OpDag>;
265+ SchTypeR op(this, input_, &workspace_, &workspace_, tiling_);
266+ using SchTypeA = ReduceSchAuxBatchInvariant<&groupSchLoopInfo, std::remove_reference_t<decltype(*this)>, false, OpDag>;
267+ SchTypeA groupOp(this, input_, output_, &workspace_, &groupTiling);
268+ 
253 op.Process(args...);269 op.Process(args...);
254 SyncAll();270 SyncAll();
255 groupOp.Process(args...);271 groupOp.Process(args...);
256 } else {272 } else {
257 SetGroupTilingNonContiguous(groupTiling);273 SetGroupTilingNonContiguous(groupTiling);
258- using SchTypeR = ReduceSchAuxNonContiguous<&SchLoopInfo, std::remove_reference_t<decltype(*this)>, true,274+ using SchTypeR = ReduceSchAuxNonContiguous<&SchLoopInfo, std::remove_reference_t<decltype(*this)>, true, OpDag>;
259- OpDag>;
260 SchTypeR op(this, input_, &workspace_, &workspace_, tiling_);275 SchTypeR op(this, input_, &workspace_, &workspace_, tiling_);
261 using SchTypeA = ReduceSchAuxNonContiguous<&groupSchLoopInfo, std::remove_reference_t<decltype(*this)>,276 using SchTypeA = ReduceSchAuxNonContiguous<&groupSchLoopInfo, std::remove_reference_t<decltype(*this)>,
262 false, OpDag>;277 false, OpDag>;
@@ -0,0 +1,499 @@
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 reduce_sch_batch_invariant.h
13+ * \brief aux for reduce schedule batch invariant (contiguous only), single-stage process when loopRCount != 0
14+ */
15+ 
16+#ifndef _REDUCE_SCH_BATCH_INVARIANT_H_
17+#define _REDUCE_SCH_BATCH_INVARIANT_H_
18+#include "reduce_sch_aux_base.h"
19+ 
20+namespace Ops {
21+namespace Base {
22+namespace ReduceOpTmpl {
23+template <auto LoopInfo, class ReduceSch, bool IsStageOne, class OpDag = void>
24+struct ReduceSchAuxBatchInvariant
25+ : public ReduceSchAuxBase<
26+ LoopInfo, ReduceSch, IsStageOne, OpDag, ReduceSchAuxBatchInvariant<LoopInfo, ReduceSch, IsStageOne, OpDag>> {
27+ using AuxBase = ReduceSchAuxBase<
28+ LoopInfo, ReduceSch, IsStageOne, OpDag, ReduceSchAuxBatchInvariant<LoopInfo, ReduceSch, IsStageOne, OpDag>>;
29+ using InDType = typename AuxBase::InDType;
30+ 
31+public:
32+ constexpr static int32_t Dim = AuxBase::Dim;
33+ constexpr static uint64_t UB_BLOCK = AuxBase::UB_BLOCK;
34+ constexpr static uint64_t MIN_DTYPE_BYTES = AuxBase::MIN_DTYPE_BYTES;
35+ 
36+public:
37+ struct IterAddr {
38+ uint64_t start = 0;
39+ uint64_t stride = 1;
40+ } iterAddr_[Dim];
41+ 
42+public:
43+ __aicore__ inline ReduceSchAuxBatchInvariant(
44+ ReduceSch* sch, GlobalTensor<uint8_t>* input, GlobalTensor<uint8_t>* output, GlobalTensor<uint8_t>* workspace,
45+ const ReduceOpTilingData* tiling) :
46+ ReduceSchAuxBase<LoopInfo, ReduceSch, IsStageOne, OpDag,
47+ ReduceSchAuxBatchInvariant<LoopInfo, ReduceSch, IsStageOne, OpDag>>(sch, input, output, workspace, tiling)
48+ {
49+ for (uint64_t i = 0; i < Dim; i++) {
50+ iterAddr_[i].stride = this->tiling_->shape[i];
51+ }
52+ }
53+ 
54+ __aicore__ inline void SetLoopRange()
55+ {
56+ int32_t blockId = GetBlockIdx();
57+ if constexpr (IsBlockCutA<LoopInfo>()) {
58+ this->loopAStartIndex_ = blockId * this->tiling_->factorACntPerCore;
59+ this->loopAEndIndex_ = this->loopAStartIndex_ + this->tiling_->factorACntPerCore;
60+ if (unlikely(this->loopAEndIndex_ > this->tiling_->factorATotalCnt)) {
61+ this->loopAEndIndex_ = this->tiling_->factorATotalCnt;
62+ }
63+ constexpr int32_t aAxisIdx = LoopInfo->loopACount - 1;
64+ constexpr int32_t aAxis = LoopInfo->loopAAxis[aAxisIdx];
65+ this->loopAAxisStep_ = Ops::Base::CeilDiv(this->tiling_->shape[aAxis], this->ubFactorA_);
66+ if constexpr (LoopInfo->loopInnerRCount > 0) {
67+ constexpr int32_t rAxisIdx = LoopInfo->loopInnerRCount - 1;
68+ constexpr int32_t rAxis = LoopInfo->loopInnerRAxis[rAxisIdx];
69+ this->loopRAxisStep_ = Ops::Base::CeilDiv(this->tiling_->shape[rAxis], this->ubFactorR_);
70+ }
71+ } else {
72+ this->loopAStartIndex_ = blockId / this->tiling_->groupR * this->tiling_->factorACntPerCore;
73+ this->loopAEndIndex_ = this->loopAStartIndex_ + this->tiling_->factorACntPerCore;
74+ if (unlikely(this->loopAEndIndex_ > this->tiling_->factorATotalCnt)) {
75+ this->loopAEndIndex_ = this->tiling_->factorATotalCnt;
76+ }
77+ if constexpr (LoopInfo->loopACount > 0) {
78+ constexpr int32_t aAxisIdx = LoopInfo->loopACount - 1;
79+ constexpr int32_t aAxis = LoopInfo->loopAAxis[aAxisIdx];
80+ this->loopAAxisStep_ = Ops::Base::CeilDiv(this->tiling_->shape[aAxis], this->ubFactorA_);
81+ }
82+ 
83+ this->loopRStartIndex_ = blockId / this->tiling_->groupR * this->tiling_->factorRTotalCnt +
84+ blockId % this->tiling_->groupR * this->tiling_->factorRCntPerCore;
85+ this->loopREndIndex_ = this->loopRStartIndex_ + this->tiling_->factorRCntPerCore;
86+ uint64_t maxRCnt = (blockId / this->tiling_->groupR + 1) * this->tiling_->factorRTotalCnt;
87+ uint64_t totalCnt = this->tiling_->factorATotalCnt * this->tiling_->factorRTotalCnt;
88+ maxRCnt = maxRCnt > totalCnt ? totalCnt : maxRCnt;
89+ if (unlikely(this->loopRStartIndex_ > maxRCnt)) {
90+ this->loopRStartIndex_ = maxRCnt;
91+ }
92+ if (unlikely(this->loopREndIndex_ > maxRCnt)) {
93+ this->loopREndIndex_ = maxRCnt;
94+ }
95+ if constexpr (LoopInfo->loopRCount > 0) {
96+ constexpr int32_t rAxisIdx = LoopInfo->loopRCount - 1;
97+ constexpr int32_t rAxis = LoopInfo->loopRAxis[rAxisIdx];
98+ this->loopRAxisStep_ = Ops::Base::CeilDiv(this->tiling_->shape[rAxis], this->ubFactorR_);
99+ }
100+ }
101+ }
102+ 
103+ template <class... Args>
104+ __aicore__ inline void Process(Args... args)
105+ {
106+ SetLoopRange();
107+ this->SetIterRRange();
108+ if constexpr (LoopInfo->loopRCount == 0) {
109+ for (uint64_t i = this->loopAStartIndex_; i < this->loopAEndIndex_; i++) {
110+ this->template CalculateIterA<LoopInfo->loopACount>(i);
111+ IterateInnerA<0, LoopInfo->loopInnerACount>(i - this->loopAStartIndex_, args...);
112+ }
113+ } else {
114+ if (this->loopRStartIndex_ == this->loopREndIndex_) {
115+ return;
116+ }
117+ for (uint64_t i = this->loopAStartIndex_; i < this->loopAEndIndex_; i++) {
118+ this->template CalculateIterA<LoopInfo->loopACount>(i);
119+ IterateInnerA<0, LoopInfo->loopInnerACount>(i - this->loopAStartIndex_, args...);
120+ }
121+ }
122+ }
123+ 
124+ template <int32_t LoopAIdx>
125+ __aicore__ inline void CalculateIterA(uint64_t step)
126+ {
127+ if constexpr (LoopAIdx != 0) {
128+ constexpr auto axis = LoopInfo->loopAAxis[LoopAIdx - 1];
129+ if constexpr (LoopAIdx == LoopInfo->loopACount) {
130+ // 切分轴
131+ auto cur = step % this->loopAAxisStep_;
132+ iterAddr_[axis].start = cur * this->ubFactorA_;
133+ iterAddr_[axis].stride = this->tiling_->shape[axis] - iterAddr_[axis].start;
134+ if (likely(iterAddr_[axis].stride >= this->ubFactorA_)) {
135+ iterAddr_[axis].stride = this->ubFactorA_;
136+ }
137+ 
138+ if constexpr (LoopAIdx > 0) {
139+ this->CalculateIterA<LoopAIdx - 1>(step / this->loopAAxisStep_);
140+ }
141+ } else {
142+ iterAddr_[axis].start = step % this->tiling_->shape[axis];
143+ iterAddr_[axis].stride = 1;
144+ this->CalculateIterA<LoopAIdx - 1>(step / this->tiling_->shape[axis]);
145+ }
146+ }
147+ }
148+ 
149+ template <int32_t start = 0, int32_t end = 0, class... Args>
150+ __aicore__ inline void IterateInnerA(int32_t aIndex, Args... args)
151+ {
152+ if constexpr (start == end) {
153+ int32_t startIdx = 0;
154+ if constexpr (LoopInfo->reduceDichotomy) {
155+ Shape<AuxBase::InnerPattern::Dim> shape = {.value = {0, 0}, .innerR = 0, .outerR = 0};
156+ SliceView<MAX_DIM> view;
157+ if constexpr (LoopInfo->loopRCount == 0) {
158+ this->LinearComputeR(aIndex, startIdx, view, shape);
159+ this->PostReduce(aIndex, view, shape);
160+ } else {
161+ this->LinearComputeR(aIndex, startIdx, view, shape);
162+ CopyOutGroup(view, shape);
163+ }
164+ }
165+ } else {
166+ constexpr int32_t axis = LoopInfo->loopInnerAAxis[start];
167+ uint64_t shapeSize = this->tiling_->sliceShape[axis];
168+ if constexpr (start + 1 == end) {
169+ uint64_t loopSize = shapeSize / this->ubFactorA_;
170+ uint64_t tail = shapeSize - loopSize * this->ubFactorA_;
171+ iterAddr_[axis].start = 0;
172+ iterAddr_[axis].stride = this->ubFactorA_;
173+ 
174+ for (uint64_t i = 0; i < loopSize; i++) {
175+ IterateInnerA<start + 1, end>(aIndex, args...);
176+ iterAddr_[axis].start += this->ubFactorA_;
177+ }
178+ 
179+ if (tail) {
180+ iterAddr_[axis].stride = shapeSize - iterAddr_[axis].start;
181+ IterateInnerA<start + 1, end>(aIndex, args...);
182+ }
183+ } else {
184+ for (uint64_t i = 0; i < shapeSize; i++) {
185+ iterAddr_[axis].start = i;
186+ IterateInnerA<start + 1, end>(aIndex, args...);
187+ }
188+ }
189+ }
190+ }
191+ 
192+ template <int32_t LoopRIdx>
193+ __aicore__ inline void CalculateIterR(uint64_t step)
194+ {
195+ uint64_t temp = step;
196+ if constexpr (LoopRIdx != 0) {
197+ for (auto idx = LoopInfo->loopRCount - 1; idx >= LoopInfo->loopACount; --idx) {
198+ if (idx == LoopInfo->loopRCount - 1) {
199+ constexpr auto axis = LoopInfo->loopRAxis[LoopInfo->loopRCount - 1];
200+ auto cur = temp % this->loopRAxisStep_;
201+ iterAddr_[axis].start = cur * this->ubFactorR_;
202+ iterAddr_[axis].stride = this->tiling_->shape[axis] - iterAddr_[axis].start;
203+ if (likely(iterAddr_[axis].stride >= this->ubFactorR_)) {
204+ iterAddr_[axis].stride = this->ubFactorR_;
205+ }
206+ temp = temp / this->loopRAxisStep_;
207+ } else {
208+ auto axis = LoopInfo->loopRAxis[idx];
209+ iterAddr_[axis].start = temp % this->tiling_->shape[axis];
210+ iterAddr_[axis].stride = 1;
211+ temp = temp / this->tiling_->shape[axis];
212+ }
213+ }
214+ }
215+ }
216+ 
217+ template <class V, class S>
218+ __aicore__ inline void CopyOutGroup(V& view, S& shape)
219+ {
220+ static_assert(
221+ IsSameType<typename AuxBase::PromoteDType, typename AuxBase::DataType>::value,
222+ "group copy out dtype must be same with pre dag out.");
223+ SliceView<CONST2> newView;
224+ int32_t blockId = GetBlockIdx();
225+ uint64_t outLen = 1;
226+ uint64_t outRepeat = 1;
227+ this->CalculateOutView(view, shape, outLen, outRepeat);
228+ newView.axis[0].repeat = outLen;
229+ newView.axis[1].repeat = outRepeat;
230+ if constexpr (AuxBase::Pattern::TailA) {
231+ newView.axis[1].srcStride = Ops::Base::CeilAlign(outLen, UB_BLOCK / MIN_DTYPE_BYTES);
232+ } else {
233+ newView.axis[1].srcStride = outLen;
234+ }
235+ constexpr int32_t axis = AuxBase::Pattern::FirstA ? 0 : 1;
236+ uint64_t addrOffset = 0;
237+ for (int32_t i = axis; i < Dim; i += CONST2) {
238+ addrOffset += iterAddr_[i].start * this->tiling_->dstStride[i];
239+ }
240+ newView.addr = (blockId % this->tiling_->groupR) *
241+ Ops::Base::CeilAlign(this->tiling_->outSize, static_cast<uint64_t>(AuxBase::VL_ELEMS)) + addrOffset;
242+ 
243+ SetEvent<HardEvent::V_MTE3>(HardEvent::V_MTE3);
244+ LocalTensor<typename AuxBase::PromoteDType> outTensor;
245+ int64_t dimA = AuxBase::Pattern::TailA ? shape.value[1] : shape.value[0];
246+ int64_t dimAAlign = Ops::Base::CeilAlign(dimA, static_cast<int64_t>(AuxBase::VL_ELEMS));
247+ this->sch_->ReduceSch::template GetCacheAux(outTensor, (this->cacheCount_ - 1) * dimAAlign);
248+ GlobalTensor<typename AuxBase::PromoteDType> globalTensor;
249+ globalTensor.SetGlobalBuffer(
250+ reinterpret_cast<__gm__ typename AuxBase::PromoteDType*>(this->workspace_[0].GetPhyAddr(0)));
251+ this->sch_->ReduceSch::template CopyOutAux(globalTensor, outTensor, newView);
252+ }
253+ 
254+ template <class V, class S, class P>
255+ __aicore__ inline void CalculatePatternARPadding(V& view, S& shape, P& padding)
256+ {
257+ const uint64_t blkSize = static_cast<uint64_t>(UB_BLOCK / sizeof(InDType));
258+ const uint64_t alignSize = view.isBlockAligned == 1U ? blkSize : 1UL;
259+ const bool isLastSplit = IsLoopSpliteRAxis<LoopInfo>(Dim - 1);
260+ const uint64_t mainBurstLen = isLastSplit ? this->ubFactorR_ : view.axis[0].repeat;
261+ const uint64_t busrtLen = view.axis[0].repeat;
262+ const uint64_t mainBurstLenAlign = Ops::Base::CeilAlign(mainBurstLen, alignSize);
263+ const uint64_t dimRAlign = Ops::Base::CeilAlign(static_cast<uint64_t>(shape.value[1]), blkSize);
264+ int32_t burstPaddingRepeat = 1;
265+ int32_t rPaddingRepeat = 1;
266+ int32_t rPaddingStart = 1;
267+ int32_t aPaddingStart = 1;
268+ int32_t aPaddingTimes = 0;
269+ for (uint64_t i = 1; i < view.axisSize; i++) {
270+ if (!view.axis[i].isAxisA) {
271+ if (IsLoopSpliteRAxis<LoopInfo>(view.axis[i].idx)) {
272+ rPaddingStart = view.axis[i].repeat;
273+ break;
274+ } else {
275+ burstPaddingRepeat *= view.axis[i].repeat;
276+ }
277+ }
278+ }
279+ // busrtLen补pad的起始位置
280+ padding.burstPaddingStart = view.axis[0].repeat;
281+ // busrtLen补pad长度为主块长度-尾块长度
282+ padding.burstPaddingLen = mainBurstLenAlign - busrtLen;
283+ // busrtLen补pad循环次数为R轴切分相同部分的大小
284+ padding.burstPaddingRepeat = burstPaddingRepeat * rPaddingStart;
285+ // R轴补pad的起始位置,即主块/尾块R轴切分的最后位置
286+ padding.rPaddingStart = rPaddingStart * burstPaddingRepeat * mainBurstLenAlign;
287+ // R轴pad的长度
288+ padding.rPaddingLen = dimRAlign - padding.rPaddingStart;
289+ // R轴补pad的循环次数,为A轴切分相同部分的大小
290+ // 因为以A轴为外循环,A轴大小在R轴循环过程中始终一致,每次A轴循环时,会重新计算shape大小
291+ padding.rPaddingRepeat = shape.value[0];
292+ // A轴补pad的起始位置,即主块/尾块A轴切分的最后位置
293+ padding.aPaddingStart = rPaddingRepeat * shape.value[1];
294+ // A轴pad的长度
295+ padding.aPaddingLen = 0;
296+ padding.aPaddingRepeat = 1;
297+ }
298+ 
299+ template <class V, class S, class P>
300+ __aicore__ inline void CalculatePatternRAPadding(V& view, S& shape, P& padding)
301+ {
302+ const uint64_t blkSize = static_cast<uint64_t>(UB_BLOCK / sizeof(InDType));
303+ const uint64_t alignSize = view.isBlockAligned == 1U ? blkSize : 1UL;
304+ const uint64_t mainBurstLen = view.axis[0].repeat;
305+ const uint64_t busrtLen = view.axis[0].repeat;
306+ const uint64_t mainBurstLenAlign = Ops::Base::CeilAlign(mainBurstLen, alignSize);
307+ const uint64_t dimAAlign = Ops::Base::CeilAlign(static_cast<uint64_t>(shape.value[1]), blkSize);
308+ int32_t burstPaddingRepeat = 1;
309+ int32_t aPaddingRepeat = 1;
310+ int32_t rPaddingStart = 1;
311+ int32_t rPaddingTimes = 0;
312+ int32_t aPaddingStart = 1;
313+ for (uint64_t i = 1; i < view.axisSize; i++) {
314+ if (view.axis[i].isAxisA) {
315+ burstPaddingRepeat *= view.axis[i].repeat;
316+ }
317+ }
318+ padding.burstPaddingStart = view.axis[0].repeat;
319+ // busrtLen补pad长度为主块长度-尾块长度
320+ padding.burstPaddingLen = mainBurstLenAlign - busrtLen;
321+ // busrtLen补pad循环次数为A轴切分相同部分的大小
322+ padding.burstPaddingRepeat = burstPaddingRepeat * aPaddingStart;
323+ // A轴补pad的起始位置,即主块/尾块A轴切分的最后位置
324+ padding.aPaddingStart = aPaddingStart * burstPaddingRepeat * mainBurstLenAlign;
325+ // A轴pad的长度
326+ padding.aPaddingLen = dimAAlign - padding.aPaddingStart;
327+ for (uint64_t i = 1; i < view.axisSize; i++) {
328+ if (!view.axis[i].isAxisA) {
329+ if (IsLoopSpliteRAxis<LoopInfo>(view.axis[i].idx)) {
330+ rPaddingStart = view.axis[i].repeat;
331+ rPaddingTimes = this->ubFactorR_ - view.axis[i].repeat;
332+ break;
333+ } else {
334+ aPaddingRepeat *= view.axis[i].repeat;
335+ }
336+ }
337+ }
338+ // A轴补pad的循环次数,为R轴切分相同部分的大小,rPaddingStart=0时,表示R轴切分主块
339+ padding.aPaddingRepeat = aPaddingRepeat * rPaddingStart;
340+ // R轴补pad的起始位置,即主块/尾块R轴切分的最后位置
341+ padding.rPaddingStart = rPaddingStart * aPaddingRepeat * shape.value[1];
342+ // R轴pad的长度
343+ padding.rPaddingLen = rPaddingTimes * aPaddingRepeat * shape.value[1];
344+ padding.rPaddingRepeat = 1;
345+ }
346+ 
347+ template <int32_t LoopInnerRIdx>
348+ __aicore__ inline void CalculateInnerIterR(uint64_t basicBlockIdx)
349+ {
350+ if constexpr (LoopInnerRIdx != 0) {
351+ constexpr auto axis = LoopInfo->loopInnerRAxis[LoopInnerRIdx - 1];
352+ if constexpr (LoopInnerRIdx == LoopInfo->loopInnerRCount) {
353+ auto cur = basicBlockIdx % this->loopRAxisStep_;
354+ iterAddr_[axis].start = cur * this->ubFactorR_;
355+ iterAddr_[axis].stride = this->tiling_->shape[axis] - iterAddr_[axis].start;
356+ if (likely(iterAddr_[axis].stride >= this->ubFactorR_)) {
357+ iterAddr_[axis].stride = this->ubFactorR_;
358+ }
359+ CalculateInnerIterR<LoopInnerRIdx - 1>(basicBlockIdx / this->loopRAxisStep_);
360+ } else {
361+ iterAddr_[axis].start = basicBlockIdx % this->tiling_->shape[axis];
362+ iterAddr_[axis].stride = 1;
363+ CalculateInnerIterR<LoopInnerRIdx - 1>(basicBlockIdx / this->tiling_->shape[axis]);
364+ }
365+ }
366+ }
367+ 
368+ template <class V>
369+ __aicore__ inline void InitView(V& view)
370+ {
371+ constexpr static auto burstLenAxis = Dim - 1;
372+ view.axis[0].start = iterAddr_[burstLenAxis].start;
373+ view.axis[0].srcStride = 1;
374+ view.axis[0].repeat = GetBurstLen<LoopInfo, burstLenAxis>(iterAddr_, this->tiling_);
375+ view.axis[0].idx = burstLenAxis;
376+ view.axis[0].isAxisA = IsAxisA<AuxBase::Pattern::FirstA>(view.axis[0].idx);
377+ view.axisSize = 1;
378+ 
379+ if constexpr (burstLenAxis > 0) {
380+ int32_t axis = burstLenAxis;
381+ for (int32_t i = 1; i < Dim; i++) {
382+ view.axisSize = i + 1;
383+ view.axis[i].start = iterAddr_[axis - 1].start;
384+ view.axis[i].repeat =
385+ GetRepeatStride<LoopInfo>(axis - 1, iterAddr_, this->tiling_, view.axis[i].srcStride);
386+ view.axis[i].idx = axis - 1;
387+ view.axis[i].isAxisA = IsAxisA<AuxBase::Pattern::FirstA>(view.axis[i].idx);
388+ if (view.axis[i].idx <= 0) {
389+ break;
390+ }
391+ axis = view.axis[i].idx;
392+ }
393+ }
394+ view.isBlockAligned = (this->tiling_->useNddma == 0);
395+ }
396+ 
397+ template <class V, class S>
398+ __aicore__ inline void CalculateInnerShapeRAMode(V& view, S& shape)
399+ {
400+ int64_t rSize = 1;
401+ int64_t aSize = 1;
402+ if constexpr (AuxBase::Pattern::ID == PATTERN_A) {
403+ aSize = view.axis[0].repeat;
404+ }
405+ for (uint64_t i = 1; i < view.axisSize; i++) {
406+ if (!view.axis[i].isAxisA) {
407+ // 第一个R轴的dstStride就是A轴大小
408+ aSize = (aSize == 1) ? view.axis[i].dstStride : aSize;
409+ if (IsLoopSpliteRAxis<LoopInfo>(view.axis[i].idx)) {
410+ rSize = rSize * this->ubFactorR_;
411+ } else {
412+ rSize = rSize * view.axis[i].repeat;
413+ }
414+ }
415+ }
416+ 
417+ shape.value[AuxBase::InnerPattern::Dim - 1] = aSize;
418+ shape.value[AuxBase::InnerPattern::Dim - 2] = rSize;
419+ }
420+ 
421+ template <class T, class V>
422+ __aicore__ inline void CalculateViewARMode(V& view)
423+ {
424+ uint64_t alignedValue = (view.isBlockAligned == 1 ? (UB_BLOCK / sizeof(T)) : 1UL);
425+ int64_t value = Ops::Base::CeilAlign(view.axis[0].repeat, alignedValue);
426+ view.axis[0].dstStride = 1;
427+ if (IsLoopSpliteRAxis<LoopInfo>(Dim - 1)) {
428+ // R是切分轴时, 主尾块会先add起来, 所以R轴按照factorR算
429+ value = Ops::Base::CeilAlign(this->ubFactorR_, alignedValue);
430+ }
431+ // 因A轴循环会重新触发shape计算,所以A轴的shape不需要统一到ubFactorA,但是R轴shape需要统一到ubFactorR
432+ for (uint64_t i = 1; i < view.axisSize; i++) {
433+ if (!view.axis[i].isAxisA) {
434+ view.axis[i].dstStride = value;
435+ if (IsLoopSpliteRAxis<LoopInfo>(view.axis[i].idx)) {
436+ value = value * this->ubFactorR_;
437+ } else {
438+ value = value * view.axis[i].repeat;
439+ }
440+ }
441+ }
442+ // nddma 整个R轴乘积block对齐
443+ if (view.isBlockAligned != 1) {
444+ value = Ops::Base::CeilAlign(static_cast<uint64_t>(value), UB_BLOCK / sizeof(T));
445+ }
446+ for (uint64_t i = 1; i < view.axisSize; i++) {
447+ if (view.axis[i].isAxisA) {
448+ view.axis[i].dstStride = value;
449+ value = value * view.axis[i].repeat;
450+ }
451+ }
452+ }
453+ 
454+ template <class T, class V>
455+ __aicore__ inline void CalculateViewRAMode(V& view)
456+ {
457+ uint64_t alignedValue = (view.isBlockAligned == 1 ? (UB_BLOCK / MIN_DTYPE_BYTES) : 1UL);
458+ int64_t value = Ops::Base::CeilAlign(view.axis[0].repeat, alignedValue);
459+ view.axis[0].dstStride = 1;
460+ // 因A轴循环会重新触发shape计算,所以A轴的shape不需要统一到ubFactorA,但是R轴shape需要统一到ubFactorR
461+ for (uint64_t i = 1; i < view.axisSize; i++) {
462+ if (view.axis[i].isAxisA) {
463+ view.axis[i].dstStride = value;
464+ value = value * view.axis[i].repeat;
465+ }
466+ }
467+ // nddma 整个A轴乘积block对齐
468+ if (view.isBlockAligned != 1) {
469+ value = Ops::Base::CeilAlign(static_cast<uint64_t>(value), UB_BLOCK / sizeof(T));
470+ }
471+ for (uint64_t i = 1; i < view.axisSize; i++) {
472+ if (!view.axis[i].isAxisA) {
473+ view.axis[i].dstStride = value;
474+ if (IsLoopSpliteRAxis<LoopInfo>(view.axis[i].idx)) {
475+ value = value * this->ubFactorR_;
476+ } else {
477+ value = value * view.axis[i].repeat;
478+ }
479+ }
480+ }
481+ }
482+ 
483+ template <class T, class V>
484+ __aicore__ inline void CalculateInView(V& view)
485+ {
486+ uint64_t addrOffset = 0;
487+ for (int32_t i = 0; i < Dim; i++) {
488+ addrOffset += view.axis[i].start * view.axis[i].srcStride;
489+ }
490+ view.addr = addrOffset;
491+ if (view.axis[0].dstStride == 0) {
492+ this->template CalculateView<T>(view);
493+ }
494+ }
495+};
496+} // namespace ReduceOpTmpl
497+} // namespace Base
498+} // namespace Ops
499+#endif
@@ -88,6 +88,7 @@ struct ReduceTilingKey {
88 uint32_t loopARCount = 0;88 uint32_t loopARCount = 0;
89 uint32_t loopInnerARCount = 0;89 uint32_t loopInnerARCount = 0;
90 bool isContiguous = true;90 bool isContiguous = true;
91+ bool batchInvariant = false;
91};92};
92 93 
93namespace ReduceOpTmpl {94namespace ReduceOpTmpl {
@@ -359,6 +360,18 @@ protected:
359 template <class Pattern>360 template <class Pattern>
360 void ComputeProgressUnitA(const uint64_t* shape);361 void ComputeProgressUnitA(const uint64_t* shape);
361 362 
363+ template <class Pattern>
364+ void ComputeRFirst(const uint64_t* shape);
365+ 
366+ template <class Pattern>
367+ void ComputeAWithRFullLoad(const uint64_t* shape);
368+ 
369+ template <class Pattern>
370+ void ComputeAWithRCannotFullLoad(const uint64_t* shape);
371+ 
372+ template <class Pattern>
373+ void SetTilingDataBatchInvariant(const uint64_t* shape);
374+ 
362 template <class Pattern>375 template <class Pattern>
363 ge::graphStatus ComputeEmptyTiling(uint64_t* shape);376 ge::graphStatus ComputeEmptyTiling(uint64_t* shape);
364 377 
@@ -391,6 +404,7 @@ protected:
391 uint64_t basicBlock_{0}; // 算子搬入的buffer大小404 uint64_t basicBlock_{0}; // 算子搬入的buffer大小
392 uint64_t resultBlock_{0}; // reduce计算后的buffer大小405 uint64_t resultBlock_{0}; // reduce计算后的buffer大小
393 int32_t dimNum_{0};406 int32_t dimNum_{0};
407+ bool needReserveA_ = 0;
394 size_t workSpaceSize_{0};408 size_t workSpaceSize_{0};
395 CacheLineBlock cBlock_;409 CacheLineBlock cBlock_;
396 ReduceTilingUnit unitA_;410 ReduceTilingUnit unitA_;
@@ -447,7 +461,8 @@ OPBASE_API ge::graphStatus Tiling4ReduceOp(gert::TilingContext* context, ReduceO
447} // namespace Base461} // namespace Base
448} // namespace Ops462} // namespace Ops
449 463 
450-#define GEN_REDUCE_TILING_KEY(result, reduceTilingKey, ...) \464+#define GEN_REDUCE_TILING_KEY(result, reduceTilingKey, ...) \
451- result = GET_TPL_TILING_KEY(reduceTilingKey.isContiguous, reduceTilingKey.patternID, reduceTilingKey.loopARCount, \465+ result = GET_TPL_TILING_KEY( \
452- reduceTilingKey.loopInnerARCount, __VA_ARGS__)466+ reduceTilingKey.isContiguous, reduceTilingKey.batchInvariant, reduceTilingKey.patternID, \
467+ reduceTilingKey.loopARCount, reduceTilingKey.loopInnerARCount, __VA_ARGS__)
453#endif468#endif
@@ -31,6 +31,7 @@ struct ReduceOpTilingData {
31 uint64_t basicBlock;31 uint64_t basicBlock;
32 uint64_t resultBlock;32 uint64_t resultBlock;
33 int32_t coreNum;33 int32_t coreNum;
34+ int32_t realCoreNum;
34 int32_t useNddma;35 int32_t useNddma;
35 float meanVar;36 float meanVar;
36 uint64_t shape[ReduceOpTmpl::MAX_DIM] = {0}; // 输入shape37 uint64_t shape[ReduceOpTmpl::MAX_DIM] = {0}; // 输入shape
@@ -60,87 +60,94 @@
60#define C4 460#define C4 4
61#define C100 10061#define C100 100
62 62 
63-#define REDUCE_TPL_PARAM bool IsContiguous, uint32_t PatternID, uint32_t LoopARCount, uint32_t LoopInnerARCount63+#define REDUCE_TPL_PARAM \
64-#define REDUCE_TPL_VALUE IsContiguous, PatternID, LoopARCount, LoopInnerARCount64+ bool IsContiguous, bool BatchInvariant, uint32_t PatternID, uint32_t LoopARCount, uint32_t LoopInnerARCount
65+#define REDUCE_TPL_VALUE IsContiguous, BatchInvariant, PatternID, LoopARCount, LoopInnerARCount
65 66 
66#define REDUCE_TPL_KEY_DECL() \67#define REDUCE_TPL_KEY_DECL() \
67- ASCENDC_TPL_BOOL_DECL(IsContiguous, C1), \68+ ASCENDC_TPL_BOOL_DECL(IsContiguous, C1), ASCENDC_TPL_BOOL_DECL(BatchInvariant, C0), \
68 ASCENDC_TPL_UINT_DECL(PatternID, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \69 ASCENDC_TPL_UINT_DECL(PatternID, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \
69 ASCENDC_TPL_UINT_DECL(LoopARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \70 ASCENDC_TPL_UINT_DECL(LoopARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \
70 ASCENDC_TPL_UINT_DECL(LoopInnerARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100)71 ASCENDC_TPL_UINT_DECL(LoopInnerARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100)
71 72 
72-#define REDUCE_TPL_KEY_SEL_EMPTY() \73+#define REDUCE_TPL_KEY_SEL_EMPTY() \
73- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \74+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
74- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, 0), \75+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, 0), \
75- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, 0), \76+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, 0), \
76 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, 0)77 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, 0)
77 78 
78-#define REDUCE_TPL_KEY_SEL_A() \79+#define REDUCE_TPL_KEY_SEL_A() \
79- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \80+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
80- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, C100), \81+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, C100), \
81- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \82+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \
82 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)83 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
83 84 
84-#define REDUCE_TPL_KEY_SEL_AR_NORMAL() \85+#define REDUCE_TPL_KEY_SEL_AR_NORMAL() \
85- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \86+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
86- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \87+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \
87- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \88+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \
88 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)89 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)
89 90 
90-#define REDUCE_TPL_KEY_SEL_AR_GROUP() \91+#define REDUCE_TPL_KEY_SEL_AR_GROUP() \
91- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \92+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
92- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \93+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \
93- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2), \94+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2), \
94 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)95 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
95 96 
96-#define REDUCE_TPL_KEY_SEL_ARA_NORMAL() \97+#define REDUCE_TPL_KEY_SEL_ARA_NORMAL() \
97- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \98+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
98- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \99+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \
99- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \100+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \
100 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)101 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)
101 102 
102-#define REDUCE_TPL_KEY_SEL_ARA_GROUP() \103+#define REDUCE_TPL_KEY_SEL_ARA_GROUP() \
103- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \104+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
104- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \105+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \
105- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A2R3), \106+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A2R3), \
106 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)107 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
107 108 
108-#define REDUCE_TPL_KEY_SEL_ARAR_NORMAL() \109+#define REDUCE_TPL_KEY_SEL_ARAR_NORMAL() \
109- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \110+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
110- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \111+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \
111- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \112+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \
112 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C2)113 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C2)
113 114 
114-#define REDUCE_TPL_KEY_SEL_ARAR_GROUP() \115+#define REDUCE_TPL_KEY_SEL_ARAR_GROUP() \
115- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \116+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
116- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \117+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \
117- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A2R3, A2R4), \118+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A2R3, A2R4), \
118 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)119 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
119 120 
120#define REDUCE_TPL_KEY_SEL_ARARARAR_NORMAL() \121#define REDUCE_TPL_KEY_SEL_ARARARAR_NORMAL() \
121 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \122 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
123+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
122 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \124 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \
123 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0), \125 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0), \
124 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)126 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)
125 127 
126-#define REDUCE_TPL_KEY_SEL_ARARARAR_GROUP() \128+#define REDUCE_TPL_KEY_SEL_ARARARAR_GROUP() \
127- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \129+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
128- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \130+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
129- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, \131+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \
130- A3R5, A3R6, A3R7, A4R5, A4R6, A4R7, A4R8), \132+ ASCENDC_TPL_UINT_SEL( \
133+ LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, A3R5, A3R6, A3R7, \
134+ A4R5, A4R6, A4R7, A4R8), \
131 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)135 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
132 136 
133#define REDUCE_TPL_KEY_SEL_ARARARARA_NORMAL() \137#define REDUCE_TPL_KEY_SEL_ARARARARA_NORMAL() \
134 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \138 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
139+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
135 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \140 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \
136 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0, A5R0), \141 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0, A5R0), \
137 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)142 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)
138 143 
139-#define REDUCE_TPL_KEY_SEL_ARARARARA_GROUP() \144+#define REDUCE_TPL_KEY_SEL_ARARARARA_GROUP() \
140- ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \145+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
141- ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \146+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
142- ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, \147+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \
143- A3R5, A3R6, A3R7, A4R5, A4R6, A4R7, A4R8, A5R6, A5R7, A5R8, A5R9), \148+ ASCENDC_TPL_UINT_SEL( \
149+ LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, A3R5, A3R6, A3R7, \
150+ A4R5, A4R6, A4R7, A4R8, A5R6, A5R7, A5R8, A5R9), \
144 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)151 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
145 152 
146#endif153#endif
@@ -0,0 +1,112 @@
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 reduce_tiling_key_decl_batch_invariant.h
13+ * \brief reduce tiling key declare batch invariant
14+ */
15+ 
16+#ifndef _REDUCE_TILING_KEY_DECL_BATCH_INVARIANT_H_
17+#define _REDUCE_TILING_KEY_DECL_BATCH_INVARIANT_H_
18+#include "ascendc/host_api/tiling/template_argument.h"
19+#include "reduce_tiling_key_decl.h"
20+ 
21+#define REDUCE_TPL_KEY_DECL_BATCH_INVARIANT() \
22+ ASCENDC_TPL_BOOL_DECL(IsContiguous, C0, C1), ASCENDC_TPL_BOOL_DECL(BatchInvariant, C0, C1), \
23+ ASCENDC_TPL_UINT_DECL(PatternID, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \
24+ ASCENDC_TPL_UINT_DECL(LoopARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \
25+ ASCENDC_TPL_UINT_DECL(LoopInnerARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100)
26+ 
27+#define REDUCE_TPL_KEY_SEL_EMPTY_BATCH_INVARIANT() \
28+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
29+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, 0), \
30+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, 0), \
31+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, 0)
32+ 
33+#define REDUCE_TPL_KEY_SEL_A_BATCH_INVARIANT() \
34+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
35+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, C100), \
36+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \
37+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
atomgit-bot

🟡 Medium Priority

变更文件 reduce_tiling_key_decl_batch_invariant.h 行 29, 35:REDUCE_TPL_KEY_SEL_EMPTY_BATCH_INVARIANT 和 REDUCE_TPL_KEY_SEL_A_BATCH_INVARIANT 使用 ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0),仅匹配 batchInvariant = false。但声明宏 REDUCE_TPL_KEY_DECL_BATCH_INVARIANT(行 22)中 BatchInvariant 声明为 C0, C1(两种取值均允许)。同一文件中其他 pattern(如 AR_NORMAL, AR_GROUP 等)的 key_sel 均使用 ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1) 允许两种取值。

当 batchInvariant = true 且 pattern 为 A 时,没有任何 key_sel 能匹配(旧 key_sel 仅匹配 C0,新 key_sel A 也仅匹配 C0),导致模板分发失败。这可能与行 990/1333 将 batchInvariant 硬编码为 1 联动触发。

建议:将 EMPTY 和 A 的 key_sel 中 ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0) 改为 ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1),与其他 pattern 保持一致。

likedislike
Incorrect?
38+ 
39+#define REDUCE_TPL_KEY_SEL_AR_NORMAL_BATCH_INVARIANT() \
40+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
41+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
42+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \
43+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \
44+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)
45+ 
46+#define REDUCE_TPL_KEY_SEL_AR_GROUP_BATCH_INVARIANT() \
47+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
48+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
49+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \
50+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2), \
51+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
52+ 
53+#define REDUCE_TPL_KEY_SEL_ARA_NORMAL_BATCH_INVARIANT() \
54+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
55+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
56+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \
57+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \
58+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)
59+ 
60+#define REDUCE_TPL_KEY_SEL_ARA_GROUP_BATCH_INVARIANT() \
61+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
62+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
63+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \
64+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A2R3), \
65+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
66+ 
67+#define REDUCE_TPL_KEY_SEL_ARAR_NORMAL_BATCH_INVARIANT() \
68+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
69+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
70+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \
71+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \
72+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C2)
73+ 
74+#define REDUCE_TPL_KEY_SEL_ARAR_GROUP_BATCH_INVARIANT() \
75+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
76+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
77+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \
78+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A2R3, A2R4), \
79+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
80+ 
81+#define REDUCE_TPL_KEY_SEL_ARARARAR_NORMAL_BATCH_INVARIANT() \
82+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
83+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
84+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \
85+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0), \
86+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)
87+ 
88+#define REDUCE_TPL_KEY_SEL_ARARARAR_GROUP_BATCH_INVARIANT() \
89+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
90+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
91+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \
92+ ASCENDC_TPL_UINT_SEL( \
93+ LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, A3R5, A3R6, A3R7, \
94+ A4R5, A4R6, A4R7, A4R8), \
95+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
96+ 
97+#define REDUCE_TPL_KEY_SEL_ARARARARA_NORMAL_BATCH_INVARIANT() \
98+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
99+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
100+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \
101+ ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0, A5R0), \
102+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)
103+ 
104+#define REDUCE_TPL_KEY_SEL_ARARARARA_GROUP_BATCH_INVARIANT() \
105+ ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
106+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0, C1), \
107+ ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \
108+ ASCENDC_TPL_UINT_SEL( \
109+ LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, A3R5, A3R6, A3R7, \
110+ A4R5, A4R6, A4R7, A4R8, A5R6, A5R7, A5R8, A5R9), \
111+ ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
112+#endif
@@ -19,67 +19,77 @@
19#include "reduce_tiling_key_decl.h"19#include "reduce_tiling_key_decl.h"
20 20 
21#define REDUCE_TPL_KEY_DECL_NON_CONTIGUOUS() \21#define REDUCE_TPL_KEY_DECL_NON_CONTIGUOUS() \
22- ASCENDC_TPL_BOOL_DECL(IsContiguous, C0, C1), \22+ ASCENDC_TPL_BOOL_DECL(IsContiguous, C0, C1), ASCENDC_TPL_BOOL_DECL(BatchInvariant, C0),\
23 ASCENDC_TPL_UINT_DECL(PatternID, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \23 ASCENDC_TPL_UINT_DECL(PatternID, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \
24 ASCENDC_TPL_UINT_DECL(LoopARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \24 ASCENDC_TPL_UINT_DECL(LoopARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100), \
25 ASCENDC_TPL_UINT_DECL(LoopInnerARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100)25 ASCENDC_TPL_UINT_DECL(LoopInnerARCount, BIT_WIDTH, ASCENDC_TPL_UI_RANGE, 1, C0, C100)
26 26 
27#define REDUCE_TPL_KEY_SEL_EMPTY_NON_CONTIGUOUS() \27#define REDUCE_TPL_KEY_SEL_EMPTY_NON_CONTIGUOUS() \
28 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \28 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
29+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
29 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, 0), \30 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, 0), \
30 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, 0), \31 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, 0), \
31 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, 0)32 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, 0)
32 33 
33#define REDUCE_TPL_KEY_SEL_A_NON_CONTIGUOUS() \34#define REDUCE_TPL_KEY_SEL_A_NON_CONTIGUOUS() \
34 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \35 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C1), \
36+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
35 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, C100), \37 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, C100), \
36 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \38 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \
37 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)39 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
38 40 
39#define REDUCE_TPL_KEY_SEL_AR_NORMAL_NON_CONTIGUOUS() \41#define REDUCE_TPL_KEY_SEL_AR_NORMAL_NON_CONTIGUOUS() \
40 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \42 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
43+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
41 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \44 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \
42 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \45 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0), \
43 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)46 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)
44 47 
45#define REDUCE_TPL_KEY_SEL_AR_GROUP_NON_CONTIGUOUS() \48#define REDUCE_TPL_KEY_SEL_AR_GROUP_NON_CONTIGUOUS() \
46 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \49 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
50+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
47 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \51 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, AR_PATTERN), \
48 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2), \52 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2), \
49 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)53 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
50 54 
51#define REDUCE_TPL_KEY_SEL_ARA_NORMAL_NON_CONTIGUOUS() \55#define REDUCE_TPL_KEY_SEL_ARA_NORMAL_NON_CONTIGUOUS() \
52 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \56 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
57+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
53 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \58 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \
54 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \59 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \
55 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)60 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C1)
56 61 
57#define REDUCE_TPL_KEY_SEL_ARA_GROUP_NON_CONTIGUOUS() \62#define REDUCE_TPL_KEY_SEL_ARA_GROUP_NON_CONTIGUOUS() \
58 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \63 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
64+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
59 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \65 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARA_PATTERN), \
60 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A2R3), \66 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A2R3), \
61 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)67 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
62 68 
63#define REDUCE_TPL_KEY_SEL_ARAR_NORMAL_NON_CONTIGUOUS() \69#define REDUCE_TPL_KEY_SEL_ARAR_NORMAL_NON_CONTIGUOUS() \
64 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \70 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
71+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
65 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \72 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \
66 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \73 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0), \
67 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C2)74 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C2)
68 75 
69#define REDUCE_TPL_KEY_SEL_ARAR_GROUP_NON_CONTIGUOUS() \76#define REDUCE_TPL_KEY_SEL_ARAR_GROUP_NON_CONTIGUOUS() \
70 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \77 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
78+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
71 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \79 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARAR_PATTERN), \
72 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A2R3, A2R4), \80 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A2R3, A2R4), \
73 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)81 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_LIST, C0)
74 82 
75#define REDUCE_TPL_KEY_SEL_ARARARAR_NORMAL_NON_CONTIGUOUS() \83#define REDUCE_TPL_KEY_SEL_ARARARAR_NORMAL_NON_CONTIGUOUS() \
76 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \84 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
85+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
77 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \86 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \
78 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0), \87 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0), \
79 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)88 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)
80 89 
81#define REDUCE_TPL_KEY_SEL_ARARARAR_GROUP_NON_CONTIGUOUS() \90#define REDUCE_TPL_KEY_SEL_ARARARAR_GROUP_NON_CONTIGUOUS() \
82 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \91 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
92+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
83 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \93 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARAR_PATTERN), \
84 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, \94 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, \
85 A3R5, A3R6, A3R7, A4R5, A4R6, A4R7, A4R8), \95 A3R5, A3R6, A3R7, A4R5, A4R6, A4R7, A4R8), \
@@ -87,12 +97,14 @@
87 97 
88#define REDUCE_TPL_KEY_SEL_ARARARARA_NORMAL_NON_CONTIGUOUS() \98#define REDUCE_TPL_KEY_SEL_ARARARARA_NORMAL_NON_CONTIGUOUS() \
89 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \99 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_AIV_ONLY), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
100+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
90 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \101 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \
91 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0, A5R0), \102 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R0, A2R0, A3R0, A4R0, A5R0), \
92 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)103 ASCENDC_TPL_UINT_SEL(LoopInnerARCount, ASCENDC_TPL_UI_RANGE, 1, C0, C4)
93 104 
94#define REDUCE_TPL_KEY_SEL_ARARARARA_GROUP_NON_CONTIGUOUS() \105#define REDUCE_TPL_KEY_SEL_ARARARARA_GROUP_NON_CONTIGUOUS() \
95 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \106 ASCENDC_TPL_KERNEL_TYPE_SEL(ASCENDC_TPL_MIX_AIV_1_0), ASCENDC_TPL_BOOL_SEL(IsContiguous, C0, C1), \
107+ ASCENDC_TPL_BOOL_SEL(BatchInvariant, C0), \
96 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \108 ASCENDC_TPL_UINT_SEL(PatternID, ASCENDC_TPL_UI_LIST, ARARARARA_PATTERN), \
97 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, \109 ASCENDC_TPL_UINT_SEL(LoopARCount, ASCENDC_TPL_UI_LIST, A1R2, A1R3, A1R4, A1R5, A2R3, A2R4, A2R5, A2R6, A3R4, \
98 A3R5, A3R6, A3R7, A4R5, A4R6, A4R7, A4R8, A5R6, A5R7, A5R8, A5R9), \110 A3R5, A3R6, A3R7, A4R5, A4R6, A4R7, A4R8, A5R6, A5R7, A5R8, A5R9), \
@@ -0,0 +1,42 @@
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 reduce_tiling_key_sel_batch_invariant.h
13+ * \brief reduce tiling key select batch invariant
14+ */
15+ 
16+#ifndef _REDUCE_TILING_KEY_SEL_BATCH_INVARIANT_H_
17+#define _REDUCE_TILING_KEY_SEL_BATCH_INVARIANT_H_
18+ 
19+#include "reduce_tiling_key_decl_batch_invariant.h"
20+ 
21+ASCENDC_TPL_SEL(
22+ // Empty
23+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_EMPTY_BATCH_INVARIANT()),
24+ // A
25+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_A_BATCH_INVARIANT()),
26+ // AR
27+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_AR_NORMAL_BATCH_INVARIANT()),
28+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_AR_GROUP_BATCH_INVARIANT()),
29+ // ARA
30+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_ARA_NORMAL_BATCH_INVARIANT()),
31+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_ARA_GROUP_BATCH_INVARIANT()),
32+ // ARAR
33+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_ARAR_NORMAL_BATCH_INVARIANT()),
34+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_ARAR_GROUP_BATCH_INVARIANT()),
35+ // ARARARAR
36+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_ARARARAR_NORMAL_BATCH_INVARIANT()),
37+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_ARARARAR_GROUP_BATCH_INVARIANT()),
38+ // ARARARARA
39+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_ARARARARA_NORMAL_BATCH_INVARIANT()),
40+ ASCENDC_TPL_ARGS_SEL(REDUCE_TPL_KEY_SEL_ARARARARA_GROUP_BATCH_INVARIANT()));
41+ 
42+#endif
@@ -15,6 +15,7 @@
15 15 
16#include "op_common/atvoss/reduce/reduce_tiling.h"16#include "op_common/atvoss/reduce/reduce_tiling.h"
17#include "op_common/op_host/util/math_util.h"17#include "op_common/op_host/util/math_util.h"
18+#include "reduce_tiling_batch_invariant.h"
18 19 
19namespace Ops {20namespace Ops {
20namespace Base {21namespace Base {
@@ -23,10 +24,7 @@ using namespace ReduceOpTmpl;
23// float数据类型, UB间reduce缓存为16K时,最大能支持的A, 公式CACHE_BUF_SIZE = MAX_INNER_A * log2(Ro)24// float数据类型, UB间reduce缓存为16K时,最大能支持的A, 公式CACHE_BUF_SIZE = MAX_INNER_A * log2(Ro)
24// log2(Ro)支持最大取值为3225// log2(Ro)支持最大取值为32
25constexpr static int32_t MAX_INNER_A = 512; // 单位字节26constexpr static int32_t MAX_INNER_A = 512; // 单位字节
26-constexpr static double THRES_HOLD = 0.95;
27constexpr static int32_t A_STEP_LEN = 4;27constexpr static int32_t A_STEP_LEN = 4;
28-constexpr static int32_t AXES_STEP = 2; // A轴和R轴循环遍历的步长
29- 
30/**28/**
31 * Ensure that the returned shape is non-scalar.29 * Ensure that the returned shape is non-scalar.
32 * When the dim num of shape is 0, this shape is considered to express a scalar.30 * When the dim num of shape is 0, this shape is considered to express a scalar.
@@ -154,8 +152,9 @@ ge::graphStatus GetInputParam(gert::TilingContext* context, ReduceOpInputParam&
154 OP_CHECK_IF((GetInputShape(context, inputIdx, opInput.shape) == ge::GRAPH_FAILED),152 OP_CHECK_IF((GetInputShape(context, inputIdx, opInput.shape) == ge::GRAPH_FAILED),
155 OP_LOGE(context, "ReduceOpTmpl get input shape failed"), return ge::GRAPH_FAILED);153 OP_LOGE(context, "ReduceOpTmpl get input shape failed"), return ge::GRAPH_FAILED);
156 154 
157- OP_CHECK_IF((GetInputStride(context, inputIdx, opInput.dimStrides) == ge::GRAPH_FAILED),155+ OP_CHECK_IF(
158- OP_LOGE(context, "ReduceOpTmpl get input stride failed"), return ge::GRAPH_FAILED);156+ (GetInputStride(context, inputIdx, opInput.dimStrides) == ge::GRAPH_FAILED),
157+ OP_LOGE(context, "ReduceOpTmpl get input stride failed"), return ge::GRAPH_FAILED);
159 return ge::GRAPH_SUCCESS;158 return ge::GRAPH_SUCCESS;
160}159}
161 160 
@@ -209,14 +208,6 @@ static void MakeWrapDim(const std::vector<int64_t>& shape, std::vector<int64_t>&
209 std::sort(axes.begin(), axes.end());208 std::sort(axes.begin(), axes.end());
210}209}
211 210 
212-static inline void AssembleUnit(ReduceTilingUnit& unit, int32_t idx, uint64_t inner, uint64_t outer, uint64_t step)
213-{
214- unit.idx = idx;
215- unit.inner = inner;
216- unit.outer = outer;
217- unit.step = step;
218-}
219- 
220template <class Pattern>211template <class Pattern>
221bool ReduceOpTiling::IsAxisA(int32_t idx)212bool ReduceOpTiling::IsAxisA(int32_t idx)
222{213{
@@ -561,6 +552,7 @@ void ReduceOpTiling::DoReduceTiling(ReduceTilingKey& key)
561 uint64_t newShape[MAX_DIM] = {0};552 uint64_t newShape[MAX_DIM] = {0};
562 int64_t newStride[MAX_DIM] = {0};553 int64_t newStride[MAX_DIM] = {0};
563 int32_t newShapeSize = 0;554 int32_t newShapeSize = 0;
555+ tilingKey_.batchInvariant = key.batchInvariant;
564 TransformShape(newShape, newStride, newShapeSize);556 TransformShape(newShape, newStride, newShapeSize);
565 557 
566 DoTilingMatchPattern(newShape, newShapeSize);558 DoTilingMatchPattern(newShape, newShapeSize);
@@ -855,7 +847,6 @@ void ReduceOpTiling::ComputeProgressUnitA(const uint64_t* shape)
855 AssembleUnit(unitA_, iA, innerA, outerA, step);847 AssembleUnit(unitA_, iA, innerA, outerA, step);
856}848}
857 849 
858-// try get reduce result block size
859template <class Pattern>850template <class Pattern>
860uint64_t ReduceOpTiling::TryGetReduceBlock(const uint64_t* shape, uint64_t preInputBufferNum, uint64_t preRestBufferNum,851uint64_t ReduceOpTiling::TryGetReduceBlock(const uint64_t* shape, uint64_t preInputBufferNum, uint64_t preRestBufferNum,
861 uint64_t postBufferNum)852 uint64_t postBufferNum)
@@ -981,20 +972,26 @@ ge::graphStatus ReduceOpTiling::ComputeTiling(uint64_t* shape)
981 if (IsEmtpyTensor<Pattern>(shape)) {972 if (IsEmtpyTensor<Pattern>(shape)) {
982 return ComputeEmptyTiling<Pattern>(shape);973 return ComputeEmptyTiling<Pattern>(shape);
983 }974 }
984- 975+ if (tilingKey_.batchInvariant && CheckIsContiguous(opInput_)) {
985- ComputeCacheLineBlockAndUnit<Pattern>(shape);976+ ComputeRFirst<Pattern>(shape);
986- 977+ if (unitR_.idx == -1) {
987- ComputeUnitA<Pattern>(shape);978+ ComputeAWithRFullLoad<Pattern>(shape);
988- 979+ } else {
989- ComputeUnitR<Pattern>(shape);980+ ComputeAWithRCannotFullLoad<Pattern>(shape);
990- 981+ }
991- ComputeProgressUnitA<Pattern>(shape);982+ SetTilingDataBatchInvariant<Pattern>(shape);
983+ } else {
984+ ComputeCacheLineBlockAndUnit<Pattern>(shape);
985+ ComputeUnitA<Pattern>(shape);
986+ ComputeUnitR<Pattern>(shape);
987+ ComputeProgressUnitA<Pattern>(shape);
988+ SetTilingData<Pattern>(shape);
989+ }
992 990 
993 OP_LOGI(context_,991 OP_LOGI(context_,
994 "tiling step outerA:%lu, innerA:%lu, stepA:%lu, idxA:%d, outerR:%lu, innerR:%lu, stepR:%lu, idxR:%d",992 "tiling step outerA:%lu, innerA:%lu, stepA:%lu, idxA:%d, outerR:%lu, innerR:%lu, stepR:%lu, idxR:%d",
995 unitA_.outer, unitA_.inner, unitA_.step, unitA_.idx, unitR_.outer, unitR_.inner, unitR_.step, unitR_.idx);993 unitA_.outer, unitA_.inner, unitA_.step, unitA_.idx, unitR_.outer, unitR_.inner, unitR_.step, unitR_.idx);
996 994 
997- SetTilingData<Pattern>(shape);
998 SetTilingKey<Pattern>();995 SetTilingKey<Pattern>();
999 return ge::GRAPH_SUCCESS;996 return ge::GRAPH_SUCCESS;
1000}997}
@@ -1039,8 +1036,8 @@ void ReduceOpTiling::SetTilingData(const uint64_t* shape)
1039 tilingData_->coreNum = static_cast<int32_t>(compileInfo_->vectorCoreNum);1036 tilingData_->coreNum = static_cast<int32_t>(compileInfo_->vectorCoreNum);
1040 tilingData_->useNddma = IsUseNddma<Pattern>(shape);1037 tilingData_->useNddma = IsUseNddma<Pattern>(shape);
1041 ComputeStride<Pattern>(shape);1038 ComputeStride<Pattern>(shape);
1042- 
1043 uint32_t realCore = CeilDiv(unitA_.outer, factorACntPerCore) * CeilDiv(unitR_.outer, factorRCntPerCore);1039 uint32_t realCore = CeilDiv(unitA_.outer, factorACntPerCore) * CeilDiv(unitR_.outer, factorRCntPerCore);
1040+ tilingData_->realCoreNum = realCore;
1044 context_->SetBlockDim(realCore);1041 context_->SetBlockDim(realCore);
1045}1042}
1046 1043 
@@ -1125,8 +1122,9 @@ void ReduceOpTiling::SetTilingKey()
1125 tilingKey_.loopARCount = static_cast<uint32_t>(aCount * CONST10 + rCount);1122 tilingKey_.loopARCount = static_cast<uint32_t>(aCount * CONST10 + rCount);
1126 tilingKey_.loopInnerARCount = static_cast<uint32_t>(innerACount * CONST10 + innerRCount);1123 tilingKey_.loopInnerARCount = static_cast<uint32_t>(innerACount * CONST10 + innerRCount);
1127 tilingKey_.isContiguous = CheckIsContiguous(opInput_);1124 tilingKey_.isContiguous = CheckIsContiguous(opInput_);
1128- OP_LOGI(context_, "patternID:%u, loopARCount:%u, loopInnerARCount:%u, isContiguous:%d", tilingKey_.patternID,1125+ OP_LOGI(
1129- tilingKey_.loopARCount, tilingKey_.loopInnerARCount, tilingKey_.isContiguous ? 1 : 0);1126+ context_, "patternID:%u, loopARCount:%u, loopInnerARCount:%u, isContiguous:%d, isBatchInvariant:%d", tilingKey_.patternID,
1127+ tilingKey_.loopARCount, tilingKey_.loopInnerARCount, tilingKey_.isContiguous ? 1 : 0, tilingKey_.batchInvariant ? 1 : 0);
1130}1128}
1131 1129 
1132void ReduceOpTiling::GetTilingKey(ReduceTilingKey& key) { key = tilingKey_; }1130void ReduceOpTiling::GetTilingKey(ReduceTilingKey& key) { key = tilingKey_; }
@@ -0,0 +1,232 @@
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 reduce_batch_invariant_tiling.h
13+ * \brief batchInvariant=1 分支的 tiling 模板实现,被 reduce_tiling.cpp include
14+ * 注意:本文件非独立编译单元,不可单独编译
15+ */
16+ 
17+#ifndef _REDUCE_BATCH_INVARIANT_TILING_H_
18+#define _REDUCE_BATCH_INVARIANT_TILING_H_
19+ 
20+#include "op_common/atvoss/reduce/reduce_tiling.h"
21+#include "op_common/op_host/util/math_util.h"
22+ 
23+namespace Ops {
24+namespace Base {
25+ 
26+constexpr static int32_t AXES_STEP = 2; // A轴和R轴循环遍历的步长
27+constexpr static double THRES_HOLD = 0.95;
28+constexpr static int32_t TAIL_R_THRES = 64;
29+ 
30+inline void AssembleUnit(ReduceTilingUnit& unit, int32_t idx, uint64_t inner, uint64_t outer, uint64_t step)
31+{
32+ unit.idx = idx;
33+ unit.inner = inner;
34+ unit.outer = outer;
35+ unit.step = step;
36+}
37+ 
38+template <class Pattern>
39+void ReduceOpTiling::ComputeRFirst(const uint64_t* shape)
40+{
41+ uint64_t dSize = ge::GetSizeByDataType(opInput_.inputDtype);
42+ uint64_t ubBlockSize = compileInfo_->ubBlockSize / dSize;
43+ uint64_t cacheSize = compileInfo_->cacheLineSize / dSize;
44+ uint64_t bBlockNum = basicBlock_ * Ratio() / opDag_.maxInputBytes;
45+ //AR和尾轴为R且尾轴R大于64,ub内不需要预留cacheline大小的空间;其余场景预留cacheline大小空间
46+ needReserveA_ = !(Pattern::ID == PATTERN_AR || (!Pattern::TailA && shape[Pattern::Dim - 1] > TAIL_R_THRES));
47+ OP_LOGD(context_, "needReserveA is %d", needReserveA_);
48+ uint64_t maxRInUB = needReserveA_ ? bBlockNum / cacheSize : bBlockNum;
49+ uint64_t innerR = 1;
50+ int32_t startR = Pattern::TailA ? Pattern::Dim - AXES_STEP : Pattern::Dim - CONST1;
51+ int32_t endR = Pattern::FirstA ? CONST1 : CONST0;
52+ int32_t iRCut = -1;
53+ 
54+ for (int32_t iR = startR; iR >= endR; iR -= AXES_STEP) {
55+ uint64_t axisLen = shape[iR];
56+ uint64_t alignedLen = (iR == Pattern::Dim - 1) ? CeilAlign(axisLen, ubBlockSize) : axisLen;
57+ if (innerR * alignedLen <= maxRInUB) {
58+ innerR *= alignedLen;
59+ continue;
60+ }
61+ iRCut = iR;
62+ break;
63+ }
64+ 
65+ if (iRCut == -1) {
66+ AssembleUnit(unitR_, -1, innerR, 1, 1);
67+ tilingData_->factorRCntPerCore = 1;
68+ tilingData_->groupR = 1;
69+ return;
70+ }
71+ 
72+ uint64_t ubFactorR = maxRInUB / innerR;
73+ innerR = innerR * ubFactorR;
74+ 
75+ uint64_t outerR = CeilDiv(shape[iRCut], ubFactorR);
76+ for (int32_t iR = iRCut - AXES_STEP; iR >= endR; iR -= AXES_STEP) {
77+ outerR *= shape[iR];
78+ }
79+ 
80+ AssembleUnit(unitR_, iRCut, innerR, outerR, ubFactorR);
81+ 
82+ if (outerR > static_cast<uint64_t>(compileInfo_->vectorCoreNum)) {
83+ tilingData_->groupR = static_cast<uint64_t>(compileInfo_->vectorCoreNum);
84+ tilingData_->factorRCntPerCore = CeilDiv(outerR, static_cast<uint64_t>(compileInfo_->vectorCoreNum));
85+ tilingData_->groupR = CeilDiv(outerR, tilingData_->factorRCntPerCore);
86+ } else {
87+ tilingData_->factorRCntPerCore = 1;
88+ tilingData_->groupR = outerR;
89+ }
90+}
91+ 
92+template <class Pattern>
93+void ReduceOpTiling::ComputeAWithRCannotFullLoad(const uint64_t* shape)
94+{
95+ uint64_t ubBlockSize = compileInfo_->ubBlockSize / opDag_.minInputBytes;
96+ uint64_t bBlockNum = basicBlock_ * Ratio() / opDag_.maxInputBytes;
97+ uint64_t innerR = unitR_.inner;
98+ uint64_t maxInnerA = resultBlock_ / opDag_.maxInputBytes;
99+ 
100+ uint64_t innerA = 1;
101+ int32_t startA = Pattern::TailA ? Pattern::Dim - CONST1 : Pattern::Dim - AXES_STEP;
102+ int32_t endA = Pattern::FirstA ? CONST0 : CONST1;
103+ int32_t iACut = -1;
104+ for (int32_t iA = startA; iA >= endA; iA -= AXES_STEP) {
105+ uint64_t axisLen = shape[iA];
106+ uint64_t alignedLen = (iA == Pattern::Dim - CONST1) ? CeilAlign(axisLen, ubBlockSize) : axisLen;
107+ if (innerA * alignedLen * innerR <= bBlockNum && innerA * alignedLen <= maxInnerA) {
108+ innerA *= alignedLen;
109+ continue;
110+ }
111+ iACut = iA;
112+ break;
113+ }
114+ 
115+ uint64_t outerA = 1;
116+ for (int32_t iA = iACut - AXES_STEP; iA >= endA; iA -= AXES_STEP) {
117+ outerA *= shape[iA];
118+ }
119+ uint64_t ubFactorA = bBlockNum / (innerR * innerA);
120+ if (iACut == -1) {
121+ outerA = 1;
122+ ubFactorA = shape[endA];
123+ } else {
124+ ubFactorA = iACut == Pattern::Dim - CONST1 ? FloorAlign(ubFactorA, ubBlockSize) : ubFactorA;
125+ if (innerA * ubFactorA > maxInnerA) {
126+ ubFactorA = FloorDiv(maxInnerA, innerA);
127+ }
128+ outerA *= CeilDiv(shape[iACut], ubFactorA);
129+ innerA = innerA * ubFactorA;
130+ }
131+ AssembleUnit(unitA_, iACut, innerA, outerA, ubFactorA);
132+}
133+ 
134+template <class Pattern>
135+void ReduceOpTiling::ComputeAWithRFullLoad(const uint64_t* shape)
136+{
137+ uint64_t dSize = opDag_.minInputBytes;
138+ uint64_t cacheSize = compileInfo_->cacheLineSize / dSize;
139+ uint64_t ubBlockSize = compileInfo_->ubBlockSize / opDag_.minInputBytes;
140+ uint64_t bBlockNum = basicBlock_ * Ratio() / opDag_.maxInputBytes;
141+ uint64_t innerR = unitR_.inner;
142+ uint64_t maxInnerA = resultBlock_ / opDag_.maxInputBytes;
143+ 
144+ uint64_t innerA = 1;
145+ int32_t startA = Pattern::TailA ? Pattern::Dim - CONST1 : Pattern::Dim - AXES_STEP;
146+ int32_t endA = Pattern::FirstA ? CONST0 : CONST1;
147+ uint64_t outerA = 1;
148+ uint64_t step = 1;
149+ for (int32_t iA = startA; iA >= endA; iA -= AXES_STEP) {
150+ outerA *= shape[iA];
151+ }
152+ OP_LOGD(context_, "outerA is %lu",outerA);
153+ int32_t iA;
154+ for (iA = startA; iA >= endA; iA -= AXES_STEP) {
155+ uint64_t axisLen = shape[iA];
156+ uint64_t maxStep = 1UL;
157+ bool splitHere = false;
158+ for (uint64_t s = 1; s <= axisLen; s++) {
159+ uint64_t tmpS = (iA == Pattern::Dim - 1) ? CeilAlign(s, ubBlockSize) : s;
160+ uint64_t tmpInnerA = innerA * tmpS;
161+ uint64_t tmpOuterA = outerA / axisLen * CeilDiv(axisLen, tmpS);
162+ double rate =
163+ static_cast<double>(tmpOuterA) / static_cast<double>(CeilAlign(tmpOuterA, compileInfo_->vectorCoreNum));
164+ bool isContinue =
165+ (tmpInnerA * innerR <= bBlockNum && tmpInnerA <= maxInnerA);
166+ if (isContinue) {
167+ if (tmpInnerA <= cacheSize) {
168+ maxStep = tmpS;
169+ } else {
170+ maxStep = rate >= THRES_HOLD ? tmpS : maxStep;
171+ }
172+ continue;
173+ } else {
174+ splitHere = true;
175+ break;
176+ }
177+ }
178+ if (splitHere || iA - AXES_STEP < 0) {
179+ step = maxStep;
180+ innerA *= step;
181+ outerA = outerA / axisLen * CeilDiv(axisLen, step);
182+ break;
183+ }
184+ innerA *= (iA == Pattern::Dim - 1 ? CeilAlign(axisLen, ubBlockSize) : axisLen);
185+ outerA /= axisLen;
186+ }
187+ AssembleUnit(unitA_, iA, innerA, outerA, step);
188+}
189+ 
190+template <class Pattern>
191+void ReduceOpTiling::SetTilingDataBatchInvariant(const uint64_t* shape)
192+{
193+ uint64_t groupR = tilingData_->groupR;
194+ tilingData_->ubFactorA = unitA_.step;
195+ tilingData_->ubFactorR = unitR_.step;
196+ tilingData_->factorATotalCnt = unitA_.outer;
197+ tilingData_->factorRTotalCnt = unitR_.outer;
198+ tilingData_->basicBlock = basicBlock_;
199+ tilingData_->resultBlock = resultBlock_;
200+ tilingData_->coreNum = static_cast<int32_t>(compileInfo_->vectorCoreNum);
201+ 
202+ if (tilingData_->groupR > 1) {
203+ OP_CHECK_IF(
204+ context_->SetScheduleMode(1) != ge::GRAPH_SUCCESS,
205+ OP_LOGE(context_->GetNodeName(), "Failed to set ScheduleMode!"), return);
206+ }
207+ 
208+ for (int32_t i = 0; i < MAX_DIM; i++) {
209+ tilingData_->shape[i] = shape[i];
210+ tilingData_->stride[i] = viewStride_[i];
211+ tilingData_->sliceNum[i] = sliceNum_[i];
212+ tilingData_->sliceShape[i] = sliceShape_[i];
213+ tilingData_->sliceStride[i] = sliceStride_[i];
214+ }
215+ 
216+ tilingData_->useNddma = IsUseNddma<Pattern>(shape);
217+ ComputeStride<Pattern>(shape);
218+ if (unitA_.outer * groupR <= compileInfo_->vectorCoreNum) {
219+ tilingData_->factorACntPerCore = 1;
220+ } else {
221+ tilingData_->factorACntPerCore = CeilDiv(unitA_.outer, FloorDiv(compileInfo_->vectorCoreNum, groupR));
222+ }
223+ uint32_t realCore =
224+ CeilDiv(unitA_.outer, tilingData_->factorACntPerCore) * CeilDiv(unitR_.outer, tilingData_->factorRCntPerCore);
225+ tilingData_->realCoreNum = realCore;
226+ context_->SetBlockDim(realCore);
227+}
228+ 
229+} // namespace Base
230+} // namespace Ops
231+ 
232+#endif // _REDUCE_BATCH_INVARIANT_TILING_H_