Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[CORE] Handle special values of inf, -inf and nan during deserialization to IR correclty. (#35490)

Andrii Staikovaf4ac1791deb
diff --git a/src/common/util/include/openvino/util/common_util.hpp b/src/common/util/include/openvino/util/common_util.hpp
index d078b1b5b25..fbdd592978d 100644
--- a/src/common/util/include/openvino/util/common_util.hpp
+++ b/src/common/util/include/openvino/util/common_util.hpp
@@ -347,6 +347,13 @@ std::optional<T> view_to_number(std::string_view sv) noexcept {
const auto result = std::from_chars(sv.data(), sv.data() + sv.size(), value);
return result.ec == std::errc() ? std::make_optional(value) : std::nullopt;
} else {
+ if (sv == "inf") {
+ return std::numeric_limits<T>::infinity();
+ } else if (sv == "-inf") {
+ return -std::numeric_limits<T>::infinity();
+ } else if (sv == "nan") {
+ return std::numeric_limits<T>::quiet_NaN();
+ }
StringViewStreamBuf buf{sv};
std::istream stream{&buf};
stream.imbue(std::locale::classic());
diff --git a/src/core/tests/pass/serialization/from_model.cpp b/src/core/tests/pass/serialization/from_model.cpp
index cede103348e..67567101110 100644
--- a/src/core/tests/pass/serialization/from_model.cpp
+++ b/src/core/tests/pass/serialization/from_model.cpp
@@ -5,16 +5,19 @@
#include <gtest/gtest.h>
#include <fstream>
+#include <sstream>
#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/graph_comparator.hpp"
#include "common_test_utils/test_common.hpp"
#include "openvino/op/add.hpp"
+#include "openvino/op/clamp.hpp"
#include "openvino/op/if.hpp"
#include "openvino/op/relu.hpp"
#include "openvino/op/split.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/pass/serialize.hpp"
+#include "openvino/runtime/core.hpp"
#include "openvino/util/file_util.hpp"
#include "read_ir.hpp"
@@ -146,6 +149,28 @@ INSTANTIATE_TEST_SUITE_P(IRSerializationFromModel,
SerializationFromModelTest,
testing::ValuesIn(get_models()),
SerializationFromModelTest::getTestCaseName);
+
+// Clamp with inf bounds: verifies that -inf/inf attributes survive XML round-trip
+TEST(IRSerializationFromModel, ClampWithInfBounds) {
+ using namespace ov;
+ auto param = std::make_shared<op::v0::Parameter>(element::f32, Shape{2, 4});
+ param->output(0).get_tensor().set_names({"X"});
+ auto clamp = std::make_shared<op::v0::Clamp>(param,
+ -std::numeric_limits<double>::infinity(),
+ std::numeric_limits<double>::infinity());
+ auto res = std::make_shared<op::v0::Result>(clamp);
+ auto expected = std::make_shared<Model>(OutputVector{res}, ParameterVector{param});
+
+ std::stringstream model_ss, weights_ss;
+ pass::Serialize(model_ss, weights_ss).run_on_model(expected);
+
+ ov::Core core;
+ auto result = core.read_model(model_ss.str(), ov::Tensor());
+
+ const auto fc = FunctionsComparator::with_default().enable(FunctionsComparator::ATTRIBUTES);
+ const auto cmp = fc.compare(result, expected);
+ EXPECT_TRUE(cmp.valid) << cmp.message;
+}
} // namespace
class SerializationFromModelTest_large : public ov::test::TestsCommon, public testing::WithParamInterface<size_t> {
diff --git a/src/core/tests/xml_util/custom_ir.cpp b/src/core/tests/xml_util/custom_ir.cpp
index e1ab70f75fc..cffc4da46a0 100644
--- a/src/core/tests/xml_util/custom_ir.cpp
+++ b/src/core/tests/xml_util/custom_ir.cpp
@@ -423,4 +423,24 @@ TEST_F(CustomIRTest, modified_serialization_deserialization) {
const auto& [is_valid, error_msg] = model_comparator().compare(ov_model, drv_model);
EXPECT_TRUE(is_valid) << error_msg;
}
+
+TEST(StrToContainer, FloatVectorWithInfAndNan) {
+ {
+ std::vector<float> result;
+ ov::util::str_to_container("1.5,inf,-inf,nan,3.0", result);
+ ASSERT_EQ(result.size(), 5);
+ EXPECT_FLOAT_EQ(result[0], 1.5f);
+ EXPECT_TRUE(std::isinf(result[1]) && result[1] > 0);
+ EXPECT_TRUE(std::isinf(result[2]) && result[2] < 0);
+ EXPECT_TRUE(std::isnan(result[3]));
+ EXPECT_FLOAT_EQ(result[4], 3.0f);
+ }
+ {
+ std::vector<double> result;
+ ov::util::str_to_container("-inf,inf", result);
+ ASSERT_EQ(result.size(), 2);
+ EXPECT_TRUE(std::isinf(result[0]) && result[0] < 0);
+ EXPECT_TRUE(std::isinf(result[1]) && result[1] > 0);
+ }
+}
} // namespace ov::test
diff --git a/src/core/xml_util/include/openvino/xml_util/xml_deserialize_util.hpp b/src/core/xml_util/include/openvino/xml_util/xml_deserialize_util.hpp
index 7b841176c08..72bbf9a3f9f 100644
--- a/src/core/xml_util/include/openvino/xml_util/xml_deserialize_util.hpp
+++ b/src/core/xml_util/include/openvino/xml_util/xml_deserialize_util.hpp
@@ -17,6 +17,7 @@
#include "openvino/op/util/multi_subgraph_base.hpp"
#include "openvino/opsets/opset.hpp"
#include "openvino/runtime/aligned_buffer.hpp"
+#include "openvino/util/common_util.hpp"
namespace ov::util {
struct GenericLayerParams;
@@ -28,9 +29,15 @@ void str_to_container(const std::string& value, T& res) {
while (getline(ss, field, ',')) {
if (field.empty())
OPENVINO_THROW("Cannot get vector of parameters! \"", value, "\" is incorrect");
- std::stringstream fs(field);
typename T::value_type val;
- fs >> val;
+ if constexpr (std::is_arithmetic_v<typename T::value_type>) {
+ auto parsed = ov::util::view_to_number<typename T::value_type>(ov::util::trim(field));
+ OPENVINO_ASSERT(parsed.has_value(), "Cannot parse '", field, "' in \"", value, "\"");
+ val = *parsed;
+ } else {
+ std::stringstream fs(field);
+ fs >> val;
+ }
res.insert(res.end(), val);
}
}
diff --git a/src/core/xml_util/src/xml_deserialize_util.cpp b/src/core/xml_util/src/xml_deserialize_util.cpp
index 50176e3c842..facfe5c3005 100644
--- a/src/core/xml_util/src/xml_deserialize_util.cpp
+++ b/src/core/xml_util/src/xml_deserialize_util.cpp
@@ -65,12 +65,9 @@ bool getParameters(const pugi::xml_node& node, const std::string& name, std::vec
template <class T>
T stringToType(const std::string& valStr) {
- T ret{0};
- std::istringstream ss(valStr);
- if (!ss.eof()) {
- ss >> ret;
- }
- return ret;
+ auto result = ov::util::view_to_number<T>(ov::util::trim(valStr));
+ OPENVINO_ASSERT(result.has_value(), "Cannot parse '", valStr, "' to number");
+ return *result;
}
bool get_partial_shape_from_attribute(const pugi::xml_node& node, const std::string& name, PartialShape& value) {