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
Show file tree
Hide file tree
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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" ?>
<net name="add_abc_initializers" version="10">
<layers>
<layer id="0" name="add_node1/Output_0/Data__const" type="Const" version="opset1">
<data element_type="f16" offset="0" shape="2,2" size="8"/>
<output>
<port id="0" precision="FP16">
<dim>2</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="1" name="C" type="Parameter" version="opset1">
<data element_type="f16" shape="2,2"/>
<output>
<port id="0" precision="FP16">
<dim>2</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="2" name="Y" type="Add" version="opset1">
<input>
<port id="0">
<dim>2</dim>
<dim>2</dim>
</port>
<port id="1">
<dim>2</dim>
<dim>2</dim>
</port>
</input>
<output>
<port id="2" precision="FP16">
<dim>2</dim>
<dim>2</dim>
</port>
</output>
</layer>
<layer id="3" name="Y/sink_port_0" type="Result" version="opset1">
<input>
<port id="0">
<dim>2</dim>
<dim>2</dim>
</port>
</input>
</layer>
</layers>
<edges>
<edge from-layer="0" from-port="0" to-layer="2" to-port="0"/>
<edge from-layer="1" from-port="0" to-layer="2" to-port="1"/>
<edge from-layer="2" from-port="2" to-layer="3" to-port="0"/>
</edges>
<meta_data>
<MO_version value="custom_master_29f1c38ba0ae51897f47946d79d6bd6be7a494f0"/>
<cli_parameters>
<caffe_parser_path value="DIR"/>
<data_type value="float"/>
<disable_nhwc_to_nchw value="False"/>
<disable_omitting_optional value="False"/>
<disable_resnet_optimization value="False"/>
<disable_weights_compression value="False"/>
<enable_concat_optimization value="False"/>
<enable_flattening_nested_params value="False"/>
<enable_ssd_gluoncv value="False"/>
<extensions value="DIR"/>
<framework value="onnx"/>
<freeze_placeholder_with_value value="{}"/>
<generate_deprecated_IR_V7 value="False"/>
<input_model value="DIR/add_abc_initializers.onnx"/>
<input_model_is_text value="False"/>
<k value="DIR/CustomLayersMapping.xml"/>
<keep_shape_ops value="True"/>
<legacy_mxnet_model value="False"/>
<log_level value="ERROR"/>
<mean_scale_values value="{}"/>
<mean_values value="()"/>
<model_name value="add_abc_initializers"/>
<output_dir value="DIR"/>
<placeholder_data_types value="{}"/>
<progress value="False"/>
<remove_memory value="False"/>
<remove_output_softmax value="False"/>
<reverse_input_channels value="False"/>
<save_params_from_nd value="False"/>
<scale_values value="()"/>
<silent value="False"/>
<static_shape value="False"/>
<stream_output value="False"/>
<unset unset_cli_parameters="batch, counts, disable_fusing, disable_gfusing, finegrain_fusing, input, input_checkpoint, input_meta_graph, input_proto, input_shape, input_symbol, mean_file, mean_file_offsets, move_to_preprocess, nd_prefix_name, output, placeholder_shapes, pretrained_model_name, saved_model_dir, saved_model_tags, scale, tensorboard_logdir, tensorflow_custom_layer_libraries, tensorflow_custom_operations_config_update, tensorflow_object_detection_api_pipeline_config, tensorflow_use_custom_operations_config, transformations_config"/>
</cli_parameters>
</meta_data>
</net>
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ INSTANTIATE_TEST_CASE_P(IRSerialization, SerializationTest,
std::make_tuple("split_equal_parts_2d.xml", "split_equal_parts_2d.bin"),
std::make_tuple("addmul_abc.xml", "addmul_abc.bin"),
std::make_tuple("add_abc_initializers.xml", "add_abc_initializers.bin"),
std::make_tuple("add_abc_initializers_nan_const.xml", "add_abc_initializers_nan_const.bin"),
std::make_tuple("experimental_detectron_roi_feature_extractor.xml", ""),
std::make_tuple("experimental_detectron_roi_feature_extractor_opset6.xml", ""),
std::make_tuple("experimental_detectron_detection_output.xml", ""),
Expand Down
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,
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