Utests: Fix the failure for half math tests.

Submitted by junyan.he@inbox.com on Nov. 11, 2015, 9:59 a.m.

Details

Message ID 1447235956-3143-1-git-send-email-junyan.he@inbox.com
State New
Headers show
Series "Utests: Fix the failure for half math tests." ( rev: 1 ) in Beignet

Not browsing as part of any series.

Commit Message

junyan.he@inbox.com Nov. 11, 2015, 9:59 a.m.
From: Junyan He <junyan.he@linux.intel.com>

We do not have native half type support on X86 platforms.
The half math functions on CPU side are just used in utests,
so we do not want to import the soft imitation code or add
dependency on some math libs for half. We just use float to
to calculate the reference value. This causes the diff between
CPU results and GPU results. We use random func to generate src
value but when this src value is very close to pi or pi/2,
the truncation diff imported by float -> half will be magnified
a lot in the result of some math functions, e.g. sin, cos and tan.
We now just use a float table as src to fix this.

Signed-off-by: Junyan He <junyan.he@linux.intel.com>
---
 utests/compiler_half.cpp | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

Patch hide | download patch | download mbox

diff --git a/utests/compiler_half.cpp b/utests/compiler_half.cpp
index 163573f..b5480a0 100644
--- a/utests/compiler_half.cpp
+++ b/utests/compiler_half.cpp
@@ -172,7 +172,7 @@  void compiler_half_basic(void)
   for (int32_t i = 0; i < (int32_t) n; ++i) {
     tmp_f = __half_to_float(((uint16_t *)buf_data[1])[i]);
     memcpy(&f, &tmp_f, sizeof(float));
-    printf("%f %f\n", f, fdst[i]);
+    //printf("%f %f\n", f, fdst[i]);
     OCL_ASSERT(fabs(f - fdst[i]) <= 0.01 * fabs(fdst[i]) || (fdst[i] == 0.0 && f == 0.0));
   }
   OCL_UNMAP_BUFFER(1);
@@ -180,11 +180,18 @@  void compiler_half_basic(void)
 
 MAKE_UTEST_FROM_FUNCTION(compiler_half_basic);
 
+static const int half_n = 16;
+static float half_test_src[half_n] = {
+  -0.23455f, 1.23413f, 2.3412, 8.234f,
+  -122.31f, -14.233f, 0.0023f, 99.322f,
+  0.0f, 0.332f, 123.12f, -0.003f,
+  16.0f, 19.22f, 128.006f, 25.032f
+};
 
 #define HALF_MATH_TEST_1ARG(NAME, CPPNAME, RANGE_L, RANGE_H)            \
   void compiler_half_math_##NAME(void)                                  \
   {                                                                     \
-    const size_t n = 16;                                                \
+    const size_t n = half_n;                                                \
     uint16_t hsrc[n];                                                   \
     float fsrc[n], fdst[n];                                             \
     uint32_t tmp_f;                                                     \
@@ -202,7 +209,7 @@  MAKE_UTEST_FROM_FUNCTION(compiler_half_basic);
     locals[0] = 16;                                                     \
                                                                         \
     for (int32_t i = 0; i < (int32_t) n; ++i) {                         \
-      fsrc[i] = RANGE_L + ((rand()%1000) / 1000.0f ) * ((RANGE_H) - (RANGE_L)); \
+      fsrc[i] = half_test_src[i];                                       \
       memcpy(&tmp_f, &fsrc[i], sizeof(float));                          \
       hsrc[i] = __float_to_half(tmp_f);                                 \
     }                                                                   \
@@ -225,7 +232,7 @@  MAKE_UTEST_FROM_FUNCTION(compiler_half_basic);
       bool isInf, infSign;                                              \
       tmp_f = __half_to_float(((uint16_t *)buf_data[1])[i], &isInf, &infSign); \
       memcpy(&f, &tmp_f, sizeof(float));                                \
-      /*printf("%.15f %.15f, diff is %%%f\n", f, fdst[i], (fabs(f - fdst[i])/fabs(fdst[i]))); */ \
+      /* printf("%.15f %.15f, diff is %f\n", f, fdst[i], (fabs(f - fdst[i])/fabs(fdst[i]))); */ \
       OCL_ASSERT(((fabs(fdst[i]) < 6e-8f) && (fabs(f) < 6e-8f)) ||      \
                  (fabs(f - fdst[i]) <= 0.03 * fabs(fdst[i])) ||         \
                  (isInf && ((infSign && fdst[i] > 65504.0f) || (!infSign && fdst[i] < -65504.0f))) || \
@@ -273,7 +280,7 @@  HALF_MATH_TEST_1ARG(ceil, ceilf, -19.0, 20.0);
       fsrc0[i] = RANGE_L + (((RANGE_H) - (RANGE_L))/n) * i;            \
       memcpy(&tmp_f, &fsrc0[i], sizeof(float));                         \
       hsrc0[i] = __float_to_half(tmp_f);                                \
-      fsrc1[i] = RANGE_L + ((rand()%1000) / 1000.0f ) * ((RANGE_H) - (RANGE_L));            \
+      fsrc1[i] = RANGE_L + (half_test_src[i/4] + 63) * ((RANGE_H) - (RANGE_L));            \
       memcpy(&tmp_f, &fsrc1[i], sizeof(float));                         \
       hsrc1[i] = __float_to_half(tmp_f);                                \
     }                                                                   \

Comments

> -----Original Message-----

> From: Beignet [mailto:beignet-bounces@lists.freedesktop.org] On Behalf Of

> junyan.he@inbox.com

> Sent: Wednesday, November 11, 2015 17:59

> To: beignet@lists.freedesktop.org

> Subject: [Beignet] [PATCH] Utests: Fix the failure for half math tests.

> 

> From: Junyan He <junyan.he@linux.intel.com>

> 

> We do not have native half type support on X86 platforms.

> The half math functions on CPU side are just used in utests, so we do not

> want to import the soft imitation code or add dependency on some math libs

> for half. We just use float to to calculate the reference value. This causes the

> diff between CPU results and GPU results. We use random func to generate

> src value but when this src value is very close to pi or pi/2, the truncation diff

> imported by float -> half will be magnified a lot in the result of some math

> functions, e.g. sin, cos and tan.

> We now just use a float table as src to fix this.

> 

> Signed-off-by: Junyan He <junyan.he@linux.intel.com>

> ---

>  utests/compiler_half.cpp | 17 ++++++++++++-----

>  1 file changed, 12 insertions(+), 5 deletions(-)

> 

> diff --git a/utests/compiler_half.cpp b/utests/compiler_half.cpp index

> 163573f..b5480a0 100644

> --- a/utests/compiler_half.cpp

> +++ b/utests/compiler_half.cpp

> @@ -172,7 +172,7 @@ void compiler_half_basic(void)

>    for (int32_t i = 0; i < (int32_t) n; ++i) {

>      tmp_f = __half_to_float(((uint16_t *)buf_data[1])[i]);

>      memcpy(&f, &tmp_f, sizeof(float));

> -    printf("%f %f\n", f, fdst[i]);

> +    //printf("%f %f\n", f, fdst[i]);

>      OCL_ASSERT(fabs(f - fdst[i]) <= 0.01 * fabs(fdst[i]) || (fdst[i] == 0.0 && f

> == 0.0));

>    }

>    OCL_UNMAP_BUFFER(1);

> @@ -180,11 +180,18 @@ void compiler_half_basic(void)

> 

>  MAKE_UTEST_FROM_FUNCTION(compiler_half_basic);

> 

> +static const int half_n = 16;

> +static float half_test_src[half_n] = {

> +  -0.23455f, 1.23413f, 2.3412, 8.234f,

> +  -122.31f, -14.233f, 0.0023f, 99.322f,

> +  0.0f, 0.332f, 123.12f, -0.003f,

> +  16.0f, 19.22f, 128.006f, 25.032f

> +};

> 

>  #define HALF_MATH_TEST_1ARG(NAME, CPPNAME, RANGE_L, RANGE_H)

Are RANGE_L and RANGE_H no longer use in this Marco?