Compute Library
 19.08
Validate.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 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_VALIDATE_H__
25 #define __ARM_COMPUTE_VALIDATE_H__
26 
27 #include "arm_compute/core/Error.h"
35 
36 #include <algorithm>
37 
38 namespace arm_compute
39 {
40 namespace detail
41 {
42 /* Check whether two dimension objects differ.
43  *
44  * @param[in] dim1 First object to be compared.
45  * @param[in] dim2 Second object to be compared.
46  * @param[in] upper_dim The dimension from which to check.
47  *
48  * @return Return true if the two objects are different.
49  */
50 template <typename T>
51 inline bool have_different_dimensions(const Dimensions<T> &dim1, const Dimensions<T> &dim2, unsigned int upper_dim)
52 {
53  for(unsigned int i = upper_dim; i < arm_compute::Dimensions<T>::num_max_dimensions; ++i)
54  {
55  if(dim1[i] != dim2[i])
56  {
57  return true;
58  }
59  }
60 
61  return false;
62 }
63 
71 template <typename T>
73 {
74 public:
82  compare_dimension(const Dimensions<T> &dim, const char *function, const char *file, int line)
83  : _dim{ dim }, _function{ function }, _file{ file }, _line{ line }
84  {
85  }
86 
94  {
95  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(have_different_dimensions(_dim, dim, 0), _function, _file, _line,
96  "Objects have different dimensions");
97  return arm_compute::Status{};
98  }
99 
100 private:
101  const Dimensions<T> &_dim;
102  const char *const _function;
103  const char *const _file;
104  const int _line;
105 };
106 
107 template <typename F>
109 {
110  return arm_compute::Status{};
111 }
112 
113 template <typename F, typename T, typename... Ts>
114 inline arm_compute::Status for_each_error(F &&func, T &&arg, Ts &&... args)
115 {
116  ARM_COMPUTE_RETURN_ON_ERROR(func(arg));
118  return arm_compute::Status{};
119 }
120 
122 template <typename T>
125 template <>
127 {
135  {
136  return tensor->info();
137  }
138 };
139 } // namespace detail
140 
150 template <typename... Ts>
151 inline arm_compute::Status error_on_nullptr(const char *function, const char *file, const int line, Ts &&... pointers)
152 {
153  const std::array<const void *, sizeof...(Ts)> pointers_array{ { std::forward<Ts>(pointers)... } };
154  bool has_nullptr = std::any_of(pointers_array.begin(), pointers_array.end(), [&](const void *ptr)
155  {
156  return (ptr == nullptr);
157  });
158  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(has_nullptr, function, file, line, "Nullptr object!");
159  return arm_compute::Status{};
160 }
161 #define ARM_COMPUTE_ERROR_ON_NULLPTR(...) \
162  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_nullptr(__func__, __FILE__, __LINE__, __VA_ARGS__))
163 #define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...) \
164  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(__func__, __FILE__, __LINE__, __VA_ARGS__))
165 
181 arm_compute::Status error_on_mismatching_windows(const char *function, const char *file, const int line,
182  const Window &full, const Window &win);
183 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w) \
184  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_mismatching_windows(__func__, __FILE__, __LINE__, f, w))
185 #define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_WINDOWS(f, w) \
186  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_windows(__func__, __FILE__, __LINE__, f, w))
187 
203 arm_compute::Status error_on_invalid_subwindow(const char *function, const char *file, const int line,
204  const Window &full, const Window &sub);
205 #define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s) \
206  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_invalid_subwindow(__func__, __FILE__, __LINE__, f, s))
207 #define ARM_COMPUTE_RETURN_ERROR_ON_INVALID_SUBWINDOW(f, s) \
208  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_invalid_subwindow(__func__, __FILE__, __LINE__, f, s))
209 
223 arm_compute::Status error_on_window_not_collapsable_at_dimension(const char *function, const char *file, const int line,
224  const Window &full, const Window &window, const int dim);
225 #define ARM_COMPUTE_ERROR_ON_WINDOW_NOT_COLLAPSABLE_AT_DIMENSION(f, w, d) \
226  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_window_not_collapsable_at_dimension(__func__, __FILE__, __LINE__, f, w, d))
227 #define ARM_COMPUTE_RETURN_ERROR_ON_WINDOW_NOT_COLLAPSABLE_AT_DIMENSION(f, w, d) \
228  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_window_not_collapsable_at_dimension(__func__, __FILE__, __LINE__, f, w, d))
229 
242 arm_compute::Status error_on_coordinates_dimensions_gte(const char *function, const char *file, const int line,
243  const Coordinates &pos, unsigned int max_dim);
244 #define ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(p, md) \
245  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_coordinates_dimensions_gte(__func__, __FILE__, __LINE__, p, md))
246 #define ARM_COMPUTE_RETURN_ERROR_ON_COORDINATES_DIMENSIONS_GTE(p, md) \
247  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_coordinates_dimensions_gte(__func__, __FILE__, __LINE__, p, md))
248 
261 arm_compute::Status error_on_window_dimensions_gte(const char *function, const char *file, const int line,
262  const Window &win, unsigned int max_dim);
263 #define ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(w, md) \
264  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_window_dimensions_gte(__func__, __FILE__, __LINE__, w, md))
265 #define ARM_COMPUTE_RETURN_ERROR_ON_WINDOW_DIMENSIONS_GTE(w, md) \
266  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_window_dimensions_gte(__func__, __FILE__, __LINE__, w, md))
267 
279 template <typename T, typename... Ts>
280 arm_compute::Status error_on_mismatching_dimensions(const char *function, const char *file, int line,
281  const Dimensions<T> &dim1, const Dimensions<T> &dim2, Ts &&... dims)
282 {
283  ARM_COMPUTE_RETURN_ON_ERROR(detail::for_each_error(detail::compare_dimension<T>(dim1, function, file, line), dim2, std::forward<Ts>(dims)...));
284  return arm_compute::Status{};
285 }
286 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(...) \
287  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_mismatching_dimensions(__func__, __FILE__, __LINE__, __VA_ARGS__))
288 #define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...) \
289  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_dimensions(__func__, __FILE__, __LINE__, __VA_ARGS__))
290 
302 template <typename... Ts>
303 arm_compute::Status error_on_tensors_not_even(const char *function, const char *file, int line,
304  const Format &format, const ITensor *tensor1, Ts... tensors)
305 {
306  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor1 == nullptr, function, file, line);
307  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward<Ts>(tensors)...));
308  const std::array < const ITensor *, 1 + sizeof...(Ts) > tensors_info_array{ { tensor1, std::forward<Ts>(tensors)... } };
309  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensors_info_array.cbegin(), tensors_info_array.cend(), [&](const ITensor * tensor)
310  {
311  const TensorShape correct_shape = adjust_odd_shape(tensor->info()->tensor_shape(), format);
312  return detail::have_different_dimensions(tensor->info()->tensor_shape(), correct_shape, 2);
313  }),
314  function, file, line, "Tensor shape has odd dimensions");
315  return arm_compute::Status{};
316 }
317 
318 #define ARM_COMPUTE_ERROR_ON_TENSORS_NOT_EVEN(...) \
319  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_tensors_not_even(__func__, __FILE__, __LINE__, __VA_ARGS__))
320 #define ARM_COMPUTE_RETURN_ERROR_ON_TENSORS_NOT_EVEN(...) \
321  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_tensors_not_even(__func__, __FILE__, __LINE__, __VA_ARGS__))
322 
335 template <typename... Ts>
336 arm_compute::Status error_on_tensors_not_subsampled(const char *function, const char *file, int line,
337  const Format &format, const TensorShape &shape, const ITensor *tensor1, Ts... tensors)
338 {
339  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor1 == nullptr, function, file, line);
340  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward<Ts>(tensors)...));
341  const TensorShape sub2_shape = calculate_subsampled_shape(shape, format);
342  const std::array < const ITensor *, 1 + sizeof...(Ts) > tensors_info_array{ { tensor1, std::forward<Ts>(tensors)... } };
343  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensors_info_array.cbegin(), tensors_info_array.cend(), [&](const ITensor * tensor)
344  {
345  return detail::have_different_dimensions(tensor->info()->tensor_shape(), sub2_shape, 2);
346  }),
347  function, file, line, "Tensor shape has mismatch dimensions for sub-sampling");
348  return arm_compute::Status{};
349 }
350 
351 #define ARM_COMPUTE_ERROR_ON_TENSORS_NOT_SUBSAMPLED(...) \
352  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_tensors_not_subsampled(__func__, __FILE__, __LINE__, __VA_ARGS__))
353 #define ARM_COMPUTE_RETURN_ERROR_ON_TENSORS_NOT_SUBSAMPLED(...) \
354  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_tensors_not_subsampled(__func__, __FILE__, __LINE__, __VA_ARGS__))
355 
367 template <typename... Ts>
368 inline arm_compute::Status error_on_mismatching_shapes(const char *function, const char *file, const int line,
369  const ITensorInfo *tensor_info_1, const ITensorInfo *tensor_info_2, Ts... tensor_infos)
370 {
371  return error_on_mismatching_shapes(function, file, line, 0U, tensor_info_1, tensor_info_2, std::forward<Ts>(tensor_infos)...);
372 }
384 template <typename... Ts>
385 inline arm_compute::Status error_on_mismatching_shapes(const char *function, const char *file, const int line,
386  const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors)
387 {
388  return error_on_mismatching_shapes(function, file, line, 0U, tensor_1, tensor_2, std::forward<Ts>(tensors)...);
389 }
402 template <typename... Ts>
403 inline arm_compute::Status error_on_mismatching_shapes(const char *function, const char *file, const int line,
404  unsigned int upper_dim, const ITensorInfo *tensor_info_1, const ITensorInfo *tensor_info_2, Ts... tensor_infos)
405 {
406  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info_1 == nullptr, function, file, line);
407  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info_2 == nullptr, function, file, line);
408  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward<Ts>(tensor_infos)...));
409 
410  const std::array < const ITensorInfo *, 2 + sizeof...(Ts) > tensors_info_array{ { tensor_info_1, tensor_info_2, std::forward<Ts>(tensor_infos)... } };
411  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(std::next(tensors_info_array.cbegin()), tensors_info_array.cend(), [&](const ITensorInfo * tensor_info)
412  {
413  return detail::have_different_dimensions((*tensors_info_array.cbegin())->tensor_shape(), tensor_info->tensor_shape(), upper_dim);
414  }),
415  function, file, line, "Tensors have different shapes");
416  return arm_compute::Status{};
417 }
430 template <typename... Ts>
431 inline arm_compute::Status error_on_mismatching_shapes(const char *function, const char *file, const int line,
432  unsigned int upper_dim, const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors)
433 {
434  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_1 == nullptr, function, file, line);
435  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_2 == nullptr, function, file, line);
436  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward<Ts>(tensors)...));
437  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_shapes(function, file, line, upper_dim, tensor_1->info(), tensor_2->info(),
439  return arm_compute::Status{};
440 }
441 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...) \
442  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_mismatching_shapes(__func__, __FILE__, __LINE__, __VA_ARGS__))
443 #define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...) \
444  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_shapes(__func__, __FILE__, __LINE__, __VA_ARGS__))
445 
456 template <typename... Ts>
457 inline arm_compute::Status error_on_mismatching_data_layouts(const char *function, const char *file, const int line,
458  const ITensorInfo *tensor_info, Ts... tensor_infos)
459 {
460  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line);
461  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward<Ts>(tensor_infos)...));
462 
463  DataLayout &&tensor_data_layout = tensor_info->data_layout();
464  const std::array<const ITensorInfo *, sizeof...(Ts)> tensors_infos_array{ { std::forward<Ts>(tensor_infos)... } };
465  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensors_infos_array.begin(), tensors_infos_array.end(), [&](const ITensorInfo * tensor_info_obj)
466  {
467  return tensor_info_obj->data_layout() != tensor_data_layout;
468  }),
469  function, file, line, "Tensors have different data layouts");
470  return arm_compute::Status{};
471 }
482 template <typename... Ts>
483 inline arm_compute::Status error_on_mismatching_data_layouts(const char *function, const char *file, const int line,
484  const ITensor *tensor, Ts... tensors)
485 {
486  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line);
487  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward<Ts>(tensors)...));
490  return arm_compute::Status{};
491 }
492 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_LAYOUT(...) \
493  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_mismatching_data_layouts(__func__, __FILE__, __LINE__, __VA_ARGS__))
494 #define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...) \
495  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_data_layouts(__func__, __FILE__, __LINE__, __VA_ARGS__))
496 
507 template <typename... Ts>
508 inline arm_compute::Status error_on_mismatching_data_types(const char *function, const char *file, const int line,
509  const ITensorInfo *tensor_info, Ts... tensor_infos)
510 {
511  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line);
512  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward<Ts>(tensor_infos)...));
513 
514  DataType &&tensor_data_type = tensor_info->data_type();
515  const std::array<const ITensorInfo *, sizeof...(Ts)> tensors_infos_array{ { std::forward<Ts>(tensor_infos)... } };
516  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensors_infos_array.begin(), tensors_infos_array.end(), [&](const ITensorInfo * tensor_info_obj)
517  {
518  return tensor_info_obj->data_type() != tensor_data_type;
519  }),
520  function, file, line, "Tensors have different data types");
521  return arm_compute::Status{};
522 }
533 template <typename... Ts>
534 inline arm_compute::Status error_on_mismatching_data_types(const char *function, const char *file, const int line,
535  const ITensor *tensor, Ts... tensors)
536 {
537  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line);
538  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward<Ts>(tensors)...));
541  return arm_compute::Status{};
542 }
543 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...) \
544  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_mismatching_data_types(__func__, __FILE__, __LINE__, __VA_ARGS__))
545 #define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...) \
546  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_data_types(__func__, __FILE__, __LINE__, __VA_ARGS__))
547 
561 template <typename... Ts>
562 inline arm_compute::Status error_on_mismatching_quantization_info(const char *function, const char *file, const int line,
563  const ITensorInfo *tensor_info_1, const ITensorInfo *tensor_info_2, Ts... tensor_infos)
564 {
565  DataType &&first_data_type = tensor_info_1->data_type();
566  const QuantizationInfo first_quantization_info = tensor_info_1->quantization_info();
567 
568  if(!is_data_type_quantized(first_data_type))
569  {
570  return arm_compute::Status{};
571  }
572 
573  const std::array < const ITensorInfo *, 1 + sizeof...(Ts) > tensor_infos_array{ { tensor_info_2, std::forward<Ts>(tensor_infos)... } };
574  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensor_infos_array.begin(), tensor_infos_array.end(), [&](const ITensorInfo * tensor_info)
575  {
576  return tensor_info->data_type() != first_data_type;
577  }),
578  function, file, line, "Tensors have different asymmetric quantized data types");
579  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensor_infos_array.begin(), tensor_infos_array.end(), [&](const ITensorInfo * tensor_info)
580  {
581  return tensor_info->quantization_info() != first_quantization_info;
582  }),
583  function, file, line, "Tensors have different quantization information");
584 
585  return arm_compute::Status{};
586 }
600 template <typename... Ts>
601 inline arm_compute::Status error_on_mismatching_quantization_info(const char *function, const char *file, const int line,
602  const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors)
603 {
604  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_quantization_info(function, file, line, tensor_1->info(), tensor_2->info(),
606  return arm_compute::Status{};
607 }
608 #define ARM_COMPUTE_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...) \
609  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_mismatching_quantization_info(__func__, __FILE__, __LINE__, __VA_ARGS__))
610 #define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...) \
611  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_quantization_info(__func__, __FILE__, __LINE__, __VA_ARGS__))
612 
622 template <typename T, typename F, typename... Fs>
623 void error_on_format_not_in(const char *function, const char *file, const int line,
624  const T *object, F &&format, Fs &&... formats)
625 {
626  ARM_COMPUTE_ERROR_ON_LOC(object == nullptr, function, file, line);
627 
628  Format &&object_format = object->info()->format();
629  ARM_COMPUTE_UNUSED(object_format);
630 
631  ARM_COMPUTE_ERROR_ON_LOC(object_format == Format::UNKNOWN, function, file, line);
632 
633  const std::array<F, sizeof...(Fs)> formats_array{ { std::forward<Fs>(formats)... } };
634  ARM_COMPUTE_UNUSED(formats_array);
635 
636  ARM_COMPUTE_ERROR_ON_LOC_MSG(object_format != format && std::none_of(formats_array.begin(), formats_array.end(), [&](const F & f)
637  {
638  return f == object_format;
639  }),
640  function, file, line, "Format %s not supported by this kernel", string_from_format(object_format).c_str());
641 }
642 #define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t, ...) ::arm_compute::error_on_format_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__)
643 
655 template <typename T, typename... Ts>
656 inline arm_compute::Status error_on_data_type_not_in(const char *function, const char *file, const int line,
657  const ITensorInfo *tensor_info, T &&dt, Ts &&... dts)
658 {
659  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line);
660 
661  const DataType &tensor_dt = tensor_info->data_type(); //NOLINT
662  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_dt == DataType::UNKNOWN, function, file, line);
663 
664  const std::array<T, sizeof...(Ts)> dts_array{ { std::forward<Ts>(dts)... } };
665  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(tensor_dt != dt && std::none_of(dts_array.begin(), dts_array.end(), [&](const T & d)
666  {
667  return d == tensor_dt;
668  }),
669  function, file, line, "ITensor data type %s not supported by this kernel", string_from_data_type(tensor_dt).c_str());
670  return arm_compute::Status{};
671 }
683 template <typename T, typename... Ts>
684 inline arm_compute::Status error_on_data_type_not_in(const char *function, const char *file, const int line,
685  const ITensor *tensor, T &&dt, Ts &&... dts)
686 {
687  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line);
688  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_not_in(function, file, line, tensor->info(), std::forward<T>(dt), std::forward<Ts>(dts)...));
689  return arm_compute::Status{};
690 }
691 #define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t, ...) \
692  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_data_type_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__))
693 #define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t, ...) \
694  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__))
695 
707 template <typename T, typename... Ts>
708 inline arm_compute::Status error_on_data_layout_not_in(const char *function, const char *file, const int line,
709  const ITensorInfo *tensor_info, T &&dl, Ts &&... dls)
710 {
711  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line);
712 
713  const DataLayout &tensor_dl = tensor_info->data_layout(); //NOLINT
714  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_dl == DataLayout::UNKNOWN, function, file, line);
715 
716  const std::array<T, sizeof...(Ts)> dls_array{ { std::forward<Ts>(dls)... } };
717  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(tensor_dl != dl && std::none_of(dls_array.begin(), dls_array.end(), [&](const T & l)
718  {
719  return l == tensor_dl;
720  }),
721  function, file, line, "ITensor data layout %s not supported by this kernel", string_from_data_layout(tensor_dl).c_str());
722  return arm_compute::Status{};
723 }
735 template <typename T, typename... Ts>
736 inline arm_compute::Status error_on_data_layout_not_in(const char *function, const char *file, const int line,
737  const ITensor *tensor, T &&dl, Ts &&... dls)
738 {
739  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line);
740  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_layout_not_in(function, file, line, tensor->info(), std::forward<T>(dl), std::forward<Ts>(dls)...));
741  return arm_compute::Status{};
742 }
743 #define ARM_COMPUTE_ERROR_ON_DATA_LAYOUT_NOT_IN(t, ...) \
744  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_data_layout_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__))
745 #define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t, ...) \
746  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_layout_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__))
747 
760 template <typename T, typename... Ts>
761 inline arm_compute::Status error_on_data_type_channel_not_in(const char *function, const char *file, const int line,
762  const ITensorInfo *tensor_info, size_t num_channels, T &&dt, Ts &&... dts)
763 {
764  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_not_in(function, file, line, tensor_info, std::forward<T>(dt), std::forward<Ts>(dts)...));
765  const size_t tensor_nc = tensor_info->num_channels();
766  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(tensor_nc != num_channels, function, file, line, "Number of channels %d. Required number of channels %d", tensor_nc, num_channels);
767  return arm_compute::Status{};
768 }
781 template <typename T, typename... Ts>
782 inline arm_compute::Status error_on_data_type_channel_not_in(const char *function, const char *file, const int line,
783  const ITensor *tensor, size_t num_channels, T &&dt, Ts &&... dts)
784 {
785  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line);
786  ARM_COMPUTE_RETURN_ON_ERROR(error_on_data_type_channel_not_in(function, file, line, tensor->info(), num_channels, std::forward<T>(dt), std::forward<Ts>(dts)...));
787  return arm_compute::Status{};
788 }
789 #define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c, ...) \
790  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_data_type_channel_not_in(__func__, __FILE__, __LINE__, t, c, __VA_ARGS__))
791 #define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c, ...) \
792  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_channel_not_in(__func__, __FILE__, __LINE__, t, c, __VA_ARGS__))
793 
804 inline arm_compute::Status error_on_unsupported_fp16(const char *function, const char *file, const int line,
805  const ITensorInfo *tensor_info, bool is_fp16_supported)
806 {
807  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line);
808  ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG((tensor_info->data_type() == DataType::F16 && !is_fp16_supported),
809  function, file, line, "FP16 not supported by the device");
810  return arm_compute::Status{};
811 }
812 
823 inline arm_compute::Status error_on_unsupported_fp16(const char *function, const char *file, const int line,
824  const ITensor *tensor, bool is_fp16_supported)
825 {
826  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line);
827  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_unsupported_fp16(function, file, line, tensor->info(), is_fp16_supported));
828  return arm_compute::Status{};
829 }
830 
840 arm_compute::Status error_on_tensor_not_2d(const char *function, const char *file, const int line,
841  const ITensor *tensor);
842 
852 arm_compute::Status error_on_tensor_not_2d(const char *function, const char *file, const int line,
853  const ITensorInfo *tensor);
854 
855 #define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t) \
856  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_tensor_not_2d(__func__, __FILE__, __LINE__, t))
857 #define ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D(t) \
858  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_tensor_not_2d(__func__, __FILE__, __LINE__, t))
859 
871 template <typename T, typename... Ts>
872 inline arm_compute::Status error_on_channel_not_in(const char *function, const char *file, const int line,
873  T cn, T &&channel, Ts &&... channels)
874 {
875  ARM_COMPUTE_RETURN_ERROR_ON_LOC(cn == Channel::UNKNOWN, function, file, line);
876 
877  const std::array<T, sizeof...(Ts)> channels_array{ { std::forward<Ts>(channels)... } };
878  ARM_COMPUTE_RETURN_ERROR_ON_LOC(channel != cn && std::none_of(channels_array.begin(), channels_array.end(), [&](const T & f)
879  {
880  return f == cn;
881  }),
882  function, file, line);
883  return arm_compute::Status{};
884 }
885 #define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN(c, ...) \
886  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_channel_not_in(__func__, __FILE__, __LINE__, c, __VA_ARGS__))
887 #define ARM_COMPUTE_RETURN_ERROR_ON_CHANNEL_NOT_IN(c, ...) \
888  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_channel_not_in(__func__, __FILE__, __LINE__, c, __VA_ARGS__))
889 
900 arm_compute::Status error_on_channel_not_in_known_format(const char *function, const char *file, const int line,
901  Format fmt, Channel cn);
902 #define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(f, c) \
903  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_channel_not_in_known_format(__func__, __FILE__, __LINE__, f, c))
904 #define ARM_COMPUTE_RETURN_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(f, c) \
905  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_channel_not_in_known_format(__func__, __FILE__, __LINE__, f, c))
906 
922 arm_compute::Status error_on_invalid_multi_hog(const char *function, const char *file, const int line,
923  const IMultiHOG *multi_hog);
924 #define ARM_COMPUTE_ERROR_ON_INVALID_MULTI_HOG(m) \
925  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_invalid_multi_hog(__func__, __FILE__, __LINE__, m))
926 #define ARM_COMPUTE_RETURN_ERROR_ON_INVALID_MULTI_HOG(m) \
927  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_invalid_multi_hog(__func__, __FILE__, __LINE__, m))
928 
938 arm_compute::Status error_on_unconfigured_kernel(const char *function, const char *file, const int line,
939  const IKernel *kernel);
940 #define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k) \
941  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_unconfigured_kernel(__func__, __FILE__, __LINE__, k))
942 #define ARM_COMPUTE_RETURN_ERROR_ON_UNCONFIGURED_KERNEL(k) \
943  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_unconfigured_kernel(__func__, __FILE__, __LINE__, k))
944 
956 arm_compute::Status error_on_invalid_subtensor(const char *function, const char *file, const int line,
957  const TensorShape &parent_shape, const Coordinates &coords, const TensorShape &shape);
958 #define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(p, c, s) \
959  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, p, c, s))
960 #define ARM_COMPUTE_RETURN_ERROR_ON_INVALID_SUBTENSOR(p, c, s) \
961  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, p, c, s))
962 
973 arm_compute::Status error_on_invalid_subtensor_valid_region(const char *function, const char *file, const int line,
974  const ValidRegion &parent_valid_region, const ValidRegion &valid_region);
975 #define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(pv, sv) \
976  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_invalid_subtensor_valid_region(__func__, __FILE__, __LINE__, pv, sv))
977 #define ARM_COMPUTE_RETURN_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(pv, sv) \
978  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_invalid_subtensor_valid_region(__func__, __FILE__, __LINE__, pv, sv))
979 }
980 #endif /* __ARM_COMPUTE_VALIDATE_H__*/
#define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line,...)
Definition: Error.h:329
bool is_data_type_quantized(DataType dt)
Check if a given data type is of quantized type.
Definition: Utils.h:1010
Shape of a tensor.
Definition: TensorShape.h:39
arm_compute::Status error_on_channel_not_in_known_format(const char *function, const char *file, const int line, Format fmt, Channel cn)
Return an error if the channel is not in format.
Definition: Validate.cpp:113
#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line,...)
If the condition is true, an error is thrown.
Definition: Error.h:231
TensorShape calculate_subsampled_shape(const TensorShape &shape, Format format, Channel channel=Channel::UNKNOWN)
Calculate subsampled shape for a given format and channel.
Definition: Utils.h:684
arm_compute::Status error_on_unconfigured_kernel(const char *function, const char *file, const int line, const IKernel *kernel)
Return an error if the kernel is not configured.
Definition: Validate.cpp:166
arm_compute::Status error_on_window_dimensions_gte(const char *function, const char *file, const int line, const Window &win, unsigned int max_dim)
Return an error if the passed window has too many dimensions.
Definition: Validate.cpp:80
#define ARM_COMPUTE_RETURN_ERROR_ON_LOC(cond, func, file, line)
If the condition is true, an error is returned.
Definition: Error.h:254
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:193
virtual DataType data_type() const =0
Data type used for each element of the tensor.
arm_compute::Status error_on_nullptr(const char *function, const char *file, const int line, Ts &&... pointers)
Create an error if one of the pointers is a nullptr.
Definition: Validate.h:151
arm_compute::Status error_on_unsupported_fp16(const char *function, const char *file, const int line, const ITensorInfo *tensor_info, bool is_fp16_supported)
Return an error if the data type of the passed tensor info is FP16 and FP16 extension is not supporte...
Definition: Validate.h:804
Store the tensor's metadata.
Definition: ITensorInfo.h:40
arm_compute::Status error_on_tensor_not_2d(const char *function, const char *file, const int line, const ITensor *tensor)
Return an error if the tensor is not 2D.
Definition: Validate.cpp:92
Status class.
Definition: Error.h:52
arm_compute::Status error_on_data_layout_not_in(const char *function, const char *file, const int line, const ITensorInfo *tensor_info, T &&dl, Ts &&... dls)
Return an error if the data layout of the passed tensor info does not match any of the data layouts p...
Definition: Validate.h:708
Interface for NEON tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2018 ARM Limited.
arm_compute::Status error_on_mismatching_shapes(const char *function, const char *file, const int line, const ITensorInfo *tensor_info_1, const ITensorInfo *tensor_info_2, Ts... tensor_infos)
Return an error if the passed two tensor infos have different shapes from the given dimension.
Definition: Validate.h:368
Function to compare two Dimensions objects and throw an error on mismatch.
Definition: Validate.h:72
1 channel, 1 F16 per channel
arm_compute::Status for_each_error(F &&)
Definition: Validate.h:108
arm_compute::Status error_on_tensors_not_even(const char *function, const char *file, int line, const Format &format, const ITensor *tensor1, Ts... tensors)
Return an error if the passed tensor objects are not even.
Definition: Validate.h:303
Get the info for a tensor, dummy struct.
Definition: Validate.h:123
Quantization information.
arm_compute::Status error_on_mismatching_quantization_info(const char *function, const char *file, const int line, const ITensorInfo *tensor_info_1, const ITensorInfo *tensor_info_2, Ts... tensor_infos)
Return an error if the passed tensor infos have different asymmetric quantized data types or differen...
Definition: Validate.h:562
const std::string & string_from_data_type(DataType dt)
Convert a data type identity into a string.
Definition: Utils.cpp:144
arm_compute::Status error_on_mismatching_windows(const char *function, const char *file, const int line, const Window &full, const Window &win)
Return an error if the passed window is invalid.
Definition: Validate.cpp:26
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:160
Channel
Available channels.
Definition: Types.h:451
Format
Image colour formats.
Definition: Types.h:52
arm_compute::Status error_on_channel_not_in(const char *function, const char *file, const int line, T cn, T &&channel, Ts &&... channels)
Return an error if the channel is not in channels.
Definition: Validate.h:872
arm_compute::Status error_on_mismatching_dimensions(const char *function, const char *file, int line, const Dimensions< T > &dim1, const Dimensions< T > &dim2, Ts &&... dims)
Return an error if the passed dimension objects differ.
Definition: Validate.h:280
arm_compute::Status error_on_tensors_not_subsampled(const char *function, const char *file, int line, const Format &format, const TensorShape &shape, const ITensor *tensor1, Ts... tensors)
Return an error if the passed tensor objects are not sub-sampled.
Definition: Validate.h:336
ITensorInfo * operator()(const ITensor *tensor)
Get the info for a tensor.
Definition: Validate.h:134
Dimensions with dimensionality.
Definition: Dimensions.h:41
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
bool have_different_dimensions(const Dimensions< T > &dim1, const Dimensions< T > &dim2, unsigned int upper_dim)
Definition: Validate.h:51
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_LOC(cond, func, file, line)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:347
TensorShape adjust_odd_shape(const TensorShape &shape, Format format)
Adjust tensor shape size if width or height are odd for a given multi-planar format.
Definition: Utils.h:657
arm_compute::Status error_on_invalid_subtensor(const char *function, const char *file, const int line, const TensorShape &parent_shape, const Coordinates &coords, const TensorShape &shape)
Return an error if if the coordinates and shape of the subtensor are within the parent tensor.
Definition: Validate.cpp:176
arm_compute::Status operator()(const Dimensions< T > &dim)
Compare the given object against the stored one.
Definition: Validate.h:93
const std::string & string_from_data_layout(DataLayout dl)
Convert a data layout identity into a string.
Definition: Utils.cpp:132
arm_compute::Status error_on_mismatching_data_types(const char *function, const char *file, const int line, const ITensorInfo *tensor_info, Ts... tensor_infos)
Return an error if the passed two tensor infos have different data types.
Definition: Validate.h:508
arm_compute::Status error_on_data_type_not_in(const char *function, const char *file, const int line, const ITensorInfo *tensor_info, T &&dt, Ts &&... dts)
Return an error if the data type of the passed tensor info does not match any of the data types provi...
Definition: Validate.h:656
arm_compute::Status error_on_invalid_multi_hog(const char *function, const char *file, const int line, const IMultiHOG *multi_hog)
Return an error if the IMultiHOG container is invalid.
Definition: Validate.cpp:144
arm_compute::Status error_on_data_type_channel_not_in(const char *function, const char *file, const int line, const ITensorInfo *tensor_info, size_t num_channels, T &&dt, Ts &&... dts)
Return an error if the data type or the number of channels of the passed tensor info does not match a...
Definition: Validate.h:761
arm_compute::Status error_on_mismatching_data_layouts(const char *function, const char *file, const int line, const ITensorInfo *tensor_info, Ts... tensor_infos)
Return an error if the passed tensor infos have different data layouts.
Definition: Validate.h:457
DataType
Available data types.
Definition: Types.h:74
compare_dimension(const Dimensions< T > &dim, const char *function, const char *file, int line)
Construct a comparison function.
Definition: Validate.h:82
DataLayout
[DataLayout enum definition]
Definition: Types.h:114
arm_compute::Status error_on_coordinates_dimensions_gte(const char *function, const char *file, const int line, const Coordinates &pos, unsigned int max_dim)
Return an error if the passed coordinates have too many dimensions.
Definition: Validate.cpp:70
arm_compute::Status error_on_invalid_subwindow(const char *function, const char *file, const int line, const Window &full, const Window &sub)
Return an error if the passed subwindow is invalid.
Definition: Validate.cpp:41
arm_compute::Status error_on_invalid_subtensor_valid_region(const char *function, const char *file, const int line, const ValidRegion &parent_valid_region, const ValidRegion &valid_region)
Return an error if the valid region of a subtensor is not inside the valid region of the parent tenso...
Definition: Validate.cpp:193
virtual size_t num_channels() const =0
The number of channels for each tensor element.
void error_on_format_not_in(const char *function, const char *file, const int line, const T *object, F &&format, Fs &&... formats)
Throw an error if the format of the passed tensor/multi-image does not match any of the formats provi...
Definition: Validate.h:623
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.
const std::string & string_from_format(Format format)
Convert a tensor format into a string.
Definition: Utils.cpp:85
arm_compute::Status error_on_window_not_collapsable_at_dimension(const char *function, const char *file, const int line, const Window &full, const Window &window, const int dim)
Return an error if the window can't be collapsed at the given dimension.
Definition: Validate.cpp:57