• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 std::string OP_NAME = "Tanh";
24 
TanhBuilder()25 TanhBuilder::TanhBuilder() {}
26 
~TanhBuilder()27 TanhBuilder::~TanhBuilder() {}
28 
29 /**
30  * Build method.
31  * 1.set attr of ops.
32  * 2.set inputIndex of ops.
33  * 3.set outputIndex of ops.
34  */
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)35 OH_NN_ReturnCode TanhBuilder::Build(const std::vector<uint32_t>& paramsIndex,
36                                     const std::vector<uint32_t>& inputsIndex,
37                                     const std::vector<uint32_t>& outputsIndex,
38                                     const std::vector<std::shared_ptr<NNTensor>>& allTensors)
39 {
40     if (m_isBuild) {
41         LOGE("[TanhBuilder] Tanh operation has been build, cannot build again.");
42         return OH_NN_OPERATION_FORBIDDEN;
43     }
44 
45     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
46     if (returnCode != OH_NN_SUCCESS) {
47         LOGE("[TanhBuilder] Passed invalid input or output index.");
48         return returnCode;
49     }
50 
51     if (!paramsIndex.empty()) {
52         LOGE("[TanhBuilder] TanhBuilder expects no parameters, but receive %zu", paramsIndex.size());
53         return OH_NN_INVALID_PARAMETER;
54     }
55 
56     m_inputsIndex = inputsIndex;
57     m_outputsIndex = outputsIndex;
58 
59     // The quantization type of the first output determinies that of the operator.
60     SetQuantType(outputsIndex, allTensors);
61 
62     m_name = OP_NAME;
63     m_isBuild = true;
64     return OH_NN_SUCCESS;
65 }
66 
GetPrimitive()67 LiteGraphPrimitvePtr TanhBuilder::GetPrimitive()
68 {
69     if (!m_isBuild) {
70         LOGE("[TanhBuilder] 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     auto primitive =
79         mindspore::lite::MindIR_Activation_CreatePrimitive(m_activationType, alpha, minVal, maxVal, approximate);
80     if (primitive == nullptr) {
81         LOGE("[TanhBuilder] Create primitive of Tanh failed.");
82         return { nullptr, DestroyLiteGraphPrimitive };
83     }
84 
85     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
86     return graphPrimitivePtr;
87 }
88 
89 REGISTER_OPS(TanhBuilder, OH_NN_OPS_TANH);
90 } // namespace Ops
91 } // namespace NeuralNetworkRuntime
92 } // namespace OHOS