Skip to content

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

[PyOV] Add Python API for `Histc-17` (#39313)

Przemyslaw Wysockifef2ff6c6381
diff --git a/src/bindings/python/src/openvino/opset17/__init__.py b/src/bindings/python/src/openvino/opset17/__init__.py
index 231adf698c0..203dfa6b1c9 100644
--- a/src/bindings/python/src/openvino/opset17/__init__.py
+++ b/src/bindings/python/src/openvino/opset17/__init__.py
@@ -8,6 +8,7 @@ from openvino.opset17.ops import erfinv
from openvino.opset17.ops import grouped_matmul
from openvino.opset17.ops import qsa_prepare
from openvino.opset17.ops import rgb_to_nv12
+from openvino.opset17.ops import histc
from openvino.opset17.ops import sparse_attention
from openvino.opset17.ops import sparse_attention_indexer
diff --git a/src/bindings/python/src/openvino/opset17/ops.py b/src/bindings/python/src/openvino/opset17/ops.py
index c1e780aee8d..9b06809e596 100644
--- a/src/bindings/python/src/openvino/opset17/ops.py
+++ b/src/bindings/python/src/openvino/opset17/ops.py
@@ -65,6 +65,33 @@ def grouped_matmul(
)
+@nameable_op
+def histc(
+ data: NodeInput,
+ *,
+ bins: int = 100,
+ min: float = 0.0,
+ max: float = 0.0,
+ preserve_input_type: bool = True,
+ name: Optional[str] = None,
+) -> Node:
+ """Return a node which computes the histogram of all input elements using equal-width bins.
+
+ :param data: The node providing input data of numeric type and arbitrary shape.
+ :param bins: Number of equal-width bins. Must be positive. Default 100.
+ :param min: Lower end of the range (inclusive). If equal to max, the range is taken from the data.
+ :param max: Upper end of the range (inclusive). If equal to min, the range is taken from the data.
+ :param preserve_input_type: If True, the output has the input element type, otherwise i32.
+ :param name: The optional name for the created output node.
+ :return: The new node performing Histc operation.
+ """
+ return _get_node_factory_opset17().create(
+ "Histc",
+ as_nodes(data, name=name),
+ {"bins": bins, "min": min, "max": max, "preserve_input_type": preserve_input_type},
+ )
+
+
@nameable_op
def rgb_to_nv12(
arg: NodeInput,
diff --git a/src/bindings/python/tests/test_graph/test_histc.py b/src/bindings/python/tests/test_graph/test_histc.py
new file mode 100644
index 00000000000..52f97a5e2df
--- /dev/null
+++ b/src/bindings/python/tests/test_graph/test_histc.py
@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*-
+# Copyright (C) 2018-2026 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import numpy as np
+import pytest
+
+from openvino import Type
+from openvino.opset15 import parameter
+import openvino.opset17 as ov
+
+
+def test_histc_defaults():
+ data = parameter([2, 3, 4], name="data", dtype=np.float32)
+ node = ov.histc(data)
+
+ assert node.get_type_name() == "Histc"
+ assert node.get_output_size() == 1
+ assert node.get_output_element_type(0) == Type.f32
+ assert list(node.get_output_shape(0)) == [100]
+ assert node.get_attributes() == {"bins": 100, "min": 0.0, "max": 0.0, "preserve_input_type": True}
+
+
+def test_histc_attributes_and_name():
+ data = parameter([5], name="data", dtype=np.float32)
+ node = ov.histc(data, bins=4, min=0.0, max=8.0, name="hist")
+
+ assert node.get_type_name() == "Histc"
+ assert node.get_friendly_name() == "hist"
+ assert node.get_output_element_type(0) == Type.f32
+ assert list(node.get_output_shape(0)) == [4]
+ assert node.get_attributes() == {"bins": 4, "min": 0.0, "max": 8.0, "preserve_input_type": True}
+
+
+ ("dtype", "preserve_input_type", "expected_type"),
+ [
+ (np.float32, True, Type.f32),
+ (np.int32, True, Type.i32),
+ (np.uint8, True, Type.u8),
+ (np.uint8, False, Type.i32),
+ (np.float32, False, Type.i32),
+ ],
+)
+def test_histc_preserve_input_type(dtype, preserve_input_type, expected_type):
+ data = parameter([5], name="data", dtype=dtype)
+ node = ov.histc(data, bins=4, preserve_input_type=preserve_input_type)
+
+ assert node.get_output_element_type(0) == expected_type
+ assert node.get_attributes()["preserve_input_type"] is preserve_input_type
+
+
+def test_histc_attributes_are_keyword_only():
+ data = parameter([5], name="data", dtype=np.float32)
+ with pytest.raises(TypeError):
+ ov.histc(data, 4)