Skip to content

Commit dad43b7

Browse files
authored
[SYCL][Test] Enable bf16 math test on all backends (#8999)
Signed-off-by: jinge90 <[email protected]>
1 parent d52760f commit dad43b7

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

sycl/include/sycl/ext/oneapi/bfloat16.hpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
#pragma once
1010

1111
#include <CL/__spirv/spirv_ops.hpp>
12+
#include <sycl/builtins.hpp>
1213
#include <sycl/half_type.hpp>
1314

14-
#if !defined(__SYCL_DEVICE_ONLY__)
15-
#include <cmath>
16-
#endif
17-
1815
extern "C" __DPCPP_SYCL_EXTERNAL uint16_t
1916
__devicelib_ConvertFToBF16INTEL(const float &) noexcept;
2017
extern "C" __DPCPP_SYCL_EXTERNAL float
@@ -46,15 +43,8 @@ class bfloat16 {
4643
~bfloat16() = default;
4744

4845
private:
49-
// Explicit conversion functions
50-
static detail::Bfloat16StorageT from_float(const float &a) {
51-
#if defined(__SYCL_DEVICE_ONLY__)
52-
#if defined(__NVPTX__)
53-
#if (__SYCL_CUDA_ARCH__ >= 800)
54-
return __nvvm_f2bf16_rn(a);
55-
#else
56-
// TODO find a better way to check for NaN
57-
if (a != a)
46+
static detail::Bfloat16StorageT from_float_fallback(const float &a) {
47+
if (sycl::isnan(a))
5848
return 0xffc1;
5949
union {
6050
uint32_t intStorage;
@@ -64,23 +54,24 @@ class bfloat16 {
6454
// Do RNE and truncate
6555
uint32_t roundingBias = ((intStorage >> 16) & 0x1) + 0x00007FFF;
6656
return static_cast<uint16_t>((intStorage + roundingBias) >> 16);
57+
}
58+
59+
// Explicit conversion functions
60+
static detail::Bfloat16StorageT from_float(const float &a) {
61+
#if defined(__SYCL_DEVICE_ONLY__)
62+
#if defined(__NVPTX__)
63+
#if (__SYCL_CUDA_ARCH__ >= 800)
64+
return __nvvm_f2bf16_rn(a);
65+
#else
66+
return from_float_fallback(a);
6767
#endif
68+
#elif defined(__AMDGCN__)
69+
return from_float_fallback(a);
6870
#else
6971
return __devicelib_ConvertFToBF16INTEL(a);
7072
#endif
71-
#else
72-
// In case float value is nan - propagate bfloat16's qnan
73-
if (std::isnan(a))
74-
return 0xffc1;
75-
union {
76-
uint32_t intStorage;
77-
float floatValue;
78-
};
79-
floatValue = a;
80-
// Do RNE and truncate
81-
uint32_t roundingBias = ((intStorage >> 16) & 0x1) + 0x00007FFF;
82-
return static_cast<uint16_t>((intStorage + roundingBias) >> 16);
8373
#endif
74+
return from_float_fallback(a);
8475
}
8576

8677
static float to_float(const detail::Bfloat16StorageT &a) {

sycl/test-e2e/BFloat16/bfloat16_builtins.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
// REQUIRES: cuda
2-
//
3-
// Currently this test fails to compile for backends other than cuda.
4-
// Other backends could use this test when bfloat16 math function support is
5-
// added.
6-
//
7-
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %s -o %t.out -Xsycl-target-backend --cuda-gpu-arch=sm_80
8-
// RUN: %t.out
1+
// RUN: %clangxx -fsycl -fsycl-targets=%sycl_triple %if cuda %{ -Xsycl-target-backend --cuda-gpu-arch=sm_80 %} %s -o %t.out
2+
// Currently the feature isn't supported on FPGA.
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
95
#include <sycl/sycl.hpp>
106

117
#include <cmath>
@@ -40,7 +36,8 @@ bool check(bool a, bool b) { return (a != b); }
4036
cgh); \
4137
accessor<int, 1, access::mode::write, target::device> ERR(err_buf, cgh); \
4238
cgh.parallel_for(N, [=](id<1> index) { \
43-
if (check(NAME(bfloat16{A[index]}), NAME(A[index]))) { \
39+
if (check(sycl::ext::oneapi::experimental::NAME(bfloat16{A[index]}), \
40+
sycl::NAME(A[index]))) { \
4441
ERR[0] = 1; \
4542
} \
4643
}); \
@@ -63,7 +60,7 @@ bool check(bool a, bool b) { return (a != b); }
6360
} \
6461
marray<RETTY, SZ> res = NAME(arg); \
6562
for (int i = 0; i < SZ; i++) { \
66-
if (check(res[i], NAME(A[index][i]))) { \
63+
if (check(res[i], sycl::NAME(A[index][i]))) { \
6764
ERR[0] = 1; \
6865
} \
6966
} \

0 commit comments

Comments
 (0)