Skip to content

[ET-SDK] Enable data_is_close for Half tensor #3790

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

Closed
wants to merge 2 commits into from
Closed
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
71 changes: 50 additions & 21 deletions sdk/bundled_program/bundled_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,37 @@ TensorImpl impl_like(bundled_program_flatbuffer::Tensor* bundled_tensor) {
#endif

/**
* Returns true if the two arrays are close according to the description on
* Returns true if the two elements are close according to the description on
* `tensors_are_close()`.
*
* T must be a floating point type. Non-floating point data should be compared
* directly.
*/
template <
typename T,
typename = std::enable_if_t<std::is_floating_point<T>::value>>
bool elem_is_close(const T& ai, const T& bi, double rtol, double atol) {
if (std::isnan(ai) && std::isnan(bi)) {
// NaN == NaN
} else if (
!std::isfinite(ai) && !std::isfinite(bi) && ((ai > 0) == (bi > 0))) {
// -Inf == -Inf
// +Inf == +Inf
} else if (rtol == 0 && atol == 0) {
// Exact comparison; avoid unnecessary math.
if (ai != bi) {
return false;
}
} else {
auto allowed_error = atol + std::abs(rtol * bi);
auto actual_error = std::abs(ai - bi);
if (!std::isfinite(actual_error) || actual_error > allowed_error) {
return false;
}
}
return true;
}

template <
typename T,
typename = std::enable_if_t<std::is_floating_point<T>::value>>
Expand All @@ -89,26 +114,23 @@ bool data_is_close(
double rtol,
double atol) {
for (size_t i = 0; i < numel; i++) {
const auto ai = a[i];
const auto bi = b[i];

if (std::isnan(ai) && std::isnan(bi)) {
// NaN == NaN
} else if (
!std::isfinite(ai) && !std::isfinite(bi) && ((ai > 0) == (bi > 0))) {
// -Inf == -Inf
// +Inf == +Inf
} else if (rtol == 0 && atol == 0) {
// Exact comparison; avoid unnecessary math.
if (ai != bi) {
return false;
}
} else {
auto allowed_error = atol + std::abs(rtol * bi);
auto actual_error = std::abs(ai - bi);
if (!std::isfinite(actual_error) || actual_error > allowed_error) {
return false;
}
if (!elem_is_close(a[i], b[i], rtol, atol)) {
return false;
}
}
return true;
}

bool data_is_close_half(
const Half* a,
const Half* b,
size_t numel,
double rtol,
double atol) {
for (size_t i = 0; i < numel; i++) {
if (!elem_is_close(
static_cast<double>(a[i]), static_cast<double>(b[i]), rtol, atol)) {
return false;
}
}
return true;
Expand Down Expand Up @@ -177,6 +199,13 @@ bool tensors_are_close(
bundled_tensor.numel(),
rtol,
atol);
} else if (bundled_tensor.scalar_type() == ScalarType::Half) {
return data_is_close_half(
bundled_tensor.const_data_ptr<Half>(),
method_output_tensor.const_data_ptr<Half>(),
bundled_tensor.numel(),
rtol,
atol);
} else {
// Non-floating-point types can be compared bitwise.
return memcmp(
Expand Down
Loading