Skip to content

Commit 7ea8147

Browse files
[SYCL][NFC] Rewrite some LIT tests to unittests (#15783)
The intent is to optimize our testing suite by avoiding device compilation where it isn't necessary --------- Patch-by: Alexey Sachkov <[email protected]> Co-authored-by: Marcos Maronas <[email protected]>
1 parent 799d267 commit 7ea8147

File tree

6 files changed

+101
-108
lines changed

6 files changed

+101
-108
lines changed

sycl/test/basic_tests/accessor/zero-dim-host-accessor.cpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

sycl/test/extensions/bfloat16_host.cpp

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <sycl/ext/oneapi/bfloat16.hpp>
2+
#include <sycl/sycl.hpp>
3+
4+
#include <gtest/gtest.h>
5+
6+
#include <cmath>
7+
#include <limits>
8+
#include <string>
9+
10+
namespace {
11+
// Helper to convert the expected bits to float value to compare with the result.
12+
typedef union {
13+
float Value;
14+
struct {
15+
uint32_t Mantissa : 23;
16+
uint32_t Exponent : 8;
17+
uint32_t Sign : 1;
18+
} RawData;
19+
} floatConvHelper;
20+
21+
float bitsToFloatConv(std::string Bits) {
22+
floatConvHelper Helper;
23+
Helper.RawData.Sign = static_cast<uint32_t>(Bits[0] - '0');
24+
uint32_t Exponent = 0;
25+
for (size_t I = 1; I != 9; ++I)
26+
Exponent = Exponent + static_cast<uint32_t>(Bits[I] - '0') * std::pow(2, 8 - I);
27+
Helper.RawData.Exponent = Exponent;
28+
uint32_t Mantissa = 0;
29+
for (size_t I = 9; I != 32; ++I)
30+
Mantissa = Mantissa + static_cast<uint32_t>(Bits[I] - '0') * std::pow(2, 31 - I);
31+
Helper.RawData.Mantissa = Mantissa;
32+
return Helper.Value;
33+
}
34+
} // namespace
35+
36+
TEST(BFloat16, BF16FromFloat) {
37+
sycl::ext::oneapi::bfloat16 B = 0.0f;
38+
auto Result = sycl::bit_cast<uint16_t>(B);
39+
ASSERT_EQ(Result, std::stoi("0000000000000000", nullptr, 2));
40+
41+
B = 42.0f;
42+
Result = sycl::bit_cast<uint16_t>(B);
43+
ASSERT_EQ(Result, std::stoi("100001000101000", nullptr, 2));
44+
45+
B = std::numeric_limits<float>::min();
46+
Result = sycl::bit_cast<uint16_t>(B);
47+
ASSERT_EQ(Result, std::stoi("0000000010000000", nullptr, 2));
48+
49+
B = std::numeric_limits<float>::max();
50+
Result = sycl::bit_cast<uint16_t>(B);
51+
ASSERT_EQ(Result, std::stoi("0111111110000000", nullptr, 2));
52+
53+
B = std::numeric_limits<float>::quiet_NaN();
54+
Result = sycl::bit_cast<uint16_t>(B);
55+
ASSERT_EQ(Result, std::stoi("1111111111000001", nullptr, 2));
56+
}
57+
58+
TEST(BFloat16, BF16ToFloat) {
59+
// See https://float.exposed/b0xffff
60+
uint16_t V = 0;
61+
float Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
62+
ASSERT_EQ(Res,
63+
bitsToFloatConv(std::string("00000000000000000000000000000000")));
64+
65+
V = 1;
66+
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
67+
ASSERT_EQ(Res,
68+
bitsToFloatConv(std::string("00000000000000010000000000000000")));
69+
70+
V = 42;
71+
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
72+
ASSERT_EQ(Res,
73+
bitsToFloatConv(std::string("00000000001010100000000000000000")));
74+
75+
// std::numeric_limits<uint16_t>::max() - 0xffff is bfloat16 -Nan and
76+
// -Nan == -Nan check in check_bf16_to_float would fail, so use not Nan:
77+
V = 65407;
78+
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
79+
ASSERT_EQ(Res,
80+
bitsToFloatConv(std::string("11111111011111110000000000000000")));
81+
}

sycl/unittests/Extensions/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_sycl_unittest(ExtensionsTests OBJECT
1717
NoDeviceIPVersion.cpp
1818
WorkGroupMemoryBackendArgument.cpp
1919
GetLastEvent.cpp
20+
BFloat16.cpp
2021
)
2122

2223
add_subdirectory(CommandGraph)

sycl/unittests/accessor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ add_sycl_unittest(AccessorTests OBJECT
88
HostAccessorReverseIterator.cpp
99
LocalAccessorDefaultCtor.cpp
1010
RuntimeProperties.cpp
11+
HostAccessor.cpp
1112
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <sycl/sycl.hpp>
2+
3+
#include <gtest/gtest.h>
4+
5+
TEST(HostAccessor, ZeroDimAccessor) {
6+
using DataT = int;
7+
using AccT = sycl::host_accessor<DataT, 0>;
8+
9+
DataT data = 42;
10+
sycl::buffer<DataT, 1> data_buf(&data, sycl::range<1>(1));
11+
AccT acc = {data_buf};
12+
13+
ASSERT_EQ(acc.get_size(), sizeof(DataT));
14+
ASSERT_EQ(acc.size(), static_cast<size_t>(1));
15+
16+
DataT &ref = acc;
17+
ASSERT_EQ(ref, data);
18+
}

0 commit comments

Comments
 (0)