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_tiling_arch35.cpp
13 : * \brief
14 : */
15 : #include "elu_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 "../../op_kernel/arch35/elu_dag.h"
21 : #include "log/log.h"
22 : #include "platform/platform_info.h"
23 : #include "register/op_impl_registry.h"
24 : #include "register/tilingdata_base.h"
25 : #include <iostream>
26 :
27 : using namespace Ops::Base;
28 : using namespace ge;
29 : using namespace EluNs;
30 :
31 : namespace optiling {
32 : constexpr uint64_t ELU_TILING_KEY_ELEMENTWISE = 101;
33 : constexpr uint64_t ELU_WORKSPACE_RESERVE_BYTE = 16777216; // 16 * 1024 * 1024
34 : const int ATTR_ELU_ALPHA_POS = 0;
35 : const int ATTR_ELU_SCALE_POS = 1;
36 : const int ATTR_ELU_INPUT_SCALE_POS = 2;
37 : const gert::Shape g_vec_1_shape = {1};
38 :
39 : inline static const gert::Shape& EnsureNotScalar(const gert::Shape& in_shape)
40 : {
41 : if (in_shape.IsScalar()) {
42 : return g_vec_1_shape;
43 : }
44 : return in_shape;
45 : }
46 :
47 : ge::graphStatus TilingPrepareForElu(gert::TilingParseContext* context)
48 : {
49 : auto compileInfoPtr = context->GetCompiledInfo<EluCompileInfo>();
50 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfoPtr);
51 : fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
52 : OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
53 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
54 : compileInfoPtr->coreNum = ascendcPlatform.GetCoreNumAiv();
55 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, compileInfoPtr->ubSize);
56 : return ge::GRAPH_SUCCESS;
57 : }
58 :
59 : ge::graphStatus EluTiling::SetTilingData()
60 : {
61 : auto rawTilingData = tilingContext->GetRawTilingData();
62 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, rawTilingData);
63 : size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);
64 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, currentWorkspace);
65 : currentWorkspace[0] = ELU_WORKSPACE_RESERVE_BYTE;
66 : tilingContext->SetTilingKey(ELU_TILING_KEY_ELEMENTWISE);
67 : tilingContext->SetBlockDim(tiling->baseTiling.blockNum);
68 : return ge::GRAPH_SUCCESS;
69 : }
70 :
71 : ge::graphStatus EluTiling::CalcOutputDtype()
72 : {
73 : auto inputDesc = tilingContext->GetInputDesc(0);
74 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc);
75 : ge::DataType inputDtype = inputDesc->GetDataType();
76 :
77 : auto outputDesc = tilingContext->GetOutputDesc(0);
78 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputDesc);
79 : this->outputDtype = outputDesc->GetDataType();
80 :
81 : OP_CHECK_IF(inputDtype != this->outputDtype,
82 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(
83 : tilingContext->GetNodeName(), "x, y",
84 : Ops::Base::ToString(inputDtype) + ", " + Ops::Base::ToString(this->outputDtype),
85 : "The dtypes of x and y must be the same"),
86 : return ge::GRAPH_FAILED);
87 : return ge::GRAPH_SUCCESS;
88 : }
89 :
90 : ge::graphStatus EluTiling::CheckShape()
91 : {
92 : auto inputStorageShape = tilingContext->GetInputShape(0);
93 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputStorageShape);
94 : const gert::Shape& inputShape = EnsureNotScalar(inputStorageShape->GetStorageShape());
95 :
96 : auto outputStorageShape = tilingContext->GetOutputShape(0);
97 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputStorageShape);
98 : const gert::Shape& outputShape = EnsureNotScalar(outputStorageShape->GetStorageShape());
99 :
100 : OP_CHECK_IF(inputShape != outputShape,
101 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(tilingContext->GetNodeName(), "x, y",
102 : ToString(inputShape) + ", " + ToString(outputShape),
103 : "The shapes of x and y must be the same"),
104 : return ge::GRAPH_FAILED);
105 : return ge::GRAPH_SUCCESS;
106 : }
107 :
108 : ge::graphStatus EluTiling::RunTiling()
109 : {
110 : ElewiseBaseTiling elewiseBaseTiling(tilingContext);
111 : OP_CHECK_IF(CalcOutputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "get output dtype failed"),
112 : return ge::GRAPH_FAILED);
113 : OP_CHECK_IF(CheckShape() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "check shape failed"),
114 : return ge::GRAPH_FAILED);
115 :
116 : tiling = tilingContext->GetTilingData<EluTilingData>();
117 : OP_CHECK_IF((tiling == nullptr), OP_LOGE(tilingContext, "Get EleBaseTilingData from context failed"),
118 : return ge::GRAPH_FAILED);
119 :
120 : ge::graphStatus res = ge::GRAPH_FAILED;
121 : if (this->outputDtype == ge::DT_FLOAT16) {
122 : res = elewiseBaseTiling.DoTiling<EluOp::EluDag<half, float>::OpDag>(tiling->baseTiling);
123 : } else if (this->outputDtype == ge::DT_FLOAT) {
124 : res = elewiseBaseTiling.DoTiling<EluOp::EluDag<float, float>::OpDag>(tiling->baseTiling);
125 : } else if (this->outputDtype == ge::DT_BF16) {
126 : res = elewiseBaseTiling.DoTiling<EluOp::EluDag<bfloat16_t, float>::OpDag>(tiling->baseTiling);
127 : } else {
128 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "x",
129 : ToString(static_cast<ge::DataType>(this->outputDtype)),
130 : "The dtype of x must be DT_FLOAT16, DT_FLOAT, or DT_BF16");
131 : return ge::GRAPH_FAILED;
132 : }
133 :
134 0 : OP_CHECK_IF(res != ge::GRAPH_SUCCESS,
135 : OP_LOGE(tilingContext->GetNodeName(), "DoTiling failed, output dtype: %s.",
136 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype).c_str()),
137 : return ge::GRAPH_FAILED);
138 :
139 : auto runtimeAttrs = tilingContext->GetAttrs();
140 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, runtimeAttrs);
141 : tiling->alpha = 1;
142 : tiling->scale = 1;
143 : tiling->inputScale = 1;
144 : const float* alphaPtr = runtimeAttrs->GetAttrPointer<float>(ATTR_ELU_ALPHA_POS);
145 : if (alphaPtr != nullptr) {
146 : tiling->alpha = (*alphaPtr);
147 : }
148 : const float* scalePtr = runtimeAttrs->GetAttrPointer<float>(ATTR_ELU_SCALE_POS);
149 : if (scalePtr != nullptr) {
150 : tiling->scale = (*scalePtr);
151 : }
152 : const float* inputScalePtr = runtimeAttrs->GetAttrPointer<float>(ATTR_ELU_INPUT_SCALE_POS);
153 : if (inputScalePtr != nullptr) {
154 : tiling->inputScale = (*inputScalePtr);
155 : }
156 :
157 : ge::graphStatus result = SetTilingData();
158 : return result;
159 : }
160 :
161 : static ge::graphStatus TilingForElu(gert::TilingContext* context)
162 : {
163 : OP_LOGD("EluTiling", "Enter TilingForElu");
164 : OP_CHECK_IF(context == nullptr, OP_LOGE(context, "Tiling context is null"), return ge::GRAPH_FAILED);
165 :
166 : auto compileInfo = context->GetCompileInfo<EluCompileInfo>();
167 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo);
168 : EluTiling eluTiling(context);
169 : return eluTiling.RunTiling();
170 : }
171 :
172 : IMPL_OP_OPTILING(Elu).Tiling(TilingForElu).TilingParse<EluCompileInfo>(TilingPrepareForElu);
173 : } // namespace optiling
|