diff options
author | Giorgio Arena <giorgio.arena@arm.com> | 2020-12-15 17:15:43 +0000 |
---|---|---|
committer | Georgios Pinitas <georgios.pinitas@arm.com> | 2020-12-18 16:39:45 +0000 |
commit | 82c0d7f709d6e4fdaceec5ea24e466dadaba1827 (patch) | |
tree | 5616eb48f17fc5f2d1d74c9bc7a38ef42176e880 /utils/Utils.h | |
parent | 00e86cd8343bd490789a1970b82d33ff24a05fdc (diff) | |
download | ComputeLibrary-82c0d7f709d6e4fdaceec5ea24e466dadaba1827.tar.gz |
Create fill functions available everywhere for easy debugging
Resolves: COMPMID-3817
Signed-off-by: Giorgio Arena <giorgio.arena@arm.com>
Change-Id: I56aae55b653a60a26bb0c6c86b786bccf9ddb793
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/4702
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Manuel Bottini <manuel.bottini@arm.com>
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
Diffstat (limited to 'utils/Utils.h')
-rw-r--r-- | utils/Utils.h | 93 |
1 files 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 <typename T> -void fill_random_tensor(T &tensor, float lower_bound, float upper_bound) +template <typename T, typename TensorType> +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<T *>(it_tensor.ptr()) = value; + }, + it_tensor); + + unmap(tensor); +} + +template <typename T, typename TensorType> +void fill_tensor_zero(TensorType &tensor) +{ + fill_tensor_value(tensor, T(0)); +} + +template <typename T, typename TensorType> +void fill_tensor_vector(TensorType &tensor, std::vector<T> 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<float> dist(lower_bound, upper_bound); + *reinterpret_cast<T *>(it_tensor.ptr()) = vec.at(i++); + }, + it_tensor); - execute_window_loop(window, [&](const Coordinates &) - { - *reinterpret_cast<half *>(it.ptr()) = (half)dist(gen); - }, - it); + unmap(tensor); +} - break; - } - case arm_compute::DataType::F32: - { - std::uniform_real_distribution<float> dist(lower_bound, upper_bound); +template <typename T, typename TensorType> +void fill_random_tensor(TensorType &tensor, std::random_device::result_type seed, T lower_bound = std::numeric_limits<T>::lowest(), T upper_bound = std::numeric_limits<T>::max()) +{ + constexpr bool is_half = std::is_same<T, half>::value; + constexpr bool is_integral = std::is_integral<T>::value && !is_half; - execute_window_loop(window, [&](const Coordinates &) - { - *reinterpret_cast<float *>(it.ptr()) = dist(gen); - }, - it); + using fp_dist_type = typename std::conditional<is_half, arm_compute::utils::uniform_real_distribution_fp16, std::uniform_real_distribution<T>>::type; + using dist_type = typename std::conditional<is_integral, std::uniform_int_distribution<T>, 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<T *>(it.ptr()) = dist(gen); + }, + it); unmap(tensor); } +template <typename T, typename TensorType> +void fill_random_tensor(TensorType &tensor, T lower_bound = std::numeric_limits<T>::lowest(), T upper_bound = std::numeric_limits<T>::max()) +{ + std::random_device rd; + fill_random_tensor(tensor, rd(), lower_bound, upper_bound); +} + template <typename T> void init_sgemm_output(T &dst, T &src0, T &src1, arm_compute::DataType dt) { |