From 82c0d7f709d6e4fdaceec5ea24e466dadaba1827 Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Tue, 15 Dec 2020 17:15:43 +0000 Subject: Create fill functions available everywhere for easy debugging Resolves: COMPMID-3817 Signed-off-by: Giorgio Arena Change-Id: I56aae55b653a60a26bb0c6c86b786bccf9ddb793 Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/4702 Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: Manuel Bottini Reviewed-by: Georgios Pinitas --- utils/Utils.h | 93 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 31 deletions(-) diff --git a/utils/Utils.h b/utils/Utils.h index 7eeeae5419..ca4509778b 100644 --- a/utils/Utils.h +++ b/utils/Utils.h @@ -722,54 +722,85 @@ void load_trained_data(T &tensor, const std::string &filename) } } -template -void fill_random_tensor(T &tensor, float lower_bound, float upper_bound) +template +void fill_tensor_value(TensorType &tensor, T value) { - std::random_device rd; - std::mt19937 gen(rd()); + map(tensor, true); Window window; window.use_tensor_dimensions(tensor.info()->tensor_shape()); + Iterator it_tensor(&tensor, window); + execute_window_loop(window, [&](const Coordinates &) + { + *reinterpret_cast(it_tensor.ptr()) = value; + }, + it_tensor); + + unmap(tensor); +} + +template +void fill_tensor_zero(TensorType &tensor) +{ + fill_tensor_value(tensor, T(0)); +} + +template +void fill_tensor_vector(TensorType &tensor, std::vector vec) +{ + ARM_COMPUTE_ERROR_ON(tensor.info()->tensor_shape().total_size() != vec.size()); + map(tensor, true); - Iterator it(&tensor, window); + Window window; + window.use_tensor_dimensions(tensor.info()->tensor_shape()); - switch(tensor.info()->data_type()) + int i = 0; + Iterator it_tensor(&tensor, window); + execute_window_loop(window, [&](const Coordinates &) { - case arm_compute::DataType::F16: - { - std::uniform_real_distribution dist(lower_bound, upper_bound); + *reinterpret_cast(it_tensor.ptr()) = vec.at(i++); + }, + it_tensor); - execute_window_loop(window, [&](const Coordinates &) - { - *reinterpret_cast(it.ptr()) = (half)dist(gen); - }, - it); + unmap(tensor); +} - break; - } - case arm_compute::DataType::F32: - { - std::uniform_real_distribution dist(lower_bound, upper_bound); +template +void fill_random_tensor(TensorType &tensor, std::random_device::result_type seed, T lower_bound = std::numeric_limits::lowest(), T upper_bound = std::numeric_limits::max()) +{ + constexpr bool is_half = std::is_same::value; + constexpr bool is_integral = std::is_integral::value && !is_half; - execute_window_loop(window, [&](const Coordinates &) - { - *reinterpret_cast(it.ptr()) = dist(gen); - }, - it); + using fp_dist_type = typename std::conditional>::type; + using dist_type = typename std::conditional, fp_dist_type>::type; - break; - } - default: - { - ARM_COMPUTE_ERROR("Unsupported format"); - } - } + std::mt19937 gen(seed); + dist_type dist(lower_bound, upper_bound); + + map(tensor, true); + + Window window; + window.use_tensor_dimensions(tensor.info()->tensor_shape()); + + Iterator it(&tensor, window); + execute_window_loop(window, [&](const Coordinates &) + { + *reinterpret_cast(it.ptr()) = dist(gen); + }, + it); unmap(tensor); } +template +void fill_random_tensor(TensorType &tensor, T lower_bound = std::numeric_limits::lowest(), T upper_bound = std::numeric_limits::max()) +{ + std::random_device rd; + fill_random_tensor(tensor, rd(), lower_bound, upper_bound); +} + template void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt) { -- cgit v1.2.1