aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorGiorgio Arena <giorgio.arena@arm.com>2021-01-06 11:34:57 +0000
committerGiorgio Arena <giorgio.arena@arm.com>2021-01-07 09:56:45 +0000
commita8e2aeb7d2d46a7ab0e9523de145af9920fc1fa3 (patch)
tree21547ff0230f1cd842bae21817d4f54c6888664a /utils
parentc5a613982c12977cef2e2e16aaf9c50fa1629a88 (diff)
downloadComputeLibrary-a8e2aeb7d2d46a7ab0e9523de145af9920fc1fa3.tar.gz
Generalize custom uniform generator for floating point data types with 16 bits
- Change name of uniform_real_distribution_fp16 to uniform_real_distribution_16bit, and make it also accept bfloat16 data type Resolves: COMPMID-4057 Signed-off-by: Giorgio Arena <giorgio.arena@arm.com> Change-Id: Id2f1a84b9c9f09cb260a0785add4fc5954d5853a Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/4768 Reviewed-by: Michele Di Giorgio <michele.digiorgio@arm.com> Comments-Addressed: Arm Jenkins <bsgcomp@arm.com> Tested-by: Arm Jenkins <bsgcomp@arm.com>
Diffstat (limited to 'utils')
-rw-r--r--utils/Utils.h19
1 files changed, 11 insertions, 8 deletions
diff --git a/utils/Utils.h b/utils/Utils.h
index 9862514a9e..9c9026edc4 100644
--- a/utils/Utils.h
+++ b/utils/Utils.h
@@ -294,16 +294,19 @@ inline void unmap(GCTensor &tensor)
* uniform_real_distribution<half> generates values that get rounded off to zero, causing
* differences between ACL and reference implementation
*/
-class uniform_real_distribution_fp16
+template <typename T>
+class uniform_real_distribution_16bit
{
+ static_assert(std::is_same<T, half>::value || std::is_same<T, bfloat16>::value, "Only half and bfloat16 data types supported");
+
public:
- using result_type = half;
+ using result_type = T;
/** Constructor
*
* @param[in] min Minimum value of the distribution
* @param[in] max Maximum value of the distribution
*/
- explicit uniform_real_distribution_fp16(float min = 0.f, float max = 1.0)
+ explicit uniform_real_distribution_16bit(float min = 0.f, float max = 1.0)
: dist(min, max)
{
}
@@ -312,9 +315,9 @@ public:
*
* @param[in] gen an uniform random bit generator object
*/
- half operator()(std::mt19937 &gen)
+ T operator()(std::mt19937 &gen)
{
- return half(dist(gen));
+ return T(dist(gen));
}
private:
@@ -770,10 +773,10 @@ void fill_tensor_vector(TensorType &tensor, std::vector<T> vec)
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;
+ constexpr bool is_fp_16bit = std::is_same<T, half>::value || std::is_same<T, bfloat16>::value;
+ constexpr bool is_integral = std::is_integral<T>::value && !is_fp_16bit;
- using fp_dist_type = typename std::conditional<is_half, arm_compute::utils::uniform_real_distribution_fp16, std::uniform_real_distribution<T>>::type;
+ using fp_dist_type = typename std::conditional<is_fp_16bit, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type;
using dist_type = typename std::conditional<is_integral, std::uniform_int_distribution<T>, fp_dist_type>::type;
std::mt19937 gen(seed);