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 silu_grad_tiling.cpp
13 : * \brief
14 : */
15 :
16 : #include "silu_grad_tiling.h"
17 : #include "platform/platform_info.h"
18 : #include "log/log.h"
19 : #include "atvoss/broadcast/broadcast_tiling.h"
20 :
21 : using namespace ge;
22 :
23 : namespace optiling {
24 :
25 : static constexpr uint64_t OP_KEY_INVALID = 0;
26 : static constexpr uint64_t OP_KEY_1 = 1;
27 : static constexpr uint64_t OP_KEY_2 = 2;
28 : static constexpr uint64_t OP_KEY_3 = 3;
29 : static constexpr uint64_t OP_KEY_4 = 4;
30 : static constexpr uint64_t OP_KEY_5 = 5;
31 : static constexpr uint64_t OP_KEY_6 = 6;
32 : static constexpr uint64_t OP_KEY_7 = 7;
33 : static constexpr uint64_t OP_KEY_8 = 8;
34 : static constexpr uint64_t OP_KEY_9 = 9;
35 : static constexpr uint64_t INDEX_0 = 0;
36 : static constexpr uint64_t INDEX_1 = 1;
37 : static constexpr uint64_t INDEX_2 = 2;
38 : static constexpr uint64_t WORKSPACE_SIZE = 32;
39 :
40 : ge::graphStatus SiluGradTiling::GetPlatformInfo()
41 : {
42 : auto platformInfo = context_->GetPlatformInfo();
43 : if (platformInfo == nullptr) {
44 : auto compileInfoPtr = reinterpret_cast<const SiluGradCompileInfo*>(context_->GetCompileInfo());
45 : OP_CHECK_IF(compileInfoPtr == nullptr, OP_LOGE(context_, "compile info is null"), return ge::GRAPH_FAILED);
46 : coreNum = compileInfoPtr->coreNum;
47 : ubSize = compileInfoPtr->ubSize;
48 : } else {
49 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfo);
50 : coreNum = ascendcPlatform.GetCoreNumAiv();
51 : uint64_t ubSizePlatForm;
52 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSizePlatForm);
53 : ubSize = ubSizePlatForm;
54 : }
55 : return ge::GRAPH_SUCCESS;
56 : }
57 :
58 : uint64_t SiluGradTiling::GetOpKey(ge::DataType dyDtype, ge::DataType xDtype, ge::DataType dxDtype) const
59 : {
60 : bool opKey1Flag = dyDtype == DT_FLOAT16 && xDtype == DT_FLOAT16 && dxDtype == DT_FLOAT16;
61 : if (opKey1Flag) {
62 : return OP_KEY_1;
63 : }
64 : bool opKey2Flag = dyDtype == DT_BF16 && xDtype == DT_BF16 && dxDtype == DT_BF16;
65 : if (opKey2Flag) {
66 : return OP_KEY_2;
67 : }
68 : bool opKey3Flag = dyDtype == DT_FLOAT && xDtype == DT_FLOAT && dxDtype == DT_FLOAT;
69 : if (opKey3Flag) {
70 : return OP_KEY_3;
71 : }
72 : bool opKey4Flag = dyDtype == DT_FLOAT16 && xDtype == DT_BF16 && dxDtype == DT_FLOAT;
73 : if (opKey4Flag) {
74 : return OP_KEY_4;
75 : }
76 : bool opKey5Flag = dyDtype == DT_FLOAT16 && xDtype == DT_FLOAT && dxDtype == DT_FLOAT;
77 : if (opKey5Flag) {
78 : return OP_KEY_5;
79 : }
80 : bool opKey6Flag = dyDtype == DT_BF16 && xDtype == DT_FLOAT16 && dxDtype == DT_FLOAT;
81 : if (opKey6Flag) {
82 : return OP_KEY_6;
83 : }
84 : bool opKey7Flag = dyDtype == DT_BF16 && xDtype == DT_FLOAT && dxDtype == DT_FLOAT;
85 : if (opKey7Flag) {
86 : return OP_KEY_7;
87 : }
88 : bool opKey8Flag = dyDtype == DT_FLOAT && xDtype == DT_FLOAT16 && dxDtype == DT_FLOAT;
89 : if (opKey8Flag) {
90 : return OP_KEY_8;
91 : }
92 : bool opKey9Flag = dyDtype == DT_FLOAT && xDtype == DT_BF16 && dxDtype == DT_FLOAT;
93 : if (opKey9Flag) {
94 : return OP_KEY_9;
95 : }
96 :
97 : return OP_KEY_INVALID;
98 : }
99 :
100 : uint64_t SiluGradTiling::GenerateTilingKey(uint64_t innerKey) const
101 : {
102 : return opKey * Ops::Base::BROADCAST_OP_KEY_OFFSET + innerKey;
103 : }
104 :
105 : std::map<uint64_t, Ops::Base::BroadcastComputeParams> SiluGradTiling::GetComputeMap(uint64_t opKeyParam) const
106 : {
107 : Ops::Base::BroadcastComputeParams computeParams0;
108 : switch (opKeyParam) {
109 : case OP_KEY_1:
110 : case OP_KEY_2:
111 : computeParams0.maxDtypeBits = static_cast<int64_t>(Ops::Base::BROADCAST_BITS_SIZE::BITS32_SIZE);
112 : computeParams0.minDtypeBits = static_cast<int64_t>(Ops::Base::BROADCAST_BITS_SIZE::BITS16_SIZE);
113 : computeParams0.extraSize = {0, 0};
114 : computeParams0.bufferDivisor = {192, 192};
115 : return {{1, computeParams0}};
116 : case OP_KEY_3:
117 : computeParams0.maxDtypeBits = static_cast<int64_t>(Ops::Base::BROADCAST_BITS_SIZE::BITS32_SIZE);
118 : computeParams0.minDtypeBits = static_cast<int64_t>(Ops::Base::BROADCAST_BITS_SIZE::BITS32_SIZE);
119 : computeParams0.extraSize = {0, 0};
120 : computeParams0.bufferDivisor = {192, 192};
121 : return {{1, computeParams0}};
122 : case OP_KEY_4:
123 : case OP_KEY_6:
124 : computeParams0.maxDtypeBits = static_cast<int64_t>(Ops::Base::BROADCAST_BITS_SIZE::BITS32_SIZE);
125 : computeParams0.minDtypeBits = static_cast<int64_t>(Ops::Base::BROADCAST_BITS_SIZE::BITS16_SIZE);
126 : computeParams0.extraSize = {0, 0};
127 : computeParams0.bufferDivisor = {128, 128};
128 : return {{1, computeParams0}};
129 : case OP_KEY_5:
130 : case OP_KEY_7:
131 : case OP_KEY_8:
132 : case OP_KEY_9:
133 : computeParams0.maxDtypeBits = static_cast<int64_t>(Ops::Base::BROADCAST_BITS_SIZE::BITS32_SIZE);
134 : computeParams0.minDtypeBits = static_cast<int64_t>(Ops::Base::BROADCAST_BITS_SIZE::BITS16_SIZE);
135 : computeParams0.extraSize = {0, 0};
136 : computeParams0.bufferDivisor = {160, 160};
137 : return {{1, computeParams0}};
138 : default:
139 : return {};
140 : }
141 : }
142 :
143 : ge::graphStatus SiluGradTiling::GetShapeAttrsInfo()
144 : {
145 : auto dy = context_->GetInputDesc(INDEX_0);
146 : OP_CHECK_NULL_WITH_CONTEXT(context_, dy);
147 : auto dyDtype = dy->GetDataType();
148 : auto x = context_->GetInputDesc(INDEX_1);
149 : OP_CHECK_NULL_WITH_CONTEXT(context_, x);
150 : auto xDtype = x->GetDataType();
151 : auto dx = context_->GetOutputDesc(INDEX_0);
152 : OP_CHECK_NULL_WITH_CONTEXT(context_, dx);
153 : auto dxDtype = dx->GetDataType();
154 :
155 : opKey = GetOpKey(dyDtype, xDtype, dxDtype);
156 : OP_CHECK_IF((opKey == OP_KEY_INVALID),
157 : OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(context_->GetNodeName(), "opKey", std::to_string(opKey),
158 : "The value of opKey cannot be 0"),
159 : return ge::GRAPH_FAILED);
160 : return ge::GRAPH_SUCCESS;
161 : }
162 :
163 : bool SiluGradTiling::IsCapable() { return true; }
164 :
165 : ge::graphStatus SiluGradTiling::DoOpTiling()
166 : {
167 : Ops::Base::BroadcastTilingParams broadcastTilingParams;
168 : for (uint64_t i = 0; i < context_->GetComputeNodeInputNum(); i++) {
169 : auto shape = context_->GetInputShape(i);
170 : OP_CHECK_NULL_WITH_CONTEXT(context_, shape);
171 : broadcastTilingParams.inShape.push_back(Ops::Base::EnsureNotScalar(shape->GetStorageShape()));
172 : }
173 :
174 : auto outShape = context_->GetOutputShape(0);
175 : OP_CHECK_NULL_WITH_CONTEXT(context_, outShape);
176 : broadcastTilingParams.outShape = Ops::Base::EnsureNotScalar(outShape->GetStorageShape());
177 : broadcastTilingParams.computeMap = GetComputeMap(opKey);
178 : broadcastTilingParams.coreNum = coreNum;
179 : broadcastTilingParams.ubSize = ubSize;
180 :
181 : Ops::Base::BroadcastTilingData broadcastTilingData;
182 : ge::graphStatus status = BroadcastTiling(broadcastTilingParams, broadcastTilingData);
183 : if (status != ge::GRAPH_SUCCESS) {
184 2 : OP_LOGE(context_->GetNodeName(), "broadcast tiling failed, opKey: %lu.", opKey);
185 : return ge::GRAPH_FAILED;
186 : }
187 :
188 : tilingKey_ = GenerateTilingKey(broadcastTilingData.innerKey);
189 : blockNum = broadcastTilingData.blockNum;
190 : tilingData.set_blockFormer(broadcastTilingData.blockFormer);
191 : tilingData.set_ubFormer(broadcastTilingData.ubFormer);
192 : tilingData.set_ubOuter(broadcastTilingData.ubOuter);
193 : tilingData.set_ubTail(broadcastTilingData.ubTail);
194 : tilingData.set_blockTail(broadcastTilingData.blockTail);
195 : tilingData.set_shapeLen(broadcastTilingData.shapeLen);
196 : tilingData.set_ubSplitAxis(broadcastTilingData.ubSplitAxis);
197 : tilingData.set_dimProductBeforeUbInner(broadcastTilingData.dimProductBeforeUbInner);
198 : tilingData.set_elemNum(broadcastTilingData.elemNum);
199 :
200 : std::copy(broadcastTilingData.dims[INDEX_0].begin(), broadcastTilingData.dims[INDEX_0].end(), input0Dims);
201 : tilingData.set_input0Dims(input0Dims);
202 : std::copy(broadcastTilingData.dims[INDEX_1].begin(), broadcastTilingData.dims[INDEX_1].end(), input1Dims);
203 : tilingData.set_input1Dims(input1Dims);
204 : std::copy(broadcastTilingData.dims[INDEX_2].begin(), broadcastTilingData.dims[INDEX_2].end(), outputDims);
205 : tilingData.set_outputDims(outputDims);
206 : std::copy(broadcastTilingData.strides[INDEX_0].begin(), broadcastTilingData.strides[INDEX_0].end(), input0Strides);
207 : tilingData.set_input0Strides(input0Strides);
208 : std::copy(broadcastTilingData.strides[INDEX_1].begin(), broadcastTilingData.strides[INDEX_1].end(), input1Strides);
209 : tilingData.set_input1Strides(input1Strides);
210 : std::copy(broadcastTilingData.strides[INDEX_2].begin(), broadcastTilingData.strides[INDEX_2].end(), outputStrides);
211 : tilingData.set_outputStrides(outputStrides);
212 :
213 : return ge::GRAPH_SUCCESS;
214 : }
215 :
216 : std::string SiluGradTiling::ToString(SiluGradTilingData& tilingDataParam) const
217 : {
218 : std::string str;
219 : str += " blockFormer:" + std::to_string(tilingDataParam.get_blockFormer());
220 : str += " ubFormer:" + std::to_string(tilingDataParam.get_ubFormer());
221 : str += " ubOuter:" + std::to_string(tilingDataParam.get_ubOuter());
222 : str += " ubTail:" + std::to_string(tilingDataParam.get_ubTail());
223 : str += " blockTail:" + std::to_string(tilingDataParam.get_blockTail());
224 : str += " shapeLen:" + std::to_string(tilingDataParam.get_shapeLen());
225 : str += " ubSplitAxis:" + std::to_string(tilingDataParam.get_ubSplitAxis());
226 : str += " dimProductBeforeUbInner:" + std::to_string(tilingDataParam.get_dimProductBeforeUbInner());
227 : str += " elemNum:" + std::to_string(tilingDataParam.get_elemNum());
228 : return str;
229 : }
230 :
231 : ge::graphStatus SiluGradTiling::DoLibApiTiling() { return ge::GRAPH_SUCCESS; }
232 :
233 : uint64_t SiluGradTiling::GetTilingKey() const { return tilingKey_; }
234 :
235 : ge::graphStatus SiluGradTiling::GetWorkspaceSize()
236 : {
237 : workspaceSize_ = WORKSPACE_SIZE;
238 : return ge::GRAPH_SUCCESS;
239 : }
240 :
241 : ge::graphStatus SiluGradTiling::PostTiling()
242 : {
243 : context_->SetTilingKey(GetTilingKey());
244 : context_->SetBlockDim(blockNum);
245 : size_t* workspaces = context_->GetWorkspaceSizes(1);
246 : OP_CHECK_NULL_WITH_CONTEXT(context_, workspaces);
247 : workspaces[0] = workspaceSize_;
248 : tilingData.SaveToBuffer(context_->GetRawTilingData()->GetData(), context_->GetRawTilingData()->GetCapacity());
249 : context_->GetRawTilingData()->SetDataSize(tilingData.GetDataSize());
250 : OP_LOGI(context_, "TilingInfo: %s.", ToString(tilingData).c_str());
251 : return ge::GRAPH_SUCCESS;
252 : }
253 :
254 : ge::graphStatus TilingForSiluGrad(gert::TilingContext* context)
255 : {
256 : auto compileInfo = reinterpret_cast<const SiluGradCompileInfo*>(context->GetCompileInfo());
257 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo);
258 : SiluGradTiling tiling(context);
259 : return tiling.DoTiling();
260 : }
261 :
262 : ge::graphStatus TilingPrepareForSiluGrad(gert::TilingParseContext* context)
263 : {
264 : auto compileInfoPtr = context->GetCompiledInfo<SiluGradCompileInfo>();
265 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfoPtr);
266 :
267 : fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
268 : OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
269 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
270 : compileInfoPtr->coreNum = ascendcPlatform.GetCoreNumAiv();
271 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, compileInfoPtr->ubSize);
272 : return ge::GRAPH_SUCCESS;
273 : }
274 :
275 : IMPL_OP_OPTILING(SiluGrad).Tiling(TilingForSiluGrad).TilingParse<SiluGradCompileInfo>(TilingPrepareForSiluGrad);
276 :
277 : } // namespace optiling
|