OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Core][Ref][CPU] Add `Histc-17` to Core, add reference implementation, reuse ref in CPU Plugin (#39286)
Przemyslaw Wysocki508f16c5c366
diff --git a/src/core/dev_api/openvino/op/ops_decl.hpp b/src/core/dev_api/openvino/op/ops_decl.hppindex 0bccc6c89b4..1afec77b345 100644--- a/src/core/dev_api/openvino/op/ops_decl.hpp+++ b/src/core/dev_api/openvino/op/ops_decl.hpp@@ -294,6 +294,7 @@ class SparseFillEmptyRows;namespace ov::op::v17 {class ErfInv;class GroupedMatMul;+class Histc;class QSAPrepare;class SparseAttention;class SparseAttentionIndexer;diff --git a/src/core/include/openvino/op/histc.hpp b/src/core/include/openvino/op/histc.hppnew file mode 100644index 00000000000..7523d466535--- /dev/null+++ b/src/core/include/openvino/op/histc.hpp@@ -0,0 +1,67 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "openvino/op/op.hpp"++namespace ov::op::v17 {+/// \brief Histc operation computes the histogram of all input elements using equal-width bins.+///+/// \ingroup ov_ops_cpp_api+class OPENVINO_API Histc : public Op {+public:+ OPENVINO_OP("Histc", "opset17");++ Histc() = default;++ /// \brief Constructs a Histc operation.+ ///+ /// \param data Input tensor of numeric type and arbitrary shape.+ /// \param bins Number of equal-width bins.+ /// \param min Lower end of the range (inclusive). If equal to max, the range is taken from data.+ /// \param max Upper end of the range (inclusive). If equal to min, the range is taken from data.+ /// \param preserve_input_type If true, the output has the input type, otherwise i32.+ Histc(const Output<Node>& data,+ int64_t bins = 100,+ double min = 0.0,+ double max = 0.0,+ bool preserve_input_type = true);++ bool visit_attributes(AttributeVisitor& visitor) override;+ void validate_and_infer_types() override;+ std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& new_args) const override;++ int64_t get_bins() const {+ return m_bins;+ }+ void set_bins(int64_t bins) {+ m_bins = bins;+ }+ double get_min() const {+ return m_min;+ }+ void set_min(double min) {+ m_min = min;+ }+ double get_max() const {+ return m_max;+ }+ void set_max(double max) {+ m_max = max;+ }+ bool get_preserve_input_type() const {+ return m_preserve_input_type;+ }+ void set_preserve_input_type(bool preserve_input_type) {+ m_preserve_input_type = preserve_input_type;+ }++private:+ int64_t m_bins{100};+ double m_min{0.0};+ double m_max{0.0};+ bool m_preserve_input_type{true};+};+} // namespace ov::op::v17diff --git a/src/core/include/openvino/op/ops.hpp b/src/core/include/openvino/op/ops.hppindex f7c38d50700..197839e1b89 100644--- a/src/core/include/openvino/op/ops.hpp+++ b/src/core/include/openvino/op/ops.hpp@@ -89,6 +89,7 @@#include "openvino/op/gru_cell.hpp"#include "openvino/op/gru_sequence.hpp"#include "openvino/op/hard_sigmoid.hpp"+#include "openvino/op/histc.hpp"#include "openvino/op/hsigmoid.hpp"#include "openvino/op/hswish.hpp"#include "openvino/op/i420_to_bgr.hpp"diff --git a/src/core/include/openvino/opsets/opset17_tbl.hpp b/src/core/include/openvino/opsets/opset17_tbl.hppindex a8de565b3df..b08fe312ed9 100644--- a/src/core/include/openvino/opsets/opset17_tbl.hpp+++ b/src/core/include/openvino/opsets/opset17_tbl.hpp@@ -18,6 +18,7 @@ _OPENVINO_OP_REG(ErfInv, ov::op::v17)_OPENVINO_OP_REG(GroupedMatMul, ov::op::v17)_OPENVINO_OP_REG(RGBtoNV12, ov::op::v17)_OPENVINO_OP_REG(BGRtoNV12, ov::op::v17)+_OPENVINO_OP_REG(Histc, ov::op::v17)_OPENVINO_OP_REG(QSAPrepare, ov::op::v17)_OPENVINO_OP_REG(SparseAttention, ov::op::v17)_OPENVINO_OP_REG(SparseAttentionIndexer, ov::op::v17)diff --git a/src/core/include/public_headers.cmake b/src/core/include/public_headers.cmakeindex a74b84a8c05..7b701d082ce 100644--- a/src/core/include/public_headers.cmake+++ b/src/core/include/public_headers.cmake@@ -145,6 +145,7 @@ set(PUBLIC_HEADERS${CMAKE_CURRENT_LIST_DIR}/openvino/op/gru_cell.hpp${CMAKE_CURRENT_LIST_DIR}/openvino/op/gru_sequence.hpp${CMAKE_CURRENT_LIST_DIR}/openvino/op/hard_sigmoid.hpp+ ${CMAKE_CURRENT_LIST_DIR}/openvino/op/histc.hpp${CMAKE_CURRENT_LIST_DIR}/openvino/op/hsigmoid.hpp${CMAKE_CURRENT_LIST_DIR}/openvino/op/hswish.hpp${CMAKE_CURRENT_LIST_DIR}/openvino/op/i420_to_bgr.hppdiff --git a/src/core/reference/include/openvino/reference/histc.hpp b/src/core/reference/include/openvino/reference/histc.hppnew file mode 100644index 00000000000..cd43d0e48a1--- /dev/null+++ b/src/core/reference/include/openvino/reference/histc.hpp@@ -0,0 +1,80 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <algorithm>+#include <cmath>+#include <cstddef>+#include <limits>+#include <type_traits>+#include <vector>++#include "openvino/core/except.hpp"++namespace ov::reference {++/**+ * @brief Reference implementation of Histc operator (compatible with torch.histc).+ *+ * @param data Pointer to input data.+ * @param out Pointer to output data with `bins` elements. Counts that do not fit into TOut saturate.+ * @param count Number of input elements.+ * @param bins Number of equal-width bins.+ * @param min Lower end of the range. If equal to max, the range is taken from the data.+ * @param max Upper end of the range. If equal to min, the range is taken from the data.+ */+template <class T, class TOut = T>+void histc(const T* data, TOut* out, const size_t count, const size_t bins, double min, double max) {+ // Like torch.histc, f32 is binned in f32; integers are widened to keep fractional range ends.+ using C = std::conditional_t<std::is_same_v<T, double> || std::is_integral_v<T>, double, float>;++ if (min == max && count > 0) {+ // Like torch.histc, a NaN element makes the range NaN, which is rejected below.+ min = std::numeric_limits<double>::infinity();+ max = -std::numeric_limits<double>::infinity();+ for (size_t i = 0; i < count; ++i) {+ const auto x = static_cast<double>(data[i]);+ if (std::isnan(x)) {+ min = max = x;+ break;+ }+ if (x < min)+ min = x;+ if (x > max)+ max = x;+ }+ }+ if (min == max) {+ if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double>) {+ min =+ std::min(static_cast<double>(std::nextafter(static_cast<T>(min), -std::numeric_limits<T>::infinity())),+ min - 1.0);+ max = std::max(static_cast<double>(std::nextafter(static_cast<T>(max), std::numeric_limits<T>::infinity())),+ max + 1.0);+ } else {+ min -= 1.0;+ max += 1.0;+ }+ }++ const auto lo = static_cast<C>(min);+ const auto hi = static_cast<C>(max);+ OPENVINO_ASSERT(std::isfinite(lo) && std::isfinite(hi), "Histc: range [", min, ", ", max, "] is not finite.");+ OPENVINO_ASSERT(lo < hi, "Histc: max must be larger than min. Got range [", min, ", ", max, "].");++ std::vector<size_t> hist(bins, 0);+ for (size_t i = 0; i < count; ++i) {+ const auto x = static_cast<C>(data[i]);+ if (!(x >= lo && x <= hi))+ continue;+ const auto idx = static_cast<size_t>((x - lo) * static_cast<C>(bins) / (hi - lo));+ ++hist[std::min(idx, bins - 1)];+ }+ std::transform(hist.begin(), hist.end(), out, [](size_t n) {+ return static_cast<TOut>(+ std::min(static_cast<double>(n), static_cast<double>(std::numeric_limits<TOut>::max())));+ });+}+} // namespace ov::referencediff --git a/src/core/reference/sources.cmake b/src/core/reference/sources.cmakeindex faff3078bef..f489f3a685d 100644--- a/src/core/reference/sources.cmake+++ b/src/core/reference/sources.cmake@@ -144,6 +144,7 @@ set(REFERENCE_HEADERS${CMAKE_CURRENT_LIST_DIR}/include/openvino/reference/grouped_matmul.hpp${CMAKE_CURRENT_LIST_DIR}/include/openvino/reference/gru_cell.hpp${CMAKE_CURRENT_LIST_DIR}/include/openvino/reference/hard_sigmoid.hpp+ ${CMAKE_CURRENT_LIST_DIR}/include/openvino/reference/histc.hpp${CMAKE_CURRENT_LIST_DIR}/include/openvino/reference/hsigmoid.hpp${CMAKE_CURRENT_LIST_DIR}/include/openvino/reference/hswish.hpp${CMAKE_CURRENT_LIST_DIR}/include/openvino/reference/identity.hppdiff --git a/src/core/shape_inference/CMakeLists.txt b/src/core/shape_inference/CMakeLists.txtindex 9ded1edb770..6c83c1b9f4b 100644--- a/src/core/shape_inference/CMakeLists.txt+++ b/src/core/shape_inference/CMakeLists.txt@@ -79,6 +79,7 @@ target_sources(${TARGET_NAME}${SHAPE_INFER_INCLUDE_DIR}/grouped_matmul_shape_inference.hpp${SHAPE_INFER_INCLUDE_DIR}/gru_cell_shape_inference.hpp${SHAPE_INFER_INCLUDE_DIR}/gru_sequence_shape_inference.hpp+ ${SHAPE_INFER_INCLUDE_DIR}/histc_shape_inference.hpp${SHAPE_INFER_INCLUDE_DIR}/i420_shape_inference.hpp${SHAPE_INFER_INCLUDE_DIR}/identity_shape_inference.hpp${SHAPE_INFER_INCLUDE_DIR}/interpolate_shape_inference.hppdiff --git a/src/core/shape_inference/include/histc_shape_inference.hpp b/src/core/shape_inference/include/histc_shape_inference.hppnew file mode 100644index 00000000000..6b257405c56--- /dev/null+++ b/src/core/shape_inference/include/histc_shape_inference.hpp@@ -0,0 +1,18 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "openvino/op/histc.hpp"+#include "utils.hpp"++namespace ov::op::v17 {++template <class TShape, class TRShape = result_shape_t<TShape>>+std::vector<TRShape> shape_infer(const Histc* op, const std::vector<TShape>& input_shapes) {+ NODE_VALIDATION_CHECK(op, input_shapes.size() == 1);+ NODE_VALIDATION_CHECK(op, op->get_bins() > 0, "Attribute 'bins' must be positive. Got: ", op->get_bins());+ return {TRShape{static_cast<typename TRShape::value_type>(op->get_bins())}};+}+} // namespace ov::op::v17diff --git a/src/core/src/op/histc.cpp b/src/core/src/op/histc.cppnew file mode 100644index 00000000000..a0a316b7848--- /dev/null+++ b/src/core/src/op/histc.cpp@@ -0,0 +1,65 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/histc.hpp"++#include <cmath>++#include "histc_shape_inference.hpp"+#include "itt.hpp"++namespace ov::op::v17 {++Histc::Histc(const Output<Node>& data,+ const int64_t bins,+ const double min,+ const double max,+ const bool preserve_input_type)+ : Op({data}),+ m_bins{bins},+ m_min{min},+ m_max{max},+ m_preserve_input_type{preserve_input_type} {+ constructor_validate_and_infer_types();+}++bool Histc::visit_attributes(AttributeVisitor& visitor) {+ OV_OP_SCOPE(v17_Histc_visit_attributes);+ visitor.on_attribute("bins", m_bins);+ visitor.on_attribute("min", m_min);+ visitor.on_attribute("max", m_max);+ visitor.on_attribute("preserve_input_type", m_preserve_input_type);+ return true;+}++void Histc::validate_and_infer_types() {+ OV_OP_SCOPE(v17_Histc_validate_and_infer_types);+ const auto& data_et = get_input_element_type(0);+ NODE_VALIDATION_CHECK(this,+ data_et.is_dynamic() || data_et.is_real() || data_et.is_integral_number(),+ "Input element type must be numeric. Got: ",+ data_et);+ NODE_VALIDATION_CHECK(this,+ std::isfinite(m_min) && std::isfinite(m_max),+ "Attributes 'min' and 'max' must be finite. Got: ",+ m_min,+ " and ",+ m_max);+ NODE_VALIDATION_CHECK(this,+ m_min <= m_max,+ "Attribute 'min' must be less or equal than 'max'. Got: ",+ m_min,+ " and ",+ m_max);++ const auto output_shapes = shape_infer(this, ov::util::get_node_input_partial_shapes(*this));+ set_output_type(0, m_preserve_input_type ? data_et : element::i32, output_shapes[0]);+}++std::shared_ptr<Node> Histc::clone_with_new_inputs(const OutputVector& new_args) const {+ OV_OP_SCOPE(v17_Histc_clone_with_new_inputs);+ check_new_args_count(this, new_args);+ return std::make_shared<Histc>(new_args.at(0), m_bins, m_min, m_max, m_preserve_input_type);+}+} // namespace ov::op::v17diff --git a/src/core/src/sources.cmake b/src/core/src/sources.cmakeindex 6f6e42d6c8f..57db353d253 100644--- a/src/core/src/sources.cmake+++ b/src/core/src/sources.cmake@@ -137,6 +137,7 @@ set(LIBRARY_SRC${CMAKE_CURRENT_LIST_DIR}/op/gru_cell.cpp${CMAKE_CURRENT_LIST_DIR}/op/gru_sequence.cpp${CMAKE_CURRENT_LIST_DIR}/op/hard_sigmoid.cpp+ ${CMAKE_CURRENT_LIST_DIR}/op/histc.cpp${CMAKE_CURRENT_LIST_DIR}/op/hsigmoid.cpp${CMAKE_CURRENT_LIST_DIR}/op/hswish.cpp${CMAKE_CURRENT_LIST_DIR}/op/i420_to_bgr.cppdiff --git a/src/core/tests/opset.cpp b/src/core/tests/opset.cppindex 401880f209f..a89eb304ab4 100644--- a/src/core/tests/opset.cpp+++ b/src/core/tests/opset.cpp@@ -79,7 +79,7 @@ INSTANTIATE_TEST_SUITE_P(opset,OpsetTestParams{ov::get_opset14, 188},OpsetTestParams{ov::get_opset15, 199},OpsetTestParams{ov::get_opset16, 203},- OpsetTestParams{ov::get_opset17, 10}),+ OpsetTestParams{ov::get_opset17, 11}),OpsetTestNameGenerator{});class MyOpOld : public ov::op::Op {diff --git a/src/core/tests/type_prop/histc.cpp b/src/core/tests/type_prop/histc.cppnew file mode 100644index 00000000000..16052149067--- /dev/null+++ b/src/core/tests/type_prop/histc.cpp@@ -0,0 +1,123 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/histc.hpp"++#include <gtest/gtest.h>++#include <limits>++#include "common_test_utils/test_assertions.hpp"+#include "common_test_utils/type_prop.hpp"++namespace ov::test {+using op::v0::Parameter;+using testing::HasSubstr;++class TypePropHistcTest : public TypePropOpTest<op::v17::Histc> {};++TEST_F(TypePropHistcTest, default_ctor) {+ const auto op = make_op();+ op->set_argument(0, std::make_shared<Parameter>(element::f32, PartialShape{2, 3}));+ op->validate_and_infer_types();++ EXPECT_EQ(op->get_bins(), 100);+ EXPECT_EQ(op->get_min(), 0.0);+ EXPECT_EQ(op->get_max(), 0.0);+ EXPECT_TRUE(op->get_preserve_input_type());+ EXPECT_EQ(op->get_output_element_type(0), element::f32);+ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{100}));+}++TEST_F(TypePropHistcTest, output_shape_does_not_depend_on_input_shape) {+ for (const auto& shape :+ {PartialShape{2, 3, 4}, PartialShape{}, PartialShape{0}, PartialShape{-1, {2, 5}}, PartialShape::dynamic()}) {+ const auto op = make_op(std::make_shared<Parameter>(element::f16, shape), 4, 0.0, 3.0);+ EXPECT_EQ(op->get_output_element_type(0), element::f16);+ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{4})) << shape;+ }+}++TEST_F(TypePropHistcTest, element_types) {+ for (const auto& et : {element::f32, element::f64, element::bf16, element::i32, element::u8, element::dynamic}) {+ const auto op = make_op(std::make_shared<Parameter>(et, PartialShape{5}), 3);+ EXPECT_EQ(op->get_output_element_type(0), et);+ }+}++TEST_F(TypePropHistcTest, i32_output_when_input_type_not_preserved) {+ for (const auto& et :+ {element::f32, element::f16, element::bf16, element::i64, element::i32, element::u8, element::dynamic}) {+ const auto op = make_op(std::make_shared<Parameter>(et, PartialShape{5}), 3, 0.0, 0.0, false);+ EXPECT_EQ(op->get_output_element_type(0), element::i32) << et;+ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{3}));+ }+}++TEST_F(TypePropHistcTest, invalid_element_type) {+ const auto data = std::make_shared<Parameter>(element::boolean, PartialShape{5});+ OV_EXPECT_THROW(std::ignore = make_op(data), NodeValidationFailure, HasSubstr("must be numeric"));+}++TEST_F(TypePropHistcTest, invalid_bins) {+ const auto data = std::make_shared<Parameter>(element::f32, PartialShape{5});+ OV_EXPECT_THROW(std::ignore = make_op(data, 0), NodeValidationFailure, HasSubstr("'bins' must be positive"));+ OV_EXPECT_THROW(std::ignore = make_op(data, -3), NodeValidationFailure, HasSubstr("'bins' must be positive"));+}++TEST_F(TypePropHistcTest, invalid_range) {+ const auto data = std::make_shared<Parameter>(element::f32, PartialShape{5});+ const auto inf = std::numeric_limits<double>::infinity();+ OV_EXPECT_THROW(std::ignore = make_op(data, 4, 2.0, 1.0),+ NodeValidationFailure,+ HasSubstr("'min' must be less or equal than 'max'"));+ OV_EXPECT_THROW(std::ignore = make_op(data, 4, -inf, 1.0), NodeValidationFailure, HasSubstr("must be finite"));+ OV_EXPECT_THROW(std::ignore = make_op(data, 4, 0.0, std::numeric_limits<double>::quiet_NaN()),+ NodeValidationFailure,+ HasSubstr("must be finite"));+ OV_EXPECT_THROW(std::ignore = make_op(data, 4, 0.0, inf), NodeValidationFailure, HasSubstr("must be finite"));+}++TEST_F(TypePropHistcTest, valid_attribute_edges) {+ const auto data = std::make_shared<Parameter>(element::f32, PartialShape{5});+ EXPECT_EQ(make_op(data, 1, 5.0, 5.0)->get_output_partial_shape(0), (PartialShape{1}));+ EXPECT_EQ(make_op(data, 3, -3.0, -1.0)->get_output_partial_shape(0), (PartialShape{3}));+ EXPECT_EQ(make_op(data, int64_t{1} << 20, -1e300, 1e300)->get_output_partial_shape(0),+ (PartialShape{int64_t{1} << 20}));+}++TEST_F(TypePropHistcTest, setters_are_validated) {+ const auto op = make_op(std::make_shared<Parameter>(element::f32, PartialShape{5}), 4, 0.0, 1.0);+ op->set_bins(6);+ op->validate_and_infer_types();+ EXPECT_EQ(op->get_output_partial_shape(0), (PartialShape{6}));++ op->set_min(2.0);+ OV_EXPECT_THROW(op->validate_and_infer_types(),+ NodeValidationFailure,+ HasSubstr("'min' must be less or equal than 'max'"));+ op->set_min(0.0);+ op->set_bins(0);+ OV_EXPECT_THROW(op->validate_and_infer_types(), NodeValidationFailure, HasSubstr("'bins' must be positive"));++ op->set_bins(4);+ op->set_preserve_input_type(false);+ op->validate_and_infer_types();+ EXPECT_EQ(op->get_output_element_type(0), element::i32);+}++TEST_F(TypePropHistcTest, clone_preserves_attributes) {+ const auto op = make_op(std::make_shared<Parameter>(element::f32, PartialShape{5}), 7, -1.5, 2.5, false);+ const auto new_data = std::make_shared<Parameter>(element::u8, PartialShape{-1, 3});+ const auto clone = ov::as_type_ptr<op::v17::Histc>(op->clone_with_new_inputs({new_data}));++ ASSERT_NE(clone, nullptr);+ EXPECT_EQ(clone->get_bins(), 7);+ EXPECT_EQ(clone->get_min(), -1.5);+ EXPECT_EQ(clone->get_max(), 2.5);+ EXPECT_FALSE(clone->get_preserve_input_type());+ EXPECT_EQ(clone->get_output_element_type(0), element::i32);+ EXPECT_EQ(clone->get_output_partial_shape(0), (PartialShape{7}));+}+} // namespace ov::testdiff --git a/src/core/tests/type_prop/sources.cmake b/src/core/tests/type_prop/sources.cmakeindex d589acbf650..a6e0f677527 100644--- a/src/core/tests/type_prop/sources.cmake+++ b/src/core/tests/type_prop/sources.cmake@@ -97,6 +97,7 @@ set(OV_CORE_TESTS_TYPE_PROP_SRCS${CMAKE_CURRENT_LIST_DIR}/gru_cell.cpp${CMAKE_CURRENT_LIST_DIR}/gru_sequence.cpp${CMAKE_CURRENT_LIST_DIR}/hard_sigmoid.cpp+ ${CMAKE_CURRENT_LIST_DIR}/histc.cpp${CMAKE_CURRENT_LIST_DIR}/hsigmoid.cpp${CMAKE_CURRENT_LIST_DIR}/hswish.cpp${CMAKE_CURRENT_LIST_DIR}/identity.cppdiff --git a/src/core/tests/visitors/op/histc.cpp b/src/core/tests/visitors/op/histc.cppnew file mode 100644index 00000000000..2c7a3aadd2e--- /dev/null+++ b/src/core/tests/visitors/op/histc.cpp@@ -0,0 +1,27 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/histc.hpp"++#include <gtest/gtest.h>++#include "openvino/op/parameter.hpp"+#include "visitors/visitors.hpp"++using ov::test::NodeBuilder;++TEST(attributes, histc_op) {+ NodeBuilder::opset().insert<ov::op::v17::Histc>();+ const auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{2, 4});+ const auto histc = std::make_shared<ov::op::v17::Histc>(data, 7, -1.5, 3.25, false);++ NodeBuilder builder(histc, {data});+ const auto g_histc = ov::as_type_ptr<ov::op::v17::Histc>(builder.create());++ EXPECT_EQ(builder.get_value_map_size(), 4);+ EXPECT_EQ(g_histc->get_bins(), histc->get_bins());+ EXPECT_EQ(g_histc->get_min(), histc->get_min());+ EXPECT_EQ(g_histc->get_max(), histc->get_max());+ EXPECT_EQ(g_histc->get_preserve_input_type(), histc->get_preserve_input_type());+}diff --git a/src/core/tests/visitors/sources.cmake b/src/core/tests/visitors/sources.cmakeindex cc5e456e205..52d94ed6f55 100644--- a/src/core/tests/visitors/sources.cmake+++ b/src/core/tests/visitors/sources.cmake@@ -97,6 +97,7 @@ set(OV_CORE_TESTS_VISITORS_SRCS${CMAKE_CURRENT_LIST_DIR}/op/gru_cell.cpp${CMAKE_CURRENT_LIST_DIR}/op/gru_sequence.cpp${CMAKE_CURRENT_LIST_DIR}/op/hard_sigmoid.cpp+ ${CMAKE_CURRENT_LIST_DIR}/op/histc.cpp${CMAKE_CURRENT_LIST_DIR}/op/hsigmoid.cpp${CMAKE_CURRENT_LIST_DIR}/op/hswish.cpp${CMAKE_CURRENT_LIST_DIR}/op/identity.cppdiff --git a/src/plugins/intel_cpu/sources.cmake b/src/plugins/intel_cpu/sources.cmakeindex 694546e27db..01fb50ef259 100644--- a/src/plugins/intel_cpu/sources.cmake+++ b/src/plugins/intel_cpu/sources.cmake@@ -299,6 +299,8 @@ set(CPU_PLUGIN_SRCS${CPU_SRC_DIR}/nodes/grid_sample.hpp${CPU_SRC_DIR}/nodes/grn.cpp${CPU_SRC_DIR}/nodes/grn.h+ ${CPU_SRC_DIR}/nodes/histc.cpp+ ${CPU_SRC_DIR}/nodes/histc.h${CPU_SRC_DIR}/nodes/identity.cpp${CPU_SRC_DIR}/nodes/identity.hpp${CPU_SRC_DIR}/nodes/if.cppdiff --git a/src/plugins/intel_cpu/src/cpu_types.cpp b/src/plugins/intel_cpu/src/cpu_types.cppindex e2888123a73..c57e489e7e6 100644--- a/src/plugins/intel_cpu/src/cpu_types.cpp+++ b/src/plugins/intel_cpu/src/cpu_types.cpp@@ -89,6 +89,7 @@ static const TypeToNameMap& get_type_to_name_tbl() {{"SoftSign", Type::Eltwise},{"ErfInv", Type::Eltwise},{"SegmentMax", Type::SegmentMax},+ {"Histc", Type::Histc},{"Select", Type::Eltwise},{"Log", Type::Eltwise},{"BitwiseAnd", Type::Eltwise},@@ -404,6 +405,7 @@ std::string NameFromType(const Type type) {CASE(RMS);CASE(SearchSorted);CASE(SegmentMax);+ CASE(Histc);CASE(LoRA);CASE(GatherMatmul);CASE(GatedDeltaNet);diff --git a/src/plugins/intel_cpu/src/cpu_types.h b/src/plugins/intel_cpu/src/cpu_types.hindex 7127cca5c6d..7eead701b6c 100644--- a/src/plugins/intel_cpu/src/cpu_types.h+++ b/src/plugins/intel_cpu/src/cpu_types.h@@ -138,6 +138,7 @@ enum class Type : uint8_t {RMS,SearchSorted,SegmentMax,+ Histc,LoRA,GatherMatmul,GatedDeltaNet,diff --git a/src/plugins/intel_cpu/src/nodes/histc.cpp b/src/plugins/intel_cpu/src/nodes/histc.cppnew file mode 100644index 00000000000..b88ef724d67--- /dev/null+++ b/src/plugins/intel_cpu/src/nodes/histc.cpp@@ -0,0 +1,107 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "histc.h"++#include <cstdint>+#include <memory>+#include <oneapi/dnnl/dnnl_common.hpp>+#include <string>++#include "cpu_types.h"+#include "graph_context.h"+#include "memory_desc/cpu_memory_desc.h"+#include "node.h"+#include "onednn/iml_type_mapper.h"+#include "openvino/core/except.hpp"+#include "openvino/core/node.hpp"+#include "openvino/core/shape.hpp"+#include "openvino/core/type.hpp"+#include "openvino/core/type/element_type.hpp"+#include "openvino/op/histc.hpp"+#include "openvino/reference/histc.hpp"+#include "shape_inference/shape_inference_cpu.hpp"+#include "utils/general_utils.h"++namespace ov::intel_cpu::node {++Histc::Histc(const std::shared_ptr<ov::Node>& op, const GraphContext::CPtr& context)+ : Node(op, context, NgraphShapeInferFactory(op)) {+ std::string errorMessage;+ if (!isSupportedOperation(op, errorMessage)) {+ OPENVINO_THROW_NOT_IMPLEMENTED(errorMessage);+ }+ const auto histc = ov::as_type_ptr<const ov::op::v17::Histc>(op);+ m_min = histc->get_min();+ m_max = histc->get_max();+ m_i32_output = !histc->get_preserve_input_type();+}++bool Histc::isSupportedOperation(const std::shared_ptr<const ov::Node>& op, std::string& errorMessage) noexcept {+ if (!ov::is_type<ov::op::v17::Histc>(op)) {+ errorMessage = "Only opset17 Histc operation is supported";+ return false;+ }+ return true;+}++void Histc::initSupportedPrimitiveDescriptors() {+ if (!supportedPrimitiveDescriptors.empty()) {+ return;+ }++ auto prec = getOriginalInputPrecisionAtPort(0);+ if (prec.is_real()) {+ prec = ov::element::f32;+ } else if (none_of(prec, ov::element::i8, ov::element::u8)) {+ prec = ov::element::i32;+ }+ addSupportedPrimDesc({{LayoutType::ncsp, prec}},+ {{LayoutType::ncsp, m_i32_output ? ov::element::i32 : prec}},+ impl_desc_type::ref);+}++bool Histc::created() const {+ return getType() == Type::Histc;+}++template <class T>+void Histc::executeImpl() {+ const auto& src = getSrcMemoryAtPort(0);+ const auto run = [&](auto* dst) {+ ov::reference::histc(src->getDataAs<const T>(),+ dst,+ ov::shape_size(src->getStaticDims()),+ ov::shape_size(getDstMemoryAtPort(0)->getStaticDims()),+ m_min,+ m_max);+ };+ if (m_i32_output) {+ run(getDstDataAtPortAs<int32_t>(0));+ } else {+ run(getDstDataAtPortAs<T>(0));+ }+}++void Histc::execute([[maybe_unused]] const dnnl::stream& strm) {+ const auto prec = getParentEdgeAt(0)->getMemory().getDesc().getPrecision();+ switch (prec) {+ case ov::element::f32:+ executeImpl<float>();+ break;+ case ov::element::i32:+ executeImpl<int32_t>();+ break;+ case ov::element::i8:+ executeImpl<int8_t>();+ break;+ case ov::element::u8:+ executeImpl<uint8_t>();+ break;+ default:+ CPU_NODE_THROW("has unsupported precision: ", prec);+ }+}++} // namespace ov::intel_cpu::nodediff --git a/src/plugins/intel_cpu/src/nodes/histc.h b/src/plugins/intel_cpu/src/nodes/histc.hnew file mode 100644index 00000000000..8abb9546df1--- /dev/null+++ b/src/plugins/intel_cpu/src/nodes/histc.h@@ -0,0 +1,49 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <memory>+#include <oneapi/dnnl/dnnl_common.hpp>+#include <string>++#include "graph_context.h"+#include "node.h"+#include "openvino/core/node.hpp"++namespace ov::intel_cpu::node {++class Histc : public Node {+public:+ Histc(const std::shared_ptr<ov::Node>& op, const GraphContext::CPtr& context);++ static bool isSupportedOperation(const std::shared_ptr<const ov::Node>& op, std::string& errorMessage) noexcept;+ void getSupportedDescriptors() override {}+ void initSupportedPrimitiveDescriptors() override;+ void execute(const dnnl::stream& strm) override;+ [[nodiscard]] bool created() const override;+ [[nodiscard]] bool needPrepareParams() const override {+ return false;+ }+ void executeDynamicImpl(const dnnl::stream& strm) override {+ execute(strm);+ }+ // Empty input still produces a zero-filled histogram.+ [[nodiscard]] bool isExecutable() const override {+ return true;+ }+ [[nodiscard]] bool neverExecute() const override {+ return false;+ }++private:+ template <class T>+ void executeImpl();++ double m_min = 0.0;+ double m_max = 0.0;+ bool m_i32_output = false;+};++} // namespace ov::intel_cpu::nodediff --git a/src/plugins/intel_cpu/src/nodes_factory.cpp b/src/plugins/intel_cpu/src/nodes_factory.cppindex 327cdbafc73..3262aa19a09 100644--- a/src/plugins/intel_cpu/src/nodes_factory.cpp+++ b/src/plugins/intel_cpu/src/nodes_factory.cpp@@ -45,6 +45,7 @@#include "nodes/gathermatmul.h"#include "nodes/generate_proposals.h"#include "nodes/grn.h"+#include "nodes/histc.h"#include "nodes/identity.hpp"#include "nodes/if.h"#include "nodes/input.h"@@ -247,6 +248,7 @@ Node::NodesFactory::NodesFactory() : Factory("NodesFactory") {INTEL_CPU_NODE(PaKVReorder, Type::PaKVReorder);INTEL_CPU_NODE(SearchSorted, Type::SearchSorted);INTEL_CPU_NODE(SegmentMax, Type::SegmentMax);+ INTEL_CPU_NODE(Histc, Type::Histc);INTEL_CPU_NODE(LoRA, Type::LoRA);INTEL_CPU_NODE(GatherMatmul, Type::GatherMatmul);INTEL_CPU_NODE(GatedDeltaNet, Type::GatedDeltaNet);diff --git a/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp b/src/plugins/intel_cpu/src/shape_inference/shape_inference.cppindex ba3f9f48778..be70aaab375 100644--- a/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp+++ b/src/plugins/intel_cpu/src/shape_inference/shape_inference.cpp@@ -66,6 +66,7 @@#include "group_convolution_shape_inference.hpp"#include "gru_cell_shape_inference.hpp"#include "gru_sequence_shape_inference.hpp"+#include "histc_shape_inference.hpp"#include "i420_shape_inference.hpp"#include "identity_shape_inference.hpp"#include "interpolate_shape_inference.hpp"@@ -132,6 +133,7 @@#include "openvino/op/gru_cell.hpp"#include "openvino/op/gru_sequence.hpp"#include "openvino/op/hard_sigmoid.hpp"+#include "openvino/op/histc.hpp"#include "openvino/op/i420_to_bgr.hpp"#include "openvino/op/i420_to_rgb.hpp"#include "openvino/op/identity.hpp"@@ -588,6 +590,7 @@ const IStaticShapeInferFactory::TRegistry IStaticShapeInferFactory::registry{OV_OP_SHAPE_INFER_MASK_REG(op::v16::SegmentMax, ShapeInferTA, util::bit::mask(1, 2)),OV_OP_SHAPE_INFER_MASK_REG(op::v16::SparseFillEmptyRows, ShapeInferTA, util::bit::mask(1, 2)),OV_OP_SHAPE_INFER_MASK_REG(op::v16::Identity, ShapeInferTA, util::bit::mask()),+ OV_OP_SHAPE_INFER_MASK_REG(op::v17::Histc, ShapeInferTA, util::bit::mask()),// opset15OV_OP_SHAPE_INFER_MASK_REG(op::v15::Squeeze, ShapeInferTA, util::bit::mask(1)),OV_OP_SHAPE_INFER_MASK_REG(op::v15::SearchSorted, ShapeInferTA, util::bit::mask()),diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/histc.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/histc.cppnew file mode 100644index 00000000000..7402a31f2b6--- /dev/null+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/histc.cpp@@ -0,0 +1,158 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/histc.hpp"++#include <limits>++#include "common_test_utils/ov_plugin_cache.hpp"+#include "common_test_utils/ov_tensor_utils.hpp"+#include "common_test_utils/test_assertions.hpp"+#include "shared_test_classes/base/ov_subgraph.hpp"++namespace ov::test {++using HistcCPUParams = std::tuple<InputShape,+ std::tuple<int64_t, double, double>, // bins, min, max+ ElementType,+ bool>; // preserve_input_type++class HistcLayerCPUTest : public testing::WithParamInterface<HistcCPUParams>, virtual public SubgraphBaseTest {+public:+ static std::string getTestCaseName(const testing::TestParamInfo<HistcCPUParams>& obj) {+ const auto& [shape, attrs, prc, preserve_input_type] = obj.param;+ const auto& [bins, min, max] = attrs;+ std::ostringstream result;+ result << "IS=" << ov::test::utils::partialShape2str({shape.first}) << "_TS=";+ for (const auto& item : shape.second) {+ result << ov::test::utils::vec2str(item) << "_";+ }+ result << "bins=" << bins << "_min=" << min << "_max=" << max << "_prc=" << prc+ << "_preserve_input_type=" << preserve_input_type;+ return result.str();+ }++protected:+ void SetUp() override {+ targetDevice = ov::test::utils::DEVICE_CPU;+ const auto& [shape, attrs, prc, preserve_input_type] = GetParam();+ const auto& [bins, min, max] = attrs;+ init_input_shapes({shape});+ const auto data = std::make_shared<ov::op::v0::Parameter>(prc, inputDynamicShapes[0]);+ const auto histc = std::make_shared<ov::op::v17::Histc>(data, bins, min, max, preserve_input_type);+ function = std::make_shared<ov::Model>(histc->outputs(), ov::ParameterVector{data}, "Histc");+ }++ void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {+ inputs.clear();+ // Quarter steps put many values exactly on bin edges and outside the explicit ranges.+ ov::test::utils::InputGenerateData in_data(-5, 10, 4, 1234);+ const auto& param = function->get_parameters()[0];+ auto tensor =+ ov::test::utils::create_and_fill_tensor(param->get_element_type(), targetInputStaticShapes[0], in_data);+ const auto& [shape, attrs, prc, preserve_input_type] = GetParam();+ // Special values are ignored for an explicit range, but make a range taken from data infinite.+ if (std::get<1>(attrs) != std::get<2>(attrs) && tensor.get_size() >= 3) {+ switch (prc) {+ case ElementType::f32:+ set_special_values<float>(tensor);+ break;+ case ElementType::f16:+ set_special_values<ov::float16>(tensor);+ break;+ case ElementType::bf16:+ set_special_values<ov::bfloat16>(tensor);+ break;+ default:+ break;+ }+ }+ inputs.insert({param, tensor});+ }++private:+ template <class T>+ static void set_special_values(ov::Tensor& tensor) {+ auto* data = tensor.data<T>();+ data[0] = T(std::numeric_limits<float>::quiet_NaN());+ data[1] = T(std::numeric_limits<float>::infinity());+ data[2] = T(-std::numeric_limits<float>::infinity());+ }+};++TEST_P(HistcLayerCPUTest, CompareWithRefs) {+ run();+}++namespace {+const std::vector<InputShape> shapes = {+ {{}, {{}}},+ {{}, {{2, 3, 17}}},+ // Single-bin counts above 255 (u8), 127 (i8) and 256 (bf16 integer step).+ {{}, {{50, 50}}},+ {{}, {{0}}},+ {{}, {{2, 0, 3}}},+ {{-1, -1}, {{4, 5}, {0, 3}, {32, 32}, {1, 1}}},+};++const std::vector<std::tuple<int64_t, double, double>> attrs = {+ {4, 0.0, 3.0},+ {100, 0.0, 0.0},+ {5, -2.5, 2.5},+ {1, 0.0, 0.0},+ {7, -1.3, 4.1},+};++INSTANTIATE_TEST_SUITE_P(smoke_Histc,+ HistcLayerCPUTest,+ ::testing::Combine(::testing::ValuesIn(shapes),+ ::testing::ValuesIn(attrs),+ ::testing::Values(ElementType::f32,+ ElementType::f16,+ ElementType::bf16,+ ElementType::i32,+ ElementType::i64,+ ElementType::u8,+ ElementType::i8,+ ElementType::u16,+ ElementType::i16),+ ::testing::Bool()),+ HistcLayerCPUTest::getTestCaseName);++// Single-bin counts above 65535 (f16 max). i16/u16 are left out: CPU computes them in i32 and the+// output conversion wraps instead of saturating, which the spec allows as implementation defined.+INSTANTIATE_TEST_SUITE_P(smoke_Histc_LargeCounts,+ HistcLayerCPUTest,+ ::testing::Combine(::testing::Values(InputShape{{}, {{300, 300}}}),+ ::testing::Values(std::tuple<int64_t, double, double>{1, 0.0, 0.0},+ std::tuple<int64_t, double, double>{4, 0.0, 3.0}),+ ::testing::Values(ElementType::f32,+ ElementType::f16,+ ElementType::bf16,+ ElementType::i32,+ ElementType::u8,+ ElementType::i8),+ ::testing::Bool()),+ HistcLayerCPUTest::getTestCaseName);++// Like torch.histc, NaN is ignored for an explicit range but makes a range taken from data invalid.+TEST(smoke_HistcCPU, nan_in_data) {+ const auto core = ov::test::utils::PluginCache::get().core();+ const auto infer = [&](double min, double max) {+ const auto data = std::make_shared<ov::op::v0::Parameter>(ElementType::f32, ov::PartialShape{-1});+ const auto histc = std::make_shared<ov::op::v17::Histc>(data, 2, min, max);+ auto request = core->compile_model(std::make_shared<ov::Model>(histc->outputs(), ov::ParameterVector{data}),+ ov::test::utils::DEVICE_CPU)+ .create_infer_request();+ std::vector<float> values{1.f, std::numeric_limits<float>::quiet_NaN(), 2.f};+ request.set_input_tensor(ov::Tensor(ElementType::f32, ov::Shape{3}, values.data()));+ request.infer();+ const auto out = request.get_output_tensor();+ return std::vector<float>(out.data<float>(), out.data<float>() + out.get_size());+ };+ EXPECT_EQ(infer(0.0, 2.0), (std::vector<float>{0.f, 2.f}));+ OV_EXPECT_THROW(std::ignore = infer(0.0, 0.0), ov::Exception, testing::HasSubstr("is not finite"));+}+} // namespace+} // namespace ov::testdiff --git a/src/plugins/intel_cpu/tests/functional/sources.cmake b/src/plugins/intel_cpu/tests/functional/sources.cmakeindex 63f6a8ba215..43ec997b837 100644--- a/src/plugins/intel_cpu/tests/functional/sources.cmake+++ b/src/plugins/intel_cpu/tests/functional/sources.cmake@@ -116,6 +116,7 @@ set(CPU_FUNC_TESTS_CUSTOM_SRCS${CMAKE_CURRENT_LIST_DIR}/custom/single_layer_tests/group_convolution.cpp${CMAKE_CURRENT_LIST_DIR}/custom/single_layer_tests/gru_cell.cpp${CMAKE_CURRENT_LIST_DIR}/custom/single_layer_tests/gru_sequence.cpp+ ${CMAKE_CURRENT_LIST_DIR}/custom/single_layer_tests/histc.cpp${CMAKE_CURRENT_LIST_DIR}/custom/single_layer_tests/instances/common/activation.cpp${CMAKE_CURRENT_LIST_DIR}/custom/single_layer_tests/instances/common/bitwise_shift.cpp${CMAKE_CURRENT_LIST_DIR}/custom/single_layer_tests/instances/common/col2im.cppdiff --git a/src/plugins/intel_cpu/tests/unit/shape_inference_test/histc_shape_inference_test.cpp b/src/plugins/intel_cpu/tests/unit/shape_inference_test/histc_shape_inference_test.cppnew file mode 100644index 00000000000..eb405dae6a9--- /dev/null+++ b/src/plugins/intel_cpu/tests/unit/shape_inference_test/histc_shape_inference_test.cpp@@ -0,0 +1,36 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include <gtest/gtest.h>++#include "common_test_utils/test_assertions.hpp"+#include "openvino/op/histc.hpp"+#include "utils.hpp"++using namespace ov;+using namespace ov::intel_cpu;+using ov::op::v0::Parameter;++class HistcShapeInferenceTest : public OpStaticShapeInferenceTest<op::v17::Histc> {};++TEST_F(HistcShapeInferenceTest, output_is_bins_for_any_input) {+ const auto data = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto op = make_op(data, 7, 0.0, 1.0);+ for (const auto& in : {StaticShape{2, 3, 4}, StaticShape{}, StaticShape{0}}) {+ const auto output_shapes = shape_inference(op.get(), StaticShapeVector{in});+ ASSERT_EQ(output_shapes.size(), 1);+ EXPECT_EQ(output_shapes.front(), StaticShape({7}));+ }+}++TEST_F(HistcShapeInferenceTest, bins_read_at_inference) {+ const auto data = std::make_shared<Parameter>(element::f32, PartialShape::dynamic());+ const auto op = make_op(data, 7, 0.0, 1.0);+ op->set_bins(1);+ EXPECT_EQ(shape_inference(op.get(), StaticShapeVector{{3}}).front(), StaticShape({1}));+ op->set_bins(0);+ OV_EXPECT_THROW(shape_inference(op.get(), StaticShapeVector{{3}}),+ NodeValidationFailure,+ testing::HasSubstr("'bins' must be positive"));+}diff --git a/src/plugins/intel_cpu/tests/unit/sources.cmake b/src/plugins/intel_cpu/tests/unit/sources.cmakeindex fde30244b33..0d5b0ab21cc 100644--- a/src/plugins/intel_cpu/tests/unit/sources.cmake+++ b/src/plugins/intel_cpu/tests/unit/sources.cmake@@ -90,6 +90,7 @@ set(CPU_UNIT_TESTS_SRCS${CMAKE_CURRENT_LIST_DIR}/shape_inference_test/group_convolution_shape_inference_test.cpp${CMAKE_CURRENT_LIST_DIR}/shape_inference_test/gru_cell_shape_inference_test.cpp${CMAKE_CURRENT_LIST_DIR}/shape_inference_test/gru_sequence_shape_inference_test.cpp+ ${CMAKE_CURRENT_LIST_DIR}/shape_inference_test/histc_shape_inference_test.cpp${CMAKE_CURRENT_LIST_DIR}/shape_inference_test/i420_shape_inference_test.cpp${CMAKE_CURRENT_LIST_DIR}/shape_inference_test/identity_shape_inference_test.cpp${CMAKE_CURRENT_LIST_DIR}/shape_inference_test/interpolate_shape_inference_test.cppdiff --git a/src/plugins/template/backend/ops/histc.cpp b/src/plugins/template/backend/ops/histc.cppnew file mode 100644index 00000000000..94415a51df7--- /dev/null+++ b/src/plugins/template/backend/ops/histc.cpp@@ -0,0 +1,66 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/reference/histc.hpp"++#include "evaluate_node.hpp"+#include "openvino/core/type/element_type_traits.hpp"+#include "openvino/op/histc.hpp"++template <ov::element::Type_t ET>+bool evaluate(const std::shared_ptr<ov::op::v17::Histc>& op,+ ov::TensorVector& outputs,+ const ov::TensorVector& inputs) {+ using T = ov::fundamental_type_for<ET>;+ outputs[0].set_shape(op->get_output_shape(0));+ const auto run = [&](auto* out) {+ ov::reference::histc(inputs[0].data<const T>(),+ out,+ inputs[0].get_size(),+ outputs[0].get_size(),+ op->get_min(),+ op->get_max());+ };+ if (outputs[0].get_element_type() == ov::element::i32) {+ run(outputs[0].data<int32_t>());+ } else {+ run(outputs[0].data<T>());+ }+ return true;+}++template <>+bool evaluate_node<ov::op::v17::Histc>(std::shared_ptr<ov::Node> node,+ ov::TensorVector& outputs,+ const ov::TensorVector& inputs) {+ const auto op = ov::as_type_ptr<ov::op::v17::Histc>(node);+ switch (node->get_input_element_type(0)) {+ case ov::element::f16:+ return evaluate<ov::element::f16>(op, outputs, inputs);+ case ov::element::bf16:+ return evaluate<ov::element::bf16>(op, outputs, inputs);+ case ov::element::f32:+ return evaluate<ov::element::f32>(op, outputs, inputs);+ case ov::element::f64:+ return evaluate<ov::element::f64>(op, outputs, inputs);+ case ov::element::i8:+ return evaluate<ov::element::i8>(op, outputs, inputs);+ case ov::element::i16:+ return evaluate<ov::element::i16>(op, outputs, inputs);+ case ov::element::i32:+ return evaluate<ov::element::i32>(op, outputs, inputs);+ case ov::element::i64:+ return evaluate<ov::element::i64>(op, outputs, inputs);+ case ov::element::u8:+ return evaluate<ov::element::u8>(op, outputs, inputs);+ case ov::element::u16:+ return evaluate<ov::element::u16>(op, outputs, inputs);+ case ov::element::u32:+ return evaluate<ov::element::u32>(op, outputs, inputs);+ case ov::element::u64:+ return evaluate<ov::element::u64>(op, outputs, inputs);+ default:+ OPENVINO_THROW("Unhandled data type ", node->get_input_element_type(0), " in evaluate_node()");+ }+}diff --git a/src/plugins/template/backend/ops/ops_evaluates.hpp b/src/plugins/template/backend/ops/ops_evaluates.hppindex 23739a6229e..c9c8d5f2a90 100644--- a/src/plugins/template/backend/ops/ops_evaluates.hpp+++ b/src/plugins/template/backend/ops/ops_evaluates.hpp@@ -586,6 +586,10 @@ extern template bool evaluate_node<ov::op::v17::GroupedMatMul>(std::shared_ptr<oov::TensorVector& outputs,const ov::TensorVector& inputs);+extern template bool evaluate_node<ov::op::v17::Histc>(std::shared_ptr<ov::Node> node,+ ov::TensorVector& outputs,+ const ov::TensorVector& inputs);+extern template bool evaluate_node<ov::op::v16::Identity>(std::shared_ptr<ov::Node> node,ov::TensorVector& outputs,const ov::TensorVector& inputs);diff --git a/src/plugins/template/backend/opset_int_tbl.hpp b/src/plugins/template/backend/opset_int_tbl.hppindex 79a6c92e93e..a76b0a24186 100644--- a/src/plugins/template/backend/opset_int_tbl.hpp+++ b/src/plugins/template/backend/opset_int_tbl.hpp@@ -181,6 +181,7 @@ _OPENVINO_OP_REG(SearchSorted, ov::op::v15)_OPENVINO_OP_REG(AvgPool, ov::op::v16)_OPENVINO_OP_REG(ErfInv, ov::op::v17)_OPENVINO_OP_REG(GroupedMatMul, ov::op::v17)+_OPENVINO_OP_REG(Histc, ov::op::v17)_OPENVINO_OP_REG(Identity, ov::op::v16)_OPENVINO_OP_REG(ISTFT, ov::op::v16)_OPENVINO_OP_REG(SegmentMax, ov::op::v16)diff --git a/src/plugins/template/backend/sources.cmake b/src/plugins/template/backend/sources.cmakeindex 06a49a84eea..c22b3936928 100644--- a/src/plugins/template/backend/sources.cmake+++ b/src/plugins/template/backend/sources.cmake@@ -65,6 +65,7 @@ set(OPS_SRC${CMAKE_CURRENT_LIST_DIR}/ops/grouped_matmul.cpp${CMAKE_CURRENT_LIST_DIR}/ops/gru_cell.cpp${CMAKE_CURRENT_LIST_DIR}/ops/hard_sigmoid.cpp+ ${CMAKE_CURRENT_LIST_DIR}/ops/histc.cpp${CMAKE_CURRENT_LIST_DIR}/ops/identity.cpp${CMAKE_CURRENT_LIST_DIR}/ops/if.cpp${CMAKE_CURRENT_LIST_DIR}/ops/interpolate.cppdiff --git a/src/plugins/template/tests/functional/op_reference/histc.cpp b/src/plugins/template/tests/functional/op_reference/histc.cppnew file mode 100644index 00000000000..f99a15fa434--- /dev/null+++ b/src/plugins/template/tests/functional/op_reference/histc.cpp@@ -0,0 +1,373 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/histc.hpp"++#include <gtest/gtest.h>++#include <cmath>+#include <limits>++#include "base_reference_test.hpp"+#include "common_test_utils/ov_plugin_cache.hpp"+#include "common_test_utils/test_assertions.hpp"+#include "openvino/op/parameter.hpp"++using namespace reference_tests;++namespace {+struct HistcParams {+ Tensor data;+ int64_t bins;+ double min;+ double max;+ Tensor expected;+ std::string name;+ bool preserve_input_type = true;+};++class ReferenceHistcLayerTest : public testing::TestWithParam<HistcParams>, public CommonReferenceTest {+public:+ void SetUp() override {+ const auto& p = GetParam();+ const auto in = std::make_shared<ov::op::v0::Parameter>(p.data.type, p.data.shape);+ const auto histc = std::make_shared<ov::op::v17::Histc>(in, p.bins, p.min, p.max, p.preserve_input_type);+ function = std::make_shared<ov::Model>(ov::OutputVector{histc}, ov::ParameterVector{in});+ inputData = {p.data.data};+ refOutData = {p.expected.data};+ }++ static std::string getTestCaseName(const testing::TestParamInfo<HistcParams>& obj) {+ const auto& p = obj.param;+ return p.name + "_type=" + p.data.type.get_type_name() + "_shape=" + p.data.shape.to_string() ++ "_preserve_input_type=" + std::to_string(p.preserve_input_type);+ }+};++TEST_P(ReferenceHistcLayerTest, CompareWithRefs) {+ Exec();+}++// Expected values match torch.histc.+template <ov::element::Type_t ET>+std::vector<HistcParams> generateParams() {+ using T = ov::fundamental_type_for<ET>;+ using ov::Shape;+ return {+ {{Shape{3}, ET, std::vector<T>{1, 2, 1}},+ 4,+ 0.,+ 3.,+ {Shape{4}, ET, std::vector<T>{0, 2, 1, 0}},+ "explicit_range"},+ {{Shape{2, 2}, ET, std::vector<T>{1, 2, 1, 4}},+ 4,+ 0.,+ 0.,+ {Shape{4}, ET, std::vector<T>{2, 1, 0, 1}},+ "data_range"},+ {{Shape{6}, ET, std::vector<T>{0, 1, 2, 3, 4, 5}},+ 3,+ 0.5,+ 4.5,+ {Shape{3}, ET, std::vector<T>{1, 2, 1}},+ "fractional_range_out_of_range_ignored"},+ {{Shape{3}, ET, std::vector<T>{3, 3, 3}}, 4, 0., 0., {Shape{4}, ET, std::vector<T>{0, 0, 3, 0}}, "equal_data"},+ {{Shape{3}, ET, std::vector<T>{7, 1, 9}},+ 2,+ 4.,+ 4.,+ {Shape{2}, ET, std::vector<T>{1, 2}},+ "min_eq_max_nonzero"},+ {{Shape{0}, ET, std::vector<T>{}}, 3, 0., 0., {Shape{3}, ET, std::vector<T>{0, 0, 0}}, "empty"},+ {{Shape{}, ET, std::vector<T>{5}}, 2, 0., 0., {Shape{2}, ET, std::vector<T>{0, 1}}, "scalar"},+ {{Shape{2, 3}, ET, std::vector<T>{0, 1, 2, 3, 4, 5}},+ 1,+ 0.,+ 10.,+ {Shape{1}, ET, std::vector<T>{6}},+ "single_bin"},+ {{Shape{5}, ET, std::vector<T>{0, 1, 2, 3, 4}},+ 4,+ 0.,+ 4.,+ {Shape{4}, ET, std::vector<T>{1, 1, 1, 2}},+ "all_on_bin_edges"},+ {{Shape{2}, ET, std::vector<T>{1, 3}},+ 10,+ 0.,+ 0.,+ {Shape{10}, ET, std::vector<T>{1, 0, 0, 0, 0, 0, 0, 0, 0, 1}},+ "more_bins_than_elements"},+ {{Shape{3}, ET, std::vector<T>{5, 6, 7}}, 2, 0., 1., {Shape{2}, ET, std::vector<T>{0, 0}}, "all_out_of_range"},+ {{Shape{0, 4}, ET, std::vector<T>{}},+ 2,+ 5.,+ 5.,+ {Shape{2}, ET, std::vector<T>{0, 0}},+ "empty_min_eq_max_nonzero"},+ };+}++template <ov::element::Type_t ET>+std::vector<HistcParams> generateFloatParams() {+ using T = ov::fundamental_type_for<ET>;+ using ov::Shape;+ const auto inf = std::numeric_limits<float>::infinity();+ auto params = generateParams<ET>();+ const std::vector<HistcParams> float_params{+ {{Shape{5}, ET, std::vector<T>{T(-1), T(0), T(0.5), T(1), T(2)}},+ 2,+ 0.,+ 1.,+ {Shape{2}, ET, std::vector<T>{1, 2}},+ "max_goes_to_last_bin"},+ {{Shape{3}, ET, std::vector<T>{T(1), T(std::numeric_limits<float>::quiet_NaN()), T(2)}},+ 2,+ 0.,+ 2.,+ {Shape{2}, ET, std::vector<T>{0, 2}},+ "nan_ignored"},+ {{Shape{2},+ ET,+ std::vector<T>{T(std::numeric_limits<float>::quiet_NaN()), T(std::numeric_limits<float>::quiet_NaN())}},+ 2,+ 0.,+ 1.,+ {Shape{2}, ET, std::vector<T>{0, 0}},+ "all_nan_explicit_range"},+ {{Shape{4}, ET, std::vector<T>{T(-0.75), T(-0.25), T(0.25), T(0.75)}},+ 4,+ -1.,+ 1.,+ {Shape{4}, ET, std::vector<T>{1, 1, 1, 1}},+ "negative_range"},+ {{Shape{3}, ET, std::vector<T>{T(-inf), T(0.5), T(inf)}},+ 2,+ 0.,+ 1.,+ {Shape{2}, ET, std::vector<T>{0, 1}},+ "infinities_ignored"},+ {{Shape{1}, ET, std::vector<T>{T(-0.0f)}}, 2, 0., 1., {Shape{2}, ET, std::vector<T>{1, 0}}, "negative_zero"},+ };+ params.insert(params.end(), float_params.begin(), float_params.end());+ return params;+}++std::vector<HistcParams> generateCombinedParams() {+ using ov::Shape;+ std::vector<std::vector<HistcParams>> all{generateFloatParams<ov::element::f32>(),+ generateFloatParams<ov::element::f64>(),+ generateFloatParams<ov::element::f16>(),+ generateFloatParams<ov::element::bf16>(),+ generateParams<ov::element::i8>(),+ generateParams<ov::element::i32>(),+ generateParams<ov::element::i64>(),+ generateParams<ov::element::u8>(),+ generateParams<ov::element::u32>()};+ // f32 range ends are rounded to f32, so 0.3f is not above max = 0.3.+ all.push_back({{{Shape{1}, ov::element::f32, std::vector<float>{0.3f}},+ 3,+ 0.,+ 0.3,+ {Shape{3}, ov::element::f32, std::vector<float>{0, 0, 1}},+ "max_rounded_to_type"},+ // Range widening uses nextafter in f32, since 1e30 - 1 == 1e30.+ {{Shape{2}, ov::element::f32, std::vector<float>{1e30f, 1e30f}},+ 2,+ 0.,+ 0.,+ {Shape{2}, ov::element::f32, std::vector<float>{0, 2}},+ "large_equal_data"},+ {{Shape{3}, ov::element::i32, std::vector<int32_t>{-5, 0, 5}},+ 2,+ -5.,+ 5.,+ {Shape{2}, ov::element::i32, std::vector<int32_t>{1, 2}},+ "signed_int"},+ {{Shape{1}, ov::element::f32, std::vector<float>{std::nextafter(1.f, 0.f)}},+ 3,+ 0.,+ 1.,+ {Shape{3}, ov::element::f32, std::vector<float>{0, 0, 1}},+ "just_below_max"},+ {{Shape{2}, ov::element::f64, std::vector<double>{1e300, 1e300}},+ 2,+ 0.,+ 0.,+ {Shape{2}, ov::element::f64, std::vector<double>{0, 2}},+ "large_equal_data"},+ // Counts are kept in size_t, so they do not stall at 256 (bf16) or 2048 (f16) increments.+ {{Shape{1000}, ov::element::bf16, std::vector<ov::bfloat16>(1000, 1)},+ 1,+ 0.,+ 2.,+ {Shape{1}, ov::element::bf16, std::vector<ov::bfloat16>{1000}},+ "count_above_bf16_step"},+ {{Shape{4000}, ov::element::f16, std::vector<ov::float16>(4000, 1)},+ 1,+ 0.,+ 2.,+ {Shape{1}, ov::element::f16, std::vector<ov::float16>{4000}},+ "count_above_f16_step"},+ // Counts that do not fit into the output type saturate.+ {{Shape{300}, ov::element::u8, std::vector<uint8_t>(300, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::u8, std::vector<uint8_t>{255}},+ "count_saturates"},+ {{Shape{255}, ov::element::u8, std::vector<uint8_t>(255, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::u8, std::vector<uint8_t>{255}},+ "count_at_type_max"},+ {{Shape{256}, ov::element::u8, std::vector<uint8_t>(256, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::u8, std::vector<uint8_t>{255}},+ "count_above_type_max"},+ {{Shape{200}, ov::element::i8, std::vector<int8_t>(200, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::i8, std::vector<int8_t>{127}},+ "count_saturates"},+ {{Shape{40000}, ov::element::i16, std::vector<int16_t>(40000, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::i16, std::vector<int16_t>{32767}},+ "count_saturates"},+ {{Shape{70000}, ov::element::u16, std::vector<uint16_t>(70000, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::u16, std::vector<uint16_t>{65535}},+ "count_saturates"},+ // Saturates to the largest finite f16 instead of becoming inf.+ {{Shape{70000}, ov::element::f16, std::vector<ov::float16>(70000, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::f16, std::vector<ov::float16>{std::numeric_limits<ov::float16>::max()}},+ "count_saturates"},+ // Only one bin saturates, the others keep exact counts.+ {{Shape{302},+ ov::element::u8,+ [] {+ std::vector<uint8_t> v(300, 0);+ v.insert(v.end(), {2, 2});+ return v;+ }()},+ 2,+ 0.,+ 2.,+ {Shape{2}, ov::element::u8, std::vector<uint8_t>{255, 2}},+ "count_saturates_one_bin"},+ // preserve_input_type = false gives exact i32 counts for narrow input types.+ {{Shape{300}, ov::element::u8, std::vector<uint8_t>(300, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::i32, std::vector<int32_t>{300}},+ "count_exact",+ false},+ {{Shape{200}, ov::element::i8, std::vector<int8_t>(200, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::i32, std::vector<int32_t>{200}},+ "count_exact",+ false},+ {{Shape{70000}, ov::element::f16, std::vector<ov::float16>(70000, 0)},+ 1,+ 0.,+ 1.,+ {Shape{1}, ov::element::i32, std::vector<int32_t>{70000}},+ "count_exact",+ false},+ {{Shape{2, 2}, ov::element::f32, std::vector<float>{1, 2, 1, 4}},+ 4,+ 0.,+ 0.,+ {Shape{4}, ov::element::i32, std::vector<int32_t>{2, 1, 0, 1}},+ "data_range",+ false},+ {{Shape{3}, ov::element::f64, std::vector<double>{1, 2, 1}},+ 4,+ 0.,+ 3.,+ {Shape{4}, ov::element::i32, std::vector<int32_t>{0, 2, 1, 0}},+ "explicit_range",+ false},+ {{Shape{3}, ov::element::i32, std::vector<int32_t>{-5, 0, 5}},+ 2,+ -5.,+ 5.,+ {Shape{2}, ov::element::i32, std::vector<int32_t>{1, 2}},+ "signed_int",+ false},+ {{Shape{0}, ov::element::i64, std::vector<int64_t>{}},+ 3,+ 0.,+ 0.,+ {Shape{3}, ov::element::i32, std::vector<int32_t>{0, 0, 0}},+ "empty",+ false},+ {{Shape{4}, ov::element::u8, std::vector<uint8_t>{0, 5, 10, 200}},+ 2,+ -10.,+ 10.,+ {Shape{2}, ov::element::u8, std::vector<uint8_t>{0, 3}},+ "unsigned_negative_min"},+ {{Shape{2}, ov::element::i64, std::vector<int64_t>{-(int64_t{1} << 40), int64_t{1} << 40}},+ 2,+ 0.,+ 0.,+ {Shape{2}, ov::element::i64, std::vector<int64_t>{1, 1}},+ "large_int_data_range"}});+ std::vector<HistcParams> combined;+ for (const auto& params : all)+ combined.insert(combined.end(), params.begin(), params.end());+ return combined;+}++INSTANTIATE_TEST_SUITE_P(smoke_Histc_With_Hardcoded_Refs,+ ReferenceHistcLayerTest,+ testing::ValuesIn(generateCombinedParams()),+ ReferenceHistcLayerTest::getTestCaseName);++TEST(ReferenceHistcInvalidRuntimeRange, Throws) {+ const auto inf = std::numeric_limits<float>::infinity();+ const auto nan = std::numeric_limits<float>::quiet_NaN();+ const auto core = ov::test::utils::PluginCache::get().core("TEMPLATE");+ const auto infer = [&](double min, double max, std::vector<float> values) {+ const auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1});+ const auto histc = std::make_shared<ov::op::v17::Histc>(data, 4, min, max);+ auto request =+ core->compile_model(std::make_shared<ov::Model>(histc->outputs(), ov::ParameterVector{data}), "TEMPLATE")+ .create_infer_request();+ request.set_input_tensor(ov::Tensor(ov::element::f32, ov::Shape{values.size()}, values.data()));+ request.infer();+ };++ // The range taken from data must be finite.+ OV_EXPECT_THROW(infer(0.0, 0.0, {1.f, inf}), ov::Exception, testing::HasSubstr("is not finite"));+ OV_EXPECT_THROW(infer(0.0, 0.0, {-inf, -inf}), ov::Exception, testing::HasSubstr("is not finite"));+ OV_EXPECT_THROW(infer(0.0, 0.0, {nan, nan}), ov::Exception, testing::HasSubstr("is not finite"));+ // A single NaN anywhere poisons the range taken from data, as in torch.histc.+ OV_EXPECT_THROW(infer(0.0, 0.0, {nan, 1.f, 2.f}), ov::Exception, testing::HasSubstr("is not finite"));+ OV_EXPECT_THROW(infer(0.0, 0.0, {1.f, nan, 2.f}), ov::Exception, testing::HasSubstr("is not finite"));+ OV_EXPECT_THROW(infer(0.0, 0.0, {1.f, 2.f, nan}), ov::Exception, testing::HasSubstr("is not finite"));+ OV_EXPECT_THROW(infer(3.0, 3.0, {1.f, nan}), ov::Exception, testing::HasSubstr("is not finite"));+ // The range collapses to [0, 0] in f32.+ OV_EXPECT_THROW(infer(0.0, 1e-50, {0.f}), ov::Exception, testing::HasSubstr("max must be larger than min"));+ // An explicit range is valid even when data contains infinities or NaN.+ OV_ASSERT_NO_THROW(infer(0.0, 1.0, {-inf, inf, 0.5f}));+ OV_ASSERT_NO_THROW(infer(0.0, 1.0, {nan, nan}));+}+} // namespacediff --git a/src/plugins/template/tests/functional/op_reference/sources.cmake b/src/plugins/template/tests/functional/op_reference/sources.cmakeindex 1dd6c16b090..e302d8ec7ba 100644--- a/src/plugins/template/tests/functional/op_reference/sources.cmake+++ b/src/plugins/template/tests/functional/op_reference/sources.cmake@@ -98,6 +98,7 @@ set(OV_TEMPLATE_TESTS_OP_REFERENCE_SRCS${CMAKE_CURRENT_LIST_DIR}/gru_cell.cpp${CMAKE_CURRENT_LIST_DIR}/gru_sequence.cpp${CMAKE_CURRENT_LIST_DIR}/hard_sigmoid.cpp+ ${CMAKE_CURRENT_LIST_DIR}/histc.cpp${CMAKE_CURRENT_LIST_DIR}/hsigmoid.cpp${CMAKE_CURRENT_LIST_DIR}/hswish.cpp${CMAKE_CURRENT_LIST_DIR}/identity.cppdiff --git a/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/src/op_impl_check/single_op_graph.cpp b/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/src/op_impl_check/single_op_graph.cppindex 2bc9a631c76..eb0c7334cd8 100644--- a/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/src/op_impl_check/single_op_graph.cpp+++ b/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/src/op_impl_check/single_op_graph.cpp@@ -588,6 +588,13 @@ std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v17::GroupedMareturn std::make_shared<ov::Model>(results, ov::ParameterVector{mat_a, mat_b}, "GroupedMatMulGraph");}+std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v17::Histc> &node) {+ const auto data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{2, 3, 4});+ const auto histcNode = std::make_shared<ov::op::v17::Histc>(data, 10, 0.0, 1.0);+ ov::ResultVector results{std::make_shared<ov::op::v0::Result>(histcNode)};+ return std::make_shared<ov::Model>(results, ov::ParameterVector{data}, "HistcGraph");+}+std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v17::QSAPrepare> &node) {const auto raw_index_keys = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 8, 16});const auto past_block_index_keys = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 0, 16});