Skip to content

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

3.6 KB
Raw Blame
# Copyright (C) 2018-2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import pytest

from pytorch_layer_test_class import PytorchLayerTest


class TestUnsqueeze(PytorchLayerTest):
    def _prepare_input(self):
        return (self.random.randn(5, 10),)

    def create_model(self, inplace=False, dim=0):
        import torch

        class aten_unsqueeze(torch.nn.Module):
            def __init__(self, dim):
                super().__init__()
                self.op = torch.unsqueeze
                self.dim = dim

            def forward(self, x):
                return x, self.op(x, self.dim)

        class aten_unsqueeze_(torch.nn.Module):
            def __init__(self, dim):
                super().__init__()
                self.dim = dim

            def forward(self, x):
                x = x.clone()
                return x, x.unsqueeze_(self.dim)

        model_class, op = (aten_unsqueeze, "aten::unsqueeze") if not inplace else (aten_unsqueeze_, "aten::unsqueeze_")

        return model_class(dim), op

    @pytest.mark.parametrize("inplace", [False, True])
    @pytest.mark.parametrize("dim", [0, 1, -1])
    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_torch_export
    @pytest.mark.precommit_fx_backend
    def test_unsqueeze(self, inplace, dim, ie_device, precision, ir_version):
        self._test(*self.create_model(inplace, dim), ie_device, precision, ir_version)

class TestUnsqueezeCopy(PytorchLayerTest):
    def _prepare_input(self):
        return (self.random.randn(5, 10),)

    def create_model(self, dim=0):
        import torch

        class aten_unsqueeze_copy(torch.nn.Module):
            def __init__(self, dim):
                super().__init__()
                self.op = torch.unsqueeze_copy
                self.dim = dim

            def forward(self, x):
                return x, self.op(x, self.dim)

        model_class, op = (aten_unsqueeze_copy, "aten::unsqueeze_copy")

        return model_class(dim), op

    @pytest.mark.parametrize("dim", [0, 1, -1])
    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_fx_backend
    def test_unsqueeze_copy(self, dim, ie_device, precision, ir_version):
        self._test(*self.create_model(dim), ie_device, precision, ir_version)

class TestUnsqueezeWithComplex(PytorchLayerTest):
    def _prepare_input(self):
        return (self.random.randn(2, 3, 2),)

    def create_model(self, inplace=False, dim=0):
        import torch

        class aten_unsqueeze(torch.nn.Module):
            def __init__(self, dim):
                super().__init__()
                self.op = torch.unsqueeze
                self.dim = dim

            def forward(self, x):
                x = torch.view_as_complex(x)
                return torch.view_as_real(self.op(x, self.dim))

        class aten_unsqueeze_(torch.nn.Module):
            def __init__(self, dim):
                super().__init__()
                self.dim = dim

            def forward(self, x):
                x = torch.view_as_complex(x)
                return torch.view_as_real(x.unsqueeze_(self.dim))

        model_class, op = (aten_unsqueeze, "aten::unsqueeze") if not inplace else (aten_unsqueeze_, "aten::unsqueeze_")

        return model_class(dim), op

    @pytest.mark.parametrize("inplace", [False, True])
    @pytest.mark.parametrize("dim", [0, 1, -1, -2])
    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_torch_export
    @pytest.mark.precommit_fx_backend
    def test_unsqueeze(self, inplace, dim, ie_device, precision, ir_version):
        self._test(*self.create_model(inplace, dim), ie_device, precision, ir_version)