已合并
Revert "限制adds/gt scalar/ge tensor在支持非连续tensor时的范围" #2782
Revert "限制adds/gt scalar/ge tensor在支持非连续tensor时的范围" #2782
已合并
raoliang_sac创建于 5月16日
5 个文件变更+16-310
Dcommon/inc/op_api/broadcast_noncontiguous_util.h+0-237
@@ -1,237 +0,0 @@
1-/**
2- * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3- * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4- * CANN Open Software License Agreement Version 2.0 (the "License").
5- * Please refer to the License for details. You may not use this file except in compliance with the License.
6- * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7- * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8- * See LICENSE in the root of the software repository for the full text of the License.
9- */
10- 
11-/*!
12- * \file broadcast_noncontiguous_util.h
13- * \brief
14- */
15- 
16-#ifndef CANN_OPS_BUILT_IN_BROADCAST_NONCONTIGUOUS_UTIL_H_
17-#define CANN_OPS_BUILT_IN_BROADCAST_NONCONTIGUOUS_UTIL_H_
18- 
19-#include "op_api/aclnn_check.h"
20-#include "opdev/platform.h"
21-#include "opdev/op_log.h"
22-#include "opdev/tensor_view_utils.h"
23- 
24-namespace op {
25- 
26-static constexpr int64_t CACHE_LINE_SIZE = 128;
27-static constexpr int64_t DATA_SIZE_LIMIT = 8192 * 64;
28-static constexpr int64_t DIM_TWO = 2;
29-static constexpr int64_t DIM_THREE = 3;
30-static constexpr int64_t DIM_FOUR = 4;
31-static constexpr int64_t LAST_TRANSPOSE_LONG_AXIS_LIMIT = 512;
32- 
33-static bool IsOnlyLastTwoAxesTransposed(const op::Shape& viewShape, const op::Strides& strides)
34-{
35- size_t shapeDim = viewShape.GetDimNum();
36- size_t stridesDim = strides.size();
37- if (viewShape.GetDimNum() < DIM_TWO || shapeDim != stridesDim) {
38- return false;
39- }
40- size_t lastDim = shapeDim - 1;
41- size_t secondLastDim = stridesDim - DIM_TWO;
42- bool transposedStride = (strides[lastDim] == viewShape[secondLastDim]) && (strides[secondLastDim] == 1);
43- bool othersContiguous = true;
44- if (shapeDim > 2) {
45- int64_t expectedStride = viewShape[lastDim] * viewShape[secondLastDim];
46- for (int64_t i = shapeDim - DIM_THREE; i >= 0; i--) {
47- if (strides[i] != expectedStride) {
48- othersContiguous = false;
49- break;
50- }
51- expectedStride = expectedStride * viewShape[i];
52- }
53- }
54- bool result = transposedStride && othersContiguous;
55- return result;
56-}
57- 
58-static bool CheckBasicConstraints(const op::Shape& viewShape, const op::Strides& viewStride)
59-{
60- size_t shapeDim = viewShape.GetDimNum();
61- size_t stridesDim = viewStride.size();
62- if (shapeDim != stridesDim) {
63- OP_LOGI("Broadcast Template NonContiguous UnSupported. shapeDim: %d stridesDim: %d", shapeDim, stridesDim);
64- return false;
65- }
66- if (shapeDim > DIM_FOUR) {
67- OP_LOGI("Broadcast Template NonContiguous UnSupported. shapeDim: %d > 4", shapeDim);
68- return false;
69- }
70- if (!IsRegBase()) {
71- OP_LOGI("Broadcast Template NonContiguous UnSupported. not RegBase");
72- return false;
73- }
74- return true;
75-}
76- 
77-static bool IsSupportedByLargeLastDim(const op::Shape& viewShape, const op::Strides& viewStride, int64_t cacheLineDim)
78-{
79- size_t shapeDim = viewShape.GetDimNum();
80- size_t stridesDim = viewStride.size();
81- int64_t largeLastDimThreshold = cacheLineDim * 8;
82-
83- if (stridesDim < 2) {
84- return false;
85- }
86-
87- int64_t lastDimSize = viewShape[shapeDim - 1];
88- int64_t lastStride = viewStride[stridesDim - 1];
89- int64_t secondLastStride = viewStride[stridesDim - DIM_TWO];
90-
91- if (lastDimSize >= largeLastDimThreshold && lastStride == 1) {
92- if (secondLastStride > largeLastDimThreshold * 100) {
93- return false;
94- }
95- if (secondLastStride >= lastDimSize && secondLastStride > largeLastDimThreshold * 10) {
96- return false;
97- }
98- OP_LOGI("Broadcast Template NonContiguous Supported. Shape[-1]: %d > %d and Stride[-1]: 1 Case",
99- lastDimSize, largeLastDimThreshold);
100- return true;
101- }
102- return false;
103-}
104- 
105-static bool IsSupportedByStridePattern(const op::Shape& viewShape, const op::Strides& viewStride, int64_t cacheLineDim)
106-{
107- size_t shapeDim = viewShape.GetDimNum();
108- size_t stridesDim = viewStride.size();
109- int64_t stridePatternThreshold = cacheLineDim * 4;
110- if (stridesDim > 1 && viewStride[stridesDim - 1] == 1 && viewShape[shapeDim - 1] < cacheLineDim &&
111- viewStride[stridesDim - DIM_TWO] > stridePatternThreshold) {
112- OP_LOGI("Broadcast Template NonContiguous Supported. Stride[-1]: 1 and Stride[-2]: %d and Shape[-1]: %d < CacheLineDim: %d Case",
113- viewStride[stridesDim - DIM_TWO], viewShape[shapeDim - 1], cacheLineDim);
114- return true;
115- }
116- return false;
117-}
118- 
119-static bool IsSupportedBySmallDataSize(
120- const op::Shape& viewShape, const op::Strides& viewStride, int64_t cacheLineDim, size_t typeSize)
121-{
122- size_t shapeDim = viewShape.GetDimNum();
123- size_t stridesDim = viewStride.size();
124- int64_t dataSize = viewShape.GetShapeSize() * typeSize;
125-
126- int64_t smallDataSizeThreshold = cacheLineDim;
127- int64_t smallDataSizeLimit = DATA_SIZE_LIMIT / 8;
128-
129- if ((viewShape[shapeDim - 1] < smallDataSizeThreshold) &&
130- (dataSize < smallDataSizeLimit) &&
131- (viewStride[stridesDim - 1] == 1)) {
132- OP_LOGI("Broadcast Template NonContiguous Supported. Stride[-1]: %d Shape[-1]: %d < CacheLineDim: %d and Tensor DataSize: %d < %d Case",
133- viewStride[stridesDim - 1], viewShape[shapeDim - 1], cacheLineDim, dataSize, smallDataSizeLimit);
134- return true;
135- }
136- return false;
137-}
138- 
139-static bool IsLastTransposePreferContiguous(
140- const op::Shape& viewShape, const op::Strides& viewStride, int64_t cacheLineDim)
141-{
142- size_t shapeDim = viewShape.GetDimNum();
143- size_t stridesDim = viewStride.size();
144- if (shapeDim < DIM_TWO || shapeDim != stridesDim) {
145- return false;
146- }
147- int64_t secondLastDimSize = viewShape[shapeDim - DIM_TWO];
148- int64_t lastDimSize = viewShape[shapeDim - 1];
149-
150- if (secondLastDimSize < cacheLineDim) {
151- OP_LOGI("Broadcast Template NonContiguous UnSupported. LastTwoAxesTransposed prefer contiguous, Shape[-2]: %ld < CacheLineDim: %ld",
152- secondLastDimSize, cacheLineDim);
153- return true;
154- }
155- if (lastDimSize >= LAST_TRANSPOSE_LONG_AXIS_LIMIT) {
156- OP_LOGI("Broadcast Template NonContiguous UnSupported. LastTwoAxesTransposed prefer contiguous, Shape[-1]: %ld >= %ld",
157- lastDimSize, LAST_TRANSPOSE_LONG_AXIS_LIMIT);
158- return true;
159- }
160- if (lastDimSize < cacheLineDim) {
161- OP_LOGI("Broadcast Template NonContiguous UnSupported. LastTwoAxesTransposed prefer contiguous, Shape[-1]: %ld < CacheLineDim: %ld",
162- lastDimSize, cacheLineDim);
163- return true;
164- }
165- if (secondLastDimSize >= cacheLineDim && lastDimSize >= cacheLineDim && lastDimSize < LAST_TRANSPOSE_LONG_AXIS_LIMIT) {
166- OP_LOGI("Broadcast Template NonContiguous UnSupported. LastTwoAxesTransposed prefer contiguous, Shape[-2]: %ld >= CacheLineDim: %ld and Shape[-1]: %ld >= CacheLineDim: %ld and Shape[-1]: %ld < %ld",
167- secondLastDimSize, cacheLineDim, lastDimSize, cacheLineDim, lastDimSize, LAST_TRANSPOSE_LONG_AXIS_LIMIT);
168- return true;
169- }
170- return false;
171-}
172- 
173-static bool IsSupportedByTransposedAxes(
174- const op::Shape& viewShape, const op::Strides& viewStride, int64_t cacheLineDim)
175-{
176- bool isOnlyLastTwoAxesTransposed = IsOnlyLastTwoAxesTransposed(viewShape, viewStride);
177-
178- if (isOnlyLastTwoAxesTransposed) {
179- bool preferContiguous = IsLastTransposePreferContiguous(viewShape, viewStride, cacheLineDim);
180-
181- if (preferContiguous) {
182- return false;
183- }
184- OP_LOGI("BroadcastTemplateNonContiguousSupport is True, LastTwoAxesTransposed Case");
185- return true;
186- }
187- return false;
188-}
189- 
190-static bool IsBroadcastTemplateNonContiguousSupport(const aclTensor* input)
191-{
192- auto viewShape = input->GetViewShape();
193- auto viewStride = input->GetViewStrides();
194- size_t typeSize = op::TypeSize(input->GetDataType());
195-
196- if (typeSize == 0) {
197- OP_LOGI("Broadcast Template NonContiguous UnSupported. typeSize is 0");
198- return false;
199- }
200- int64_t cacheLineDim = CACHE_LINE_SIZE / typeSize;
201- 
202- if (!CheckBasicConstraints(viewShape, viewStride)) {
203- return false;
204- }
205-
206- bool isContiguous = op::IsContiguous(input);
207- if (isContiguous) {
208- OP_LOGI("Broadcast Template NonContiguous Supported. Tensor is Contiguous");
209- return true;
210- }
211-
212- bool largeLastDim = IsSupportedByLargeLastDim(viewShape, viewStride, cacheLineDim);
213- if (largeLastDim) {
214- return true;
215- }
216-
217- bool stridePattern = IsSupportedByStridePattern(viewShape, viewStride, cacheLineDim);
218- if (stridePattern) {
219- return true;
220- }
221-
222- bool smallDataSize = IsSupportedBySmallDataSize(viewShape, viewStride, cacheLineDim, typeSize);
223- if (smallDataSize) {
224- return true;
225- }
226-
227- bool transposedAxes = IsSupportedByTransposedAxes(viewShape, viewStride, cacheLineDim);
228- if (transposedAxes) {
229- return true;
230- }
231-
232- return false;
233-}
234- 
235-} // namespace op
236- 
237-#endif // CANN_OPS_BUILT_IN_BROADCAST_NONCONTIGUOUS_UTIL_H_
Mmath/add/op_api/aclnn_add.cpp+3-1
@@ -336,6 +336,7 @@ aclnnStatus aclnnAddGetWorkspaceSize(
336 return ACLNN_SUCCESS;336 return ACLNN_SUCCESS;
337 }337 }
338 338 
339+ bool isSupportNonContiguous = IsRegBase();
339 auto selfWithStride = uniqueExecutor.get()->CreateView(340 auto selfWithStride = uniqueExecutor.get()->CreateView(
340 self, self->GetViewShape(), self->GetStorageShape(), self->GetViewStrides(), self->GetViewOffset());341 self, self->GetViewShape(), self->GetStorageShape(), self->GetViewStrides(), self->GetViewOffset());
341 CHECK_RET(selfWithStride != nullptr, ACLNN_ERR_INNER_NULLPTR);342 CHECK_RET(selfWithStride != nullptr, ACLNN_ERR_INNER_NULLPTR);
@@ -349,7 +350,8 @@ aclnnStatus aclnnAddGetWorkspaceSize(
349 // 判断输入是否符合kernel支持的混合输入类型350 // 判断输入是否符合kernel支持的混合输入类型
350 bool isMixDataType = isAddMixDtypeSupport(self, other);351 bool isMixDataType = isAddMixDtypeSupport(self, other);
351 if (isMixDataType && !(alpha->ToFloat() > 1 || alpha->ToFloat() < 1)) {352 if (isMixDataType && !(alpha->ToFloat() > 1 || alpha->ToFloat() < 1)) {
352- if (l0op::IsAddSupportNonContiguous(self, other)) {353+ // 无需调用Cast,直接调用L0带混合数据类型的kernel
354+ if (isSupportNonContiguous) {
353 addOpOut = l0op::Add(selfWithStride, otherWithStride, uniqueExecutor.get());355 addOpOut = l0op::Add(selfWithStride, otherWithStride, uniqueExecutor.get());
354 } else {356 } else {
355 // 固定写法,将输入self转换成连续的tensor357 // 固定写法,将输入self转换成连续的tensor
Mmath/add/op_api/add.cpp+5-13
@@ -10,7 +10,6 @@
10 10 
11#include "add.h"11#include "add.h"
12#include "op_api/aclnn_check.h"12#include "op_api/aclnn_check.h"
13-#include "op_api/broadcast_noncontiguous_util.h"
14#include "opdev/aicpu/aicpu_task.h"13#include "opdev/aicpu/aicpu_task.h"
15#include "opdev/make_op_executor.h"14#include "opdev/make_op_executor.h"
16#include "opdev/op_log.h"15#include "opdev/op_log.h"
@@ -40,7 +39,7 @@ static const std::initializer_list<DataType> ASCEND610LITE_AICORE_DTYPE_SUPPORT_
40static inline const std::initializer_list<DataType>& GetAiCoreDtypeSupportListBySocVersion()39static inline const std::initializer_list<DataType>& GetAiCoreDtypeSupportListBySocVersion()
41{40{
42 auto curArch = GetCurrentPlatformInfo().GetCurNpuArch();41 auto curArch = GetCurrentPlatformInfo().GetCurNpuArch();
43- OP_LOGI("curArch is %u", static_cast<uint32_t>(curArch));42+ OP_LOGI("AddL0", "curArch is %u", static_cast<uint32_t>(curArch));
44 switch (curArch) {43 switch (curArch) {
45 case NpuArch::DAV_2201:44 case NpuArch::DAV_2201:
46 case NpuArch::DAV_3510: {45 case NpuArch::DAV_3510: {
@@ -58,22 +57,15 @@ static inline const std::initializer_list<DataType>& GetAiCoreDtypeSupportListBy
58 }57 }
59}58}
60 59 
60+// 根据芯片类型、dtype判断算子是否支持走aicore
61static inline bool IsAiCoreSupport(const aclTensor* self)61static inline bool IsAiCoreSupport(const aclTensor* self)
62{62{
63 return CheckType(self->GetDataType(), GetAiCoreDtypeSupportListBySocVersion());63 return CheckType(self->GetDataType(), GetAiCoreDtypeSupportListBySocVersion());
64}64}
65 65 
66-bool IsAddSupportNonContiguous(const aclTensor* self, const aclTensor* other)66+bool IsAddSupportNonContiguous(const aclTensor* self, const aclTensor *other) {
67-{67+ bool isSupportNonContiguous = IsRegBase();
68- bool selfNonContiguousSupport = IsBroadcastTemplateNonContiguousSupport(self);68+ return isSupportNonContiguous && IsAiCoreSupport(self) && IsAiCoreSupport(other);
69- bool otherNonContiguousSupport = IsBroadcastTemplateNonContiguousSupport(other);
70- bool selfAiCoreSupport = IsAiCoreSupport(self);
71- bool otherAiCoreSupport = IsAiCoreSupport(other);
72- OP_LOGI(
73- "IsAddSupportNonContiguous: selfNonContiguousSupport %d otherNonContiguousSupport %d selfAiCoreSupport %d "
74- "otherAiCoreSupport %d",
75- selfNonContiguousSupport, otherNonContiguousSupport, selfAiCoreSupport, otherAiCoreSupport);
76- return selfNonContiguousSupport && otherNonContiguousSupport && selfAiCoreSupport && otherAiCoreSupport;
77}69}
78 70 
79// AICORE算子kernel71// AICORE算子kernel
Mmath/greater/op_api/greater.cpp+4-7
@@ -8,7 +8,6 @@
8 * See LICENSE in the root of the software repository for the full text of the License.8 * See LICENSE in the root of the software repository for the full text of the License.
9 */9 */
10#include "greater.h"10#include "greater.h"
11-#include "op_api/broadcast_noncontiguous_util.h"
12#include "opdev/aicpu/aicpu_task.h"11#include "opdev/aicpu/aicpu_task.h"
13#include "opdev/make_op_executor.h"12#include "opdev/make_op_executor.h"
14#include "opdev/op_def.h"13#include "opdev/op_def.h"
@@ -45,6 +44,7 @@ static const std::initializer_list<op::DataType> REGBASE_DTYPE_SUPPORT_LIST = {
45 op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_BF16,44 op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_BF16,
46 op::DataType::DT_INT64, op::DataType::DT_UINT64, op::DataType::DT_BOOL};45 op::DataType::DT_INT64, op::DataType::DT_UINT64, op::DataType::DT_BOOL};
47 46 
47+// 根据dtype判断算子是否支持走aicore
48static bool IsAiCoreSupport(const aclTensor* self)48static bool IsAiCoreSupport(const aclTensor* self)
49{49{
50 auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();50 auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
@@ -60,13 +60,10 @@ static bool IsAiCoreSupport(const aclTensor* self)
60 return CheckType(self->GetDataType(), AICORE_DTYPE_SUPPORT_910_LIST);60 return CheckType(self->GetDataType(), AICORE_DTYPE_SUPPORT_910_LIST);
61}61}
62 62 
63+// 判断tensor是否支持非连续
63bool IsGreaterSupportNonContiguous(const aclTensor* self) {64bool IsGreaterSupportNonContiguous(const aclTensor* self) {
64- bool selfNonContiguousSupport = IsBroadcastTemplateNonContiguousSupport(self);65+ bool isSupportNonContiguous = IsRegBase();
65- bool selfAiCoreSupport = IsAiCoreSupport(self);66+ return isSupportNonContiguous && IsAiCoreSupport(self);
66- OP_LOGI(
67- "IsGreaterSupportNonContiguous: selfNonContiguousSupport %d selfAiCoreSupport %d",
68- selfNonContiguousSupport, selfAiCoreSupport);
69- return selfNonContiguousSupport && selfAiCoreSupport;
70}67}
71 68 
72// AICORE算子kernel69// AICORE算子kernel
Mmath/greater_equal/op_api/greater_equal.cpp+4-52
@@ -17,7 +17,6 @@
17#include "opdev/op_log.h"17#include "opdev/op_log.h"
18#include "opdev/shape_utils.h"18#include "opdev/shape_utils.h"
19#include "op_api/aclnn_check.h"19#include "op_api/aclnn_check.h"
20-#include "opdev/tensor_view_utils.h"
21 20 
22using namespace op;21using namespace op;
23 22 
@@ -43,6 +42,7 @@ static const std::initializer_list<op::DataType> REGBASE_DTYPE_SUPPORT_LIST = {
43 op::DataType::DT_UINT8, op::DataType::DT_BF16, op::DataType::DT_INT64,42 op::DataType::DT_UINT8, op::DataType::DT_BF16, op::DataType::DT_INT64,
44 op::DataType::DT_UINT64, op::DataType::DT_BOOL};43 op::DataType::DT_UINT64, op::DataType::DT_BOOL};
45 44 
45+// 根据芯片类型、dtype判断算子是否支持走aicore
46static inline bool IsAiCoreSupport(const aclTensor *self) {46static inline bool IsAiCoreSupport(const aclTensor *self) {
47 auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();47 auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
48 if (IsRegBase(npuArch)) {48 if (IsRegBase(npuArch)) {
@@ -55,58 +55,10 @@ static inline bool IsAiCoreSupport(const aclTensor *self) {
55 return op::CheckType(self->GetDataType(), AICORE_DTYPE_SUPPORT_LIST);55 return op::CheckType(self->GetDataType(), AICORE_DTYPE_SUPPORT_LIST);
56}56}
57 57 
58-static bool IsGeTensorSupportNonContiguous(const aclTensor* input)58+// 判断tensor是否支持非连续
59-{
60- auto viewShape = input->GetViewShape();
61- auto viewStride = input->GetViewStrides();
62- size_t typeSize = op::TypeSize(input->GetDataType());
63-
64- if (typeSize == 0) {
65- OP_LOGI("GeTensor NonContiguous UnSupported. typeSize is 0");
66- return false;
67- }
68-
69- size_t shapeDim = viewShape.GetDimNum();
70- size_t stridesDim = viewStride.size();
71- if (shapeDim != stridesDim) {
72- OP_LOGI("GeTensor NonContiguous UnSupported. shapeDim: %d != stridesDim: %d", shapeDim, stridesDim);
73- return false;
74- }
75- if (shapeDim > 4) {
76- OP_LOGI("GeTensor NonContiguous UnSupported. shapeDim: %d > 4", shapeDim);
77- return false;
78- }
79- if (!IsRegBase()) {
80- OP_LOGI("GeTensor NonContiguous UnSupported. not RegBase");
81- return false;
82- }
83- if (op::IsContiguous(input)) {
84- OP_LOGI("GeTensor NonContiguous Supported. tensor is contiguous");
85- return true;
86- }
87- if (viewStride[stridesDim - 1] != 1) {
88- OP_LOGI("GeTensor NonContiguous UnSupported. stride[-1]: %d != 1", viewStride[stridesDim - 1]);
89- return false;
90- }
91- 
92- int64_t cacheLineDim = 128 / typeSize;
93- if (viewShape[shapeDim - 1] >= cacheLineDim) {
94- OP_LOGI("GeTensor NonContiguous Supported. large last dim, shape[-1]: %d >= cacheLineDim: %d",
95- viewShape[shapeDim - 1], cacheLineDim);
96- return true;
97- }
98- OP_LOGI("GeTensor NonContiguous UnSupported. shape[-1]: %d < cacheLineDim: %d",
99- viewShape[shapeDim - 1], cacheLineDim);
100- return false;
101-}
102- 
103bool IsGreaterEqualSupportNonContiguous(const aclTensor* self) {59bool IsGreaterEqualSupportNonContiguous(const aclTensor* self) {
104- bool selfNonContiguousSupport = IsGeTensorSupportNonContiguous(self);60+ bool isSupportNonContiguous = IsRegBase();
105- bool selfAiCoreSupport = IsAiCoreSupport(self);61+ return isSupportNonContiguous && IsAiCoreSupport(self);
106- OP_LOGI(
107- "IsGreaterEqualSupportNonContiguous: selfNonContiguousSupport %d selfAiCoreSupport %d",
108- selfNonContiguousSupport, selfAiCoreSupport);
109- return selfNonContiguousSupport && selfAiCoreSupport;
110}62}
111 63 
112const aclTensor *GreaterEqual(const aclTensor *self, const aclTensor *other, aclOpExecutor *executor) {64const aclTensor *GreaterEqual(const aclTensor *self, const aclTensor *other, aclOpExecutor *executor) {