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