Skip to content

[NFC][SYCL] Convert zero-dim-host-accessor and bfloat16_host LIT test to unittests #15783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions sycl/test/basic_tests/accessor/zero-dim-host-accessor.cpp

This file was deleted.

91 changes: 0 additions & 91 deletions sycl/test/extensions/bfloat16_host.cpp

This file was deleted.

81 changes: 81 additions & 0 deletions sycl/unittests/Extensions/BFloat16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <sycl/ext/oneapi/bfloat16.hpp>
#include <sycl/sycl.hpp>

#include <gtest/gtest.h>

#include <cmath>
#include <limits>
#include <string>

namespace {
// Helper to convert the expected bits to float value to compare with the result.
typedef union {
float Value;
struct {
uint32_t Mantissa : 23;
uint32_t Exponent : 8;
uint32_t Sign : 1;
} RawData;
} floatConvHelper;

float bitsToFloatConv(std::string Bits) {
floatConvHelper Helper;
Helper.RawData.Sign = static_cast<uint32_t>(Bits[0] - '0');
uint32_t Exponent = 0;
for (size_t I = 1; I != 9; ++I)
Exponent = Exponent + static_cast<uint32_t>(Bits[I] - '0') * std::pow(2, 8 - I);
Helper.RawData.Exponent = Exponent;
uint32_t Mantissa = 0;
for (size_t I = 9; I != 32; ++I)
Mantissa = Mantissa + static_cast<uint32_t>(Bits[I] - '0') * std::pow(2, 31 - I);
Helper.RawData.Mantissa = Mantissa;
return Helper.Value;
}
} // namespace

TEST(BFloat16, BF16FromFloat) {
sycl::ext::oneapi::bfloat16 B = 0.0f;
auto Result = sycl::bit_cast<uint16_t>(B);
ASSERT_EQ(Result, std::stoi("0000000000000000", nullptr, 2));

B = 42.0f;
Result = sycl::bit_cast<uint16_t>(B);
ASSERT_EQ(Result, std::stoi("100001000101000", nullptr, 2));

B = std::numeric_limits<float>::min();
Result = sycl::bit_cast<uint16_t>(B);
ASSERT_EQ(Result, std::stoi("0000000010000000", nullptr, 2));

B = std::numeric_limits<float>::max();
Result = sycl::bit_cast<uint16_t>(B);
ASSERT_EQ(Result, std::stoi("0111111110000000", nullptr, 2));

B = std::numeric_limits<float>::quiet_NaN();
Result = sycl::bit_cast<uint16_t>(B);
ASSERT_EQ(Result, std::stoi("1111111111000001", nullptr, 2));
}

TEST(BFloat16, BF16ToFloat) {
// See https://float.exposed/b0xffff
uint16_t V = 0;
float Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
ASSERT_EQ(Res,
bitsToFloatConv(std::string("00000000000000000000000000000000")));

V = 1;
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
ASSERT_EQ(Res,
bitsToFloatConv(std::string("00000000000000010000000000000000")));

V = 42;
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
ASSERT_EQ(Res,
bitsToFloatConv(std::string("00000000001010100000000000000000")));

// std::numeric_limits<uint16_t>::max() - 0xffff is bfloat16 -Nan and
// -Nan == -Nan check in check_bf16_to_float would fail, so use not Nan:
V = 65407;
Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V);
ASSERT_EQ(Res,
bitsToFloatConv(std::string("11111111011111110000000000000000")));
}
1 change: 1 addition & 0 deletions sycl/unittests/Extensions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_sycl_unittest(ExtensionsTests OBJECT
KernelProperties.cpp
NoDeviceIPVersion.cpp
GetLastEvent.cpp
BFloat16.cpp
)

add_subdirectory(CommandGraph)
Expand Down
1 change: 1 addition & 0 deletions sycl/unittests/accessor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ add_sycl_unittest(AccessorTests OBJECT
HostAccessorReverseIterator.cpp
LocalAccessorDefaultCtor.cpp
RuntimeProperties.cpp
HostAccessor.cpp
)
18 changes: 18 additions & 0 deletions sycl/unittests/accessor/HostAccessor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <sycl/sycl.hpp>

#include <gtest/gtest.h>

TEST(HostAccessor, ZeroDimAccessor) {
using DataT = int;
using AccT = sycl::host_accessor<DataT, 0>;

DataT data = 42;
sycl::buffer<DataT, 1> data_buf(&data, sycl::range<1>(1));
AccT acc = {data_buf};

ASSERT_EQ(acc.get_size(), sizeof(DataT));
ASSERT_EQ(acc.size(), static_cast<size_t>(1));

DataT &ref = acc;
ASSERT_EQ(ref, data);
}
Loading