-
Notifications
You must be signed in to change notification settings - Fork 794
[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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
029cee5
[NFC][SYCL] Convert zero-dim-host-accessor.cpp and bfloat16_host.cpp …
uditagarwal97 bc609de
Add unit tests
uditagarwal97 eea4806
Add explicit typecast to size_t
uditagarwal97 96c9a8b
Update sycl/unittests/Extensions/BFloat16.cpp
uditagarwal97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"))); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.