Skip to content

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

2.8 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 TestEinsumBatchMatMul(PytorchLayerTest):
    def _prepare_input(self):
        return (self.random.randn(5, 2, 3), self.random.randn(5, 3, 4),)

    def create_model(self):
        import torch

        class EinsumModelBatchMatmul(torch.nn.Module):
            def forward(self, x, y):
                eqn = "bij, bjk -> bik"
                return torch.einsum(eqn, x, y)


        return EinsumModelBatchMatmul(), "aten::einsum"

    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_torch_export
    def test_einsum_batch_matmul(self, ie_device, precision, ir_version):
        self._test(*self.create_model(), ie_device, precision, ir_version)


class TestEinsumBatchDiagonal(PytorchLayerTest):
    def _prepare_input(self):
        return (self.random.randn(3, 5, 5),)

    def create_model(self):
        import torch

        class EinsumModelBatchDiagonal(torch.nn.Module):
            def forward(self, x):
                eqn = "kii -> ki"
                return torch.einsum(eqn, x)


        return EinsumModelBatchDiagonal(), "aten::einsum"

    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_torch_export
    @pytest.mark.xfail(reason='OpenVINO CPU plugin does not support einsum diagonal')
    def test_einsum_batch_diagonal(self, ie_device, precision, ir_version):
        self._test(*self.create_model(), ie_device, precision, ir_version, dynamic_shapes=False)


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

    def create_model(self):
        import torch

        class EinsumModelInnerProd(torch.nn.Module):
            def forward(self, x, y):
                eqn = "i,i"
                return torch.einsum(eqn, x, y)


        return EinsumModelInnerProd(), "aten::einsum"

    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_torch_export
    def test_einsum_inner_prod(self, ie_device, precision, ir_version):
        self._test(*self.create_model(), ie_device, precision, ir_version)


class TestEinsumTranspose(PytorchLayerTest):
    def _prepare_input(self):
        return (self.random.randn(3, 5),)

    def create_model(self):
        import torch

        class EinsumModelTranspose(torch.nn.Module):
            def forward(self, x):
                eqn = "ij->ji"
                return torch.einsum(eqn, x)


        return EinsumModelTranspose(), "aten::einsum"

    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_torch_export
    def test_einsum_transpose(self, ie_device, precision, ir_version):
        self._test(*self.create_model(), ie_device, precision, ir_version)