Skip to content
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

[SYCL] Optimize vec<bfloat> math builtins #14106

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
67 changes: 67 additions & 0 deletions sycl/include/sycl/ext/oneapi/experimental/bfloat16_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,26 @@ template <size_t N> sycl::marray<bool, N> isnan(sycl::marray<bfloat16, N> x) {
template <typename T, int N = num_elements_v<T>>
std::enable_if_t<is_vec_or_swizzle_bf16_v<T>, sycl::vec<int16_t, N>>
isnan(T x) {

#if defined(__SYCL_DEVICE_ONLY__) && (defined(__SPIR__) || defined(__SPIRV__))
// Convert BFloat16 vector to float vec and call isnan()
maarquitos14 marked this conversation as resolved.
Show resolved Hide resolved
sycl::vec<float, N> FVec =
x.template convert<float, sycl::rounding_mode::automatic>();
auto res = isnan(FVec);
maarquitos14 marked this conversation as resolved.
Show resolved Hide resolved

// For vec<float>, the return type of isnan is vec<int32_t> so,
// an explicit conversion is required to vec<int16_t>.
return res.template convert<int16_t>();
#else

sycl::vec<int16_t, N> res;
for (size_t i = 0; i < N; i++) {
// The result of isnan is 0 or 1 but SPEC requires
// isnan() of vec/swizzle to return -1 or 0.
res[i] = isnan(x[i]) ? -1 : 0;
}
return res;
#endif
}

/******************* fabs ********************/
Expand Down Expand Up @@ -120,11 +133,19 @@ sycl::marray<bfloat16, N> fabs(sycl::marray<bfloat16, N> x) {
template <typename T, int N = num_elements_v<T>>
std::enable_if_t<is_vec_or_swizzle_bf16_v<T>, sycl::vec<bfloat16, N>>
fabs(T x) {
#if defined(__SYCL_DEVICE_ONLY__) && (defined(__SPIR__) || defined(__SPIRV__))
// Convert BFloat16 vector to float vec.
sycl::vec<float, N> FVec =
x.template convert<float, sycl::rounding_mode::automatic>();
auto res = fabs(FVec);
return res.template convert<bfloat16>();
#else
sycl::vec<bfloat16, N> res;
for (size_t i = 0; i < N; i++) {
res[i] = fabs(x[i]);
}
return res;
#endif
}

/******************* fmin ********************/
Expand Down Expand Up @@ -193,11 +214,21 @@ std::enable_if_t<is_vec_or_swizzle_bf16_v<T1> && is_vec_or_swizzle_bf16_v<T2> &&
N1 == N2,
sycl::vec<bfloat16, N1>>
fmin(T1 x, T2 y) {
#if defined(__SYCL_DEVICE_ONLY__) && (defined(__SPIR__) || defined(__SPIRV__))
// Convert BFloat16 vectors to float vecs.
sycl::vec<float, N1> FVecX =
x.template convert<float, sycl::rounding_mode::automatic>();
sycl::vec<float, N1> FVecY =
y.template convert<float, sycl::rounding_mode::automatic>();
auto res = fmin(FVecX, FVecY);
return res.template convert<bfloat16>();
#else
sycl::vec<bfloat16, N1> res;
for (size_t i = 0; i < N1; i++) {
res[i] = fmin(x[i], y[i]);
}
return res;
#endif
}

/******************* fmax ********************/
Expand Down Expand Up @@ -265,11 +296,21 @@ std::enable_if_t<is_vec_or_swizzle_bf16_v<T1> && is_vec_or_swizzle_bf16_v<T2> &&
N1 == N2,
sycl::vec<bfloat16, N1>>
fmax(T1 x, T2 y) {
#if defined(__SYCL_DEVICE_ONLY__) && (defined(__SPIR__) || defined(__SPIRV__))
// Convert BFloat16 vectors to float vecs.
sycl::vec<float, N1> FVecX =
x.template convert<float, sycl::rounding_mode::automatic>();
sycl::vec<float, N1> FVecY =
y.template convert<float, sycl::rounding_mode::automatic>();
auto res = fmax(FVecX, FVecY);
return res.template convert<bfloat16>();
#else
sycl::vec<bfloat16, N1> res;
for (size_t i = 0; i < N1; i++) {
res[i] = fmax(x[i], y[i]);
}
return res;
#endif
}

/******************* fma *********************/
Expand Down Expand Up @@ -327,11 +368,24 @@ std::enable_if_t<is_vec_or_swizzle_bf16_v<T1> && is_vec_or_swizzle_bf16_v<T2> &&
is_vec_or_swizzle_bf16_v<T3> && N1 == N2 && N2 == N3,
sycl::vec<bfloat16, N1>>
fma(T1 x, T2 y, T3 z) {
#if defined(__SYCL_DEVICE_ONLY__) && (defined(__SPIR__) || defined(__SPIRV__))
// Convert BFloat16 vectors to float vecs.
sycl::vec<float, N1> FVecX =
x.template convert<float, sycl::rounding_mode::automatic>();
sycl::vec<float, N1> FVecY =
y.template convert<float, sycl::rounding_mode::automatic>();
sycl::vec<float, N1> FVecZ =
z.template convert<float, sycl::rounding_mode::automatic>();

auto res = fma(FVecX, FVecY, FVecZ);
return res.template convert<bfloat16>();
#else
sycl::vec<bfloat16, N1> res;
for (size_t i = 0; i < N1; i++) {
res[i] = fma(x[i], y[i], z[i]);
}
return res;
#endif
}

/******************* unary math operations ********************/
Expand All @@ -352,6 +406,18 @@ fma(T1 x, T2 y, T3 z) {
return res; \
}

#if defined(__SYCL_DEVICE_ONLY__) && (defined(__SPIR__) || defined(__SPIRV__))
#define BFLOAT16_MATH_FP32_WRAPPERS_VEC(op) \
/* Overload for BF16 vec and swizzles. */ \
maarquitos14 marked this conversation as resolved.
Show resolved Hide resolved
template <typename T, int N = num_elements_v<T>> \
std::enable_if_t<is_vec_or_swizzle_bf16_v<T>, sycl::vec<bfloat16, N>> op( \
T x) { \
sycl::vec<float, N> FVec = \
x.template convert<float, sycl::rounding_mode::automatic>(); \
auto res = op(FVec); \
return res.template convert<bfloat16>(); \
}
#else
#define BFLOAT16_MATH_FP32_WRAPPERS_VEC(op) \
/* Overload for BF16 vec and swizzles. */ \
template <typename T, int N = num_elements_v<T>> \
Expand All @@ -363,6 +429,7 @@ fma(T1 x, T2 y, T3 z) {
} \
return res; \
}
#endif

BFLOAT16_MATH_FP32_WRAPPERS(ceil)
BFLOAT16_MATH_FP32_WRAPPERS_MARRAY(ceil)
Expand Down
6 changes: 5 additions & 1 deletion sycl/include/sycl/vector_preview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,11 @@ class SwizzleOp {
template <typename convertT, rounding_mode roundingMode>
vec<convertT, sizeof...(Indexes)> convert() const {
// First materialize the swizzle to vec_t and then apply convert() to it.
vec_t Tmp = *this;
vec_t Tmp;
std::array<int, getNumElements()> Idxs{Indexes...};
for (size_t I = 0; I < Idxs.size(); ++I) {
Tmp[I] = (*m_Vector)[Idxs[I]];
}
return Tmp.template convert<convertT, roundingMode>();
}

Expand Down
12 changes: 5 additions & 7 deletions sycl/test-e2e/BFloat16/bfloat16_vec_builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ bool check(bool a, bool b) { return (a != b); }
for (int i = 0; i < SZ; i++) { \
arg[i] = INPVAL; \
} \
/* Perform the operation. */ \
vec<RETTY, SZ> \
res = sycl::ext::oneapi::experimental::NAME(arg); \
/* Perform the operation. */ \
maarquitos14 marked this conversation as resolved.
Show resolved Hide resolved
vec<RETTY, SZ> res = sycl::ext::oneapi::experimental::NAME(arg); \
vec<RETTY, 2> res2 = \
sycl::ext::oneapi::experimental::NAME(arg.template swizzle<0, 0>()); \
/* Check the result. */ \
/* Check the result. */ \
maarquitos14 marked this conversation as resolved.
Show resolved Hide resolved
if (res2[0] != res[0] || res2[1] != res[0]) { \
ERR[0] += 1; \
} \
Expand All @@ -56,9 +55,8 @@ bool check(bool a, bool b) { return (a != b); }
arg[i] = INPVAL; \
arg2[i] = inpVal2; \
} \
/* Perform the operation. */ \
vec<RETTY, SZ> \
res = sycl::ext::oneapi::experimental::NAME(arg, arg2); \
/* Perform the operation. */ \
maarquitos14 marked this conversation as resolved.
Show resolved Hide resolved
vec<RETTY, SZ> res = sycl::ext::oneapi::experimental::NAME(arg, arg2); \
/* Swizzle and vec different combination. */ \
vec<RETTY, 2> res2 = sycl::ext::oneapi::experimental::NAME( \
arg.template swizzle<0, 0>(), arg2.template swizzle<0, 0>()); \
Expand Down
Loading
Loading