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 : #include <iostream>
12 : #include <graph/utils/type_utils.h>
13 : #include "log/log.h"
14 : #include "platform/platform_ascendc.h"
15 : #include "register/op_def_registry.h"
16 : #include "register/tilingdata_base.h"
17 : #include "activation/fast_gelu_grad/op_kernel/arch35/fast_gelu_grad_dag.h"
18 : #include "activation/fast_gelu_grad/op_kernel/arch35/fast_gelu_grad_struct.h"
19 : #include "atvoss/elewise/elewise_tiling.h"
20 : #include "atvoss/broadcast/broadcast_tiling.h"
21 : #include "fast_gelu_grad_tiling_arch35.h"
22 :
23 : using namespace FastGeluGradOp;
24 :
25 : namespace optiling {
26 : const int64_t SYSWORKSPACE = 16777216; // 16 * 1024 * 1024
27 :
28 : ge::graphStatus FastGeluGradTiling::CalcInputDtype()
29 : {
30 : auto inputDesc = tilingContext->GetInputDesc(0);
31 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc);
32 : this->inputDtype = inputDesc->GetDataType();
33 : OP_CHECK_IF(
34 : this->inputDtype != ge::DT_FLOAT16 && this->inputDtype != ge::DT_BF16 && this->inputDtype != ge::DT_FLOAT,
35 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(
36 : tilingContext->GetNodeName(), "x",
37 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->inputDtype)),
38 : "The dtype of x must be DT_FLOAT16, DT_BF16, or DT_FLOAT"),
39 : return ge::GRAPH_FAILED);
40 :
41 : auto inputDesc1 = tilingContext->GetInputDesc(1);
42 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc1);
43 : this->inputDtype1 = inputDesc1->GetDataType();
44 : OP_CHECK_IF(
45 : this->inputDtype1 != ge::DT_FLOAT16 && this->inputDtype1 != ge::DT_BF16 && this->inputDtype1 != ge::DT_FLOAT,
46 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(
47 : tilingContext->GetNodeName(), "dy",
48 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->inputDtype1)),
49 : "The dtype of dy must be DT_FLOAT16, DT_BF16, or DT_FLOAT"),
50 : return ge::GRAPH_FAILED);
51 :
52 : OP_CHECK_IF(this->inputDtype1 != this->inputDtype,
53 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(
54 : tilingContext->GetNodeName(), "x, dy",
55 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->inputDtype)) + ", " +
56 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->inputDtype1)),
57 : "The dtypes of x and dy must be the same"),
58 : return ge::GRAPH_FAILED);
59 :
60 : return ge::GRAPH_SUCCESS;
61 : }
62 :
63 : ge::graphStatus FastGeluGradTiling::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(
71 : tilingContext->GetNodeName(), "z",
72 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->outputDtype)),
73 : "The dtype of z must be DT_FLOAT16, DT_BF16, or DT_FLOAT"),
74 : return ge::GRAPH_FAILED);
75 : OP_CHECK_IF(this->outputDtype != this->inputDtype,
76 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(
77 : tilingContext->GetNodeName(), "x, z",
78 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->inputDtype)) + ", " +
79 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->outputDtype)),
80 : "The dtypes of x and z must be the same"),
81 : return ge::GRAPH_FAILED);
82 : return ge::GRAPH_SUCCESS;
83 : }
84 :
85 : ge::graphStatus FastGeluGradTiling::CheckShape()
86 : {
87 : auto selfStorageShape = tilingContext->GetInputShape(0);
88 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, selfStorageShape);
89 : const gert::Shape& inputXShape = Ops::Base::EnsureNotScalar(selfStorageShape->GetStorageShape());
90 :
91 : auto dyStorageShape = tilingContext->GetInputShape(1);
92 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, dyStorageShape);
93 : const gert::Shape& inputDyShape = Ops::Base::EnsureNotScalar(dyStorageShape->GetStorageShape());
94 :
95 : auto outStorageShape = tilingContext->GetOutputShape(0);
96 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outStorageShape);
97 : const gert::Shape& outputShape = Ops::Base::EnsureNotScalar(outStorageShape->GetStorageShape());
98 :
99 : OP_CHECK_IF(inputXShape != inputDyShape,
100 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
101 : tilingContext->GetNodeName(), "x, dy",
102 : Ops::Base::ToString(inputXShape) + ", " + Ops::Base::ToString(inputDyShape),
103 : "The shapes of x and dy must be the same"),
104 : return ge::GRAPH_FAILED);
105 : OP_CHECK_IF(inputXShape != outputShape,
106 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
107 : tilingContext->GetNodeName(), "x, z",
108 : Ops::Base::ToString(inputXShape) + ", " + Ops::Base::ToString(outputShape),
109 : "The shapes of x and z must be the same"),
110 : return ge::GRAPH_FAILED);
111 : return ge::GRAPH_SUCCESS;
112 : }
113 :
114 : std::string FastGeluGradTiling::DataTypeToSerialString(const ge::DataType type) const
115 : {
116 : const auto it = DATATYPE_TO_STRING_MAP.find(type);
117 : if (it != DATATYPE_TO_STRING_MAP.end()) {
118 : return it->second;
119 : } else {
120 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON("FastGeluGrad", "x",
121 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(type)),
122 : "The dtype of x must be DT_FLOAT16, DT_BF16, or DT_FLOAT");
123 : return "UNDEFINED";
124 : }
125 : }
126 :
127 : ge::graphStatus FastGeluGradTiling::RunTiling()
128 : {
129 : ElewiseBaseTiling elewiseBaseTiling(tilingContext);
130 : // 获取tiling计算所需的参数
131 : ge::graphStatus status = ge::GRAPH_FAILED;
132 : status = CalcInputDtype();
133 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_CHECK_NULL_WITH_CONTEXT(tilingContext, "get input dtype failed"),
134 : return ge::GRAPH_FAILED);
135 : status = CalcOutputDtype();
136 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_CHECK_NULL_WITH_CONTEXT(tilingContext, "get output dtype failed"),
137 : return ge::GRAPH_FAILED);
138 : status = CheckShape();
139 : OP_CHECK_IF(status == ge::GRAPH_FAILED, OP_CHECK_NULL_WITH_CONTEXT(tilingContext, "check shape failed"),
140 : return ge::GRAPH_FAILED);
141 : auto tiling = tilingContext->GetTilingData<EleBaseTilingDataV2>();
142 : OP_CHECK_IF((tiling == nullptr),
143 : OP_LOGE(tilingContext->GetNodeName(), "Get FastGeluGradTiling from GE context failed"),
144 : return ge::GRAPH_FAILED);
145 : if (this->outputDtype == ge::DT_FLOAT16) {
146 : dType = TPL_FP16;
147 : status = elewiseBaseTiling.DoTiling<FastGeluGradDag::FastGeluGradNeedCast<half>::OpDag>(*tiling);
148 : } else if (this->outputDtype == ge::DT_BF16) {
149 : dType = TPL_BF16;
150 : status = elewiseBaseTiling.DoTiling<FastGeluGradDag::FastGeluGradNeedCast<bfloat16_t>::OpDag>(*tiling);
151 : } else if (this->outputDtype == ge::DT_FLOAT) {
152 : dType = TPL_FP32;
153 : status = elewiseBaseTiling.DoTiling<FastGeluGradDag::FastGeluGradNoCast<float>::OpDag>(*tiling);
154 : } else {
155 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(
156 : tilingContext->GetNodeName(), "z",
157 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->outputDtype)),
158 : "The dtype of z must be DT_FLOAT16, DT_BF16, or DT_FLOAT");
159 : return ge::GRAPH_FAILED;
160 : }
161 7 : OP_CHECK_IF(status == ge::GRAPH_FAILED,
162 : OP_LOGE(tilingContext->GetNodeName(), "elewiseBaseTiling failed, output dtype: %s.",
163 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->outputDtype)).c_str()),
164 : return ge::GRAPH_FAILED);
165 : const uint64_t tilingKey = GET_TPL_TILING_KEY(tiling->scheMode, dType);
166 : OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : tilingKey=%ld.", tilingKey);
167 : tilingContext->SetTilingKey(tilingKey);
168 : tilingContext->SetBlockDim(tiling->blockNum);
169 : size_t usr_workspaceSize = 0;
170 : size_t sys_workspaceSize = SYSWORKSPACE;
171 : size_t* current_workspace = tilingContext->GetWorkspaceSizes(1);
172 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, current_workspace);
173 : current_workspace[0] = sys_workspaceSize + usr_workspaceSize;
174 : return ge::GRAPH_SUCCESS;
175 : }
176 :
177 : static ge::graphStatus TilingFuncFastGeluGrad(gert::TilingContext* tilingContext)
178 : {
179 : auto compileInfo = tilingContext->GetCompileInfo<ElewiseCompileInfo>();
180 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, compileInfo);
181 :
182 : OP_LOGD(tilingContext->GetNodeName(), "START FastGeluGrad AscendC Tiling \n");
183 : FastGeluGradTiling FastGeluGradTiling(tilingContext);
184 : return FastGeluGradTiling.RunTiling();
185 : }
186 :
187 : ge::graphStatus TilingPrepareForFastGeluGrad([[maybe_unused]] gert::TilingParseContext* context)
188 : {
189 : return ge::GRAPH_SUCCESS;
190 : }
191 :
192 : IMPL_OP_OPTILING(FastGeluGrad)
193 : .Tiling(TilingFuncFastGeluGrad)
194 : .TilingParse<ElewiseCompileInfo>(TilingPrepareForFastGeluGrad);
195 : } // namespace optiling
|