Skip to content

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

[PT FE] support shift operations for pytorch frontend (#29142)

Mohamed Ashraf72c83573d538
diff --git a/src/frontends/pytorch/src/op/bitwise.cpp b/src/frontends/pytorch/src/op/bitwise.cpp
index 03ae5de900e..b23276d08fd 100644
--- a/src/frontends/pytorch/src/op/bitwise.cpp
+++ b/src/frontends/pytorch/src/op/bitwise.cpp
@@ -4,8 +4,10 @@
#include "openvino/frontend/pytorch/node_context.hpp"
#include "openvino/op/bitwise_and.hpp"
+#include "openvino/op/bitwise_left_shift.hpp"
#include "openvino/op/bitwise_not.hpp"
#include "openvino/op/bitwise_or.hpp"
+#include "openvino/op/bitwise_right_shift.hpp"
#include "openvino/op/bitwise_xor.hpp"
#include "openvino/op/convert_like.hpp"
#include "utils.hpp"
@@ -73,6 +75,37 @@ OutputVector translate_bitwise_xor(const NodeContext& context) {
return {xor_x};
};
+OutputVector translate_bitwise_left_shift(const NodeContext& context) {
+ num_inputs_check(context, 2, 3);
+ Output<Node> x;
+ Output<Node> y;
+ std::tie(x, y) = get_inputs_with_promoted_types(context, 0, 1);
+ auto lshift = context.mark_node(std::make_shared<ov::op::v15::BitwiseLeftShift>(x, y))->output(0);
+ if (!context.input_is_none(2)) {
+ auto out = context.get_input(2);
+ if (out.get_element_type().is_dynamic() || lshift.get_element_type() != out.get_element_type()) {
+ lshift = context.mark_node(std::make_shared<ov::op::v1::ConvertLike>(lshift, out));
+ }
+ context.mutate_input(2, lshift);
+ }
+ return {lshift};
+};
+
+OutputVector translate_bitwise_right_shift(const NodeContext& context) {
+ num_inputs_check(context, 2, 3);
+ Output<Node> x;
+ Output<Node> y;
+ std::tie(x, y) = get_inputs_with_promoted_types(context, 0, 1);
+ auto rshift = context.mark_node(std::make_shared<ov::op::v15::BitwiseRightShift>(x, y))->output(0);
+ if (!context.input_is_none(2)) {
+ auto out = context.get_input(2);
+ if (out.get_element_type().is_dynamic() || rshift.get_element_type() != out.get_element_type()) {
+ rshift = context.mark_node(std::make_shared<ov::op::v1::ConvertLike>(rshift, out));
+ }
+ context.mutate_input(2, rshift);
+ }
+ return {rshift};
+}
} // namespace op
} // namespace pytorch
} // namespace frontend
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index 040e58bc7a7..3ccada7289a 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -54,8 +54,10 @@ OP_CONVERTER(translate_bool);
OP_CONVERTER(translate_batch_norm);
OP_CONVERTER(translate_bernoulli);
OP_CONVERTER(translate_bitwise_and);
+OP_CONVERTER(translate_bitwise_left_shift);
OP_CONVERTER(translate_bitwise_not);
OP_CONVERTER(translate_bitwise_or);
+OP_CONVERTER(translate_bitwise_right_shift);
OP_CONVERTER(translate_bitwise_xor);
OP_CONVERTER(translate_bucketize);
OP_CONVERTER(translate_cat);
@@ -362,6 +364,8 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
return {
{"aten::__and__", op::translate_bitwise_and},
{"aten::__iand__", op::inplace_op<op::translate_bitwise_and>},
+ {"aten::__lshift__", op::translate_bitwise_left_shift},
+ {"aten::__rshift__", op::translate_bitwise_right_shift},
{"aten::__derive_index", op::translate_derive_index},
{"aten::__getitem__", op::translate_getitem},
{"aten::__not__", op::translate_1to1_match_1_inputs<opset10::LogicalNot>},
@@ -431,8 +435,10 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::batch_norm", op::translate_batch_norm},
{"aten::bernoulli", op::translate_bernoulli},
{"aten::bitwise_and", op::translate_bitwise_and},
+ {"aten::bitwise_left_shift", op::translate_bitwise_left_shift},
{"aten::bitwise_not", op::translate_bitwise_not},
{"aten::bitwise_or", op::translate_bitwise_or},
+ // {"aten::bitwise_right_shift", op::translate_bitwise_right_shift}, - temporarily disable
{"aten::bitwise_xor", op::translate_bitwise_xor},
{"aten::bmm", op::translate_1to1_match_2_inputs<opset10::MatMul>},
{"aten::Bool", op::translate_bool},
@@ -808,6 +814,8 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_fx() {
op::translate_fake_quantize_per_tensor_affine_fx},
{"aten._local_scalar_dense.default", op::skip_node},
{"aten._log_softmax.default", op::translate_log_softmax_fx},
+ {"aten.__lshift__.Tensor", op::translate_bitwise_left_shift},
+ {"aten.__rshift__.Tensor", op::translate_bitwise_right_shift},
{"aten._native_batch_norm_legit.default", op::translate_batch_norm_legit_fx},
{"aten._native_batch_norm_legit.no_stats", op::translate_batch_norm_legit_no_stats_fx},
{"aten._native_batch_norm_legit_functional.default", op::translate_batch_norm_legit_fx},
@@ -855,8 +863,10 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_fx() {
{"aten.baddbmm.default", op::translate_addmm_fx},
{"aten.bitwise_and.Scalar", op::translate_bitwise_and},
{"aten.bitwise_and.Tensor", op::translate_bitwise_and},
+ {"aten.bitwise_left_shift.Tensor", op::translate_bitwise_left_shift},
{"aten.bitwise_not.default", op::translate_bitwise_not},
{"aten.bitwise_or.Tensor", op::translate_bitwise_or},
+ // {"aten.bitwise_right_shift.Tensor", op::translate_bitwise_right_shift}, - temporarily disable
{"aten.bitwise_xor.Tensor", op::translate_bitwise_xor},
{"aten.bmm.default", op::translate_1to1_match_2_inputs_align_types<opset10::MatMul>},
{"aten.bucketize.Tensor", op::translate_bucketize},
diff --git a/tests/layer_tests/pytorch_tests/test_shift_operations.py b/tests/layer_tests/pytorch_tests/test_shift_operations.py
new file mode 100644
index 00000000000..acd29af0bdf
--- /dev/null
+++ b/tests/layer_tests/pytorch_tests/test_shift_operations.py
@@ -0,0 +1,107 @@
+# Copyright (C) 2018-2025 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import numpy as np
+import pytest
+import torch
+from pytorch_layer_test_class import PytorchLayerTest, skip_if_export
+
+
+class TestShiftOperators(PytorchLayerTest):
+ def _prepare_input(self, lhs_dtype, rhs_dtype, lhs_shape, rhs_shape):
+ choices = np.array([1, 2, 4, 8, 16, 32])
+ shifts = np.array([0, 1, 2, 3, 4, 5])
+
+ x = np.random.choice(choices, lhs_shape).astype(lhs_dtype)
+ y = np.random.choice(shifts, rhs_shape).astype(rhs_dtype)
+ return x, y
+
+ def create_model(self):
+ class aten_shift(torch.nn.Module):
+ def forward(self, lhs, rhs):
+ return lhs << rhs, lhs >> rhs
+
+ ref_net = None
+ return aten_shift(), ref_net, ("aten::__lshift__", "aten::__rshift__")
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.precommit_fx_backend
+ @pytest.mark.parametrize("lhs_dtype", ["int32", "int64"])
+ @pytest.mark.parametrize("rhs_dtype", ["int32", "int64"])
+ @pytest.mark.parametrize(
+ ("lhs_shape", "rhs_shape"),
+ [
+ ([2, 3], [2, 3]),
+ ([2, 3], []),
+ ([], [2, 3]),
+ ([], []),
+ ],
+ )
+ def test_shift_operators(self, lhs_dtype, rhs_dtype, lhs_shape, rhs_shape, ie_device, precision, ir_version):
+ self._test(
+ *self.create_model(),
+ ie_device,
+ precision,
+ ir_version,
+ kwargs_to_prepare_input={
+ "lhs_dtype": lhs_dtype,
+ "rhs_dtype": rhs_dtype,
+ "lhs_shape": lhs_shape,
+ "rhs_shape": rhs_shape,
+ },
+ trace_model=True,
+ freeze_model=False,
+ )
+
+
+class TestBitwiseShiftFunctions(PytorchLayerTest):
+ def _prepare_input(self, lhs_dtype, rhs_dtype, lhs_shape, rhs_shape):
+ choices = np.array([1, 2, 4, 8, 16, 32])
+ shifts = np.array([0, 1, 2, 3, 4, 5])
+
+ x = np.random.choice(choices, lhs_shape).astype(lhs_dtype)
+ y = np.random.choice(shifts, rhs_shape).astype(rhs_dtype)
+ return x, y
+
+ def create_model(self):
+ class aten_bitwise_shift(torch.nn.Module):
+ def forward(self, lhs, rhs):
+ return (
+ torch.bitwise_left_shift(lhs, rhs),
+ # torch.bitwise_right_shift(lhs, rhs) - temporarily disable
+ )
+
+ return aten_bitwise_shift(), None, ("aten::bitwise_left_shift",) # "aten::bitwise_right_shift")
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.precommit_fx_backend
+ @pytest.mark.parametrize("lhs_dtype", ["int32", "int64"])
+ @pytest.mark.parametrize("rhs_dtype", ["int32", "int64"])
+ @pytest.mark.parametrize(
+ ("lhs_shape", "rhs_shape"),
+ [
+ ([2, 3], [2, 3]),
+ ([2, 3], []),
+ ([], [2, 3]),
+ ([], []),
+ ],
+ )
+ def test_bitwise_shift_functions(self, lhs_dtype, rhs_dtype, lhs_shape, rhs_shape, ie_device, precision, ir_version):
+ self._test(
+ *self.create_model(),
+ ie_device,
+ precision,
+ ir_version,
+ kwargs_to_prepare_input={
+ "lhs_dtype": lhs_dtype,
+ "rhs_dtype": rhs_dtype,
+ "lhs_shape": lhs_shape,
+ "rhs_shape": rhs_shape,
+ },
+ trace_model=True,
+ freeze_model=False,
+ )
\ No newline at end of file