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

Fix comparison of constant with short float NAN values #4299

Merged
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -325,32 +325,44 @@ struct Equal {
};

template <>
struct Equal<float> {
static bool equal_value(float lhs, float rhs) {
return std::abs(lhs - rhs) < 1e-5;
struct Equal<ngraph::bfloat16> {
static bool equal_value(ngraph::bfloat16 lhs, ngraph::bfloat16 rhs) {
if (lhs.to_bits() == rhs.to_bits()) {
return true;
}
return std::abs(lhs - rhs) < 1e-3;
}
};

template <>
struct Equal<double> {
static bool equal_value(double lhs, double rhs) {
return std::abs(lhs - rhs) < 1e-5;
struct Equal<ngraph::float16> {
static bool equal_value(ngraph::float16 lhs, ngraph::float16 rhs) {
if (lhs.to_bits() == rhs.to_bits()) {
return true;
}
return std::abs(lhs - rhs) < 1e-3;
}
};

template <>
struct Equal<std::vector<double>> {
static bool equal_value(const std::vector<double>& lhs, const std::vector<double>& rhs) {
return lhs.size() == rhs.size() &&
std::equal(begin(lhs), end(lhs), begin(rhs), Equal<double>::equal_value);
struct Equal<float> {
static bool equal_value(float lhs, float rhs) {
return std::abs(lhs - rhs) < 1e-4;
}
};

template <>
struct Equal<std::vector<float>> {
static bool equal_value(const std::vector<float>& lhs, const std::vector<float>& rhs) {
struct Equal<double> {
static bool equal_value(double lhs, double rhs) {
return std::abs(lhs - rhs) < 1e-5;
}
};

template <typename T>
struct Equal<std::vector<T>> {
static bool equal_value(const std::vector<T>& lhs, const std::vector<T>& rhs) {
return lhs.size() == rhs.size() &&
std::equal(begin(lhs), end(lhs), begin(rhs), Equal<float>::equal_value);
std::equal(begin(lhs), end(lhs), begin(rhs), Equal<T>::equal_value);
}
};

Expand Down Expand Up @@ -439,6 +451,45 @@ struct Equal<SpecialBodyPorts> {
}
};

using Constant = ngraph::opset1::Constant;
template <> struct Equal<std::shared_ptr<Constant>> {
static bool equal_value(const std::shared_ptr<Constant> lhs,
blesniewski marked this conversation as resolved.
Show resolved Hide resolved
const std::shared_ptr<Constant> rhs) {
const auto lhs_t = lhs->get_element_type();
const auto rhs_t = rhs->get_element_type();
if (lhs_t != rhs_t) {
return false;
}

switch (lhs_t) {
case ngraph::element::Type_t::bf16: {
auto lhs_v = lhs->cast_vector<ngraph::bfloat16>();
auto rhs_v = rhs->cast_vector<ngraph::bfloat16>();
return Equal<std::vector<ngraph::bfloat16>>::equal_value(lhs_v, rhs_v);
break;
}
case ngraph::element::Type_t::f16: {
const auto &lhs_v = lhs->cast_vector<ngraph::float16>();
const auto &rhs_v = rhs->cast_vector<ngraph::float16>();
return Equal<std::vector<ngraph::float16>>::equal_value(lhs_v, rhs_v);
break;
}
case ngraph::element::Type_t::f32: {
const auto &lhs_v = lhs->cast_vector<float>();
const auto &rhs_v = rhs->cast_vector<float>();
return Equal<std::vector<float>>::equal_value(lhs_v, rhs_v);
break;
}
default: {
const auto &lhs_v = lhs->cast_vector<double>();
const auto &rhs_v = rhs->cast_vector<double>();
return Equal<std::vector<double>>::equal_value(lhs_v, rhs_v);
break;
}
}
return false;
}
};
} // namespace equal

namespace str {
Expand Down Expand Up @@ -741,22 +792,13 @@ FunctionsComparator::Result FunctionsComparator::compare(
using Constant = ngraph::opset1::Constant;
auto const1 = ngraph::as_type_ptr<Constant>(node1->get_input_node_shared_ptr(i));
auto const2 = ngraph::as_type_ptr<Constant>(node2->get_input_node_shared_ptr(i));

const auto equal = [](std::shared_ptr<Constant> c1, std::shared_ptr<Constant> c2) {
const auto& c1v = c1->cast_vector<double>();
const auto& c2v = c2->cast_vector<double>();

return c1v.size() == c2v.size() && std::equal(
begin(c1v), end(c1v), begin(c2v),
[](const double& s1, const double& s2) {
return std::abs(s1 - s2) < 0.001;
});
};

if (const1 && const2 && !equal(const1, const2)) {
using namespace ::attr_comparison::equal;
if (const1 && const2 &&
!Equal<std::shared_ptr<Constant>>::equal_value(const1, const2)) {
err_log << "Different Constant values detected\n"
<< node1->description() << " Input(" << i << ") and "
<< node2->description() << " Input(" << i << ")" << std::endl;
<< node2->description() << " Input(" << i << ")"
<< std::endl;
}
}

Expand Down