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_tiling_arch35.cpp
13 : * \brief
14 : */
15 : #include "elu_grad_tiling_arch35.h"
16 : #include <graph/utils/type_utils.h>
17 : #include "tiling/platform/platform_ascendc.h"
18 : #include "register/op_impl_registry.h"
19 : #include "log/log.h"
20 : #include <iostream>
21 : #include "../op_kernel/arch35/elu_grad_dag.h"
22 : #include "../op_kernel/arch35/elu_grad_struct.h"
23 :
24 : using namespace ge;
25 : using namespace EluGradOp;
26 :
27 : namespace optiling {
28 : const int64_t ASCEND_WORKSPACE = 16777216; // 16M
29 :
30 : const gert::Shape g_vec_1_shape = {1};
31 : inline const gert::Shape& EnsureNotScalar(const gert::Shape& in_shape)
32 : {
33 : if (in_shape.IsScalar()) {
34 : return g_vec_1_shape;
35 : }
36 : return in_shape;
37 : }
38 :
39 : ge::graphStatus EluGradTiling::CalcInputDtype()
40 : {
41 : auto gradsDesc = tilingContext->GetInputDesc(0);
42 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, gradsDesc);
43 : auto activationsDesc = tilingContext->GetInputDesc(1);
44 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, activationsDesc);
45 : this->gradsDtype = gradsDesc->GetDataType();
46 : OP_CHECK_IF(
47 : this->gradsDtype != ge::DT_FLOAT16 && this->gradsDtype != ge::DT_BF16 && this->gradsDtype != ge::DT_FLOAT,
48 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "grads",
49 : Ops::Base::ToString(static_cast<ge::DataType>(this->gradsDtype)),
50 : "The dtype of grads must be DT_FLOAT16, DT_BF16, or DT_FLOAT"),
51 : return ge::GRAPH_FAILED);
52 : this->activationsDtype = activationsDesc->GetDataType();
53 : OP_CHECK_IF(this->activationsDtype != this->gradsDtype,
54 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(
55 : tilingContext->GetNodeName(), "grads, activations",
56 : Ops::Base::ToString(static_cast<ge::DataType>(this->gradsDtype)) + ", " +
57 : Ops::Base::ToString(static_cast<ge::DataType>(this->activationsDtype)),
58 : "The dtypes of grads and activations must be the same"),
59 : return ge::GRAPH_FAILED);
60 : return ge::GRAPH_SUCCESS;
61 : }
62 :
63 : ge::graphStatus EluGradTiling::CalcOutputDtype()
64 : {
65 : auto outputDesc = tilingContext->GetOutputDesc(0);
66 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputDesc);
67 : this->outputDtype = outputDesc->GetDataType();
68 : OP_CHECK_IF(
69 : this->outputDtype != ge::DT_FLOAT16 && this->outputDtype != ge::DT_BF16 && this->outputDtype != ge::DT_FLOAT,
70 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "y",
71 : Ops::Base::ToString(static_cast<ge::DataType>(this->outputDtype)),
72 : "The dtype of y must be DT_FLOAT16, DT_BF16, or DT_FLOAT"),
73 : return ge::GRAPH_FAILED);
74 : OP_CHECK_IF(
75 : this->outputDtype != this->gradsDtype,
76 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(tilingContext->GetNodeName(), "grads, y",
77 : Ops::Base::ToString(static_cast<ge::DataType>(this->gradsDtype)) + ", " +
78 : Ops::Base::ToString(static_cast<ge::DataType>(this->outputDtype)),
79 : "The dtypes of grads and y must be the same"),
80 : return ge::GRAPH_FAILED);
81 : return ge::GRAPH_SUCCESS;
82 : }
83 :
84 : ge::graphStatus EluGradTiling::CheckShape()
85 : {
86 : auto gradsStorageShape = tilingContext->GetInputShape(0);
87 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, gradsStorageShape);
88 : const gert::Shape& gradsShape = EnsureNotScalar(gradsStorageShape->GetStorageShape());
89 :
90 : auto activationsStorageShape = tilingContext->GetInputShape(1);
91 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, activationsStorageShape);
92 : const gert::Shape& activationsShape = EnsureNotScalar(activationsStorageShape->GetStorageShape());
93 :
94 : auto outStorageShape = tilingContext->GetOutputShape(0);
95 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outStorageShape);
96 : const gert::Shape& outputShape = EnsureNotScalar(outStorageShape->GetStorageShape());
97 :
98 : OP_CHECK_IF(gradsShape != outputShape && gradsShape != activationsShape,
99 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(tilingContext->GetNodeName(), "grads, activations, y",
100 : Ops::Base::ToString(gradsShape) + ", " +
101 : Ops::Base::ToString(activationsShape) + ", " +
102 : Ops::Base::ToString(outputShape),
103 : "The shapes of grads, activations, and y must be the same"),
104 : return ge::GRAPH_FAILED);
105 : return ge::GRAPH_SUCCESS;
106 : }
107 :
108 : ge::graphStatus EluGradTiling::RunTiling()
109 : {
110 : ElewiseBaseTiling elewiseBaseTiling(tilingContext);
111 : // 获取tiling计算所需的参数
112 : ge::graphStatus status = ge::GRAPH_FAILED;
113 : status = CalcInputDtype();
114 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Get input dtype failed"), return ge::GRAPH_FAILED);
115 : status = CalcOutputDtype();
116 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Get output dtype failed"), return ge::GRAPH_FAILED);
117 : status = CheckShape();
118 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "Check shape failed"), return ge::GRAPH_FAILED);
119 :
120 : auto tiling = tilingContext->GetTilingData<EleBaseTilingDataV2>();
121 : OP_CHECK_IF((tiling == nullptr), OP_LOGE(tilingContext->GetNodeName(), "Get EluGradTiling from GE context failed"),
122 : return ge::GRAPH_FAILED);
123 :
124 : if (this->outputDtype == ge::DT_FLOAT16) {
125 : dType = static_cast<uint64_t>(TPL_FP16);
126 : status = elewiseBaseTiling.DoTiling<EluGradDag<half>::OpDag>(*tiling);
127 : } else if (this->outputDtype == ge::DT_BF16) {
128 : dType = static_cast<uint64_t>(TPL_BF16);
129 : status = elewiseBaseTiling.DoTiling<EluGradDag<bfloat16_t>::OpDag>(*tiling);
130 : } else if (this->outputDtype == ge::DT_FLOAT) {
131 : dType = static_cast<uint64_t>(TPL_FP32);
132 : status = elewiseBaseTiling.DoTiling<EluGradDag<float>::OpDag>(*tiling);
133 : } else {
134 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "y",
135 : Ops::Base::ToString(static_cast<ge::DataType>(this->outputDtype)),
136 : "The dtype of y must be DT_FLOAT16, DT_BF16, or DT_FLOAT");
137 : return ge::GRAPH_FAILED;
138 : }
139 17 : OP_CHECK_IF(status == ge::GRAPH_FAILED,
140 : OP_LOGE(tilingContext->GetNodeName(), "ElewiseBaseTiling failed, output dtype: %s.",
141 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype).c_str()),
142 : return ge::GRAPH_FAILED);
143 :
144 : const uint64_t tilingKey = GET_TPL_TILING_KEY(tiling->scheMode, dType);
145 : OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : tilingKey=%ld.", tilingKey);
146 : tilingContext->SetTilingKey(tilingKey);
147 : tilingContext->SetBlockDim(tiling->blockNum);
148 : size_t usrWorkspaceSize = 0;
149 : size_t sysWorkspaceSize = ASCEND_WORKSPACE;
150 : size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);
151 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, currentWorkspace);
152 : currentWorkspace[0] = sysWorkspaceSize + usrWorkspaceSize;
153 : return ge::GRAPH_SUCCESS;
154 : }
155 :
156 : static ge::graphStatus Tiling4EluGrad(gert::TilingContext* tilingContextSelf)
157 : {
158 : OP_LOGD(tilingContextSelf->GetNodeName(), "Tiling4EluGrad rt2.0 is running.");
159 : auto compileInfo = tilingContextSelf->GetCompileInfo<EluGradCompileInfo>();
160 : OP_CHECK_NULL_WITH_CONTEXT(tilingContextSelf, compileInfo);
161 : EluGradTiling eluGradTiling(tilingContextSelf);
162 : return eluGradTiling.RunTiling();
163 : }
164 :
165 : static ge::graphStatus TilingPrepareForEluGrad(gert::TilingParseContext* context)
166 : {
167 : auto compileInfoPtr = context->GetCompiledInfo<EluGradCompileInfo>();
168 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfoPtr);
169 : fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
170 : OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
171 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
172 : compileInfoPtr->coreNum = ascendcPlatform.GetCoreNumAiv();
173 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, compileInfoPtr->ubSize);
174 : return ge::GRAPH_SUCCESS;
175 : }
176 :
177 : IMPL_OP_OPTILING(EluGrad).Tiling(Tiling4EluGrad).TilingParse<EluGradCompileInfo>(TilingPrepareForEluGrad);
178 : } // namespace optiling
|