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 "tanh_builder.h"
17
18 namespace OHOS {
19 namespace NeuralNetworkRuntime {
20 namespace Ops {
21 static const int INPUT_NUM = 1;
22 static const int OUTPUT_NUM = 1;
23 static const int PARAM_NUM = 0;
24 static const std::string OP_NAME = "Tanh";
25
TanhBuilder()26 TanhBuilder::TanhBuilder() {}
27
~TanhBuilder()28 TanhBuilder::~TanhBuilder() {}
29
30 /**
31 * Build method.
32 * 1.set attr of ops.
33 * 2.set inputIndex of ops.
34 * 3.set outputIndex of ops.
35 */
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)36 OH_NN_ReturnCode TanhBuilder::Build(const std::vector<uint32_t>& paramsIndex,
37 const std::vector<uint32_t>& inputsIndex,
38 const std::vector<uint32_t>& outputsIndex,
39 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
40 {
41 if (m_isBuild) {
42 LOGE("[TanhBuilder] Tanh operation has been build, cannot build again.");
43 return OH_NN_OPERATION_FORBIDDEN;
44 }
45
46 OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
47 if (returnCode != OH_NN_SUCCESS) {
48 LOGE("[TanhBuilder] Passed invalid input or output index.");
49 return returnCode;
50 }
51
52 m_inputsIndex = inputsIndex;
53 m_outputsIndex = outputsIndex;
54
55 returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_NUM);
56 if (returnCode != OH_NN_SUCCESS) {
57 LOGE("[TanhBuilder] Passed invalid param index.");
58 return returnCode;
59 }
60
61 // The quantization type of the first output determinies that of the operator.
62 SetQuantType(outputsIndex, allTensors);
63
64 m_name = OP_NAME;
65 m_isBuild = true;
66 return OH_NN_SUCCESS;
67 }
68
GetPrimitive()69 LiteGraphPrimitvePtr TanhBuilder::GetPrimitive()
70 {
71 if (!m_isBuild) {
72 LOGE("[TanhBuilder] Cannot get primitive before call build.");
73 return { nullptr, DestroyLiteGraphPrimitive };
74 }
75
76 float alpha {0.0f};
77 float minVal {0.0f};
78 float maxVal {0.0f};
79 bool approximate {false};
80 auto primitive =
81 mindspore::lite::MindIR_Activation_CreatePrimitive(m_activationType, alpha, minVal, maxVal, approximate);
82 if (primitive == nullptr) {
83 LOGE("[TanhBuilder] Create primitive of Tanh failed.");
84 return { nullptr, DestroyLiteGraphPrimitive };
85 }
86
87 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
88 return graphPrimitivePtr;
89 }
90
91 REGISTER_OPS(TanhBuilder, OH_NN_OPS_TANH);
92 } // namespace Ops
93 } // namespace NeuralNetworkRuntime
94 } // namespace OHOS