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 : /*!
12 : * \file elu_grad_v2_tiling_arch35.cpp
13 : * \brief
14 : */
15 : #include "elu_grad_v2_tiling_arch35.h"
16 : #include <graph/utils/type_utils.h>
17 : #include "tiling/platform/platform_ascendc.h"
18 : #include "../op_kernel/arch35/elu_grad_v2_dag.h"
19 : #include "atvoss/elewise/elewise_tiling.h"
20 : #include "atvoss/elewise/elewise_base_struct.h"
21 : #include "../op_kernel/arch35/elu_grad_v2_struct.h"
22 : #include "log/log.h"
23 : #include "register/op_impl_registry.h"
24 :
25 : #include <iostream>
26 :
27 : using namespace ge;
28 : using namespace EluGradV2Op;
29 :
30 : namespace optiling {
31 : const uint64_t ALPHA_ATTR_IDX = 0;
32 : const uint64_t SCALE_ATTR_IDX = 1;
33 : const uint64_t INPUT_SCALE_ATTR_IDX = 2;
34 : const uint64_t IS_RESULT_ATTR_IDX = 3;
35 : const int64_t ASCEND_WORKSPACE = 16777216; // 16M
36 :
37 : const gert::Shape g_vec_1_shape = {1};
38 : /**
39 : * Ensure that the returned shape is non-scalar.
40 : * When the dim num of shape is 0, this shape is considered to express a scalar.
41 : * This function returns the original shape when it receives a non-scalar shape,
42 : * and returns the vector shape that returns a {1} when it receives a scalar shape
43 : * @param in_shape input shape
44 : * @return non-scalar shape
45 : */
46 : inline const gert::Shape& EnsureNotScalar(const gert::Shape& in_shape)
47 : {
48 : if (in_shape.IsScalar()) {
49 : return g_vec_1_shape;
50 : }
51 : return in_shape;
52 : }
53 :
54 : ge::graphStatus EluGradV2Tiling::CalcInputDtype()
55 : {
56 : auto gradsDesc = tilingContext->GetInputDesc(0);
57 : auto activationsDesc = tilingContext->GetInputDesc(1);
58 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, gradsDesc);
59 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, activationsDesc);
60 : this->gradsDtype = gradsDesc->GetDataType();
61 : this->activationsDtype = activationsDesc->GetDataType();
62 : OP_CHECK_IF(
63 : this->gradsDtype != ge::DT_FLOAT16 && this->gradsDtype != ge::DT_BF16 && this->gradsDtype != ge::DT_FLOAT,
64 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "grads",
65 : Ops::Base::ToString(static_cast<ge::DataType>(this->gradsDtype)),
66 : "The dtype of grads must be DT_FLOAT16, DT_BF16, or DT_FLOAT"),
67 : return ge::GRAPH_FAILED);
68 : OP_CHECK_IF(this->activationsDtype != this->gradsDtype,
69 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(
70 : tilingContext->GetNodeName(), "grads, activations",
71 : Ops::Base::ToString(static_cast<ge::DataType>(this->gradsDtype)) + ", " +
72 : Ops::Base::ToString(static_cast<ge::DataType>(this->activationsDtype)),
73 : "The dtypes of grads and activations must be the same"),
74 : return ge::GRAPH_FAILED);
75 : return ge::GRAPH_SUCCESS;
76 : }
77 :
78 : ge::graphStatus EluGradV2Tiling::CalcOutputDtype()
79 : {
80 : auto outputDesc = tilingContext->GetOutputDesc(0);
81 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputDesc);
82 : this->outputDtype = outputDesc->GetDataType();
83 : OP_CHECK_IF(
84 : this->outputDtype != ge::DT_FLOAT16 && this->outputDtype != ge::DT_BF16 && this->outputDtype != ge::DT_FLOAT,
85 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "y",
86 : Ops::Base::ToString(static_cast<ge::DataType>(this->outputDtype)),
87 : "The dtype of y must be DT_FLOAT16, DT_BF16, or DT_FLOAT"),
88 : return ge::GRAPH_FAILED);
89 : OP_CHECK_IF(
90 : this->outputDtype != this->gradsDtype,
91 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(tilingContext->GetNodeName(), "grads, y",
92 : Ops::Base::ToString(static_cast<ge::DataType>(this->gradsDtype)) + ", " +
93 : Ops::Base::ToString(static_cast<ge::DataType>(this->outputDtype)),
94 : "The dtypes of grads and y must be the same"),
95 : return ge::GRAPH_FAILED);
96 : return ge::GRAPH_SUCCESS;
97 : }
98 :
99 : ge::graphStatus EluGradV2Tiling::CheckShape()
100 : {
101 : auto gradsStorageShapeV2 = tilingContext->GetInputShape(0);
102 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, gradsStorageShapeV2);
103 : const gert::Shape& gradsShape = EnsureNotScalar(gradsStorageShapeV2->GetStorageShape());
104 :
105 : auto activationsStorageShapeV2 = tilingContext->GetInputShape(1);
106 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, activationsStorageShapeV2);
107 : const gert::Shape& activationsShape = EnsureNotScalar(activationsStorageShapeV2->GetStorageShape());
108 :
109 : auto outStorageShapeV2 = tilingContext->GetOutputShape(0);
110 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outStorageShapeV2);
111 : const gert::Shape& outputShape = EnsureNotScalar(outStorageShapeV2->GetStorageShape());
112 :
113 : OP_CHECK_IF(gradsShape != outputShape && gradsShape != activationsShape,
114 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(tilingContext->GetNodeName(), "grads, activations, y",
115 : Ops::Base::ToString(gradsShape) + ", " +
116 : Ops::Base::ToString(activationsShape) + ", " +
117 : Ops::Base::ToString(outputShape),
118 : "The shapes of grads, activations, and y must be the same"),
119 : return ge::GRAPH_FAILED);
120 : return ge::GRAPH_SUCCESS;
121 : }
122 :
123 : ge::graphStatus EluGradV2Tiling::SetTilingData(bool is_result)
124 : {
125 : if (this->outputDtype == ge::DT_FLOAT16 && is_result) {
126 : dType = static_cast<uint64_t>(EluGradV2_TPL_FP16);
127 : } else if (this->outputDtype == ge::DT_BF16 && is_result) {
128 : dType = static_cast<uint64_t>(EluGradV2_TPL_BF16);
129 : } else if (this->outputDtype == ge::DT_FLOAT && is_result) {
130 : dType = static_cast<uint64_t>(EluGradV2_TPL_FP32);
131 : } else if (this->outputDtype == ge::DT_FLOAT16 && !is_result) {
132 : dType = static_cast<uint64_t>(EluGradV2_TPL_FP16_N);
133 : } else if (this->outputDtype == ge::DT_BF16 && !is_result) {
134 : dType = static_cast<uint64_t>(EluGradV2_TPL_BF16_N);
135 : } else if (this->outputDtype == ge::DT_FLOAT && !is_result) {
136 : dType = static_cast<uint64_t>(EluGradV2_TPL_FP32_N);
137 : } else {
138 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "y",
139 : Ops::Base::ToString(static_cast<ge::DataType>(this->outputDtype)),
140 : "The dtype of y must be DT_FLOAT16, DT_BF16, or DT_FLOAT");
141 : return ge::GRAPH_FAILED;
142 : }
143 : return ge::GRAPH_SUCCESS;
144 : }
145 :
146 : ge::graphStatus EluGradV2Tiling::SetAttr()
147 : {
148 : auto attrs = tilingContext->GetAttrs();
149 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, attrs);
150 : const float* alphaValueAttr = attrs->GetAttrPointer<float>(ALPHA_ATTR_IDX);
151 : const float* scaleValueAttr = attrs->GetAttrPointer<float>(SCALE_ATTR_IDX);
152 : const float* inputScaleValueAttr = attrs->GetAttrPointer<float>(INPUT_SCALE_ATTR_IDX);
153 : this->isResult = *tilingContext->GetAttrs()->GetAttrPointer<bool>(IS_RESULT_ATTR_IDX);
154 : float alphaValue = alphaValueAttr == nullptr ? 1.0f : *alphaValueAttr;
155 : OP_CHECK_IF(this->isResult && alphaValue < 0,
156 : OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(
157 : tilingContext->GetNodeName(), "alpha", std::to_string(alphaValue),
158 : "If is_result is true, the value of alpha must be greater than or equal to 0"),
159 : return ge::GRAPH_FAILED);
160 : float scale = scaleValueAttr == nullptr ? 1.0f : *scaleValueAttr;
161 : float inputScale = inputScaleValueAttr == nullptr ? 1.0f : *inputScaleValueAttr;
162 : float negcoef = alphaValue * scale;
163 : tiling->negcoef = negcoef;
164 : OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : negcoef=%f.", negcoef);
165 : tiling->scale = scale;
166 : OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : scale=%f.", scale);
167 : tiling->inputScale = inputScale;
168 : OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : inputScale=%f.", inputScale);
169 :
170 : return ge::GRAPH_SUCCESS;
171 : }
172 :
173 : ge::graphStatus EluGradV2Tiling::RunTiling()
174 : {
175 : ElewiseBaseTiling elewiseBaseTiling(tilingContext);
176 : ge::graphStatus status = ge::GRAPH_FAILED;
177 : tiling = tilingContext->GetTilingData<EluGradV2TilingData>();
178 : OP_CHECK_IF((tiling == nullptr), OP_LOGE(tilingContext, "Get EleBaseTilingData from context failed"),
179 : return ge::GRAPH_FAILED);
180 : status = CalcInputDtype();
181 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Get input dtype failed"), return ge::GRAPH_FAILED);
182 : status = CalcOutputDtype();
183 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Get output dtype failed"), return ge::GRAPH_FAILED);
184 : status = CheckShape();
185 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Check shape failed"), return ge::GRAPH_FAILED);
186 : status = SetAttr();
187 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "SetAttr failed"), return ge::GRAPH_FAILED);
188 : status = SetTilingData(this->isResult);
189 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "SetTilingData failed"), return ge::GRAPH_FAILED);
190 : if (dType == static_cast<uint64_t>(EluGradV2_TPL_FP16)) {
191 : status = elewiseBaseTiling.DoTiling<EluGradV2IsResultOp<half>::OpDag>(tiling->baseTiling);
192 : } else if (dType == static_cast<uint64_t>(EluGradV2_TPL_BF16)) {
193 : status = elewiseBaseTiling.DoTiling<EluGradV2IsResultOp<bfloat16_t>::OpDag>(tiling->baseTiling);
194 : } else if (dType == static_cast<uint64_t>(EluGradV2_TPL_FP32)) {
195 : status = elewiseBaseTiling.DoTiling<EluGradV2IsResultOp<float>::OpDag>(tiling->baseTiling);
196 : } else if (dType == static_cast<uint64_t>(EluGradV2_TPL_FP16_N)) {
197 : status = elewiseBaseTiling.DoTiling<EluGradV2NoResultOp<half>::OpDag>(tiling->baseTiling);
198 : } else if (dType == static_cast<uint64_t>(EluGradV2_TPL_BF16_N)) {
199 : status = elewiseBaseTiling.DoTiling<EluGradV2NoResultOp<bfloat16_t>::OpDag>(tiling->baseTiling);
200 : } else if (dType == static_cast<uint64_t>(EluGradV2_TPL_FP32_N)) {
201 : status = elewiseBaseTiling.DoTiling<EluGradV2NoResultOp<float>::OpDag>(tiling->baseTiling);
202 : } else {
203 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "y",
204 : Ops::Base::ToString(static_cast<ge::DataType>(this->outputDtype)),
205 : "The dtype of y must be DT_FLOAT16, DT_BF16, or DT_FLOAT");
206 : return ge::GRAPH_FAILED;
207 : }
208 8 : OP_CHECK_IF(status == ge::GRAPH_FAILED,
209 : OP_LOGE(tilingContext->GetNodeName(), "elewiseBaseTiling failed, output dtype: %s.",
210 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype).c_str()),
211 : return ge::GRAPH_FAILED);
212 : schMode = tiling->baseTiling.scheMode;
213 : const uint64_t tilingKey = GET_TPL_TILING_KEY(schMode, dType);
214 : OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : tilingKey=%ld.", tilingKey);
215 : tilingContext->SetTilingKey(tilingKey);
216 : tilingContext->SetBlockDim(tiling->baseTiling.blockNum);
217 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, tilingContext->GetRawTilingData());
218 :
219 : size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);
220 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, currentWorkspace);
221 : currentWorkspace[0] = static_cast<uint64_t>(ASCEND_WORKSPACE);
222 :
223 : return ge::GRAPH_SUCCESS;
224 : }
225 :
226 : static ge::graphStatus Tiling4EluGradV2(gert::TilingContext* tilingContextSelf)
227 : {
228 : OP_LOGD(tilingContextSelf->GetNodeName(), "Tiling4EluGradV2 rt2.0 is running.");
229 : auto compileInfo = tilingContextSelf->GetCompileInfo<EluGradV2CompileInfo>();
230 : OP_CHECK_NULL_WITH_CONTEXT(tilingContextSelf, compileInfo);
231 : EluGradV2Tiling eluGradV2Tiling(tilingContextSelf);
232 : return eluGradV2Tiling.RunTiling();
233 : }
234 :
235 : static ge::graphStatus TilingPrepareForEluGradV2(gert::TilingParseContext* context)
236 : {
237 : auto compileInfoPtr = context->GetCompiledInfo<EluGradV2CompileInfo>();
238 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfoPtr);
239 : fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
240 : OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
241 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
242 : compileInfoPtr->coreNum = ascendcPlatform.GetCoreNumAiv();
243 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, compileInfoPtr->ubSize);
244 : return ge::GRAPH_SUCCESS;
245 : }
246 :
247 : IMPL_OP_OPTILING(EluGradV2).Tiling(Tiling4EluGradV2).TilingParse<EluGradV2CompileInfo>(TilingPrepareForEluGradV2);
248 : } // namespace optiling
|