Skip to content

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

[core] Fix constant folding for unsupported FP precision (#35053)

Pawel Raaszdbe70c50b1d9
diff --git a/src/core/src/validation_util.cpp b/src/core/src/validation_util.cpp
index 5e9e8cbf314..2c2081f04a4 100644
--- a/src/core/src/validation_util.cpp
+++ b/src/core/src/validation_util.cpp
@@ -107,14 +107,13 @@ std::shared_ptr<ov::op::v0::Constant> ov::util::constantfold_subgraph(const ov::
}
auto original_node = node;
- const auto inputs = get_inputs_from_map(node, node_map);
if (ov::util::node_requires_precision_conversion(node.get())) {
- node = ov::util::convert_to_supported_precision(node.get(), inputs);
+ node = ov::util::convert_to_supported_precision(node.get(), get_inputs_from_map(node, node_map));
}
- OutputVector outputs(node->get_output_size());
- if (node->constant_fold(outputs, inputs)) {
+ if (OutputVector outputs(node->get_output_size());
+ node->constant_fold(outputs, get_inputs_from_map(node, node_map))) {
stack.pop();
for (size_t i = 0; i < outputs.size(); i++) {
node_map[original_node->output(i)] = outputs[i].get_node_shared_ptr();
@@ -127,9 +126,9 @@ std::shared_ptr<ov::op::v0::Constant> ov::util::constantfold_subgraph(const ov::
stack.push(input.get_node_shared_ptr());
}
}
- // if none of the inputs was pushed to stack, it means the node that was not constantfolded
- // is processed the second time. If that case - the node is not constfoldable.
- // A good example would be a node that all of its inputs are constants and yet it cannot be constantfolded
+ // if none of the inputs was pushed to stack, it means the node that was not constant folded
+ // is processed the second time. If that case - the node is not constant foldable.
+ // A good example would be a node that all of its inputs are constants and yet it cannot be constant folded
// for some reason (like lack of evaluate, it's a op::util::FrameworkNode, etc.).
if (stack_size_before == stack.size()) {
return nullptr;
diff --git a/src/core/tests/validation_utils.cpp b/src/core/tests/validation_utils.cpp
index 6e8c9190c36..8adc801b2b7 100644
--- a/src/core/tests/validation_utils.cpp
+++ b/src/core/tests/validation_utils.cpp
@@ -7,8 +7,10 @@
#include "common_test_utils/test_assertions.hpp"
#include "openvino/core/type.hpp"
#include "openvino/core/validation_util.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/divide.hpp"
+#include "openvino/op/erf.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/parameter.hpp"
@@ -98,15 +100,28 @@ TEST(constantfold_subgraph, shapeof) {
ASSERT_EQ(expected, actual);
}
-namespace ov {
-namespace test {
+namespace ov::test {
-using op::v0::Concat;
-using op::v0::Constant;
-using op::v0::Parameter;
+using op::v0::Concat, op::v0::Constant, op::v0::Parameter;
+using ov::op::v1::Add, ov::op::v1::Divide, ov::op::v0::Erf;
-using testing::HasSubstr;
-using testing::Values;
+using testing::HasSubstr, testing::Values, testing::Pointwise, testing::FloatNear;
+
+TEST(constantfold_subgraph, fold_unsupported_precision_multiple_nodes) {
+ auto init = Constant::create(element::f16, Shape{1, 1, 1, 2}, {10.f, 20.f});
+ auto a = Constant::create(element::f16, Shape{1}, {1.5f});
+ auto div = std::make_shared<Divide>(init, a);
+ auto erf = std::make_shared<Erf>(div);
+ auto b = Constant::create(element::f16, Shape{1}, {10.f});
+ auto add = std::make_shared<Add>(erf, b);
+
+ auto folded_node = ov::util::constantfold_subgraph(add);
+ ASSERT_NE(folded_node, nullptr);
+ EXPECT_EQ(folded_node->get_element_type(), ov::element::f16);
+ const auto actual = folded_node->cast_vector<float>();
+ const std::vector<float> expected{11.f, 11.f};
+ EXPECT_THAT(actual, Pointwise(FloatNear(1e-5f), expected));
+}
using NormalizeAxisTest = testing::Test;
@@ -279,5 +294,4 @@ TEST_P(NormalizeAxisTestP, try_normalize_axis) {
EXPECT_EQ(exp_axis, util::try_normalize_axis(axis, rank));
}
-} // namespace test
-} // namespace ov
+} // namespace ov::test