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 swish_tiling_arch35.cpp
13 : * \brief
14 : */
15 :
16 : #include "swish_tiling_arch35.h"
17 : #include "log/log.h"
18 : #include "platform/platform_info.h"
19 : #include "activation/swish/op_kernel/arch35/swish_dag.h"
20 : #include "activation/swish/op_kernel/arch35/swish_struct.h"
21 : #include "register/op_impl_registry.h"
22 : #include "atvoss/elewise/elewise_tiling.h"
23 : #include "atvoss/broadcast/broadcast_tiling.h"
24 : #include "util/math_util.h"
25 :
26 : using namespace AscendC;
27 : using namespace ge;
28 : using namespace SwishOp;
29 : using namespace Ops::Base;
30 :
31 : namespace optiling {
32 : static constexpr uint64_t OP_KEY_INVALID = 0;
33 : static constexpr uint64_t OP_KEY_1 = 1;
34 : static constexpr uint64_t OP_KEY_2 = 2;
35 : static constexpr uint64_t OP_KEY_3 = 3;
36 : static constexpr uint64_t INDEX_0 = 0;
37 : static constexpr uint64_t WORKSPACE_SIZE = 32;
38 : const int64_t ASCEND_WORKSPACE = 16777216; // 16 * 1024 * 1024
39 : static constexpr float NEG_ONE = -1.0f;
40 : static constexpr float ZERO = 0.0;
41 :
42 : ge::graphStatus SwishTiling::CalcInputDtype()
43 : {
44 : auto inputDesc = tilingContext->GetInputDesc(0);
45 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputDesc);
46 : this->inputDtype = inputDesc->GetDataType();
47 : OP_CHECK_IF(
48 : this->inputDtype != ge::DT_FLOAT16 && this->inputDtype != ge::DT_BF16 && this->inputDtype != ge::DT_FLOAT,
49 : OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(tilingContext->GetNodeName(), "x",
50 : ge::TypeUtils::DataTypeToSerialString(this->inputDtype),
51 : "The dtype of x must be DT_FLOAT16, DT_BF16 or DT_FLOAT"),
52 : return ge::GRAPH_FAILED);
53 : return ge::GRAPH_SUCCESS;
54 : }
55 :
56 : ge::graphStatus SwishTiling::CalcOutputDtype()
57 : {
58 : auto outputDesc = tilingContext->GetOutputDesc(0);
59 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputDesc);
60 : this->outputDtype = outputDesc->GetDataType();
61 : OP_CHECK_IF(
62 : this->outputDtype != ge::DT_FLOAT16 && this->outputDtype != ge::DT_BF16 && this->outputDtype != ge::DT_FLOAT,
63 : OP_LOGE_FOR_INVALID_DTYPE(tilingContext->GetNodeName(), "y",
64 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype),
65 : "DT_FLOAT16, DT_BF16, DT_FLOAT"),
66 : return ge::GRAPH_FAILED);
67 : OP_CHECK_IF(this->outputDtype != this->inputDtype,
68 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(tilingContext->GetNodeName(), "y, x",
69 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype) + ", " +
70 : ge::TypeUtils::DataTypeToSerialString(this->inputDtype),
71 : "The dtypes of y and x must be the same"),
72 : return ge::GRAPH_FAILED);
73 : return ge::GRAPH_SUCCESS;
74 : }
75 :
76 : ge::graphStatus SwishTiling::CheckShape()
77 : {
78 : auto inputStorageShape = tilingContext->GetInputShape(0);
79 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, inputStorageShape);
80 : const gert::Shape& inputYShape = EnsureNotScalar(inputStorageShape->GetStorageShape());
81 :
82 : auto outputStorageShape = tilingContext->GetOutputShape(0);
83 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, outputStorageShape);
84 : const gert::Shape& outputZShape = EnsureNotScalar(outputStorageShape->GetStorageShape());
85 :
86 : OP_CHECK_IF(inputYShape != outputZShape,
87 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
88 : tilingContext->GetNodeName(), "x, y",
89 : Ops::Base::ToString(inputYShape) + ", " + Ops::Base::ToString(outputZShape),
90 : "The shapes of x and y must be the same"),
91 : return ge::GRAPH_FAILED);
92 : return ge::GRAPH_SUCCESS;
93 : }
94 :
95 : ge::graphStatus SwishTiling::SetAttr()
96 : {
97 : auto attrs = tilingContext->GetAttrs();
98 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, attrs);
99 : const float* scaleValueAttr = attrs->GetAttrPointer<float>(SwishDag::PLACEHOLDER_INDEX_0);
100 : float scale = scaleValueAttr == nullptr ? 1.0f : *scaleValueAttr;
101 :
102 : attrScale = scale;
103 :
104 : if (IsFloatEqual(scale, NEG_ONE)) {
105 : attrWork = static_cast<uint64_t>(TPL_SCALE_NEG_ONE);
106 : } else if (IsFloatEqual(scale, ZERO)) {
107 : attrWork = static_cast<uint64_t>(TPL_SCALE_ZERO);
108 : } else {
109 : attrWork = static_cast<uint64_t>(TPL_SCALE_OTHER);
110 : }
111 :
112 : return ge::GRAPH_SUCCESS;
113 : }
114 :
115 : ge::graphStatus SwishTiling::RunTiling()
116 : {
117 : ElewiseBaseTiling elewiseBaseTiling(tilingContext);
118 :
119 : OP_CHECK_IF(CalcInputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "get input dtype failed"),
120 : return ge::GRAPH_FAILED);
121 : OP_CHECK_IF(CalcOutputDtype() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "get output dtype failed"),
122 : return ge::GRAPH_FAILED);
123 : OP_CHECK_IF(CheckShape() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "check shape failed"),
124 : return ge::GRAPH_FAILED);
125 : OP_CHECK_IF(SetAttr() == ge::GRAPH_FAILED, OP_LOGE(tilingContext, "set Attr failed"), return ge::GRAPH_FAILED);
126 :
127 : ge::graphStatus baseTilingResult = ge::GRAPH_FAILED;
128 : if (this->outputDtype == ge::DT_FLOAT16) {
129 : if (attrWork == static_cast<uint64_t>(TPL_SCALE_NEG_ONE)) {
130 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishNegOne<half>::OpDag>();
131 : } else if (attrWork == static_cast<uint64_t>(TPL_SCALE_ZERO)) {
132 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishZero<half>::OpDag>();
133 : } else {
134 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishOther<half>::OpDag>();
135 : }
136 : } else if (this->outputDtype == ge::DT_BF16) {
137 : if (attrWork == static_cast<uint64_t>(TPL_SCALE_NEG_ONE)) {
138 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishNegOne<bfloat16_t>::OpDag>();
139 : } else if (attrWork == static_cast<uint64_t>(TPL_SCALE_ZERO)) {
140 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishZero<bfloat16_t>::OpDag>();
141 : } else {
142 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishOther<bfloat16_t>::OpDag>();
143 : }
144 : } else if (this->outputDtype == ge::DT_FLOAT) {
145 : if (attrWork == static_cast<uint64_t>(TPL_SCALE_NEG_ONE)) {
146 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishNegOne<float>::OpDag>();
147 : } else if (attrWork == static_cast<uint64_t>(TPL_SCALE_ZERO)) {
148 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishZero<float>::OpDag>();
149 : } else {
150 : baseTilingResult = elewiseBaseTiling.DoTiling32B<SwishDag::SwishOther<float>::OpDag>();
151 : }
152 : } else {
153 : OP_LOGE_FOR_INVALID_DTYPE(tilingContext->GetNodeName(), "y",
154 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype),
155 : "DT_FLOAT16, DT_BF16, DT_FLOAT");
156 : return ge::GRAPH_FAILED;
157 : }
158 1 : OP_CHECK_IF(baseTilingResult == ge::GRAPH_FAILED,
159 : OP_LOGE(tilingContext->GetNodeName(), "elewiseBaseTiling failed, output dtype: %s.",
160 : ge::TypeUtils::DataTypeToSerialString(this->outputDtype).c_str()),
161 : return ge::GRAPH_FAILED);
162 : elewiseBaseTiling.SetScalar<float>(attrScale);
163 : size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);
164 : OP_CHECK_NULL_WITH_CONTEXT(tilingContext, currentWorkspace);
165 : currentWorkspace[0] = ASCEND_WORKSPACE;
166 : const uint64_t tilingKey = GET_TPL_TILING_KEY(schMode, attrWork);
167 : OP_LOGD(tilingContext->GetNodeName(), "[TilingData] : tilingKey=%lu", tilingKey);
168 : tilingContext->SetTilingKey(tilingKey);
169 : tilingContext->SetBlockDim(elewiseBaseTiling.GetBlockDim());
170 :
171 : return ge::GRAPH_SUCCESS;
172 : }
173 :
174 : ge::graphStatus TilingForSwish(gert::TilingContext* tilingContextGen)
175 : {
176 : OP_LOGD(tilingContextGen->GetNodeName(), "TilingForSwish rt2.0 is running");
177 : auto compileInfo = tilingContextGen->GetCompileInfo<SwishCompileInfo>();
178 : OP_CHECK_NULL_WITH_CONTEXT(tilingContextGen, compileInfo);
179 : SwishTiling baseOpTiling(tilingContextGen);
180 : return baseOpTiling.RunTiling();
181 : }
182 :
183 : ge::graphStatus TilingPrepareForSwish(gert::TilingParseContext* context)
184 : {
185 : auto compileInfoPtr = context->GetCompiledInfo<SwishCompileInfo>();
186 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfoPtr);
187 : fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
188 : OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
189 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
190 : compileInfoPtr->coreNum = ascendcPlatform.GetCoreNumAiv();
191 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, compileInfoPtr->ubSize);
192 : return ge::GRAPH_SUCCESS;
193 : }
194 :
195 : IMPL_OP_OPTILING(Swish).Tiling(TilingForSwish).TilingParse<SwishCompileInfo>(TilingPrepareForSwish);
196 :
197 : } // namespace optiling
|