• 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 "sqrt_builder.h"
17 
18 #include "mindir.h"
19 
20 namespace OHOS {
21 namespace NeuralNetworkRuntime {
22 namespace Ops {
23 static const int INPUT_NUM = 1;
24 static const int OUTPUT_NUM = 1;
25 static const std::string OP_NAME = "Sqrt";
26 
SqrtBuilder()27 SqrtBuilder::SqrtBuilder() {}
28 
~SqrtBuilder()29 SqrtBuilder::~SqrtBuilder() {}
30 
31 /**
32  * Build method.
33  * 1.set attr of ops.
34  * 2.set inputIndex of ops.
35  * 3.set outputIndex of ops.
36  */
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)37 OH_NN_ReturnCode SqrtBuilder::Build(const std::vector<uint32_t>& paramsIndex,
38                                     const std::vector<uint32_t>& inputsIndex,
39                                     const std::vector<uint32_t>& outputsIndex,
40                                     const std::vector<std::shared_ptr<NNTensor>>& allTensors)
41 {
42     if (m_isBuild) {
43         LOGE("[SqrtBuilder] Sqrt operation has been build, cannot build again.");
44         return OH_NN_OPERATION_FORBIDDEN;
45     }
46 
47     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
48     if (returnCode != OH_NN_SUCCESS) {
49         LOGE("[SqrtBuilder] Passed invalid input or output index.");
50         return returnCode;
51     }
52 
53     if (!paramsIndex.empty()) {
54         LOGE("[SqrtBuilder] sqrt expects no parameters, but receive %zu", paramsIndex.size());
55         return OH_NN_INVALID_PARAMETER;
56     }
57 
58     m_inputsIndex = inputsIndex;
59     m_outputsIndex = outputsIndex;
60 
61     // The quantization type of the first output determinies that of the operator.
62     SetQuantType(outputsIndex, allTensors);
63     m_isBuild = true;
64     m_name = OP_NAME;
65     return OH_NN_SUCCESS;
66 }
67 
GetPrimitive()68 LiteGraphTensorPtr SqrtBuilder::GetPrimitive()
69 {
70     if (!m_isBuild) {
71         LOGE("[SqrtBuilder] Cannot get primitive before call build.");
72         return {nullptr, DestroyLiteGraphPrimitive};
73     }
74 
75     auto primitive = mindspore::lite::MindIR_Sqrt_CreatePrimitive();
76     if (primitive == nullptr) {
77         LOGE("[SqrtBuilder] Create primitive of Sqrt failed.");
78         return {nullptr, DestroyLiteGraphPrimitive};
79     }
80 
81     LiteGraphTensorPtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
82     return graphPrimitivePtr;
83 }
84 
85 REGISTER_OPS(SqrtBuilder, OH_NN_OPS_SQRT);
86 } // namespace Ops
87 } // namespace NeuralNetworkRuntime
88 } // namespace OHOS
89