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 "mul_builder.h"
17
18 #include "frameworks/native/transform.h"
19 #include "frameworks/native/validation.h"
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 int SCALE_LENGTH = 1;
28 static const std::string OP_NAME = "Mul";
29
MulBuilder()30 MulBuilder::MulBuilder() {}
31
~MulBuilder()32 MulBuilder::~MulBuilder() {}
33
SetActivationType(std::shared_ptr<NNTensor> tensor)34 OH_NN_ReturnCode MulBuilder::SetActivationType(std::shared_ptr<NNTensor> tensor)
35 {
36 tensor->IdentifyOpParameter();
37 if (tensor->GetElementCount() != SCALE_LENGTH) {
38 LOGE("[Mul] Mul SetActivationType failed. The shape of activation should be scaler.");
39 return OH_NN_INVALID_PARAMETER;
40 }
41
42 if (tensor->GetDataType() != OH_NN_INT8) {
43 LOGE("[Mul] Mul SetActivationType failed. The activation should be type OH_NN_INT8.");
44 return OH_NN_INVALID_PARAMETER;
45 }
46
47 void* buffer = tensor->GetBuffer();
48 if (buffer == nullptr) {
49 LOGE("[Mul] SetActivationType failed, the activationType passed a empty buffer.");
50 return OH_NN_INVALID_PARAMETER;
51 }
52
53 int8_t* fuseData = static_cast<int8_t*>(buffer);
54 if (!OHOS::NeuralNetworkRuntime::Validation::ValidateFuseType(static_cast<OH_NN_FuseType>(*fuseData))) {
55 LOGE("[Mul] Mul SetActivationType failed. Fuse activation type is invalid");
56 return OH_NN_INVALID_PARAMETER;
57 }
58
59 auto fuseType = (OH_NN_FuseType)(*fuseData);
60 m_activationType = NNToMS::TransfromFusionType(fuseType);
61 return OH_NN_SUCCESS;
62 }
63
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)64 OH_NN_ReturnCode MulBuilder::Build(const std::vector<uint32_t>& paramsIndex,
65 const std::vector<uint32_t>& inputsIndex,
66 const std::vector<uint32_t>& outputsIndex,
67 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
68 {
69 if (m_isBuild) {
70 LOGE("[Mul] Mul build failed. operation has been build, cannot build again.");
71 return OH_NN_OPERATION_FORBIDDEN;
72 }
73
74 OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
75 if (returnCode != OH_NN_SUCCESS) {
76 LOGE("[Mul] Mul build failed. Passed invalid input or output index of Mul operation index.");
77 return returnCode;
78 }
79
80 m_inputsIndex = inputsIndex;
81 m_outputsIndex = outputsIndex;
82
83 for (int i : paramsIndex) {
84 std::shared_ptr<NNTensor> tensor = allTensors[i];
85 switch (tensor->GetType()) {
86 case OH_NN_MUL_ACTIVATION_TYPE:
87 returnCode = SetActivationType(tensor);
88 break;
89 default:
90 LOGE("[Mul] Parameter Type is invalid, type=%d", tensor->GetType());
91 return OH_NN_INVALID_PARAMETER;
92 }
93
94 if (returnCode != OH_NN_SUCCESS) {
95 LOGE("[Mul] Mul build failed. Passed invalid param.");
96 return returnCode;
97 }
98 }
99
100 // The quantization type of the first output determinies that of the operator.
101 SetQuantType(outputsIndex, allTensors);
102 m_name = OP_NAME;
103 m_isBuild = true;
104 return OH_NN_SUCCESS;
105 }
106
GetPrimitive()107 LiteGraphPrimitvePtr MulBuilder::GetPrimitive()
108 {
109 if (!m_isBuild) {
110 LOGE("[Mul] Mul GetPrimitive failed. Cannot get primitive before call build.");
111 return {nullptr, DestroyLiteGraphPrimitive};
112 }
113
114 void* primitive = mindspore::lite::MindIR_MulFusion_CreatePrimitive(m_activationType);
115 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
116 return graphPrimitivePtr;
117 }
118
119 REGISTER_OPS(MulBuilder, OH_NN_OPS_MUL);
120 } // namespace Ops
121 } // namespace NeuralNetworkRuntime
122 } // namespace OHOS