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 "eltwise_builder.h"
17
18 namespace OHOS {
19 namespace NeuralNetworkRuntime {
20 namespace Ops {
21 static const int INPUT_NUM = 2;
22 static const int OUTPUT_NUM = 1;
23 static constexpr int SCALAR_LENGTH = 1;
24 static const std::string OP_NAME = "Eltwise";
25
EltwiseBuilder()26 EltwiseBuilder::EltwiseBuilder() {}
27
~EltwiseBuilder()28 EltwiseBuilder::~EltwiseBuilder() {}
29
SetMode(std::shared_ptr<NNTensor> tensor)30 OH_NN_ReturnCode EltwiseBuilder::SetMode(std::shared_ptr<NNTensor> tensor)
31 {
32 tensor->IdentifyOpParameter();
33
34 if (tensor->GetDataType() != OH_NN_INT8) {
35 LOGE("[Eltwise] SetMode failed, the EltwiseMode should be type OH_NN_INT8.");
36 return OH_NN_INVALID_PARAMETER;
37 }
38
39 if (tensor->GetElementCount() != SCALAR_LENGTH) {
40 LOGE("[Eltwise] SetMode failed, the eltwiseMode shoule be a scalar");
41 return OH_NN_INVALID_PARAMETER;
42 }
43
44 void* buffer = tensor->GetBuffer();
45 if (buffer == nullptr) {
46 LOGE("[Eltwise] SetMode GetBuffer return nullptr");
47 return OH_NN_INVALID_PARAMETER;
48 }
49 int8_t eltwiseMode = *static_cast<int8_t*>(buffer);
50 if (eltwiseMode < mindspore::lite::ELTWISE_MODE_PROD ||
51 eltwiseMode > mindspore::lite::ELTWISE_MODE_UNKNOWN) {
52 LOGE("[Eltwise] SetMode failed, passed invalid eltwiseMode, received %d", eltwiseMode);
53 return OH_NN_INVALID_PARAMETER;
54 }
55 m_mode = (mindspore::lite::EltwiseMode)eltwiseMode;
56
57 return OH_NN_SUCCESS;
58 }
59
60
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)61 OH_NN_ReturnCode EltwiseBuilder::Build(const std::vector<uint32_t>& paramsIndex,
62 const std::vector<uint32_t>& inputsIndex,
63 const std::vector<uint32_t>& outputsIndex,
64 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
65 {
66 if (m_isBuild) {
67 LOGE("[Eltwise] Build failed, operation has been build, cannot build again.");
68 return OH_NN_OPERATION_FORBIDDEN;
69 }
70
71 OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
72 if (returnCode != OH_NN_SUCCESS) {
73 LOGE("[Eltwise] Build failed, passed invalid input index or output indices.");
74 return returnCode;
75 }
76
77 m_inputsIndex = inputsIndex;
78 m_outputsIndex = outputsIndex;
79
80 for (int i : paramsIndex) {
81 std::shared_ptr<NNTensor> tensor = allTensors[i];
82 switch (tensor->GetType()) {
83 case OH_NN_ELTWISE_MODE:
84 returnCode = SetMode(tensor);
85 break;
86 default:
87 LOGE("[Eltwise] Build failed, param invalid, type = %d.", tensor->GetType());
88 return OH_NN_INVALID_PARAMETER;
89 }
90 if (returnCode != OH_NN_SUCCESS) {
91 LOGE("[Eltwise] Build failed, passed invalid param.");
92 return returnCode;
93 }
94 }
95
96 // The quantization type of the first output determinies that of the operator.
97 SetQuantType(outputsIndex, allTensors);
98
99 m_isBuild = true;
100 m_name = OP_NAME;
101 return OH_NN_SUCCESS;
102 }
103
GetPrimitive()104 LiteGraphPrimitvePtr EltwiseBuilder::GetPrimitive()
105 {
106 if (!m_isBuild) {
107 LOGE("[Eltwise] GetPrimitive failed, cannot get primitive before call build.");
108 return {nullptr, DestroyLiteGraphPrimitive};
109 }
110
111 void* primitive = mindspore::lite::MindIR_Eltwise_CreatePrimitive(m_mode);
112 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
113 return graphPrimitivePtr;
114 }
115
116 REGISTER_OPS(EltwiseBuilder, OH_NN_OPS_ELTWISE);
117 } // namespace Ops
118 } // namespace NeuralNetworkRuntime
119 } // namespace OHOS