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 "maximum_builder.h"
17
18 #include "mindir.h"
19
20 #include "frameworks/native/ops_registry.h"
21
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
24 namespace Ops {
25 static const int INPUT_NUM = 2;
26 static const int OUTPUT_NUM = 1;
27 static const std::string OP_NAME = "Maximum";
28
MaximumBuilder()29 MaximumBuilder::MaximumBuilder() {}
30
~MaximumBuilder()31 MaximumBuilder::~MaximumBuilder() {}
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 MaximumBuilder::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("[Maximum] Maximum Build failed. operation has been build, cannot build again.");
40 return OH_NN_OPERATION_FORBIDDEN;
41 }
42
43 OH_NN_ReturnCode ret = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
44 if (ret != OH_NN_SUCCESS) {
45 LOGE("[Maximum] Maximum Build failed. The input or output index of Maximum operation is invalid.");
46 return ret;
47 }
48
49 if (!paramsIndex.empty()) {
50 LOGW("[Maximum] Maximum Build failed. Maximum expects no parameters, but receive %zu", paramsIndex.size());
51 return OH_NN_INVALID_PARAMETER;
52 }
53
54 m_inputsIndex = inputsIndex;
55 m_outputsIndex = outputsIndex;
56
57 m_isBuild = true;
58 m_name = OP_NAME;
59 return OH_NN_SUCCESS;
60 }
61
GetPrimitive()62 LiteGraphPrimitvePtr MaximumBuilder::GetPrimitive()
63 {
64 if (!m_isBuild) {
65 LOGE("[Maximum] Maximum GetPrimitive failed. Cannot get primitive before call build.");
66 return {nullptr, DestroyLiteGraphPrimitive};
67 }
68
69 void* primitive = mindspore::lite::MindIR_Maximum_CreatePrimitive();
70 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
71 return graphPrimitivePtr;
72 }
73
74 REGISTER_OPS(MaximumBuilder, OH_NN_OPS_MAXIMUM);
75 } // namespace Ops
76 } // namespace NeuralNetworkRuntime
77 } // namespace OHOS