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 sigmoid_grad_tiling_arch35.cpp
13 : * \brief
14 : */
15 :
16 : #include "sigmoid_grad_tiling_arch35.h"
17 : #include "platform/platform_info.h"
18 : #include "log/log.h"
19 :
20 : using namespace ge;
21 :
22 : namespace optiling {
23 :
24 : static constexpr uint64_t INDEX_0 = 0;
25 : static constexpr uint64_t INDEX_1 = 1;
26 : static constexpr uint64_t WORKSPACE_SIZE = 32;
27 : static constexpr uint64_t OP_KEY_INVALID = 0;
28 : static constexpr uint64_t OP_KEY_1 = 1;
29 : static constexpr uint64_t OP_KEY_2 = 2;
30 : static constexpr uint64_t OP_KEY_3 = 3;
31 :
32 : ge::graphStatus SigmoidGradTiling::GetPlatformInfo()
33 : {
34 : auto platformInfo = context_->GetPlatformInfo();
35 : if (platformInfo == nullptr) {
36 : auto compileInfoPtr = context_->GetCompileInfo<SigmoidGradCompileInfo>();
37 : OP_CHECK_IF(compileInfoPtr == nullptr, OP_LOGE(context_, "compile info is null"), return ge::GRAPH_FAILED);
38 : coreNum = compileInfoPtr->coreNum;
39 : ubSize = compileInfoPtr->ubSize;
40 : } else {
41 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfo);
42 : coreNum = ascendcPlatform.GetCoreNumAiv();
43 : uint64_t ubSizePlatForm = 0;
44 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ubSizePlatForm);
45 : ubSize = ubSizePlatForm;
46 : }
47 : return ge::GRAPH_SUCCESS;
48 : }
49 :
50 : uint64_t SigmoidGradTiling::GetOpKey(ge::DataType yDtype, ge::DataType dyDtype, ge::DataType zDtype) const
51 : {
52 : bool opKey1Flag = yDtype == DT_FLOAT16 && dyDtype == DT_FLOAT16 && zDtype == DT_FLOAT16;
53 : if (opKey1Flag) {
54 : return OP_KEY_1;
55 : }
56 : bool opKey2Flag = yDtype == DT_BF16 && dyDtype == DT_BF16 && zDtype == DT_BF16;
57 : if (opKey2Flag) {
58 : return OP_KEY_2;
59 : }
60 : bool opKey3Flag = yDtype == DT_FLOAT && dyDtype == DT_FLOAT && zDtype == DT_FLOAT;
61 : if (opKey3Flag) {
62 : return OP_KEY_3;
63 : }
64 :
65 : return OP_KEY_INVALID;
66 : }
67 :
68 : uint64_t SigmoidGradTiling::GenerateTilingKey(uint64_t innerKey) const
69 : {
70 : return opKey * Ops::Base::OP_KEY_OFFSET + innerKey;
71 : }
72 :
73 : std::map<uint64_t, Ops::Base::ComputeParams> SigmoidGradTiling::GetComputeMap(uint64_t opKeyParam) const
74 : {
75 : Ops::Base::ComputeParams computeParams0;
76 : switch (opKeyParam) {
77 : case OP_KEY_1:
78 : computeParams0.maxDtypeBits = static_cast<int64_t>(Ops::Base::BITS_SIZE::BITS32_SIZE);
79 : computeParams0.minDtypeBits = static_cast<int64_t>(Ops::Base::BITS_SIZE::BITS16_SIZE);
80 : computeParams0.extraSize = {0};
81 : computeParams0.bufferDivisor = {96};
82 : return {{0, computeParams0}};
83 : case OP_KEY_2:
84 : computeParams0.maxDtypeBits = static_cast<int64_t>(Ops::Base::BITS_SIZE::BITS32_SIZE);
85 : computeParams0.minDtypeBits = static_cast<int64_t>(Ops::Base::BITS_SIZE::BITS16_SIZE);
86 : computeParams0.extraSize = {0};
87 : computeParams0.bufferDivisor = {96};
88 : return {{0, computeParams0}};
89 : case OP_KEY_3:
90 : computeParams0.maxDtypeBits = static_cast<int64_t>(Ops::Base::BITS_SIZE::BITS32_SIZE);
91 : computeParams0.minDtypeBits = static_cast<int64_t>(Ops::Base::BITS_SIZE::BITS32_SIZE);
92 : computeParams0.extraSize = {0};
93 : computeParams0.bufferDivisor = {192};
94 : return {{0, computeParams0}};
95 : default:
96 : return {};
97 : }
98 : }
99 :
100 : ge::graphStatus SigmoidGradTiling::GetShapeAttrsInfo()
101 : {
102 : auto y = context_->GetInputDesc(INDEX_0);
103 : OP_CHECK_NULL_WITH_CONTEXT(context_, y);
104 : auto yDtype = y->GetDataType();
105 : auto dy = context_->GetInputDesc(INDEX_1);
106 : OP_CHECK_NULL_WITH_CONTEXT(context_, dy);
107 : auto dyDtype = dy->GetDataType();
108 : auto z = context_->GetOutputDesc(INDEX_0);
109 : OP_CHECK_NULL_WITH_CONTEXT(context_, z);
110 : auto zDtype = z->GetDataType();
111 :
112 : opKey = GetOpKey(yDtype, dyDtype, zDtype);
113 : OP_CHECK_IF((opKey == OP_KEY_INVALID),
114 : OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON(context_->GetNodeName(), "y, dy, z",
115 : ge::TypeUtils::DataTypeToSerialString(yDtype) + ", " +
116 : ge::TypeUtils::DataTypeToSerialString(dyDtype) + ", " +
117 : ge::TypeUtils::DataTypeToSerialString(zDtype),
118 : "The dtypes of y, dy, z must be the same"),
119 : return ge::GRAPH_FAILED);
120 : return ge::GRAPH_SUCCESS;
121 : }
122 :
123 : bool SigmoidGradTiling::IsCapable() { return true; }
124 :
125 : ge::graphStatus SigmoidGradTiling::DoOpTiling()
126 : {
127 : auto yShape = context_->GetInputShape(INDEX_0);
128 : OP_CHECK_NULL_WITH_CONTEXT(context_, yShape);
129 : auto dyShape = context_->GetInputShape(INDEX_1);
130 : OP_CHECK_NULL_WITH_CONTEXT(context_, dyShape);
131 : if (!Ops::Base::IsSameElewiseShape(yShape->GetStorageShape(), dyShape->GetStorageShape())) {
132 : OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(
133 : context_->GetNodeName(), "y, dy",
134 : Ops::Base::ToString(yShape->GetStorageShape()) + ", " + Ops::Base::ToString(dyShape->GetStorageShape()),
135 : "The shapes of y and dy must be the same");
136 : return ge::GRAPH_FAILED;
137 : }
138 : Ops::Base::ElewiseTilingParams elewiseTilingParams;
139 : elewiseTilingParams.shape = yShape->GetStorageShape();
140 : elewiseTilingParams.computeMap = GetComputeMap(opKey);
141 : elewiseTilingParams.coreNum = coreNum;
142 : elewiseTilingParams.ubSize = ubSize;
143 :
144 : Ops::Base::ElewiseTilingData elewiseTilingData;
145 : auto status = Ops::Base::ElewiseTiling(elewiseTilingParams, elewiseTilingData);
146 5 : OP_CHECK_IF((status == ge::GRAPH_FAILED),
147 : OP_LOGE(context_->GetNodeName(), "elewise tiling failed, opKey: %lu.", opKey), return ge::GRAPH_FAILED);
148 :
149 : tilingKey_ = GenerateTilingKey(elewiseTilingData.innerKey);
150 : blockNum = elewiseTilingData.blockNum;
151 : tilingData.set_dim0(elewiseTilingData.dim0);
152 : tilingData.set_blockFormer(elewiseTilingData.blockFormer);
153 : tilingData.set_ubFormer(elewiseTilingData.ubFormer);
154 : tilingData.set_ubLoopOfFormerBlock(elewiseTilingData.ubLoopOfFormerBlock);
155 : tilingData.set_ubLoopOfTailBlock(elewiseTilingData.ubLoopOfTailBlock);
156 : tilingData.set_ubTailOfFormerBlock(elewiseTilingData.ubTailOfFormerBlock);
157 : tilingData.set_ubTailOfTailBlock(elewiseTilingData.ubTailOfTailBlock);
158 : tilingData.set_elemNum(elewiseTilingData.elemNum);
159 :
160 : return ge::GRAPH_SUCCESS;
161 : }
162 :
163 : std::string SigmoidGradTiling::ToString(SigmoidGradTilingData& tilingDataParam) const
164 : {
165 : std::string str;
166 : str += " dim0:" + std::to_string(tilingDataParam.get_dim0());
167 : str += " blockFormer:" + std::to_string(tilingDataParam.get_blockFormer());
168 : str += " ubFormer:" + std::to_string(tilingDataParam.get_ubFormer());
169 : str += " ubLoopOfFormerBlock:" + std::to_string(tilingDataParam.get_ubLoopOfFormerBlock());
170 : str += " ubLoopOfTailBlock:" + std::to_string(tilingDataParam.get_ubLoopOfTailBlock());
171 : str += " ubTailOfFormerBlock:" + std::to_string(tilingDataParam.get_ubTailOfFormerBlock());
172 : str += " ubTailOfTailBlock:" + std::to_string(tilingDataParam.get_ubTailOfTailBlock());
173 : str += " elemNum:" + std::to_string(tilingDataParam.get_elemNum());
174 : return str;
175 : }
176 :
177 : ge::graphStatus SigmoidGradTiling::DoLibApiTiling() { return ge::GRAPH_SUCCESS; }
178 :
179 : uint64_t SigmoidGradTiling::GetTilingKey() const { return tilingKey_; }
180 :
181 : ge::graphStatus SigmoidGradTiling::GetWorkspaceSize()
182 : {
183 : workspaceSize_ = WORKSPACE_SIZE;
184 : return ge::GRAPH_SUCCESS;
185 : }
186 :
187 : ge::graphStatus SigmoidGradTiling::PostTiling()
188 : {
189 : context_->SetTilingKey(GetTilingKey());
190 : context_->SetBlockDim(blockNum);
191 : size_t* workspaces = context_->GetWorkspaceSizes(1);
192 : OP_CHECK_IF(workspaces == nullptr, OP_LOGE(context_, "workspace is null"), return ge::GRAPH_FAILED);
193 : workspaces[0] = workspaceSize_;
194 : tilingData.SaveToBuffer(context_->GetRawTilingData()->GetData(), context_->GetRawTilingData()->GetCapacity());
195 : context_->GetRawTilingData()->SetDataSize(tilingData.GetDataSize());
196 : OP_LOGI(context_, "TilingInfo: %s.", ToString(tilingData).c_str());
197 : return ge::GRAPH_SUCCESS;
198 : }
199 :
200 : ge::graphStatus TilingForSigmoidGrad(gert::TilingContext* context)
201 : {
202 : auto compileInfo = context->GetCompileInfo<SigmoidGradCompileInfo>();
203 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfo);
204 : SigmoidGradTiling tiling(context);
205 : return tiling.DoTiling();
206 : }
207 :
208 : ge::graphStatus TilingPrepareForSigmoidGrad(gert::TilingParseContext* context)
209 : {
210 : auto compileInfoPtr = context->GetCompiledInfo<SigmoidGradCompileInfo>();
211 : OP_CHECK_NULL_WITH_CONTEXT(context, compileInfoPtr);
212 : fe::PlatFormInfos* platformInfoPtr = context->GetPlatformInfo();
213 : OP_CHECK_NULL_WITH_CONTEXT(context, platformInfoPtr);
214 : auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr);
215 : compileInfoPtr->coreNum = ascendcPlatform.GetCoreNumAiv();
216 : ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, compileInfoPtr->ubSize);
217 : return ge::GRAPH_SUCCESS;
218 : }
219 :
220 : IMPL_OP_OPTILING(SigmoidGrad)
221 : .Tiling(TilingForSigmoidGrad)
222 : .TilingParse<SigmoidGradCompileInfo>(TilingPrepareForSigmoidGrad);
223 :
224 : } // namespace optiling
|