Skip to content

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

1.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 TestGather(PytorchLayerTest):
    def _prepare_input(self, m, n, max_val, out=False):
        import numpy as np
        index = self.random.randint(0, max_val, (m, n), dtype=np.int64)
        inp = self.random.randn(m, n)
        if out:
            axis = int(max_val == n)
            out = np.zeros_like(inp)
            return (inp, index, out)
        return (inp, index)

    def create_model(self, axis, out):
        import torch

        class aten_gather(torch.nn.Module):
            def __init__(self, axis, out=False):
                super().__init__()
                self.axis = axis
                if out:
                    self.forward = self.forward_out

            def forward(self, x, index):
                return torch.gather(x, self.axis, index)

            def forward_out(self, x, index, out):
                return torch.gather(x, self.axis, index, out=out)


        return aten_gather(axis, out), "aten::gather"

    @pytest.mark.nightly
    @pytest.mark.precommit
    @pytest.mark.precommit_torch_export
    @pytest.mark.parametrize("m", [2, 10, 100])
    @pytest.mark.parametrize("n", [2, 10, 100])
    @pytest.mark.parametrize("axis", [0, 1])
    @pytest.mark.parametrize("out", [True, False])
    def test_gather(self, m, n, axis, out, ie_device, precision, ir_version):
        self._test(*self.create_model(axis, out), ie_device, precision, ir_version, kwargs_to_prepare_input={
            "m": m, "n": n, "max_val": m if axis == 0 else n, "out": out
        })