Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PT FE] add support of shift operations #29057

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/frontends/pytorch/src/op/bitwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "openvino/op/bitwise_not.hpp"
#include "openvino/op/bitwise_or.hpp"
#include "openvino/op/bitwise_xor.hpp"
#include "openvino/op/bitwise_left_shift.hpp"
#include "openvino/op/bitwise_right_shift.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix code style, sort includes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mvafin
thanks for your review, I've sorted them.

#include "openvino/op/convert_like.hpp"
#include "utils.hpp"

Expand Down Expand Up @@ -73,7 +75,39 @@ 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
} // namespace ov
} // namespace ov
6 changes: 6 additions & 0 deletions src/frontends/pytorch/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ OP_CONVERTER(translate_bitwise_and);
OP_CONVERTER(translate_bitwise_not);
OP_CONVERTER(translate_bitwise_or);
OP_CONVERTER(translate_bitwise_xor);
OP_CONVERTER(translate_bitwise_left_shift);
OP_CONVERTER(translate_bitwise_right_shift);
OP_CONVERTER(translate_bucketize);
OP_CONVERTER(translate_cat);
OP_CONVERTER(translate_cdist);
Expand Down Expand Up @@ -343,6 +345,10 @@ 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::bitwise_left_shift", op::translate_bitwise_left_shift},
{"aten::bitwise_right_shift", 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>},
Expand Down
108 changes: 108 additions & 0 deletions tests/layer_tests/pytorch_tests/test_shiftOperations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 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)
)

ref_net = None
return aten_bitwise_shift(), ref_net, ("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,
)
Loading