Skip to content

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

[TESTS] Fix all warnings in pytorch layer tests (#34627)

Maxim Vafin0d766c455aa1
diff --git a/tests/layer_tests/pytest.ini b/tests/layer_tests/pytest.ini
index 750c54760ac..83822f17dd7 100644
--- a/tests/layer_tests/pytest.ini
+++ b/tests/layer_tests/pytest.ini
@@ -7,3 +7,10 @@ markers =
precommit_torch_export
precommit_jax_fe
timeout
+
+filterwarnings =
+ # torch.jit.script/trace/trace_method are deprecated in favour of torch.compile/torch.export.
+ # These warnings fire on every scripted/traced test and are not actionable at the test level.
+ ignore:`torch.jit.script` is deprecated:DeprecationWarning
+ ignore:`torch.jit.trace` is deprecated:DeprecationWarning
+ ignore:`torch.jit.trace_method` is deprecated:DeprecationWarning
diff --git a/tests/layer_tests/pytorch_tests/test_add.py b/tests/layer_tests/pytorch_tests/test_add.py
index bbaff3451df..9b1112348ce 100644
--- a/tests/layer_tests/pytorch_tests/test_add.py
+++ b/tests/layer_tests/pytorch_tests/test_add.py
@@ -63,6 +63,7 @@ class TestAddTypes(PytorchLayerTest):
super().__init__()
self.lhs_type = lhs_type
self.rhs_type = rhs_type
+ self.register_buffer('scalar_one', torch.tensor(1))
if len(lhs_shape) == 0:
self.forward = self.forward1
elif len(rhs_shape) == 0:
@@ -71,10 +72,10 @@ class TestAddTypes(PytorchLayerTest):
self.forward = self.forward3
def forward1(self, rhs):
- return torch.add(torch.tensor(1).to(self.lhs_type), rhs.to(self.rhs_type), alpha=2)
+ return torch.add(self.scalar_one.to(self.lhs_type), rhs.to(self.rhs_type), alpha=2)
def forward2(self, lhs):
- return torch.add(lhs.to(self.lhs_type), torch.tensor(1).to(self.rhs_type), alpha=2)
+ return torch.add(lhs.to(self.lhs_type), self.scalar_one.to(self.rhs_type), alpha=2)
def forward3(self, lhs, rhs):
return torch.add(lhs.to(self.lhs_type), rhs.to(self.rhs_type), alpha=2)
diff --git a/tests/layer_tests/pytorch_tests/test_all.py b/tests/layer_tests/pytorch_tests/test_all.py
index 82fdcb08999..2878690e7e7 100644
--- a/tests/layer_tests/pytorch_tests/test_all.py
+++ b/tests/layer_tests/pytorch_tests/test_all.py
@@ -58,10 +58,15 @@ class aten_all_out(torch.nn.Module):
), out
class TestAll(PytorchLayerTest):
- def _prepare_input(self, out=False):
+ def _prepare_input(self, out=False, dim=None, keepdim=None):
if not out:
return (self.input_tensor,)
- return (self.input_tensor, np.zeros_like(self.input_tensor, dtype=bool if self.input_tensor.dtype != np.uint8 else np.uint8))
+ out_dtype = bool if self.input_tensor.dtype != np.uint8 else np.uint8
+ if dim is not None:
+ input_shape = list(self.input_tensor.shape)
+ out_shape = input_shape[:dim] + ([1] if keepdim else []) + input_shape[dim + 1:]
+ return (self.input_tensor, np.zeros(out_shape, dtype=out_dtype))
+ return (self.input_tensor, np.array(False, dtype=out_dtype))
@pytest.mark.parametrize("input_shape, d_type", [
(np.eye(5,5), np.int64),
@@ -119,4 +124,4 @@ class TestAll(PytorchLayerTest):
self.input_tensor = input_shape
for dim in range(len(self.input_tensor.shape)):
self._test(aten_all(dim, keepdim) if not out else aten_all_out(dim, keepdim), "aten::all",
- ie_device, precision, ir_version, trace_model=True, freeze_model=False, kwargs_to_prepare_input={"out": out})
+ ie_device, precision, ir_version, trace_model=True, freeze_model=False, kwargs_to_prepare_input={"out": out, "dim": dim, "keepdim": keepdim})
diff --git a/tests/layer_tests/pytorch_tests/test_any.py b/tests/layer_tests/pytorch_tests/test_any.py
index d80a55e8e70..0d3f3e3f934 100644
--- a/tests/layer_tests/pytorch_tests/test_any.py
+++ b/tests/layer_tests/pytorch_tests/test_any.py
@@ -58,10 +58,15 @@ class aten_any_out(torch.nn.Module):
), out
class TestAny(PytorchLayerTest):
- def _prepare_input(self, out=False):
+ def _prepare_input(self, out=False, dim=None, keepdim=None):
if not out:
return (self.input_tensor,)
- return (self.input_tensor, np.zeros_like(self.input_tensor, dtype=bool if self.input_tensor.dtype != np.uint8 else np.uint8))
+ out_dtype = bool if self.input_tensor.dtype != np.uint8 else np.uint8
+ if dim is not None:
+ input_shape = list(self.input_tensor.shape)
+ out_shape = input_shape[:dim] + ([1] if keepdim else []) + input_shape[dim + 1:]
+ return (self.input_tensor, np.zeros(out_shape, dtype=out_dtype))
+ return (self.input_tensor, np.array(False, dtype=out_dtype))
@pytest.mark.parametrize("input_shape, d_type", [
(np.eye(5,5), np.int64),
@@ -119,4 +124,4 @@ class TestAny(PytorchLayerTest):
self.input_tensor = input_shape
for dim in range(len(self.input_tensor.shape)):
self._test(aten_any(dim, keepdim) if not out else aten_any_out(dim, keepdim), "aten::any",
- ie_device, precision, ir_version, trace_model=True, freeze_model=False, kwargs_to_prepare_input={"out": out})
+ ie_device, precision, ir_version, trace_model=True, freeze_model=False, kwargs_to_prepare_input={"out": out, "dim": dim, "keepdim": keepdim})
diff --git a/tests/layer_tests/pytorch_tests/test_arange.py b/tests/layer_tests/pytorch_tests/test_arange.py
index eefb4c32817..06dc323b697 100644
--- a/tests/layer_tests/pytorch_tests/test_arange.py
+++ b/tests/layer_tests/pytorch_tests/test_arange.py
@@ -196,16 +196,13 @@ class TestArange(PytorchLayerTest):
class TestArangeLegacy(PytorchLayerTest):
def _prepare_input(self, end, start=None, step=None, dtype="int64", ref_dtype=None):
- import numpy as np
- if start is None and step is None:
- return (np.array(end).astype(dtype),) if not ref_dtype else (np.array(end).astype(dtype), np.zeros(1).astype(ref_dtype))
- if step is None:
- return (np.array(start).astype(dtype), np.array(end).astype(dtype)) if not ref_dtype else (np.array(start).astype(dtype), np.array(end).astype(dtype), np.zeros(1).astype(ref_dtype))
- return (np.array(start).astype(dtype), np.array(end).astype(dtype), np.array(step).astype(dtype)) if not ref_dtype else (np.array(start).astype(dtype), np.array(end).astype(dtype), np.array(step).astype(dtype), np.zeros(1).astype(ref_dtype))
+ scalars = [x for x in [start, end, step] if x is not None]
+ inputs = tuple(np.array(v).astype(dtype) for v in scalars)
+ if ref_dtype:
+ inputs += (np.zeros(1).astype(ref_dtype),)
+ return inputs
def create_model(self, dtype=None, num_inputs=1, use_out=False, ref_dtype=False):
- import torch
-
dtype_map = {
"float32": torch.float32,
"float64": torch.float64,
@@ -245,7 +242,7 @@ class TestArangeLegacy(PytorchLayerTest):
self.dtype = dtype
def forward(self, x):
- return torch.arange(x, out=torch.zeros(1, dtype=self.dtype))
+ return torch.arange(x, out=torch.zeros(int(x), dtype=self.dtype))
class aten_arange_start_end_out(torch.nn.Module):
def __init__(self, out) -> None:
diff --git a/tests/layer_tests/pytorch_tests/test_complex.py b/tests/layer_tests/pytorch_tests/test_complex.py
index 86218132742..c3759d97b2f 100644
--- a/tests/layer_tests/pytorch_tests/test_complex.py
+++ b/tests/layer_tests/pytorch_tests/test_complex.py
@@ -18,16 +18,17 @@ class TestComplex(PytorchLayerTest):
self.dtype = dtype
x = torch.tensor([1.0 + 2.0j, 3.0 + 4.0j], dtype=dtype)
self.complex_attr = torch.nn.Parameter(x)
+ self.register_buffer(
+ 'complex_const',
+ torch.tensor([5.0 + 6.0j, 7.0 + 8.0j], dtype=dtype),
+ )
def forward(self, x):
- complex_const = torch.tensor(
- [5.0 + 6.0j, 7.0 + 8.0j], dtype=self.dtype)
-
real_attr = self.complex_attr.real
imag_attr = self.complex_attr.imag
- real_const = complex_const.real
- imag_const = complex_const.imag
+ real_const = self.complex_const.real
+ imag_const = self.complex_const.imag
real_result = x + real_attr + real_const
imag_result = imag_attr + imag_const
@@ -43,6 +44,7 @@ class TestComplex(PytorchLayerTest):
torch.complex32,
torch.complex64,
torch.complex128])
+ @pytest.mark.filterwarnings("ignore:ComplexHalf support is experimental:UserWarning")
def test_complex(self, dtype, ie_device, precision, ir_version):
self._test(*self.create_model(dtype), ie_device,
precision, ir_version, trace_model=True)
diff --git a/tests/layer_tests/pytorch_tests/test_cross.py b/tests/layer_tests/pytorch_tests/test_cross.py
index 776470ed080..9b82a806561 100644
--- a/tests/layer_tests/pytorch_tests/test_cross.py
+++ b/tests/layer_tests/pytorch_tests/test_cross.py
@@ -5,9 +5,15 @@
# SPDX-License-Identifier: Apache-2.0
import pytest
+import torch
+from packaging import version
from pytorch_layer_test_class import PytorchLayerTest
+# torch.cross without the dim argument is deprecated in PyTorch 2.9.
+# torch.linalg.cross is already tested by TestLinalgCross.
+_CROSS_NO_DIM_DEPRECATED = version.parse(torch.__version__) >= version.parse("2.9.0")
+
class TestLinalgCross(PytorchLayerTest):
def _prepare_input(self, x_shape, y_shape, out, dtype):
@@ -119,6 +125,9 @@ class TestCross(PytorchLayerTest):
@pytest.mark.parametrize("out", [True, False])
@pytest.mark.parametrize('dtype', ['float32', 'float64'])
def test_linalg_cross(self, x_shape, y_shape, dim, out, dtype, ie_device, precision, ir_version):
+ if dim is None and _CROSS_NO_DIM_DEPRECATED:
+ pytest.skip("torch.cross without dim is deprecated in this PyTorch version; "
+ "torch.linalg.cross is already covered by TestLinalgCross")
self._test(*self.create_model(dim, out, x_shape), ie_device, precision, ir_version,
use_convert_model=True,
kwargs_to_prepare_input={"x_shape": x_shape,
diff --git a/tests/layer_tests/pytorch_tests/test_deformable_convolution.py b/tests/layer_tests/pytorch_tests/test_deformable_convolution.py
index ff94924694c..c609423fcb3 100644
--- a/tests/layer_tests/pytorch_tests/test_deformable_convolution.py
+++ b/tests/layer_tests/pytorch_tests/test_deformable_convolution.py
@@ -170,6 +170,9 @@ class TestDeformableConvolution(PytorchLayerTest):
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
+ # TracerWarning fires inside torchvision.ops.deform_conv (n_offset_grps check)
+ # which is not actionable at the test level.
+ @pytest.mark.filterwarnings("ignore:Converting a tensor to a Python boolean:torch.jit.TracerWarning")
def test_deformable_convolution2d(self, params, bias, mask, ie_device, precision, ir_version):
self._test(*self.create_model(**params, bias=bias, mask=mask),
ie_device, precision, ir_version, trace_model=True,
diff --git a/tests/layer_tests/pytorch_tests/test_divmod.py b/tests/layer_tests/pytorch_tests/test_divmod.py
index ac137499809..4c32d540de8 100644
--- a/tests/layer_tests/pytorch_tests/test_divmod.py
+++ b/tests/layer_tests/pytorch_tests/test_divmod.py
@@ -31,6 +31,9 @@ class TestBuiltinDivmod():
@pytest.mark.nightly
@pytest.mark.precommit
+ # TracerWarning fires from OV's own patch_functions.py which patches divmod() to
+ # work during TorchScript tracing; not actionable at the test level.
+ @pytest.mark.filterwarnings("ignore:Converting a tensor to a Python number:torch.jit.TracerWarning")
def test_divmod_on_assert_path(self, ie_device, precision):
fw_model = self.divmod_on_assert_path()
inputs = SeededRandom(42).torch_randn(2, 3, 28)
@@ -51,6 +54,12 @@ class TestBuiltinDivmod():
@pytest.mark.parametrize("x_shape", [[1], [2], [3], [4], [5], [6]])
@pytest.mark.nightly
@pytest.mark.precommit
+ # TracerWarning fires from OV's own patch_functions.py which patches divmod() to
+ # work during TorchScript tracing; not actionable at the test level.
+ @pytest.mark.filterwarnings("ignore:Converting a tensor to a Python number:torch.jit.TracerWarning")
+ # The divmod result is a Python scalar; torch.tensor(scalar) inside forward
+ # is traced as a constant, which is the intended and correct behaviour here.
+ @pytest.mark.filterwarnings("ignore:torch.tensor results are registered as constants:torch.jit.TracerWarning")
def test_divmod_on_compute_path(self, ie_device, precision, x_shape):
fw_model = self.divmod_on_compute_path()
x = SeededRandom(42).torch_randn(*x_shape)
diff --git a/tests/layer_tests/pytorch_tests/test_embedding_bag.py b/tests/layer_tests/pytorch_tests/test_embedding_bag.py
index c5028788200..204fea97cc3 100644
--- a/tests/layer_tests/pytorch_tests/test_embedding_bag.py
+++ b/tests/layer_tests/pytorch_tests/test_embedding_bag.py
@@ -55,6 +55,9 @@ class TestEmbeddingBag1dOffsets(PytorchLayerTest):
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
+ # TracerWarning comes from inside torch.nn.functional.embedding_bag (size check on
+ # per_sample_weights) which is not actionable at the test level.
+ @pytest.mark.filterwarnings("ignore:Converting a tensor to a Python boolean:torch.jit.TracerWarning")
@pytest.mark.parametrize("indicies_dtype", ["int", "int32"])
@pytest.mark.parametrize(
"mode, per_sample_weights", [("mean", False), ("sum", False), ("sum", True)]
@@ -132,6 +135,9 @@ class TestEmbeddingBag2d(PytorchLayerTest):
condition=platform.system() == "Darwin" and platform.machine() == "arm64",
reason="Ticket - 122715",
)
+ # TracerWarning comes from inside torch.nn.functional.embedding_bag (size check on
+ # per_sample_weights) which is not actionable at the test level.
+ @pytest.mark.filterwarnings("ignore:Converting a tensor to a Python boolean:torch.jit.TracerWarning")
def test_embedding_bag(
self,
ie_device,
@@ -212,6 +218,9 @@ class TestEmbeddingBagPretrained(PytorchLayerTest):
condition=platform.system() == "Darwin" and platform.machine() == "arm64",
reason="Ticket - 122715",
)
+ # TracerWarning comes from inside torch.nn.functional.embedding_bag (size check on
+ # per_sample_weights) which is not actionable at the test level.
+ @pytest.mark.filterwarnings("ignore:Converting a tensor to a Python boolean:torch.jit.TracerWarning")
def test_embedding_bag(
self,
ie_device,
diff --git a/tests/layer_tests/pytorch_tests/test_fft.py b/tests/layer_tests/pytorch_tests/test_fft.py
index 8ea00872af1..a3216729a09 100644
--- a/tests/layer_tests/pytorch_tests/test_fft.py
+++ b/tests/layer_tests/pytorch_tests/test_fft.py
@@ -48,16 +48,23 @@ class TestRFFTN(PytorchLayerTest):
class aten_fft(torch.nn.Module):
- def __init__(self, op, n, dim, norm):
+ def __init__(self, op, n, dim, norm, in_complex=False):
super().__init__()
self.n = n
self.dim = dim
self.norm = norm
self.op = op
+ if in_complex:
+ self.forward = self.forward_complex
def forward(self, x):
- if x.shape[-1] == 2:
- x = torch.view_as_complex(x)
+ res = self.op(x, self.n, dim=self.dim, norm=self.norm)
+ if res.dtype.is_complex:
+ return torch.view_as_real(res)
+ return res
+
+ def forward_complex(self, x):
+ x = torch.view_as_complex(x)
res = self.op(x, self.n, dim=self.dim, norm=self.norm)
if res.dtype.is_complex:
return torch.view_as_real(res)
@@ -90,7 +97,7 @@ class TestFFT(PytorchLayerTest):
self.input_shape = input_shape + [2]
else:
self.input_shape = input_shape
- m = aten_fft(op, n, dim, norm)
+ m = aten_fft(op, n, dim, norm, in_complex)
self._test(m, aten_name, ie_device,
precision, ir_version, trace_model=True, dynamic_shapes=False, custom_eps=1e-3)
@@ -114,7 +121,7 @@ class TestFFT(PytorchLayerTest):
self.input_shape = input_shape + [2]
else:
self.input_shape = input_shape
- m = aten_fft(op, s, dim, norm)
+ m = aten_fft(op, s, dim, norm, in_complex)
self._test(m, aten_name, ie_device,
precision, ir_version, trace_model=True, dynamic_shapes=False, custom_eps=1e-3)
@@ -152,6 +159,6 @@ class TestFFT(PytorchLayerTest):
self.input_shape = input_shape + (2,)
else:
self.input_shape = input_shape
- m = aten_fft(op, s, dim, norm)
+ m = aten_fft(op, s, dim, norm, in_complex)
self._test(m, aten_name, ie_device,
precision, ir_version, trace_model=True, dynamic_shapes=False, custom_eps=1e-3)
diff --git a/tests/layer_tests/pytorch_tests/test_full.py b/tests/layer_tests/pytorch_tests/test_full.py
index 3371076e5a2..04dc5d1198e 100644
--- a/tests/layer_tests/pytorch_tests/test_full.py
+++ b/tests/layer_tests/pytorch_tests/test_full.py
@@ -58,7 +58,7 @@ class TestFull(PytorchLayerTest):
self.dtype = dtype
def forward(self, x: float):
- return torch.full(self.shape, x, out=torch.tensor(1, dtype=self.dtype))
+ return torch.full(self.shape, x, out=torch.zeros(self.shape, dtype=self.dtype))
class aten_full_out_with_names(torch.nn.Module):
def __init__(self, shape, dtype):
@@ -67,7 +67,7 @@ class TestFull(PytorchLayerTest):
self.dtype = dtype
def forward(self, x: float):
- return torch.full(self.shape, x, out=torch.tensor(1, dtype=self.dtype), names=None)
+ return torch.full(self.shape, x, out=torch.zeros(self.shape, dtype=self.dtype), names=None)
model = aten_full(shape)
if use_dtype or use_out:
@@ -262,7 +262,7 @@ class TestFullLike(PytorchLayerTest):
self.dtype = dtype
def forward(self, input_t: torch.Tensor, x: float):
- return torch.full_like(input_t, x, out=torch.tensor(1, dtype=self.dtype))
+ return torch.full_like(input_t, x, out=torch.zeros_like(input_t, dtype=self.dtype))
model = aten_full_like()
@@ -431,7 +431,7 @@ class TestZerosAndOnes(PytorchLayerTest):
def forward(self, x):
shape = x.shape
- return self.op(shape, out=torch.tensor(0, dtype=self.dtype))
+ return self.op(shape, out=torch.zeros_like(x, dtype=self.dtype))
class aten_op_out_with_names(torch.nn.Module):
def __init__(self, op, dtype):
@@ -441,7 +441,7 @@ class TestZerosAndOnes(PytorchLayerTest):
def forward(self, x):
shape = x.shape
- return self.op(shape, out=torch.tensor(0, dtype=self.dtype), names=None)
+ return self.op(shape, out=torch.zeros_like(x, dtype=self.dtype), names=None)
class aten_op_like_out(torch.nn.Module):
def __init__(self, op, dtype):
@@ -450,7 +450,7 @@ class TestZerosAndOnes(PytorchLayerTest):
self.dtype = dtype
def forward(self, x):
- return self.op(x, out=torch.tensor(0, dtype=self.dtype))
+ return self.op(x, out=torch.zeros_like(x, dtype=self.dtype))
like = op_type.endswith('_like')
op = ops[op_type]
diff --git a/tests/layer_tests/pytorch_tests/test_gather.py b/tests/layer_tests/pytorch_tests/test_gather.py
index fe7c590941f..8057236466b 100644
--- a/tests/layer_tests/pytorch_tests/test_gather.py
+++ b/tests/layer_tests/pytorch_tests/test_gather.py
@@ -13,7 +13,7 @@ class TestGather(PytorchLayerTest):
inp = self.random.randn(m, n)
if out:
axis = int(max_val == n)
- out = np.zeros_like(np.take(inp, index, axis))
+ out = np.zeros_like(inp)
return (inp, index, out)
return (inp, index)
diff --git a/tests/layer_tests/pytorch_tests/test_gcd.py b/tests/layer_tests/pytorch_tests/test_gcd.py
index 6aea028f55c..2cbd74ff095 100644
--- a/tests/layer_tests/pytorch_tests/test_gcd.py
+++ b/tests/layer_tests/pytorch_tests/test_gcd.py
@@ -29,7 +29,7 @@ class TestGcd(PytorchLayerTest):
super().__init__()
def forward(self, int_a: int, int_b: int):
- return torch.tensor(torch.gcd(int_a, int_b))
+ return torch.gcd(int_a, int_b)
return aten_gcd_int(), "aten::gcd"
diff --git a/tests/layer_tests/pytorch_tests/test_istft.py b/tests/layer_tests/pytorch_tests/test_istft.py
index 58757da656c..88bca178fc5 100644
--- a/tests/layer_tests/pytorch_tests/test_istft.py
+++ b/tests/layer_tests/pytorch_tests/test_istft.py
@@ -8,7 +8,7 @@ from pytorch_layer_test_class import PytorchLayerTest
class TestISTFT(PytorchLayerTest):
- def _prepare_input(self, n_fft, hop_length, win_length, center, normalized, signal_shape, signal_length = None, rand_data=False, out_dtype="float32"):
+ def _prepare_input(self, n_fft, hop_length, win_length, center, normalized, signal_shape, rand_data=False, out_dtype="float32"):
import numpy as np
if rand_data:
@@ -29,30 +29,27 @@ class TestISTFT(PytorchLayerTest):
if center and hop_length and hop_length != n_fft:
window = np.hanning(window_size)
- else: # If 'center' is false, the window can't contain zeros at the beginning
+ else:
+ # If center=False the window can't contain zeros at the beginning.
window = np.hamming(window_size)
window = window.astype(out_dtype)
window_tensor = torch.from_numpy(window)
stft_out = torch.stft(
- signal,
- n_fft,
- hop_length=hop_length,
- win_length=win_length,
- window=window_tensor,
- center=center,
- pad_mode="reflect",
- normalized=normalized,
- onesided=True,
- return_complex=True,
- )
-
- if (signal_length is not None):
- signal_length_input = np.array(signal_length)
- return (torch.view_as_real(stft_out).numpy().astype(out_dtype), window, signal_length_input)
- else:
- return (torch.view_as_real(stft_out).numpy().astype(out_dtype), window)
+ signal,
+ n_fft,
+ hop_length=hop_length,
+ win_length=win_length,
+ window=window_tensor,
+ center=center,
+ pad_mode="reflect",
+ normalized=normalized,
+ onesided=True,
+ return_complex=True,
+ )
+
+ return (torch.view_as_real(stft_out).numpy().astype(out_dtype), window)
def create_model(self, n_fft, hop_length, win_length, normalized, center):
@@ -78,26 +75,26 @@ class TestISTFT(PytorchLayerTest):
normalized=self.normalized,
onesided=True,
return_complex=False,
- length = None
)
return aten_istft(n_fft, hop_length, win_length, normalized, center), "aten::istft"
- def create_model_with_sig_len(self, n_fft, hop_length, win_length, normalized, center):
+ def create_model_with_sig_len(self, n_fft, hop_length, win_length, normalized, center, signal_length):
class aten_istft(torch.nn.Module):
- def __init__(self, n_fft, hop_length, win_length, normalized, center):
+ def __init__(self, n_fft, hop_length, win_length, normalized, center, signal_length):
super().__init__()
self.n_fft = n_fft
self.hop_length = hop_length
self.win_length = win_length
self.normalized = normalized
self.center = center
+ self.signal_length = signal_length
- def forward(self, x, window, sig_length):
+ def forward(self, x, window):
return torch.istft(
torch.view_as_complex(x),
self.n_fft,
@@ -108,21 +105,20 @@ class TestISTFT(PytorchLayerTest):
normalized=self.normalized,
onesided=True,
return_complex=False,
- length = sig_length.item()
+ length=self.signal_length
)
- return aten_istft(n_fft, hop_length, win_length, normalized, center), "aten::istft"
+ return aten_istft(n_fft, hop_length, win_length, normalized, center, signal_length), "aten::istft"
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize(("trace_model"), [False, True])
+ @pytest.mark.parametrize("trace_model", [False, True])
@pytest.mark.parametrize(("signal_shape", "n_fft", "hop_length", "window_size"), [
[(1, 48), 16, 16, 16],
[(1, 48), 16, 8, 16],
[(1, 48), 16, 4, 16],
[(1, 48), 16, 1, 16],
- [(1, 48), 16, 4, 16],
[(2, 48), 16, 4, 16],
[(3, 48), 16, 4, 16],
[(4, 48), 16, 4, 16],
@@ -138,8 +134,8 @@ class TestISTFT(PytorchLayerTest):
[(1, 48), 16, 8, None],
[(1, 48), 16, None, None],
])
- @pytest.mark.parametrize(("normalized"), [True, False])
- @pytest.mark.parametrize(("center"), [True, False])
+ @pytest.mark.parametrize("normalized", [True, False])
+ @pytest.mark.parametrize("center", [True, False])
def test_istft(self, n_fft, hop_length, window_size, signal_shape, normalized, center, ie_device, precision, ir_version, trace_model):
if ie_device == "GPU":
pytest.xfail(reason="ISTFT op is not supported on GPU yet")
@@ -149,25 +145,26 @@ class TestISTFT(PytorchLayerTest):
reason="torch istft doesn't allow for zeros padding in window, when `center` is false ")
self._test(*self.create_model(n_fft, hop_length, window_size, normalized, center), ie_device, precision,
- ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length" : hop_length, "center": center, "normalized": normalized}, trace_model=trace_model)
+ ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length": hop_length, "center": center, "normalized": normalized}, trace_model=trace_model)
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize(("trace_model"), [False, True])
+ @pytest.mark.parametrize("trace_model", [False, True])
@pytest.mark.parametrize(("signal_shape", "n_fft", "hop_length", "window_size", "signal_length"), [
[(3, 48), 16, 8, 16, 48],
[(3, 48), 16, 8, 16, 32],
[(3, 48), 16, 8, 16, 55],
])
- @pytest.mark.parametrize(("normalized"), [True, False])
- @pytest.mark.parametrize(("center"), [True, False])
+ @pytest.mark.parametrize("normalized", [True, False])
+ @pytest.mark.parametrize("center", [True, False])
+ @pytest.mark.filterwarnings("ignore:The length of signal is shorter than the length parameter:UserWarning")
def test_istft_with_sig_len(self, n_fft, hop_length, window_size, signal_shape, normalized, center, signal_length, ie_device, precision, ir_version, trace_model):
if ie_device == "GPU":
pytest.xfail(reason="ISTFT op is not supported on GPU yet")
- self._test(*self.create_model_with_sig_len(n_fft, hop_length, window_size, normalized, center), ie_device, precision,
- ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length" : hop_length, "center": center, "normalized": normalized, "signal_length": signal_length}, trace_model=trace_model)
+ self._test(*self.create_model_with_sig_len(n_fft, hop_length, window_size, normalized, center, signal_length), ie_device, precision,
+ ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length": hop_length, "center": center, "normalized": normalized}, trace_model=trace_model)
class TestISTFTDefaultParams(PytorchLayerTest):
@@ -191,12 +188,9 @@ class TestISTFTDefaultParams(PytorchLayerTest):
if normalized is not None:
stft_kwargs['normalized'] = normalized
- stft_out = torch.stft(
- signal,
- **stft_kwargs
- )
+ stft_out = torch.stft(signal, **stft_kwargs)
- return (torch.view_as_real(stft_out).numpy().astype(out_dtype))
+ return torch.view_as_real(stft_out).numpy().astype(out_dtype)
def create_model(self, n_fft, hop_length, win_length, normalized, center):
@@ -234,17 +228,19 @@ class TestISTFTDefaultParams(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize(("trace_model"), [True])
+ @pytest.mark.parametrize("trace_model", [True])
@pytest.mark.parametrize(("signal_shape", "n_fft", "hop_length", "window_size"), [
[(1, 48), 16, None, 16],
[(1, 48), 16, 8, None],
[(1, 48), 16, None, None],
])
- @pytest.mark.parametrize(("normalized"), [True, None])
- @pytest.mark.parametrize(("center"), [False, None])
+ @pytest.mark.parametrize("normalized", [True, None])
+ @pytest.mark.parametrize("center", [False, None])
+ # No window tensor is passed intentionally; torch.istft emits a UserWarning about rectangular window.
+ @pytest.mark.filterwarnings("ignore:A window was not provided:UserWarning")
def test_istft(self, n_fft, hop_length, window_size, signal_shape, normalized, center, ie_device, precision, ir_version, trace_model):
if ie_device == "GPU":
pytest.xfail(reason="ISTFT op is not supported on GPU yet")
self._test(*self.create_model(n_fft, hop_length, window_size, normalized, center), ie_device, precision,
- ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length" : hop_length, "center": center, "normalized": normalized}, trace_model=trace_model)
+ ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape, "n_fft": n_fft, "hop_length": hop_length, "center": center, "normalized": normalized}, trace_model=trace_model)
diff --git a/tests/layer_tests/pytorch_tests/test_lstm.py b/tests/layer_tests/pytorch_tests/test_lstm.py
index 43fb373d6d8..28a43d696b4 100644
--- a/tests/layer_tests/pytorch_tests/test_lstm.py
+++ b/tests/layer_tests/pytorch_tests/test_lstm.py
@@ -7,6 +7,13 @@ import torch
from pytorch_layer_test_class import PytorchLayerTest
+# torch.nn.modules.rnn performs tensor-to-bool shape checks (input_size and
+# hidden_size validation) inside the traced forward pass. The warning is not
+# actionable at the test level; shapes are constant for all parametrize cases.
+pytestmark = pytest.mark.filterwarnings(
+ "ignore:Converting a tensor to a Python boolean:torch.jit.TracerWarning"
+)
+
class aten_lstm(torch.nn.Module):
def __init__(self, input_size, hidden_size, num_layers, has_bias, bidirectional, batch_first):
diff --git a/tests/layer_tests/pytorch_tests/test_mul.py b/tests/layer_tests/pytorch_tests/test_mul.py
index 453904b6503..b9848093387 100644
--- a/tests/layer_tests/pytorch_tests/test_mul.py
+++ b/tests/layer_tests/pytorch_tests/test_mul.py
@@ -56,6 +56,7 @@ class TestMulTypes(PytorchLayerTest):
super().__init__()
self.lhs_type = lhs_type
self.rhs_type = rhs_type
+ self.register_buffer('scalar_three', torch.tensor(3))
if len(lhs_shape) == 0:
self.forward = self.forward1
elif len(rhs_shape) == 0:
@@ -64,10 +65,10 @@ class TestMulTypes(PytorchLayerTest):
self.forward = self.forward3
def forward1(self, rhs):
- return torch.mul(torch.tensor(3).to(self.lhs_type), rhs.to(self.rhs_type))
+ return torch.mul(self.scalar_three.to(self.lhs_type), rhs.to(self.rhs_type))
def forward2(self, lhs):
- return torch.mul(lhs.to(self.lhs_type), torch.tensor(3).to(self.rhs_type))
+ return torch.mul(lhs.to(self.lhs_type), self.scalar_three.to(self.rhs_type))
def forward3(self, lhs, rhs):
return torch.mul(lhs.to(self.lhs_type), rhs.to(self.rhs_type))
diff --git a/tests/layer_tests/pytorch_tests/test_norm.py b/tests/layer_tests/pytorch_tests/test_norm.py
index 44979f920e6..e63409ef2f3 100644
--- a/tests/layer_tests/pytorch_tests/test_norm.py
+++ b/tests/layer_tests/pytorch_tests/test_norm.py
@@ -10,6 +10,23 @@ from packaging import version
from pytorch_layer_test_class import PytorchLayerTest
+# torch._VF.frobenius_norm is deprecated in PyTorch 2.9 in favour of linalg.vector_norm.
+# aten::linalg_vector_norm is already tested by TestLinalgVectorNorm.
+_FROBENIUS_NORM_DEPRECATED = version.parse(torch.__version__) >= version.parse("2.9.0")
+
+
+def _compute_out_shape(input_shape, dim, keepdim):
+ """Return the output shape for a norm reduction given dim and keepdim."""
+ ndim = len(input_shape)
+ if dim is None:
+ return tuple(1 for _ in input_shape) if keepdim else ()
+ dims = {dim % ndim} if isinstance(dim, int) else {d % ndim for d in dim}
+ return tuple(
+ (1 if i in dims else s) if keepdim else s
+ for i, s in enumerate(input_shape)
+ if keepdim or i not in dims
+ )
+
class TestNorm(PytorchLayerTest):
@@ -55,7 +72,7 @@ class TestNorm(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize('p', [-2, -1, 0, 1, 2, 2.5, float('inf'), float('-inf')])
+ @pytest.mark.parametrize('p', [-1, 0, 1, 2, 2.5, float('inf'), float('-inf')])
@pytest.mark.parametrize('dim', [[0], [0, 1], [0, 1, 2]])
@pytest.mark.parametrize('keepdim', [True, False])
def test_norm(self, ie_device, precision, ir_version, p, dim, keepdim):
@@ -64,7 +81,7 @@ class TestNorm(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize('p', [-2, -1, 0, 1, 2, 2.5, float('inf'), float('-inf')])
+ @pytest.mark.parametrize('p', [-1, 0, 1, 2, 2.5, float('inf'), float('-inf')])
@pytest.mark.parametrize('dim', [None, [0], [0, 1], [0, 1, 2]])
@pytest.mark.parametrize('keepdim', [None, True, False])
def test_norm_tensor(self, ie_device, precision, ir_version, p, dim, keepdim):
@@ -84,16 +101,20 @@ class TestWeightNorm(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
+ # torch.nn.utils.weight_norm is deprecated; we intentionally test aten::_weight_norm
+ # so the new parametrizations API cannot be used here.
+ @pytest.mark.filterwarnings("ignore:`torch.nn.utils.weight_norm` is deprecated:FutureWarning")
def test_weight_norm(self, ie_device, precision, ir_version):
self._test(*self.create_model(), ie_device, precision, ir_version, trace_model=True, freeze_model=False)
class TestFrobeniusNorm(PytorchLayerTest):
- def _prepare_input(self, out=False, dtype="float32"):
- x = self.random.randn(10, 12, 14, dtype=dtype)
+ def _prepare_input(self, out=False, dtype="float32", dim=None, keepdim=False):
+ input_shape = (10, 12, 14)
+ x = self.random.randn(*input_shape, dtype=dtype)
if not out:
return (x,)
- y = np.zeros_like(x)
+ y = np.zeros(_compute_out_shape(input_shape, dim, keepdim), dtype=dtype)
return (x, y)
def create_model(self, dim, keepdim, out):
@@ -121,20 +142,24 @@ class TestFrobeniusNorm(PytorchLayerTest):
@pytest.mark.parametrize('keepdim', [True, False])
@pytest.mark.parametrize("out", [False, True])
@pytest.mark.parametrize("dtype", ["float32", "float64"])
+ @pytest.mark.skipif(_FROBENIUS_NORM_DEPRECATED,
+ reason="torch._VF.frobenius_norm is deprecated in this PyTorch version; "
+ "aten::linalg_vector_norm is already covered by TestLinalgVectorNorm")
def test_frobenius_norm(self, ie_device, precision, ir_version, dim, keepdim, out, dtype):
self._test(*self.create_model(dim, keepdim, out), ie_device, precision, ir_version,
- kwargs_to_prepare_input={"out": out, "dtype": dtype}
+ kwargs_to_prepare_input={"out": out, "dtype": dtype, "dim": dim, "keepdim": keepdim}
)
class TestLinalgVectorNorm(PytorchLayerTest):
- def _prepare_input(self, out=False, out_dtype=None):
+ def _prepare_input(self, out=False, out_dtype=None, dim=None, keepdim=False):
+ input_shape = (1, 2, 3)
if not out:
- return (self.random.randn(1, 2, 3),)
- x = self.random.randn(1, 2, 3)
- y = self.random.randn(1, 2, 3, dtype=
- out_dtype if out_dtype is not None else np.float32)
+ return (self.random.randn(*input_shape),)
+ x = self.random.randn(*input_shape)
+ np_dtype = out_dtype if out_dtype is not None else "float32"
+ y = np.zeros(_compute_out_shape(input_shape, dim, keepdim), dtype=np_dtype)
return (x, y)
def create_model(self, p, dim, keepdim, dtype_str, out, out_as_dtype):
@@ -184,26 +209,28 @@ class TestLinalgVectorNorm(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize('p', [-2, -1, 0, 1, 2, 2.5, float('inf'), float('-inf')])
+ @pytest.mark.parametrize('p', [0, 1, 2, 2.5, float('inf'), float('-inf')])
@pytest.mark.parametrize('dim', [0, [0, 1], None])
@pytest.mark.parametrize('keepdim', [True, False])
- @pytest.mark.parametrize("dtype", ["float32", "float64", None])
+ @pytest.mark.parametrize("dtype", ["float32", None])
@pytest.mark.parametrize("out", [True, False])
@pytest.mark.parametrize("prim_dtype", [True, False])
def test_linalg_vector_norm(self, p, dim, keepdim, dtype, out, prim_dtype, ie_device, precision, ir_version):
self._test(*self.create_model(p, dim, keepdim, dtype, out, prim_dtype),
ie_device, precision, ir_version,
- kwargs_to_prepare_input={"out": out or prim_dtype, "out_dtype": dtype if prim_dtype else None})
+ kwargs_to_prepare_input={"out": out or prim_dtype, "out_dtype": dtype if prim_dtype else None,
+ "dim": dim, "keepdim": keepdim})
class TestLinalgMatrixNorm(PytorchLayerTest):
- def _prepare_input(self, out=False, out_dtype=None):
+ def _prepare_input(self, out=False, out_dtype=None, dim=None, keepdim=False):
if not out:
return (self.random.randn(3, 3),)
- x = self.random.randn(1, 3, 3)
- y = self.random.randn(1, 3, 3, dtype=
- out_dtype if out_dtype is not None else np.float32)
+ input_shape = (1, 3, 3)
+ x = self.random.randn(*input_shape)
+ np_dtype = out_dtype if out_dtype is not None else "float32"
+ y = np.zeros(_compute_out_shape(input_shape, dim, keepdim), dtype=np_dtype)
return (x, y)
def create_model(self, p, dim, keepdim, dtype_str, out, out_as_dtype):
@@ -256,7 +283,7 @@ class TestLinalgMatrixNorm(PytorchLayerTest):
@pytest.mark.parametrize('p', [-1, 1, float('inf'), float('-inf'), "fro"])
@pytest.mark.parametrize('dim', [[0, 1], [-1, -2]])
@pytest.mark.parametrize('keepdim', [True, False])
- @pytest.mark.parametrize("dtype", ["float32", "float64", None])
+ @pytest.mark.parametrize("dtype", ["float32", None])
@pytest.mark.parametrize("out", [True, False])
@pytest.mark.parametrize("prim_dtype", [True, False])
@pytest.mark.xfail(condition=platform.system() in ('Darwin', 'Linux') and platform.machine() in ('arm', 'armv7l',
@@ -266,17 +293,18 @@ class TestLinalgMatrixNorm(PytorchLayerTest):
def test_linalg_matrix_norm(self, p, dim, keepdim, dtype, out, prim_dtype, ie_device, precision, ir_version):
self._test(*self.create_model(p, dim, keepdim, dtype, out, prim_dtype),
ie_device, precision, ir_version,
- kwargs_to_prepare_input={"out": out or prim_dtype, "out_dtype": dtype if prim_dtype else None})
+ kwargs_to_prepare_input={"out": out or prim_dtype, "out_dtype": dtype if prim_dtype else None,
+ "dim": dim, "keepdim": keepdim})
class TestLinalgNorm(PytorchLayerTest):
- def _prepare_input(self, out=False, out_dtype=None, input_shape=(3, 3)):
+ def _prepare_input(self, out=False, out_dtype=None, input_shape=(3, 3), dim=None, keepdim=False):
if not out:
return (self.random.randn(*input_shape),)
x = self.random.randn(*input_shape)
- y = self.random.randn(*input_shape, dtype=
- out_dtype if out_dtype is not None else np.float32)
+ np_dtype = out_dtype if out_dtype is not None else "float32"
+ y = np.zeros(_compute_out_shape(input_shape, dim, keepdim), dtype=np_dtype)
return (x, y)
def create_model(self, p, dim, keepdim, dtype_str, out, out_as_dtype):
@@ -286,7 +314,7 @@ class TestLinalgNorm(PytorchLayerTest):
}
dtype = dtypes.get(dtype_str)
- class aten_linalg_matrix_norm(torch.nn.Module):
+ class aten_linalg_norm(torch.nn.Module):
def __init__(self, p, dim, keepdim, dtype, out, out_as_dtype) -> None:
super().__init__()
@@ -322,27 +350,37 @@ class TestLinalgNorm(PytorchLayerTest):
)
- return aten_linalg_matrix_norm(p, dim, keepdim, dtype, out, out_as_dtype), "aten::linalg_norm"
+ return aten_linalg_norm(p, dim, keepdim, dtype, out, out_as_dtype), "aten::linalg_norm"
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize('p,dim', [
- (-1, [0, 1]), (1, [-1, -2]), (float('inf'), [1, 0]),
- (float('-inf'), [-2, -1]), (0, 1), (1, -1),
- (None, None), (2.5, 0), (-1, 1), (2, 0),
- (float('inf'), 1), (float('-inf'), 1), ("fro", (0, 1))])
+ @pytest.mark.parametrize('p,dim,input_shape', [
+ # rank-1: unique branch ord=None+rank1 -> norm_vector(p=2)
+ (None, None, [3]), # ord=None, dim=None -> L2 vector
+ # rank-2: only dim=None cases, which are invalid for rank>=3
+ (2, None, [1, 3]), # numeric ord, dim=None -> norm_vector(p=2)
+ (None, None, [1, 3]), # ord=None, dim=None -> frobenius_norm
+ # rank-3: all explicit-dim dispatch branches
+ (-1, [0, 1], [1, 3, 3]), # numeric ord, 2-elem dim -> norm_matrix(p=-1)
+ (1, -1, [1, 3, 3]), # numeric ord, scalar dim -> norm_vector(p=1)
+ (2.5, 0, [1, 3, 3]), # numeric ord, scalar dim -> norm_vector(else)
+ (float('inf'), 1, [1, 3, 3]), # numeric ord, scalar dim -> norm_vector(p=inf)
+ ("fro", (0, 1), [1, 3, 3]), # string ord -> frobenius_norm
+ (0, 1, [1, 3, 3]), # p==0 branch
+ ])
@pytest.mark.parametrize('keepdim', [True, False])
- @pytest.mark.parametrize("dtype", ["float32", "float64", None])
+ @pytest.mark.parametrize("dtype", ["float32", None])
@pytest.mark.parametrize("out", [True, False])
@pytest.mark.parametrize("prim_dtype", [True, False])
- @pytest.mark.parametrize("input_shape", [[1, 3], [3, 3], [1, 3, 3]])
def test_linalg_norm(self, p, dim, keepdim, dtype, out, prim_dtype, input_shape, ie_device, precision, ir_version):
self._test(*self.create_model(p, dim, keepdim, dtype, out, prim_dtype),
ie_device, precision, ir_version,
kwargs_to_prepare_input={
"out": out or prim_dtype,
"out_dtype": dtype if prim_dtype else None,
- "input_shape": input_shape
+ "input_shape": input_shape,
+ "dim": dim,
+ "keepdim": keepdim
})
@@ -388,7 +426,7 @@ class TestRMSNorm(PytorchLayerTest):
@pytest.mark.precommit_torch_export
@pytest.mark.skipif(version.parse(torch.__version__) < version.parse("2.4"),
reason="Not supported in PyTorch versions earlier than 2.4.")
- @pytest.mark.parametrize("normalized_shape", [[10,],
+ @pytest.mark.parametrize("normalized_shape", [[10],
[10, 10],
[5, 10, 10]])
@pytest.mark.parametrize('gamma', [True, False])
diff --git a/tests/layer_tests/pytorch_tests/test_rand.py b/tests/layer_tests/pytorch_tests/test_rand.py
index a98ddf33db9..e9825597fa3 100644
--- a/tests/layer_tests/pytorch_tests/test_rand.py
+++ b/tests/layer_tests/pytorch_tests/test_rand.py
@@ -6,6 +6,11 @@ import torch
from pytorch_layer_test_class import PytorchLayerTest
+# Random ops produce different outputs on each trace run, so the tracer always
+# warns that the traced output doesn't match the Python function output.
+# This is expected and not actionable at the test level.
+pytestmark = pytest.mark.filterwarnings("ignore::torch.jit.TracerWarning")
+
class TestInplaceNormal(PytorchLayerTest):
def _prepare_input(self):
diff --git a/tests/layer_tests/pytorch_tests/test_roi_align.py b/tests/layer_tests/pytorch_tests/test_roi_align.py
index 81e0bfe0750..52563000ab2 100644
--- a/tests/layer_tests/pytorch_tests/test_roi_align.py
+++ b/tests/layer_tests/pytorch_tests/test_roi_align.py
@@ -53,6 +53,9 @@ class TestROIAlign(PytorchLayerTest):
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
@pytest.mark.precommit_fx_backend
+ # TracerWarning fires inside torch.assert inside torchvision roi_align;
+ # shape is constant for all parametrize cases.
+ @pytest.mark.filterwarnings("ignore:Converting a tensor to a Python boolean:torch.jit.TracerWarning")
def test_roi_align(self, ie_device, precision, ir_version, input_shape, boxes, output_size,
spatial_scale, sampling_ratio, aligned):
self.input_tensor = self.random.randn(*input_shape)
diff --git a/tests/layer_tests/pytorch_tests/test_scatter.py b/tests/layer_tests/pytorch_tests/test_scatter.py
index daa6975cffc..e9a7c7d9a8a 100644
--- a/tests/layer_tests/pytorch_tests/test_scatter.py
+++ b/tests/layer_tests/pytorch_tests/test_scatter.py
@@ -92,36 +92,34 @@ class TestScatter(PytorchLayerTest):
@pytest.mark.precommit
@pytest.mark.precommit_fx_backend
@pytest.mark.parametrize("dim", [1, -1, 0])
- @pytest.mark.parametrize(
- "index",
- [
- None, # Empty tensor scenario.
- torch.tensor([[0, 1, 2, 3]]),
- torch.tensor([[0, 5], [4, 1], [2, 3]]),
- ],
- )
- @pytest.mark.parametrize("src", [torch.arange(1, 26).reshape(5, 5), 1])
- @pytest.mark.parametrize("dtype", ["int32", "int64", "float32", "float64"])
+ @pytest.mark.parametrize("src,reduce,index", [
+ # tensor src + no reduce: all index shapes valid (tensor+reduce deprecated since PyTorch 1.12)
+ (torch.arange(1, 26).reshape(5, 5), None, None),
+ (torch.arange(1, 26).reshape(5, 5), None, torch.tensor([[0, 1, 2, 3]])),
+ (torch.arange(1, 26).reshape(5, 5), None, torch.tensor([[0, 5], [4, 1], [2, 3]])),
+ # scalar src + no reduce: all index shapes valid
+ (1, None, None),
+ (1, None, torch.tensor([[0, 1, 2, 3]])),
+ (1, None, torch.tensor([[0, 5], [4, 1], [2, 3]])),
+ # scalar src + reduce
+ (1, "add", None),
+ (1, "add", torch.tensor([[0, 1, 2, 3]])),
+ (1, "add", torch.tensor([[0, 5], [4, 1], [2, 3]])),
+ (1, "multiply", None),
+ (1, "multiply", torch.tensor([[0, 1, 2, 3]])),
+ (1, "multiply", torch.tensor([[0, 5], [4, 1], [2, 3]])),
+ ])
+ @pytest.mark.parametrize("dtype", ["int32", "float32"])
@pytest.mark.parametrize(["inplace", "has_out"], [(True, False), (False, True), (False, False)])
- @pytest.mark.parametrize("reduce", [None, "add", "multiply"])
- def test_scatter(self, dim, index, src, dtype, inplace, has_out, reduce, ie_device, precision, ir_version):
+ def test_scatter(self, src, reduce, index, dim, dtype, inplace, has_out, ie_device, precision, ir_version):
if isinstance(src, torch.Tensor):
src = src.to(getattr(torch, dtype))
- freeze = True
- if index is None:
- # Freeze creates empty constant tensor which isn't supported by OV.
- freeze = False
- if (not freeze) and reduce:
- pytest.skip(
- "Cannot test reduce parameters with empty indexes due to issues with empty constant tensor or issues with prim::GetAttr str inputs."
- )
self._test(
*self.create_model(dim, index, src, inplace, reduce, has_out),
ie_device,
precision,
ir_version,
kwargs_to_prepare_input={"dtype": dtype, "out": has_out},
- freeze_model=freeze,
)
@@ -189,29 +187,22 @@ class TestScatterReduce(PytorchLayerTest):
@pytest.mark.parametrize(
"index",
[
- None, # Empty tensor scenario.
+ # index=None (empty tensor) is always skipped: reduce is always a non-None string,
+ # and string prim::GetAttr nodes are unresolvable with freeze_model=False.
torch.tensor([[0, 1, 2, 3]]),
torch.tensor([[0, 5], [4, 1], [2, 3]]),
],
)
@pytest.mark.parametrize("src", [torch.arange(1, 26).reshape(5, 5)])
- @pytest.mark.parametrize("dtype", ["int32", "int64", "float32", "float64"])
+ @pytest.mark.parametrize("dtype", ["int32", "float32"])
@pytest.mark.parametrize(["inplace", "has_out"], [(True, False), (False, True), (False, False)])
@pytest.mark.parametrize("reduce", ["sum", "prod", "mean", "amax", "amin"])
@pytest.mark.parametrize("include_self", [True, False])
- def test_scatter_reduce(self, dim, index, src, dtype, inplace, has_out, reduce, include_self, ie_device, precision, ir_version):
+ def test_scatter_reduce(self, dim, index, src, dtype, inplace, has_out, reduce, include_self, ie_device, precision, ir_version):
if isinstance(src, torch.Tensor):
src = src.to(getattr(torch, dtype))
- freeze = True
- if index is None:
- # Freeze creates empty constant tensor which isn't supported by OV.
- freeze = False
- if (not freeze) and reduce:
- pytest.skip(
- "Cannot test reduce parameters with empty indexes due to issues with empty constant tensor or issues with prim::GetAttr str inputs."
- )
- kwargs = dict(kwargs_to_prepare_input={"dtype": dtype, "out": has_out}, freeze_model=freeze)
- if reduce == "mean" and dtype in ["int32", "int64"]:
+ kwargs = dict(kwargs_to_prepare_input={"dtype": dtype, "out": has_out})
+ if reduce == "mean" and dtype == "int32":
# rounding can be different on torch vs ov
kwargs["custom_eps"] = 1.
self._test(
@@ -262,21 +253,17 @@ class TestScatterAdd(PytorchLayerTest):
@pytest.mark.parametrize(
"index",
[
- None, # Empty tensor scenario.
+ None, # Empty tensor scenario: scatter_add with zero-size index (no-op).
torch.tensor([[0, 1, 2, 3]]),
torch.tensor([[0, 5], [4, 1], [2, 3]]),
],
)
@pytest.mark.parametrize("src", [torch.arange(1, 26).reshape(5, 5)])
- @pytest.mark.parametrize("dtype", ["int32", "int64", "float32", "float64"])
+ @pytest.mark.parametrize("dtype", ["int32", "float32"])
@pytest.mark.parametrize("inplace", [skip_if_export(True), False])
def test_scatter_add(self, dim, index, src, dtype, inplace, ie_device, precision, ir_version):
if isinstance(src, torch.Tensor):
src = src.to(getattr(torch, dtype))
- if index is None:
- pytest.skip(
- "Cannot test reduce parameters with empty indexes due to issues with empty constant tensor or issues with prim::GetAttr str inputs."
- )
self._test(
*self.create_model(dim, index, src, inplace),
ie_device,
diff --git a/tests/layer_tests/pytorch_tests/test_stft.py b/tests/layer_tests/pytorch_tests/test_stft.py
index 1d23f5332fe..4b132b2b7a2 100644
--- a/tests/layer_tests/pytorch_tests/test_stft.py
+++ b/tests/layer_tests/pytorch_tests/test_stft.py
@@ -2,9 +2,15 @@
# SPDX-License-Identifier: Apache-2.0
import pytest
+import torch
+from packaging import version
from pytorch_layer_test_class import PytorchLayerTest
+# stft with return_complex=False is deprecated in PyTorch 2.9.
+# return_complex=True is already tested in TestSTFTAttrs.
+_STFT_RETURN_REAL_DEPRECATED = version.parse(torch.__version__) >= version.parse("2.9.0")
+
class TestSTFT(PytorchLayerTest):
def _prepare_input(self, win_length, signal_shape, rand_data=False, out_dtype="float32"):
@@ -25,8 +31,6 @@ class TestSTFT(PytorchLayerTest):
return (signal, window.astype(out_dtype))
def create_model(self, n_fft, hop_length, win_length, normalized):
- import torch
-
class aten_stft(torch.nn.Module):
def __init__(self, n_fft, hop_length, win_length, normalized):
@@ -55,8 +59,8 @@ class TestSTFT(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize(("trace_model"), [True, False])
- @pytest.mark.parametrize(("signal_shape"), [(1, 256), (2, 128), (128,)])
+ @pytest.mark.parametrize("trace_model", [True, False])
+ @pytest.mark.parametrize("signal_shape", [(1, 256), (2, 128), (128,)])
@pytest.mark.parametrize(("n_fft", "hop_length", "window_size"), [
[16, 4, 16],
[32, 32, 32],
@@ -64,10 +68,13 @@ class TestSTFT(PytorchLayerTest):
[24, 32, 20],
[128, 128, 128],
])
- @pytest.mark.parametrize(("normalized"), [True, False])
+ @pytest.mark.parametrize("normalized", [True, False])
def test_stft(self, n_fft, hop_length, window_size, signal_shape, normalized, ie_device, precision, ir_version, trace_model):
if ie_device == "GPU":
pytest.xfail(reason="STFT op is not supported on GPU yet")
+ if _STFT_RETURN_REAL_DEPRECATED:
+ pytest.skip("stft with return_complex=False is deprecated in this PyTorch version; "
+ "return_complex=True is already covered by TestSTFTAttrs")
self._test(*self.create_model(n_fft, hop_length, window_size, normalized), ie_device, precision,
ir_version, kwargs_to_prepare_input={"win_length": window_size, "signal_shape": signal_shape}, trace_model=trace_model)
@@ -80,8 +87,6 @@ class TestSTFTAttrs(PytorchLayerTest):
return (signal,)
def create_model_with_attrs(self, n_fft, hop_length, win_length, center, pad_mode, normalized, onesided, return_complex):
- import torch
-
class aten_stft_attrs(torch.nn.Module):
def __init__(self, n_fft, hop_length, win_length, center, pad_mode, normalized, onesided, return_complex):
@@ -119,11 +124,13 @@ class TestSTFTAttrs(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
- @pytest.mark.parametrize(("trace_model"), [True, False])
+ # Several parametrize entries intentionally pass window=None to test default-window
+ # handling; PyTorch warns about the missing window in those cases.
+ @pytest.mark.filterwarnings("ignore:A window was not provided:UserWarning")
+ @pytest.mark.parametrize("trace_model", [True, False])
@pytest.mark.parametrize(("n_fft", "hop_length", "win_length", "center", "pad_mode", "normalized", "onesided", "return_complex"), [
[16, 4, 16, False, "reflect", False, True, False], # default window
[16, 4, 14, True, "reflect", False, True, False], # center True
- [16, 4, 14, True, "reflect", False, True, False], # center True
[16, 4, 14, True, "replicate", False, True, False], # center True
[16, 4, 14, False, "replicate", False, True, False], # center False
[16, None, 16, False, "reflect", False, True, False], # hop_length None
@@ -138,6 +145,10 @@ class TestSTFTAttrs(PytorchLayerTest):
if ie_device == "GPU":
pytest.xfail(reason="STFT op is not supported on GPU yet")
+ if not return_complex and _STFT_RETURN_REAL_DEPRECATED:
+ pytest.skip("stft with return_complex=False is deprecated in this PyTorch version; "
+ "return_complex=True is already covered by other entries in this class")
+
if center is True and trace_model is False:
pytest.xfail(
reason="torch stft uses list() for `center` subgrpah before aten::stft, that leads to error: No conversion rule found for operations: aten::list")
diff --git a/tests/layer_tests/pytorch_tests/test_sum.py b/tests/layer_tests/pytorch_tests/test_sum.py
index ef89f1a971e..6c95b907c0d 100644
--- a/tests/layer_tests/pytorch_tests/test_sum.py
+++ b/tests/layer_tests/pytorch_tests/test_sum.py
@@ -7,7 +7,7 @@ from pytorch_layer_test_class import PytorchLayerTest, skip_if_export
class TestSum(PytorchLayerTest):
- def _prepare_input(self, out=False, input_dtype="float32", out_dtype="float32"):
+ def _prepare_input(self, out=False, input_dtype="float32", out_dtype="float32", axes=None, keep_dims=None):
# This test had sporadically failed by accuracy. Try to resolve that by using int numbers in input
import numpy as np
min_value = -10 if input_dtype not in ["uint8", "bool"] else 0
@@ -17,7 +17,18 @@ class TestSum(PytorchLayerTest):
return (input, )
if out_dtype is None:
out_dtype = input_dtype if input_dtype not in ["uint8", "bool"] else "int64"
- out = np.zeros((1, 3, 5, 5), dtype=out_dtype)
+ # compute correct output shape based on axes and keep_dims
+ input_shape = [1, 3, 5, 5]
+ ndim = len(input_shape)
+ if axes is None:
+ out_shape = ()
+ elif keep_dims:
+ axes_norm = set([axes % ndim] if isinstance(axes, int) else [a % ndim for a in axes])
+ out_shape = tuple(1 if i in axes_norm else s for i, s in enumerate(input_shape))
+ else:
+ axes_norm = set([axes % ndim] if isinstance(axes, int) else [a % ndim for a in axes])
+ out_shape = tuple(s for i, s in enumerate(input_shape) if i not in axes_norm)
+ out = np.zeros(out_shape, dtype=out_dtype)
return input, out
def create_model(self, axes, keep_dims, out, dtype, input_dtype):
@@ -99,5 +110,6 @@ class TestSum(PytorchLayerTest):
def test_sum(self, axes, keep_dims, out, dtype, input_dtype, ie_device, precision, ir_version):
self._test(*self.create_model(axes, keep_dims, out, dtype, input_dtype),
ie_device, precision, ir_version,
- kwargs_to_prepare_input={"out": out, "input_dtype": input_dtype, "out_dtype": dtype}
+ kwargs_to_prepare_input={"out": out, "input_dtype": input_dtype, "out_dtype": dtype,
+ "axes": axes, "keep_dims": keep_dims}
)
diff --git a/tests/layer_tests/pytorch_tests/test_take_along_dim.py b/tests/layer_tests/pytorch_tests/test_take_along_dim.py
index 4ba6d4c73d1..c21b728fdf8 100644
--- a/tests/layer_tests/pytorch_tests/test_take_along_dim.py
+++ b/tests/layer_tests/pytorch_tests/test_take_along_dim.py
@@ -13,10 +13,7 @@ class TestTakeAlongDim(PytorchLayerTest):
inp = self.random.randn(m, n)
if out:
axis = int(max_val == n)
- if flattenize:
- out = np.zeros_like(np.take(inp, index))
- else:
- out = np.zeros_like(np.take(inp, index, axis))
+ out = np.zeros_like(index, dtype=inp.dtype)
return (inp, index, out)
return (inp, index)
diff --git a/tests/layer_tests/pytorch_tests/test_to.py b/tests/layer_tests/pytorch_tests/test_to.py
index c83ee74f042..0375611afa6 100644
--- a/tests/layer_tests/pytorch_tests/test_to.py
+++ b/tests/layer_tests/pytorch_tests/test_to.py
@@ -167,6 +167,7 @@ class TestAtenToComplex(PytorchLayerTest):
torch.complex128])
@pytest.mark.nightly
@pytest.mark.precommit
+ @pytest.mark.filterwarnings("ignore:ComplexHalf support is experimental:UserWarning")
def test_aten_to_complex(self, dtype, ie_device, precision, ir_version):
self._test(*self.create_model(dtype), ie_device, precision,
ir_version, trace_model=True)
@@ -196,6 +197,7 @@ class TestAtenToFromComplex(PytorchLayerTest):
torch.float64])
@pytest.mark.nightly
@pytest.mark.precommit
+ @pytest.mark.filterwarnings("ignore:Casting complex values to real discards the imaginary part:UserWarning")
def test_aten_to_from_complex(self, dtype, ie_device, precision, ir_version):
self._test(*self.create_model(dtype), ie_device, precision,
ir_version)
@@ -225,6 +227,7 @@ class TestAtenToFromComplexTensor(PytorchLayerTest):
torch.float64])
@pytest.mark.nightly
@pytest.mark.precommit
+ @pytest.mark.filterwarnings("ignore:Casting complex values to real discards the imaginary part:UserWarning")
def test_aten_to_from_complex(self, dtype, ie_device, precision, ir_version):
self._test(*self.create_model(dtype), ie_device, precision,
ir_version)
diff --git a/tests/layer_tests/pytorch_tests/test_transpose.py b/tests/layer_tests/pytorch_tests/test_transpose.py
index 0332518e7ef..3f193fcb5a0 100644
--- a/tests/layer_tests/pytorch_tests/test_transpose.py
+++ b/tests/layer_tests/pytorch_tests/test_transpose.py
@@ -4,8 +4,14 @@
import numpy as np
import pytest
import torch
+from packaging import version
from pytorch_layer_test_class import PytorchLayerTest
+# x.T (numpy_T) on non-2D tensors is deprecated in PyTorch 2.9:
+# - 0-D: "Tensor.T is deprecated on 0-D tensors"
+# - 1-D: "The use of x.T on tensors of dimension other than 2 ... is deprecated"
+_TENSOR_T_NON2D_DEPRECATED = version.parse(torch.__version__) >= version.parse("2.9.0")
+
class TestTranspose(PytorchLayerTest):
def _prepare_input(self):
@@ -106,6 +112,8 @@ class TestTSmall(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
def test_t_small(self, num_dims, input_dtype, mode, ie_device, precision, ir_version):
+ if mode == "numpy" and num_dims != 2 and _TENSOR_T_NON2D_DEPRECATED:
+ pytest.skip("x.T on non-2D tensors is deprecated in PyTorch 2.9")
self._test(
*self.create_model(mode),
ie_device,
diff --git a/tests/layer_tests/pytorch_tests/test_unary_ops.py b/tests/layer_tests/pytorch_tests/test_unary_ops.py
index ea3d9be1949..194e1f5a465 100644
--- a/tests/layer_tests/pytorch_tests/test_unary_ops.py
+++ b/tests/layer_tests/pytorch_tests/test_unary_ops.py
@@ -136,9 +136,14 @@ class unary_op_complex_net(torch.nn.Module):
class TestUnaryOp(PytorchLayerTest):
- def _prepare_input(self):
- # random number in range [1, 11)
- x = self.random.torch_rand(2, 10) * 10 + 1
+ def _prepare_input(self, unit_range=False):
+ if unit_range:
+ # atanh requires inputs in (-1, 1); use uniform random in that range.
+ # Integer dtypes will truncate to 0, giving atanh(0)=0 — still exercises the op.
+ x = self.random.torch_rand(2, 10) * 1.99 - 0.995
+ else:
+ # random number in range [1, 11)
+ x = self.random.torch_rand(2, 10) * 10 + 1
return (x.to(self.dtype).numpy(),)
@pytest.mark.nightly
@@ -186,7 +191,8 @@ class TestUnaryOp(PytorchLayerTest):
if self.use_torch_export() and op_type == "aten::atanh" and dtype in [torch.int8, torch.int32, torch.int64]:
pytest.xfail(reason="torch.export after 2.4.0 doesn't support unsigned int types for atanh in some configurations")
self._test(unary_op_net(OPS[op_type], dtype), op_type,
- ie_device, precision, ir_version)
+ ie_device, precision, ir_version,
+ kwargs_to_prepare_input={"unit_range": op_type == "aten::atanh"})
@pytest.mark.nightly
@pytest.mark.precommit
@@ -273,7 +279,8 @@ class TestUnaryOp(PytorchLayerTest):
def test_unary_op_out(self, op_type, dtype, ie_device, precision, ir_version):
self.dtype = dtype
self._test(unary_op_out_net(OPS[op_type], dtype), op_type,
- ie_device, precision, ir_version)
+ ie_device, precision, ir_version,
+ kwargs_to_prepare_input={"unit_range": op_type == "aten::atanh"})
@pytest.mark.nightly
@pytest.mark.precommit
diff --git a/tests/layer_tests/pytorch_tests/test_where.py b/tests/layer_tests/pytorch_tests/test_where.py
index 371826e2f4d..d377824f4b2 100644
--- a/tests/layer_tests/pytorch_tests/test_where.py
+++ b/tests/layer_tests/pytorch_tests/test_where.py
@@ -3,9 +3,14 @@
import numpy as np
import pytest
+import torch
+from packaging import version
from pytorch_layer_test_class import PytorchLayerTest, skip_if_export
+# torch.where with a uint8 condition tensor is deprecated in PyTorch 2.9.
+_WHERE_UINT8_DEPRECATED = version.parse(torch.__version__) >= version.parse("2.9.0")
+
class Testwhere(PytorchLayerTest):
def _prepare_input(self, mask_fill='ones', mask_dtype=bool, return_x_y=False, x_dtype="float32", y_dtype=None, scalar_cond=False):
@@ -68,6 +73,8 @@ class Testwhere(PytorchLayerTest):
@pytest.mark.precommit
@pytest.mark.precommit_torch_export
def test_where(self, mask_fill, mask_dtype, x_dtype, y_dtype, ie_device, precision, ir_version):
+ if mask_dtype == np.uint8 and _WHERE_UINT8_DEPRECATED:
+ pytest.skip("torch.where with uint8 condition is deprecated in PyTorch 2.9; use bool condition instead")
self._test(*self.create_model(False, dtypes=(x_dtype, y_dtype)),
ie_device, precision, ir_version,
kwargs_to_prepare_input={
@@ -85,6 +92,8 @@ class Testwhere(PytorchLayerTest):
@pytest.mark.nightly
@pytest.mark.precommit
def test_where_as_nonzero(self, mask_fill, mask_dtype, x_dtype, ie_device, precision, ir_version):
+ if mask_dtype == np.uint8 and _WHERE_UINT8_DEPRECATED:
+ pytest.skip("torch.where with uint8 condition is deprecated in PyTorch 2.9; use bool condition instead")
# torch.jit.trace is used here (trace_model=True) so that example inputs annotate concrete
# shapes, giving translate_where a static input rank to split NonZero output into per-dim
# index tensors. Without example inputs torch.jit.script leaves rank dynamic and