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
feat(pytorch): Add support for aten::bincount (#34485)
Lagmator2236ef3d68c9f3
diff --git a/src/frontends/common_translators/include/common_translators.hpp b/src/frontends/common_translators/include/common_translators.hppindex b19ca479ad0..c63190afc57 100644--- a/src/frontends/common_translators/include/common_translators.hpp+++ b/src/frontends/common_translators/include/common_translators.hpp@@ -27,6 +27,11 @@ COMMON_OP_CONVERTER(translate_unsqueeze);OutputVector translate_atan2_util(const NodeContext& context, const Output<Node>& lhs, const Output<Node>& rhs);OutputVector translate_erfc_util(const NodeContext& context, const Output<Node>& data);+OutputVector translate_bincount_common(const NodeContext& context,+ const Output<Node>& arr,+ const Output<Node>& size,+ const Output<Node>& weights = Output<Node>());+} // namespace common_translators} // namespace frontend} // namespace ovdiff --git a/src/frontends/common_translators/src/op/bincount.cpp b/src/frontends/common_translators/src/op/bincount.cppnew file mode 100644index 00000000000..2591835e9f6--- /dev/null+++ b/src/frontends/common_translators/src/op/bincount.cpp@@ -0,0 +1,58 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "common_translators.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/convert_like.hpp"+#include "openvino/op/equal.hpp"+#include "openvino/op/multiply.hpp"+#include "openvino/op/range.hpp"+#include "openvino/op/reduce_sum.hpp"+#include "openvino/op/reshape.hpp"+#include "openvino/op/unsqueeze.hpp"++namespace ov {+namespace frontend {+namespace common_translators {++using namespace ov::op;++OutputVector translate_bincount_common(const NodeContext& context,+ const Output<Node>& arr,+ const Output<Node>& size,+ const Output<Node>& weights) {+ auto start = context.mark_node(v0::Constant::create(element::i32, Shape{}, {0}));+ auto step = context.mark_node(v0::Constant::create(element::i32, Shape{}, {1}));+ auto range = context.mark_node(std::make_shared<v4::Range>(start, size, step, element::i32));++ auto flatten_shape = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {-1}));+ auto arr_flat = context.mark_node(std::make_shared<v1::Reshape>(arr, flatten_shape, false));++ auto axis_zero = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {0}));+ auto axis_one = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {1}));+ auto range_col = context.mark_node(std::make_shared<v0::Unsqueeze>(range, axis_one));+ auto arr_row = context.mark_node(std::make_shared<v0::Unsqueeze>(arr_flat, axis_zero));++ auto mask = context.mark_node(std::make_shared<v1::Equal>(range_col, arr_row));++ Output<Node> weighted;+ if (weights.get_node_shared_ptr()) {+ auto weights_flat = context.mark_node(std::make_shared<v1::Reshape>(weights, flatten_shape, false));+ auto weights_row = context.mark_node(std::make_shared<v0::Unsqueeze>(weights_flat, axis_zero));+ auto mask_cast = context.mark_node(std::make_shared<v1::ConvertLike>(mask, weights_row));+ weighted = context.mark_node(std::make_shared<v1::Multiply>(mask_cast, weights_row));+ } else {+ weighted = context.mark_node(std::make_shared<v0::Convert>(mask, element::i64));+ }++ auto reduce_axis = context.mark_node(v0::Constant::create(element::i32, Shape{}, {1}));+ auto result = context.mark_node(std::make_shared<v1::ReduceSum>(weighted, reduce_axis));++ return {result};+}++} // namespace common_translators+} // namespace frontend+} // namespace ovdiff --git a/src/frontends/pytorch/src/op/bincount.cpp b/src/frontends/pytorch/src/op/bincount.cppnew file mode 100644index 00000000000..865b7c9e937--- /dev/null+++ b/src/frontends/pytorch/src/op/bincount.cpp@@ -0,0 +1,54 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "common_translators.hpp"+#include "openvino/frontend/pytorch/node_context.hpp"+#include "openvino/op/add.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"+#include "openvino/op/maximum.hpp"+#include "openvino/op/reduce_max.hpp"+#include "utils.hpp"++namespace ov {+namespace frontend {+namespace pytorch {+namespace op {++using namespace ov::op;++OutputVector translate_bincount(const NodeContext& context) {+ num_inputs_check(context, 1, 3);+ auto input = context.mark_node(std::make_shared<v0::Convert>(context.get_input(0), element::i32));++ Output<Node> weights;+ if (!context.input_is_none(1)) {+ weights = context.get_input(1);+ auto weights_type = weights.get_element_type();+ // torch.bincount accumulates weights as float64, except float32 weights which keep their type+ if (weights_type != element::f32 && weights_type != element::f64) {+ weights = context.mark_node(std::make_shared<v0::Convert>(weights, element::f64));+ }+ }++ Output<Node> minlength;+ if (!context.input_is_none(2)) {+ minlength = context.mark_node(std::make_shared<v0::Convert>(context.get_input(2), element::i32));+ } else {+ minlength = context.mark_node(v0::Constant::create(element::i32, Shape{}, {0}));+ }++ auto reduce_axis = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {0}));+ auto max_val = context.mark_node(std::make_shared<v1::ReduceMax>(input, reduce_axis, false));+ auto one = context.mark_node(v0::Constant::create(element::i32, Shape{}, {1}));+ auto max_plus_one = context.mark_node(std::make_shared<v1::Add>(max_val, one));+ auto size = context.mark_node(std::make_shared<v1::Maximum>(max_plus_one, minlength));++ return common_translators::translate_bincount_common(context, input, size, weights);+}++} // namespace op+} // namespace pytorch+} // namespace frontend+} // namespace ovdiff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cppindex 36c5c122b2b..038eb783ba3 100644--- a/src/frontends/pytorch/src/op_table.cpp+++ b/src/frontends/pytorch/src/op_table.cpp@@ -55,6 +55,7 @@ OP_CONVERTER(translate_avg_pool3d);OP_CONVERTER(translate_bool);OP_CONVERTER(translate_batch_norm);OP_CONVERTER(translate_bernoulli);+OP_CONVERTER(translate_bincount);OP_CONVERTER(translate_bitwise_and);OP_CONVERTER(translate_bitwise_left_shift);OP_CONVERTER(translate_bitwise_not);@@ -458,6 +459,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {{"aten::baddbmm", op::translate_addmm},{"aten::batch_norm", op::translate_batch_norm},{"aten::bernoulli", op::translate_bernoulli},+ {"aten::bincount", op::translate_bincount},{"aten::bitwise_and", op::translate_bitwise_and},{"aten::bitwise_left_shift", op::translate_bitwise_left_shift},{"aten::bitwise_not", op::translate_bitwise_not},diff --git a/tests/layer_tests/pytorch_tests/test_bincount.py b/tests/layer_tests/pytorch_tests/test_bincount.pynew file mode 100644index 00000000000..dbd8940196b--- /dev/null+++ b/tests/layer_tests/pytorch_tests/test_bincount.py@@ -0,0 +1,69 @@+# Copyright (C) 2018-2026 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import numpy as np+import pytest+import torch++from pytorch_layer_test_class import PytorchLayerTest+++class TestBincount(PytorchLayerTest):++ def _prepare_input(self, input_data, i_dtype, w_dtype, with_weights):+ inputs = (np.array(input_data, dtype=i_dtype),)+ if with_weights:+ n = len(input_data)+ if np.issubdtype(w_dtype, np.integer):+ weights = np.array([i % 5 for i in range(n)], dtype=w_dtype)+ else:+ weights = np.array([(i % 5) * 1.5 for i in range(n)], dtype=w_dtype)+ inputs += (weights,)+ return inputs++ def create_model(self, with_weights, minlength):+ class BincountNoWeights(torch.nn.Module):+ def __init__(self, minlength):+ super().__init__()+ self.minlength = minlength++ def forward(self, x):+ return torch.bincount(x, minlength=self.minlength)++ class BincountWithWeights(torch.nn.Module):+ def __init__(self, minlength):+ super().__init__()+ self.minlength = minlength++ def forward(self, x, w):+ return torch.bincount(x, weights=w, minlength=self.minlength)++ model = BincountWithWeights(minlength) if with_weights else BincountNoWeights(minlength)+ return model, "aten::bincount"++ @pytest.mark.nightly+ @pytest.mark.precommit+ @pytest.mark.parametrize("with_weights", [True, False])+ @pytest.mark.parametrize("minlength", [0, 5, 10])+ @pytest.mark.parametrize("input_data,i_dtype,w_dtype", [+ ([0, 1, 1, 2, 2, 2, 3], np.int32, np.float32),+ ([0, 0, 0, 0], np.int64, np.float64),+ ([0, 1, 2, 3, 4], np.int32, np.int64),+ ([3, 3, 3, 3, 3], np.int64, np.int32),+ ])+ def test_bincount(self, with_weights, minlength, input_data, i_dtype, w_dtype,+ ie_device, precision, ir_version):+ self._test(+ *self.create_model(with_weights, minlength),+ ie_device,+ precision,+ ir_version,+ trace_model=True,+ dynamic_shapes=False,+ kwargs_to_prepare_input={+ "input_data": input_data,+ "i_dtype": i_dtype,+ "w_dtype": w_dtype,+ "with_weights": with_weights,+ },+ )