OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
# Copyright (C) 2018-2026 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import pytest
import torch
from pytorch_layer_test_class import PytorchLayerTest
class TestNarrow(PytorchLayerTest):
def _prepare_input(self):
return (self.random.randn(*self.input_shape),)
def create_model(self, dim, start, length):
class aten_narrow(torch.nn.Module):
def __init__(self, dim, start, length):
super().__init__()
self.dim = dim
self.start = start
self.length = length
def forward(self, input_tensor):
return torch.narrow(input_tensor, dim=self.dim, start=self.start, length=self.length)
return aten_narrow(dim, start, length), "aten::narrow"
@pytest.mark.parametrize("input_shape", [
[3, 3], [3, 4, 5]
])
@pytest.mark.parametrize("dim", [0, 1, -1])
@pytest.mark.parametrize("start", [0, 1])
@pytest.mark.parametrize("length", [1, 2])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
def test_narrow(self, input_shape, dim, start, length, ie_device, precision, ir_version):
self.input_shape = input_shape
self._test(*self.create_model(dim, start, length), ie_device, precision, ir_version)
@pytest.mark.parametrize("input_shape", [
[3, 3], [3, 4, 5]
])
@pytest.mark.parametrize("dim", [0, 1, -1])
@pytest.mark.parametrize("start,length", [
# A negative start counts from the end of the dimension. The windows reaching the end of
# the axis (start + length == 0) used to come out empty.
(-1, 1), (-2, 2), (-2, 1), (-3, 2), (-3, 3),
])
@pytest.mark.nightly
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
def test_narrow_negative_start(self, input_shape, dim, start, length, ie_device, precision, ir_version):
self.input_shape = input_shape
self._test(*self.create_model(dim, start, length), ie_device, precision, ir_version)