From 4fa32e59ad2912ac5962b222243aea813d59d313 Mon Sep 17 00:00:00 2001 From: blesniewski Date: Thu, 11 Feb 2021 16:02:03 +0100 Subject: [PATCH 1/5] fix comparison of constant with short float NAN values --- .../common_test_utils/ngraph_test_utils.cpp | 95 ++++++++++++++++--- 1 file changed, 83 insertions(+), 12 deletions(-) diff --git a/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp b/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp index 4045c8fd00e0af..b5657f39f62ae1 100644 --- a/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp +++ b/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp @@ -324,6 +324,26 @@ struct Equal { } }; +template <> +struct Equal { + 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-5; + } +}; + +template <> +struct Equal { + 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-5; + } +}; + template <> struct Equal { static bool equal_value(float lhs, float rhs) { @@ -339,13 +359,20 @@ struct Equal { }; template <> -struct Equal> { - static bool equal_value(const std::vector& lhs, const std::vector& rhs) { +struct Equal> { + static bool equal_value(const std::vector& lhs, const std::vector& rhs) { return lhs.size() == rhs.size() && - std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); + std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); } }; +template <> +struct Equal> { + static bool equal_value(const std::vector& lhs, const std::vector& rhs) { + return lhs.size() == rhs.size() && + std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); + } +}; template <> struct Equal> { static bool equal_value(const std::vector& lhs, const std::vector& rhs) { @@ -354,6 +381,14 @@ struct Equal> { } }; +template <> +struct Equal> { + static bool equal_value(const std::vector& lhs, const std::vector& rhs) { + return lhs.size() == rhs.size() && + std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); + } +}; + template <> struct Equal { static bool equal_value( @@ -742,15 +777,51 @@ FunctionsComparator::Result FunctionsComparator::compare( auto const1 = ngraph::as_type_ptr(node1->get_input_node_shared_ptr(i)); auto const2 = ngraph::as_type_ptr(node2->get_input_node_shared_ptr(i)); - const auto equal = [](std::shared_ptr c1, std::shared_ptr c2) { - const auto& c1v = c1->cast_vector(); - const auto& c2v = c2->cast_vector(); - - 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; - }); + const auto equal = [](std::shared_ptr c1, + std::shared_ptr 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(); + auto c2v = c2->cast_vector(); + rc = c1v.size() == c2v.size() + ? Equal>::equal_value(c1v, c2v) + : false; + break; + } + case ngraph::element::Type_t::f16: { + const auto &c1v = c1->cast_vector(); + const auto &c2v = c2->cast_vector(); + rc = c1v.size() == c2v.size() + ? Equal>::equal_value(c1v, c2v) + : false; + break; + } + case ngraph::element::Type_t::f32: { + const auto &c1v = c1->cast_vector(); + const auto &c2v = c2->cast_vector(); + rc = c1v.size() == c2v.size() + ? Equal>::equal_value(c1v, c2v) + : false; + break; + } + default: { + const auto &c1v = c1->cast_vector(); + const auto &c2v = c2->cast_vector(); + rc = c1v.size() == c2v.size() + ? Equal>::equal_value(c1v, c2v) + : false; + break; + } + } + return rc; }; if (const1 && const2 && !equal(const1, const2)) { From 0b235c96fe592260732f731d6e2c4f20a7de7a5b Mon Sep 17 00:00:00 2001 From: blesniewski Date: Thu, 11 Feb 2021 17:14:28 +0100 Subject: [PATCH 2/5] adjust precision, remove elvises --- .../common_test_utils/ngraph_test_utils.cpp | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp b/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp index b5657f39f62ae1..da395efb18afb6 100644 --- a/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp +++ b/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp @@ -328,9 +328,9 @@ template <> struct Equal { static bool equal_value(ngraph::bfloat16 lhs, ngraph::bfloat16 rhs) { if (lhs.to_bits() == rhs.to_bits()) { - return true; + return true; } - return std::abs(lhs - rhs) < 1e-5; + return std::abs(lhs - rhs) < 1e-3; } }; @@ -338,16 +338,16 @@ template <> struct Equal { static bool equal_value(ngraph::float16 lhs, ngraph::float16 rhs) { if (lhs.to_bits() == rhs.to_bits()) { - return true; + return true; } - return std::abs(lhs - rhs) < 1e-5; + return std::abs(lhs - rhs) < 1e-3; } }; template <> struct Equal { static bool equal_value(float lhs, float rhs) { - return std::abs(lhs - rhs) < 1e-5; + return std::abs(lhs - rhs) < 1e-4; } }; @@ -789,42 +789,39 @@ FunctionsComparator::Result FunctionsComparator::compare( bool rc = false; switch (c1t) { case ngraph::element::Type_t::bf16: { - auto c1v = c1->cast_vector(); - auto c2v = c2->cast_vector(); - rc = c1v.size() == c2v.size() - ? Equal>::equal_value(c1v, c2v) - : false; - break; + auto c1v = c1->cast_vector(); + auto c2v = c2->cast_vector(); + rc = c1v.size() == c2v.size() && + Equal>::equal_value(c1v, c2v); + break; } case ngraph::element::Type_t::f16: { - const auto &c1v = c1->cast_vector(); - const auto &c2v = c2->cast_vector(); - rc = c1v.size() == c2v.size() - ? Equal>::equal_value(c1v, c2v) - : false; - break; + const auto &c1v = c1->cast_vector(); + const auto &c2v = c2->cast_vector(); + rc = c1v.size() == c2v.size() && + Equal>::equal_value(c1v, c2v); + break; } case ngraph::element::Type_t::f32: { - const auto &c1v = c1->cast_vector(); - const auto &c2v = c2->cast_vector(); - rc = c1v.size() == c2v.size() - ? Equal>::equal_value(c1v, c2v) - : false; - break; + const auto &c1v = c1->cast_vector(); + const auto &c2v = c2->cast_vector(); + rc = c1v.size() == c2v.size() && + Equal>::equal_value(c1v, c2v); + break; } default: { - const auto &c1v = c1->cast_vector(); - const auto &c2v = c2->cast_vector(); - rc = c1v.size() == c2v.size() - ? Equal>::equal_value(c1v, c2v) - : false; - break; + const auto &c1v = c1->cast_vector(); + const auto &c2v = c2->cast_vector(); + rc = c1v.size() == c2v.size() && + Equal>::equal_value(c1v, c2v); + break; } } return rc; }; if (const1 && const2 && !equal(const1, const2)) { + equal(const1, const2); err_log << "Different Constant values detected\n" << node1->description() << " Input(" << i << ") and " << node2->description() << " Input(" << i << ")" << std::endl; From 6be88108bfba00fc4ac8f7834d4bb980fe63de6d Mon Sep 17 00:00:00 2001 From: blesniewski Date: Fri, 12 Feb 2021 10:57:09 +0100 Subject: [PATCH 3/5] more templates --- .../common_test_utils/ngraph_test_utils.cpp | 122 +++++++----------- 1 file changed, 48 insertions(+), 74 deletions(-) diff --git a/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp b/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp index da395efb18afb6..373360cdeb5fc8 100644 --- a/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp +++ b/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp @@ -358,34 +358,11 @@ struct Equal { } }; -template <> -struct Equal> { - static bool equal_value(const std::vector& lhs, const std::vector& rhs) { - return lhs.size() == rhs.size() && - std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); - } -}; - -template <> -struct Equal> { - static bool equal_value(const std::vector& lhs, const std::vector& rhs) { - return lhs.size() == rhs.size() && - std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); - } -}; -template <> -struct Equal> { - static bool equal_value(const std::vector& lhs, const std::vector& rhs) { - return lhs.size() == rhs.size() && - std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); - } -}; - -template <> -struct Equal> { - static bool equal_value(const std::vector& lhs, const std::vector& rhs) { +template +struct Equal> { + static bool equal_value(const std::vector& lhs, const std::vector& rhs) { return lhs.size() == rhs.size() && - std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); + std::equal(begin(lhs), end(lhs), begin(rhs), Equal::equal_value); } }; @@ -474,6 +451,45 @@ struct Equal { } }; +using Constant = ngraph::opset1::Constant; +template <> struct Equal> { + static bool equal_value(const std::shared_ptr lhs, + const std::shared_ptr 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(); + auto rhs_v = rhs->cast_vector(); + return Equal>::equal_value(lhs_v, rhs_v); + break; + } + case ngraph::element::Type_t::f16: { + const auto &lhs_v = lhs->cast_vector(); + const auto &rhs_v = rhs->cast_vector(); + return Equal>::equal_value(lhs_v, rhs_v); + break; + } + case ngraph::element::Type_t::f32: { + const auto &lhs_v = lhs->cast_vector(); + const auto &rhs_v = rhs->cast_vector(); + return Equal>::equal_value(lhs_v, rhs_v); + break; + } + default: { + const auto &lhs_v = lhs->cast_vector(); + const auto &rhs_v = rhs->cast_vector(); + return Equal>::equal_value(lhs_v, rhs_v); + break; + } + } + return false; + } +}; } // namespace equal namespace str { @@ -776,55 +792,13 @@ FunctionsComparator::Result FunctionsComparator::compare( using Constant = ngraph::opset1::Constant; auto const1 = ngraph::as_type_ptr(node1->get_input_node_shared_ptr(i)); auto const2 = ngraph::as_type_ptr(node2->get_input_node_shared_ptr(i)); - - const auto equal = [](std::shared_ptr c1, - std::shared_ptr 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(); - auto c2v = c2->cast_vector(); - rc = c1v.size() == c2v.size() && - Equal>::equal_value(c1v, c2v); - break; - } - case ngraph::element::Type_t::f16: { - const auto &c1v = c1->cast_vector(); - const auto &c2v = c2->cast_vector(); - rc = c1v.size() == c2v.size() && - Equal>::equal_value(c1v, c2v); - break; - } - case ngraph::element::Type_t::f32: { - const auto &c1v = c1->cast_vector(); - const auto &c2v = c2->cast_vector(); - rc = c1v.size() == c2v.size() && - Equal>::equal_value(c1v, c2v); - break; - } - default: { - const auto &c1v = c1->cast_vector(); - const auto &c2v = c2->cast_vector(); - rc = c1v.size() == c2v.size() && - Equal>::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>::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; } } From bba2e43d816a396f47f6980b7102603b6bc703dd Mon Sep 17 00:00:00 2001 From: blesniewski Date: Fri, 12 Feb 2021 15:40:51 +0100 Subject: [PATCH 4/5] add ir serialization test with float16 const --- .../models/add_abc_initializers_nan_const.bin | Bin 0 -> 17 bytes .../add_abc_initializers_nan_const.prototxt | 95 ++++++++++++++++++ .../models/add_abc_initializers_nan_const.xml | 93 +++++++++++++++++ .../ir_serialization/serialize.cpp | 1 + .../common_test_utils/ngraph_test_utils.cpp | 4 +- 5 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.bin create mode 100644 inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.prototxt create mode 100644 inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.xml diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.bin b/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.bin new file mode 100644 index 0000000000000000000000000000000000000000..16a41a06ca8aa97ca1b80cada20efb2d82daf818 GIT binary patch literal 17 XcmZQzU~o9#z~K0P|6T_mW^e=mG6V+M literal 0 HcmV?d00001 diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.prototxt b/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.prototxt new file mode 100644 index 00000000000000..8d8c441c088ed7 --- /dev/null +++ b/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.prototxt @@ -0,0 +1,95 @@ +ir_version: 3 +producer_name: "nGraph ONNX Importer" +graph { + node { + output: "B" + op_type: "Constant" + attribute { + name: "value" + t { + dims: 2 + dims: 2 + data_type: 1 + float_data: 1 + float_data: 2 + float_data: 3 + float_data: 4 + name: "const_tensor" + } + type: TENSOR + } + } + node { + input: "A" + input: "B" + output: "X" + name: "add_node1" + op_type: "Add" + } + node { + input: "X" + input: "C" + output: "Y" + name: "add_node2" + op_type: "Add" + } + name: "test_graph" + initializer { + dims: 2 + dims: 2 + data_type: 1 + name: "A" + raw_data: "\000\000\200?\000\000\000@\000\000@@\000\000\200@" + } + input { + name: "A" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + input { + name: "C" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } + output { + name: "Y" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 2 + } + dim { + dim_value: 2 + } + } + } + } + } +} +opset_import { + version: 4 +} diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.xml b/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.xml new file mode 100644 index 00000000000000..c0ecd4025c550c --- /dev/null +++ b/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.xml @@ -0,0 +1,93 @@ + + + + + + + + 2 + 2 + + + + + + + + 2 + 2 + + + + + + + 2 + 2 + + + 2 + 2 + + + + + 2 + 2 + + + + + + + 2 + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/serialize.cpp b/inference-engine/tests/functional/inference_engine/ir_serialization/serialize.cpp index 067ed94bc7dc06..254622157ad7f0 100644 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/serialize.cpp +++ b/inference-engine/tests/functional/inference_engine/ir_serialization/serialize.cpp @@ -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", ""), diff --git a/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp b/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp index 373360cdeb5fc8..81a207d9c6bc62 100644 --- a/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp +++ b/inference-engine/tests/ie_test_utils/common_test_utils/ngraph_test_utils.cpp @@ -453,8 +453,8 @@ struct Equal { using Constant = ngraph::opset1::Constant; template <> struct Equal> { - static bool equal_value(const std::shared_ptr lhs, - const std::shared_ptr rhs) { + static bool equal_value(const std::shared_ptr& lhs, + const std::shared_ptr& rhs) { const auto lhs_t = lhs->get_element_type(); const auto rhs_t = rhs->get_element_type(); if (lhs_t != rhs_t) { From 594f32666dbf9c0c51b3cd6b360dc6a8fa1fbcd1 Mon Sep 17 00:00:00 2001 From: blesniewski Date: Fri, 12 Feb 2021 16:18:36 +0100 Subject: [PATCH 5/5] remove unused prototxt --- .../add_abc_initializers_nan_const.prototxt | 95 ------------------- 1 file changed, 95 deletions(-) delete mode 100644 inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.prototxt diff --git a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.prototxt b/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.prototxt deleted file mode 100644 index 8d8c441c088ed7..00000000000000 --- a/inference-engine/tests/functional/inference_engine/ir_serialization/models/add_abc_initializers_nan_const.prototxt +++ /dev/null @@ -1,95 +0,0 @@ -ir_version: 3 -producer_name: "nGraph ONNX Importer" -graph { - node { - output: "B" - op_type: "Constant" - attribute { - name: "value" - t { - dims: 2 - dims: 2 - data_type: 1 - float_data: 1 - float_data: 2 - float_data: 3 - float_data: 4 - name: "const_tensor" - } - type: TENSOR - } - } - node { - input: "A" - input: "B" - output: "X" - name: "add_node1" - op_type: "Add" - } - node { - input: "X" - input: "C" - output: "Y" - name: "add_node2" - op_type: "Add" - } - name: "test_graph" - initializer { - dims: 2 - dims: 2 - data_type: 1 - name: "A" - raw_data: "\000\000\200?\000\000\000@\000\000@@\000\000\200@" - } - input { - name: "A" - type { - tensor_type { - elem_type: 1 - shape { - dim { - dim_value: 2 - } - dim { - dim_value: 2 - } - } - } - } - } - input { - name: "C" - type { - tensor_type { - elem_type: 1 - shape { - dim { - dim_value: 2 - } - dim { - dim_value: 2 - } - } - } - } - } - output { - name: "Y" - type { - tensor_type { - elem_type: 1 - shape { - dim { - dim_value: 2 - } - dim { - dim_value: 2 - } - } - } - } - } -} -opset_import { - version: 4 -}