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 1 commit
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 @@ -358,34 +358,11 @@ struct Equal<double> {
}
};

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

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

template <>
struct Equal<std::vector<double>> {
static bool equal_value(const std::vector<double>& lhs, const std::vector<double>& rhs) {
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<double>::equal_value);
std::equal(begin(lhs), end(lhs), begin(rhs), Equal<T>::equal_value);
}
};

Expand Down Expand Up @@ -474,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 @@ -776,55 +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) {
using namespace ::attr_comparison::equal;
const auto c1t = c1->get_element_type();
const auto c2t = c2->get_element_type();
if (c1t != c2t) {
return false;
}

bool rc = false;
switch (c1t) {
case ngraph::element::Type_t::bf16: {
auto c1v = c1->cast_vector<ngraph::bfloat16>();
auto c2v = c2->cast_vector<ngraph::bfloat16>();
rc = c1v.size() == c2v.size() &&
Equal<std::vector<ngraph::bfloat16>>::equal_value(c1v, c2v);
break;
}
case ngraph::element::Type_t::f16: {
const auto &c1v = c1->cast_vector<ngraph::float16>();
const auto &c2v = c2->cast_vector<ngraph::float16>();
rc = c1v.size() == c2v.size() &&
Equal<std::vector<ngraph::float16>>::equal_value(c1v, c2v);
break;
}
case ngraph::element::Type_t::f32: {
const auto &c1v = c1->cast_vector<float>();
const auto &c2v = c2->cast_vector<float>();
rc = c1v.size() == c2v.size() &&
Equal<std::vector<float>>::equal_value(c1v, c2v);
break;
}
default: {
const auto &c1v = c1->cast_vector<double>();
const auto &c2v = c2->cast_vector<double>();
rc = c1v.size() == c2v.size() &&
Equal<std::vector<double>>::equal_value(c1v, c2v);
break;
}
}
return rc;
};

if (const1 && const2 && !equal(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