Skip to content

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

[PT FE] Support aten::ravel operator (#38097)

Anurag Singh41e483fc50de
diff --git a/src/frontends/pytorch/src/op/ravel.cpp b/src/frontends/pytorch/src/op/ravel.cpp
new file mode 100644
index 00000000000..a3e485f8fa6
--- /dev/null
+++ b/src/frontends/pytorch/src/op/ravel.cpp
@@ -0,0 +1,45 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/frontend/complex_type_mark.hpp"
+#include "openvino/frontend/pytorch/node_context.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/reshape.hpp"
+#include "utils.hpp"
+
+namespace ov {
+namespace frontend {
+namespace pytorch {
+namespace op {
+
+using namespace ov::op;
+
+OutputVector translate_ravel(const NodeContext& context) {
+ // Schema: aten::ravel(Tensor self) -> Tensor
+ num_inputs_check(context, 1, 1, true);
+ auto tensor = context.get_input(0);
+ Output<Node> shape = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {-1}));
+
+ auto complex_type_mark = as_type_ptr<ComplexTypeMark>(tensor.get_node_shared_ptr());
+ if (complex_type_mark) {
+ tensor = complex_type_mark->get_data();
+ auto const_2 = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {2}));
+ shape = context.mark_node(std::make_shared<v0::Concat>(OutputVector{shape, const_2}, 0));
+ }
+
+ auto reshape = context.mark_node(std::make_shared<v1::Reshape>(tensor, shape, false));
+
+ if (complex_type_mark) {
+ const auto& complex_dtype = complex_type_mark->get_complex_part_type();
+ return {context.mark_node(std::make_shared<ComplexTypeMark>(reshape, complex_dtype))};
+ } else {
+ return {reshape};
+ }
+};
+
+} // namespace op
+} // namespace pytorch
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index d721fb39851..beeac0d5ad5 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -235,6 +235,7 @@ OP_CONVERTER(translate_randn);
OP_CONVERTER(translate_randint);
OP_CONVERTER(translate_rand_like);
OP_CONVERTER(translate_randn_like);
+OP_CONVERTER(translate_ravel);
OP_CONVERTER(translate_reciprocal);
OP_CONVERTER(translate_reflection_pad_nd);
OP_CONVERTER(translate_relu6);
@@ -702,6 +703,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::randint", op::translate_randint},
{"aten::randn", op::translate_randn},
{"aten::randn_like", op::translate_randn_like},
+ {"aten::ravel", op::translate_ravel},
{"aten::real", common_translators::translate_real},
{"aten::reciprocal", op::optional_out<op::translate_reciprocal, 1>},
{"aten::reciprocal_", op::inplace_op<op::translate_reciprocal>},
diff --git a/tests/layer_tests/pytorch_tests/test_ravel.py b/tests/layer_tests/pytorch_tests/test_ravel.py
new file mode 100644
index 00000000000..7df3409e127
--- /dev/null
+++ b/tests/layer_tests/pytorch_tests/test_ravel.py
@@ -0,0 +1,77 @@
+# Copyright (C) 2018-2026 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import pytest
+import numpy as np
+
+from pytorch_layer_test_class import PytorchLayerTest
+
+
+class TestRavel(PytorchLayerTest):
+ def _prepare_input(self, shape, dtype="float32"):
+ if dtype.startswith("int"):
+ return (self.random.randint(-100, 100, size=shape).astype(dtype),)
+ return (self.random.randn(*shape).astype(dtype),)
+
+ def create_model(self):
+ import torch
+
+ class aten_ravel(torch.nn.Module):
+ def forward(self, x):
+ return torch.ravel(x)
+
+ return aten_ravel(), "aten::ravel"
+
+ @pytest.mark.parametrize("shape", [
+ [2, 3],
+ [2, 3, 4],
+ [2, 3, 4, 5],
+ [1],
+ [5],
+ [1, 1, 1],
+ [],
+ ])
+ @pytest.mark.parametrize("dtype", ["float32", "float64", "int32", "int64", "int8"])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_ravel(self, shape, dtype, ie_device, precision, ir_version):
+ self._test(
+ *self.create_model(),
+ ie_device,
+ precision,
+ ir_version,
+ kwargs_to_prepare_input={"shape": shape, "dtype": dtype},
+ )
+
+
+class TestRavelComplex(PytorchLayerTest):
+ def _prepare_input(self, shape):
+ return (self.random.randn(*shape, 2).astype(np.float32),)
+
+ def create_model(self):
+ import torch
+
+ class aten_ravel(torch.nn.Module):
+ def forward(self, x):
+ x = torch.view_as_complex(x)
+ return torch.view_as_real(torch.ravel(x))
+
+ return aten_ravel(), "aten::ravel"
+
+ @pytest.mark.parametrize("shape", [
+ [2, 3],
+ [2, 3, 4],
+ [],
+ ])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_ravel_complex(self, shape, ie_device, precision, ir_version):
+ self._test(
+ *self.create_model(),
+ ie_device,
+ precision,
+ ir_version,
+ kwargs_to_prepare_input={"shape": shape},
+ )