Compute Library
 21.08
Helpers.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
25 
26 #include <algorithm>
27 #include <cmath>
28 
29 namespace arm_compute
30 {
31 namespace test
32 {
33 namespace validation
34 {
35 template <>
37 {
38  const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
40 #if defined(_OPENMP)
41  #pragma omp parallel for
42 #endif /* _OPENMP */
43  for(int i = 0; i < src.num_elements(); ++i)
44  {
45  dst[i] = dequantize_qasymm8(src[i], quantization_info);
46  }
47  return dst;
48 }
49 
50 template <>
52 {
53  const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
55 
56 #if defined(_OPENMP)
57  #pragma omp parallel for
58 #endif /* _OPENMP */
59  for(int i = 0; i < src.num_elements(); ++i)
60  {
61  dst[i] = dequantize_qasymm8_signed(src[i], quantization_info);
62  }
63  return dst;
64 }
65 
66 template <>
68 {
69  const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
71 
72 #if defined(_OPENMP)
73  #pragma omp parallel for
74 #endif /* _OPENMP */
75  for(int i = 0; i < src.num_elements(); ++i)
76  {
77  dst[i] = dequantize_qasymm16(src[i], quantization_info);
78  }
79  return dst;
80 }
81 
82 template <>
84 {
85  SimpleTensor<uint8_t> dst{ src.shape(), DataType::QASYMM8, 1, quantization_info };
86  const UniformQuantizationInfo &qinfo = quantization_info.uniform();
87 
88 #if defined(_OPENMP)
89  #pragma omp parallel for
90 #endif /* _OPENMP */
91  for(int i = 0; i < src.num_elements(); ++i)
92  {
93  dst[i] = quantize_qasymm8(src[i], qinfo);
94  }
95  return dst;
96 }
97 
98 template <>
100 {
101  SimpleTensor<int8_t> dst{ src.shape(), DataType::QASYMM8_SIGNED, 1, quantization_info };
102  const UniformQuantizationInfo &qinfo = quantization_info.uniform();
103 
104 #if defined(_OPENMP)
105  #pragma omp parallel for
106 #endif /* _OPENMP */
107  for(int i = 0; i < src.num_elements(); ++i)
108  {
109  dst[i] = quantize_qasymm8_signed(src[i], qinfo);
110  }
111  return dst;
112 }
113 
114 template <>
116 {
117  SimpleTensor<uint16_t> dst{ src.shape(), DataType::QASYMM16, 1, quantization_info };
118  const UniformQuantizationInfo &qinfo = quantization_info.uniform();
119 
120 #if defined(_OPENMP)
121  #pragma omp parallel for
122 #endif /* _OPENMP */
123  for(int i = 0; i < src.num_elements(); ++i)
124  {
125  dst[i] = quantize_qasymm16(src[i], qinfo);
126  }
127  return dst;
128 }
129 
130 template <>
132 {
133  SimpleTensor<int16_t> dst{ src.shape(), DataType::QSYMM16, 1, quantization_info };
134  const UniformQuantizationInfo &qinfo = quantization_info.uniform();
135 
136 #if defined(_OPENMP)
137  #pragma omp parallel for
138 #endif /* _OPENMP */
139  for(int i = 0; i < src.num_elements(); ++i)
140  {
141  dst[i] = quantize_qsymm16(src[i], qinfo);
142  }
143  return dst;
144 }
145 
146 template <>
148 {
149  const UniformQuantizationInfo &quantization_info = src.quantization_info().uniform();
151 
152 #if defined(_OPENMP)
153  #pragma omp parallel for
154 #endif /* _OPENMP */
155  for(int i = 0; i < src.num_elements(); ++i)
156  {
157  dst[i] = dequantize_qsymm16(src[i], quantization_info);
158  }
159  return dst;
160 }
161 
162 template <typename T>
164 {
165  ARM_COMPUTE_ERROR_ON(a.shape()[0] != b.shape()[1]);
166  ARM_COMPUTE_ERROR_ON(a.shape()[1] != out.shape()[1]);
167  ARM_COMPUTE_ERROR_ON(b.shape()[0] != out.shape()[0]);
168 
169  const int M = a.shape()[1]; // Rows
170  const int N = b.shape()[0]; // Cols
171  const int K = b.shape()[1];
172 
173 #if defined(_OPENMP)
174  #pragma omp parallel for collapse(2)
175 #endif /* _OPENMP */
176  for(int y = 0; y < M; ++y)
177  {
178  for(int x = 0; x < N; ++x)
179  {
180  float acc = 0.0f;
181  for(int k = 0; k < K; ++k)
182  {
183  acc += a[y * K + k] * b[x + k * N];
184  }
185 
186  out[x + y * N] = acc;
187  }
188  }
189 }
190 
191 template <typename T>
193 {
194  ARM_COMPUTE_ERROR_ON((in.shape()[0] != out.shape()[1]) || (in.shape()[1] != out.shape()[0]));
195 
196  const int width = in.shape()[0];
197  const int height = in.shape()[1];
198 
199 #if defined(_OPENMP)
200  #pragma omp parallel for collapse(2)
201 #endif /* _OPENMP */
202  for(int y = 0; y < height; ++y)
203  {
204  for(int x = 0; x < width; ++x)
205  {
206  const T val = in[x + y * width];
207 
208  out[x * height + y] = val;
209  }
210  }
211 }
212 
213 template <typename T>
214 void get_tile(const SimpleTensor<T> &in, SimpleTensor<T> &tile, const Coordinates &coord)
215 {
216  ARM_COMPUTE_ERROR_ON(tile.shape().num_dimensions() > 2);
217 
218  const int w_tile = tile.shape()[0];
219  const int h_tile = tile.shape()[1];
220 
221  // Fill the tile with zeros
222  std::fill(tile.data() + 0, (tile.data() + (w_tile * h_tile)), static_cast<T>(0));
223 
224  // Check if with the dimensions greater than 2 we could have out-of-bound reads
225  for(size_t d = 2; d < Coordinates::num_max_dimensions; ++d)
226  {
227  if(coord[d] < 0 || coord[d] >= static_cast<int>(in.shape()[d]))
228  {
229  ARM_COMPUTE_ERROR("coord[d] < 0 || coord[d] >= in.shape()[d] with d >= 2");
230  }
231  }
232 
233  // Since we could have out-of-bound reads along the X and Y dimensions,
234  // we start calculating the input address with x = 0 and y = 0
235  Coordinates start_coord = coord;
236  start_coord[0] = 0;
237  start_coord[1] = 0;
238 
239  // Get input and roi pointers
240  auto in_ptr = static_cast<const T *>(in(start_coord));
241  auto roi_ptr = static_cast<T *>(tile.data());
242 
243  const int x_in_start = std::max(0, coord[0]);
244  const int y_in_start = std::max(0, coord[1]);
245  const int x_in_end = std::min(static_cast<int>(in.shape()[0]), coord[0] + w_tile);
246  const int y_in_end = std::min(static_cast<int>(in.shape()[1]), coord[1] + h_tile);
247 
248  // Number of elements to copy per row
249  const int n = x_in_end - x_in_start;
250 
251  // Starting coordinates for the ROI
252  const int x_tile_start = coord[0] > 0 ? 0 : std::abs(coord[0]);
253  const int y_tile_start = coord[1] > 0 ? 0 : std::abs(coord[1]);
254 
255  // Update input pointer
256  in_ptr += x_in_start;
257  in_ptr += (y_in_start * in.shape()[0]);
258 
259  // Update ROI pointer
260  roi_ptr += x_tile_start;
261  roi_ptr += (y_tile_start * tile.shape()[0]);
262 
263  for(int y = y_in_start; y < y_in_end; ++y)
264  {
265  // Copy per row
266  std::copy(in_ptr, in_ptr + n, roi_ptr);
267 
268  in_ptr += in.shape()[0];
269  roi_ptr += tile.shape()[0];
270  }
271 }
272 
273 template <typename T>
274 void zeros(SimpleTensor<T> &in, const Coordinates &anchor, const TensorShape &shape)
275 {
277  ARM_COMPUTE_ERROR_ON(in.shape().num_dimensions() > 2);
279 
280  // Check if with the dimensions greater than 2 we could have out-of-bound reads
281  for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
282  {
283  if(anchor[d] < 0 || ((anchor[d] + shape[d]) > in.shape()[d]))
284  {
285  ARM_COMPUTE_ERROR("anchor[d] < 0 || (anchor[d] + shape[d]) > in.shape()[d]");
286  }
287  }
288 
289  // Get input pointer
290  auto in_ptr = static_cast<T *>(in(anchor[0] + anchor[1] * in.shape()[0]));
291 
292  const unsigned int n = in.shape()[0];
293 
294  for(unsigned int y = 0; y < shape[1]; ++y)
295  {
296  std::fill(in_ptr, in_ptr + shape[0], 0);
297  in_ptr += n;
298  }
299 }
300 
301 std::pair<int, int> get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max)
302 {
303  ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max");
304 
305  const int min_bound = quantize_qasymm8(min, quant_info.uniform());
306  const int max_bound = quantize_qasymm8(max, quant_info.uniform());
307  return std::pair<int, int> { min_bound, max_bound };
308 }
309 
310 std::pair<int, int> get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max)
311 {
312  ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max");
313 
314  const int min_bound = quantize_qasymm8_signed(min, quant_info.uniform());
315  const int max_bound = quantize_qasymm8_signed(max, quant_info.uniform());
316  return std::pair<int, int> { min_bound, max_bound };
317 }
318 
319 std::pair<int, int> get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id)
320 {
321  ARM_COMPUTE_ERROR_ON_MSG(min > max, "min must be lower equal than max");
322 
323  const int min_bound = quantize_qsymm8_per_channel(min, quant_info, channel_id);
324  const int max_bound = quantize_qsymm8_per_channel(max, quant_info, channel_id);
325  return std::pair<int, int> { min_bound, max_bound };
326 }
327 
328 void add_padding_x(std::initializer_list<ITensor *> tensors, const DataLayout &data_layout, bool only_right_pad)
329 {
330  if(data_layout == DataLayout::NHWC)
331  {
332  constexpr unsigned int lower = 1U;
333  constexpr unsigned int upper = 16U;
334 
335  std::uniform_int_distribution<unsigned int> distribution(lower, upper);
336  size_t seed_offset = 0;
337 
338  for(ITensor *tensor : tensors)
339  {
340  ARM_COMPUTE_ERROR_ON(!tensor->info()->is_resizable());
341 
342  std::mt19937 gen(library->seed() + seed_offset++);
343 
344  const unsigned int right = distribution(gen);
345  const unsigned int left = only_right_pad ? 0 : distribution(gen);
346 
347  tensor->info()->extend_padding(PaddingSize(0U, right, 0U, left));
348  }
349  }
350 }
351 
352 void add_padding_y(std::initializer_list<ITensor *> tensors, const DataLayout &data_layout)
353 {
354  if(data_layout == DataLayout::NHWC)
355  {
356  constexpr unsigned int lower = 1U;
357  constexpr unsigned int upper = 4U;
358 
359  std::uniform_int_distribution<unsigned int> distribution(lower, upper);
360  size_t seed_offset = 0;
361 
362  for(ITensor *tensor : tensors)
363  {
364  ARM_COMPUTE_ERROR_ON(!tensor->info()->is_resizable());
365 
366  std::mt19937 gen(library->seed() + seed_offset++);
367 
368  const unsigned int top = distribution(gen);
369  const unsigned int bottom = distribution(gen);
370 
371  tensor->info()->extend_padding(PaddingSize(top, 0U, bottom, 0U));
372  }
373  }
374 }
375 
376 template void get_tile(const SimpleTensor<float> &in, SimpleTensor<float> &roi, const Coordinates &coord);
377 template void get_tile(const SimpleTensor<half> &in, SimpleTensor<half> &roi, const Coordinates &coord);
378 template void get_tile(const SimpleTensor<int> &in, SimpleTensor<int> &roi, const Coordinates &coord);
379 template void get_tile(const SimpleTensor<short> &in, SimpleTensor<short> &roi, const Coordinates &coord);
380 template void get_tile(const SimpleTensor<char> &in, SimpleTensor<char> &roi, const Coordinates &coord);
381 template void zeros(SimpleTensor<float> &in, const Coordinates &anchor, const TensorShape &shape);
382 template void zeros(SimpleTensor<half> &in, const Coordinates &anchor, const TensorShape &shape);
383 template void transpose_matrix(const SimpleTensor<float> &in, SimpleTensor<float> &out);
384 template void transpose_matrix(const SimpleTensor<half> &in, SimpleTensor<half> &out);
385 template void transpose_matrix(const SimpleTensor<int> &in, SimpleTensor<int> &out);
386 template void transpose_matrix(const SimpleTensor<short> &in, SimpleTensor<short> &out);
387 template void transpose_matrix(const SimpleTensor<char> &in, SimpleTensor<char> &out);
388 template void matrix_multiply(const SimpleTensor<float> &a, const SimpleTensor<float> &b, SimpleTensor<float> &out);
389 template void matrix_multiply(const SimpleTensor<half> &a, const SimpleTensor<half> &b, SimpleTensor<half> &out);
390 
391 } // namespace validation
392 } // namespace test
393 } // namespace arm_compute
std::pair< int, int > get_quantized_bounds(const QuantizationInfo &quant_info, float min, float max)
Helper function to compute quantized min and max bounds.
Definition: Helpers.cpp:301
int16_t quantize_qsymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 16-bit symmetric quantization scheme.
Shape of a tensor.
Definition: TensorShape.h:39
quantized, symmetric fixed-point 16-bit number
float dequantize_qasymm8(uint8_t value, const INFO_TYPE &qinfo)
Dequantize a value given an unsigned 8-bit asymmetric quantization scheme.
SimpleTensor< float > b
Definition: DFT.cpp:157
uint8_t quantize_qasymm8(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given an unsigned 8-bit asymmetric quantization scheme.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void zeros(SimpleTensor< T > &in, const Coordinates &anchor, const TensorShape &shape)
Fill with zeros the input tensor in the area defined by anchor and shape.
Definition: Helpers.cpp:274
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
const DataLayout data_layout
Definition: Im2Col.cpp:151
SimpleTensor< float > convert_from_asymmetric(const SimpleTensor< uint8_t > &src)
Definition: Helpers.cpp:36
Quantization info when assuming per layer quantization.
quantized, asymmetric fixed-point 16-bit number
unsigned int M
void transpose_matrix(const SimpleTensor< T > &in, SimpleTensor< T > &out)
Transpose matrix.
Definition: Helpers.cpp:192
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
void add_padding_x(std::initializer_list< ITensor *> tensors, const DataLayout &data_layout, bool only_right_pad)
Add random padding along the X axis (between 1 and 16 columns per side) to all the input tensors...
Definition: Helpers.cpp:328
SimpleTensor< T > copy(const SimpleTensor< T > &src, const TensorShape &output_shape)
Definition: Copy.cpp:37
Interface for CPU tensor.
Definition: ITensor.h:36
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
float dequantize_qasymm16(uint16_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 16-bit asymmetric quantization scheme.
Quantization information.
library fill(src, distribution, 0)
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:76
int8_t quantize_qasymm8_signed(float value, const INFO_TYPE &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a signed 8-bit asymmetric quantization scheme.
float dequantize_qsymm16(int16_t value, const UniformQuantizationInfo &qinfo)
Dequantize a value given a 16-bit symmetric quantization scheme.
quantized, asymmetric fixed-point 8-bit number unsigned
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Coordinates of an item.
Definition: Coordinates.h:37
unsigned int N
UniformQuantizationInfo uniform() const
Return per layer quantization info.
DataLayout data_layout() const override
Data layout of the tensor.
Definition: SimpleTensor.h:351
std::uniform_real_distribution< float > distribution(-5.f, 5.f)
BorderSize PaddingSize
Container for 2D padding size.
Definition: Types.h:379
SimpleTensor< T > tile(const SimpleTensor< T > &src, const Multiples &multiples)
Definition: Tile.cpp:38
SimpleTensor< uint8_t > convert_to_asymmetric(const SimpleTensor< float > &src, const QuantizationInfo &quantization_info)
Convert float simple tensor into quantized using specified quantization information.
Definition: Helpers.cpp:83
SimpleTensor< int16_t > convert_to_symmetric(const SimpleTensor< float > &src, const QuantizationInfo &quantization_info)
Convert float simple tensor into quantized using specified quantization information.
Definition: Helpers.cpp:131
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
std::pair< int, int > get_quantized_qasymm8_signed_bounds(const QuantizationInfo &quant_info, float min, float max)
Helper function to compute asymmetric quantized signed min and max bounds.
Definition: Helpers.cpp:310
void get_tile(const SimpleTensor< T > &in, SimpleTensor< T > &tile, const Coordinates &coord)
Get a 2D tile from a tensor.
Definition: Helpers.cpp:214
int8_t quantize_qsymm8_per_channel(float value, const QuantizationInfo &qinfo, size_t channel_id=0)
Quantize a value given a 8-bit symmetric per channel quantization scheme.
const QuantizationInfo qinfo
Definition: Im2Col.cpp:155
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
Num samples, height, width, channels.
std::pair< int, int > get_symm_quantized_per_channel_bounds(const QuantizationInfo &quant_info, float min, float max, size_t channel_id)
Helper function to compute symmetric quantized min and max bounds.
Definition: Helpers.cpp:319
int num_elements() const override
Number of elements of the tensor.
Definition: SimpleTensor.h:406
float dequantize_qasymm8_signed(int8_t value, const INFO_TYPE &qinfo)
Dequantize a value given a signed 8-bit asymmetric quantization scheme.
quantized, asymmetric fixed-point 8-bit number signed
void matrix_multiply(const SimpleTensor< T > &a, const SimpleTensor< T > &b, SimpleTensor< T > &out)
Matrix multiply between 2 float simple tensors.
Definition: Helpers.cpp:163
QuantizationInfo quantization_info() const override
Quantization info in case of asymmetric quantized type.
Definition: SimpleTensor.h:332
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
DataLayout
[DataLayout enum definition]
Definition: Types.h:111
SimpleTensor< float > convert_from_symmetric(const SimpleTensor< int16_t > &src)
Definition: Helpers.cpp:147
uint16_t quantize_qasymm16(float value, const UniformQuantizationInfo &qinfo, RoundingPolicy rounding_policy=RoundingPolicy::TO_NEAREST_UP)
Quantize a value given a 16-bit asymmetric quantization scheme.
void add_padding_y(std::initializer_list< ITensor *> tensors, const DataLayout &data_layout)
Add random padding along the Y axis (between 1 and 4 rows per side) to all the input tensors...
Definition: Helpers.cpp:352
const T * data() const
Constant pointer to the underlying buffer.
Definition: SimpleTensor.h:418
unsigned int K