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] Add E2E test for -foffload-fp32-prec-div/sqrt #17037

Open
wants to merge 6 commits into
base: sycl
Choose a base branch
from
Open
Changes from 2 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/test-e2e/DeviceLib/built-ins/offload-prec-div-sqrt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// RUN: %{build} -foffload-fp32-prec-div -foffload-fp32-prec-sqrt -o %t.out
// RUN: %{run} %t.out

// Test if div and sqrt become precise from IEEE-754 perspective when
// -foffload-fp32-prec-div -foffload-fp32-prec-sqrt are passed.

#include <cmath>
#include <sycl/detail/core.hpp>
#include <sycl/usm.hpp>

constexpr float value = 560.0f;
constexpr float divider = 280.0f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test of division is 560/280 ? That has an exact solution ( 2 ) . I know the float number space is sparse, but is 560 / 280 really a test that'll result in a measurable ULP difference? Wouldn't 279 be a better divider?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the divider to 279.9f


// Reference
// https://github.com/KhronosGroup/SYCL-CTS/blob/SYCL-2020/util/accuracy.h
template <typename T> T get_ulp_std(T x) {
const T inf = std::numeric_limits<T>::infinity();
const T negative = std::fabs(std::nextafter(x, -inf) - x);
const T positive = std::fabs(std::nextafter(x, inf) - x);
return std::fmin(negative, positive);
}

template <typename T> int ulp_difference(const T &lhs, const T &rhs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it return int?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention was to remove ulps diff as an integer, but implicit cast was undesired here (and actually was wrong). I've simplified the function.

return get_ulp_std(lhs) - get_ulp_std(rhs);
}

void test_div() {
sycl::queue q(sycl::default_selector_v);
float *in_value = (float *)sycl::malloc_shared(sizeof(float), q);
float *in_divider = (float *)sycl::malloc_shared(sizeof(float), q);
float *output = (float *)sycl::malloc_shared(sizeof(float), q);
*in_value = value;
*in_divider = divider;
q.submit([&](sycl::handler &h) {
h.single_task([=] {
float res = *in_value / *in_divider;
*output = res;
});
}).wait();

float hostRef = value / divider;
int ulpDiff = ulp_difference<float>(hostRef, *output);
assert(std::abs(ulpDiff) < 1 && "Division is not precise");
}

void test_sqrt() {
sycl::queue q(sycl::default_selector_v);
float *in_value = (float *)sycl::malloc_shared(sizeof(float), q);
float *output = (float *)sycl::malloc_shared(sizeof(float), q);
*in_value = value;
q.submit([&](sycl::handler &h) {
h.single_task([=] {
float res = sycl::sqrt(*in_value);
*output = res;
});
}).wait();

float hostRef = std::sqrt(value);
int ulpDiff = ulp_difference<float>(hostRef, *output);
assert(std::abs(ulpDiff) < 1 && "Sqrt is not precise");
}

int main() {
test_div();
test_sqrt();
return 0;
}