Skip to content

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

Implement native aten::fractional_max_pool2d and aten::fractional_max_pool3d - Resolves #29696 (#37431)

M Muthu Kumarane2d74885f67c
diff --git a/docs/sphinx_setup/_static/conformance_files/pytorch_ops.csv b/docs/sphinx_setup/_static/conformance_files/pytorch_ops.csv
index 27e9e270482..df761a5993e 100644
--- a/docs/sphinx_setup/_static/conformance_files/pytorch_ops.csv
+++ b/docs/sphinx_setup/_static/conformance_files/pytorch_ops.csv
@@ -150,6 +150,8 @@ aten::floor_divide,
aten::floor_divide_,
aten::floordiv,
aten::fmod,
+aten::fractional_max_pool2d,
+aten::fractional_max_pool3d,
aten::frobenius_norm,
aten::full,
aten::full_like,
diff --git a/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py b/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py
index 0397cf00a43..70bc074297f 100644
--- a/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py
+++ b/src/bindings/python/src/openvino/frontend/pytorch/torchdynamo/op_support.py
@@ -127,6 +127,8 @@ class OperatorSupport(OpSupport):
"torch.ops.aten.floor.default": None,
"torch.ops.aten.fmod.Scalar": None,
"torch.ops.aten.fmod.Tensor": None,
+ "torch.ops.aten.fractional_max_pool2d.default": None,
+ "torch.ops.aten.fractional_max_pool3d.default": None,
"torch.ops.aten.full.default": None,
"torch.ops.aten.full.names": None,
"torch.ops.aten.full_like.default": None,
diff --git a/src/frontends/pytorch/src/op/fractional_max_poolnd.cpp b/src/frontends/pytorch/src/op/fractional_max_poolnd.cpp
new file mode 100644
index 00000000000..5406e3c7c0d
--- /dev/null
+++ b/src/frontends/pytorch/src/op/fractional_max_poolnd.cpp
@@ -0,0 +1,254 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <limits>
+
+#include "openvino/frontend/pytorch/node_context.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/broadcast.hpp"
+#include "openvino/op/concat.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/convert_like.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/floor.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/gather_elements.hpp"
+#include "openvino/op/greater_eq.hpp"
+#include "openvino/op/maximum.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/not_equal.hpp"
+#include "openvino/op/range.hpp"
+#include "openvino/op/reduce_max.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/select.hpp"
+#include "openvino/op/shape_of.hpp"
+#include "openvino/op/slice.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/topk.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "utils.hpp"
+
+namespace ov {
+namespace frontend {
+namespace pytorch {
+namespace op {
+
+using namespace ov::op;
+
+namespace {
+
+Output<Node> get_int_list_input(const NodeContext& context, size_t idx) {
+ Output<Node> values;
+ if (context.get_input_type(idx).is<type::List>()) {
+ const auto list_elems = get_list_as_outputs(context.get_input(static_cast<int>(idx)));
+ OutputVector to_concat;
+ auto flat_shape = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {-1}));
+ for (auto elem : list_elems) {
+ if (elem.get_element_type() != element::i32) {
+ elem = context.mark_node(std::make_shared<v0::Convert>(elem, element::i32));
+ }
+ to_concat.push_back(context.mark_node(std::make_shared<v1::Reshape>(elem, flat_shape, false)));
+ }
+ values = context.mark_node(std::make_shared<v0::Concat>(to_concat, 0));
+ } else {
+ values = get_input_as_i32(context, idx);
+ }
+ auto flat_shape = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {-1}));
+ return context.mark_node(std::make_shared<v1::Reshape>(values, flat_shape, false));
+}
+
+Output<Node> generate_intervals(const NodeContext& context,
+ const Output<Node>& input_size,
+ const Output<Node>& output_size,
+ const Output<Node>& pool_size,
+ const Output<Node>& sample) {
+ auto zero = context.mark_node(v0::Constant::create(element::i32, Shape{}, {0}));
+ auto one = context.mark_node(v0::Constant::create(element::i32, Shape{}, {1}));
+
+ auto out_minus_one = context.mark_node(std::make_shared<v1::Subtract>(output_size, one));
+ auto in_minus_pool = context.mark_node(std::make_shared<v1::Subtract>(input_size, pool_size));
+ auto safe_denominator = context.mark_node(std::make_shared<v1::Maximum>(out_minus_one, one));
+
+ auto numerator = context.mark_node(std::make_shared<v1::ConvertLike>(in_minus_pool, sample));
+ auto denominator = context.mark_node(std::make_shared<v1::ConvertLike>(safe_denominator, sample));
+ auto alpha = context.mark_node(std::make_shared<v1::Divide>(numerator, denominator));
+
+ auto steps = context.mark_node(std::make_shared<v4::Range>(zero, out_minus_one, one, sample.get_element_type()));
+ auto steps_plus_sample = context.mark_node(std::make_shared<v1::Add>(steps, sample));
+ auto scaled_steps = context.mark_node(std::make_shared<v1::Multiply>(steps_plus_sample, alpha));
+ auto floor_scaled_steps = context.mark_node(std::make_shared<v0::Floor>(scaled_steps));
+ auto scaled_sample = context.mark_node(std::make_shared<v1::Multiply>(sample, alpha));
+ auto floor_scaled_sample = context.mark_node(std::make_shared<v0::Floor>(scaled_sample));
+ Output<Node> body = context.mark_node(std::make_shared<v1::Subtract>(floor_scaled_steps, floor_scaled_sample));
+ body = context.mark_node(std::make_shared<v0::Convert>(body, element::i32));
+
+ auto sample_shape = context.mark_node(std::make_shared<v3::ShapeOf>(sample, element::i32));
+ auto last = context.mark_node(std::make_shared<v3::Broadcast>(in_minus_pool, sample_shape));
+
+ return context.mark_node(std::make_shared<v0::Concat>(OutputVector{body, last}, -1));
+}
+
+std::vector<int64_t> all_axes_except(int64_t count, int64_t offset, int64_t keep) {
+ std::vector<int64_t> axes;
+ for (int64_t i = 0; i < count; ++i) {
+ if (i != keep) {
+ axes.push_back(offset + i);
+ }
+ }
+ return axes;
+}
+
+OutputVector translate_fractional_max_pool_nd(const NodeContext& context,
+ int64_t spatial_rank,
+ const std::vector<int64_t>& sample_index_per_dim) {
+ num_inputs_check(context, 4, 4);
+ auto input = context.get_input(0);
+ auto random_samples = context.get_input(3);
+
+ const auto input_rank = input.get_partial_shape().rank();
+ PYTORCH_OP_CONVERSION_CHECK(input_rank.is_static(),
+ "fractional_max_pool",
+ spatial_rank,
+ "d requires input with static rank.");
+ const auto rank = input_rank.get_length();
+ PYTORCH_OP_CONVERSION_CHECK(rank == spatial_rank + 1 || rank == spatial_rank + 2,
+ "fractional_max_pool",
+ spatial_rank,
+ "d expects input of rank ",
+ spatial_rank + 1,
+ " or ",
+ spatial_rank + 2,
+ ".");
+
+ auto zero = context.mark_node(v0::Constant::create(element::i32, Shape{}, {0}));
+ auto one = context.mark_node(v0::Constant::create(element::i32, Shape{}, {1}));
+ auto samples_axis = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {2}));
+ auto slice_step = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {1}));
+ const bool unbatched = rank == spatial_rank + 1;
+ if (unbatched) {
+ input = context.mark_node(std::make_shared<v0::Unsqueeze>(input, zero));
+ }
+
+ auto input_shape = context.mark_node(std::make_shared<v3::ShapeOf>(input, element::i32));
+ auto pool_size = get_int_list_input(context, 1);
+ auto output_size = get_int_list_input(context, 2);
+
+ OutputVector input_dims;
+ OutputVector pool_dims;
+ OutputVector output_dims;
+ for (int64_t d = 0; d < spatial_rank; ++d) {
+ auto dim_idx = context.mark_node(v0::Constant::create(element::i32, Shape{}, {d}));
+ auto input_dim_idx = context.mark_node(v0::Constant::create(element::i32, Shape{}, {d + 2}));
+ input_dims.push_back(context.mark_node(std::make_shared<v8::Gather>(input_shape, input_dim_idx, zero)));
+ pool_dims.push_back(context.mark_node(std::make_shared<v8::Gather>(pool_size, dim_idx, zero)));
+ output_dims.push_back(context.mark_node(std::make_shared<v8::Gather>(output_size, dim_idx, zero)));
+ }
+
+ OutputVector strides(spatial_rank);
+ strides[spatial_rank - 1] = one;
+ for (int64_t d = spatial_rank - 2; d >= 0; --d) {
+ strides[d] = context.mark_node(std::make_shared<v1::Multiply>(strides[d + 1], input_dims[d + 1]));
+ }
+
+ const auto expand_axes_shape = Shape{static_cast<size_t>(spatial_rank - 1)};
+ Output<Node> start_idx;
+ Output<Node> window_offsets;
+ Output<Node> window_size = one;
+ for (int64_t d = 0; d < spatial_rank; ++d) {
+ const auto sample_idx = sample_index_per_dim[d];
+ auto sample_begin = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {sample_idx}));
+ auto sample_end = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {sample_idx + 1}));
+ auto sample = context.mark_node(
+ std::make_shared<v8::Slice>(random_samples, sample_begin, sample_end, slice_step, samples_axis));
+
+ auto seq = generate_intervals(context, input_dims[d], output_dims[d], pool_dims[d], sample);
+ auto seq_scaled = context.mark_node(std::make_shared<v1::Multiply>(seq, strides[d]));
+ auto seq_axes = context.mark_node(
+ v0::Constant::create(element::i64, expand_axes_shape, all_axes_except(spatial_rank, 2, d)));
+ Output<Node> seq_expanded = context.mark_node(std::make_shared<v0::Unsqueeze>(seq_scaled, seq_axes));
+ if (d == 0) {
+ start_idx = seq_expanded;
+ } else {
+ start_idx = context.mark_node(std::make_shared<v1::Add>(start_idx, seq_expanded));
+ }
+
+ auto offsets = context.mark_node(std::make_shared<v4::Range>(zero, pool_dims[d], one, element::i32));
+ auto offsets_scaled = context.mark_node(std::make_shared<v1::Multiply>(offsets, strides[d]));
+ auto offset_axes = context.mark_node(
+ v0::Constant::create(element::i64, expand_axes_shape, all_axes_except(spatial_rank, 0, d)));
+ Output<Node> offsets_expanded = context.mark_node(std::make_shared<v0::Unsqueeze>(offsets_scaled, offset_axes));
+ if (d == 0) {
+ window_offsets = offsets_expanded;
+ } else {
+ window_offsets = context.mark_node(std::make_shared<v1::Add>(window_offsets, offsets_expanded));
+ }
+ window_size = context.mark_node(std::make_shared<v1::Multiply>(window_size, pool_dims[d]));
+ }
+
+ auto flat_window_shape = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {-1}));
+ window_offsets = context.mark_node(std::make_shared<v1::Reshape>(window_offsets, flat_window_shape, false));
+
+ auto window_axis = context.mark_node(v0::Constant::create(element::i32, Shape{}, {spatial_rank + 2}));
+ auto start_idx_expanded = context.mark_node(std::make_shared<v0::Unsqueeze>(start_idx, window_axis));
+ auto gather_indices = context.mark_node(std::make_shared<v1::Add>(start_idx_expanded, window_offsets));
+
+ auto flat_input_shape = context.mark_node(v0::Constant::create(element::i32, Shape{3}, {0, 0, -1}));
+ auto input_flat = context.mark_node(std::make_shared<v1::Reshape>(input, flat_input_shape, true));
+ auto flat_axis = context.mark_node(v0::Constant::create(element::i32, Shape{}, {2}));
+ auto windows = context.mark_node(std::make_shared<v8::Gather>(input_flat, gather_indices, flat_axis, 2));
+
+ auto is_nan = context.mark_node(std::make_shared<v1::NotEqual>(windows, windows));
+ auto neg_inf =
+ context.mark_node(v0::Constant::create(element::f32, Shape{}, {-std::numeric_limits<float>::infinity()}));
+ auto neg_inf_like = context.mark_node(std::make_shared<v1::ConvertLike>(neg_inf, windows));
+ auto windows_no_nan = context.mark_node(std::make_shared<v1::Select>(is_nan, neg_inf_like, windows));
+
+ auto k = context.mark_node(v0::Constant::create(element::i32, Shape{}, {1}));
+ auto topk = context.mark_node(std::make_shared<v11::TopK>(windows_no_nan,
+ k,
+ -1,
+ v11::TopK::Mode::MAX,
+ v11::TopK::SortType::SORT_VALUES,
+ element::i32,
+ true));
+
+ auto positions = context.mark_node(std::make_shared<v4::Range>(zero, window_size, one, element::i32));
+ auto no_position = context.mark_node(v0::Constant::create(element::i32, Shape{}, {-1}));
+ auto nan_positions = context.mark_node(std::make_shared<v1::Select>(is_nan, positions, no_position));
+ auto reduce_axis = context.mark_node(v0::Constant::create(element::i32, Shape{1}, {-1}));
+ auto last_nan = context.mark_node(std::make_shared<v1::ReduceMax>(nan_positions, reduce_axis, true));
+ auto has_nan = context.mark_node(std::make_shared<v1::GreaterEqual>(last_nan, zero));
+ auto local_idx = context.mark_node(std::make_shared<v1::Select>(has_nan, last_nan, topk->output(1)));
+
+ auto pooled = context.mark_node(std::make_shared<v6::GatherElements>(windows, local_idx, -1));
+ auto local_offset = context.mark_node(std::make_shared<v8::Gather>(window_offsets, local_idx, zero));
+ auto indices = context.mark_node(std::make_shared<v1::Add>(start_idx_expanded, local_offset));
+
+ Output<Node> pooled_out = context.mark_node(std::make_shared<v0::Squeeze>(pooled, window_axis));
+ Output<Node> indices_out = context.mark_node(std::make_shared<v0::Squeeze>(indices, window_axis));
+ indices_out = context.mark_node(std::make_shared<v0::Convert>(indices_out, element::i64));
+
+ if (unbatched) {
+ pooled_out = context.mark_node(std::make_shared<v0::Squeeze>(pooled_out, zero));
+ indices_out = context.mark_node(std::make_shared<v0::Squeeze>(indices_out, zero));
+ }
+ return {pooled_out, indices_out};
+}
+
+} // namespace
+
+OutputVector translate_fractional_max_pool2d(const NodeContext& context) {
+ return translate_fractional_max_pool_nd(context, 2, {1, 0});
+}
+
+OutputVector translate_fractional_max_pool3d(const NodeContext& context) {
+ return translate_fractional_max_pool_nd(context, 3, {0, 1, 2});
+}
+
+} // 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 9147e554157..1844bd78a81 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -24,6 +24,8 @@ OP_CONVERTER(translate_adaptive_avg_pool2d);
OP_CONVERTER(translate_adaptive_avg_pool1d);
OP_CONVERTER(translate_adaptive_max_pool3d);
OP_CONVERTER(translate_adaptive_max_pool2d);
+OP_CONVERTER(translate_fractional_max_pool2d);
+OP_CONVERTER(translate_fractional_max_pool3d);
OP_CONVERTER(translate_adaptive_max_pool1d);
OP_CONVERTER(translate_add);
OP_CONVERTER(translate_add_);
@@ -419,6 +421,8 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::adaptive_max_pool1d", op::quantizable_op<op::translate_adaptive_max_pool1d>},
{"aten::adaptive_max_pool2d", op::quantizable_op<op::translate_adaptive_max_pool2d>},
{"aten::adaptive_max_pool3d", op::quantizable_op<op::translate_adaptive_max_pool3d>},
+ {"aten::fractional_max_pool2d", op::quantizable_op<op::translate_fractional_max_pool2d>},
+ {"aten::fractional_max_pool3d", op::quantizable_op<op::translate_fractional_max_pool3d>},
{"aten::add", op::translate_add},
{"aten::add_", op::translate_add_},
{"aten::addcmul", op::translate_addcmul},
diff --git a/tests/layer_tests/pytorch_tests/test_fractional_max_pool2d.py b/tests/layer_tests/pytorch_tests/test_fractional_max_pool2d.py
new file mode 100644
index 00000000000..db650ea6e17
--- /dev/null
+++ b/tests/layer_tests/pytorch_tests/test_fractional_max_pool2d.py
@@ -0,0 +1,106 @@
+# Copyright (C) 2018-2026 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import pytest
+import torch
+import torch.nn.functional as F
+
+from pytorch_layer_test_class import PytorchLayerTest
+
+
+def inject_edge_values(tensor):
+ flat = tensor.reshape(-1)
+ flat[0] = 5.0
+ flat[1] = 5.0
+ flat[2] = float('nan')
+ flat[3] = -10.0
+ return tensor
+
+
+class aten_fractional_max_pool(torch.nn.Module):
+ def __init__(self, pool_fn, kernel_size, output_size, return_indices, samples_shape) -> None:
+ super().__init__()
+ self.pool_fn = pool_fn
+ self.kernel_size = kernel_size
+ self.output_size = output_size
+ self.return_indices = return_indices
+ gen = torch.Generator().manual_seed(42)
+ self.random_samples = torch.rand(*samples_shape, generator=gen)
+
+ def forward(self, input_tensor):
+ return self.pool_fn(input_tensor, self.kernel_size, output_size=self.output_size,
+ return_indices=self.return_indices, _random_samples=self.random_samples)
+
+
+class TestFractionalMaxPool2D(PytorchLayerTest):
+
+ def _prepare_input(self):
+ return (self.input_tensor,)
+
+ def create_model(self, kernel_size, output_size, return_indices):
+ unbatched = self.input_tensor.ndim == 3
+ batch = 1 if unbatched else self.input_tensor.shape[0]
+ channels = self.input_tensor.shape[-3]
+ model = aten_fractional_max_pool(F.fractional_max_pool2d, kernel_size, output_size, return_indices,
+ (batch, channels, 2))
+ return model, "aten::fractional_max_pool2d"
+
+ @pytest.mark.parametrize('input_shape,kernel_size,output_size', [
+ ([2, 1, 4, 4], [2, 2], [2, 2]),
+ ([2, 1, 4, 4], [2, 2], [3, 3]),
+ ([1, 3, 32, 32], [3, 3], [4, 4]),
+ ([1, 3, 32, 32], [2, 3], [16, 10]),
+ ([3, 32, 32], [2, 2], [4, 4]),
+ ([1, 3, 12, 14], [2, 3], [3, 4]),
+ ([1, 3, 12, 14], [3, 2], [1, 1]),
+ ([1, 3, 12, 14], [3, 3], [10, 12]),
+ ])
+ @pytest.mark.parametrize('return_indices', [False, True])
+ @pytest.mark.parametrize('edge_values', [False, True])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.precommit_fx_backend
+ def test_fractional_max_pool2d(self, ie_device, precision, ir_version, input_shape, kernel_size, output_size,
+ return_indices, edge_values):
+ self.input_tensor = self.random.randn(*input_shape)
+ if edge_values:
+ inject_edge_values(self.input_tensor)
+ self._test(*self.create_model(kernel_size, output_size, return_indices),
+ ie_device, precision, ir_version, trace_model=True)
+
+
+class TestFractionalMaxPool3D(PytorchLayerTest):
+
+ def _prepare_input(self):
+ return (self.input_tensor,)
+
+ def create_model(self, kernel_size, output_size, return_indices):
+ unbatched = self.input_tensor.ndim == 4
+ batch = 1 if unbatched else self.input_tensor.shape[0]
+ channels = self.input_tensor.shape[-4]
+ model = aten_fractional_max_pool(F.fractional_max_pool3d, kernel_size, output_size, return_indices,
+ (batch, channels, 3))
+ return model, "aten::fractional_max_pool3d"
+
+ @pytest.mark.parametrize('input_shape,kernel_size,output_size', [
+ ([2, 1, 4, 4, 4], [2, 2, 2], [2, 2, 2]),
+ ([2, 1, 5, 5, 5], [2, 2, 2], [3, 3, 3]),
+ ([1, 3, 10, 12, 14], [2, 3, 2], [2, 3, 4]),
+ ([1, 3, 10, 12, 14], [3, 3, 3], [4, 4, 4]),
+ ([1, 3, 10, 12, 14], [2, 2, 2], [1, 1, 1]),
+ ([3, 8, 8, 8], [2, 2, 2], [4, 4, 4]),
+ ])
+ @pytest.mark.parametrize('return_indices', [False, True])
+ @pytest.mark.parametrize('edge_values', [False, True])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ @pytest.mark.precommit_fx_backend
+ def test_fractional_max_pool3d(self, ie_device, precision, ir_version, input_shape, kernel_size, output_size,
+ return_indices, edge_values):
+ self.input_tensor = self.random.randn(*input_shape)
+ if edge_values:
+ inject_edge_values(self.input_tensor)
+ self._test(*self.create_model(kernel_size, output_size, return_indices),
+ ie_device, precision, ir_version, trace_model=True)