Compute Library
 21.05
Utils.h
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  */
24 #ifndef ARM_COMPUTE_TEST_UTILS_H
25 #define ARM_COMPUTE_TEST_UTILS_H
26 
28 #include "arm_compute/core/Error.h"
32 #include "arm_compute/core/Types.h"
33 #include "support/StringSupport.h"
35 
36 #ifdef ARM_COMPUTE_CL
39 #endif /* ARM_COMPUTE_CL */
40 
41 #include <cmath>
42 #include <cstddef>
43 #include <limits>
44 #include <memory>
45 #include <random>
46 #include <sstream>
47 #include <string>
48 #include <type_traits>
49 #include <vector>
50 
53 
54 namespace arm_compute
55 {
56 #ifdef ARM_COMPUTE_CL
57 class CLTensor;
58 #endif /* ARM_COMPUTE_CL */
59 namespace test
60 {
61 /** Round floating-point value with half value rounding to positive infinity.
62  *
63  * @param[in] value floating-point value to be rounded.
64  *
65  * @return Floating-point value of rounded @p value.
66  */
67 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
68 inline T round_half_up(T value)
69 {
70  return std::floor(value + 0.5f);
71 }
72 
73 /** Round floating-point value with half value rounding to nearest even.
74  *
75  * @param[in] value floating-point value to be rounded.
76  * @param[in] epsilon precision.
77  *
78  * @return Floating-point value of rounded @p value.
79  */
80 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
82 {
83  T positive_value = std::abs(value);
84  T ipart = 0;
85  std::modf(positive_value, &ipart);
86  // If 'value' is exactly halfway between two integers
87  if(std::abs(positive_value - (ipart + 0.5f)) < epsilon)
88  {
89  // If 'ipart' is even then return 'ipart'
90  if(std::fmod(ipart, 2.f) < epsilon)
91  {
92  return support::cpp11::copysign(ipart, value);
93  }
94  // Else return the nearest even integer
95  return support::cpp11::copysign(std::ceil(ipart + 0.5f), value);
96  }
97  // Otherwise use the usual round to closest
98  return support::cpp11::copysign(support::cpp11::round(positive_value), value);
99 }
100 
101 namespace traits
102 {
103 // *INDENT-OFF*
104 // clang-format off
105 /** Promote a type */
106 template <typename T> struct promote { };
107 /** Promote uint8_t to uint16_t */
108 template <> struct promote<uint8_t> { using type = uint16_t; /**< Promoted type */ };
109 /** Promote int8_t to int16_t */
110 template <> struct promote<int8_t> { using type = int16_t; /**< Promoted type */ };
111 /** Promote uint16_t to uint32_t */
112 template <> struct promote<uint16_t> { using type = uint32_t; /**< Promoted type */ };
113 /** Promote int16_t to int32_t */
114 template <> struct promote<int16_t> { using type = int32_t; /**< Promoted type */ };
115 /** Promote uint32_t to uint64_t */
116 template <> struct promote<uint32_t> { using type = uint64_t; /**< Promoted type */ };
117 /** Promote int32_t to int64_t */
118 template <> struct promote<int32_t> { using type = int64_t; /**< Promoted type */ };
119 /** Promote float to float */
120 template <> struct promote<float> { using type = float; /**< Promoted type */ };
121 /** Promote half to half */
122 template <> struct promote<half> { using type = half; /**< Promoted type */ };
123 
124 /** Get promoted type */
125 template <typename T>
126 using promote_t = typename promote<T>::type;
127 
128 template <typename T>
129 using make_signed_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_signed<T>, std::common_type<T>>::type;
130 
131 template <typename T>
132 using make_unsigned_conditional_t = typename std::conditional<std::is_integral<T>::value, std::make_unsigned<T>, std::common_type<T>>::type;
133 
134 // clang-format on
135 // *INDENT-ON*
136 }
137 
138 /** Look up the format corresponding to a channel.
139  *
140  * @param[in] channel Channel type.
141  *
142  * @return Format that contains the given channel.
143  */
145 {
146  switch(channel)
147  {
148  case Channel::R:
149  case Channel::G:
150  case Channel::B:
151  return Format::RGB888;
152  default:
153  throw std::runtime_error("Unsupported channel");
154  }
155 }
156 
157 /** Return the format of a channel.
158  *
159  * @param[in] channel Channel type.
160  *
161  * @return Format of the given channel.
162  */
164 {
165  switch(channel)
166  {
167  case Channel::R:
168  case Channel::G:
169  case Channel::B:
170  return Format::U8;
171  default:
172  throw std::runtime_error("Unsupported channel");
173  }
174 }
175 
176 /** Base case of foldl.
177  *
178  * @return value.
179  */
180 template <typename F, typename T>
181 inline T foldl(F &&, const T &value)
182 {
183  return value;
184 }
185 
186 /** Base case of foldl.
187  *
188  * @return func(value1, value2).
189  */
190 template <typename F, typename T, typename U>
191 inline auto foldl(F &&func, T &&value1, U &&value2) -> decltype(func(value1, value2))
192 {
193  return func(value1, value2);
194 }
195 
196 /** Fold left.
197  *
198  * @param[in] func Binary function to be called.
199  * @param[in] initial Initial value.
200  * @param[in] value Argument passed to the function.
201  * @param[in] values Remaining arguments.
202  */
203 template <typename F, typename I, typename T, typename... Vs>
204 inline I foldl(F &&func, I &&initial, T &&value, Vs &&... values)
205 {
206  return foldl(std::forward<F>(func), func(std::forward<I>(initial), std::forward<T>(value)), std::forward<Vs>(values)...);
207 }
208 
209 /** Create a valid region based on tensor shape, border mode and border size
210  *
211  * @param[in] a_shape Shape used as size of the valid region.
212  * @param[in] border_undefined (Optional) Boolean indicating if the border mode is undefined.
213  * @param[in] border_size (Optional) Border size used to specify the region to exclude.
214  *
215  * @return A valid region starting at (0, 0, ...) with size of @p shape if @p border_undefined is false; otherwise
216  * return A valid region starting at (@p border_size.left, @p border_size.top, ...) with reduced size of @p shape.
217  */
218 inline ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined = false, BorderSize border_size = BorderSize(0))
219 {
220  ValidRegion valid_region{ Coordinates(), a_shape };
221 
222  Coordinates &anchor = valid_region.anchor;
224 
225  if(border_undefined)
226  {
227  ARM_COMPUTE_ERROR_ON(shape.num_dimensions() < 2);
228 
229  anchor.set(0, border_size.left);
230  anchor.set(1, border_size.top);
231 
232  const int valid_shape_x = std::max(0, static_cast<int>(shape.x()) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right));
233  const int valid_shape_y = std::max(0, static_cast<int>(shape.y()) - static_cast<int>(border_size.top) - static_cast<int>(border_size.bottom));
234 
235  shape.set(0, valid_shape_x);
236  shape.set(1, valid_shape_y);
237  }
238 
239  return valid_region;
240 }
241 
242 /** Write the value after casting the pointer according to @p data_type.
243  *
244  * @warning The type of the value must match the specified data type.
245  *
246  * @param[out] ptr Pointer to memory where the @p value will be written.
247  * @param[in] value Value that will be written.
248  * @param[in] data_type Data type that will be written.
249  */
250 template <typename T>
252 {
253  switch(data_type)
254  {
255  case DataType::U8:
256  case DataType::QASYMM8:
257  *reinterpret_cast<uint8_t *>(ptr) = value;
258  break;
259  case DataType::S8:
261  case DataType::QSYMM8:
263  *reinterpret_cast<int8_t *>(ptr) = value;
264  break;
265  case DataType::U16:
266  case DataType::QASYMM16:
267  *reinterpret_cast<uint16_t *>(ptr) = value;
268  break;
269  case DataType::S16:
270  case DataType::QSYMM16:
271  *reinterpret_cast<int16_t *>(ptr) = value;
272  break;
273  case DataType::U32:
274  *reinterpret_cast<uint32_t *>(ptr) = value;
275  break;
276  case DataType::S32:
277  *reinterpret_cast<int32_t *>(ptr) = value;
278  break;
279  case DataType::U64:
280  *reinterpret_cast<uint64_t *>(ptr) = value;
281  break;
282  case DataType::S64:
283  *reinterpret_cast<int64_t *>(ptr) = value;
284  break;
285  case DataType::BFLOAT16:
286  *reinterpret_cast<bfloat16 *>(ptr) = bfloat16(value);
287  break;
288  case DataType::F16:
289  *reinterpret_cast<half *>(ptr) = value;
290  break;
291  case DataType::F32:
292  *reinterpret_cast<float *>(ptr) = value;
293  break;
294  case DataType::F64:
295  *reinterpret_cast<double *>(ptr) = value;
296  break;
297  case DataType::SIZET:
298  *reinterpret_cast<size_t *>(ptr) = value;
299  break;
300  default:
301  ARM_COMPUTE_ERROR("NOT SUPPORTED!");
302  }
303 }
304 
305 /** Saturate a value of type T against the numeric limits of type U.
306  *
307  * @param[in] val Value to be saturated.
308  *
309  * @return saturated value.
310  */
311 template <typename U, typename T>
313 {
314  if(val > static_cast<T>(std::numeric_limits<U>::max()))
315  {
316  val = static_cast<T>(std::numeric_limits<U>::max());
317  }
318  if(val < static_cast<T>(std::numeric_limits<U>::lowest()))
319  {
320  val = static_cast<T>(std::numeric_limits<U>::lowest());
321  }
322  return val;
323 }
324 
325 /** Find the signed promoted common type.
326  */
327 template <typename... T>
329 {
330  /** Common type */
331  using common_type = typename std::common_type<T...>::type;
332  /** Promoted type */
334  /** Intermediate type */
336 };
337 
338 /** Find the unsigned promoted common type.
339  */
340 template <typename... T>
342 {
343  /** Common type */
344  using common_type = typename std::common_type<T...>::type;
345  /** Promoted type */
347  /** Intermediate type */
349 };
350 
351 /** Convert a linear index into n-dimensional coordinates.
352  *
353  * @param[in] shape Shape of the n-dimensional tensor.
354  * @param[in] index Linear index specifying the i-th element.
355  *
356  * @return n-dimensional coordinates.
357  */
358 inline Coordinates index2coord(const TensorShape &shape, int index)
359 {
360  int num_elements = shape.total_size();
361 
362  ARM_COMPUTE_ERROR_ON_MSG(index < 0 || index >= num_elements, "Index has to be in [0, num_elements]");
363  ARM_COMPUTE_ERROR_ON_MSG(num_elements == 0, "Cannot create coordinate from empty shape");
364 
365  Coordinates coord{ 0 };
366 
367  for(int d = shape.num_dimensions() - 1; d >= 0; --d)
368  {
369  num_elements /= shape[d];
370  coord.set(d, index / num_elements);
371  index %= num_elements;
372  }
373 
374  return coord;
375 }
376 
377 /** Linearise the given coordinate.
378  *
379  * Transforms the given coordinate into a linear offset in terms of
380  * elements.
381  *
382  * @param[in] shape Shape of the n-dimensional tensor.
383  * @param[in] coord The to be converted coordinate.
384  *
385  * @return Linear offset to the element.
386  */
387 inline int coord2index(const TensorShape &shape, const Coordinates &coord)
388 {
389  ARM_COMPUTE_ERROR_ON_MSG(shape.total_size() == 0, "Cannot get index from empty shape");
390  ARM_COMPUTE_ERROR_ON_MSG(coord.num_dimensions() == 0, "Cannot get index of empty coordinate");
391 
392  int index = 0;
393  int dim_size = 1;
394 
395  for(unsigned int i = 0; i < coord.num_dimensions(); ++i)
396  {
397  index += coord[i] * dim_size;
398  dim_size *= shape[i];
399  }
400 
401  return index;
402 }
403 
404 /** Check if a coordinate is within a valid region */
406 {
407  for(size_t d = 0; d < Coordinates::num_max_dimensions; ++d)
408  {
409  if(coord[d] < valid_region.start(d) || coord[d] >= valid_region.end(d))
410  {
411  return false;
412  }
413  }
414 
415  return true;
416 }
417 
418 /** Create and initialize a tensor of the given type.
419  *
420  * @param[in] info Tensor information to be used to create the tensor
421  * @param[in] ctx (Optional) Pointer to the runtime context.
422  *
423  * @return Initialized tensor of given type.
424  */
425 template <typename T>
426 inline T create_tensor(const TensorInfo &info, IRuntimeContext *ctx = nullptr)
427 {
428  T tensor(ctx);
429  tensor.allocator()->init(info);
430  return tensor;
431 }
432 
433 /** Create and initialize a tensor of the given type.
434  *
435  * @param[in] shape Tensor shape.
436  * @param[in] data_type Data type.
437  * @param[in] num_channels (Optional) Number of channels.
438  * @param[in] quantization_info (Optional) Quantization info for asymmetric quantized types.
439  * @param[in] data_layout (Optional) Data layout. Default is NCHW.
440  * @param[in] ctx (Optional) Pointer to the runtime context.
441  *
442  * @return Initialized tensor of given type.
443  */
444 template <typename T>
445 inline T create_tensor(const TensorShape &shape, DataType data_type, int num_channels = 1,
447 {
448  T tensor(ctx);
449  TensorInfo info(shape, num_channels, data_type);
450  info.set_quantization_info(quantization_info);
451  info.set_data_layout(data_layout);
452 
453  return create_tensor<T>(info, ctx);
454 }
455 
456 /** Create and initialize a tensor of the given type.
457  *
458  * @param[in] shape Tensor shape.
459  * @param[in] format Format type.
460  * @param[in] ctx (Optional) Pointer to the runtime context.
461  *
462  * @return Initialized tensor of given type.
463  */
464 template <typename T>
465 inline T create_tensor(const TensorShape &shape, Format format, IRuntimeContext *ctx = nullptr)
466 {
467  TensorInfo info(shape, format);
468 
469  return create_tensor<T>(info, ctx);
470 }
471 
472 /** Create a vector with a uniform distribution of floating point values across the specified range.
473  *
474  * @param[in] num_values The number of values to be created.
475  * @param[in] min The minimum value in distribution (inclusive).
476  * @param[in] max The maximum value in distribution (inclusive).
477  * @param[in] seed The random seed to be used.
478  *
479  * @return A vector that contains the requested number of random floating point values
480  */
481 template <typename T, typename = typename std::enable_if<std::is_floating_point<T>::value>::type>
482 inline std::vector<T> generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed)
483 {
484  std::vector<T> v(num_values);
485  std::mt19937 gen(seed);
486  std::uniform_real_distribution<T> dist(min, max);
487 
488  for(unsigned int i = 0; i < num_values; ++i)
489  {
490  v.at(i) = dist(gen);
491  }
492 
493  return v;
494 }
495 
496 template <typename T, typename ArrayAccessor_T>
497 inline void fill_array(ArrayAccessor_T &&array, const std::vector<T> &v)
498 {
499  array.resize(v.size());
500  std::memcpy(array.buffer(), v.data(), v.size() * sizeof(T));
501 }
502 
503 /** Obtain numpy type string from DataType.
504  *
505  * @param[in] data_type Data type.
506  *
507  * @return numpy type string.
508  */
509 inline std::string get_typestring(DataType data_type)
510 {
511  // Check endianness
512  const unsigned int i = 1;
513  const char *c = reinterpret_cast<const char *>(&i);
514  std::string endianness;
515  if(*c == 1)
516  {
517  endianness = std::string("<");
518  }
519  else
520  {
521  endianness = std::string(">");
522  }
523  const std::string no_endianness("|");
524 
525  switch(data_type)
526  {
527  case DataType::U8:
528  return no_endianness + "u" + support::cpp11::to_string(sizeof(uint8_t));
529  case DataType::S8:
530  return no_endianness + "i" + support::cpp11::to_string(sizeof(int8_t));
531  case DataType::U16:
532  return endianness + "u" + support::cpp11::to_string(sizeof(uint16_t));
533  case DataType::S16:
534  return endianness + "i" + support::cpp11::to_string(sizeof(int16_t));
535  case DataType::U32:
536  return endianness + "u" + support::cpp11::to_string(sizeof(uint32_t));
537  case DataType::S32:
538  return endianness + "i" + support::cpp11::to_string(sizeof(int32_t));
539  case DataType::U64:
540  return endianness + "u" + support::cpp11::to_string(sizeof(uint64_t));
541  case DataType::S64:
542  return endianness + "i" + support::cpp11::to_string(sizeof(int64_t));
543  case DataType::F32:
544  return endianness + "f" + support::cpp11::to_string(sizeof(float));
545  case DataType::F64:
546  return endianness + "f" + support::cpp11::to_string(sizeof(double));
547  case DataType::SIZET:
548  return endianness + "u" + support::cpp11::to_string(sizeof(size_t));
549  default:
550  ARM_COMPUTE_ERROR("NOT SUPPORTED!");
551  }
552 }
553 
554 /** Sync if necessary.
555  */
556 template <typename TensorType>
557 inline void sync_if_necessary()
558 {
559 #ifdef ARM_COMPUTE_CL
560  if(opencl_is_available() && std::is_same<typename std::decay<TensorType>::type, arm_compute::CLTensor>::value)
561  {
563  }
564 #endif /* ARM_COMPUTE_CL */
565 }
566 
567 /** Sync tensor if necessary.
568  *
569  * @note: If the destination tensor not being used on OpenGL ES, GPU will optimize out the operation.
570  *
571  * @param[in] tensor Tensor to be sync.
572  */
573 template <typename TensorType>
575 {
576  ARM_COMPUTE_UNUSED(tensor);
577 }
578 
579 /** Construct and return object for dimensions' state filled with the given value
580  *
581  * @param[in] value The value to fill
582  *
583  * @return Constructed class
584  */
586 {
587  auto states = ITensorInfo::TensorDimsState{};
588  std::fill(states.begin(), states.end(), value);
589  return states;
590 }
591 
592 /** Construct and return object for dimensions' state filled with the value for dynamic state
593  *
594  * @return Constructed class filled with the value for dynamic state
595  */
597 {
599 }
600 
601 /** Construct and return object for dimensions' state filled with the value for non-dynamic state
602  *
603  * @return Constructed class filled with the value for non-dynamic state
604  */
606 {
608 }
609 
610 /** Set the dimension states of the given tensor to dynamic
611  *
612  * @param[in] t The tensor to set to dynamic state
613  *
614  */
615 template <typename TensorType>
617 {
618  t.info()->set_tensor_dims_state(construct_dynamic_dims_state());
619 }
620 
621 /** Set the dimension states of the given tensor to state
622  *
623  * @param[in] t The tensor to set to static state
624  *
625  */
626 template <typename TensorType>
628 {
629  t.info()->set_tensor_dims_state(construct_static_dims_state());
630 }
631 } // namespace test
632 } // namespace arm_compute
633 #endif /* ARM_COMPUTE_TEST_UTILS_H */
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:76
typename std::conditional< std::is_integral< T >::value, std::make_signed< T >, std::common_type< T > >::type make_signed_conditional_t
Definition: Utils.h:129
typename traits::make_unsigned_conditional_t< promoted_type >::type intermediate_type
Intermediate type.
Definition: Utils.h:348
Shape of a tensor.
Definition: TensorShape.h:39
ITensorInfo::TensorDimsState construct_dims_state(int32_t value)
Construct and return object for dimensions' state filled with the given value.
Definition: Utils.h:585
quantized, symmetric fixed-point 16-bit number
TensorShape shape
Shape of the valid region.
Definition: Types.h:255
T foldl(F &&, const T &value)
Base case of foldl.
Definition: Utils.h:181
Container for 2D border size.
Definition: Types.h:267
static CLScheduler & get()
Access the scheduler singleton.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
Format get_format_for_channel(Channel channel)
Look up the format corresponding to a channel.
Definition: Utils.h:144
T copysign(T x, T y)
Composes a floating point value with the magnitude of x and the sign of y.
TensorType
Memory type.
Definition: Types.h:38
1 channel, 1 U8 per channel
typename promote< T >::type promote_t
Get promoted type.
Definition: Utils.h:126
std::string to_string(T &&value)
Convert integer and float values to string.
half_float::half half
16-bit floating point type
Definition: Types.h:46
1 channel, 1 F32 per channel
void sync_if_necessary()
Sync if necessary.
Definition: Utils.h:557
#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
quantized, asymmetric fixed-point 16-bit number
1 channel, 1 U16 per channel
const ValidRegion valid_region
Definition: Scale.cpp:223
decltype(strategy::transforms) typedef type
Copyright (c) 2017-2021 Arm Limited.
int coord2index(const TensorShape &shape, const Coordinates &coord)
Linearise the given coordinate.
Definition: Utils.h:387
1 channel, 1 F16 per channel
arm_compute::bfloat16 bfloat16
Definition: bfloat.hpp:30
1 channel, 1 S32 per channel
T round_half_even(T value, T epsilon=std::numeric_limits< T >::epsilon())
Round floating-point value with half value rounding to nearest even.
Definition: Utils.h:81
16-bit brain floating-point number
signed 64-bit number
3 channels, 1 U8 per channel
const DataType data_type
Definition: Im2Col.cpp:150
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
Quantization information.
Format get_channel_format(Channel channel)
Return the format of a channel.
Definition: Utils.h:163
void store_value_with_data_type(void *ptr, T value, DataType data_type)
Write the value after casting the pointer according to data_type.
Definition: Utils.h:251
bool is_in_valid_region(const ValidRegion &valid_region, Coordinates coord)
Check if a coordinate is within a valid region.
Definition: Utils.h:405
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
void sync_tensor_if_necessary(TensorType &tensor)
Sync tensor if necessary.
Definition: Utils.h:574
library fill(src, distribution, 0)
void set_tensor_dynamic(TensorType &t)
Set the dimension states of the given tensor to dynamic.
Definition: Utils.h:616
1 channel, 1 U32 per channel
Channel
Available channels.
Definition: Types.h:440
Format
Image colour formats.
Definition: Types.h:54
quantized, asymmetric fixed-point 8-bit number unsigned
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
Find the signed promoted common type.
Definition: Utils.h:328
int start(unsigned int d) const
Return the start of the valid region for the given dimension d.
Definition: Types.h:228
Coordinates of an item.
Definition: Coordinates.h:37
void fill_array(ArrayAccessor_T &&array, const std::vector< T > &v)
Definition: Utils.h:497
T create_tensor(const TensorInfo &info, IRuntimeContext *ctx=nullptr)
Create and initialize a tensor of the given type.
Definition: Utils.h:426
Coordinates index2coord(const TensorShape &shape, int index)
Convert a linear index into n-dimensional coordinates.
Definition: Utils.h:358
static constexpr int32_t get_static_state_value()
Get the value representing static dimension state.
Definition: ITensorInfo.h:58
1 channel, 1 S16 per channel
std::string get_typestring(DataType data_type)
Obtain numpy type string from DataType.
Definition: Utils.h:509
quantized, symmetric fixed-point 8-bit number
Num samples, channels, height, width.
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.cpp:70
quantized, symmetric per channel fixed-point 8-bit number
Basic pool of threads to execute CPP/Neon code on several cores in parallel.
FloorUKernelPtr func
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
int end(unsigned int d) const
Return the end of the valid region for the given dimension d.
Definition: Types.h:234
traits::promote_t< common_type > promoted_type
Promoted type.
Definition: Utils.h:333
ITensorInfo::TensorDimsState construct_dynamic_dims_state()
Construct and return object for dimensions' state filled with the value for dynamic state.
Definition: Utils.h:596
T round(T value)
Round floating-point value with half value rounding away from zero.
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
typename traits::make_signed_conditional_t< promoted_type >::type intermediate_type
Intermediate type.
Definition: Utils.h:335
T saturate_cast(T val)
Saturate a value of type T against the numeric limits of type U.
Definition: Utils.h:312
Wrapper to configure the Khronos OpenCL C++ header.
traits::promote_t< common_type > promoted_type
Promoted type.
Definition: Utils.h:346
Store the tensor's metadata.
Definition: TensorInfo.h:43
void set_tensor_static(TensorType &t)
Set the dimension states of the given tensor to state.
Definition: Utils.h:627
quantized, asymmetric fixed-point 8-bit number signed
64-bit floating-point number
T round_half_up(T value)
Round floating-point value with half value rounding to positive infinity.
Definition: Utils.h:68
Container for valid region of a window.
Definition: Types.h:182
typename std::conditional< std::is_integral< T >::value, std::make_unsigned< T >, std::common_type< T > >::type make_unsigned_conditional_t
Definition: Utils.h:132
static constexpr size_t num_max_dimensions
Number of dimensions the tensor has.
Definition: Dimensions.h:46
unsigned 64-bit number
DataType
Available data types.
Definition: Types.h:77
Find the unsigned promoted common type.
Definition: Utils.h:341
static constexpr int32_t get_dynamic_state_value()
Get the value representing dynamic dimension state.
Definition: ITensorInfo.h:49
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
signed 8-bit number
std::vector< T > generate_random_real(unsigned int num_values, T min, T max, std::random_device::result_type seed)
Create a vector with a uniform distribution of floating point values across the specified range.
Definition: Utils.h:482
Coordinates anchor
Anchor for the start of the valid region.
Definition: Types.h:254
ValidRegion shape_to_valid_region(const TensorShape &a_shape, bool border_undefined=false, BorderSize border_size=BorderSize(0))
Create a valid region based on tensor shape, border mode and border size.
Definition: Utils.h:218
typename std::common_type< T... >::type common_type
Common type.
Definition: Utils.h:331
ITensorInfo::TensorDimsState construct_static_dims_state()
Construct and return object for dimensions' state filled with the value for non-dynamic state.
Definition: Utils.h:605
typename std::common_type< T... >::type common_type
Common type.
Definition: Utils.h:344
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:154
Basic implementation of the OpenCL tensor interface.
Definition: CLTensor.h:41