-
Notifications
You must be signed in to change notification settings - Fork 769
[SYCL] Implement bf16 conversions on host device #5954
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
steffenlarsen
merged 12 commits into
intel:sycl
from
MrSidims:private/MrSidims/bf16_host
Aug 24, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
90ea38c
[SYCL] Implement bf16 conversions on host device
MrSidims a91a5b5
Clang-format
MrSidims 0636c0b
Fix test
MrSidims 8055dd1
Final fixes
MrSidims e75ba37
Merge remote-tracking branch 'origin/sycl' into private/MrSidims/bf16…
MrSidims 1c4cccc
Apply intel/llvm review change
MrSidims f97c42a
Merge remote-tracking branch 'intel/sycl' into private/MrSidims/bf16_…
steffenlarsen e5c66f4
Fix extension location and namespace in tests
steffenlarsen 708c809
Merge branch 'sycl' into private/MrSidims/bf16_host
steffenlarsen d16b7bb
Merge remote-tracking branch 'origin/sycl' into private/MrSidims/bf16…
MrSidims 81c7411
Fix namespace
MrSidims cf7d68d
Grammar fix
MrSidims 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 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,88 @@ | ||
//==------------ bfloat16_host.cpp - SYCL vectors test ---------------------==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// RUN: %clangxx -fsycl %s -o %t.out | ||
// RUN: %RUN_ON_HOST %t.out | ||
#include <sycl/ext/oneapi/experimental/bfloat16.hpp> | ||
#include <sycl/sycl.hpp> | ||
|
||
#include <cmath> | ||
#include <cstdint> | ||
#include <iostream> | ||
#include <limits> | ||
#include <string> | ||
|
||
// 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') * 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') * pow(2, 31 - I); | ||
Helper.RawData.Mantissa = Mantissa; | ||
return Helper.Value; | ||
} | ||
|
||
bool check_bf16_from_float(float Val, uint16_t Expected) { | ||
uint16_t Result = sycl::ext::oneapi::experimental::bfloat16::from_float(Val); | ||
if (Result != Expected) { | ||
std::cout << "from_float check for Val = " << Val << " failed!\n" | ||
<< "Expected " << Expected << " Got " << Result << "\n"; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool check_bf16_to_float(uint16_t Val, float Expected) { | ||
float Result = sycl::ext::oneapi::experimental::bfloat16::to_float(Val); | ||
if (Result != Expected) { | ||
std::cout << "to_float check for Val = " << Val << " failed!\n" | ||
<< "Expected " << Expected << " Got " << Result << "\n"; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int main() { | ||
bool Success = | ||
check_bf16_from_float(0.0f, std::stoi("0000000000000000", nullptr, 2)); | ||
Success &= | ||
check_bf16_from_float(42.0f, std::stoi("100001000101000", nullptr, 2)); | ||
Success &= check_bf16_from_float(std::numeric_limits<float>::min(), | ||
std::stoi("0000000010000000", nullptr, 2)); | ||
Success &= check_bf16_from_float(std::numeric_limits<float>::max(), | ||
std::stoi("0111111110000000", nullptr, 2)); | ||
Success &= check_bf16_from_float(std::numeric_limits<float>::quiet_NaN(), | ||
std::stoi("1111111111000001", nullptr, 2)); | ||
|
||
Success &= check_bf16_to_float( | ||
0, bitsToFloatConv(std::string("00000000000000000000000000000000"))); | ||
Success &= check_bf16_to_float( | ||
1, bitsToFloatConv(std::string("01000111100000000000000000000000"))); | ||
Success &= check_bf16_to_float( | ||
42, bitsToFloatConv(std::string("01001010001010000000000000000000"))); | ||
Success &= check_bf16_to_float( | ||
std::numeric_limits<uint16_t>::max(), | ||
bitsToFloatConv(std::string("01001111011111111111111100000000"))); | ||
if (!Success) | ||
return -1; | ||
return 0; | ||
} |
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.