From 3faea25fe0bcb9f72bfe3da185085ed634d1b162 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Mon, 30 Oct 2017 14:13:50 +0000 Subject: COMPMID-617: Adds validation to CLPoolingLayer Change-Id: Ied405a9c0e9746598d03ac6a944ad87e9b6494eb Reviewed-on: http://mpd-gerrit.cambridge.arm.com/93680 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- arm_compute/core/Validate.h | 659 ++++++++++++++++++++++++++++++-------------- 1 file changed, 457 insertions(+), 202 deletions(-) (limited to 'arm_compute/core/Validate.h') diff --git a/arm_compute/core/Validate.h b/arm_compute/core/Validate.h index 34da339f03..1e2d262683 100644 --- a/arm_compute/core/Validate.h +++ b/arm_compute/core/Validate.h @@ -64,9 +64,9 @@ inline bool have_different_dimensions(const Dimensions &dim1, const Dimension /** Functor to compare two @ref Dimensions objects and throw an error on mismatch. * * @param[in] dim Object to compare against. - * @param[in] function Function in which the error occured. - * @param[in] file File in which the error occured. - * @param[in] line Line in which the error occured. + * @param[in] function Function in which the error occurred. + * @param[in] file File in which the error occurred. + * @param[in] line Line in which the error occurred. */ template class compare_dimension @@ -81,10 +81,11 @@ public: * * @param[in] dim To be compared object. */ - void operator()(const Dimensions &dim) + arm_compute::Error operator()(const Dimensions &dim) { - ARM_COMPUTE_ERROR_ON_LOC_MSG(have_different_dimensions(_dim, dim, 0), _function, _file, _line, - "Objects have different dimensions"); + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(have_different_dimensions(_dim, dim, 0), _function, _file, _line, + "Objects have different dimensions"); + return arm_compute::Error{}; } private: @@ -93,27 +94,59 @@ private: const char *const _file; const int _line; }; + +template +inline arm_compute::Error for_each_error(F &&) +{ + return arm_compute::Error{}; +} + +template +inline arm_compute::Error for_each_error(F &&func, T &&arg, Ts &&... args) +{ + ARM_COMPUTE_RETURN_ON_ERROR(func(arg)); + ARM_COMPUTE_RETURN_ON_ERROR(for_each_error(func, args...)); + return arm_compute::Error{}; +} + +template +struct get_tensor_info_t; +template <> +struct get_tensor_info_t +{ + ITensorInfo *operator()(const ITensor *tensor) + { + return tensor->info(); + } +}; } // namespace detail -/** Throw an error if one of the pointers is a nullptr. + +/** Create an error if one of the pointers is a nullptr. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. * @param[in] line Line on which the error occurred. * @param[in] pointers Pointers to check against nullptr. + * + * @return Error */ template -void error_on_nullptr(const char *function, const char *file, const int line, Ts &&... pointers) +inline arm_compute::Error error_on_nullptr(const char *function, const char *file, const int line, Ts &&... pointers) { - auto is_nullptr = [&](const void *ptr) + const std::array pointers_array{ { std::forward(pointers)... } }; + bool has_nullptr = std::any_of(pointers_array.begin(), pointers_array.end(), [&](const void *ptr) { - ARM_COMPUTE_ERROR_ON_LOC(ptr == nullptr, function, file, line); - }; - - for_each(is_nullptr, std::forward(pointers)...); + return (ptr == nullptr); + }); + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(has_nullptr, function, file, line, "Nullptr object!"); + return arm_compute::Error{}; } -#define ARM_COMPUTE_ERROR_ON_NULLPTR(...) ::arm_compute::error_on_nullptr(__func__, __FILE__, __LINE__, __VA_ARGS__) +#define ARM_COMPUTE_ERROR_ON_NULLPTR(...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_nullptr(__func__, __FILE__, __LINE__, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(__func__, __FILE__, __LINE__, __VA_ARGS__)) -/** Throw an error if the passed window is invalid. +/** Return an error if the passed window is invalid. * * The subwindow is invalid if: * - It is not a valid window. @@ -125,12 +158,17 @@ void error_on_nullptr(const char *function, const char *file, const int line, Ts * @param[in] line Line on which the error occurred. * @param[in] full Full size window * @param[in] win Window to validate. + * + * @return Error */ -void error_on_mismatching_windows(const char *function, const char *file, const int line, - const Window &full, const Window &win); -#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w) ::arm_compute::error_on_mismatching_windows(__func__, __FILE__, __LINE__, f, w) +arm_compute::Error error_on_mismatching_windows(const char *function, const char *file, const int line, + const Window &full, const Window &win); +#define ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(f, w) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_mismatching_windows(__func__, __FILE__, __LINE__, f, w)) +#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_WINDOWS(f, w) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_windows(__func__, __FILE__, __LINE__, f, w)) -/** Throw an error if the passed subwindow is invalid. +/** Return an error if the passed subwindow is invalid. * * The subwindow is invalid if: * - It is not a valid window. @@ -142,12 +180,17 @@ void error_on_mismatching_windows(const char *function, const char *file, const * @param[in] line Line on which the error occurred. * @param[in] full Full size window * @param[in] sub Sub-window to validate. + * + * @return Error */ -void error_on_invalid_subwindow(const char *function, const char *file, const int line, - const Window &full, const Window &sub); -#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s) ::arm_compute::error_on_invalid_subwindow(__func__, __FILE__, __LINE__, f, s) +arm_compute::Error error_on_invalid_subwindow(const char *function, const char *file, const int line, + const Window &full, const Window &sub); +#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_invalid_subwindow(__func__, __FILE__, __LINE__, f, s)) +#define ARM_COMPUTE_RETURN_ERROR_ON_INVALID_SUBWINDOW(f, s) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_invalid_subwindow(__func__, __FILE__, __LINE__, f, s)) -/** Throw an error if the window can't be collapsed at the given dimension. +/** Return an error if the window can't be collapsed at the given dimension. * * The window cannot be collapsed if the given dimension not equal to the full window's dimension or not start from 0. * @@ -157,12 +200,17 @@ void error_on_invalid_subwindow(const char *function, const char *file, const in * @param[in] full Full size window * @param[in] window Window to be collapsed. * @param[in] dim Dimension need to be checked. + * + * @return Error */ -void 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); -#define ARM_COMPUTE_ERROR_ON_WINDOW_NOT_COLLAPSABLE_AT_DIMENSION(f, w, d) ::arm_compute::error_on_window_not_collapsable_at_dimension(__func__, __FILE__, __LINE__, f, w, d) +arm_compute::Error 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); +#define ARM_COMPUTE_ERROR_ON_WINDOW_NOT_COLLAPSABLE_AT_DIMENSION(f, w, d) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_window_not_collapsable_at_dimension(__func__, __FILE__, __LINE__, f, w, d)) +#define ARM_COMPUTE_RETURN_ERROR_ON_WINDOW_NOT_COLLAPSABLE_AT_DIMENSION(f, w, d) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_window_not_collapsable_at_dimension(__func__, __FILE__, __LINE__, f, w, d)) -/** Throw an error if the passed coordinates have too many dimensions. +/** Return an error if the passed coordinates have too many dimensions. * * The coordinates have too many dimensions if any of the dimensions greater or equal to max_dim is different from 0. * @@ -171,12 +219,17 @@ void error_on_window_not_collapsable_at_dimension(const char *function, const ch * @param[in] line Line on which the error occurred. * @param[in] pos Coordinates to validate * @param[in] max_dim Maximum number of dimensions allowed. + * + * @return Error */ -void error_on_coordinates_dimensions_gte(const char *function, const char *file, const int line, - const Coordinates &pos, unsigned int max_dim); -#define ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(p, md) ::arm_compute::error_on_coordinates_dimensions_gte(__func__, __FILE__, __LINE__, p, md) +arm_compute::Error error_on_coordinates_dimensions_gte(const char *function, const char *file, const int line, + const Coordinates &pos, unsigned int max_dim); +#define ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(p, md) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_coordinates_dimensions_gte(__func__, __FILE__, __LINE__, p, md)) +#define ARM_COMPUTE_RETURN_ERROR_ON_COORDINATES_DIMENSIONS_GTE(p, md) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_coordinates_dimensions_gte(__func__, __FILE__, __LINE__, p, md)) -/** Throw an error if the passed window has too many dimensions. +/** Return an error if the passed window has too many dimensions. * * The window has too many dimensions if any of the dimension greater or equal to max_dim is different from 0. * @@ -185,12 +238,17 @@ void error_on_coordinates_dimensions_gte(const char *function, const char *file, * @param[in] line Line on which the error occurred. * @param[in] win Window to validate * @param[in] max_dim Maximum number of dimensions allowed. + * + * @return Error */ -void error_on_window_dimensions_gte(const char *function, const char *file, const int line, - const Window &win, unsigned int max_dim); -#define ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(w, md) ::arm_compute::error_on_window_dimensions_gte(__func__, __FILE__, __LINE__, w, md) +arm_compute::Error error_on_window_dimensions_gte(const char *function, const char *file, const int line, + const Window &win, unsigned int max_dim); +#define ARM_COMPUTE_ERROR_ON_WINDOW_DIMENSIONS_GTE(w, md) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_window_dimensions_gte(__func__, __FILE__, __LINE__, w, md)) +#define ARM_COMPUTE_RETURN_ERROR_ON_WINDOW_DIMENSIONS_GTE(w, md) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_window_dimensions_gte(__func__, __FILE__, __LINE__, w, md)) -/** Throw an error if the passed dimension objects differ. +/** Return an error if the passed dimension objects differ. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. @@ -198,20 +256,39 @@ void error_on_window_dimensions_gte(const char *function, const char *file, cons * @param[in] dim1 The first object to be compared. * @param[in] dim2 The second object to be compared. * @param[in] dims (Optional) Further allowed objects. + * + * @return Error */ template -void error_on_mismatching_dimensions(const char *function, const char *file, int line, - const Dimensions &dim1, const Dimensions &dim2, Ts &&... dims) +arm_compute::Error error_on_mismatching_dimensions(const char *function, const char *file, int line, + const Dimensions &dim1, const Dimensions &dim2, Ts &&... dims) { - ARM_COMPUTE_UNUSED(function); - ARM_COMPUTE_UNUSED(file); - ARM_COMPUTE_UNUSED(line); - - for_each(detail::compare_dimension(dim1, function, file, line), dim2, std::forward(dims)...); + ARM_COMPUTE_RETURN_ON_ERROR(detail::for_each_error(detail::compare_dimension(dim1, function, file, line), dim2, std::forward(dims)...)); + return arm_compute::Error{}; } -#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(...) ::arm_compute::error_on_mismatching_dimensions(__func__, __FILE__, __LINE__, __VA_ARGS__) +#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_mismatching_dimensions(__func__, __FILE__, __LINE__, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_dimensions(__func__, __FILE__, __LINE__, __VA_ARGS__)) -/** Throw an error if the passed two tensors have different shapes from the given dimension +/** Return an error if the passed two tensor infos have different shapes from the given dimension + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor_info_1 The first tensor info to be compared. + * @param[in] tensor_info_2 The second tensor info to be compared. + * @param[in] tensor_infos (Optional) Further allowed tensor infos. + * + * @return Error + */ +template +inline arm_compute::Error 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 error_on_mismatching_shapes(function, file, line, 0U, tensor_info_1, tensor_info_2, std::forward(tensor_infos)...); +} +/** Return an error if the passed two tensors have different shapes from the given dimension * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. @@ -219,15 +296,44 @@ void error_on_mismatching_dimensions(const char *function, const char *file, int * @param[in] tensor_1 The first tensor to be compared. * @param[in] tensor_2 The second tensor to be compared. * @param[in] tensors (Optional) Further allowed tensors. + * + * @return Error */ template -void error_on_mismatching_shapes(const char *function, const char *file, const int line, - const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors) +inline arm_compute::Error error_on_mismatching_shapes(const char *function, const char *file, const int line, + const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors) { - error_on_mismatching_shapes(function, file, line, 0U, tensor_1, tensor_2, std::forward(tensors)...); + return error_on_mismatching_shapes(function, file, line, 0U, tensor_1, tensor_2, std::forward(tensors)...); } +/** Return an error if the passed two tensors have different shapes from the given dimension + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] upper_dim The dimension from which to check. + * @param[in] tensor_info_1 The first tensor info to be compared. + * @param[in] tensor_info_2 The second tensor info to be compared. + * @param[in] tensor_infos (Optional) Further allowed tensor infos. + * + * @return Error + */ +template +inline arm_compute::Error error_on_mismatching_shapes(const char *function, const char *file, const int line, + unsigned int upper_dim, const ITensorInfo *tensor_info_1, const ITensorInfo *tensor_info_2, Ts... tensor_infos) +{ + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info_1 == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info_2 == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward(tensor_infos)...)); -/** Throw an error if the passed two tensors have different shapes from the given dimension + const std::array < const ITensorInfo *, 2 + sizeof...(Ts) > tensors_info_array{ { tensor_info_1, tensor_info_2, std::forward(tensor_infos)... } }; + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(std::next(tensors_info_array.cbegin()), tensors_info_array.cend(), [&](const ITensorInfo * tensor_info) + { + return detail::have_different_dimensions((*tensors_info_array.cbegin())->tensor_shape(), tensor_info->tensor_shape(), upper_dim); + }), + function, file, line, "Tensors have different shapes"); + return arm_compute::Error{}; +} +/** Return an error if the passed two tensors have different shapes from the given dimension * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. @@ -236,112 +342,140 @@ void error_on_mismatching_shapes(const char *function, const char *file, const i * @param[in] tensor_1 The first tensor to be compared. * @param[in] tensor_2 The second tensor to be compared. * @param[in] tensors (Optional) Further allowed tensors. + * + * @return Error */ template -void error_on_mismatching_shapes(const char *function, const char *file, const int line, - unsigned int upper_dim, const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors) +inline arm_compute::Error error_on_mismatching_shapes(const char *function, const char *file, const int line, + unsigned int upper_dim, const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors) { - ARM_COMPUTE_UNUSED(function); - ARM_COMPUTE_UNUSED(file); - ARM_COMPUTE_UNUSED(line); - - const std::array < const ITensor *, 2 + sizeof...(Ts) > tensors_array{ { tensor_1, tensor_2, std::forward(tensors)... } }; - ARM_COMPUTE_UNUSED(tensors_array); + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_1 == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_2 == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward(tensors)...)); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_shapes(function, file, line, upper_dim, tensor_1->info(), tensor_2->info(), + detail::get_tensor_info_t()(tensors)...)); + return arm_compute::Error{}; +} +#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_mismatching_shapes(__func__, __FILE__, __LINE__, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_shapes(__func__, __FILE__, __LINE__, __VA_ARGS__)) - ARM_COMPUTE_ERROR_ON_LOC(tensors_array.cbegin() == nullptr, function, file, line); +/** Return an error if the passed two tensor infos have different data types + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor_info The first tensor info to be compared. + * @param[in] tensor_infos (Optional) Further allowed tensor infos. + * + * @return Error + */ +template +inline arm_compute::Error error_on_mismatching_data_types(const char *function, const char *file, const int line, + const ITensorInfo *tensor_info, Ts... tensor_infos) +{ + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward(tensor_infos)...)); - ARM_COMPUTE_ERROR_ON_LOC_MSG(std::any_of(std::next(tensors_array.cbegin()), tensors_array.cend(), [&](const ITensor * tensor) + DataType &&tensor_data_type = tensor_info->data_type(); + const std::array tensors_infos_array{ { std::forward(tensor_infos)... } }; + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensors_infos_array.begin(), tensors_infos_array.end(), [&](const ITensorInfo * tensor_info_obj) { - ARM_COMPUTE_ERROR_ON_LOC(tensor == nullptr, function, file, line); - return detail::have_different_dimensions((*tensors_array.cbegin())->info()->tensor_shape(), tensor->info()->tensor_shape(), upper_dim); + return tensor_info_obj->data_type() != tensor_data_type; }), - function, file, line, "Tensors have different shapes"); + function, file, line, "Tensors have different data types"); + return arm_compute::Error{}; } -#define ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(...) ::arm_compute::error_on_mismatching_shapes(__func__, __FILE__, __LINE__, __VA_ARGS__) - -/** Throw an error if the passed two tensors have different data types +/** Return an error if the passed two tensors have different data types * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. * @param[in] line Line on which the error occurred. * @param[in] tensor The first tensor to be compared. * @param[in] tensors (Optional) Further allowed tensors. + * + * @return Error */ template -void error_on_mismatching_data_types(const char *function, const char *file, const int line, - const ITensor *tensor, Ts... tensors) +inline arm_compute::Error error_on_mismatching_data_types(const char *function, const char *file, const int line, + const ITensor *tensor, Ts... tensors) { - ARM_COMPUTE_UNUSED(function); - ARM_COMPUTE_UNUSED(file); - ARM_COMPUTE_UNUSED(line); - ARM_COMPUTE_UNUSED(tensor); - - ARM_COMPUTE_ERROR_ON_LOC(tensor == nullptr, function, file, line); - - DataType &&tensor_data_type = tensor->info()->data_type(); - ARM_COMPUTE_UNUSED(tensor_data_type); - - const std::array tensors_array{ { std::forward(tensors)... } }; - ARM_COMPUTE_UNUSED(tensors_array); - - ARM_COMPUTE_ERROR_ON_LOC_MSG(std::any_of(tensors_array.begin(), tensors_array.end(), [&](const ITensor * tensor_obj) - { - ARM_COMPUTE_ERROR_ON_LOC(tensor_obj == nullptr, function, file, line); - return tensor_obj->info()->data_type() != tensor_data_type; - }), - function, file, line, "Tensors have different data types"); + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_nullptr(function, file, line, std::forward(tensors)...)); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_data_types(function, file, line, tensor->info(), + detail::get_tensor_info_t()(tensors)...)); + return arm_compute::Error{}; } +#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_mismatching_data_types(__func__, __FILE__, __LINE__, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_data_types(__func__, __FILE__, __LINE__, __VA_ARGS__)) -#define ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(...) ::arm_compute::error_on_mismatching_data_types(__func__, __FILE__, __LINE__, __VA_ARGS__) - -/** Throw an error if the passed tensors have different fixed point data types or different fixed point positions +/** Return an error if the passed tensor infos have different fixed point data types or different fixed point positions * * @note: If the first tensor doesn't have fixed point data type, the function returns without throwing an error * - * @param[in] function Function in which the error occurred. - * @param[in] file Name of the file where the error occurred. - * @param[in] line Line on which the error occurred. - * @param[in] tensor_1 The first tensor to be compared. - * @param[in] tensor_2 The second tensor to be compared. - * @param[in] tensors (Optional) Further allowed tensors. + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor_info_1 The first tensor info to be compared. + * @param[in] tensor_info_2 The second tensor info to be compared. + * @param[in] tensor_infos (Optional) Further allowed tensor infos. + * + * @return Error */ template -void error_on_mismatching_fixed_point(const char *function, const char *file, const int line, - const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors) +inline arm_compute::Error error_on_mismatching_fixed_point(const char *function, const char *file, const int line, + const ITensorInfo *tensor_info_1, const ITensorInfo *tensor_info_2, Ts... tensor_infos) { - ARM_COMPUTE_UNUSED(function); - ARM_COMPUTE_UNUSED(file); - ARM_COMPUTE_UNUSED(line); - ARM_COMPUTE_UNUSED(tensor_1); - ARM_COMPUTE_UNUSED(tensor_2); - - DataType &&first_data_type = tensor_1->info()->data_type(); - const int first_fixed_point_position = tensor_1->info()->fixed_point_position(); - ARM_COMPUTE_UNUSED(first_data_type); - ARM_COMPUTE_UNUSED(first_fixed_point_position); + DataType &&first_data_type = tensor_info_1->data_type(); + const int first_fixed_point_position = tensor_info_1->fixed_point_position(); if((first_data_type != DataType::QS8) && (first_data_type != DataType::QS16)) { - return; + return arm_compute::Error{}; } - const std::array < const ITensor *, 1 + sizeof...(Ts) > tensors_array{ { tensor_2, std::forward(tensors)... } }; - ARM_COMPUTE_UNUSED(tensors_array); - - ARM_COMPUTE_ERROR_ON_LOC_MSG(std::any_of(tensors_array.begin(), tensors_array.end(), [&](const ITensor * tensor) + const std::array < const ITensorInfo *, 1 + sizeof...(Ts) > tensor_infos_array{ { tensor_info_2, std::forward(tensor_infos)... } }; + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensor_infos_array.begin(), tensor_infos_array.end(), [&](const ITensorInfo * tensor_info) { - return tensor->info()->data_type() != first_data_type; + return tensor_info->data_type() != first_data_type; }), function, file, line, "Tensors have different fixed point data types"); - - ARM_COMPUTE_ERROR_ON_LOC_MSG(std::any_of(tensors_array.begin(), tensors_array.end(), [&](const ITensor * tensor) + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensor_infos_array.begin(), tensor_infos_array.end(), [&](const ITensorInfo * tensor_info) { - return tensor->info()->fixed_point_position() != first_fixed_point_position; + return tensor_info->fixed_point_position() != first_fixed_point_position; }), function, file, line, "Tensors have different fixed point positions"); -} -#define ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(...) ::arm_compute::error_on_mismatching_fixed_point(__func__, __FILE__, __LINE__, __VA_ARGS__) + return arm_compute::Error{}; +} +/** Return an error if the passed tensor have different fixed point data types or different fixed point positions + * + * @note: If the first tensor doesn't have fixed point data type, the function returns without throwing an error + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor_1 The first tensor to be compared. + * @param[in] tensor_2 The second tensor to be compared. + * @param[in] tensors (Optional) Further allowed tensors. + * + * @return Error + */ +template +inline arm_compute::Error error_on_mismatching_fixed_point(const char *function, const char *file, const int line, + const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors) +{ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_fixed_point(function, file, line, tensor_1->info(), tensor_2->info(), + detail::get_tensor_info_t()(tensors)...)); + return arm_compute::Error{}; +} +#define ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_mismatching_fixed_point(__func__, __FILE__, __LINE__, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_fixed_point(__func__, __FILE__, __LINE__, __VA_ARGS__)) /** Throw an error if the format of the passed tensor/multi-image does not match any of the formats provided. * @@ -374,38 +508,80 @@ void error_on_format_not_in(const char *function, const char *file, const int li } #define ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(t, ...) ::arm_compute::error_on_format_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__) -/** Throw an error if the data type of the passed tensor does not match any of the data types provided. +/** Return an error if the data type of the passed tensor info does not match any of the data types provided. * - * @param[in] function Function in which the error occurred. - * @param[in] file Name of the file where the error occurred. - * @param[in] line Line on which the error occurred. - * @param[in] tensor Tensor to validate. - * @param[in] dt First data type allowed. - * @param[in] dts (Optional) Further allowed data types. + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor_info Tensor info to validate. + * @param[in] dt First data type allowed. + * @param[in] dts (Optional) Further allowed data types. + * + * @return Error */ template -void error_on_data_type_not_in(const char *function, const char *file, const int line, - const ITensor *tensor, T &&dt, Ts &&... dts) +inline arm_compute::Error error_on_data_type_not_in(const char *function, const char *file, const int line, + const ITensorInfo *tensor_info, T &&dt, Ts &&... dts) { - ARM_COMPUTE_ERROR_ON_LOC(tensor == nullptr, function, file, line); - - const DataType &tensor_dt = tensor->info()->data_type(); //NOLINT - ARM_COMPUTE_UNUSED(tensor_dt); + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line); - ARM_COMPUTE_ERROR_ON_LOC(tensor_dt == DataType::UNKNOWN, function, file, line); + const DataType &tensor_dt = tensor_info->data_type(); //NOLINT + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_dt == DataType::UNKNOWN, function, file, line); const std::array dts_array{ { std::forward(dts)... } }; - ARM_COMPUTE_UNUSED(dts_array); - - ARM_COMPUTE_ERROR_ON_LOC_MSG(tensor_dt != dt && std::none_of(dts_array.begin(), dts_array.end(), [&](const T & d) + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(tensor_dt != dt && std::none_of(dts_array.begin(), dts_array.end(), [&](const T & d) { return d == tensor_dt; }), function, file, line, "ITensor data type %s not supported by this kernel", string_from_data_type(tensor_dt).c_str()); + return arm_compute::Error{}; } -#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t, ...) ::arm_compute::error_on_data_type_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__) +/** Return an error if the data type of the passed tensor does not match any of the data types provided. + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor Tensor to validate. + * @param[in] dt First data type allowed. + * @param[in] dts (Optional) Further allowed data types. + * + * @return Error + */ +template +inline arm_compute::Error error_on_data_type_not_in(const char *function, const char *file, const int line, + const ITensor *tensor, T &&dt, Ts &&... dts) +{ + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_not_in(function, file, line, tensor->info(), std::forward(dt), std::forward(dts)...)); + return arm_compute::Error{}; +} +#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(t, ...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_data_type_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t, ...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_not_in(__func__, __FILE__, __LINE__, t, __VA_ARGS__)) -/** Throw an error if the data type or the number of channels of the passed tensor does not match any of the data types and number of channels provided. +/** Return an error if the data type or the number of channels of the passed tensor info does not match any of the data types and number of channels provided. + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor_info Tensor info to validate. + * @param[in] num_channels Number of channels to check + * @param[in] dt First data type allowed. + * @param[in] dts (Optional) Further allowed data types. + * + * @return Error + */ +template +inline arm_compute::Error 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) +{ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_not_in(function, file, line, tensor_info, std::forward(dt), std::forward(dts)...)); + const size_t tensor_nc = tensor_info->num_channels(); + 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); + return arm_compute::Error{}; +} +/** Return an error if the data type or the number of channels of the passed tensor does not match any of the data types and number of channels provided. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. @@ -414,32 +590,39 @@ void error_on_data_type_not_in(const char *function, const char *file, const int * @param[in] num_channels Number of channels to check * @param[in] dt First data type allowed. * @param[in] dts (Optional) Further allowed data types. + * + * @return Error */ template -void error_on_data_type_channel_not_in(const char *function, const char *file, const int line, - const ITensor *tensor, size_t num_channels, T &&dt, Ts &&... dts) +inline arm_compute::Error error_on_data_type_channel_not_in(const char *function, const char *file, const int line, + const ITensor *tensor, size_t num_channels, T &&dt, Ts &&... dts) { - error_on_data_type_not_in(function, file, line, tensor, std::forward(dt), std::forward(dts)...); - - const size_t tensor_nc = tensor->info()->num_channels(); - ARM_COMPUTE_UNUSED(tensor_nc); - - ARM_COMPUTE_ERROR_ON_LOC_MSG(tensor_nc != num_channels, function, file, line, "Number of channels %d. Required number of channels %d", tensor_nc, num_channels); + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ON_ERROR(error_on_data_type_channel_not_in(function, file, line, tensor->info(), num_channels, std::forward(dt), std::forward(dts)...)); + return arm_compute::Error{}; } -#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c, ...) ::arm_compute::error_on_data_type_channel_not_in(__func__, __FILE__, __LINE__, t, c, __VA_ARGS__) +#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c, ...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_data_type_channel_not_in(__func__, __FILE__, __LINE__, t, c, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c, ...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_data_type_channel_not_in(__func__, __FILE__, __LINE__, t, c, __VA_ARGS__)) -/** Throw an error if the tensor is not 2D. +/** Return an error if the tensor is not 2D. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. * @param[in] line Line on which the error occurred. * @param[in] tensor Tensor to validate. + * + * @return Error */ -void error_on_tensor_not_2d(const char *function, const char *file, const int line, - const ITensor *tensor); -#define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t) ::arm_compute::error_on_tensor_not_2d(__func__, __FILE__, __LINE__, t) +arm_compute::Error error_on_tensor_not_2d(const char *function, const char *file, const int line, + const ITensor *tensor); +#define ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(t) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_tensor_not_2d(__func__, __FILE__, __LINE__, t)) +#define ARM_COMPUTE_RETURN_ERROR_ON_TENSOR_NOT_2D(t) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_tensor_not_2d(__func__, __FILE__, __LINE__, t)) -/** Throw an error if the channel is not in channels. +/** Return an error if the channel is not in channels. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. @@ -447,36 +630,46 @@ void error_on_tensor_not_2d(const char *function, const char *file, const int li * @param[in] cn Input channel * @param[in] channel First channel allowed. * @param[in] channels (Optional) Further allowed channels. + * + * @return Error */ template -void error_on_channel_not_in(const char *function, const char *file, const int line, - T cn, T &&channel, Ts &&... channels) +inline arm_compute::Error error_on_channel_not_in(const char *function, const char *file, const int line, + T cn, T &&channel, Ts &&... channels) { - ARM_COMPUTE_ERROR_ON_LOC(cn == Channel::UNKNOWN, function, file, line); + ARM_COMPUTE_RETURN_ERROR_ON_LOC(cn == Channel::UNKNOWN, function, file, line); const std::array channels_array{ { std::forward(channels)... } }; - ARM_COMPUTE_UNUSED(channels_array); - ARM_COMPUTE_ERROR_ON_LOC(channel != cn && std::none_of(channels_array.begin(), channels_array.end(), [&](const T & f) + ARM_COMPUTE_RETURN_ERROR_ON_LOC(channel != cn && std::none_of(channels_array.begin(), channels_array.end(), [&](const T & f) { return f == cn; }), function, file, line); + return arm_compute::Error{}; } -#define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN(c, ...) ::arm_compute::error_on_channel_not_in(__func__, __FILE__, __LINE__, c, __VA_ARGS__) +#define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN(c, ...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_channel_not_in(__func__, __FILE__, __LINE__, c, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_CHANNEL_NOT_IN(c, ...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_channel_not_in(__func__, __FILE__, __LINE__, c, __VA_ARGS__)) -/** Throw an error if the channel is not in format. +/** Return an error if the channel is not in format. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. * @param[in] line Line on which the error occurred. * @param[in] fmt Input channel * @param[in] cn First channel allowed. + * + * @return Error */ -void error_on_channel_not_in_known_format(const char *function, const char *file, const int line, - Format fmt, Channel cn); -#define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(f, c) ::arm_compute::error_on_channel_not_in_known_format(__func__, __FILE__, __LINE__, f, c) +arm_compute::Error error_on_channel_not_in_known_format(const char *function, const char *file, const int line, + Format fmt, Channel cn); +#define ARM_COMPUTE_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(f, c) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_channel_not_in_known_format(__func__, __FILE__, __LINE__, f, c)) +#define ARM_COMPUTE_RETURN_ERROR_ON_CHANNEL_NOT_IN_KNOWN_FORMAT(f, c) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_channel_not_in_known_format(__func__, __FILE__, __LINE__, f, c)) -/** Throw an error if the @ref IMultiHOG container is invalid +/** Return an error if the @ref IMultiHOG container is invalid * * An @ref IMultiHOG container is invalid if: * @@ -488,23 +681,31 @@ void error_on_channel_not_in_known_format(const char *function, const char *file * @param[in] file Name of the file where the error occurred. * @param[in] line Line on which the error occurred. * @param[in] multi_hog IMultiHOG container to validate + * + * @return Error */ -void error_on_invalid_multi_hog(const char *function, const char *file, const int line, - const IMultiHOG *multi_hog); -#define ARM_COMPUTE_ERROR_ON_INVALID_MULTI_HOG(m) ::arm_compute::error_on_invalid_multi_hog(__func__, __FILE__, __LINE__, m) +arm_compute::Error error_on_invalid_multi_hog(const char *function, const char *file, const int line, + const IMultiHOG *multi_hog); +#define ARM_COMPUTE_ERROR_ON_INVALID_MULTI_HOG(m) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_invalid_multi_hog(__func__, __FILE__, __LINE__, m)) +#define ARM_COMPUTE_RETURN_ERROR_ON_INVALID_MULTI_HOG(m) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_invalid_multi_hog(__func__, __FILE__, __LINE__, m)) -/** Throw an error if the kernel is not configured. +/** Return an error if the kernel is not configured. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. * @param[in] line Line on which the error occurred. * @param[in] kernel Kernel to validate. */ -void error_on_unconfigured_kernel(const char *function, const char *file, const int line, - const IKernel *kernel); -#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k) ::arm_compute::error_on_unconfigured_kernel(__func__, __FILE__, __LINE__, k) +arm_compute::Error error_on_unconfigured_kernel(const char *function, const char *file, const int line, + const IKernel *kernel); +#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_unconfigured_kernel(__func__, __FILE__, __LINE__, k)) +#define ARM_COMPUTE_RETURN_ERROR_ON_UNCONFIGURED_KERNEL(k) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_unconfigured_kernel(__func__, __FILE__, __LINE__, k)) -/** Throw an error if if the coordinates and shape of the subtensor are within the parent tensor. +/** Return an error if if the coordinates and shape of the subtensor are within the parent tensor. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. @@ -512,24 +713,57 @@ void error_on_unconfigured_kernel(const char *function, const char *file, const * @param[in] parent_shape Parent tensor shape * @param[in] coords Coordinates inside the parent tensor where the first element of the subtensor is * @param[in] shape Shape of the subtensor + * + * @return Error */ -void error_on_invalid_subtensor(const char *function, const char *file, const int line, - const TensorShape &parent_shape, const Coordinates &coords, const TensorShape &shape); -#define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(p, c, s) ::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, p, c, s) +arm_compute::Error error_on_invalid_subtensor(const char *function, const char *file, const int line, + const TensorShape &parent_shape, const Coordinates &coords, const TensorShape &shape); +#define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(p, c, s) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, p, c, s)) +#define ARM_COMPUTE_RETURN_ERROR_ON_INVALID_SUBTENSOR(p, c, s) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_invalid_subtensor(__func__, __FILE__, __LINE__, p, c, s)) -/** Throw an error if the valid region of a subtensor is not inside the valid region of the parent tensor. +/** Return an error if the valid region of a subtensor is not inside the valid region of the parent tensor. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. * @param[in] line Line on which the error occurred. * @param[in] parent_valid_region Parent valid region. * @param[in] valid_region Valid region of subtensor. + * + * @return Error */ -void error_on_invalid_subtensor_valid_region(const char *function, const char *file, const int line, - const ValidRegion &parent_valid_region, const ValidRegion &valid_region); -#define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(pv, sv) ::arm_compute::error_on_invalid_subtensor_valid_region(__func__, __FILE__, __LINE__, pv, sv) - -/** Throw an error if the input fixed-point positions are different. +arm_compute::Error error_on_invalid_subtensor_valid_region(const char *function, const char *file, const int line, + const ValidRegion &parent_valid_region, const ValidRegion &valid_region); +#define ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(pv, sv) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_invalid_subtensor_valid_region(__func__, __FILE__, __LINE__, pv, sv)) +#define ARM_COMPUTE_RETURN_ERROR_ON_INVALID_SUBTENSOR_VALID_REGION(pv, sv) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_invalid_subtensor_valid_region(__func__, __FILE__, __LINE__, pv, sv)) + +/** Return an error if the input fixed-point positions are different. + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] tensor_info_1 The first tensor info to be compared. + * @param[in] tensor_info_2 The second tensor info to be compared. + * @param[in] tensor_infos (Optional) Further allowed tensor infos. + * + * @return Error + */ +template +inline arm_compute::Error error_on_mismatching_fixed_point_position(const char *function, const char *file, const int line, + const ITensorInfo *tensor_info_1, const ITensorInfo *tensor_info_2, Ts... tensor_infos) +{ + const std::array < const ITensorInfo *, 1 + sizeof...(Ts) > tensor_info_array{ { tensor_info_2, std::forward(tensor_infos)... } }; + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(std::any_of(tensor_info_array.begin(), tensor_info_array.end(), [&](const ITensorInfo * tensor_info) + { + return tensor_info->fixed_point_position() != tensor_info_1->fixed_point_position(); + }), + function, file, line, "Tensors have different fixed-point positions"); + return arm_compute::Error{}; +} +/** Return an error if the input fixed-point positions are different. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. @@ -537,43 +771,64 @@ void error_on_invalid_subtensor_valid_region(const char *function, const char *f * @param[in] tensor_1 The first tensor to be compared. * @param[in] tensor_2 The second tensor to be compared. * @param[in] tensors (Optional) Further allowed tensors. + * + * @return Error */ template -void error_on_mismatching_fixed_point_position(const char *function, const char *file, const int line, - const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors) +inline arm_compute::Error error_on_mismatching_fixed_point_position(const char *function, const char *file, const int line, + const ITensor *tensor_1, const ITensor *tensor_2, Ts... tensors) { - const std::array < const ITensor *, 1 + sizeof...(Ts) > tensors_array{ { tensor_2, std::forward(tensors)... } }; - ARM_COMPUTE_UNUSED(tensors_array); - - ARM_COMPUTE_ERROR_ON_LOC_MSG(std::any_of(tensors_array.begin(), tensors_array.end(), [&](const ITensor * tensor) - { - return tensor->info()->fixed_point_position() != tensor_1->info()->fixed_point_position(); - }), - function, file, line, "Tensors have different fixed-point positions"); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_fixed_point_position(function, file, line, tensor_1->info(), tensor_2->info(), + detail::get_tensor_info_t()(tensors)...)); + return arm_compute::Error{}; } -#define ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(...) ::arm_compute::error_on_mismatching_fixed_point_position(__func__, __FILE__, __LINE__, __VA_ARGS__) +#define ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_mismatching_fixed_point_position(__func__, __FILE__, __LINE__, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_mismatching_fixed_point_position(__func__, __FILE__, __LINE__, __VA_ARGS__)) -/** Throw an error if the fixed-point value is not representable in the specified Q format. +/** Return an error if the fixed-point value is not representable in the specified Q format. + * + * @param[in] function Function in which the error occurred. + * @param[in] file Name of the file where the error occurred. + * @param[in] line Line on which the error occurred. + * @param[in] value The floating point value to be checked. + * @param[in] tensor_info Input tensor info that has information on data type and fixed-point position. + * + * @return Error + */ +inline arm_compute::Error error_on_value_not_representable_in_fixed_point(const char *function, const char *file, int line, + float value, const ITensorInfo *tensor_info) +{ + const int fixed_point_position = tensor_info->fixed_point_position(); + const DataType dt = tensor_info->data_type(); + const unsigned int q_max_range = 0xFFFFFFFFu >> (((sizeof(unsigned int) - element_size_from_data_type(dt)) * 8) + 1); + const float max_range = q_max_range / (static_cast(1 << fixed_point_position)); + + ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(value > max_range, function, file, line, + "Value %f is not representable in %s with fixed-point position %d", value, string_from_data_type(dt).c_str(), fixed_point_position); + return arm_compute::Error{}; +} +/** Return an error an error if the fixed-point value is not representable in the specified Q format. * * @param[in] function Function in which the error occurred. * @param[in] file Name of the file where the error occurred. * @param[in] line Line on which the error occurred. * @param[in] value The floating point value to be checked. * @param[in] tensor Input tensor that has information on data type and fixed-point position. + * + * @return Error */ -template -void error_on_value_not_representable_in_fixed_point(const char *function, const char *file, int line, - float value, const ITensor *tensor) +inline arm_compute::Error error_on_value_not_representable_in_fixed_point(const char *function, const char *file, int line, + float value, const ITensor *tensor) { - const int fixed_point_position = tensor->info()->fixed_point_position(); - const DataType dt = tensor->info()->data_type(); - const unsigned int q_max_range = 0xFFFFFFFFu >> (((sizeof(unsigned int) - element_size_from_data_type(dt)) * 8) + 1); - const float max_range = q_max_range / (static_cast(1 << fixed_point_position)); - ARM_COMPUTE_UNUSED(max_range); - - ARM_COMPUTE_ERROR_ON_LOC_MSG(value > max_range, function, file, line, - "Value %f is not representable in %s with fixed-point position %d", value, string_from_data_type(dt).c_str(), fixed_point_position); + ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line); + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_value_not_representable_in_fixed_point(function, file, line, value, tensor->info())); + return arm_compute::Error{}; } -#define ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(...) ::arm_compute::error_on_value_not_representable_in_fixed_point(__func__, __FILE__, __LINE__, __VA_ARGS__) +#define ARM_COMPUTE_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(...) \ + ARM_COMPUTE_ERROR_THROW(::arm_compute::error_on_value_not_representable_in_fixed_point(__func__, __FILE__, __LINE__, __VA_ARGS__)) +#define ARM_COMPUTE_RETURN_ERROR_ON_VALUE_NOT_REPRESENTABLE_IN_FIXED_POINT(...) \ + ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_value_not_representable_in_fixed_point(__func__, __FILE__, __LINE__, __VA_ARGS__)) } #endif /* __ARM_COMPUTE_VALIDATE_H__*/ -- cgit v1.2.1