Skip to content

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

[CPU][ARM] GridSample decomposition on ARM (#31591)

Nesterov Alexanderce524976e13a
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/arm/pass/grid_sample_decomposition.cpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/arm/pass/grid_sample_decomposition.cpp
new file mode 100644
index 00000000000..259f96e4387
--- /dev/null
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/arm/pass/grid_sample_decomposition.cpp
@@ -0,0 +1,855 @@
+// Copyright (C) 2018-2025 Intel
+// SPDX-License-Identifier: Apache-2.0
+//
+// GridSample decomposition implemented with a single unified builder.
+// - No use of ov::reference::* (explicitly forbidden).
+// - Shapes are extracted via ShapeOf/Gather wrapped in make_try_fold, so static
+// shapes fold to Constants, while dynamic shapes remain symbolic.
+// - Three interpolation modes are supported: NEAREST, BILINEAR, BICUBIC.
+// - Padding modes: ZEROS / BORDER / REFLECTION. align_corners is respected.
+//
+
+#include "transformations/cpu_opset/arm/pass/grid_sample_decomposition.hpp"
+
+#include <cstddef>
+#include <functional>
+#include <memory>
+#include <unordered_set>
+#include <vector>
+
+#include "openvino/core/graph_util.hpp"
+#include "openvino/core/node.hpp"
+#include "openvino/core/rt_info.hpp"
+#include "openvino/core/type/element_type.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/op/abs.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/divide.hpp"
+#include "openvino/op/equal.hpp"
+#include "openvino/op/floor.hpp"
+#include "openvino/op/floor_mod.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/gather_nd.hpp"
+#include "openvino/op/greater_eq.hpp"
+#include "openvino/op/grid_sample.hpp"
+#include "openvino/op/less.hpp"
+#include "openvino/op/logical_and.hpp"
+#include "openvino/op/maximum.hpp"
+#include "openvino/op/minimum.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/negative.hpp"
+#include "openvino/op/range.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/round.hpp"
+#include "openvino/op/select.hpp"
+#include "openvino/op/shape_of.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "openvino/pass/matcher_pass.hpp"
+#include "openvino/pass/pattern/matcher.hpp"
+#include "openvino/pass/pattern/op/label.hpp"
+#include "openvino/pass/pattern/op/pattern.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations/utils/utils.hpp"
+#include "utils/general_utils.h"
+
+namespace ov::intel_cpu {
+
+// ============================================================================
+// INTERNAL SHARED: graph-level builders (reused by both static & dynamic paths)
+// ============================================================================
+
+namespace {
+
+struct Ctx {
+ // Scalars with sizes (might be Constants in static path or ShapeOf-slices in dynamic)
+ std::shared_ptr<Node> n_dim = nullptr;
+ std::shared_ptr<Node> c_dim = nullptr;
+ std::shared_ptr<Node> h_in = nullptr;
+ std::shared_ptr<Node> w_in = nullptr;
+ std::shared_ptr<Node> h_out = nullptr;
+ std::shared_ptr<Node> w_out = nullptr;
+ std::shared_ptr<Node> grid_shape = nullptr; // 1D shape of grid [N, H_out, W_out, 2]
+ // Types
+ element::Type element_type = element::dynamic;
+ element::Type calc_type = element::dynamic; // set to f32 for math
+ // Float constants
+ std::shared_ptr<Node> c0 = nullptr;
+ std::shared_ptr<Node> c1 = nullptr;
+ std::shared_ptr<Node> c2 = nullptr;
+ std::shared_ptr<Node> c0_5 = nullptr;
+ // Integer constants
+ std::shared_ptr<Node> i32_0 = nullptr;
+ std::shared_ptr<Node> i32_1 = nullptr;
+ std::shared_ptr<Node> i32_2 = nullptr;
+ std::shared_ptr<Node> i32_3 = nullptr;
+ std::shared_ptr<Node> i32_neg1 = nullptr;
+ // Common axes
+ std::shared_ptr<Node> axis_0 = nullptr;
+ std::shared_ptr<Node> axis_3 = nullptr;
+ std::shared_ptr<Node> unsqueeze_axis_0 = nullptr;
+ std::shared_ptr<Node> unsqueeze_axis_neg1 = nullptr;
+ std::shared_ptr<Node> unsqueeze_axis_3 = nullptr; // for [N,H,W] -> [N,H,W,1]
+ // NHWC transpose pattern
+ std::shared_ptr<Node> to_nhwc_perm = nullptr;
+ // Pixels coords after normalization
+ std::shared_ptr<Node> x = nullptr;
+ std::shared_ptr<Node> y = nullptr;
+ // Normalized coords (from grid, in [-1, 1])
+ std::shared_ptr<Node> x_norm = nullptr;
+ std::shared_ptr<Node> y_norm = nullptr;
+ // Data as NHWC for GatherND
+ std::shared_ptr<Node> data_nhwc = nullptr;
+ // Original data (NCHW)
+ std::shared_ptr<Node> data_nchw = nullptr;
+};
+
+// Problematic NEAREST modes that should keep original op or require special handling
+inline bool is_nearest_problematic(const op::v9::GridSample::Attributes& attrs) {
+ using IM = op::v9::GridSample::InterpolationMode;
+ using PM = op::v9::GridSample::PaddingMode;
+ return attrs.mode == IM::NEAREST && !attrs.align_corners &&
+ any_of(attrs.padding_mode, PM::REFLECTION, PM::BORDER, PM::ZEROS);
+}
+
+// ---- small helpers ----
+std::shared_ptr<Node> to_f32(const std::shared_ptr<Node>& input_node) {
+ return std::make_shared<op::v0::Convert>(input_node, element::f32);
+}
+
+// Helper: run original GridSample in f32 and convert result back to original dtype
+std::shared_ptr<Node> grid_sample_in_f32_and_back(const std::shared_ptr<op::v9::GridSample>& gs) {
+ auto attrs = gs->get_attributes();
+ auto data_f32 = std::make_shared<op::v0::Convert>(gs->input_value(0), element::f32);
+ auto grid_f32 = std::make_shared<op::v0::Convert>(gs->input_value(1), element::f32);
+ auto gs_f32 = std::make_shared<op::v9::GridSample>(data_f32, grid_f32, attrs);
+ return std::make_shared<op::v0::Convert>(gs_f32, gs->get_output_element_type(0));
+}
+
+// Copy runtime info from the original node to the replacement subgraph feeding `output`.
+// Traversal stops at the inputs of `from` to avoid propagating RT info into
+// pre-existing producers (e.g., upstream Parameters/Constants or other nodes
+// that must not be modified by this pass).
+void copy_rt_to_subgraph(const std::shared_ptr<Node>& from, const std::shared_ptr<Node>& output) {
+ // Treat direct inputs of `from` as graph traversal boundaries
+ std::unordered_set<const Node*> boundaries;
+ for (size_t i = 0; i < from->get_input_size(); ++i) {
+ auto in = from->input_value(i).get_node_shared_ptr();
+ if (in) {
+ boundaries.insert(in.get());
+ }
+ }
+
+ std::vector<std::shared_ptr<Node>> stack;
+ stack.push_back(output);
+ std::unordered_set<const Node*> visited;
+ std::vector<std::shared_ptr<Node>> subgraph_nodes;
+ while (!stack.empty()) {
+ auto node = stack.back();
+ stack.pop_back();
+ if (!node) {
+ continue;
+ }
+ // Do not step across or modify boundary nodes
+ if (boundaries.count(node.get()) > 0) {
+ continue;
+ }
+ if (visited.count(node.get()) > 0) {
+ continue;
+ }
+ visited.insert(node.get());
+ subgraph_nodes.push_back(node);
+ for (const auto& input : node->inputs()) {
+ auto src = input.get_source_output().get_node_shared_ptr();
+ if (src) {
+ stack.push_back(src);
+ }
+ }
+ }
+ ov::copy_runtime_info(NodeVector{from}, subgraph_nodes);
+}
+
+// Normalize grid [-1, 1] to pixel coordinates (no padding/reflection here)
+void normalize_grid_to_pixels(const Ctx& ctx,
+ const std::shared_ptr<Node>& x_grid_coord,
+ const std::shared_ptr<Node>& y_grid_coord,
+ const std::shared_ptr<Node>& width_in_float,
+ const std::shared_ptr<Node>& height_in_float,
+ bool align_corners,
+ std::shared_ptr<Node>& x_pixel_out,
+ std::shared_ptr<Node>& y_pixel_out) {
+ if (align_corners) {
+ auto w_m1 = std::make_shared<op::v1::Subtract>(width_in_float, ctx.c1);
+ auto h_m1 = std::make_shared<op::v1::Subtract>(height_in_float, ctx.c1);
+ x_pixel_out = std::make_shared<op::v1::Multiply>(
+ std::make_shared<op::v1::Divide>(std::make_shared<op::v1::Add>(x_grid_coord, ctx.c1), ctx.c2),
+ w_m1);
+ y_pixel_out = std::make_shared<op::v1::Multiply>(
+ std::make_shared<op::v1::Divide>(std::make_shared<op::v1::Add>(y_grid_coord, ctx.c1), ctx.c2),
+ h_m1);
+ } else {
+ // ((t + 1) * size - 1) / 2
+ auto x_p1_w =
+ std::make_shared<op::v1::Multiply>(std::make_shared<op::v1::Add>(x_grid_coord, ctx.c1), width_in_float);
+ auto y_p1_h =
+ std::make_shared<op::v1::Multiply>(std::make_shared<op::v1::Add>(y_grid_coord, ctx.c1), height_in_float);
+ x_pixel_out = std::make_shared<op::v1::Divide>(std::make_shared<op::v1::Subtract>(x_p1_w, ctx.c1), ctx.c2);
+ y_pixel_out = std::make_shared<op::v1::Divide>(std::make_shared<op::v1::Subtract>(y_p1_h, ctx.c1), ctx.c2);
+ }
+}
+
+// Create batch indices tensor of shape [N,H_out,W_out] (int32)
+std::shared_ptr<Node> create_batch_indices(const Ctx& ctx) {
+ // range(0, N)
+ auto range = std::make_shared<op::v4::Range>(ctx.i32_0, ctx.n_dim, ctx.i32_1, element::i32);
+ // reshape to [N,1,1]
+ auto n_dim_1d_bs = std::make_shared<op::v0::Unsqueeze>(ctx.n_dim, ctx.i32_0);
+ auto batch_shape = std::make_shared<op::v0::Concat>(OutputVector{n_dim_1d_bs,
+ op::v0::Constant::create(element::i32, {1}, {1}),
+ op::v0::Constant::create(element::i32, {1}, {1})},
+ 0);
+ auto batch_indices = std::make_shared<op::v1::Reshape>(range, batch_shape, false);
+ // broadcast to [N,H_out,W_out]
+ auto n_dim_1d = std::make_shared<op::v0::Unsqueeze>(ctx.n_dim, ctx.i32_0);
+ auto h_out_1d = std::make_shared<op::v0::Unsqueeze>(ctx.h_out, ctx.i32_0);
+ auto w_out_1d = std::make_shared<op::v0::Unsqueeze>(ctx.w_out, ctx.i32_0);
+ auto bshape = std::make_shared<op::v0::Concat>(OutputVector{n_dim_1d, h_out_1d, w_out_1d}, 0);
+ auto bcast = std::make_shared<op::v3::Broadcast>(batch_indices, bshape);
+ return std::make_shared<op::v0::Convert>(bcast, element::i32);
+}
+
+// Gather from NHWC by explicit (b, y, x) indices with batch_dims=0
+std::shared_ptr<Node> gather_hw_nhwc(const Ctx& ctx,
+ const std::shared_ptr<Node>& x_coord,
+ const std::shared_ptr<Node>& y_coord) {
+ auto x_i32 = std::make_shared<op::v0::Convert>(x_coord, element::i32);
+ auto y_i32 = std::make_shared<op::v0::Convert>(y_coord, element::i32);
+ auto bidx = create_batch_indices(ctx);
+
+ auto b = std::make_shared<op::v0::Unsqueeze>(bidx, ctx.unsqueeze_axis_neg1);
+ auto yy = std::make_shared<op::v0::Unsqueeze>(y_i32, ctx.unsqueeze_axis_neg1);
+ auto xx = std::make_shared<op::v0::Unsqueeze>(x_i32, ctx.unsqueeze_axis_neg1);
+ auto indices = std::make_shared<op::v0::Concat>(OutputVector{b, yy, xx}, -1);
+ return std::make_shared<op::v8::GatherND>(ctx.data_nhwc, indices, 0);
+}
+
+// Reflection helpers (continuous/indexed)
+std::shared_ptr<Node> reflect_coord(const Ctx& ctx,
+ const std::shared_ptr<Node>& coord,
+ const std::shared_ptr<Node>& size_f,
+ bool align_corners) {
+ auto size_is_one = std::make_shared<op::v1::Equal>(size_f, ctx.c1);
+ if (const auto size_flag = ov::util::get_constant_from_source(size_is_one)) {
+ const auto mask = size_flag->cast_vector<bool>();
+ if (!mask.empty() && mask.front()) {
+ auto target_shape = std::make_shared<op::v3::ShapeOf>(coord, element::i32);
+ return std::make_shared<op::v3::Broadcast>(ctx.c0, target_shape);
+ }
+ }
+ if (align_corners) {
+ auto size_m1 = std::make_shared<op::v1::Subtract>(size_f, ctx.c1);
+ auto period = std::make_shared<op::v1::Multiply>(ctx.c2, size_m1);
+ auto period_ok = std::make_shared<op::v1::Maximum>(period, ctx.c1);
+ auto r = std::make_shared<op::v1::FloorMod>(coord, period_ok);
+ auto d = std::make_shared<op::v1::Subtract>(r, size_m1);
+ auto ad = std::make_shared<op::v0::Abs>(d);
+ auto ref = std::make_shared<op::v1::Subtract>(size_m1, ad);
+ return std::make_shared<op::v1::Select>(size_is_one, ctx.c0, ref);
+ }
+ auto is_negative = std::make_shared<op::v1::Less>(coord, ctx.c0);
+ auto neg_coord = std::make_shared<op::v0::Negative>(coord);
+ auto neg_minus_one = std::make_shared<op::v1::Subtract>(neg_coord, ctx.c1);
+ auto coord_positive = std::make_shared<op::v1::Select>(is_negative, neg_minus_one, coord);
+
+ auto two_size = std::make_shared<op::v1::Multiply>(ctx.c2, size_f);
+ auto two_size_safe = std::make_shared<op::v1::Maximum>(two_size, ctx.c1);
+ auto mod = std::make_shared<op::v1::FloorMod>(coord_positive, two_size_safe);
+ auto need_ref = std::make_shared<op::v1::GreaterEqual>(mod, size_f);
+ auto two_size_m1 = std::make_shared<op::v1::Subtract>(two_size_safe, ctx.c1);
+ auto ref_val = std::make_shared<op::v1::Subtract>(two_size_m1, mod);
+ auto result = std::make_shared<op::v1::Select>(need_ref, ref_val, mod);
+ return std::make_shared<op::v1::Select>(size_is_one, ctx.c0, result);
+}
+
+std::shared_ptr<Node> reflect_index(const Ctx& ctx,
+ const std::shared_ptr<Node>& idx,
+ const std::shared_ptr<Node>& size_f,
+ bool align_corners) {
+ auto size_is_one = std::make_shared<op::v1::Equal>(size_f, ctx.c1);
+ if (const auto size_flag = ov::util::get_constant_from_source(size_is_one)) {
+ const auto mask = size_flag->cast_vector<bool>();
+ if (!mask.empty() && mask.front()) {
+ auto target_shape = std::make_shared<op::v3::ShapeOf>(idx, element::i32);
+ return std::make_shared<op::v3::Broadcast>(ctx.c0, target_shape);
+ }
+ }
+ if (align_corners) {
+ auto size_m1 = std::make_shared<op::v1::Subtract>(size_f, ctx.c1);
+ auto two_sm1 = std::make_shared<op::v1::Multiply>(ctx.c2, size_m1);
+ auto period_safe = std::make_shared<op::v1::Maximum>(two_sm1, ctx.c1);
+ auto mod1 = std::make_shared<op::v1::FloorMod>(idx, period_safe);
+ auto mod2 = std::make_shared<op::v1::FloorMod>(std::make_shared<op::v1::Add>(mod1, period_safe), period_safe);
+ auto flipped = std::make_shared<op::v1::Subtract>(two_sm1, mod2);
+ auto cond = std::make_shared<op::v1::GreaterEqual>(mod2, size_f);
+ auto ref = std::make_shared<op::v1::Select>(cond, flipped, mod2);
+ return std::make_shared<op::v1::Select>(size_is_one, ctx.c0, ref);
+ }
+ auto two_s = std::make_shared<op::v1::Multiply>(ctx.c2, size_f);
+ auto two_s_safe = std::make_shared<op::v1::Maximum>(two_s, ctx.c1);
+ auto mod1 = std::make_shared<op::v1::FloorMod>(idx, two_s_safe);
+ auto mod2 = std::make_shared<op::v1::FloorMod>(std::make_shared<op::v1::Add>(mod1, two_s_safe), two_s_safe);
+ auto need_rf = std::make_shared<op::v1::GreaterEqual>(mod2, size_f);
+ auto two_s_m1 = std::make_shared<op::v1::Subtract>(two_s_safe, ctx.c1);
+ auto ref_val = std::make_shared<op::v1::Subtract>(two_s_m1, mod2);
+ auto res = std::make_shared<op::v1::Select>(need_rf, ref_val, mod2);
+ return std::make_shared<op::v1::Select>(size_is_one, ctx.c0, res);
+}
+
+// Clamp integer-ish indices to [0..size-1] (safe)
+void clamp_indices_inplace(const Ctx& ctx,
+ std::shared_ptr<Node>& xi,
+ std::shared_ptr<Node>& yi,
+ const std::shared_ptr<Node>& w_in_f,
+ const std::shared_ptr<Node>& h_in_f) {
+ auto w_m1 = std::make_shared<op::v1::Subtract>(w_in_f, ctx.c1);
+ auto h_m1 = std::make_shared<op::v1::Subtract>(h_in_f, ctx.c1);
+ xi = std::make_shared<op::v1::Minimum>(std::make_shared<op::v1::Maximum>(xi, ctx.c0), w_m1);
+ yi = std::make_shared<op::v1::Minimum>(std::make_shared<op::v1::Maximum>(yi, ctx.c0), h_m1);
+}
+
+// Inside mask for ZEROS padding (check BEFORE clamp)
+std::shared_ptr<Node> inside_mask_indexed(const Ctx& ctx,
+ const std::shared_ptr<Node>& xi,
+ const std::shared_ptr<Node>& yi,
+ const std::shared_ptr<Node>& w_in_f,
+ const std::shared_ptr<Node>& h_in_f) {
+ auto x_ge_0 = std::make_shared<op::v1::GreaterEqual>(xi, ctx.c0);
+ auto y_ge_0 = std::make_shared<op::v1::GreaterEqual>(yi, ctx.c0);
+ auto x_lt_w = std::make_shared<op::v1::Less>(xi, w_in_f);
+ auto y_lt_h = std::make_shared<op::v1::Less>(yi, h_in_f);
+ return std::make_shared<op::v1::LogicalAnd>(std::make_shared<op::v1::LogicalAnd>(x_ge_0, x_lt_w),
+ std::make_shared<op::v1::LogicalAnd>(y_ge_0, y_lt_h));
+}
+
+// ---- builders for interpolation modes ----
+
+// NEAREST (HALF_TO_EVEN rounding; reflect after rounding; always clamp)
+std::shared_ptr<Node> build_nearest_nhwc(const Ctx& ctx, const ov::op::v9::GridSample::Attributes& attrs) {
+ const bool is_zeros = attrs.padding_mode == op::v9::GridSample::PaddingMode::ZEROS;
+ const bool is_reflection = attrs.padding_mode == op::v9::GridSample::PaddingMode::REFLECTION;
+
+ auto w_in_f = to_f32(ctx.w_in);
+ auto h_in_f = to_f32(ctx.h_in);
+
+ std::shared_ptr<Node> x_idx = std::make_shared<op::v5::Round>(ctx.x, op::v5::Round::RoundMode::HALF_TO_EVEN);
+ std::shared_ptr<Node> y_idx = std::make_shared<op::v5::Round>(ctx.y, op::v5::Round::RoundMode::HALF_TO_EVEN);
+
+ std::shared_ptr<Node> x_unclamped = x_idx;
+ std::shared_ptr<Node> y_unclamped = y_idx;
+
+ if (is_reflection) {
+ x_idx = reflect_index(ctx, x_idx, w_in_f, attrs.align_corners);
+ y_idx = reflect_index(ctx, y_idx, h_in_f, attrs.align_corners);
+ }
+ clamp_indices_inplace(ctx, x_idx, y_idx, w_in_f, h_in_f);
+
+ std::shared_ptr<Node> out = gather_hw_nhwc(ctx, x_idx, y_idx);
+
+ if (is_zeros) {
+ auto ok = inside_mask_indexed(ctx, x_unclamped, y_unclamped, w_in_f, h_in_f);
+ auto mask = std::make_shared<op::v0::Convert>(ok, ctx.calc_type);
+ auto maske = std::make_shared<op::v0::Unsqueeze>(mask, ctx.unsqueeze_axis_neg1);
+ if (ctx.element_type != ctx.calc_type) {
+ out = std::make_shared<op::v0::Convert>(out, ctx.calc_type);
+ }
+ out = std::make_shared<op::v1::Multiply>(out, maske);
+ if (ctx.element_type != ctx.calc_type) {
+ out = std::make_shared<op::v0::Convert>(out, ctx.element_type);
+ }
+ }
+
+ return out;
+}
+
+// BILINEAR
+std::shared_ptr<Node> build_bilinear_nhwc(const Ctx& ctx, const ov::op::v9::GridSample::Attributes& attrs) {
+ const bool is_border = attrs.padding_mode == op::v9::GridSample::PaddingMode::BORDER;
+ const bool is_zeros = attrs.padding_mode == op::v9::GridSample::PaddingMode::ZEROS;
+ const bool is_reflection = attrs.padding_mode == op::v9::GridSample::PaddingMode::REFLECTION;
+
+ auto w_in_f = to_f32(ctx.w_in);
+ auto h_in_f = to_f32(ctx.h_in);
+
+ // For REFLECTION, match reference semantics:
+ // - when align_corners == true: reflect continuous coordinates prior to flooring
+ // - when align_corners == false: do NOT reflect continuous coords here; indices are
+ // reflected later via reflect_index (post-floor) like in the reference path
+ std::shared_ptr<Node> x_pad = ctx.x, y_pad = ctx.y;
+ if (is_reflection && attrs.align_corners) {
+ x_pad = reflect_coord(ctx, ctx.x, w_in_f, true);
+ y_pad = reflect_coord(ctx, ctx.y, h_in_f, true);
+ }
+
+ std::shared_ptr<Node> x0 = std::make_shared<op::v0::Floor>(x_pad);
+ std::shared_ptr<Node> y0 = std::make_shared<op::v0::Floor>(y_pad);
+ std::shared_ptr<Node> x1 = std::make_shared<op::v1::Add>(x0, ctx.c1);
+ std::shared_ptr<Node> y1 = std::make_shared<op::v1::Add>(y0, ctx.c1);
+
+ std::shared_ptr<Node> dx = std::make_shared<op::v1::Subtract>(x_pad, x0);
+ std::shared_ptr<Node> dy = std::make_shared<op::v1::Subtract>(y_pad, y0);
+ if (!is_zeros) {
+ auto eps = op::v0::Constant::create(ctx.calc_type, {}, {1e-7F});
+ auto one_m_eps = std::make_shared<op::v1::Subtract>(ctx.c1, eps);
+ dx = std::make_shared<op::v1::Minimum>(std::make_shared<op::v1::Maximum>(dx, ctx.c0), one_m_eps);
+ dy = std::make_shared<op::v1::Minimum>(std::make_shared<op::v1::Maximum>(dy, ctx.c0), one_m_eps);
+ }
+ auto omdx = std::make_shared<op::v1::Subtract>(ctx.c1, dx);
+ auto omdy = std::make_shared<op::v1::Subtract>(ctx.c1, dy);
+
+ std::vector<std::shared_ptr<Node>> weights{std::make_shared<op::v1::Multiply>(omdx, omdy),
+ std::make_shared<op::v1::Multiply>(dx, omdy),
+ std::make_shared<op::v1::Multiply>(omdx, dy),
+ std::make_shared<op::v1::Multiply>(dx, dy)};
+
+ if (is_reflection) {
+ x0 = reflect_index(ctx, x0, w_in_f, attrs.align_corners);
+ x1 = reflect_index(ctx, x1, w_in_f, attrs.align_corners);
+ y0 = reflect_index(ctx, y0, h_in_f, attrs.align_corners);
+ y1 = reflect_index(ctx, y1, h_in_f, attrs.align_corners);
+ }
+
+ std::vector<std::shared_ptr<Node>> xs{x0, x1, x0, x1};
+ std::vector<std::shared_ptr<Node>> ys{y0, y0, y1, y1};
+
+ std::shared_ptr<Node> res;
+ for (int i = 0; i < 4; ++i) {
+ auto xi = xs[i], yi = ys[i];
+ if (is_zeros || is_border) {
+ clamp_indices_inplace(ctx, xi, yi, w_in_f, h_in_f);
+ }
+ auto v = gather_hw_nhwc(ctx, xi, yi);
+
+ if (ctx.element_type != ctx.calc_type) {
+ v = std::make_shared<op::v0::Convert>(v, ctx.calc_type);
+ }
+
+ if (is_zeros) {
+ auto ok = inside_mask_indexed(ctx, xs[i], ys[i], w_in_f, h_in_f);
+ auto mask = std::make_shared<op::v0::Convert>(ok, ctx.calc_type);
+ v = std::make_shared<op::v1::Multiply>(v,
+ std::make_shared<op::v0::Unsqueeze>(mask, ctx.unsqueeze_axis_neg1));
+ }
+
+ auto wexp = std::make_shared<op::v0::Unsqueeze>(weights[i], ctx.unsqueeze_axis_neg1);
+ auto tap = std::make_shared<op::v1::Multiply>(v, wexp);
+ res = res ? std::shared_ptr<Node>(std::make_shared<op::v1::Add>(res, tap)) : tap;
+ }
+ if (ctx.element_type != ctx.calc_type) {
+ res = std::make_shared<op::v0::Convert>(res, ctx.element_type);
+ }
+ return res;
+}
+
+// BICUBIC
+std::shared_ptr<Node> build_bicubic_nhwc(const Ctx& ctx, const ov::op::v9::GridSample::Attributes& attrs) {
+ const bool is_border = attrs.padding_mode == op::v9::GridSample::PaddingMode::BORDER;
+ const bool is_zeros = attrs.padding_mode == op::v9::GridSample::PaddingMode::ZEROS;
+ const bool is_reflection = attrs.padding_mode == op::v9::GridSample::PaddingMode::REFLECTION;
+
+ auto w_in_f = to_f32(ctx.w_in);
+ auto h_in_f = to_f32(ctx.h_in);
+
+ std::shared_ptr<Node> x_pad = ctx.x, y_pad = ctx.y;
+ if (is_reflection && attrs.align_corners) {
+ x_pad = reflect_coord(ctx, ctx.x, w_in_f, true);
+ y_pad = reflect_coord(ctx, ctx.y, h_in_f, true);
+ }
+
+ auto x0 = std::make_shared<op::v0::Floor>(x_pad);
+ auto y0 = std::make_shared<op::v0::Floor>(y_pad);
+ auto dx_raw = std::make_shared<op::v1::Subtract>(x_pad, x0);
+ auto dy_raw = std::make_shared<op::v1::Subtract>(y_pad, y0);
+
+ std::shared_ptr<Node> dx = dx_raw;
+ std::shared_ptr<Node> dy = dy_raw;
+ if (!is_zeros) {
+ auto eps = op::v0::Constant::create(ctx.calc_type, {}, {1e-7F});
+ auto one_m_eps = std::make_shared<op::v1::Subtract>(ctx.c1, eps);
+ dx = std::make_shared<op::v1::Minimum>(std::make_shared<op::v1::Maximum>(dx_raw, ctx.c0), one_m_eps);
+ dy = std::make_shared<op::v1::Minimum>(std::make_shared<op::v1::Maximum>(dy_raw, ctx.c0), one_m_eps);
+ }
+
+ auto w_is_one = std::make_shared<op::v1::Equal>(w_in_f, ctx.c1);
+ auto h_is_one = std::make_shared<op::v1::Equal>(h_in_f, ctx.c1);
+ dx = std::make_shared<op::v1::Select>(w_is_one, ctx.c0, dx);
+ dy = std::make_shared<op::v1::Select>(h_is_one, ctx.c0, dy);
+
+ // cubic weights (a = -0.75), match OV reference cubic_coeffs formulation exactly
+ auto a = op::v0::Constant::create(ctx.calc_type, {}, {-0.75F});
+ auto c3 = op::v0::Constant::create(ctx.calc_type, {}, {3.0F});
+ auto c4 = op::v0::Constant::create(ctx.calc_type, {}, {4.0F});
+ auto c5 = op::v0::Constant::create(ctx.calc_type, {}, {5.0F});
+ auto c8 = op::v0::Constant::create(ctx.calc_type, {}, {8.0F});
+
+ auto dx_p1 = std::make_shared<op::v1::Add>(dx, ctx.c1); // dx + 1
+ auto one_m_dx = std::make_shared<op::v1::Subtract>(ctx.c1, dx); // 1 - dx
+ auto two_m_dx = std::make_shared<op::v1::Subtract>(ctx.c2, dx); // 2 - dx
+
+ auto dx2 = std::make_shared<op::v1::Multiply>(dx, dx);
+ auto dx_p1_2 = std::make_shared<op::v1::Multiply>(dx_p1, dx_p1);
+ auto one_m_dx_2 = std::make_shared<op::v1::Multiply>(one_m_dx, one_m_dx);
+ auto two_m_dx_2 = std::make_shared<op::v1::Multiply>(two_m_dx, two_m_dx);
+
+ // v0 = ((A*(dx+1) - 5A) * (dx+1) + 8A) * (dx+1) - 4A
+ auto a_dx_p1 = std::make_shared<op::v1::Multiply>(a, dx_p1);
+ auto a5 = std::make_shared<op::v1::Multiply>(c5, a);
+ auto term_v0_1 = std::make_shared<op::v1::Subtract>(a_dx_p1, a5);
+ auto term_v0_2 = std::make_shared<op::v1::Add>(std::make_shared<op::v1::Multiply>(term_v0_1, dx_p1),
+ std::make_shared<op::v1::Multiply>(c8, a));
+ std::shared_ptr<Node> w_x0 =
+ std::make_shared<op::v1::Subtract>(std::make_shared<op::v1::Multiply>(term_v0_2, dx_p1),
+ std::make_shared<op::v1::Multiply>(c4, a));
+
+ // v1 = ((A + 2) * dx - (A + 3)) * dx * dx + 1
+ auto ap2 = std::make_shared<op::v1::Add>(a, ctx.c2);
+ auto ap3 = std::make_shared<op::v1::Add>(a, c3);
+ std::shared_ptr<Node> w_x1 = std::make_shared<op::v1::Add>(
+ std::make_shared<op::v1::Multiply>(
+ std::make_shared<op::v1::Subtract>(std::make_shared<op::v1::Multiply>(ap2, dx), ap3),
+ dx2),
+ ctx.c1);
+
+ // v2 = ((A + 2) * (1 - dx) - (A + 3)) * (1 - dx)^2 + 1
+ std::shared_ptr<Node> w_x2 = std::make_shared<op::v1::Add>(
+ std::make_shared<op::v1::Multiply>(
+ std::make_shared<op::v1::Subtract>(std::make_shared<op::v1::Multiply>(ap2, one_m_dx), ap3),
+ one_m_dx_2),
+ ctx.c1);
+
+ // v3 = ((A * (2 - dx) - 5A) * (2 - dx) + 8A) * (2 - dx) - 4A
+ auto a_two_m_dx = std::make_shared<op::v1::Multiply>(a, two_m_dx);
+ auto term_v3_1 = std::make_shared<op::v1::Subtract>(a_two_m_dx, a5);
+ auto term_v3_2 = std::make_shared<op::v1::Add>(std::make_shared<op::v1::Multiply>(term_v3_1, two_m_dx),
+ std::make_shared<op::v1::Multiply>(c8, a));
+ std::shared_ptr<Node> w_x3 =
+ std::make_shared<op::v1::Subtract>(std::make_shared<op::v1::Multiply>(term_v3_2, two_m_dx),
+ std::make_shared<op::v1::Multiply>(c4, a));
+
+ w_x0 = std::make_shared<op::v1::Select>(w_is_one, ctx.c0, w_x0);
+ w_x1 = std::make_shared<op::v1::Select>(w_is_one, ctx.c1, w_x1);
+ w_x2 = std::make_shared<op::v1::Select>(w_is_one, ctx.c0, w_x2);
+ w_x3 = std::make_shared<op::v1::Select>(w_is_one, ctx.c0, w_x3);
+
+ auto dy_p1 = std::make_shared<op::v1::Add>(dy, ctx.c1); // dy + 1
+ auto one_m_dy = std::make_shared<op::v1::Subtract>(ctx.c1, dy); // 1 - dy
+ auto two_m_dy = std::make_shared<op::v1::Subtract>(ctx.c2, dy); // 2 - dy
+
+ auto dy2 = std::make_shared<op::v1::Multiply>(dy, dy);
+ auto dy_p1_2 = std::make_shared<op::v1::Multiply>(dy_p1, dy_p1);
+ auto one_m_dy_2 = std::make_shared<op::v1::Multiply>(one_m_dy, one_m_dy);
+ auto two_m_dy_2 = std::make_shared<op::v1::Multiply>(two_m_dy, two_m_dy);
+
+ // y weights per OV reference cubic_coeffs(dy)
+ auto a_dy_p1 = std::make_shared<op::v1::Multiply>(a, dy_p1);
+ auto term_wy0_1 = std::make_shared<op::v1::Subtract>(a_dy_p1, a5);
+ auto term_wy0_2 = std::make_shared<op::v1::Add>(std::make_shared<op::v1::Multiply>(term_wy0_1, dy_p1),
+ std::make_shared<op::v1::Multiply>(c8, a));
+ std::shared_ptr<Node> w_y0 =
+ std::make_shared<op::v1::Subtract>(std::make_shared<op::v1::Multiply>(term_wy0_2, dy_p1),
+ std::make_shared<op::v1::Multiply>(c4, a));
+
+ std::shared_ptr<Node> w_y1 = std::make_shared<op::v1::Add>(
+ std::make_shared<op::v1::Multiply>(
+ std::make_shared<op::v1::Subtract>(std::make_shared<op::v1::Multiply>(ap2, dy), ap3),
+ dy2),
+ ctx.c1);
+
+ std::shared_ptr<Node> w_y2 = std::make_shared<op::v1::Add>(
+ std::make_shared<op::v1::Multiply>(
+ std::make_shared<op::v1::Subtract>(std::make_shared<op::v1::Multiply>(ap2, one_m_dy), ap3),
+ one_m_dy_2),
+ ctx.c1);
+
+ auto a_two_m_dy = std::make_shared<op::v1::Multiply>(a, two_m_dy);
+ auto term_wy3_1 = std::make_shared<op::v1::Subtract>(a_two_m_dy, a5);
+ auto term_wy3_2 = std::make_shared<op::v1::Add>(std::make_shared<op::v1::Multiply>(term_wy3_1, two_m_dy),
+ std::make_shared<op::v1::Multiply>(c8, a));
+ std::shared_ptr<Node> w_y3 =
+ std::make_shared<op::v1::Subtract>(std::make_shared<op::v1::Multiply>(term_wy3_2, two_m_dy),
+ std::make_shared<op::v1::Multiply>(c4, a));
+
+ w_y0 = std::make_shared<op::v1::Select>(h_is_one, ctx.c0, w_y0);
+ w_y1 = std::make_shared<op::v1::Select>(h_is_one, ctx.c1, w_y1);
+ w_y2 = std::make_shared<op::v1::Select>(h_is_one, ctx.c0, w_y2);
+ w_y3 = std::make_shared<op::v1::Select>(h_is_one, ctx.c0, w_y3);
+
+ std::vector<std::shared_ptr<Node>> x_idx{
+ std::make_shared<op::v1::Subtract>(x0, ctx.c1),
+ x0,
+ std::make_shared<op::v1::Add>(x0, ctx.c1),
+ std::make_shared<op::v1::Add>(x0, ctx.c2),
+ };
+ std::vector<std::shared_ptr<Node>> y_idx{
+ std::make_shared<op::v1::Subtract>(y0, ctx.c1),
+ y0,
+ std::make_shared<op::v1::Add>(y0, ctx.c1),
+ std::make_shared<op::v1::Add>(y0, ctx.c2),
+ };
+ std::vector<std::shared_ptr<Node>> wx{w_x0, w_x1, w_x2, w_x3};
+ std::vector<std::shared_ptr<Node>> wy{w_y0, w_y1, w_y2, w_y3};
+
+ // validity masks for ZEROS (before clamping)
+ std::vector<std::shared_ptr<Node>> x_ok(4), y_ok(4);
+ if (is_zeros) {
+ for (int i = 0; i < 4; ++i) {
+ x_ok[i] = std::make_shared<op::v1::LogicalAnd>(std::make_shared<op::v1::GreaterEqual>(x_idx[i], ctx.c0),
+ std::make_shared<op::v1::Less>(x_idx[i], w_in_f));
+ y_ok[i] = std::make_shared<op::v1::LogicalAnd>(std::make_shared<op::v1::GreaterEqual>(y_idx[i], ctx.c0),
+ std::make_shared<op::v1::Less>(y_idx[i], h_in_f));
+ }
+ }
+
+ if (is_reflection) {
+ for (int i = 0; i < 4; ++i) {
+ x_idx[i] = reflect_index(ctx, x_idx[i], w_in_f, attrs.align_corners);
+ y_idx[i] = reflect_index(ctx, y_idx[i], h_in_f, attrs.align_corners);
+ }
+ } else {
+ auto w_is_one_chk = std::make_shared<op::v1::Equal>(w_in_f, ctx.c1);
+ auto h_is_one_chk = std::make_shared<op::v1::Equal>(h_in_f, ctx.c1);
+ for (int i = 0; i < 4; ++i) {
+ x_idx[i] = std::make_shared<op::v1::Select>(w_is_one_chk, ctx.c0, x_idx[i]);
+ y_idx[i] = std::make_shared<op::v1::Select>(h_is_one_chk, ctx.c0, y_idx[i]);
+ }
+ }
+
+ std::shared_ptr<Node> sum;
+ for (int iy = 0; iy < 4; ++iy) {
+ std::shared_ptr<Node> row;
+ for (int ix = 0; ix < 4; ++ix) {
+ auto xi = x_idx[ix];
+ auto yi = y_idx[iy];
+
+ if (is_zeros || is_border) {
+ clamp_indices_inplace(ctx, xi, yi, w_in_f, h_in_f);
+ }
+
+ auto val = gather_hw_nhwc(ctx, xi, yi);
+ if (ctx.element_type != ctx.calc_type) {
+ val = std::make_shared<op::v0::Convert>(val, ctx.calc_type);
+ }
+
+ auto wxy = std::make_shared<op::v1::Multiply>(wx[ix], wy[iy]);
+ if (is_zeros) {
+ auto ok = std::make_shared<op::v1::LogicalAnd>(x_ok[ix], y_ok[iy]);
+ auto mask = std::make_shared<op::v0::Convert>(ok, ctx.calc_type);
+ val = std::make_shared<op::v1::Multiply>(
+ val,
+ std::make_shared<op::v0::Unsqueeze>(mask, ctx.unsqueeze_axis_neg1));
+ }
+
+ // Expand weight on last axis (NHWC)
+ auto wexp = std::make_shared<op::v0::Unsqueeze>(wxy, ctx.unsqueeze_axis_neg1);
+ auto tap = std::make_shared<op::v1::Multiply>(val, wexp);
+
+ row = row ? std::shared_ptr<Node>(std::make_shared<op::v1::Add>(row, tap)) : tap;
+ }
+ sum = sum ? std::shared_ptr<Node>(std::make_shared<op::v1::Add>(sum, row)) : row;
+ }
+
+ if (ctx.element_type != ctx.calc_type) {
+ sum = std::make_shared<op::v0::Convert>(sum, ctx.element_type);
+ }
+ return sum;
+}
+
+// ---- ctx builders ----
+
+// Helper to initialize common constants that are shared between static and dynamic paths
+void init_common_constants(Ctx& context) {
+ // Float constants
+ context.c0 = op::v0::Constant::create(context.calc_type, {}, {0.0F});
+ context.c1 = op::v0::Constant::create(context.calc_type, {}, {1.0F});
+ context.c2 = op::v0::Constant::create(context.calc_type, {}, {2.0F});
+ context.c0_5 = op::v0::Constant::create(context.calc_type, {}, {0.5F});
+
+ // Integer constants
+ context.i32_0 = op::v0::Constant::create(element::i32, {}, {0});
+ context.i32_1 = op::v0::Constant::create(element::i32, {}, {1});
+ context.i32_2 = op::v0::Constant::create(element::i32, {}, {2});
+ context.i32_3 = op::v0::Constant::create(element::i32, {}, {3});
+ context.i32_neg1 = op::v0::Constant::create(element::i32, {}, {-1});
+
+ // Common axes (just references to integer constants)
+ context.axis_0 = context.i32_0;
+ context.axis_3 = context.i32_3;
+ // Axes for Unsqueeze: match unit-test expectations (i32 scalars)
+ context.unsqueeze_axis_0 = op::v0::Constant::create(element::i32, {}, {0});
+ context.unsqueeze_axis_neg1 = op::v0::Constant::create(element::i32, {}, {-1});
+ context.unsqueeze_axis_3 = op::v0::Constant::create(element::i32, {}, {3});
+
+ // NHWC transpose pattern
+ context.to_nhwc_perm = op::v0::Constant::create(element::i32, {4}, {0, 2, 3, 1});
+}
+
+// Unified context building: uses ShapeOf/Gather which can be folded to constants for static shapes
+bool build_ctx(const std::shared_ptr<ov::op::v9::GridSample>& grid_sample_op, Ctx& context) {
+ const auto& data = grid_sample_op->input_value(0);
+ const auto& grid = grid_sample_op->input_value(1);
+
+ const auto& data_ps = data.get_partial_shape();
+ const auto& grid_ps = grid.get_partial_shape();
+ if ((data_ps.rank().is_static() && data_ps.rank().get_length() != 4) ||
+ (grid_ps.rank().is_static() && grid_ps.rank().get_length() != 4)) {
+ return false;
+ }
+ if (grid_ps.rank().is_static() && grid_ps.rank().get_length() == 4) {
+ const auto& last_dim = grid_ps[3];
+ if (last_dim.is_static() && last_dim.get_length() != 2) {
+ return false;
+ }
+ }
+
+ context.element_type = data.get_element_type();
+ context.calc_type = element::f32;
+
+ // Initialize all common constants
+ init_common_constants(context);
+
+ // Build shape extraction subgraphs using make_try_fold (automatically folds to constants if shapes are static)
+ auto dshape = ov::op::util::make_try_fold<op::v3::ShapeOf>(data, element::i32);
+ context.n_dim = ov::op::util::make_try_fold<op::v8::Gather>(dshape, context.i32_0, context.axis_0);
+ context.c_dim = ov::op::util::make_try_fold<op::v8::Gather>(dshape, context.i32_1, context.axis_0);
+ context.h_in = ov::op::util::make_try_fold<op::v8::Gather>(dshape, context.i32_2, context.axis_0);
+ context.w_in = ov::op::util::make_try_fold<op::v8::Gather>(dshape, context.i32_3, context.axis_0);
+
+ auto gshape = ov::op::util::make_try_fold<op::v3::ShapeOf>(grid, element::i32);
+ context.grid_shape = gshape;
+ context.h_out = ov::op::util::make_try_fold<op::v8::Gather>(gshape, context.i32_1, context.axis_0);
+ context.w_out = ov::op::util::make_try_fold<op::v8::Gather>(gshape, context.i32_2, context.axis_0);
+
+ // split grid channels (x,y)
+ auto grid_conv = grid.get_element_type() == context.calc_type
+ ? grid
+ : std::shared_ptr<Node>(std::make_shared<op::v0::Convert>(grid, context.calc_type));
+ context.x_norm = std::make_shared<op::v8::Gather>(grid_conv, context.i32_0, context.axis_3);
+ context.y_norm = std::make_shared<op::v8::Gather>(grid_conv, context.i32_1, context.axis_3);
+
+ // Keep references to data in both layouts for different gather strategies
+ // NCHW original
+ context.data_nchw = data.get_node_shared_ptr();
+ // NHWC via transpose
+ context.data_nhwc = std::make_shared<op::v1::Transpose>(data, context.to_nhwc_perm);
+ return true;
+}
+
+// Common glue: build ctx, normalize, build mode, NHWC->NCHW, replace
+using BuildModeFn = std::function<std::shared_ptr<Node>(const Ctx&, const ov::op::v9::GridSample::Attributes&)>;
+bool decompose_impl(const std::shared_ptr<ov::op::v9::GridSample>& grid_sample_op,
+ const BuildModeFn& build_mode_result_nhwc) {
+ Ctx context{};
+ if (!build_ctx(grid_sample_op, context)) {
+ return false;
+ }
+
+ auto w_in_f = to_f32(context.w_in);
+ auto h_in_f = to_f32(context.h_in);
+
+ normalize_grid_to_pixels(context,
+ context.x_norm,
+ context.y_norm,
+ w_in_f,
+ h_in_f,
+ grid_sample_op->get_attributes().align_corners,
+ context.x,
+ context.y);
+
+ auto result_nhwc = build_mode_result_nhwc(context, grid_sample_op->get_attributes());
+ if (!result_nhwc) {
+ return false;
+ }
+
+ auto to_nchw = op::v0::Constant::create(element::i32, {4}, {0, 3, 1, 2});
+ auto result = std::make_shared<op::v1::Transpose>(result_nhwc, to_nchw);
+
+ result->set_friendly_name(grid_sample_op->get_friendly_name());
+ copy_rt_to_subgraph(grid_sample_op, result);
+ ov::replace_node_update_name(grid_sample_op, result);
+ return true;
+}
+
+} // unnamed namespace
+
+// Unified matcher that handles all interpolation modes
+GridSampleDecomposition::GridSampleDecomposition() {
+ auto data_pattern = ov::pass::pattern::any_input(ov::pass::pattern::rank_equals(4));
+ auto grid_pattern = ov::pass::pattern::any_input(ov::pass::pattern::rank_equals(4));
+ auto pat = ov::pass::pattern::wrap_type<op::v9::GridSample>({data_pattern, grid_pattern});
+
+ matcher_pass_callback callback = [this](ov::pass::pattern::Matcher& matcher) {
+ auto grid_sample = std::dynamic_pointer_cast<op::v9::GridSample>(matcher.get_match_root());
+ if (!grid_sample || transformation_callback(grid_sample)) {
+ return false;
+ }
+ const auto& attrs = grid_sample->get_attributes();
+ const bool is_f32_data = grid_sample->get_input_element_type(0) == element::f32;
+ const bool is_f32_grid = grid_sample->get_input_element_type(1) == element::f32;
+
+ switch (attrs.mode) {
+ case op::v9::GridSample::InterpolationMode::NEAREST: {
+ // For non-f32 inputs, convert to f32 for padding modes with ref path limitations
+ if ((!is_f32_data || !is_f32_grid) && any_of(attrs.padding_mode,
+ op::v9::GridSample::PaddingMode::REFLECTION,
+ op::v9::GridSample::PaddingMode::BORDER,
+ op::v9::GridSample::PaddingMode::ZEROS)) {
+ auto out = grid_sample_in_f32_and_back(grid_sample);
+ out->set_friendly_name(grid_sample->get_friendly_name());
+ copy_rt_to_subgraph(grid_sample, out);
+ ov::replace_node_update_name(grid_sample, out);
+ return true;
+ }
+ if (is_f32_data && is_f32_grid && is_nearest_problematic(attrs)) {
+ return false; // keep original GridSample -> plugin will fallback to Reference
+ }
+ return decompose_impl(grid_sample, build_nearest_nhwc);
+ }
+ case op::v9::GridSample::InterpolationMode::BILINEAR: {
+ // Decompose for all padding modes, including REFLECTION
+ return decompose_impl(grid_sample, build_bilinear_nhwc);
+ }
+ case op::v9::GridSample::InterpolationMode::BICUBIC: {
+ if ((!is_f32_data || !is_f32_grid) && (attrs.padding_mode == op::v9::GridSample::PaddingMode::REFLECTION)) {
+ auto out = grid_sample_in_f32_and_back(grid_sample);
+ out->set_friendly_name(grid_sample->get_friendly_name());
+ copy_rt_to_subgraph(grid_sample, out);
+ ov::replace_node_update_name(grid_sample, out);
+ return true;
+ }
+ if (is_f32_data && is_f32_grid && attrs.padding_mode == op::v9::GridSample::PaddingMode::ZEROS &&
+ !attrs.align_corners) {
+ return false; // keep original GridSample
+ }
+ if ((!is_f32_data || !is_f32_grid) && attrs.padding_mode == op::v9::GridSample::PaddingMode::ZEROS &&
+ !attrs.align_corners) {
+ auto out = grid_sample_in_f32_and_back(grid_sample);
+ out->set_friendly_name(grid_sample->get_friendly_name());
+ copy_rt_to_subgraph(grid_sample, out);
+ ov::replace_node_update_name(grid_sample, out);
+ return true;
+ }
+ return decompose_impl(grid_sample, build_bicubic_nhwc);
+ }
+ default:
+ return false;
+ }
+ };
+
+ this->register_matcher(std::make_shared<ov::pass::pattern::Matcher>(pat, "GridSampleDecomposition"), callback);
+}
+
+} // namespace ov::intel_cpu
diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/arm/pass/grid_sample_decomposition.hpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/arm/pass/grid_sample_decomposition.hpp
new file mode 100644
index 00000000000..28351c3998a
--- /dev/null
+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/arm/pass/grid_sample_decomposition.hpp
@@ -0,0 +1,82 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/pass/graph_rewrite.hpp"
+#include "openvino/pass/matcher_pass.hpp"
+
+namespace ov::intel_cpu {
+
+/**
+ * @brief GridSampleDecomposition decomposes GridSample operation into primitive operations
+ *
+ * This transformation enables execution on ARM platforms without native GridSample support
+ * by decomposing the operation into simpler primitives that can be optimized by ACL.
+ *
+ * The transformation handles all three interpolation modes:
+ * - NEAREST: Simpler mode that finds the nearest pixel without interpolation
+ * - BILINEAR: Uses 2x2 pixel neighborhood with bilinear weighting
+ * - BICUBIC: Uses 4x4 pixel neighborhood with cubic weighting for smoother results
+ *
+ * Common decomposition pattern for all modes:
+ *
+ * Before:
+ * +-------------------+ +----------------------+
+ * | Data[N,C,H,W] | | Grid[N,H_out,W_out,2]|
+ * +--------+----------+ +---------+------------+
+ * | |
+ * +----v-------------------------v----+
+ * | GridSample |
+ * | (mode, padding, align_corners) |
+ * +----------------+------------------+
+ * |
+ * +--------v---------------+
+ * | Output[N,C,H_out,W_out]|
+ * +------------------------+
+ *
+ * After (general structure):
+ * +-------------------+ +----------------------+
+ * | Data[N,C,H,W] | | Grid[N,H_out,W_out,2]|
+ * +--------+----------+ +---------+------------+
+ * | |
+ * | +-----v------+
+ * | | Split |
+ * | | x,y coords |
+ * | +--+-----+---+
+ * | | |
+ * | +----v-+ +v----+
+ * | | Norm | | Norm|
+ * | | X | | Y |
+ * | +----+-+ +-+---+
+ * | | |
+ * | +----v------v---+
+ * | | Mode-specific |
+ * | | interpolation |
+ * | +----+----------+
+ * | |
+ * +----v----------------------v----+
+ * | GatherND (mode-specific) |
+ * +----------------+---------------+
+ * |
+ * +-----v--------+
+ * | Weighted |
+ * | combination |
+ * +-----+--------+
+ * |
+ * +--------v---------------+
+ * | Output[N,C,H_out,W_out]|
+ * +------------------------+
+ *
+ * The matcher selects the appropriate decomposition path based on
+ * ov::op::v9::GridSample::Attributes::mode and applies special-case handling
+ * for known problematic combinations (e.g., non-f32 inputs, specific padding modes).
+ */
+class GridSampleDecomposition : public ov::pass::MatcherPass {
+public:
+ OPENVINO_RTTI("GridSampleDecomposition", "0");
+ GridSampleDecomposition();
+};
+
+} // namespace ov::intel_cpu
\ No newline at end of file
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index 70481d97275..0c417c4dc29 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -271,6 +271,7 @@
# include "transformations/cpu_opset/arm/pass/convert_reduce_multi_axis.hpp"
# include "transformations/cpu_opset/arm/pass/convert_reduce_no_keep_dims.hpp"
# include "transformations/cpu_opset/arm/pass/deconv_1d_decomposition.hpp"
+# include "transformations/cpu_opset/arm/pass/grid_sample_decomposition.hpp"
# include "transformations/cpu_opset/common/op/sdpa.hpp"
# include "transformations/cpu_opset/common/pass/decompose_integer_divide.hpp"
#else
@@ -661,6 +662,8 @@ void Transformations::PreLpt(const std::vector<ov::element::Type>& defaultPrecis
CPU_REGISTER_PASS_ARM(manager, ConvertConv1D);
CPU_REGISTER_PASS_ARM(manager, ConvertGroupConv1D);
CPU_REGISTER_PASS_ARM(manager, ConvertGroupConvolution);
+ // Register GridSample decomposition that handles all interpolation modes
+ CPU_REGISTER_PASS_ARM(manager, GridSampleDecomposition);
CPU_REGISTER_PASS_ARM(manager, Deconv1DDecomposition);
// The plugin computes Divide in floating point precision.
// To preserve correct math for integer division we need to insert explicit Floor operation.
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/grid_sample.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/grid_sample.cpp
new file mode 100644
index 00000000000..5b9f49b67ae
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/grid_sample.cpp
@@ -0,0 +1,197 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "grid_sample.hpp"
+
+#include <algorithm>
+#include <cctype>
+#include "common_test_utils/ov_tensor_utils.hpp"
+#include "gtest/gtest.h"
+#include "openvino/op/grid_sample.hpp"
+#include "openvino/core/enum_names.hpp"
+#include "utils/cpu_test_utils.hpp"
+#include "utils/general_utils.h"
+
+using namespace CPUTestUtils;
+
+namespace ov {
+namespace test {
+
+std::string GridSampleLayerTestCPU::getTestCaseName(testing::TestParamInfo<GridSampleLayerTestCPUParams> obj) {
+ const auto& [inputShapes,
+ interpolateMode,
+ paddingMode,
+ alignCorners,
+ dataPrecision,
+ gridPrecision,
+ cpuParams,
+ additionalConfig] = obj.param;
+ std::ostringstream result;
+ for (size_t i = 0; i < inputShapes.size(); ++i) {
+ result << "IS[" << i << "]=" << inputShapes[i] << "_";
+ }
+
+ // Use EnumNames to get string representation and convert to uppercase for compatibility with skip patterns
+ auto to_upper = [](const std::string& str) {
+ std::string upper_str = str;
+ std::transform(upper_str.begin(), upper_str.end(), upper_str.begin(), ::toupper);
+ return upper_str;
+ };
+
+ result << "interpMode=" << to_upper(ov::EnumNames<ov::op::v9::GridSample::InterpolationMode>::as_string(interpolateMode)) << "_";
+ result << "padMode=" << to_upper(ov::EnumNames<ov::op::v9::GridSample::PaddingMode>::as_string(paddingMode)) << "_";
+ result << "alignCorners=" << (alignCorners ? "True" : "False") << "_";
+ result << "dataPrc=" << dataPrecision << "_";
+ result << "gridPrc=" << gridPrecision;
+ result << CPUTestsBase::getTestCaseName(cpuParams);
+
+ if (!additionalConfig.empty()) {
+ result << "_PluginConf";
+ for (auto& item : additionalConfig) {
+ if (item.second == ov::element::bf16)
+ result << "_" << item.first << "=" << item.second.as<std::string>();
+ }
+ }
+
+ return result.str();
+}
+
+void GridSampleLayerTestCPU::SetUp() {
+ abs_threshold = 0.0005;
+ const auto& [inputShapes,
+ interpolateMode,
+ paddingMode,
+ alignCorners,
+ dataPrecision,
+ gridPrecision,
+ cpuParams,
+ additionalConfig] = this->GetParam();
+ std::tie(inFmts, outFmts, priority, selectedType) = cpuParams;
+ targetDevice = ov::test::utils::DEVICE_CPU;
+ init_input_shapes(inputShapes);
+ configuration.insert(additionalConfig.begin(), additionalConfig.end());
+
+ if (intel_cpu::contains_key_value(additionalConfig, {ov::hint::inference_precision.name(), ov::element::bf16})) {
+ selectedType = makeSelectedTypeStr(selectedType, ElementType::bf16);
+ } else {
+ auto execType = dataPrecision == ov::element::i32 ? ov::element::i32 : ov::element::f32;
+ selectedType = makeSelectedTypeStr(selectedType, execType);
+ }
+ if (gridPrecision == ov::element::bf16) {
+ rel_threshold = 0.01f;
+ }
+
+ ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(dataPrecision, inputDynamicShapes[0]),
+ std::make_shared<ov::op::v0::Parameter>(gridPrecision, inputDynamicShapes[1])};
+ params[0]->set_friendly_name("data");
+ params[1]->set_friendly_name("grid");
+ ov::op::v9::GridSample::Attributes attributes = {alignCorners, interpolateMode, paddingMode};
+ auto gridSample = std::make_shared<ov::op::v9::GridSample>(params[0], params[1], attributes);
+ function = makeNgraphFunction(dataPrecision, params, gridSample, "GridSampleCPU");
+}
+
+TEST_P(GridSampleLayerTestCPU, CompareWithRefs) {
+ run();
+ CheckPluginRelatedResults(compiledModel, "GridSample");
+}
+
+namespace GridSample {
+
+const std::vector<ov::op::v9::GridSample::InterpolationMode>& allInterpolationModes() {
+ static const std::vector<ov::op::v9::GridSample::InterpolationMode> modes = {
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::InterpolationMode::BICUBIC,
+ ov::op::v9::GridSample::InterpolationMode::NEAREST};
+ return modes;
+}
+
+const std::vector<ov::op::v9::GridSample::PaddingMode>& allPaddingModes() {
+ static const std::vector<ov::op::v9::GridSample::PaddingMode> modes = {
+ ov::op::v9::GridSample::PaddingMode::ZEROS,
+ ov::op::v9::GridSample::PaddingMode::BORDER,
+ ov::op::v9::GridSample::PaddingMode::REFLECTION};
+ return modes;
+}
+
+const std::vector<bool>& alignCornersValues() {
+ static const std::vector<bool> values = {true, false};
+ return values;
+}
+
+const std::vector<std::vector<InputShape>>& getStaticShapes() {
+ static const std::vector<std::vector<InputShape>> shapes = [] {
+ std::vector<std::vector<InputShape>> base = {
+ {{{}, {{1, 5, 1, 1}}}, {{}, {{1, 1, 1, 2}}}},
+ {{{}, {{2, 4, 7, 1}}}, {{}, {{2, 1, 2, 2}}}},
+ {{{}, {{3, 3, 3, 3}}}, {{}, {{3, 3, 1, 2}}}},
+ {{{}, {{4, 2, 5, 4}}}, {{}, {{4, 2, 2, 2}}}},
+ {{{}, {{5, 1, 5, 5}}}, {{}, {{5, 1, 5, 2}}}},
+ {{{}, {{4, 2, 4, 6}}}, {{}, {{4, 2, 3, 2}}}},
+ {{{}, {{3, 3, 5, 7}}}, {{}, {{3, 7, 1, 2}}}},
+ {{{}, {{2, 4, 7, 7}}}, {{}, {{2, 2, 4, 2}}}},
+ {{{}, {{2, 5, 8, 8}}}, {{}, {{2, 3, 3, 2}}}},
+ {{{}, {{2, 6, 9, 8}}}, {{}, {{2, 2, 5, 2}}}},
+ };
+ std::vector<std::vector<InputShape>> extra = {
+ {{{}, {{1, 7, 5, 3}}}, {{}, {{1, 1, 11, 2}}}},
+ {{{}, {{2, 6, 7, 2}}}, {{}, {{2, 6, 2, 2}}}},
+ {{{}, {{3, 2, 9, 1}}}, {{}, {{3, 3, 13, 2}}}},
+ {{{}, {{4, 7, 3, 4}}}, {{}, {{4, 5, 5, 2}}}},
+ {{{}, {{5, 3, 2, 13}}}, {{}, {{5, 1, 31, 2}}}},
+ {{{}, {{4, 3, 5, 14}}}, {{}, {{4, 4, 8, 2}}}},
+ {{{}, {{3, 2, 2, 15}}}, {{}, {{3, 33, 1, 2}}}},
+ {{{}, {{2, 1, 6, 16}}}, {{}, {{2, 8, 8, 2}}}},
+ {{{}, {{2, 3, 7, 17}}}, {{}, {{2, 9, 9, 2}}}},
+ };
+ base.insert(base.end(), extra.begin(), extra.end());
+ static const std::vector<std::vector<InputShape>> all = base;
+ return all;
+ }();
+ return shapes;
+}
+
+const std::vector<std::vector<InputShape>>& getDynamicShapes() {
+ static const std::vector<std::vector<InputShape>> shapes = {
+ // from master dynamicInSapes (full set)
+ {{{ov::Dimension(1, 15), -1, -1, -1}, {{1, 1, 1, 1}, {6, 3, 1, 2}, {4, 5, 3, 1}, {2, 7, 2, 2}}},
+ {{ov::Dimension(1, 16), -1, -1, -1}, {{1, 1, 1, 2}, {6, 2, 2, 2}, {4, 1, 3, 2}, {2, 1, 2, 2}}}},
+ {{{-1, -1, -1, -1}, {{1, 2, 1, 5}, {3, 4, 2, 3}, {5, 6, 7, 1}, {7, 8, 2, 4}}},
+ {{-1, -1, -1, 2}, {{1, 2, 4, 2}, {3, 1, 7, 2}, {5, 2, 3, 2}, {7, 1, 5, 2}}}},
+ {{{ov::Dimension(2, 15), -1, -1, -1}, {{8, 3, 3, 3}, {6, 5, 2, 5}, {4, 7, 1, 11}, {2, 9, 3, 4}}},
+ {{-1, 3, 7, 2}, {{8, 3, 7, 2}, {6, 3, 7, 2}, {4, 3, 7, 2}, {2, 3, 7, 2}}}},
+ {{{3, 4, 4, 5}, {{3, 4, 4, 5}, {3, 4, 4, 5}, {3, 4, 4, 5}, {3, 4, 4, 5}}},
+ {{-1, -1, -1, 2}, {{3, 3, 4, 2}, {3, 1, 11, 2}, {3, 2, 5, 2}, {3, 3, 3, 2}}}},
+ {{{-1, -1, -1, -1}, {{1, 2, 1, 13}, {3, 4, 7, 2}, {5, 6, 3, 5}, {7, 8, 4, 4}}},
+ {{-1, -1, -1, -1}, {{1, 4, 4, 2}, {3, 3, 5, 2}, {5, 2, 7, 2}, {7, 1, 13, 2}}}},
+ {{{-1, -1, -1, -1}, {{2, 11, 1, 17}, {4, 9, 6, 3}, {6, 7, 7, 3}, {8, 3, 2, 11}}},
+ {{-1, -1, -1, 2}, {{2, 5, 4, 2}, {4, 1, 19, 2}, {6, 6, 3, 2}, {8, 1, 17, 2}}}},
+ {{{3, -1, -1, -1}, {{3, 2, 1, 23}, {3, 4, 3, 8}, {3, 6, 5, 5}, {3, 8, 31, 1}}},
+ {{-1, -1, -1, 2}, {{3, 31, 1, 2}, {3, 6, 4, 2}, {3, 23, 1, 2}, {3, 11, 2, 2}}}},
+ {{{-1, 3, -1, -1}, {{8, 3, 8, 4}, {6, 3, 33, 1}, {4, 3, 8, 6}, {2, 3, 8, 8}}},
+ {{-1, -1, -1, 2}, {{8, 8, 8, 2}, {6, 8, 7, 2}, {4, 1, 33, 2}, {2, 4, 8, 2}}}},
+ };
+ return shapes;
+}
+
+const std::vector<std::vector<InputShape>>& getAllShapes() {
+ static const std::vector<std::vector<InputShape>> shapes = [] {
+ std::vector<std::vector<InputShape>> allShapes;
+ const auto& staticShapes = getStaticShapes();
+ const auto& dynamicShapes = getDynamicShapes();
+ allShapes.reserve(staticShapes.size() + dynamicShapes.size());
+ allShapes.insert(allShapes.end(), staticShapes.begin(), staticShapes.end());
+ allShapes.insert(allShapes.end(), dynamicShapes.begin(), dynamicShapes.end());
+ return allShapes;
+ }();
+ return shapes;
+}
+
+const std::vector<ov::AnyMap>& additionalConfigs() {
+ static const std::vector<ov::AnyMap> configs = {{}, {{ov::hint::inference_precision.name(), ov::element::bf16}}};
+ return configs;
+}
+
+} // namespace GridSample
+} // namespace test
+} // namespace ov
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/grid_sample.hpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/grid_sample.hpp
new file mode 100644
index 00000000000..2b72857e8ab
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/classes/grid_sample.hpp
@@ -0,0 +1,52 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "gtest/gtest.h"
+#include "openvino/op/grid_sample.hpp"
+#include "shared_test_classes/base/ov_subgraph.hpp"
+#include "utils/cpu_test_utils.hpp"
+
+using namespace CPUTestUtils;
+
+namespace ov {
+namespace test {
+
+typedef std::tuple<std::vector<InputShape>, // Input shapes
+ ov::op::v9::GridSample::InterpolationMode, // Interpolation mode
+ ov::op::v9::GridSample::PaddingMode, // Padding mode
+ bool, // Align corners
+ ElementType, // Data precision
+ ElementType, // Grid precision
+ CPUSpecificParams, // CPU specific params
+ ov::AnyMap // Additional config
+ >
+ GridSampleLayerTestCPUParams;
+
+class GridSampleLayerTestCPU : public testing::WithParamInterface<GridSampleLayerTestCPUParams>,
+ virtual public SubgraphBaseTest,
+ public CPUTestsBase {
+public:
+ static std::string getTestCaseName(testing::TestParamInfo<GridSampleLayerTestCPUParams> obj);
+
+protected:
+ void SetUp() override;
+};
+
+namespace GridSample {
+// Common test parameters
+const std::vector<ov::op::v9::GridSample::InterpolationMode>& allInterpolationModes();
+const std::vector<ov::op::v9::GridSample::InterpolationMode>& armInterpolationModes();
+const std::vector<ov::op::v9::GridSample::PaddingMode>& allPaddingModes();
+const std::vector<bool>& alignCornersValues();
+const std::vector<std::vector<InputShape>>& getStaticShapes();
+const std::vector<std::vector<InputShape>>& getDynamicShapes();
+const std::vector<std::vector<InputShape>>& getAllShapes();
+const std::vector<CPUSpecificParams>& getCPUInfoForCommon();
+const std::vector<ov::AnyMap>& additionalConfigs();
+} // namespace GridSample
+
+} // namespace test
+} // namespace ov
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/grid_sample.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/grid_sample.cpp
deleted file mode 100644
index b36b8873872..00000000000
--- a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/grid_sample.cpp
+++ /dev/null
@@ -1,344 +0,0 @@
-// Copyright (C) 2018-2025 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "common_test_utils/ov_tensor_utils.hpp"
-#include "shared_test_classes/base/ov_subgraph.hpp"
-#include "utils/cpu_test_utils.hpp"
-#include "utils/general_utils.h"
-#include "openvino/op/grid_sample.hpp"
-
-using namespace CPUTestUtils;
-
-namespace ov {
-namespace test {
-
-typedef std::tuple<std::vector<InputShape>, // Input shapes
- ov::op::v9::GridSample::InterpolationMode, // Interpolation mode
- ov::op::v9::GridSample::PaddingMode, // Padding mode
- bool, // Align corners
- ElementType, // Data precision
- ElementType, // Grid precision
- CPUSpecificParams, // CPU specific params
- ov::AnyMap // Additional config
- >
- GridSampleLayerTestCPUParams;
-
-class GridSampleLayerTestCPU : public testing::WithParamInterface<GridSampleLayerTestCPUParams>,
- virtual public SubgraphBaseTest,
- public CPUTestsBase {
-public:
- static std::string getTestCaseName(const testing::TestParamInfo<GridSampleLayerTestCPUParams>& obj) {
- const auto &[inputShapes, interpolateMode, paddingMode, alignCorners, dataPrecision, gridPrecision, cpuParams,
- additionalConfig] = obj.param;
- std::ostringstream result;
- result << "IS=(";
- for (size_t i = 0lu; i < inputShapes.size(); i++) {
- result << ov::test::utils::partialShape2str({inputShapes[i].first})
- << (i < inputShapes.size() - 1lu ? "_" : "");
- }
- result << ")_TS=";
- for (size_t i = 0lu; i < inputShapes.front().second.size(); i++) {
- result << "{";
- for (size_t j = 0lu; j < inputShapes.size(); j++) {
- result << ov::test::utils::vec2str(inputShapes[j].second[i])
- << (j < inputShapes.size() - 1lu ? "_" : "");
- }
- result << "}_";
- }
-
- result << "interpMode="
- << (interpolateMode == ov::op::v9::GridSample::InterpolationMode::BILINEAR ? "BILINEAR"
- : interpolateMode == ov::op::v9::GridSample::InterpolationMode::BICUBIC ? "BICUBIC"
- : "NEAREST")
- << "_";
- result << "padMode="
- << (paddingMode == ov::op::v9::GridSample::PaddingMode::ZEROS ? "ZEROS"
- : paddingMode == ov::op::v9::GridSample::PaddingMode::BORDER ? "BORDER"
- : "REFLECTION")
- << "_";
- result << "alignCorners=" << (alignCorners ? "True" : "False") << "_";
- result << "dataPrc=" << dataPrecision << "_";
- result << "gridPrc=" << gridPrecision;
- result << CPUTestsBase::getTestCaseName(cpuParams);
-
- if (!additionalConfig.empty()) {
- result << "_PluginConf";
- for (auto& item : additionalConfig) {
- if (item.second == ov::element::bf16)
- result << "_" << item.first << "=" << item.second.as<std::string>();
- }
- }
-
- return result.str();
- }
-
-protected:
- void SetUp() override {
- abs_threshold = 0.0005;
- const auto &[inputShapes, interpolateMode, paddingMode, alignCorners, dataPrecision, gridPrecision, cpuParams,
- additionalConfig] = this->GetParam();
- std::tie(inFmts, outFmts, priority, selectedType) = cpuParams;
- targetDevice = ov::test::utils::DEVICE_CPU;
- init_input_shapes(inputShapes);
- configuration.insert(additionalConfig.begin(), additionalConfig.end());
-
- if (intel_cpu::contains_key_value(additionalConfig, {ov::hint::inference_precision.name(), ov::element::bf16})) {
- selectedType = makeSelectedTypeStr(selectedType, ElementType::bf16);
- } else {
- auto execType = dataPrecision == ov::element::i32 ? ov::element::i32 : ov::element::f32;
- selectedType = makeSelectedTypeStr(selectedType, execType);
- }
- if (gridPrecision == ov::element::bf16) {
- rel_threshold = 0.01f;
- }
-
- ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(dataPrecision, inputDynamicShapes[0]),
- std::make_shared<ov::op::v0::Parameter>(gridPrecision, inputDynamicShapes[1])};
- params[0]->set_friendly_name("data");
- params[1]->set_friendly_name("grid");
- ov::op::v9::GridSample::Attributes attributes = {alignCorners, interpolateMode, paddingMode};
- auto gridSampleNode = std::make_shared<ov::op::v9::GridSample>(params[0], params[1], attributes);
-
- function = makeNgraphFunction(dataPrecision, params, gridSampleNode, "GridSampleCPU");
- }
-
- void generate_inputs(const std::vector<ov::Shape>& targetInputStaticShapes) override {
- inputs.clear();
- const auto& funcInputs = function->inputs();
-
- for (size_t i = 0; i < funcInputs.size(); ++i) {
- const auto& funcInput = funcInputs[i];
- ov::Tensor tensor;
- ov::test::utils::InputGenerateData in_data;
-
- if (funcInput.get_node()->get_friendly_name() == "data") {
- int32_t range = std::accumulate(targetInputStaticShapes[0].begin(), targetInputStaticShapes[0].end(), 1u, std::multiplies<uint32_t>());
- in_data.start_from = -range / 2;
- in_data.range = range;
- tensor = utils::create_and_fill_tensor(funcInput.get_element_type(), targetInputStaticShapes[0], in_data);
- } else if (funcInput.get_node()->get_friendly_name() == "grid") {
- int32_t range = std::max(targetInputStaticShapes[0][2], targetInputStaticShapes[0][3]) + 2;
- in_data.start_from = -1;
- in_data.range = range;
- in_data.resolution = range / 2;
- tensor = utils::create_and_fill_tensor(funcInput.get_element_type(), targetInputStaticShapes[1], in_data);
- }
- inputs.insert({funcInput.get_node_shared_ptr(), tensor});
- }
- }
-};
-
-TEST_P(GridSampleLayerTestCPU, CompareWithRefs) {
- run();
- CheckPluginRelatedResults(compiledModel, "GridSample");
-}
-
-namespace {
-
-std::vector<ov::AnyMap> additionalConfig = {{{ov::hint::inference_precision(ov::element::f32)}},
- {{ov::hint::inference_precision(ov::element::bf16)}}};
-
-std::vector<ov::op::v9::GridSample::InterpolationMode> interpolateMode{ov::op::v9::GridSample::InterpolationMode::BILINEAR,
- ov::op::v9::GridSample::InterpolationMode::BICUBIC,
- ov::op::v9::GridSample::InterpolationMode::NEAREST};
-
-std::vector<ov::op::v9::GridSample::PaddingMode> paddingMode{ov::op::v9::GridSample::PaddingMode::ZEROS,
- ov::op::v9::GridSample::PaddingMode::BORDER,
- ov::op::v9::GridSample::PaddingMode::REFLECTION};
-
-std::vector<bool> alignCorners{true, false};
-
-std::vector<CPUSpecificParams> getCPUInfo() {
- std::vector<CPUSpecificParams> resCPUParams;
- if (ov::with_cpu_x86_avx512f()) {
- resCPUParams.push_back(CPUSpecificParams{{}, {}, {"jit_avx512"}, "jit_avx512"});
- } else if (ov::with_cpu_x86_avx2()) {
- resCPUParams.push_back(CPUSpecificParams{{}, {}, {"jit_avx2"}, "jit_avx2"});
- } else if (ov::with_cpu_x86_avx()) {
- resCPUParams.push_back(CPUSpecificParams{{}, {}, {"jit_avx"}, "jit_avx"});
- } else if (ov::with_cpu_x86_sse42()) {
- resCPUParams.push_back(CPUSpecificParams{{}, {}, {"jit_sse42"}, "jit_sse42"});
- }
- return resCPUParams;
-}
-
-std::vector<std::vector<InputShape>> getStaticShapes() {
- // SSE42
- std::vector<std::vector<InputShape>> result = {{{{}, {{1, 5, 1, 1}}}, // Static shapes
- {{}, {{1, 1, 1, 2}}}},
- {{{}, {{2, 4, 7, 1}}}, // Static shapes
- {{}, {{2, 1, 2, 2}}}},
- {{{}, {{3, 3, 3, 3}}}, // Static shapes
- {{}, {{3, 3, 1, 2}}}},
- {{{}, {{4, 2, 5, 4}}}, // Static shapes
- {{}, {{4, 2, 2, 2}}}},
- {{{}, {{5, 1, 5, 5}}}, // Static shapes
- {{}, {{5, 1, 5, 2}}}},
- {{{}, {{4, 2, 4, 6}}}, // Static shapes
- {{}, {{4, 2, 3, 2}}}},
- {{{}, {{3, 3, 5, 7}}}, // Static shapes
- {{}, {{3, 7, 1, 2}}}},
- {{{}, {{2, 4, 7, 7}}}, // Static shapes
- {{}, {{2, 2, 4, 2}}}},
- {{{}, {{2, 5, 8, 8}}}, // Static shapes
- {{}, {{2, 3, 3, 2}}}},
- {{{}, {{2, 6, 9, 8}}}, // Static shapes
- {{}, {{2, 2, 5, 2}}}}};
- // AVX2, AVX
- if (ov::with_cpu_x86_avx2() || ov::with_cpu_x86_avx()) {
- std::vector<std::vector<InputShape>> tmp = {{{{}, {{1, 7, 5, 3}}}, // Static shapes
- {{}, {{1, 1, 11, 2}}}},
- {{{}, {{2, 6, 7, 2}}}, // Static shapes
- {{}, {{2, 6, 2, 2}}}},
- {{{}, {{3, 5, 6, 3}}}, // Static shapes
- {{}, {{3, 1, 13, 2}}}},
- {{{}, {{4, 4, 5, 6}}}, // Static shapes
- {{}, {{4, 2, 7, 2}}}},
- {{{}, {{5, 3, 4, 5}}}, // Static shapes
- {{}, {{5, 3, 5, 2}}}},
- {{{}, {{4, 2, 7, 6}}}, // Static shapes
- {{}, {{4, 4, 4, 2}}}},
- {{{}, {{3, 3, 9, 7}}}, // Static shapes
- {{}, {{3, 1, 17, 2}}}},
- {{{}, {{2, 4, 9, 8}}}, // Static shapes
- {{}, {{2, 19, 1, 2}}}}};
- result.insert(result.end(), tmp.begin(), tmp.end());
- }
- // AVX512
- if (ov::with_cpu_x86_avx512f()) {
- std::vector<std::vector<InputShape>> tmp = {{{{}, {{1, 7, 2, 9}}}, // Static shapes
- {{}, {{1, 4, 5, 2}}}},
- {
- {{}, {{2, 6, 3, 10}}}, // Static shapes
- {{}, {{2, 3, 7, 2}}},
- },
- {{{}, {{3, 5, 2, 11}}}, // Static shapes
- {{}, {{3, 4, 6, 2}}}},
- {
- {{}, {{4, 4, 4, 12}}}, // Static shapes
- {{}, {{4, 5, 5, 2}}},
- },
- {
- {{}, {{5, 3, 2, 13}}}, // Static shapes
- {{}, {{5, 1, 31, 2}}},
- },
- {
- {{}, {{4, 3, 5, 14}}}, // Static shapes
- {{}, {{4, 4, 8, 2}}},
- },
- {
- {{}, {{3, 2, 2, 15}}}, // Static shapes
- {{}, {{3, 33, 1, 2}}},
- },
- {
- {{}, {{2, 1, 6, 16}}}, // Static shapes
- {{}, {{2, 8, 8, 2}}},
- },
- {
- {{}, {{2, 3, 7, 17}}}, // Static shapes
- {{}, {{2, 9, 9, 2}}},
- }};
- result.insert(result.end(), tmp.begin(), tmp.end());
- }
-
- return result;
-}
-
-INSTANTIATE_TEST_SUITE_P(smoke_static,
- GridSampleLayerTestCPU,
- ::testing::Combine(::testing::ValuesIn(getStaticShapes()),
- ::testing::ValuesIn(interpolateMode),
- ::testing::ValuesIn(paddingMode),
- ::testing::ValuesIn(alignCorners),
- ::testing::ValuesIn({ElementType::f32, ElementType::i32}),
- ::testing::ValuesIn({ElementType::f32}),
- ::testing::ValuesIn(getCPUInfo()),
- ::testing::Values(additionalConfig[0])),
- GridSampleLayerTestCPU::getTestCaseName);
-
-INSTANTIATE_TEST_SUITE_P(nightly_static_1,
- GridSampleLayerTestCPU,
- ::testing::Combine(::testing::ValuesIn(getStaticShapes()),
- ::testing::ValuesIn(interpolateMode),
- ::testing::ValuesIn(paddingMode),
- ::testing::ValuesIn(alignCorners),
- ::testing::ValuesIn({ElementType::bf16, ElementType::i8}),
- ::testing::ValuesIn({ElementType::f32, ElementType::bf16}),
- ::testing::ValuesIn(getCPUInfo()),
- ::testing::Values(additionalConfig[0])),
- GridSampleLayerTestCPU::getTestCaseName);
-
-INSTANTIATE_TEST_SUITE_P(nightly_static_2,
- GridSampleLayerTestCPU,
- ::testing::Combine(::testing::ValuesIn(getStaticShapes()),
- ::testing::ValuesIn(interpolateMode),
- ::testing::ValuesIn(paddingMode),
- ::testing::ValuesIn(alignCorners),
- ::testing::ValuesIn({ElementType::f32}),
- ::testing::ValuesIn({ElementType::bf16}),
- ::testing::ValuesIn(getCPUInfo()),
- ::testing::Values(additionalConfig[0])),
- GridSampleLayerTestCPU::getTestCaseName);
-
-const std::vector<std::vector<InputShape>> dynamicInSapes = {
- {{{ov::Dimension(1, 15), -1, -1, -1}, // Dynamic shape 0
- {{1, 1, 1, 1}, {6, 3, 1, 2}, {4, 5, 3, 1}, {2, 7, 2, 2}}}, // Target shapes
- {{ov::Dimension(1, 16), -1, -1, -1}, // Dynamic shape 1
- {{1, 1, 1, 2}, {6, 2, 2, 2}, {4, 1, 3, 2}, {2, 1, 2, 2}}}}, // Target shapes
- {{{-1, -1, -1, -1}, // Dynamic shape 0
- {{1, 2, 1, 5}, {3, 4, 2, 3}, {5, 6, 7, 1}, {7, 8, 2, 4}}}, // Target shapes
- {{-1, -1, -1, 2}, // Dynamic shape 1
- {{1, 2, 4, 2}, {3, 1, 7, 2}, {5, 2, 3, 2}, {7, 1, 5, 2}}}}, // Target shapes
- {{{ov::Dimension(2, 15), -1, -1, -1}, // Dynamic shape 0
- {{8, 3, 3, 3}, {6, 5, 2, 5}, {4, 7, 1, 11}, {2, 9, 3, 4}}}, // Target shapes
- {{-1, 3, 7, 2}, // Dynamic shape 1
- {{8, 3, 7, 2}, {6, 3, 7, 2}, {4, 3, 7, 2}, {2, 3, 7, 2}}}}, // Target shapes
- {{{3, 4, 4, 5}, // Dynamic shape 0
- {{3, 4, 4, 5}, {3, 4, 4, 5}, {3, 4, 4, 5}, {3, 4, 4, 5}}}, // Target shapes
- {{-1, -1, -1, 2}, // Dynamic shape 1
- {{3, 3, 4, 2}, {3, 1, 11, 2}, {3, 2, 5, 2}, {3, 3, 3, 2}}}}, // Target shapes
- {{{-1, -1, -1, -1}, // Dynamic shape 0
- {{1, 2, 1, 13}, {3, 4, 7, 2}, {5, 6, 3, 5}, {7, 8, 4, 4}}}, // Target shapes
- {{-1, -1, -1, -1}, // Dynamic shape 1
- {{1, 4, 4, 2}, {3, 3, 5, 2}, {5, 2, 7, 2}, {7, 1, 13, 2}}}}, // Target shapes
- {{{-1, -1, -1, -1}, // Dynamic shape 0
- {{2, 11, 1, 17}, {4, 9, 6, 3}, {6, 7, 7, 3}, {8, 3, 2, 11}}}, // Target shapes
- {{-1, -1, -1, 2}, // Dynamic shape 1
- {{2, 5, 4, 2}, {4, 1, 19, 2}, {6, 6, 3, 2}, {8, 1, 17, 2}}}}, // Target shapes
- {{{3, -1, -1, -1}, // Dynamic shape 0
- {{3, 2, 1, 23}, {3, 4, 3, 8}, {3, 6, 5, 5}, {3, 8, 31, 1}}}, // Target shapes
- {{-1, -1, -1, 2}, // Dynamic shape 1
- {{3, 31, 1, 2}, {3, 6, 4, 2}, {3, 23, 1, 2}, {3, 11, 2, 2}}}}, // Target shapes
- {{{-1, 3, -1, -1}, // Dynamic shape 0
- {{8, 3, 8, 4}, {6, 3, 33, 1}, {4, 3, 8, 6}, {2, 3, 8, 8}}}, // Target shapes
- {{-1, -1, -1, 2}, // Dynamic shape 1
- {{8, 8, 8, 2}, {6, 8, 7, 2}, {4, 1, 33, 2}, {2, 4, 8, 2}}}} // Target shapes
-};
-
-INSTANTIATE_TEST_SUITE_P(smoke_dynamic,
- GridSampleLayerTestCPU,
- ::testing::Combine(::testing::ValuesIn(dynamicInSapes),
- ::testing::ValuesIn(interpolateMode),
- ::testing::ValuesIn(paddingMode),
- ::testing::ValuesIn(alignCorners),
- ::testing::Values(ElementType::f32),
- ::testing::Values(ElementType::f32),
- ::testing::ValuesIn(getCPUInfo()),
- ::testing::Values(additionalConfig[0])),
- GridSampleLayerTestCPU::getTestCaseName);
-
-INSTANTIATE_TEST_SUITE_P(nightly_dynamic,
- GridSampleLayerTestCPU,
- ::testing::Combine(::testing::ValuesIn(dynamicInSapes),
- ::testing::ValuesIn(interpolateMode),
- ::testing::ValuesIn(paddingMode),
- ::testing::ValuesIn(alignCorners),
- ::testing::ValuesIn({ElementType::bf16, ElementType::i32}),
- ::testing::ValuesIn({ElementType::bf16}),
- ::testing::ValuesIn(getCPUInfo()),
- ::testing::Values(additionalConfig[0])),
- GridSampleLayerTestCPU::getTestCaseName);
-} // namespace
-} // namespace test
-} // namespace ov
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/arm/grid_sample.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/arm/grid_sample.cpp
new file mode 100644
index 00000000000..b357221e738
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/arm/grid_sample.cpp
@@ -0,0 +1,64 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "custom/single_layer_tests/classes/grid_sample.hpp"
+
+#include "utils/cpu_test_utils.hpp"
+
+using namespace CPUTestUtils;
+
+namespace ov::test {
+
+using namespace GridSample;
+
+namespace {
+
+std::vector<ov::AnyMap> additionalConfigARM = {{{ov::hint::inference_precision(ov::element::f32)}},
+ {{ov::hint::inference_precision(ov::element::f16)}}};
+
+std::vector<CPUSpecificParams> getCPUInfoARM() {
+ std::vector<CPUSpecificParams> resCPUParams;
+ resCPUParams.push_back(CPUSpecificParams{{}, {}, {"ref"}, "ref"});
+ return resCPUParams;
+}
+
+} // namespace
+
+INSTANTIATE_TEST_SUITE_P(ARM_smoke,
+ GridSampleLayerTestCPU,
+ ::testing::Combine(::testing::ValuesIn(getAllShapes()),
+ ::testing::ValuesIn(allInterpolationModes()),
+ ::testing::ValuesIn(allPaddingModes()),
+ ::testing::ValuesIn(alignCornersValues()),
+ ::testing::ValuesIn({ElementType::f32, ElementType::i32}),
+ ::testing::ValuesIn({ElementType::f32}),
+ ::testing::ValuesIn(getCPUInfoARM()),
+ ::testing::Values(additionalConfigARM[0])),
+ GridSampleLayerTestCPU::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(ARM_nightly_1,
+ GridSampleLayerTestCPU,
+ ::testing::Combine(::testing::ValuesIn(getAllShapes()),
+ ::testing::ValuesIn(allInterpolationModes()),
+ ::testing::ValuesIn(allPaddingModes()),
+ ::testing::ValuesIn(alignCornersValues()),
+ ::testing::ValuesIn({ElementType::f16, ElementType::i8}),
+ ::testing::ValuesIn({ElementType::f32, ElementType::f16}),
+ ::testing::ValuesIn(getCPUInfoARM()),
+ ::testing::Values(additionalConfigARM[0])),
+ GridSampleLayerTestCPU::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(ARM_nightly_2,
+ GridSampleLayerTestCPU,
+ ::testing::Combine(::testing::ValuesIn(getAllShapes()),
+ ::testing::ValuesIn(allInterpolationModes()),
+ ::testing::ValuesIn(allPaddingModes()),
+ ::testing::ValuesIn(alignCornersValues()),
+ ::testing::ValuesIn({ElementType::f32, ElementType::i32}),
+ ::testing::ValuesIn({ElementType::f16}),
+ ::testing::ValuesIn(getCPUInfoARM()),
+ ::testing::Values(additionalConfigARM[1])),
+ GridSampleLayerTestCPU::getTestCaseName);
+
+} // namespace ov::test
diff --git a/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/x64/grid_sample.cpp b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/x64/grid_sample.cpp
new file mode 100644
index 00000000000..2440984aee2
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/functional/custom/single_layer_tests/instances/x64/grid_sample.cpp
@@ -0,0 +1,72 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "custom/single_layer_tests/classes/grid_sample.hpp"
+
+#include "utils/cpu_test_utils.hpp"
+
+using namespace CPUTestUtils;
+
+namespace ov::test {
+
+using namespace GridSample;
+
+namespace {
+
+std::vector<ov::AnyMap> additionalConfigX64 = {{{ov::hint::inference_precision(ov::element::f32)}},
+ {{ov::hint::inference_precision(ov::element::bf16)}}};
+
+std::vector<CPUSpecificParams> getCPUInfoX64() {
+ std::vector<CPUSpecificParams> resCPUParams;
+ if (ov::with_cpu_x86_avx512f()) {
+ resCPUParams.push_back(CPUSpecificParams{{}, {}, {"jit_avx512"}, "jit_avx512"});
+ } else if (ov::with_cpu_x86_avx2()) {
+ resCPUParams.push_back(CPUSpecificParams{{}, {}, {"jit_avx2"}, "jit_avx2"});
+ } else if (ov::with_cpu_x86_avx()) {
+ resCPUParams.push_back(CPUSpecificParams{{}, {}, {"jit_avx"}, "jit_avx"});
+ } else if (ov::with_cpu_x86_sse42()) {
+ resCPUParams.push_back(CPUSpecificParams{{}, {}, {"jit_sse42"}, "jit_sse42"});
+ }
+ return resCPUParams;
+}
+
+} // namespace
+
+INSTANTIATE_TEST_SUITE_P(x64_smoke,
+ GridSampleLayerTestCPU,
+ ::testing::Combine(::testing::ValuesIn(getAllShapes()),
+ ::testing::ValuesIn(allInterpolationModes()),
+ ::testing::ValuesIn(allPaddingModes()),
+ ::testing::ValuesIn(alignCornersValues()),
+ ::testing::ValuesIn({ElementType::f32, ElementType::i32}),
+ ::testing::ValuesIn({ElementType::f32}),
+ ::testing::ValuesIn(getCPUInfoX64()),
+ ::testing::Values(additionalConfigX64[0])),
+ GridSampleLayerTestCPU::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(x64_nightly_1,
+ GridSampleLayerTestCPU,
+ ::testing::Combine(::testing::ValuesIn(getAllShapes()),
+ ::testing::ValuesIn(allInterpolationModes()),
+ ::testing::ValuesIn(allPaddingModes()),
+ ::testing::ValuesIn(alignCornersValues()),
+ ::testing::ValuesIn({ElementType::bf16, ElementType::i8}),
+ ::testing::ValuesIn({ElementType::f32, ElementType::bf16}),
+ ::testing::ValuesIn(getCPUInfoX64()),
+ ::testing::Values(additionalConfigX64[0])),
+ GridSampleLayerTestCPU::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(x64_nightly_2,
+ GridSampleLayerTestCPU,
+ ::testing::Combine(::testing::ValuesIn(getAllShapes()),
+ ::testing::ValuesIn(allInterpolationModes()),
+ ::testing::ValuesIn(allPaddingModes()),
+ ::testing::ValuesIn(alignCornersValues()),
+ ::testing::ValuesIn({ElementType::f32, ElementType::i32}),
+ ::testing::ValuesIn({ElementType::bf16}),
+ ::testing::ValuesIn(getCPUInfoX64()),
+ ::testing::Values(additionalConfigX64[1])),
+ GridSampleLayerTestCPU::getTestCaseName);
+
+} // namespace ov::test
diff --git a/src/plugins/intel_cpu/tests/unit/transformations/arm/grid_sample_decomposition.cpp b/src/plugins/intel_cpu/tests/unit/transformations/arm/grid_sample_decomposition.cpp
new file mode 100644
index 00000000000..482c964c1e1
--- /dev/null
+++ b/src/plugins/intel_cpu/tests/unit/transformations/arm/grid_sample_decomposition.cpp
@@ -0,0 +1,843 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/cpu_opset/arm/pass/grid_sample_decomposition.hpp"
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <string>
+#include <tuple>
+#include <vector>
+
+#include "common_test_utils/ov_test_utils.hpp"
+#include "openvino/core/model.hpp"
+#include "openvino/core/validation_util.hpp"
+#include "openvino/op/abs.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/divide.hpp"
+#include "openvino/op/equal.hpp"
+#include "openvino/op/floor.hpp"
+#include "openvino/op/floor_mod.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/gather_nd.hpp"
+#include "openvino/op/greater_eq.hpp"
+#include "openvino/op/grid_sample.hpp"
+#include "openvino/op/less.hpp"
+#include "openvino/op/less_eq.hpp"
+#include "openvino/op/logical_and.hpp"
+#include "openvino/op/maximum.hpp"
+#include "openvino/op/minimum.hpp"
+#include "openvino/op/multiply.hpp"
+#include "openvino/op/negative.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/range.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/result.hpp"
+#include "openvino/op/round.hpp"
+#include "openvino/op/select.hpp"
+#include "openvino/op/shape_of.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/transpose.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "openvino/pass/manager.hpp"
+#include "transformations/init_node_info.hpp"
+
+using namespace testing;
+using namespace ov::intel_cpu;
+
+// ========== Helper function implementations ==========
+namespace {
+using namespace ov;
+using namespace ov::op;
+
+struct Ctx {
+ std::shared_ptr<Node> n_dim, c_dim, h_in, w_in, h_out, w_out;
+ element::Type element_type = element::dynamic;
+ element::Type calc_type = element::f32;
+ std::shared_ptr<Node> c0, c1, c2, c0_5;
+ std::shared_ptr<Node> i32_0, i32_1, i32_2, i32_3, i32_neg1;
+ std::shared_ptr<Node> axis_0, axis_3;
+ std::shared_ptr<Node> to_nhwc_perm;
+ std::shared_ptr<Node> x, y, x_norm, y_norm;
+ std::shared_ptr<Node> data_nhwc, data_nchw;
+};
+
+std::shared_ptr<Node> to_f32(const std::shared_ptr<Node>& n) {
+ return std::make_shared<v0::Convert>(n, element::f32);
+}
+
+void init_common_constants(Ctx& ctx) {
+ ctx.c0 = v0::Constant::create(ctx.calc_type, {}, {0.0f});
+ ctx.c1 = v0::Constant::create(ctx.calc_type, {}, {1.0f});
+ ctx.c2 = v0::Constant::create(ctx.calc_type, {}, {2.0f});
+ ctx.c0_5 = v0::Constant::create(ctx.calc_type, {}, {0.5f});
+ ctx.i32_0 = v0::Constant::create(element::i32, {}, {0});
+ ctx.i32_1 = v0::Constant::create(element::i32, {}, {1});
+ ctx.i32_2 = v0::Constant::create(element::i32, {}, {2});
+ ctx.i32_3 = v0::Constant::create(element::i32, {}, {3});
+ ctx.i32_neg1 = v0::Constant::create(element::i32, {}, {-1});
+ ctx.axis_0 = ctx.i32_0;
+ ctx.axis_3 = ctx.i32_3;
+ ctx.to_nhwc_perm = v0::Constant::create(element::i32, {4}, {0, 2, 3, 1});
+}
+
+bool build_ctx(const std::shared_ptr<v0::Parameter>& data, const std::shared_ptr<v0::Parameter>& grid, Ctx& ctx) {
+ const auto& data_ps = data->get_partial_shape();
+ const auto& grid_ps = grid->get_partial_shape();
+ if ((data_ps.rank().is_static() && data_ps.rank().get_length() != 4) ||
+ (grid_ps.rank().is_static() && grid_ps.rank().get_length() != 4)) {
+ return false;
+ }
+ if (grid_ps.rank().is_static() && grid_ps.rank().get_length() == 4) {
+ const auto& last_dim = grid_ps[3];
+ if (last_dim.is_static() && last_dim.get_length() != 2) {
+ return false;
+ }
+ }
+ ctx.element_type = data->get_element_type();
+ ctx.calc_type = element::f32;
+ init_common_constants(ctx);
+ if (data->get_partial_shape().is_static()) {
+ const auto& s = data->get_partial_shape().to_shape();
+ ctx.n_dim = v0::Constant::create(element::i32, {}, {static_cast<int32_t>(s[0])});
+ ctx.c_dim = v0::Constant::create(element::i32, {}, {static_cast<int32_t>(s[1])});
+ ctx.h_in = v0::Constant::create(element::i32, {}, {static_cast<int32_t>(s[2])});
+ ctx.w_in = v0::Constant::create(element::i32, {}, {static_cast<int32_t>(s[3])});
+ } else {
+ auto dshape = std::make_shared<v3::ShapeOf>(data, element::i32);
+ ctx.n_dim = std::make_shared<v8::Gather>(dshape, ctx.i32_0, ctx.axis_0);
+ ctx.c_dim = std::make_shared<v8::Gather>(dshape, ctx.i32_1, ctx.axis_0);
+ ctx.h_in = std::make_shared<v8::Gather>(dshape, ctx.i32_2, ctx.axis_0);
+ ctx.w_in = std::make_shared<v8::Gather>(dshape, ctx.i32_3, ctx.axis_0);
+ }
+ if (grid->get_partial_shape().is_static()) {
+ const auto& s = grid->get_partial_shape().to_shape();
+ ctx.h_out = v0::Constant::create(element::i32, {}, {static_cast<int32_t>(s[1])});
+ ctx.w_out = v0::Constant::create(element::i32, {}, {static_cast<int32_t>(s[2])});
+ } else {
+ auto gshape = std::make_shared<v3::ShapeOf>(grid, element::i32);
+ ctx.h_out = std::make_shared<v8::Gather>(gshape, ctx.i32_1, ctx.axis_0);
+ ctx.w_out = std::make_shared<v8::Gather>(gshape, ctx.i32_2, ctx.axis_0);
+ }
+ std::shared_ptr<Node> grid_any = grid;
+ if (grid->get_element_type() != ctx.calc_type) {
+ grid_any = std::make_shared<v0::Convert>(grid, ctx.calc_type);
+ }
+ ctx.x_norm = std::make_shared<v8::Gather>(grid_any, ctx.i32_0, ctx.axis_3);
+ ctx.y_norm = std::make_shared<v8::Gather>(grid_any, ctx.i32_1, ctx.axis_3);
+ ctx.data_nchw = data;
+ ctx.data_nhwc = std::make_shared<v1::Transpose>(data, ctx.to_nhwc_perm);
+ return true;
+}
+
+void normalize_grid_to_pixels(const Ctx& ctx,
+ const std::shared_ptr<Node>& xg,
+ const std::shared_ptr<Node>& yg,
+ const std::shared_ptr<Node>& w,
+ const std::shared_ptr<Node>& h,
+ bool align_corners,
+ std::shared_ptr<Node>& xo,
+ std::shared_ptr<Node>& yo) {
+ if (align_corners) {
+ auto wm1 = std::make_shared<v1::Subtract>(w, ctx.c1);
+ auto hm1 = std::make_shared<v1::Subtract>(h, ctx.c1);
+ xo = std::make_shared<v1::Multiply>(std::make_shared<v1::Divide>(std::make_shared<v1::Add>(xg, ctx.c1), ctx.c2),
+ wm1);
+ yo = std::make_shared<v1::Multiply>(std::make_shared<v1::Divide>(std::make_shared<v1::Add>(yg, ctx.c1), ctx.c2),
+ hm1);
+ } else {
+ auto x_p1_w = std::make_shared<v1::Multiply>(std::make_shared<v1::Add>(xg, ctx.c1), w);
+ auto y_p1_h = std::make_shared<v1::Multiply>(std::make_shared<v1::Add>(yg, ctx.c1), h);
+ xo = std::make_shared<v1::Divide>(std::make_shared<v1::Subtract>(x_p1_w, ctx.c1), ctx.c2);
+ yo = std::make_shared<v1::Divide>(std::make_shared<v1::Subtract>(y_p1_h, ctx.c1), ctx.c2);
+ }
+}
+
+std::shared_ptr<Node> create_batch_indices(const Ctx& ctx) {
+ auto range = std::make_shared<v4::Range>(ctx.i32_0, ctx.n_dim, ctx.i32_1, element::i32);
+ auto n_dim_1d_bs = std::make_shared<v0::Unsqueeze>(ctx.n_dim, ctx.i32_0);
+ auto batch_shape = std::make_shared<v0::Concat>(OutputVector{n_dim_1d_bs,
+ v0::Constant::create(element::i32, {1}, {1}),
+ v0::Constant::create(element::i32, {1}, {1})},
+ 0);
+ auto batch_indices = std::make_shared<v1::Reshape>(range, batch_shape, false);
+ auto n_dim_1d = std::make_shared<v0::Unsqueeze>(ctx.n_dim, ctx.i32_0);
+ auto h_out_1d = std::make_shared<v0::Unsqueeze>(ctx.h_out, ctx.i32_0);
+ auto w_out_1d = std::make_shared<v0::Unsqueeze>(ctx.w_out, ctx.i32_0);
+ auto bshape = std::make_shared<v0::Concat>(OutputVector{n_dim_1d, h_out_1d, w_out_1d}, 0);
+ auto bcast = std::make_shared<v3::Broadcast>(batch_indices, bshape);
+ return std::make_shared<v0::Convert>(bcast, element::i32);
+}
+
+std::shared_ptr<Node> gather_hw_nhwc(const Ctx& ctx, const std::shared_ptr<Node>& x, const std::shared_ptr<Node>& y) {
+ auto xi = std::make_shared<v0::Convert>(x, element::i32);
+ auto yi = std::make_shared<v0::Convert>(y, element::i32);
+ auto bidx = create_batch_indices(ctx);
+ auto b = std::make_shared<v0::Unsqueeze>(bidx, ctx.i32_neg1);
+ auto yy = std::make_shared<v0::Unsqueeze>(yi, ctx.i32_neg1);
+ auto xx = std::make_shared<v0::Unsqueeze>(xi, ctx.i32_neg1);
+ auto indices = std::make_shared<v0::Concat>(OutputVector{b, yy, xx}, -1);
+ return std::make_shared<v8::GatherND>(ctx.data_nhwc, indices, 0);
+}
+
+std::shared_ptr<Node> reflect_coord(const Ctx& ctx,
+ const std::shared_ptr<Node>& coord,
+ const std::shared_ptr<Node>& size_f,
+ bool align_corners) {
+ auto size_is_one = std::make_shared<v1::Equal>(size_f, ctx.c1);
+ if (const auto size_flag = ov::util::get_constant_from_source(size_is_one)) {
+ const auto mask = size_flag->cast_vector<bool>();
+ if (!mask.empty() && mask.front()) {
+ return ctx.c0;
+ }
+ }
+ if (align_corners) {
+ auto size_m1 = std::make_shared<v1::Subtract>(size_f, ctx.c1);
+ auto period = std::make_shared<v1::Multiply>(ctx.c2, size_m1);
+ auto period_ok = std::make_shared<v1::Maximum>(period, ctx.c1);
+ auto r = std::make_shared<v1::FloorMod>(coord, period_ok);
+ auto d = std::make_shared<v1::Subtract>(r, size_m1);
+ auto ad = std::make_shared<v0::Abs>(d);
+ auto ref = std::make_shared<v1::Subtract>(size_m1, ad);
+ return std::make_shared<v1::Select>(size_is_one, ctx.c0, ref);
+ }
+ auto is_negative = std::make_shared<v1::Less>(coord, ctx.c0);
+ auto neg_coord = std::make_shared<v0::Negative>(coord);
+ auto neg_minus_one = std::make_shared<v1::Subtract>(neg_coord, ctx.c1);
+ auto coord_positive = std::make_shared<v1::Select>(is_negative, neg_minus_one, coord);
+ auto two_size = std::make_shared<v1::Multiply>(ctx.c2, size_f);
+ auto two_size_safe = std::make_shared<v1::Maximum>(two_size, ctx.c1);
+ auto mod = std::make_shared<v1::FloorMod>(coord_positive, two_size_safe);
+ auto need_ref = std::make_shared<v1::GreaterEqual>(mod, size_f);
+ auto two_size_m1 = std::make_shared<v1::Subtract>(two_size_safe, ctx.c1);
+ auto ref_val = std::make_shared<v1::Subtract>(two_size_m1, mod);
+ auto res = std::make_shared<v1::Select>(need_ref, ref_val, mod);
+ return std::make_shared<v1::Select>(size_is_one, ctx.c0, res);
+}
+
+std::shared_ptr<Node> reflect_index(const Ctx& ctx,
+ const std::shared_ptr<Node>& idx,
+ const std::shared_ptr<Node>& size_f,
+ bool align_corners) {
+ auto size_is_one = std::make_shared<v1::Equal>(size_f, ctx.c1);
+ if (const auto size_flag = ov::util::get_constant_from_source(size_is_one)) {
+ const auto mask = size_flag->cast_vector<bool>();
+ if (!mask.empty() && mask.front()) {
+ return ctx.c0;
+ }
+ }
+ if (align_corners) {
+ auto size_m1 = std::make_shared<v1::Subtract>(size_f, ctx.c1);
+ auto two_sm1 = std::make_shared<v1::Multiply>(ctx.c2, size_m1);
+ auto period_safe = std::make_shared<v1::Maximum>(two_sm1, ctx.c1);
+ auto mod1 = std::make_shared<v1::FloorMod>(idx, period_safe);
+ auto mod2 = std::make_shared<v1::FloorMod>(std::make_shared<v1::Add>(mod1, period_safe), period_safe);
+ auto flipped = std::make_shared<v1::Subtract>(two_sm1, mod2);
+ auto cond = std::make_shared<v1::GreaterEqual>(mod2, size_f);
+ auto ref = std::make_shared<v1::Select>(cond, flipped, mod2);
+ return std::make_shared<v1::Select>(size_is_one, ctx.c0, ref);
+ }
+ auto two_s = std::make_shared<v1::Multiply>(ctx.c2, size_f);
+ auto two_s_safe = std::make_shared<v1::Maximum>(two_s, ctx.c1);
+ auto mod1 = std::make_shared<v1::FloorMod>(idx, two_s_safe);
+ auto mod2 = std::make_shared<v1::FloorMod>(std::make_shared<v1::Add>(mod1, two_s_safe), two_s_safe);
+ auto need_rf = std::make_shared<v1::GreaterEqual>(mod2, size_f);
+ auto two_s_m1 = std::make_shared<v1::Subtract>(two_s_safe, ctx.c1);
+ auto ref_val = std::make_shared<v1::Subtract>(two_s_m1, mod2);
+ auto res = std::make_shared<v1::Select>(need_rf, ref_val, mod2);
+ return std::make_shared<v1::Select>(size_is_one, ctx.c0, res);
+}
+
+static std::shared_ptr<Node> inside_mask_indexed(const Ctx& ctx,
+ const std::shared_ptr<Node>& xi,
+ const std::shared_ptr<Node>& yi,
+ const std::shared_ptr<Node>& w_in_f,
+ const std::shared_ptr<Node>& h_in_f);
+
+void clamp_indices_inplace(const Ctx& ctx,
+ std::shared_ptr<Node>& xi,
+ std::shared_ptr<Node>& yi,
+ const std::shared_ptr<Node>& w_in_f,
+ const std::shared_ptr<Node>& h_in_f) {
+ auto w_m1 = std::make_shared<v1::Subtract>(w_in_f, ctx.c1);
+ auto h_m1 = std::make_shared<v1::Subtract>(h_in_f, ctx.c1);
+ xi = std::make_shared<v1::Minimum>(std::make_shared<v1::Maximum>(xi, ctx.c0), w_m1);
+ yi = std::make_shared<v1::Minimum>(std::make_shared<v1::Maximum>(yi, ctx.c0), h_m1);
+}
+
+std::shared_ptr<Node> build_nearest_nhwc(const Ctx& ctx, const ov::op::v9::GridSample::Attributes& attrs) {
+ const bool is_zeros = attrs.padding_mode == v9::GridSample::PaddingMode::ZEROS;
+ const bool is_reflection = attrs.padding_mode == v9::GridSample::PaddingMode::REFLECTION;
+ auto w_in_f = to_f32(ctx.w_in);
+ auto h_in_f = to_f32(ctx.h_in);
+ std::shared_ptr<Node> x_idx = std::make_shared<v5::Round>(ctx.x, v5::Round::RoundMode::HALF_TO_EVEN);
+ std::shared_ptr<Node> y_idx = std::make_shared<v5::Round>(ctx.y, v5::Round::RoundMode::HALF_TO_EVEN);
+ auto x_unclamped = x_idx;
+ auto y_unclamped = y_idx;
+ if (is_reflection) {
+ x_idx = reflect_index(ctx, x_idx, w_in_f, attrs.align_corners);
+ y_idx = reflect_index(ctx, y_idx, h_in_f, attrs.align_corners);
+ }
+ clamp_indices_inplace(ctx, x_idx, y_idx, w_in_f, h_in_f);
+ auto out = gather_hw_nhwc(ctx, x_idx, y_idx);
+ if (is_zeros) {
+ auto ok = inside_mask_indexed(ctx, x_unclamped, y_unclamped, w_in_f, h_in_f);
+ auto mask = std::make_shared<v0::Convert>(ok, ctx.calc_type);
+ auto maske = std::make_shared<v0::Unsqueeze>(mask, ctx.i32_neg1);
+ if (ctx.element_type != ctx.calc_type) {
+ out = std::make_shared<v0::Convert>(out, ctx.calc_type);
+ }
+ out = std::make_shared<v1::Multiply>(out, maske);
+ if (ctx.element_type != ctx.calc_type) {
+ out = std::make_shared<v0::Convert>(out, ctx.element_type);
+ }
+ }
+ return out;
+}
+
+// inside mask for zeros (same as in transformation)
+static std::shared_ptr<Node> inside_mask_indexed(const Ctx& ctx,
+ const std::shared_ptr<Node>& xi,
+ const std::shared_ptr<Node>& yi,
+ const std::shared_ptr<Node>& w_in_f,
+ const std::shared_ptr<Node>& h_in_f) {
+ auto x_ge_0 = std::make_shared<v1::GreaterEqual>(xi, ctx.c0);
+ auto y_ge_0 = std::make_shared<v1::GreaterEqual>(yi, ctx.c0);
+ auto x_lt_w = std::make_shared<v1::Less>(xi, w_in_f);
+ auto y_lt_h = std::make_shared<v1::Less>(yi, h_in_f);
+ return std::make_shared<v1::LogicalAnd>(std::make_shared<v1::LogicalAnd>(x_ge_0, x_lt_w),
+ std::make_shared<v1::LogicalAnd>(y_ge_0, y_lt_h));
+}
+
+std::shared_ptr<Node> build_bilinear_nhwc(const Ctx& ctx, const ov::op::v9::GridSample::Attributes& attrs) {
+ const bool is_border = attrs.padding_mode == v9::GridSample::PaddingMode::BORDER;
+ const bool is_zeros = attrs.padding_mode == v9::GridSample::PaddingMode::ZEROS;
+ const bool is_reflection = attrs.padding_mode == v9::GridSample::PaddingMode::REFLECTION;
+
+ auto w_in_f = to_f32(ctx.w_in);
+ auto h_in_f = to_f32(ctx.h_in);
+
+ std::shared_ptr<Node> x_pad = ctx.x, y_pad = ctx.y;
+ if (is_reflection && attrs.align_corners) {
+ x_pad = reflect_coord(ctx, ctx.x, w_in_f, true);
+ y_pad = reflect_coord(ctx, ctx.y, h_in_f, true);
+ }
+
+ std::shared_ptr<Node> x0 = std::make_shared<v0::Floor>(x_pad);
+ std::shared_ptr<Node> y0 = std::make_shared<v0::Floor>(y_pad);
+ std::shared_ptr<Node> x1 = std::make_shared<v1::Add>(x0, ctx.c1);
+ std::shared_ptr<Node> y1 = std::make_shared<v1::Add>(y0, ctx.c1);
+
+ std::shared_ptr<Node> dx = std::make_shared<v1::Subtract>(x_pad, x0);
+ std::shared_ptr<Node> dy = std::make_shared<v1::Subtract>(y_pad, y0);
+ if (!is_zeros) {
+ auto eps = v0::Constant::create(ctx.calc_type, {}, {1e-7F});
+ auto one_m_eps = std::make_shared<v1::Subtract>(ctx.c1, eps);
+ dx = std::make_shared<v1::Minimum>(std::make_shared<v1::Maximum>(dx, ctx.c0), one_m_eps);
+ dy = std::make_shared<v1::Minimum>(std::make_shared<v1::Maximum>(dy, ctx.c0), one_m_eps);
+ }
+ auto omdx = std::make_shared<v1::Subtract>(ctx.c1, dx);
+ auto omdy = std::make_shared<v1::Subtract>(ctx.c1, dy);
+
+ std::vector<std::shared_ptr<Node>> weights{std::make_shared<v1::Multiply>(omdx, omdy),
+ std::make_shared<v1::Multiply>(dx, omdy),
+ std::make_shared<v1::Multiply>(omdx, dy),
+ std::make_shared<v1::Multiply>(dx, dy)};
+
+ if (is_reflection) {
+ x0 = reflect_index(ctx, x0, w_in_f, attrs.align_corners);
+ x1 = reflect_index(ctx, x1, w_in_f, attrs.align_corners);
+ y0 = reflect_index(ctx, y0, h_in_f, attrs.align_corners);
+ y1 = reflect_index(ctx, y1, h_in_f, attrs.align_corners);
+ }
+
+ std::vector<std::shared_ptr<Node>> xs{x0, x1, x0, x1};
+ std::vector<std::shared_ptr<Node>> ys{y0, y0, y1, y1};
+
+ std::shared_ptr<Node> res;
+ for (int i = 0; i < 4; ++i) {
+ auto xi = xs[i], yi = ys[i];
+ if (is_zeros || is_border) {
+ clamp_indices_inplace(ctx, xi, yi, w_in_f, h_in_f);
+ }
+ auto v = gather_hw_nhwc(ctx, xi, yi);
+ if (ctx.element_type != ctx.calc_type) {
+ v = std::make_shared<v0::Convert>(v, ctx.calc_type);
+ }
+ if (is_zeros) {
+ auto ok = inside_mask_indexed(ctx, xs[i], ys[i], w_in_f, h_in_f);
+ auto mask = std::make_shared<v0::Convert>(ok, ctx.calc_type);
+ v = std::make_shared<v1::Multiply>(v, std::make_shared<v0::Unsqueeze>(mask, ctx.i32_neg1));
+ }
+ auto wexp = std::make_shared<v0::Unsqueeze>(weights[i], ctx.i32_neg1);
+ auto tap = std::make_shared<v1::Multiply>(v, wexp);
+ res = res ? std::shared_ptr<Node>(std::make_shared<v1::Add>(res, tap)) : tap;
+ }
+ if (ctx.element_type != ctx.calc_type) {
+ res = std::make_shared<v0::Convert>(res, ctx.element_type);
+ }
+ return res;
+}
+
+// Bicubic: mirror the transformation structure (weights + 4x4 taps accumulation)
+std::shared_ptr<Node> build_bicubic_nhwc(const Ctx& ctx, const ov::op::v9::GridSample::Attributes& attrs) {
+ const bool is_border = attrs.padding_mode == v9::GridSample::PaddingMode::BORDER;
+ const bool is_zeros = attrs.padding_mode == v9::GridSample::PaddingMode::ZEROS;
+ const bool is_reflection = attrs.padding_mode == v9::GridSample::PaddingMode::REFLECTION;
+ auto w_in_f = to_f32(ctx.w_in);
+ auto h_in_f = to_f32(ctx.h_in);
+ std::shared_ptr<Node> x_pad = ctx.x, y_pad = ctx.y;
+ if (is_reflection && attrs.align_corners) {
+ x_pad = reflect_coord(ctx, ctx.x, w_in_f, true);
+ y_pad = reflect_coord(ctx, ctx.y, h_in_f, true);
+ }
+ auto x0 = std::make_shared<v0::Floor>(x_pad);
+ auto y0 = std::make_shared<v0::Floor>(y_pad);
+ auto dx_raw = std::make_shared<v1::Subtract>(x_pad, x0);
+ auto dy_raw = std::make_shared<v1::Subtract>(y_pad, y0);
+ std::shared_ptr<Node> dx = dx_raw;
+ std::shared_ptr<Node> dy = dy_raw;
+ if (!is_zeros) {
+ auto eps = v0::Constant::create(ctx.calc_type, {}, {1e-7F});
+ auto one_m_eps = std::make_shared<v1::Subtract>(ctx.c1, eps);
+ dx = std::make_shared<v1::Minimum>(std::make_shared<v1::Maximum>(dx_raw, ctx.c0), one_m_eps);
+ dy = std::make_shared<v1::Minimum>(std::make_shared<v1::Maximum>(dy_raw, ctx.c0), one_m_eps);
+ }
+ auto w_is_one = std::make_shared<v1::Equal>(w_in_f, ctx.c1);
+ auto h_is_one = std::make_shared<v1::Equal>(h_in_f, ctx.c1);
+ dx = std::make_shared<v1::Select>(w_is_one, ctx.c0, dx);
+ dy = std::make_shared<v1::Select>(h_is_one, ctx.c0, dy);
+ auto a = v0::Constant::create(ctx.calc_type, {}, {-0.75F});
+ auto c3 = v0::Constant::create(ctx.calc_type, {}, {3.0F});
+ auto c4 = v0::Constant::create(ctx.calc_type, {}, {4.0F});
+ auto c5 = v0::Constant::create(ctx.calc_type, {}, {5.0F});
+ auto c8 = v0::Constant::create(ctx.calc_type, {}, {8.0F});
+ auto dx_p1 = std::make_shared<v1::Add>(dx, ctx.c1);
+ auto one_m_dx = std::make_shared<v1::Subtract>(ctx.c1, dx);
+ auto two_m_dx = std::make_shared<v1::Subtract>(ctx.c2, dx);
+ auto dx2 = std::make_shared<v1::Multiply>(dx, dx);
+ auto dx_p1_2 = std::make_shared<v1::Multiply>(dx_p1, dx_p1);
+ auto one_m_dx_2 = std::make_shared<v1::Multiply>(one_m_dx, one_m_dx);
+ auto two_m_dx_2 = std::make_shared<v1::Multiply>(two_m_dx, two_m_dx);
+ auto a_dx_p1 = std::make_shared<v1::Multiply>(a, dx_p1);
+ auto a5 = std::make_shared<v1::Multiply>(c5, a);
+ auto term_v0_1 = std::make_shared<v1::Subtract>(a_dx_p1, a5);
+ auto term_v0_2 = std::make_shared<v1::Add>(std::make_shared<v1::Multiply>(term_v0_1, dx_p1),
+ std::make_shared<v1::Multiply>(c8, a));
+ std::shared_ptr<Node> w_x0 = std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(term_v0_2, dx_p1),
+ std::make_shared<v1::Multiply>(c4, a));
+ auto ap2 = std::make_shared<v1::Add>(a, ctx.c2);
+ auto ap3 = std::make_shared<v1::Add>(a, c3);
+ std::shared_ptr<Node> w_x1 = std::make_shared<v1::Add>(
+ std::make_shared<v1::Multiply>(std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(ap2, dx), ap3),
+ dx2),
+ ctx.c1);
+ std::shared_ptr<Node> w_x2 = std::make_shared<v1::Add>(
+ std::make_shared<v1::Multiply>(
+ std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(ap2, one_m_dx), ap3),
+ one_m_dx_2),
+ ctx.c1);
+ auto a_two_m_dx = std::make_shared<v1::Multiply>(a, two_m_dx);
+ auto term_v3_1 = std::make_shared<v1::Subtract>(a_two_m_dx, a5);
+ auto term_v3_2 = std::make_shared<v1::Add>(std::make_shared<v1::Multiply>(term_v3_1, two_m_dx),
+ std::make_shared<v1::Multiply>(c8, a));
+ std::shared_ptr<Node> w_x3 = std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(term_v3_2, two_m_dx),
+ std::make_shared<v1::Multiply>(c4, a));
+ w_x0 = std::make_shared<v1::Select>(w_is_one, ctx.c0, w_x0);
+ w_x1 = std::make_shared<v1::Select>(w_is_one, ctx.c1, w_x1);
+ w_x2 = std::make_shared<v1::Select>(w_is_one, ctx.c0, w_x2);
+ w_x3 = std::make_shared<v1::Select>(w_is_one, ctx.c0, w_x3);
+ auto dy_p1 = std::make_shared<v1::Add>(dy, ctx.c1);
+ auto one_m_dy = std::make_shared<v1::Subtract>(ctx.c1, dy);
+ auto two_m_dy = std::make_shared<v1::Subtract>(ctx.c2, dy);
+ auto dy2 = std::make_shared<v1::Multiply>(dy, dy);
+ auto dy_p1_2 = std::make_shared<v1::Multiply>(dy_p1, dy_p1);
+ auto one_m_dy_2 = std::make_shared<v1::Multiply>(one_m_dy, one_m_dy);
+ auto two_m_dy_2 = std::make_shared<v1::Multiply>(two_m_dy, two_m_dy);
+ auto a_dy_p1 = std::make_shared<v1::Multiply>(a, dy_p1);
+ auto term_wy0_1 = std::make_shared<v1::Subtract>(a_dy_p1, a5);
+ auto term_wy0_2 = std::make_shared<v1::Add>(std::make_shared<v1::Multiply>(term_wy0_1, dy_p1),
+ std::make_shared<v1::Multiply>(c8, a));
+ std::shared_ptr<Node> w_y0 = std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(term_wy0_2, dy_p1),
+ std::make_shared<v1::Multiply>(c4, a));
+ std::shared_ptr<Node> w_y1 = std::make_shared<v1::Add>(
+ std::make_shared<v1::Multiply>(std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(ap2, dy), ap3),
+ dy2),
+ ctx.c1);
+ std::shared_ptr<Node> w_y2 = std::make_shared<v1::Add>(
+ std::make_shared<v1::Multiply>(
+ std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(ap2, one_m_dy), ap3),
+ one_m_dy_2),
+ ctx.c1);
+ auto a_two_m_dy = std::make_shared<v1::Multiply>(a, two_m_dy);
+ auto term_wy3_1 = std::make_shared<v1::Subtract>(a_two_m_dy, a5);
+ auto term_wy3_2 = std::make_shared<v1::Add>(std::make_shared<v1::Multiply>(term_wy3_1, two_m_dy),
+ std::make_shared<v1::Multiply>(c8, a));
+ std::shared_ptr<Node> w_y3 = std::make_shared<v1::Subtract>(std::make_shared<v1::Multiply>(term_wy3_2, two_m_dy),
+ std::make_shared<v1::Multiply>(c4, a));
+ w_y0 = std::make_shared<v1::Select>(h_is_one, ctx.c0, w_y0);
+ w_y1 = std::make_shared<v1::Select>(h_is_one, ctx.c1, w_y1);
+ w_y2 = std::make_shared<v1::Select>(h_is_one, ctx.c0, w_y2);
+ w_y3 = std::make_shared<v1::Select>(h_is_one, ctx.c0, w_y3);
+ std::vector<std::shared_ptr<Node>> x_idx{
+ std::make_shared<v1::Subtract>(x0, ctx.c1),
+ x0,
+ std::make_shared<v1::Add>(x0, ctx.c1),
+ std::make_shared<v1::Add>(x0, ctx.c2),
+ };
+ std::vector<std::shared_ptr<Node>> y_idx{
+ std::make_shared<v1::Subtract>(y0, ctx.c1),
+ y0,
+ std::make_shared<v1::Add>(y0, ctx.c1),
+ std::make_shared<v1::Add>(y0, ctx.c2),
+ };
+ std::vector<std::shared_ptr<Node>> wx{w_x0, w_x1, w_x2, w_x3};
+ std::vector<std::shared_ptr<Node>> wy{w_y0, w_y1, w_y2, w_y3};
+ std::vector<std::shared_ptr<Node>> x_ok(4), y_ok(4);
+ if (is_zeros) {
+ for (int i = 0; i < 4; ++i) {
+ x_ok[i] = std::make_shared<v1::LogicalAnd>(std::make_shared<v1::GreaterEqual>(x_idx[i], ctx.c0),
+ std::make_shared<v1::Less>(x_idx[i], w_in_f));
+ y_ok[i] = std::make_shared<v1::LogicalAnd>(std::make_shared<v1::GreaterEqual>(y_idx[i], ctx.c0),
+ std::make_shared<v1::Less>(y_idx[i], h_in_f));
+ }
+ }
+ if (is_reflection) {
+ for (int i = 0; i < 4; ++i) {
+ x_idx[i] = reflect_index(ctx, x_idx[i], w_in_f, attrs.align_corners);
+ y_idx[i] = reflect_index(ctx, y_idx[i], h_in_f, attrs.align_corners);
+ }
+ } else {
+ auto w_is_one_chk = std::make_shared<v1::Equal>(w_in_f, ctx.c1);
+ auto h_is_one_chk = std::make_shared<v1::Equal>(h_in_f, ctx.c1);
+ for (int i = 0; i < 4; ++i) {
+ x_idx[i] = std::make_shared<v1::Select>(w_is_one_chk, ctx.c0, x_idx[i]);
+ y_idx[i] = std::make_shared<v1::Select>(h_is_one_chk, ctx.c0, y_idx[i]);
+ }
+ }
+ std::shared_ptr<Node> sum;
+ for (int iy = 0; iy < 4; ++iy) {
+ std::shared_ptr<Node> row;
+ for (int ix = 0; ix < 4; ++ix) {
+ auto xi = x_idx[ix];
+ auto yi = y_idx[iy];
+ if (is_zeros || is_border) {
+ clamp_indices_inplace(ctx, xi, yi, w_in_f, h_in_f);
+ }
+ auto val = gather_hw_nhwc(ctx, xi, yi);
+ if (ctx.element_type != ctx.calc_type) {
+ val = std::make_shared<v0::Convert>(val, ctx.calc_type);
+ }
+ auto wxy = std::make_shared<v1::Multiply>(wx[ix], wy[iy]);
+ if (is_zeros) {
+ auto ok = std::make_shared<v1::LogicalAnd>(x_ok[ix], y_ok[iy]);
+ auto mask = std::make_shared<v0::Convert>(ok, ctx.calc_type);
+ val = std::make_shared<v1::Multiply>(val, std::make_shared<v0::Unsqueeze>(mask, ctx.i32_neg1));
+ }
+ auto wexp = std::make_shared<v0::Unsqueeze>(wxy, ctx.i32_neg1);
+ auto tap = std::make_shared<v1::Multiply>(val, wexp);
+ row = row ? std::shared_ptr<Node>(std::make_shared<v1::Add>(row, tap)) : tap;
+ }
+ sum = sum ? std::shared_ptr<Node>(std::make_shared<v1::Add>(sum, row)) : row;
+ }
+ if (ctx.element_type != ctx.calc_type) {
+ sum = std::make_shared<v0::Convert>(sum, ctx.element_type);
+ }
+ return sum;
+}
+} // namespace
+
+static std::shared_ptr<ov::Model> create_expected_decomposed_pattern(const ov::PartialShape& data_shape,
+ const ov::PartialShape& grid_shape,
+ const ov::element::Type& data_type,
+ const ov::element::Type& grid_type,
+ const ov::op::v9::GridSample::Attributes& attrs) {
+ auto data = std::make_shared<ov::op::v0::Parameter>(data_type, data_shape);
+ auto grid = std::make_shared<ov::op::v0::Parameter>(grid_type, grid_shape);
+
+ // Replicate transformation decision points: for some NEAREST combos keep original
+ const bool is_f32_data = data_type == ov::element::f32;
+ const bool is_f32_grid = grid_type == ov::element::f32;
+ const bool nearest_reflection_border_bad = attrs.mode == ov::op::v9::GridSample::InterpolationMode::NEAREST &&
+ (attrs.padding_mode == ov::op::v9::GridSample::PaddingMode::REFLECTION ||
+ attrs.padding_mode == ov::op::v9::GridSample::PaddingMode::BORDER) &&
+ !attrs.align_corners;
+ const bool nearest_zeros_bad = attrs.mode == ov::op::v9::GridSample::InterpolationMode::NEAREST &&
+ attrs.padding_mode == ov::op::v9::GridSample::PaddingMode::ZEROS &&
+ !attrs.align_corners;
+ const bool bicubic_zeros_bad = attrs.mode == ov::op::v9::GridSample::InterpolationMode::BICUBIC &&
+ attrs.padding_mode == ov::op::v9::GridSample::PaddingMode::ZEROS &&
+ !attrs.align_corners;
+
+ std::shared_ptr<ov::Node> out_node;
+ if (is_f32_data && is_f32_grid && (nearest_reflection_border_bad || nearest_zeros_bad || bicubic_zeros_bad)) {
+ // Keep original GridSample
+ out_node = std::make_shared<ov::op::v9::GridSample>(data, grid, attrs);
+ } else if ((!is_f32_data || !is_f32_grid) && nearest_reflection_border_bad) {
+ // Fallback: convert to f32, use GridSample, convert back
+ auto data_f32 = std::make_shared<ov::op::v0::Convert>(data, ov::element::f32);
+ auto grid_f32 = std::make_shared<ov::op::v0::Convert>(grid, ov::element::f32);
+ auto gs = std::make_shared<ov::op::v9::GridSample>(data_f32, grid_f32, attrs);
+ out_node = std::make_shared<ov::op::v0::Convert>(gs, data_type);
+ } else {
+ // Manual decomposition path (generic dynamic)
+ Ctx ctx{};
+ if (!build_ctx(data, grid, ctx)) {
+ // Fallback to original if shapes are not 4D
+ out_node = std::make_shared<ov::op::v9::GridSample>(data, grid, attrs);
+ } else {
+ auto w_in_f = to_f32(ctx.w_in);
+ auto h_in_f = to_f32(ctx.h_in);
+ normalize_grid_to_pixels(ctx, ctx.x_norm, ctx.y_norm, w_in_f, h_in_f, attrs.align_corners, ctx.x, ctx.y);
+ std::shared_ptr<ov::Node> result_nhwc;
+ switch (attrs.mode) {
+ case ov::op::v9::GridSample::InterpolationMode::NEAREST:
+ result_nhwc = build_nearest_nhwc(ctx, attrs);
+ break;
+ case ov::op::v9::GridSample::InterpolationMode::BILINEAR:
+ result_nhwc = build_bilinear_nhwc(ctx, attrs);
+ break;
+ case ov::op::v9::GridSample::InterpolationMode::BICUBIC:
+ result_nhwc = build_bicubic_nhwc(ctx, attrs);
+ break;
+ }
+ auto to_nchw = ov::op::v0::Constant::create(ov::element::i32, {4}, {0, 3, 1, 2});
+ out_node = std::make_shared<ov::op::v1::Transpose>(result_nhwc, to_nchw);
+ }
+ }
+
+ out_node->set_friendly_name("GridSample_ref");
+ auto result = std::make_shared<ov::op::v0::Result>(out_node);
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{data, grid});
+}
+
+// ========== Test parameters structure ==========
+struct GridSampleTestParams {
+ ov::PartialShape data_shape;
+ ov::PartialShape grid_shape;
+ ov::element::Type data_type;
+ ov::element::Type grid_type;
+ bool align_corners;
+ ov::op::v9::GridSample::InterpolationMode interp_mode;
+ ov::op::v9::GridSample::PaddingMode padding_mode;
+};
+
+// ========== Base test classes ==========
+class GridSampleDecompositionTest : public TransformationTestsF, public WithParamInterface<GridSampleTestParams> {
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<GridSampleTestParams>& obj) {
+ const auto& p = obj.param;
+ std::ostringstream result;
+ result << "data_shape=" << p.data_shape << "_grid_shape=" << p.grid_shape << "_data_type=" << p.data_type
+ << "_grid_type=" << p.grid_type << "_align=" << p.align_corners
+ << "_interp=" << ov::as_string(p.interp_mode) << "_padding=" << ov::as_string(p.padding_mode);
+ return result.str();
+ }
+
+protected:
+ void SetUp() override {
+ TransformationTestsF::SetUp();
+ // Use default comparator; structural equality is not strictly enforced here
+
+ const auto& p = GetParam();
+ auto data = std::make_shared<ov::op::v0::Parameter>(p.data_type, p.data_shape);
+ auto grid = std::make_shared<ov::op::v0::Parameter>(p.grid_type, p.grid_shape);
+
+ ov::op::v9::GridSample::Attributes attrs{p.align_corners, p.interp_mode, p.padding_mode};
+ auto grid_sample = std::make_shared<ov::op::v9::GridSample>(data, grid, attrs);
+ auto result = std::make_shared<ov::op::v0::Result>(grid_sample);
+ model = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{data, grid});
+
+ model_ref = create_expected_decomposed_pattern(p.data_shape, p.grid_shape, p.data_type, p.grid_type, attrs);
+ manager.register_pass<ov::intel_cpu::GridSampleDecomposition>();
+ }
+};
+
+TEST_P(GridSampleDecompositionTest, CompareGraphs) {}
+
+// ========== Test parameters ==========
+const std::vector<GridSampleTestParams> testStaticShapes = {
+ // BILINEAR + BORDER - static shapes
+ {{1, 2, 4, 4},
+ {1, 3, 3, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ {{2, 3, 8, 8},
+ {2, 5, 5, 2},
+ ov::element::f32,
+ ov::element::f32,
+ true,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // NEAREST mode
+ {{1, 2, 4, 4},
+ {1, 3, 3, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::NEAREST,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // BICUBIC mode
+ {{1, 2, 4, 4},
+ {1, 3, 3, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BICUBIC,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // ZEROS padding
+ {{1, 2, 4, 4},
+ {1, 3, 3, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::ZEROS},
+
+ // REFLECTION padding
+ {{1, 2, 4, 4},
+ {1, 3, 3, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::REFLECTION},
+
+ // Different data types
+ {{1, 2, 4, 4},
+ {1, 3, 3, 2},
+ ov::element::f16,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ {{1, 2, 4, 4},
+ {1, 3, 3, 2},
+ ov::element::i32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // Large dimensions
+ {{4, 16, 64, 64},
+ {4, 32, 32, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // Edge cases
+ {{1, 1, 1, 1},
+ {1, 1, 1, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // Typical CV size
+ {{10, 3, 224, 224},
+ {10, 112, 112, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // All combinations for comprehensive coverage
+ {{2, 3, 5, 7},
+ {2, 4, 6, 2},
+ ov::element::f32,
+ ov::element::f32,
+ true,
+ ov::op::v9::GridSample::InterpolationMode::NEAREST,
+ ov::op::v9::GridSample::PaddingMode::ZEROS},
+
+ {{1, 1, 8, 8},
+ {1, 4, 4, 2},
+ ov::element::f16,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::NEAREST,
+ ov::op::v9::GridSample::PaddingMode::REFLECTION},
+
+ {{3, 2, 6, 10},
+ {3, 5, 8, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BICUBIC,
+ ov::op::v9::GridSample::PaddingMode::ZEROS},
+
+ {{1, 4, 12, 16},
+ {1, 10, 14, 2},
+ ov::element::f32,
+ ov::element::f32,
+ true,
+ ov::op::v9::GridSample::InterpolationMode::BICUBIC,
+ ov::op::v9::GridSample::PaddingMode::REFLECTION},
+};
+
+const std::vector<GridSampleTestParams> testDynamicShapes = {
+ // Dynamic batch dimension
+ {{-1, 3, 8, 8},
+ {-1, 4, 4, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // Dynamic spatial dimensions
+ {{2, 3, -1, -1},
+ {2, -1, -1, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::BILINEAR,
+ ov::op::v9::GridSample::PaddingMode::BORDER},
+
+ // Fully dynamic
+ {{-1, -1, -1, -1},
+ {-1, -1, -1, 2},
+ ov::element::f32,
+ ov::element::f32,
+ false,
+ ov::op::v9::GridSample::InterpolationMode::NEAREST,
+ ov::op::v9::GridSample::PaddingMode::ZEROS},
+
+ // Dynamic with different modes
+ {{-1, 3, -1, -1},
+ {-1, -1, -1, 2},
+ ov::element::f32,
+ ov::element::f32,
+ true,
+ ov::op::v9::GridSample::InterpolationMode::BICUBIC,
+ ov::op::v9::GridSample::PaddingMode::REFLECTION},
+};
+
+INSTANTIATE_TEST_SUITE_P(StaticShapes,
+ GridSampleDecompositionTest,
+ ::testing::ValuesIn(testStaticShapes),
+ GridSampleDecompositionTest::getTestCaseName);
+
+INSTANTIATE_TEST_SUITE_P(DynamicShapes,
+ GridSampleDecompositionTest,
+ ::testing::ValuesIn(testDynamicShapes),
+ GridSampleDecompositionTest::getTestCaseName);