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 "resize_bilinear_builder.h"
17 
18 #include "ops_registry.h"
19 
20 namespace OHOS {
21 namespace NeuralNetworkRuntime {
22 namespace Ops {
23 static const int INPUT_NUM = 1;
24 static const int OUTPUT_NUM = 1;
25 static const int PARAM_MAX_NUM = 5;
26 static const int SCALE_LENGTH = 1;
27 static const std::string OP_NAME = "ResizeBilinear";
28 
ResizeBilinearBuilder()29 ResizeBilinearBuilder::ResizeBilinearBuilder() {}
30 
~ResizeBilinearBuilder()31 ResizeBilinearBuilder::~ResizeBilinearBuilder() {}
32 
SetNewHeight(const std::shared_ptr<NNTensor> & tensor)33 OH_NN_ReturnCode ResizeBilinearBuilder::SetNewHeight(const std::shared_ptr<NNTensor>& tensor)
34 {
35     tensor->IdentifyOpParameter();
36     if (tensor->GetElementCount() != SCALE_LENGTH) {
37         LOGE("[ResizeBilinear] SetNewHeight failed, the new_height dimensions should be scaler.");
38         return OH_NN_INVALID_PARAMETER;
39     }
40 
41     if (tensor->GetDataType() != OH_NN_INT64) {
42         LOGE("[ResizeBilinear] SetNewHeight failed, the new_height should be type OH_NN_INT64");
43         return OH_NN_INVALID_PARAMETER;
44     }
45 
46     void* buffer = tensor->GetBuffer();
47     if (buffer == nullptr) {
48         LOGE("[ResizeBilinear] ResizeBilinear failed, the new_height passed buffer is empty.");
49         return OH_NN_INVALID_PARAMETER;
50     }
51 
52     m_newHeight = *(static_cast<uint64_t *>(buffer));
53     return OH_NN_SUCCESS;
54 }
55 
SetNewWidth(const std::shared_ptr<NNTensor> & tensor)56 OH_NN_ReturnCode ResizeBilinearBuilder::SetNewWidth(const std::shared_ptr<NNTensor>& tensor)
57 {
58     tensor->IdentifyOpParameter();
59     if (tensor->GetElementCount() != SCALE_LENGTH) {
60         LOGE("[ResizeBilinear] SetNewWidth failed, the new_width dimensions should be scaler.");
61         return OH_NN_INVALID_PARAMETER;
62     }
63 
64     if (tensor->GetDataType() != OH_NN_INT64) {
65         LOGE("[ResizeBilinear] SetNewWidth failed, the new_width should be type OH_NN_INT64");
66         return OH_NN_INVALID_PARAMETER;
67     }
68 
69     void* buffer = tensor->GetBuffer();
70     if (buffer == nullptr) {
71         LOGE("[ResizeBilinear] SetNewWidth failed, the new_width passed buffer is empty.");
72         return OH_NN_INVALID_PARAMETER;
73     }
74 
75     m_newWidth = *(static_cast<uint64_t *>(buffer));
76     return OH_NN_SUCCESS;
77 }
78 
SetPreserveAspectRatio(const std::shared_ptr<NNTensor> & tensor)79 OH_NN_ReturnCode ResizeBilinearBuilder::SetPreserveAspectRatio(const std::shared_ptr<NNTensor>& tensor)
80 {
81     tensor->IdentifyOpParameter();
82     if (tensor->GetElementCount() != SCALE_LENGTH) {
83         LOGE("[ResizeBilinear] SetPreserveAspectRatio failed, the preserve_aspect_ratio dimensions should be scaler.");
84         return OH_NN_INVALID_PARAMETER;
85     }
86 
87     if (tensor->GetDataType() != OH_NN_BOOL) {
88         LOGE("[ResizeBilinear] SetPreserveAspectRatio failed, the preserve_aspect_ratio should be type OH_NN_BOOL");
89         return OH_NN_INVALID_PARAMETER;
90     }
91 
92     void* buffer = tensor->GetBuffer();
93     if (buffer == nullptr) {
94         LOGE("[ResizeBilinear] SetPreserveAspectRatio failed, the preserve_aspect_ratio passed buffer is empty.");
95         return OH_NN_INVALID_PARAMETER;
96     }
97 
98     m_preserveAspectRatio = *(static_cast<bool *>(buffer));
99     return OH_NN_SUCCESS;
100 }
101 
SetCoordinateTransformMode(const std::shared_ptr<NNTensor> & tensor)102 OH_NN_ReturnCode ResizeBilinearBuilder::SetCoordinateTransformMode(const std::shared_ptr<NNTensor>& tensor)
103 {
104     tensor->IdentifyOpParameter();
105     if (tensor->GetElementCount() != SCALE_LENGTH) {
106         LOGE("[ResizeBilinear] SetCoordinateTransformMode failed,"
107             "the coordinate_transform_mode dimensions should be scaler.");
108         return OH_NN_INVALID_PARAMETER;
109     }
110 
111     if (tensor->GetDataType() != OH_NN_INT8) {
112         LOGE("[ResizeBilinear] SetCoordinateTransformMode failed,"
113             "the coordinate_transform_mode should be type OH_NN_INT32");
114         return OH_NN_INVALID_PARAMETER;
115     }
116 
117     void* buffer = tensor->GetBuffer();
118     if (buffer == nullptr) {
119         LOGE("[ResizeBilinear] SetCoordinateTransformMode failed,"
120             "the coordinate_transform_mode passed buffer is empty.");
121         return OH_NN_INVALID_PARAMETER;
122     }
123 
124     m_coordinateTransformMode = *(static_cast<mindspore::lite::CoordinateTransformMode *>(buffer));
125     return OH_NN_SUCCESS;
126 }
127 
SetExcludeOutside(const std::shared_ptr<NNTensor> & tensor)128 OH_NN_ReturnCode ResizeBilinearBuilder::SetExcludeOutside(const std::shared_ptr<NNTensor>& tensor)
129 {
130     tensor->IdentifyOpParameter();
131     if (tensor->GetElementCount() != SCALE_LENGTH) {
132         LOGE("[ResizeBilinear] SetExcludeOutside failed, the exclude_outside dimensions should be scaler.");
133         return OH_NN_INVALID_PARAMETER;
134     }
135 
136     if (tensor->GetDataType() != OH_NN_INT64) {
137         LOGE("[ResizeBilinear] SetExcludeOutside failed, the exclude_outside should be type OH_NN_INT64");
138         return OH_NN_INVALID_PARAMETER;
139     }
140 
141     void* buffer = tensor->GetBuffer();
142     if (buffer == nullptr) {
143         LOGE("[ResizeBilinear] SetExcludeOutside failed, the exclude_outside passed buffer is empty.");
144         return OH_NN_INVALID_PARAMETER;
145     }
146 
147     m_excludeOutside = *(static_cast<uint64_t *>(buffer));
148     return OH_NN_SUCCESS;
149 }
150 
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)151 OH_NN_ReturnCode ResizeBilinearBuilder::Build(const std::vector<uint32_t>& paramsIndex,
152                                               const std::vector<uint32_t>& inputsIndex,
153                                               const std::vector<uint32_t>& outputsIndex,
154                                               const std::vector<std::shared_ptr<NNTensor>>& allTensors)
155 {
156     if (m_isBuild) {
157         LOGE("[ResizeBilinear] Build failed, the Resize operation has been build, cannot build again.");
158         return OH_NN_OPERATION_FORBIDDEN;
159     }
160 
161     OH_NN_ReturnCode returnCode = CheckIOIndex(inputsIndex, outputsIndex, allTensors, INPUT_NUM, OUTPUT_NUM);
162     if (returnCode != OH_NN_SUCCESS) {
163         LOGE("[ResizeBilinear] Build failed, passed invalid input or output index.");
164         return returnCode;
165     }
166 
167     m_inputsIndex = inputsIndex;
168     m_outputsIndex = outputsIndex;
169 
170     returnCode = CheckParamIndex(paramsIndex, allTensors, PARAM_MAX_NUM);
171     if (returnCode != OH_NN_SUCCESS) {
172         LOGE("[ResizeBilinear] Build failed, passed invalid params index.");
173         return returnCode;
174     }
175 
176     for (uint32_t i : paramsIndex) {
177         std::shared_ptr<NNTensor> tensor = allTensors[i];
178         if (m_paramMap.find(tensor->GetType()) != m_paramMap.end()) {
179             returnCode = (this->*(m_paramMap[tensor->GetType()]))(tensor);
180         } else {
181             LOGE("[ResizeBilinear] Build failed, param invalid, type=%d", tensor->GetType());
182             return OH_NN_INVALID_PARAMETER;
183         }
184 
185         if (returnCode != OH_NN_SUCCESS) {
186             LOGE("[ResizeBilinear] Build failed, passed invalid param.");
187             return returnCode;
188         }
189     }
190 
191     SetQuantType(outputsIndex, allTensors);
192 
193     m_name = OP_NAME;
194     m_isBuild = true;
195     return OH_NN_SUCCESS;
196 }
197 
GetPrimitive()198 LiteGraphPrimitvePtr ResizeBilinearBuilder::GetPrimitive()
199 {
200     if (!m_isBuild) {
201         LOGE("[ResizeBilinear] GetPrimitive failed, cannot get primitive before call build.");
202         return {nullptr, DestroyLiteGraphPrimitive};
203     }
204 
205     float cubicCoeff{0.0f};
206     float extrapolationValue{0.0f};
207     mindspore::lite::NearestMode nearestMode{mindspore::lite::NEAREST_MODE_NORMAL};
208 
209     void* primitive = mindspore::lite::MindIR_Resize_CreatePrimitive(m_method, m_newHeight, m_newWidth,
210         m_preserveAspectRatio, m_coordinateTransformMode, cubicCoeff, m_excludeOutside,
211         extrapolationValue, nearestMode);
212 
213     LiteGraphPrimitvePtr graphPrimitivePtr(primitive, DestroyLiteGraphPrimitive);
214     return graphPrimitivePtr;
215 }
216 
217 REGISTER_OPS(ResizeBilinearBuilder, OH_NN_OPS_RESIZE_BILINEAR);
218 } // namespace Ops
219 } // namespace NeuralNetworkRuntime
220 } // namespace OHOS