1 /*
2  * Copyright (c) 2024 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 "minimum_builder.h"
17 
18 #include "mindir.h"
19 #include "ops_registry.h"
20 
21 namespace OHOS {
22 namespace NeuralNetworkRuntime {
23 namespace Ops {
24 static const int INPUT_NUM = 2;
25 static const int OUTPUT_NUM = 1;
26 static const int PARAM_NUM = 0;
27 static const std::string OP_NAME = "Minimum";
28 
MinimumBuilder()29 MinimumBuilder::MinimumBuilder() {}
30 
~MinimumBuilder()31 MinimumBuilder::~MinimumBuilder() {}
32 
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)33 OH_NN_ReturnCode MinimumBuilder::Build(const std::vector<uint32_t>& paramsIndex,
34                                        const std::vector<uint32_t>& inputsIndex,
35                                        const std::vector<uint32_t>& outputsIndex,
36                                        const std::vector<std::shared_ptr<NNTensor>>& allTensors)
37 {
38     if (m_isBuild) {
39         LOGE("[Minimum] Build failed, the minimum operation has been build. cannot build again.");
40         return OH_NN_OPERATION_FORBIDDEN;
41     }
42 
43     auto ret = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
44     if (ret != OH_NN_SUCCESS) {
45         LOGE("[Minimum] Build failed, passed invalid input or output index.");
46         return ret;
47     }
48 
49     m_inputsIndex = inputsIndex;
50     m_outputsIndex = outputsIndex;
51 
52     ret = CheckParamIndex(paramsIndex, allTensors, PARAM_NUM);
53     if (ret != OH_NN_SUCCESS) {
54         LOGE("[Minimum] Build failed, passed invalid param index.");
55         return ret;
56     }
57 
58     m_name = OP_NAME;
59     m_isBuild = true;
60     return OH_NN_SUCCESS;
61 }
62 
GetPrimitive()63 LiteGraphPrimitvePtr MinimumBuilder::GetPrimitive()
64 {
65     if (!m_isBuild) {
66         LOGE("[Minimum] GetPrimitive failed, cannot get primitive before call build.");
67         return {nullptr, DestroyLiteGraphPrimitive};
68     }
69 
70     void* primitive = mindspore::lite::MindIR_Minimum_CreatePrimitive();
71     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive) ;
72     return graphPrimitivePtr;
73 }
74 
75 REGISTER_OPS(MinimumBuilder, OH_NN_OPS_MINIMUM);
76 } // namespace Ops
77 } // namespace NeuralNetworkRuntime
78 } // namespace OHOS
79