Skip to content

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

3.5 KB
Raw Blame
# 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 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 = self.random.choice(choices, lhs_shape).astype(lhs_dtype)
        y = self.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

        return aten_shift(), ("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,
            fx_kind=["aten.__lshift__", "aten.__rshift__"],
        )


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 = self.random.choice(choices, lhs_shape).astype(lhs_dtype)
        y = self.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(), ("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,
            fx_kind="aten.bitwise_left_shift",
        )