1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "sigmoid_builder.h"
17
18 #include "mindir.h"
19
20 #include "ops_registry.h"
21
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
24 namespace Ops {
25 static const int INPUT_NUM = 1;
26 static const int OUTPUT_NUM = 1;
27 static const int PARAM_NUM = 0;
28 static const std::string OP_NAME = "Sigmoid";
29
SigmoidBuilder()30 SigmoidBuilder::SigmoidBuilder() {}
31
~SigmoidBuilder()32 SigmoidBuilder::~SigmoidBuilder() {}
33
Build(const std::vector<uint32_t> & paramsIndex,const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex,const std::vector<std::shared_ptr<NNTensor>> & allTensors)34 OH_NN_ReturnCode SigmoidBuilder::Build(const std::vector<uint32_t>& paramsIndex,
35 const std::vector<uint32_t>& inputsIndex,
36 const std::vector<uint32_t>& outputsIndex,
37 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
38 {
39 if (m_isBuild) {
40 LOGE("[SigmoidBuilder] Build failed, the Sigmoid operation has been build, cannot build again.");
41 return OH_NN_OPERATION_FORBIDDEN;
42 }
43
44 OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
45 if (returnCode != OH_NN_SUCCESS) {
46 LOGE("[SigmoidBuilder] Build failed, passed invalid input or output index.");
47 return returnCode;
48 }
49
50 m_inputsIndex = inputsIndex;
51 m_outputsIndex = outputsIndex;
52
53 returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_NUM);
54 if (returnCode != OH_NN_SUCCESS) {
55 LOGE("[SigmoidBuilder] Build failed, passed invalid input or output index.");
56 return returnCode;
57 }
58
59 // The quantization type of the first output determinies that of the operator.
60 SetQuantType(outputsIndex, allTensors);
61
62 m_isBuild = true;
63 m_name = OP_NAME;
64 return OH_NN_SUCCESS;
65 }
66
GetPrimitive()67 LiteGraphPrimitvePtr SigmoidBuilder::GetPrimitive()
68 {
69 if (!m_isBuild) {
70 LOGE("[SigmoidBuilder] GetPrimitive failed, cannot get primitive before call build.");
71 return {nullptr, DestroyLiteGraphPrimitive};
72 }
73
74 float alpha{0.0f};
75 float minVal{0.0f};
76 float maxVal{0.0f};
77 bool approximate{false};
78 mindspore::lite::ActivationType activationType{mindspore::lite::ACTIVATION_TYPE_SIGMOID};
79
80 void* primitive = mindspore::lite::MindIR_Activation_CreatePrimitive(activationType, alpha, minVal,
81 maxVal, approximate);
82
83 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
84 return graphPrimitivePtr;
85 }
86
87 REGISTER_OPS(SigmoidBuilder, OH_NN_OPS_SIGMOID);
88 } // namespace Ops
89 } // namespace NeuralNetworkRuntime
90 } // namespace OHOS