Line data Source code
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 : * \file relu_v2_tiling_arch35.cpp
12 : * \brief
13 : */
14 :
15 : #include "relu_v2_tiling_arch35.h"
16 : #include <graph/utils/type_utils.h>
17 : #include "tiling/tiling_api.h"
18 : #include "tiling/platform/platform_ascendc.h"
19 : #include "register/op_def_registry.h"
20 : #include "log/log.h"
21 : #include "register/tilingdata_base.h"
22 : #include "../op_kernel/arch35/relu_v2_dag.h"
23 : #include "../op_kernel/arch35/relu_v2_tiling_struct.h"
24 : #include "op_host/tiling_util.h"
25 :
26 : #include <iostream>
27 :
28 : using namespace ge;
29 : using namespace ReluV2Op;
30 : using namespace ReluV2Ns;
31 :
32 : namespace optiling {
33 : constexpr uint64_t SYS_WORKSPACE = 16777216; // 16M
34 : constexpr uint64_t RELU_TILING_KEY_ELEMENTWISE_FP16 = 101;
35 : constexpr uint64_t RELU_TILING_KEY_ELEMENTWISE_BF16 = 102;
36 : constexpr uint64_t RELU_TILING_KEY_ELEMENTWISE_FP32 = 103;
37 : constexpr uint64_t RELU_TILING_KEY_ELEMENTWISE_INT8 = 104;
38 : constexpr uint64_t RELU_TILING_KEY_ELEMENTWISE_INT32 = 105;
39 : constexpr uint64_t RELU_TILING_KEY_ELEMENTWISE_UINT8 = 106;
40 : constexpr uint64_t RELU_TILING_KEY_ELEMENTWISE_INT64 = 107;
41 : const gert::Shape g_vec_1_shape = {1};
42 :
43 : class ReluV2Tiling {
44 : public:
45 3 : explicit ReluV2Tiling(gert::TilingContext* context) : tilingContext(context) {};
46 : ge::graphStatus RunTiling();
47 : ReluV2TilingData* tiling = nullptr;
48 :
49 : protected:
50 : ge::graphStatus CalcOutputDtype();
51 : ge::graphStatus CalcInputDtype();
52 : ge::graphStatus CheckShape();
53 : ge::graphStatus SetTilingData();
54 :
55 : private:
56 : gert::TilingContext* tilingContext;
57 : ge::DataType inputDtype = ge::DT_UNDEFINED;
58 : ge::DataType outputDtype = ge::DT_UNDEFINED;
59 : };
60 :
61 : ge::graphStatus ReluV2Tiling::SetTilingData()
62 : {
63 : auto rawTilingData = tilingContext->GetRawTilingData();
64 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, rawTilingData);
65 :
66 : size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);
67 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, currentWorkspace);
68 : currentWorkspace[0] = SYS_WORKSPACE;
69 :
70 : tilingContext->SetBlockDim(tiling->baseTiling.blockNum);
71 : return ge::GRAPH_SUCCESS;
72 : }
73 :
74 : ge::graphStatus ReluV2Tiling::CalcInputDtype()
75 : {
76 : auto inputDesc = tilingContext->GetInputDesc(0);
77 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc);
78 :
79 : this->inputDtype = inputDesc->GetDataType();
80 : return ge::GRAPH_SUCCESS;
81 : }
82 :
83 : static inline const gert::Shape& EnsureNotScalar(const gert::Shape& in_shape)
84 : {
85 : if (in_shape.IsScalar()) {
86 : return g_vec_1_shape;
87 : }
88 : return in_shape;
89 : }
90 :
91 : ge::graphStatus ReluV2Tiling::CheckShape()
92 : {
93 : auto gradientsStorageShape = tilingContext->GetInputShape(0);
94 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, gradientsStorageShape);
95 : const gert::Shape& inputGradientsShape = EnsureNotScalar(gradientsStorageShape->GetStorageShape());
96 :
97 : auto backpropsStorageShape = tilingContext->GetOutputShape(0);
98 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, backpropsStorageShape);
99 : const gert::Shape& outputShape = EnsureNotScalar(backpropsStorageShape->GetStorageShape());
100 : auto maskStorageShape = tilingContext->GetOutputShape(1);
101 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, maskStorageShape);
102 : const gert::Shape& outputMaskShape = EnsureNotScalar(maskStorageShape->GetStorageShape());
103 :
104 : auto dimNum = inputGradientsShape.GetDimNum();
105 :
106 : OP_CHECK_IF((dimNum < 1 || inputGradientsShape.GetDim(dimNum - 1) % 8 != 0),
107 : OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON(tilingContext->GetNodeName(), "x",
108 : Ops::Base::ToString(inputGradientsShape),
109 : "The last dimension of x must be divisible by 8"),
110 : return ge::GRAPH_FAILED);
111 :
112 : OP_CHECK_IF(inputGradientsShape != outputMaskShape,
113 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
114 : tilingContext->GetNodeName(), "x, mask",
115 : Ops::Base::ToString(inputGradientsShape) + ", " + Ops::Base::ToString(outputMaskShape),
116 : "The shapes of x and mask must be the same"),
117 : return ge::GRAPH_FAILED);
118 : OP_CHECK_IF(inputGradientsShape != outputShape,
119 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
120 : tilingContext->GetNodeName(), "x, backprops",
121 : Ops::Base::ToString(inputGradientsShape) + ", " + Ops::Base::ToString(outputShape),
122 : "The shapes of x and backprops must be the same"),
123 : return ge::GRAPH_FAILED);
124 : return ge::GRAPH_SUCCESS;
125 : }
126 :
127 : ge::graphStatus ReluV2Tiling::CalcOutputDtype()
128 : {
129 : auto inputDesc = tilingContext->GetInputDesc(0);
130 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc);
131 : this->inputDtype = inputDesc->GetDataType();
132 :
133 : auto outputDesc = tilingContext->GetOutputDesc(0);
134 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputDesc);
135 : this->outputDtype = outputDesc->GetDataType();
136 :
137 : OP_CHECK_IF(this->inputDtype != this->outputDtype,
138 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(tilingContext->GetNodeName(), "x, y",
139 : ge::TypeUtils::DataTypeToSerialString(this->inputDtype) + ", " +
140 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype),
141 : "The dtypes of x and y must be the same"),
142 : return ge::GRAPH_FAILED);
143 : return ge::GRAPH_SUCCESS;
144 : }
145 :
146 : ge::graphStatus ReluV2Tiling::RunTiling()
147 : {
148 : ElewiseBaseTiling elewiseBaseTiling(tilingContext);
149 : OP_CHECK_IF(CalcInputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Get input dtype failed"),
150 : return ge::GRAPH_FAILED);
151 : OP_CHECK_IF(CalcOutputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Get output dtype failed"),
152 : return ge::GRAPH_FAILED);
153 : OP_CHECK_IF(CheckShape() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Check shape failed"),
154 : return ge::GRAPH_FAILED);
155 :
156 : tiling = tilingContext->GetTilingData<ReluV2TilingData>();
157 : OP_CHECK_IF((tiling == nullptr), OP_LOGE(tilingContext, "Get EleBaseTilingData from context failed"),
158 : return ge::GRAPH_FAILED);
159 : ge::graphStatus res = ge::GRAPH_FAILED;
160 : if (this->outputDtype == ge::DT_FLOAT16) {
161 : res = elewiseBaseTiling.DoTiling<ReluV2DAG<half, half>::OpDag>(tiling->baseTiling);
162 : tilingContext->SetTilingKey(RELU_TILING_KEY_ELEMENTWISE_FP16);
163 : } else if (this->outputDtype == ge::DT_BF16) {
164 : res = elewiseBaseTiling.DoTiling<ReluV2DAG<bfloat16_t, float>::OpDag>(tiling->baseTiling);
165 : tilingContext->SetTilingKey(RELU_TILING_KEY_ELEMENTWISE_BF16);
166 : } else if (this->outputDtype == ge::DT_FLOAT) {
167 : res = elewiseBaseTiling.DoTiling<ReluV2DAG<float, float>::OpDag>(tiling->baseTiling);
168 : tilingContext->SetTilingKey(RELU_TILING_KEY_ELEMENTWISE_FP32);
169 : } else if (this->outputDtype == ge::DT_INT8) {
170 : res = elewiseBaseTiling.DoTiling<ReluV2DAG<int8_t, half>::OpDag>(tiling->baseTiling);
171 : tilingContext->SetTilingKey(RELU_TILING_KEY_ELEMENTWISE_INT8);
172 : } else if (this->outputDtype == ge::DT_INT32) {
173 : res = elewiseBaseTiling.DoTiling<ReluV2DAG<int32_t, int32_t>::OpDag>(tiling->baseTiling);
174 : tilingContext->SetTilingKey(RELU_TILING_KEY_ELEMENTWISE_INT32);
175 : } else if (this->outputDtype == ge::DT_UINT8) {
176 : res = elewiseBaseTiling.DoTiling<ReluV2DAG<uint8_t, half>::OpDag>(tiling->baseTiling);
177 : tilingContext->SetTilingKey(RELU_TILING_KEY_ELEMENTWISE_UINT8);
178 : } else if (this->outputDtype == ge::DT_INT64) {
179 : res = elewiseBaseTiling.DoTiling<ReluV2MaxDAG<int64_t>::OpDag>(tiling->baseTiling);
180 : tilingContext->SetTilingKey(RELU_TILING_KEY_ELEMENTWISE_INT64);
181 : } else {
182 : OP_LOGE_FOR_INVALID_DTYPE(tilingContext->GetNodeName(), "y",
183 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype),
184 : "DT_FLOAT16, DT_BF16, DT_FLOAT, DT_INT8, DT_INT32, DT_UINT8, DT_INT64");
185 : return ge::GRAPH_FAILED;
186 : }
187 :
188 4 : OP_CHECK_IF(res == ge::GRAPH_FAILED,
189 : OP_LOGE(tilingContext->GetNodeName(), "DoTiling failed, output dtype: %s.",
190 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype).c_str()),
191 : return ge::GRAPH_FAILED);
192 : return SetTilingData();
193 : }
194 :
195 : static ge::graphStatus Tiling4ReluV2(gert::TilingContext* context)
196 : {
197 : OP_LOGD("ReluV2Tiling", "Enter Tiling4ReluV2");
198 : if (context == nullptr) {
199 : OP_LOGE("ReluV2Tiling", "Tiling context is null");
200 : return ge::GRAPH_FAILED;
201 : }
202 : auto compileInfo = context->GetCompileInfo<ReluV2CompileInfo>();
203 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo);
204 : ReluV2Tiling tiling(context);
205 : return tiling.RunTiling();
206 : }
207 :
208 : ge::graphStatus TilingPrepareForReluV2(gert::TilingParseContext* context)
209 : {
210 : auto compileInfoPtr = context->GetCompiledInfo<ReluV2CompileInfo>();
211 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfoPtr);
212 : fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
213 : OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
214 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
215 : compileInfoPtr->coreNum = ascendcPlatform.GetCoreNumAiv();
216 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, compileInfoPtr->ubSize);
217 : return ge::GRAPH_SUCCESS;
218 : }
219 :
220 : IMPL_OP_OPTILING(ReluV2).Tiling(Tiling4ReluV2).TilingParse<ReluV2CompileInfo>(TilingPrepareForReluV2);
221 : } // namespace optiling
|