Create fill functions available everywhere for easy debugging
authorGiorgio Arena <giorgio.arena@arm.com>
Tue, 15 Dec 2020 17:15:43 +0000 (17:15 +0000)
committerGeorgios Pinitas <georgios.pinitas@arm.com>
Fri, 18 Dec 2020 16:39:45 +0000 (16:39 +0000)
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>
utils/Utils.h

index 7eeeae54191aca5fd25b5adf121d42c99cb456bf..ca4509778b9211f7d69fa69c167068840d4c139e 100644 (file)
@@ -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)
 {