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 "scale_builder.h"
17
18 #include "frameworks/native/ops_registry.h"
19 #include "frameworks/native/validation.h"
20 #include "frameworks/native/transform.h"
21
22 namespace OHOS {
23 namespace NeuralNetworkRuntime {
24 namespace Ops {
25 static const int INPUT_NUM = 3;
26 static const int OUTPUT_NUM = 1;
27 static const int SCALE_LENGTH = 1;
28 static const std::string OP_NAME = "Scale";
29
ScaleBuilder()30 ScaleBuilder::ScaleBuilder() {}
31
~ScaleBuilder()32 ScaleBuilder::~ScaleBuilder() {}
33
SetAxis(std::shared_ptr<NNTensor> tensor)34 OH_NN_ReturnCode ScaleBuilder::SetAxis(std::shared_ptr<NNTensor> tensor)
35 {
36 tensor->IdentifyOpParameter();
37 if (tensor->GetDataType() != OH_NN_INT64) {
38 LOGE("[ScaleBuilder] SetAxis failed, the axis should be type OH_NN_INT64.");
39 return OH_NN_INVALID_PARAMETER;
40 }
41
42 if (tensor->GetElementCount() != SCALE_LENGTH) {
43 LOGE("[ScaleBuilder] SetAxis failed, the axis dimensions should be scaler.");
44 return OH_NN_INVALID_PARAMETER;
45 }
46
47 void* buffer = tensor->GetBuffer();
48 if (buffer == nullptr) {
49 LOGE("[ScaleBuilder] SetAxis failed, the axis passed buffer is empty.");
50 return OH_NN_INVALID_PARAMETER;
51 }
52
53 m_axis = static_cast<uint64_t*>(buffer);
54 return OH_NN_SUCCESS;
55 }
56
SetActivationType(std::shared_ptr<NNTensor> tensor)57 OH_NN_ReturnCode ScaleBuilder::SetActivationType(std::shared_ptr<NNTensor> tensor)
58 {
59 tensor->IdentifyOpParameter();
60 if (tensor->GetDataType() != OH_NN_INT8) {
61 LOGE("[ScaleBuilder] SetActivationType failed, the activation should be type OH_NN_INT32.");
62 return OH_NN_INVALID_PARAMETER;
63 }
64
65 if (tensor->GetElementCount() != SCALE_LENGTH) {
66 LOGE("[ScaleBuilder] SetActivationType failed, the activation dimensions should be scaler.");
67 return OH_NN_INVALID_PARAMETER;
68 }
69
70 void* buffer = tensor->GetBuffer();
71 if (buffer == nullptr) {
72 LOGE("[ScaleBuilder] SetActivationType failed, the activation passed buffer is empty.");
73 return OH_NN_INVALID_PARAMETER;
74 }
75
76 const int8_t* fuseData = static_cast<const int8_t*>(buffer);
77 if (!OHOS::NeuralNetworkRuntime::Validation::ValidateFuseType(static_cast<OH_NN_FuseType>(*fuseData))) {
78 LOGE("[ScaleBuilder] SetActivationType failed, the activation input is invalid.");
79 return OH_NN_INVALID_PARAMETER;
80 }
81
82 auto fuseType = (OH_NN_FuseType)(*fuseData);
83 m_activationType = NNToMS::TransfromFusionType(fuseType);
84 return OH_NN_SUCCESS;
85 }
86
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)87 OH_NN_ReturnCode ScaleBuilder::Build(const std::vector<uint32_t>& paramsIndex,
88 const std::vector<uint32_t>& inputsIndex,
89 const std::vector<uint32_t>& outputsIndex,
90 const std::vector<std::shared_ptr<NNTensor>>& allTensors)
91 {
92 if (m_isBuild) {
93 LOGE("[ScaleBuilder] Build failed, the scale operation has been build, cannot build again.");
94 return OH_NN_OPERATION_FORBIDDEN;
95 }
96
97 OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
98 if (returnCode != OH_NN_SUCCESS) {
99 LOGE("[ScaleBuilder] Build failed, passed invalid input or output index.");
100 return returnCode;
101 }
102
103 m_inputsIndex = inputsIndex;
104 m_outputsIndex = outputsIndex;
105
106 for (uint32_t i : paramsIndex) {
107 std::shared_ptr<NNTensor> tensor = allTensors[i];
108 switch (tensor->GetType()) {
109 case OH_NN_SCALE_AXIS:
110 returnCode = SetAxis(tensor);
111 break;
112 case OH_NN_SCALE_ACTIVATIONTYPE:
113 returnCode = SetActivationType(tensor);
114 break;
115 default:
116 LOGE("[ResizeBilinear] Build failed, parameter type is invalid. type=%d", tensor->GetType());
117 return OH_NN_INVALID_PARAMETER;
118 }
119
120 if (returnCode != OH_NN_SUCCESS) {
121 LOGE("[ScaleBuilder] Build failed, passed invalid param.");
122 return returnCode;
123 }
124 }
125
126 // The quantization type of the first output determinies that of the operator.
127 SetQuantType(outputsIndex, allTensors);
128
129 m_isBuild = true;
130 m_name = OP_NAME;
131 return OH_NN_SUCCESS;
132 }
133
GetPrimitive()134 LiteGraphPrimitvePtr ScaleBuilder::GetPrimitive()
135 {
136 if (!m_isBuild) {
137 LOGE("[ScaleBuilder] GetPrimitive failed, cannot get primitive before call build.");
138 return {nullptr, DestroyLiteGraphPrimitive};
139 }
140
141 void* primitive = mindspore::lite::MindIR_ScaleFusion_CreatePrimitive(*m_axis, m_activationType);
142 LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
143 return graphPrimitivePtr;
144 }
145
146 REGISTER_OPS(ScaleBuilder, OH_NN_OPS_SCALE);
147 } // namespace Ops
148 } // namespace NeuralNetworkRuntime
149 } // namespace OHOS