ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Convert.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Convert.h"
18
19#include "Swizzle.h"
20#include "ir/DataType.h"
22#include <memory>
23
25{
26
27::arm_compute::TensorShape asTensorShape(const ir::Shape &shape, bool apply_dim_correction)
28{
29 // If shape's rank is 0, the tensor is a scalar
30 // Sometimes, some ACL kernel can use a scalar as tensor. But ACL does not allocate buffer for
31 // tensor having rank as 0.
32 const auto tensor_shape = shape.rank() == 0 ? ir::Shape{1} : shape;
33
34 const uint32_t rank = tensor_shape.rank();
35
36 ::arm_compute::TensorShape res{};
37
38 res.set_num_dimensions(rank);
39
40 for (uint32_t axis = 0; axis < rank; ++axis)
41 {
42 // NOTE In some cases, in incorrect dimensions is required.
43 // For example, intput_size is 1 in LSTM. The input-to-input weights([num_units, input_size]) of
44 // LSTM is used as the weight of the FullyConnected.
45 // The FullyConnected's weight must be greater or equal than 2-dimensions.
46 // However, if the dimension correction is applied to input_to_input_weights with input_size
47 // equal to 1, it will be changed to 1-D.
48 // So input_to_input_weights is not used by the weight of FullyConnected.
49 res.set(ToARMComputeAxis(rank, axis).value(), tensor_shape.dim(axis), apply_dim_correction);
50 }
51
52 return res;
53}
54
55::arm_compute::Coordinates asTensorCoordinate(const ir::Coordinates &coord)
56{
57 const uint32_t rank = coord.size();
58
59 ::arm_compute::Coordinates res{};
60
61 res.set_num_dimensions(rank);
62
63 for (uint32_t axis = 0; axis < rank; ++axis)
64 {
65 res.set(ToARMComputeAxis(rank, axis).value(), coord[axis]);
66 }
67
68 return res;
69}
70
71::arm_compute::DataType asDataType(const ir::DataType type)
72{
73 switch (type)
74 {
75 case ir::DataType::FLOAT32:
76 return ::arm_compute::DataType::F32;
77 case ir::DataType::INT32:
78 return ::arm_compute::DataType::S32;
79 case ir::DataType::UINT32:
80 return ::arm_compute::DataType::U32;
81 case ir::DataType::QUANT_UINT8_ASYMM:
82 return ::arm_compute::DataType::QASYMM8;
83 case ir::DataType::BOOL8:
84 case ir::DataType::UINT8:
85 return ::arm_compute::DataType::U8;
86 case ir::DataType::QUANT_INT8_SYMM:
87 return ::arm_compute::DataType::QSYMM8;
88 case ir::DataType::QUANT_INT8_ASYMM:
89 return ::arm_compute::DataType::QASYMM8_SIGNED;
90 case ir::DataType::FLOAT16:
91 return ::arm_compute::DataType::F16;
92 case ir::DataType::INT64:
93 return ::arm_compute::DataType::S64;
94 case ir::DataType::QUANT_INT16_ASYMM:
95 return ::arm_compute::DataType::QASYMM16;
96 case ir::DataType::QUANT_INT8_SYMM_PER_CHANNEL:
97 return ::arm_compute::DataType::QSYMM8_PER_CHANNEL;
98 default:
99 throw std::runtime_error("Not supported internal data type, yet");
100 break;
101 }
102}
103
104::arm_compute::QuantizationInfo asQuantizationInfo(const float scale, const int32_t offset)
105{
106 return ::arm_compute::QuantizationInfo(scale, offset);
107}
108
109::arm_compute::TensorInfo asTensorInfo(const ir::Shape &shape, const ir::TypeInfo &typeInfo,
110 bool apply_dim_correction)
111{
112 ::arm_compute::TensorInfo info(asTensorShape(shape, apply_dim_correction), 1,
113 asDataType(typeInfo.type()),
114 asQuantizationInfo(typeInfo.scale(), typeInfo.zero_point()));
115 info.set_data_layout(::arm_compute::DataLayout::NHWC);
116 return info;
117}
118
119::arm_compute::PadStrideInfo asPadStrideInfo(const ir::ExplicitPadding &padding,
120 const ir::Stride &stride)
121{
122 return ::arm_compute::PadStrideInfo{stride.horizontal,
123 stride.vertical,
124 padding.left,
125 padding.right,
126 padding.top,
127 padding.bottom,
128 ::arm_compute::DimensionRoundingType::FLOOR};
129}
130
131::arm_compute::ActivationLayerInfo asActivationLayerInfo(const ir::Activation act_code)
132{
133 switch (act_code)
134 {
136 return ::arm_compute::ActivationLayerInfo{};
138 return ::arm_compute::ActivationLayerInfo{
139 ::arm_compute::ActivationLayerInfo::ActivationFunction::RELU};
141 return ::arm_compute::ActivationLayerInfo{
142 ::arm_compute::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 1.0f, -1.0f};
144 return ::arm_compute::ActivationLayerInfo{
145 ::arm_compute::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, 6.0f, 0.0f};
146 // Cases for activation of LSTM.
148 return ::arm_compute::ActivationLayerInfo{
149 ::arm_compute::ActivationLayerInfo::ActivationFunction::TANH, 1.0f, 1.0f};
151 // NOTE The sigmoid function is a special case of the Logistic function when L=1, k=1, x0=0.
152 // TODO In ACL and nnapi sepc, currently, Logistic's L always is 1, k always is 1, x0 always
153 // 0(always sigmoid) regardless of values of the parameter.
154 // If ACL support non-sigmoid logistic, should fix param values.
155 return ::arm_compute::ActivationLayerInfo{
156 ::arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC, 0.0f, 0.0f};
157 default:
158 throw std::runtime_error{"Not supported internal activation, yet"};
159 break;
160 }
161}
162
163::arm_compute::ActivationLayerInfo
165 float beta)
166{
167 switch (op_type)
168 {
170 if (beta == 0.f)
171 {
173 {
174 return ::arm_compute::ActivationLayerInfo{
175 ::arm_compute::ActivationLayerInfo::ActivationFunction::RELU};
176 }
177 else
178 {
179 return ::arm_compute::ActivationLayerInfo{
180 ::arm_compute::ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, alpha};
181 }
182 }
183 else
184 {
185 return ::arm_compute::ActivationLayerInfo{
186 ::arm_compute::ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU, alpha, beta};
187 }
189 return ::arm_compute::ActivationLayerInfo{
190 ::arm_compute::ActivationLayerInfo::ActivationFunction::TANH, alpha, beta};
192 // NOTE The sigmoid function is a special case of the Logistic function when L=1, k=1, x0=0.
193 // TODO In ACL and nnapi sepc, currently, Logistic's L always is 1, k always is 1, x0 always
194 // 0(always sigmoid) regardless of values of the parameter.
195 // If ACL support non-sigmoid logistic, should fix param values.
196 return ::arm_compute::ActivationLayerInfo{
197 ::arm_compute::ActivationLayerInfo::ActivationFunction::LOGISTIC};
199 return ::arm_compute::ActivationLayerInfo{
200 ::arm_compute::ActivationLayerInfo::ActivationFunction::LEAKY_RELU, alpha};
201 default:
202 throw std::runtime_error{"Not supported internal elementwise activation, yet"};
203 break;
204 }
205}
206
207arm_compute::Coordinates asCoordinates(const ir::Operand &operand, int32_t rank)
208{
209 std::set<uint32_t> axes = asSet(operand, rank);
210
211 arm_compute::Coordinates reduce_axes;
212 for (const int32_t axis : axes)
213 {
214 reduce_axes.set(reduce_axes.num_dimensions(), axis);
215 }
216
217 return reduce_axes;
218}
219
220std::set<uint32_t> asSet(const ir::Operand &operand, int32_t rank)
221{
222 std::set<std::uint32_t> axes;
223
224 for (size_t i = 0; i < operand.shape().num_elements(); ++i)
225 {
226 int32_t axis = 0;
227 switch (operand.typeInfo().type())
228 {
229 case ir::DataType::INT32:
230 axis = reinterpret_cast<const int32_t *>(operand.data()->base())[i];
231 break;
232 case ir::DataType::INT64:
233 axis = reinterpret_cast<const int64_t *>(operand.data()->base())[i];
234 break;
235 default:
236 throw std::runtime_error("acl_common::asSet: Not supported data type");
237 }
238 if (axis < 0)
239 axis += rank;
240 axes.insert(ToARMComputeAxis(rank, axis).value());
241 }
242
243 return axes;
244}
245
246std::unique_ptr<AclFunction> asAclFunction(std::unique_ptr<::arm_compute::IFunction> &&layer)
247{
248 return std::make_unique<AclFunction>(std::move(layer));
249}
250
251ir::DataType asRuntimeDataType(::arm_compute::DataType data_type)
252{
253 switch (data_type)
254 {
255 case ::arm_compute::DataType::F32:
256 return ir::DataType::FLOAT32;
257 case ::arm_compute::DataType::S32:
258 return ir::DataType::INT32;
259 case ::arm_compute::DataType::U32:
260 return ir::DataType::UINT32;
261 case ::arm_compute::DataType::QASYMM8:
262 return ir::DataType::QUANT_UINT8_ASYMM;
263 case ::arm_compute::DataType::QASYMM8_SIGNED:
264 return ir::DataType::QUANT_INT8_ASYMM;
265 case ::arm_compute::DataType::U8:
266 return ir::DataType::UINT8;
267 case ::arm_compute::DataType::QSYMM8:
268 return ir::DataType::QUANT_INT8_SYMM;
269 case ::arm_compute::DataType::F16:
270 return ir::DataType::FLOAT16;
271 case ::arm_compute::DataType::S64:
272 return ir::DataType::INT64;
273 default:
274 throw std::runtime_error{"Not supported acl data type, yet"};
275 break;
276 }
277}
278
279arm_compute::PoolingType convertPoolType(ir::operation::Pool2D::PoolType pool_type_ir)
280{
281 switch (pool_type_ir)
282 {
284 return arm_compute::PoolingType::AVG;
286 return arm_compute::PoolingType::L2;
288 return arm_compute::PoolingType::MAX;
289 default:
290 throw std::runtime_error("convertPoolType: Not supported operation yet");
291 }
292}
293
294arm_compute::ReductionOperation convertReduceType(ir::operation::Reduce::ReduceType reduce_type_ir)
295{
296 switch (reduce_type_ir)
297 {
299 return arm_compute::ReductionOperation::MAX;
301 return arm_compute::ReductionOperation::MIN;
303 return arm_compute::ReductionOperation::SUM;
304 default:
305 throw std::runtime_error("convertReduceType: Not supported operation yet");
306 }
307}
308
309arm_compute::PixelValue asPixelValue(const ir::Operand &operand)
310{
311 assert(operand.isConstant());
312 assert(operand.shape().num_elements() == 1);
313 switch (operand.typeInfo().type())
314 {
315 case ir::DataType::INT32:
316 return arm_compute::PixelValue(operand.asScalar<int32_t>());
317 case ir::DataType::INT64:
318 return arm_compute::PixelValue(operand.asScalar<int64_t>());
319 case ir::DataType::UINT32:
320 return arm_compute::PixelValue(operand.asScalar<uint64_t>());
321 case ir::DataType::UINT8:
322 return arm_compute::PixelValue(operand.asScalar<uint8_t>());
323 case ir::DataType::FLOAT32:
324 return arm_compute::PixelValue(operand.asScalar<float>());
325 default:
326 throw std::runtime_error("asPixelValue : Not supported datatype yet");
327 }
328}
329
330arm_compute::Size2D asDilation(uint32_t dilation_width, uint32_t dilation_height)
331{
332 assert(dilation_width != 0);
333 assert(dilation_height != 0);
334
335 return arm_compute::Size2D(dilation_width, dilation_height);
336}
337
338} // namespace onert::backend::acl_common
const Dimension & dim(uint32_t axis) const
Definition TensorShape.h:38
uint32_t rank(void) const
Definition TensorShape.h:35
Class to represent position(offset) of tensor. Assume that the front is higher dimensional....
Definition Coordinates.h:35
size_t size() const
Return size of coordinates.
Definition Coordinates.h:93
const TypeInfo & typeInfo(void) const
Definition Operand.h:45
T asScalar(void) const
Definition Operand.h:86
const Shape & shape(void) const
Definition Operand.h:44
void data(std::shared_ptr< Data > &&data)
Definition Operand.h:62
bool isConstant(void) const
Get true if Operand is const, otherwise false a.
Definition Operand.h:77
float scale() const
Definition TypeInfo.h:51
int32_t zero_point() const
Definition TypeInfo.h:53
DataType type() const
Definition TypeInfo.h:50
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540
volatile const char info[]
arm_compute::PoolingType convertPoolType(ir::operation::Pool2D::PoolType pool_type_ir)
Definition Convert.cc:279
ARMComputeAxis ToARMComputeAxis(uint32_t rank, uint32_t axis)
Definition Swizzle.h:45
::arm_compute::Coordinates asTensorCoordinate(const ir::Coordinates &coord)
Definition Convert.cc:55
std::set< uint32_t > asSet(const ir::Operand &operand, int32_t rank)
Definition Convert.cc:220
::arm_compute::ActivationLayerInfo asActivationLayerInfo(const ir::Activation act_code)
Definition Convert.cc:131
arm_compute::ReductionOperation convertReduceType(ir::operation::Reduce::ReduceType reduce_type_ir)
Definition Convert.cc:294
arm_compute::Coordinates asCoordinates(const ir::Operand &operand, int32_t rank)
Definition Convert.cc:207
arm_compute::PixelValue asPixelValue(const ir::Operand &operand)
Definition Convert.cc:309
arm_compute::Size2D asDilation(uint32_t dilation_width, uint32_t dilation_height)
Definition Convert.cc:330
::arm_compute::PadStrideInfo asPadStrideInfo(const ir::ExplicitPadding &padding, const ir::Stride &stride)
Definition Convert.cc:119
std::unique_ptr< AclFunction > asAclFunction(std::unique_ptr<::arm_compute::IFunction > &&layer)
Definition Convert.cc:246
::arm_compute::TensorShape asTensorShape(const ir::Shape &shape, bool apply_dim_correction)
Definition Convert.cc:27
::arm_compute::TensorInfo asTensorInfo(const ir::Shape &shape, const ir::TypeInfo &typeInfo, bool apply_dim_correction)
Definition Convert.cc:109
::arm_compute::DataType asDataType(const ir::DataType type)
Definition Convert.cc:71
::arm_compute::QuantizationInfo asQuantizationInfo(const float scale, const int32_t offset)
Definition Convert.cc:104
ir::DataType asRuntimeDataType(::arm_compute::DataType data_type)
Definition Convert.cc:251