Skip to content

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

[PyOV] Extend Python API with `Squeeze-15` (#27281)

Przemyslaw Wysockia0b73e0a7a69
diff --git a/src/bindings/python/src/openvino/runtime/opset15/__init__.py b/src/bindings/python/src/openvino/runtime/opset15/__init__.py
index 6cc9c24827a..c4dd48d9087 100644
--- a/src/bindings/python/src/openvino/runtime/opset15/__init__.py
+++ b/src/bindings/python/src/openvino/runtime/opset15/__init__.py
@@ -188,7 +188,7 @@ from openvino.runtime.opset1.ops import space_to_depth
from openvino.runtime.opset1.ops import split
from openvino.runtime.opset1.ops import sqrt
from openvino.runtime.opset1.ops import squared_difference
-from openvino.runtime.opset1.ops import squeeze
+from openvino.runtime.opset15.ops import squeeze
from openvino.runtime.opset15.ops import stft
from openvino.runtime.opset1.ops import strided_slice
from openvino.runtime.opset1.ops import subtract
diff --git a/src/bindings/python/src/openvino/runtime/opset15/ops.py b/src/bindings/python/src/openvino/runtime/opset15/ops.py
index b3a131602af..93aacb29572 100644
--- a/src/bindings/python/src/openvino/runtime/opset15/ops.py
+++ b/src/bindings/python/src/openvino/runtime/opset15/ops.py
@@ -348,3 +348,42 @@ def search_sorted(
inputs = as_nodes(sorted_sequence, values, name=name)
attributes = {"right_mode": right_mode}
return _get_node_factory_opset15().create("SearchSorted", inputs, attributes)
+
+
+@nameable_op
+def squeeze(
+ data: NodeInput,
+ axes: Optional[NodeInput] = None,
+ allow_axis_skip: bool = False,
+ name: Optional[str] = None,
+) -> Node:
+ """Perform squeeze operation on input tensor.
+
+ :param data: The node with data tensor.
+ :param axes: Optional list of integers, indicating the dimensions to squeeze.
+ Negative indices are supported. One of: input node or array.
+ :param allow_axis_skip: If true, shape inference results in a dynamic rank, when
+ selected axis has value 1 in its dynamic range. Used only if axes input
+ is given. Defaults to false.
+ :param name: Optional new name for output node.
+ :return: The new node performing a squeeze operation on input tensor.
+
+ Remove single-dimensional entries from the shape of a tensor.
+ Takes an optional parameter `axes` with a list of axes to squeeze.
+ If `axes` is not provided, all the single dimensions will be removed from the shape.
+
+ For example:
+
+ Inputs: tensor with shape [1, 2, 1, 3, 1, 1], axes=[2, 4]
+
+ Result: tensor with shape [1, 2, 3, 1]
+ """
+ if axes is None:
+ inputs = as_nodes(data, name=name)
+ else:
+ inputs = as_nodes(data, axes, name=name)
+ return _get_node_factory_opset15().create(
+ "Squeeze",
+ inputs,
+ {"allow_axis_skip": allow_axis_skip}
+ )
diff --git a/src/bindings/python/tests/test_graph/test_ops_fused.py b/src/bindings/python/tests/test_graph/test_ops_fused.py
index bdbf4a1a9f1..2bab743bfd7 100644
--- a/src/bindings/python/tests/test_graph/test_ops_fused.py
+++ b/src/bindings/python/tests/test_graph/test_ops_fused.py
@@ -110,17 +110,6 @@ def test_clamp_operator():
assert list(model.get_output_shape(0)) == [2, 2]
-def test_squeeze_operator():
- data_shape = [1, 2, 1, 3, 1, 1]
- parameter_data = ov.parameter(data_shape, name="Data", dtype=np.float32)
- axes = [2, 4]
- model = ov.squeeze(parameter_data, axes)
-
- assert model.get_type_name() == "Squeeze"
- assert model.get_output_size() == 1
- assert list(model.get_output_shape(0)) == [1, 2, 3, 1]
-
-
def test_squared_difference_operator():
x1_shape = [1, 2, 3, 4]
x2_shape = [2, 3, 4]
diff --git a/src/bindings/python/tests/test_graph/test_squeeze.py b/src/bindings/python/tests/test_graph/test_squeeze.py
new file mode 100644
index 00000000000..869d84a0414
--- /dev/null
+++ b/src/bindings/python/tests/test_graph/test_squeeze.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import openvino.runtime.opset1 as ov_opset1
+import openvino.runtime.opset15 as ov_opset15
+import numpy as np
+import pytest
+
+
+def test_squeeze_v1_operator():
+ data_shape = [1, 2, 1, 3, 1, 1]
+ parameter_data = ov_opset1.parameter(data_shape, name="Data", dtype=np.float32)
+ axes = [2, 4]
+ model = ov_opset1.squeeze(parameter_data, axes)
+
+ assert model.get_type_name() == "Squeeze"
+ assert model.get_output_size() == 1
+ assert list(model.get_output_shape(0)) == [1, 2, 3, 1]
+
+
[email protected](("input_shape", "axes", "allow_axis_skip", "expected_shape"), [
+ ((1, 2, 1, 3, 1, 1), [1, 2, 4], True, [1, 2, 3, 1]),
+ ((1, 2, 1, 3, 1, 1), [1, 2, 4], False, [1, 2, 3, 1]),
+ ((2, -1, 3), [1], False, [2, 3])
+])
+def test_squeeze_v15_operator(input_shape, axes, allow_axis_skip, expected_shape):
+ parameter_data = ov_opset15.parameter(input_shape, name="Data", dtype=np.float32)
+ model = ov_opset15.squeeze(parameter_data, axes, allow_axis_skip, name="Squeeze")
+
+ assert model.get_type_name() == "Squeeze"
+ assert model.get_output_size() == 1
+ assert list(model.get_output_shape(0)) == expected_shape
+
+
+def test_squeeze_v15_dynamic_rank_output():
+ parameter_data = ov_opset15.parameter((2, -1, 3), name="Data", dtype=np.float32)
+ model = ov_opset15.squeeze(parameter_data, [1], True, name="Squeeze")
+
+ assert model.get_type_name() == "Squeeze"
+ assert model.get_output_size() == 1
+ assert model.get_output_partial_shape(0).to_string() == "[...]"
+
+
+def test_squeeze_v15_axes_not_given():
+ parameter_data = ov_opset15.parameter((1, 3, 1, 1, 3, 5), name="Data", dtype=np.float32)
+ model = ov_opset15.squeeze(data=parameter_data, name="Squeeze")
+
+ assert model.get_type_name() == "Squeeze"
+ assert model.get_output_size() == 1
+ assert list(model.get_output_shape(0)) == [3, 3, 5]