Skip to content

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

[PyTorch] Support cat with rank-one empty tensors (#39434)

Maxim Vafin4f938b8f5628
diff --git a/src/frontends/pytorch/src/op/cat.cpp b/src/frontends/pytorch/src/op/cat.cpp
index fb242547a05..9e4273c979f 100644
--- a/src/frontends/pytorch/src/op/cat.cpp
+++ b/src/frontends/pytorch/src/op/cat.cpp
@@ -106,7 +106,18 @@ OutputVector translate_cat_common(const NodeContext& context,
}
}
}
- auto concat = context.mark_node(std::make_shared<v0::Concat>(inputs_vec, axis));
+ OutputVector concat_inputs;
+ std::copy_if(inputs_vec.begin(),
+ inputs_vec.end(),
+ std::back_inserter(concat_inputs),
+ [is_complex](const auto& input) {
+ // Only shape (0,) is special: PyTorch skips it in cat regardless of the other inputs' rank,
+ // while v0::Concat requires equal ranks. Other empty tensors keep their rank and stay in
+ // Concat. Skipped inputs still participate in type promotion above.
+ return input.get_partial_shape() != (is_complex ? PartialShape{0, 2} : PartialShape{0});
+ });
+ auto concat = concat_inputs.empty() ? inputs_vec.front()
+ : context.mark_node(std::make_shared<v0::Concat>(concat_inputs, axis));
if (is_complex) {
concat = context.mark_node(std::make_shared<ComplexTypeMark>(concat));
}
diff --git a/tests/layer_tests/pytorch_tests/test_cat.py b/tests/layer_tests/pytorch_tests/test_cat.py
index 8d23fdb465a..f55465fa3f3 100644
--- a/tests/layer_tests/pytorch_tests/test_cat.py
+++ b/tests/layer_tests/pytorch_tests/test_cat.py
@@ -4,6 +4,7 @@
import pytest
import torch
import numpy as np
+import openvino as ov
from pytorch_layer_test_class import PytorchLayerTest
@@ -153,6 +154,87 @@ class TestCat(PytorchLayerTest):
ie_device, precision, ir_version, freeze_model=False)
+class TestCatEmpty(PytorchLayerTest):
+ def _prepare_input(self, shape, dtype):
+ return (self.random.randn(*shape).astype(dtype),)
+
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.nightly
+ @pytest.mark.parametrize("dynamo", [False, True])
+ @pytest.mark.parametrize("empty_dtype", [torch.float32, torch.float64])
+ def test_empty_cache_conversion(self, dynamo, empty_dtype, ie_device, precision, ir_version):
+ class EmptyCache(torch.nn.Module):
+ def forward(self, keys):
+ empty = torch.tensor([], dtype=empty_dtype, device=keys.device)
+ return torch.cat([empty, keys], dim=-2)
+
+ model = EmptyCache().eval()
+ keys = self.random.torch_randn(1, 1, 4, 8)
+ converted = ov.convert_model(model, example_input=(keys,), dynamo=dynamo)
+ compiled = ov.Core().compile_model(converted, ie_device, {"INFERENCE_PRECISION_HINT": "f32"})
+ for _ in range(3):
+ keys = self.random.torch_randn(1, 1, 4, 8)
+ expected = model(keys).numpy()
+ actual = compiled((keys.numpy(),))[compiled.output(0)]
+ assert actual.shape == expected.shape
+ assert actual.dtype == expected.dtype
+ np.testing.assert_array_equal(actual, expected)
+
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.nightly
+ @pytest.mark.parametrize("axis", [0, 1, -2])
+ @pytest.mark.parametrize("position", ["first", "middle", "last", "multiple", "all"])
+ @pytest.mark.parametrize("empty_dtype", [torch.float32, torch.float64, torch.int32])
+ def test_cat_empty(self, axis, position, empty_dtype, ie_device, precision, ir_version):
+ class CatEmpty(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.empty = torch.empty(0, dtype=empty_dtype)
+
+ def forward(self, x):
+ if position == "first":
+ return torch.cat([self.empty, x], axis)
+ if position == "middle":
+ return torch.cat([x, self.empty, x], axis)
+ if position == "last":
+ return torch.cat([x, self.empty], axis)
+ if position == "multiple":
+ return torch.cat([self.empty, x, self.empty], axis)
+ return torch.cat([self.empty, torch.empty(0, dtype=x.dtype)], axis), x
+
+ self._test(CatEmpty(), "aten::cat", ie_device, precision, ir_version,
+ trace_model=True, dynamic_shapes=False,
+ kwargs_to_prepare_input={"shape": (1, 1, 4, 8), "dtype": np.float32})
+
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.nightly
+ @pytest.mark.parametrize("axis", [1, -2])
+ def test_cat_empty_complex(self, axis, ie_device, precision, ir_version):
+ class CatEmptyComplex(torch.nn.Module):
+ def forward(self, x):
+ empty = torch.view_as_complex(torch.empty(0, 2))
+ return torch.view_as_real(torch.cat([empty, torch.view_as_complex(x), empty], axis))
+
+ self._test(CatEmptyComplex(), "aten::cat", ie_device, precision, ir_version,
+ trace_model=True,
+ kwargs_to_prepare_input={"shape": (1, 1, 4, 8, 2), "dtype": np.float32})
+
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.nightly
+ def test_cat_higher_rank_empty(self, ie_device, precision, ir_version):
+ class CatHigherRankEmpty(torch.nn.Module):
+ def forward(self, x):
+ empty = x[:, :0]
+ return torch.cat([empty, x, empty], 1)
+
+ self._test(CatHigherRankEmpty(), "aten::cat", ie_device, precision, ir_version,
+ kwargs_to_prepare_input={"shape": (2, 3, 4), "dtype": np.float32})
+
+
class TestCatAlignTypes(PytorchLayerTest):
def _prepare_input(self, in_types):
in_vals = []