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