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 leaky_relu_tiling_arch35.cpp
13 : * \brief
14 : */
15 : #include "leaky_relu_tiling_arch35.h"
16 :
17 : #include <graph/utils/type_utils.h>
18 :
19 : #include "tiling/platform/platform_ascendc.h"
20 :
21 : #include "register/op_def_registry.h"
22 : #include "register/tilingdata_base.h"
23 : #include "register/op_impl_registry.h"
24 : #include "atvoss/elewise/elewise_tiling.h"
25 :
26 : #include "log/log.h"
27 : #include "../../op_kernel/arch35/leaky_relu_dag.h"
28 : #include "../../op_kernel/arch35/leaky_relu_struct.h"
29 :
30 : #include <iostream>
31 :
32 : using namespace ge;
33 : using namespace LeakyReluOp;
34 : using namespace Ops::Base;
35 :
36 : namespace optiling {
37 : const size_t ASCEND_WORKSPACE = 16777216; // 16M
38 : const gert::Shape g_vec_1_shape = {1};
39 :
40 : static inline const gert::Shape& EnsureNotScalar(const gert::Shape& in_shape)
41 : {
42 : if (in_shape.IsScalar()) {
43 : return g_vec_1_shape;
44 : }
45 : return in_shape;
46 : }
47 :
48 : ge::graphStatus LeakyReluTiling::CalcInputDtype()
49 : {
50 : auto inputDesc = tilingContext->GetInputDesc(0);
51 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc);
52 : this->inputDtype = inputDesc->GetDataType();
53 : OP_CHECK_IF(
54 : this->inputDtype != ge::DT_FLOAT16 && this->inputDtype != ge::DT_BF16 && this->inputDtype != ge::DT_FLOAT,
55 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(
56 : tilingContext->GetNodeName(), "x",
57 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->inputDtype)),
58 : "The dtype of x must be DT_FLOAT16, DT_BF16, or DT_FLOAT"),
59 : return ge::GRAPH_FAILED);
60 : return ge::GRAPH_SUCCESS;
61 : }
62 :
63 : ge::graphStatus LeakyReluTiling::CheckShape()
64 : {
65 : auto inputStorageShape = tilingContext->GetInputShape(0);
66 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputStorageShape);
67 : const gert::Shape& inputYShape = EnsureNotScalar(inputStorageShape->GetStorageShape());
68 :
69 : auto outputStorageShape = tilingContext->GetOutputShape(0);
70 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputStorageShape);
71 : const gert::Shape& outputZShape = EnsureNotScalar(outputStorageShape->GetStorageShape());
72 :
73 : OP_CHECK_IF(inputYShape != outputZShape,
74 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
75 : tilingContext->GetNodeName(), "x, y",
76 : Ops::Base::ToString(inputYShape) + ", " + Ops::Base::ToString(outputZShape),
77 : "The shapes of x and y must be the same"),
78 : return ge::GRAPH_FAILED);
79 : return ge::GRAPH_SUCCESS;
80 : }
81 :
82 : ge::graphStatus LeakyReluTiling::CalcOutputDtype()
83 : {
84 : auto outputDesc = tilingContext->GetOutputDesc(0);
85 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputDesc);
86 : this->outputDtype = outputDesc->GetDataType();
87 : OP_CHECK_IF(this->outputDtype != this->inputDtype,
88 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(
89 : tilingContext->GetNodeName(), "x, y",
90 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->inputDtype)) + ", " +
91 : ge::TypeUtils::DataTypeToSerialString(static_cast<ge::DataType>(this->outputDtype)),
92 : "The dtypes of x and y must be the same"),
93 : return ge::GRAPH_FAILED);
94 : return ge::GRAPH_SUCCESS;
95 : }
96 :
97 : ge::graphStatus LeakyReluTiling::RunTiling()
98 : {
99 : ElewiseBaseTiling eleBaseTiling(tilingContext);
100 :
101 : OP_CHECK_IF(CalcInputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "get input dtype failed"),
102 : return ge::GRAPH_FAILED);
103 : OP_CHECK_IF(CalcOutputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "get output dtype failed"),
104 : return ge::GRAPH_FAILED);
105 : OP_CHECK_IF(CheckShape() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "check shape failed"),
106 : return ge::GRAPH_FAILED);
107 :
108 : auto attrs = tilingContext->GetAttrs();
109 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, attrs);
110 : const float* scaleValueAttr = attrs->GetAttrPointer<float>(0);
111 : float negativeSlope = scaleValueAttr != nullptr ? *scaleValueAttr : 0.0f;
112 :
113 : ge::graphStatus baseTilingResult = ge::GRAPH_FAILED;
114 : if (this->outputDtype == ge::DT_FLOAT16) {
115 : dType = static_cast<uint64_t>(TPL_FP16);
116 : baseTilingResult = eleBaseTiling.DoTiling24B<LeakyReluCastDag<half, float>::OpDag>();
117 : } else if (this->outputDtype == ge::DT_BF16) {
118 : dType = static_cast<uint64_t>(TPL_BF16);
119 : baseTilingResult = eleBaseTiling.DoTiling24B<LeakyReluCastDag<bfloat16_t, float>::OpDag>();
120 : } else if (this->outputDtype == ge::DT_FLOAT) {
121 : dType = static_cast<uint64_t>(TPL_FP32);
122 : baseTilingResult = eleBaseTiling.DoTiling24B<LeakyReluDag<float, float>::OpDag>();
123 : } else {
124 : OP_LOGE_FOR_INVALID_DTYPE(tilingContext->GetNodeName(), "y",
125 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype),
126 : "DT_FLOAT16, DT_BF16, DT_FLOAT");
127 : return ge::GRAPH_FAILED;
128 : }
129 0 : OP_CHECK_IF(baseTilingResult == ge::GRAPH_FAILED,
130 : OP_LOGE(tilingContext->GetNodeName(), "elewiseBaseTiling failed, output dtype: %s.",
131 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype).c_str()),
132 : return ge::GRAPH_FAILED);
133 :
134 : eleBaseTiling.SetScalar<float>(negativeSlope);
135 :
136 : size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);
137 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, currentWorkspace);
138 : currentWorkspace[0] = ASCEND_WORKSPACE;
139 :
140 : const uint64_t tilingKey = GET_TPL_TILING_KEY(schMode, dType);
141 : OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : tilingKey=%lu", tilingKey);
142 : tilingContext->SetTilingKey(tilingKey);
143 : tilingContext->SetBlockDim(eleBaseTiling.GetBlockDim());
144 : return ge::GRAPH_SUCCESS;
145 : }
146 :
147 : static ge::graphStatus TilingForLeakyRelu(gert::TilingContext* context)
148 : {
149 : OP_LOGD("LeakyReluTiling", "Enter TilingForLeakyRelu");
150 : if (context == nullptr) {
151 : OP_LOGE("LeakyReluTiling", "Tiling context is null");
152 : return ge::GRAPH_FAILED;
153 : }
154 :
155 : auto compileInfo = context->GetCompileInfo<LeakrReluCompileInfo>();
156 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo);
157 : LeakyReluTiling tiling(context);
158 : return tiling.RunTiling();
159 : }
160 :
161 : ge::graphStatus TilingPrepareForLeakyRelu(gert::TilingParseContext* context)
162 : {
163 : auto compileInfoPtr = context->GetCompiledInfo<LeakrReluCompileInfo>();
164 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfoPtr);
165 : fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
166 : OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
167 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
168 : compileInfoPtr->coreNum = ascendcPlatform.GetCoreNumAiv();
169 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, compileInfoPtr->ubSize);
170 : return ge::GRAPH_SUCCESS;
171 : }
172 :
173 : IMPL_OP_OPTILING(LeakyRelu).Tiling(TilingForLeakyRelu).TilingParse<LeakrReluCompileInfo>(TilingPrepareForLeakyRelu);
174 : } // namespace optiling
|