Skip to content

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

2.4 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 aten_kthvalue(torch.nn.Module):
    def __init__(self, k, dim, keepdim):
        super().__init__()
        self.k = k
        self.dim = dim
        self.keepdim = keepdim

    def forward(self, x):
        kwargs = {}
        if self.dim is not None:
            kwargs["dim"] = self.dim
        if self.keepdim is not None:
            kwargs["keepdim"] = self.keepdim
        return torch.kthvalue(x, self.k, **kwargs)


class TestKthValue(PytorchLayerTest):
    def _prepare_input(self, shape, dtype):
        size = np.prod(shape)
        data = np.arange(size, dtype=dtype)
        self.random.shuffle(data)
        data = data.reshape(shape)
        return (data,)

    @pytest.mark.parametrize("shape, k, dim", [
        ([2, 3], 2, 1),
        ([4, 5, 6], 3, 0),
        ([4, 5, 6], 4, -2),
        ([2, 3, 4], 3, None),
        ([10], 1, 0),
        ([3, 8, 4], 8, 1),
        ([3, 8, 4, 2, 2, 2], 8, 1),
    ])
    @pytest.mark.parametrize("keepdim", [False, True])
    @pytest.mark.parametrize("dtype", [np.float32, np.float64, np.int32, np.int64])
    @pytest.mark.nightly
    @pytest.mark.precommit
    def test_kthvalue(self, shape, k, dim, keepdim, dtype, ie_device, precision, ir_version):
        self._test(aten_kthvalue(k, dim, keepdim),
                   "aten::kthvalue",
                   ie_device, precision, ir_version,
                   kwargs_to_prepare_input={"shape": shape, "dtype": dtype},
                   trace_model=True,
                   )


class TestSymbolicMinKthValue(PytorchLayerTest):
    def _prepare_input(self, count):
        return (np.arange(count, dtype=np.float32) - 0.5,)

    @pytest.mark.parametrize("count", [2, 4, 7])
    @pytest.mark.nightly
    @pytest.mark.precommit_torch_export
    def test_symbolic_min(self, count, ie_device, precision, ir_version):
        if not self.use_torch_export():
            pytest.skip("Tests symbolic min in an exported graph")

        class Model(torch.nn.Module):
            def forward(self, x):
                selected = x[x > 0]
                k = torch.sym_min(3, selected.shape[0])
                return torch.kthvalue(selected, k)

        self._test(Model(), "aten::kthvalue", ie_device, precision, ir_version,
                   kwargs_to_prepare_input={"count": count})