Skip to content

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

[Py OV] Create ov.experimental submodule (#25929)

Alicja Miloszewskac1e795c08c41
diff --git a/src/bindings/python/src/openvino/__init__.py b/src/bindings/python/src/openvino/__init__.py
index db69d2054b1..57f03f00c2e 100644
--- a/src/bindings/python/src/openvino/__init__.py
+++ b/src/bindings/python/src/openvino/__init__.py
@@ -21,6 +21,7 @@ except ImportError:
from openvino import runtime as runtime
from openvino import frontend as frontend
from openvino import helpers as helpers
+from openvino import experimental as experimental
from openvino import preprocess as preprocess
from openvino import utils as utils
from openvino import properties as properties
diff --git a/src/bindings/python/src/openvino/experimental/__init__.py b/src/bindings/python/src/openvino/experimental/__init__.py
new file mode 100644
index 00000000000..ed445a816eb
--- /dev/null
+++ b/src/bindings/python/src/openvino/experimental/__init__.py
@@ -0,0 +1,12 @@
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+"""
+Package: openvino
+This module provides access to experimental functionality that is subject to change without prior notice.
+"""
+
+# flake8: noqa
+
+from openvino._pyopenvino.experimental import evaluate_as_partial_shape
+from openvino._pyopenvino.experimental import evaluate_both_bounds
diff --git a/src/bindings/python/src/pyopenvino/CMakeLists.txt b/src/bindings/python/src/pyopenvino/CMakeLists.txt
index 99592f4552b..328ce87f9c2 100644
--- a/src/bindings/python/src/pyopenvino/CMakeLists.txt
+++ b/src/bindings/python/src/pyopenvino/CMakeLists.txt
@@ -56,7 +56,7 @@ endif()
# create target
-file(GLOB_RECURSE SOURCES core/*.cpp graph/*.cpp frontend/*.cpp utils/*.cpp pyopenvino.cpp)
+file(GLOB_RECURSE SOURCES core/*.cpp experimental/*.cpp graph/*.cpp frontend/*.cpp utils/*.cpp pyopenvino.cpp)
list(FILTER SOURCES EXCLUDE REGEX ".*(frontend/(onnx|tensorflow|paddle|pytorch|jax))/*")
pybind11_add_module(${PROJECT_NAME} MODULE NO_EXTRAS ${SOURCES})
diff --git a/src/bindings/python/src/pyopenvino/experimental/experimental.cpp b/src/bindings/python/src/pyopenvino/experimental/experimental.cpp
new file mode 100644
index 00000000000..e44f945f9e2
--- /dev/null
+++ b/src/bindings/python/src/pyopenvino/experimental/experimental.cpp
@@ -0,0 +1,43 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "pyopenvino/experimental/experimental.hpp"
+
+#include "openvino/core/bound_evaluation_util.hpp"
+#include "openvino/core/validation_util.hpp"
+
+namespace py = pybind11;
+
+void regmodule_experimental(py::module m) {
+ py::module m_dev = m.def_submodule("experimental", "openvino.experimental submodule");
+
+ m_dev.def("evaluate_as_partial_shape",
+ &ov::util::evaluate_as_partial_shape,
+ py::arg("output"),
+ py::arg("partial_shape"),
+ R"(
+ Evaluates lower and upper value estimations for the output tensor.
+ The estimation will be represented as a partial shape object,
+ using Dimension(min, max) for each element.
+
+ :param output: Node output pointing to the tensor for estimation.
+ :type output: openvino.runtime.Output
+ :param partial_shape: The resulting estimation will be stored in this PartialShape.
+ :type partial_shape: openvino.PartialShape
+ :return: True if estimation evaluation was successful, false otherwise.
+ :rtype: bool
+ )");
+ m_dev.def("evaluate_both_bounds",
+ &ov::util::evaluate_both_bounds,
+ py::arg("output"),
+ R"(
+ Evaluates lower and upper value estimations of the output tensor.
+ It traverses the graph upwards to deduce the estimation.
+
+ :param output: Node output pointing to the tensor for estimation.
+ :type output: openvino.runtime.Output
+ :return: Tensors representing the lower and upper bound value estimations.
+ :rtype: Tuple[openvino.Tensor, openvino.Tensor]
+ )");
+}
diff --git a/src/bindings/python/src/pyopenvino/experimental/experimental.hpp b/src/bindings/python/src/pyopenvino/experimental/experimental.hpp
new file mode 100644
index 00000000000..3550915fb02
--- /dev/null
+++ b/src/bindings/python/src/pyopenvino/experimental/experimental.hpp
@@ -0,0 +1,11 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <pybind11/pybind11.h>
+
+namespace py = pybind11;
+
+void regmodule_experimental(py::module m);
diff --git a/src/bindings/python/src/pyopenvino/pyopenvino.cpp b/src/bindings/python/src/pyopenvino/pyopenvino.cpp
index 5e7af2d61b2..1e01ed3d37e 100644
--- a/src/bindings/python/src/pyopenvino/pyopenvino.cpp
+++ b/src/bindings/python/src/pyopenvino/pyopenvino.cpp
@@ -36,6 +36,7 @@
#include "pyopenvino/core/tensor.hpp"
#include "pyopenvino/core/variable_state.hpp"
#include "pyopenvino/core/version.hpp"
+#include "pyopenvino/experimental/experimental.hpp"
#include "pyopenvino/frontend/decoder.hpp"
#include "pyopenvino/frontend/extension.hpp"
#include "pyopenvino/frontend/frontend.hpp"
@@ -252,6 +253,7 @@ PYBIND11_MODULE(_pyopenvino, m) {
regmodule_graph_onnx_import(m);
#endif
regmodule_graph_op_util(m_op);
+ regmodule_experimental(m);
py::module m_preprocess =
m.def_submodule("preprocess", "Package openvino.runtime.preprocess that wraps ov::preprocess");
regclass_graph_PrePostProcessor(m_preprocess);
diff --git a/src/bindings/python/tests/test_runtime/test_experimental.py b/src/bindings/python/tests/test_runtime/test_experimental.py
new file mode 100644
index 00000000000..66144e0b0f5
--- /dev/null
+++ b/src/bindings/python/tests/test_runtime/test_experimental.py
@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+from openvino.experimental import evaluate_as_partial_shape, evaluate_both_bounds
+
+import pytest
+from openvino.runtime import Shape, PartialShape, Dimension, Type
+from openvino.runtime.op import Constant
+import openvino.runtime.opset13 as ops
+import numpy as np
+
+
+def graph_with_partial_value():
+ bounds = [
+ (-6, -6), (-5, -4), (-4, 0), (-4, 4), (-4, 3),
+ (-3, 4), (0, 0), (0, 2), (1, 1), (1, 42),
+ ]
+ shape = []
+ subtrahend = []
+
+ for (lower, upper) in bounds:
+ if lower >= 0:
+ shape.append(Dimension(lower, upper))
+ subtrahend.append(0)
+ else:
+ shape.append(Dimension(0, upper - lower))
+ subtrahend.append(lower)
+
+ parameter_node = ops.parameter(PartialShape(shape), Type.dynamic)
+ shape_of = ops.shape_of(parameter_node)
+ subtract = ops.add(
+ shape_of,
+ Constant(Type.i64, Shape([len(subtrahend)]), subtrahend),
+ )
+ return subtract
+
+
+def test_evaluate_both_bounds(graph_with_partial_value):
+ node = graph_with_partial_value
+ lb, ub = evaluate_both_bounds(node.output(0))
+ lower_bounds = [-6, -5, -4, -4, -4, -3, 0, 0, 1, 1]
+ upper_bounds = [-6, -4, 0, 4, 3, 4, 0, 2, 1, 42]
+
+ assert np.equal(lb.data, lower_bounds).all()
+ assert np.equal(ub.data, upper_bounds).all()
+
+
+def test_evaluate_as_partial_shape(graph_with_partial_value):
+ node = ops.abs(graph_with_partial_value)
+ output_value = PartialShape([])
+ assert evaluate_as_partial_shape(node.output(0), output_value)
diff --git a/tools/benchmark_tool/openvino/__init__.py b/tools/benchmark_tool/openvino/__init__.py
index db69d2054b1..57f03f00c2e 100644
--- a/tools/benchmark_tool/openvino/__init__.py
+++ b/tools/benchmark_tool/openvino/__init__.py
@@ -21,6 +21,7 @@ except ImportError:
from openvino import runtime as runtime
from openvino import frontend as frontend
from openvino import helpers as helpers
+from openvino import experimental as experimental
from openvino import preprocess as preprocess
from openvino import utils as utils
from openvino import properties as properties
diff --git a/tools/ovc/openvino/__init__.py b/tools/ovc/openvino/__init__.py
index db69d2054b1..57f03f00c2e 100644
--- a/tools/ovc/openvino/__init__.py
+++ b/tools/ovc/openvino/__init__.py
@@ -21,6 +21,7 @@ except ImportError:
from openvino import runtime as runtime
from openvino import frontend as frontend
from openvino import helpers as helpers
+from openvino import experimental as experimental
from openvino import preprocess as preprocess
from openvino import utils as utils
from openvino import properties as properties