Skip to content

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

[ONNX] Support GridSample opset 20+ (#36070)

Maxim Vafin9a6179336b78
diff --git a/docs/articles_en/documentation/compatibility-and-support/supported-operations.rst b/docs/articles_en/documentation/compatibility-and-support/supported-operations.rst
index 346724fb029..9836132766d 100644
--- a/docs/articles_en/documentation/compatibility-and-support/supported-operations.rst
+++ b/docs/articles_en/documentation/compatibility-and-support/supported-operations.rst
@@ -141,7 +141,7 @@ Data as of OpenVINO 2025.4.1 (December 18, 2025).
GlobalMaxPool
Greater
GreaterOrEqual
- GridSample
+ GridSample Only 4D input tensors are supported.
GroupNormalization
GRU
HammingWindow
diff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.md
index 7ebd331a0c7..2f15ce9d6f3 100644
--- a/src/frontends/onnx/docs/supported_ops.md
+++ b/src/frontends/onnx/docs/supported_ops.md
@@ -78,7 +78,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)
| |GlobalMaxPool |1 |22, 1 | |
| |Greater |1 |13, 9, 7, 1 | |
| |GreaterOrEqual |16, 12 |16, 12 | |
-| |GridSample |16 |22, 20, 16 | |
+| |GridSample |20, 16 |22, 20, 16 | |
| |GroupNormalization |21, 18 |21, 18 | |
| |HammingWindow |17 |17 | |
| |HannWindow |17 |17 | |
diff --git a/src/frontends/onnx/frontend/src/op/grid_sample.cpp b/src/frontends/onnx/frontend/src/op/grid_sample.cpp
index 2a05e720ad2..967f6913f3a 100644
--- a/src/frontends/onnx/frontend/src/op/grid_sample.cpp
+++ b/src/frontends/onnx/frontend/src/op/grid_sample.cpp
@@ -5,30 +5,64 @@
#include "openvino/op/grid_sample.hpp"
#include "core/operator_set.hpp"
+#include "exceptions.hpp"
using namespace ov::op;
namespace ov {
namespace frontend {
namespace onnx {
namespace ai_onnx {
-namespace opset_1 {
-ov::OutputVector grid_sample(const ov::frontend::onnx::Node& node) {
+namespace {
+ov::OutputVector grid_sample_impl(const ov::frontend::onnx::Node& node,
+ const std::unordered_map<std::string, v9::GridSample::InterpolationMode>& mode_map,
+ const std::string& default_mode) {
const auto data = node.get_ov_inputs().at(0);
const auto grid = node.get_ov_inputs().at(1);
+ const auto& data_rank = data.get_partial_shape().rank();
+ CHECK_VALID_NODE(node,
+ data_rank.is_dynamic() || data_rank.get_length() == 4,
+ "GridSample is only supported for 4D input tensors. Got rank: ",
+ data_rank.get_length());
+
v9::GridSample::Attributes attributes{};
attributes.align_corners = node.get_attribute_value<int64_t>("align_corners", 0);
- attributes.mode = ov::EnumNames<v9::GridSample::InterpolationMode>::as_enum(
- node.get_attribute_value<std::string>("mode", "bilinear"));
+ const auto mode = node.get_attribute_value<std::string>("mode", default_mode);
+ const auto mode_it = mode_map.find(mode);
+ CHECK_VALID_NODE(node, mode_it != mode_map.end(), "Unsupported GridSample mode: ", mode);
+ attributes.mode = mode_it->second;
attributes.padding_mode = ov::EnumNames<v9::GridSample::PaddingMode>::as_enum(
node.get_attribute_value<std::string>("padding_mode", "zeros"));
return {std::make_shared<v9::GridSample>(data, grid, attributes)};
}
-ONNX_OP("GridSample", OPSET_SINCE(1), ai_onnx::opset_1::grid_sample);
+} // namespace
+
+namespace opset_1 {
+ov::OutputVector grid_sample(const ov::frontend::onnx::Node& node) {
+ static const std::unordered_map<std::string, v9::GridSample::InterpolationMode> mode_map{
+ {"bilinear", v9::GridSample::InterpolationMode::BILINEAR},
+ {"bicubic", v9::GridSample::InterpolationMode::BICUBIC},
+ {"nearest", v9::GridSample::InterpolationMode::NEAREST},
+ };
+ return grid_sample_impl(node, mode_map, "bilinear");
+}
+ONNX_OP("GridSample", OPSET_RANGE(1, 19), ai_onnx::opset_1::grid_sample);
} // namespace opset_1
+
+namespace opset_20 {
+ov::OutputVector grid_sample(const ov::frontend::onnx::Node& node) {
+ static const std::unordered_map<std::string, v9::GridSample::InterpolationMode> mode_map{
+ {"linear", v9::GridSample::InterpolationMode::BILINEAR},
+ {"cubic", v9::GridSample::InterpolationMode::BICUBIC},
+ {"nearest", v9::GridSample::InterpolationMode::NEAREST},
+ };
+ return grid_sample_impl(node, mode_map, "linear");
+}
+ONNX_OP("GridSample", OPSET_SINCE(20), ai_onnx::opset_20::grid_sample);
+} // namespace opset_20
} // namespace ai_onnx
} // namespace onnx
} // namespace frontend
diff --git a/src/frontends/onnx/tests/__init__.py b/src/frontends/onnx/tests/__init__.py
index 25aeb8191c8..052be60f804 100644
--- a/src/frontends/onnx/tests/__init__.py
+++ b/src/frontends/onnx/tests/__init__.py
@@ -151,7 +151,7 @@ xfail_issue_119926 = xfail_test(reason="ROIAlign AssertionError: Not equal to to
# ONNX 1.15
xfail_issue_125485 = xfail_test(reason="AffineGrid operation is not supported")
xfail_issue_125488 = xfail_test(reason="ImageDecoder operation is not supported")
-skip_issue_125487 = pytest.mark.skip(reason="GridSample doesn't support cubic and linear modes, and 4D tensor") # Need to enable after bumping to 1.15
+skip_issue_125487 = pytest.mark.skip(reason="GridSample doesn't support volumetric (5D) inputs")
skip_issue_125489 = pytest.mark.skip(reason="IsInf changed behavior since opset-20") # Need to enable after opset-20 will be released
skip_issue_124587 = pytest.mark.skip(reason="Fail on new macos machines")
xfail_issue_125491 = xfail_test(reason="AveragePool mismatch with differences in shapes")
diff --git a/src/frontends/onnx/tests/models/grid_sample_opset20.prototxt b/src/frontends/onnx/tests/models/grid_sample_opset20.prototxt
new file mode 100644
index 00000000000..23df2945567
--- /dev/null
+++ b/src/frontends/onnx/tests/models/grid_sample_opset20.prototxt
@@ -0,0 +1,95 @@
+ir_version: 9
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "X"
+ input: "Grid"
+ output: "Y"
+ op_type: "GridSample"
+ attribute {
+ name: "align_corners"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "mode"
+ s: "linear"
+ type: STRING
+ }
+ attribute {
+ name: "padding_mode"
+ s: "zeros"
+ type: STRING
+ }
+ }
+ name: "test_gridsample_opset20"
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "Grid"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 6
+ }
+ dim {
+ dim_value: 6
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 6
+ }
+ dim {
+ dim_value: 6
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 20
+}
diff --git a/src/frontends/onnx/tests/models/grid_sample_opset20_5d.prototxt b/src/frontends/onnx/tests/models/grid_sample_opset20_5d.prototxt
new file mode 100644
index 00000000000..dac4a15ba70
--- /dev/null
+++ b/src/frontends/onnx/tests/models/grid_sample_opset20_5d.prototxt
@@ -0,0 +1,104 @@
+ir_version: 9
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "X"
+ input: "Grid"
+ output: "Y"
+ op_type: "GridSample"
+ attribute {
+ name: "align_corners"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "mode"
+ s: "linear"
+ type: STRING
+ }
+ attribute {
+ name: "padding_mode"
+ s: "zeros"
+ type: STRING
+ }
+ }
+ name: "test_gridsample_opset20_5d"
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "Grid"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 3
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 20
+}
diff --git a/src/frontends/onnx/tests/models/grid_sample_opset20_cubic.prototxt b/src/frontends/onnx/tests/models/grid_sample_opset20_cubic.prototxt
new file mode 100644
index 00000000000..fcb4aef9367
--- /dev/null
+++ b/src/frontends/onnx/tests/models/grid_sample_opset20_cubic.prototxt
@@ -0,0 +1,95 @@
+ir_version: 9
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "X"
+ input: "Grid"
+ output: "Y"
+ op_type: "GridSample"
+ attribute {
+ name: "align_corners"
+ i: 0
+ type: INT
+ }
+ attribute {
+ name: "mode"
+ s: "cubic"
+ type: STRING
+ }
+ attribute {
+ name: "padding_mode"
+ s: "zeros"
+ type: STRING
+ }
+ }
+ name: "test_gridsample_opset20_cubic"
+ input {
+ name: "X"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ input {
+ name: "Grid"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 6
+ }
+ dim {
+ dim_value: 6
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "Y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 6
+ }
+ dim {
+ dim_value: 6
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 20
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index a5350e9d5fc..005458aeb37 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -6188,6 +6188,60 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_grid_sample) {
test_case.run();
}
+OPENVINO_TEST(${BACKEND_NAME}, onnx_grid_sample_opset20_linear) {
+ const auto model = convert_model("grid_sample_opset20.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>(Shape{1, 1, 4, 4}, gen_range<float>(16));
+ test_case.add_input<float>(
+ Shape{1, 6, 6, 2},
+ {-1.0000f, -1.0000f, -0.6000f, -1.0000f, -0.2000f, -1.0000f, 0.2000f, -1.0000f, 0.6000f, -1.0000f, 1.0000f,
+ -1.0000f, -1.0000f, -0.6000f, -0.6000f, -0.6000f, -0.2000f, -0.6000f, 0.2000f, -0.6000f, 0.6000f, -0.6000f,
+ 1.0000f, -0.6000f, -1.0000f, -0.2000f, -0.6000f, -0.2000f, -0.2000f, -0.2000f, 0.2000f, -0.2000f, 0.6000f,
+ -0.2000f, 1.0000f, -0.2000f, -1.0000f, 0.2000f, -0.6000f, 0.2000f, -0.2000f, 0.2000f, 0.2000f, 0.2000f,
+ 0.6000f, 0.2000f, 1.0000f, 0.2000f, -1.0000f, 0.6000f, -0.6000f, 0.6000f, -0.2000f, 0.6000f, 0.2000f,
+ 0.6000f, 0.6000f, 0.6000f, 1.0000f, 0.6000f, -1.0000f, 1.0000f, -0.6000f, 1.0000f, -0.2000f, 1.0000f,
+ 0.2000f, 1.0000f, 0.6000f, 1.0000f, 1.0000f, 1.0000});
+
+ test_case.add_expected_output<float>(
+ Shape{1, 1, 6, 6},
+ {0.0000f, 0.1500f, 0.5500f, 0.9500f, 1.3500f, 0.7500f, 0.6000f, 1.5000f, 2.3000f,
+ 3.1000f, 3.9000f, 2.1000f, 2.2000f, 4.7000f, 5.5000f, 6.3000f, 7.1000f, 3.7000f,
+ 3.8000f, 7.9000f, 8.7000f, 9.5000f, 10.3000f, 5.3000f, 5.4000f, 11.1000f, 11.9000f,
+ 12.7000f, 13.5000f, 6.9000f, 3.0000f, 6.1500f, 6.5500f, 6.9500f, 7.3500f, 3.7500});
+
+ test_case.run();
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_grid_sample_opset20_cubic) {
+ const auto model = convert_model("grid_sample_opset20_cubic.onnx");
+
+ auto test_case = ov::test::TestCase(model, s_device);
+ test_case.add_input<float>(Shape{1, 1, 4, 4}, gen_range<float>(16));
+ test_case.add_input<float>(
+ Shape{1, 6, 6, 2},
+ {-1.0000f, -1.0000f, -0.6000f, -1.0000f, -0.2000f, -1.0000f, 0.2000f, -1.0000f, 0.6000f, -1.0000f, 1.0000f,
+ -1.0000f, -1.0000f, -0.6000f, -0.6000f, -0.6000f, -0.2000f, -0.6000f, 0.2000f, -0.6000f, 0.6000f, -0.6000f,
+ 1.0000f, -0.6000f, -1.0000f, -0.2000f, -0.6000f, -0.2000f, -0.2000f, -0.2000f, 0.2000f, -0.2000f, 0.6000f,
+ -0.2000f, 1.0000f, -0.2000f, -1.0000f, 0.2000f, -0.6000f, 0.2000f, -0.2000f, 0.2000f, 0.2000f, 0.2000f,
+ 0.6000f, 0.2000f, 1.0000f, 0.2000f, -1.0000f, 0.6000f, -0.6000f, 0.6000f, -0.2000f, 0.6000f, 0.2000f,
+ 0.6000f, 0.6000f, 0.6000f, 1.0000f, 0.6000f, -1.0000f, 1.0000f, -0.6000f, 1.0000f, -0.2000f, 1.0000f,
+ 0.2000f, 1.0000f, 0.6000f, 1.0000f, 1.0000f, 1.0000});
+
+ test_case.add_expected_output<float>(
+ Shape{1, 1, 6, 6},
+ {-0.2344f, -0.3005f, 0.1930f, 0.5570f, 1.1332f, 0.6094f, 0.3594f, 1.2865f, 2.1882f,
+ 2.9965f, 4.4699f, 2.2330f, 2.1783f, 5.2767f, 5.6800f, 6.4080f, 8.1440f, 3.8658f,
+ 3.6343f, 8.5098f, 8.5920f, 9.3200f, 11.3770f, 5.3218f, 6.0939f, 14.0200f, 13.6572f,
+ 14.4655f, 17.2033f, 7.9675f, 3.1406f, 7.1937f, 6.9430f, 7.3070f, 8.6273f, 3.9844f});
+
+ test_case.run_with_tolerance_as_fp(1.0e-3f);
+}
+
+OPENVINO_TEST(${BACKEND_NAME}, onnx_grid_sample_opset20_5d_input_rejected) {
+ EXPECT_THROW(convert_model("grid_sample_opset20_5d.onnx"), ov::Exception);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_concat_empty_init) {
const auto model = convert_model("concat_empty_init.onnx");
diff --git a/src/frontends/onnx/tests/tests_python/test_backend.py b/src/frontends/onnx/tests/tests_python/test_backend.py
index 2837365d3af..a74708de462 100644
--- a/src/frontends/onnx/tests/tests_python/test_backend.py
+++ b/src/frontends/onnx/tests/tests_python/test_backend.py
@@ -523,14 +523,6 @@ tests_expected_to_fail = [
),
(
skip_issue_125487,
- "OnnxBackendNodeModelTest.test_gridsample_aligncorners_true_cpu",
- "OnnxBackendNodeModelTest.test_gridsample_bicubic_align_corners_0_additional_1_cpu",
- "OnnxBackendNodeModelTest.test_gridsample_bicubic_align_corners_1_additional_1_cpu",
- "OnnxBackendNodeModelTest.test_gridsample_bicubic_cpu",
- "OnnxBackendNodeModelTest.test_gridsample_bilinear_align_corners_0_additional_1_cpu",
- "OnnxBackendNodeModelTest.test_gridsample_bilinear_align_corners_1_additional_1_cpu",
- "OnnxBackendNodeModelTest.test_gridsample_bilinear_cpu",
- "OnnxBackendNodeModelTest.test_gridsample_cpu",
"OnnxBackendNodeModelTest.test_gridsample_volumetric_bilinear_align_corners_0_cpu",
"OnnxBackendNodeModelTest.test_gridsample_volumetric_bilinear_align_corners_1_cpu",
"OnnxBackendNodeModelTest.test_gridsample_volumetric_nearest_align_corners_0_cpu",