Skip to content

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

[ONNX FE] Support TensorScatter (opset 24) (#38888)

Szymon Gutaj6aabd6aa418b
diff --git a/src/core/reference/include/openvino/reference/floor_mod.hpp b/src/core/reference/include/openvino/reference/floor_mod.hpp
index f593ceeb45e..5c084c182ec 100644
--- a/src/core/reference/include/openvino/reference/floor_mod.hpp
+++ b/src/core/reference/include/openvino/reference/floor_mod.hpp
@@ -6,7 +6,9 @@
#include <cmath>
#include <cstddef>
+#include <limits>
+#include "openvino/core/except.hpp"
#include "openvino/reference/autobroadcast_binop.hpp"
#include "openvino/reference/mod.hpp"
@@ -16,13 +18,28 @@ namespace func {
template <class T, typename std::enable_if<std::is_unsigned<T>::value>::type* = nullptr>
constexpr T floor_mod(const T x, const T y) {
+ OPENVINO_ASSERT(y != T{0});
return mod(x, y);
}
-template <class T, typename std::enable_if<ov::is_floating_point<T>() || std::is_signed<T>::value>::type* = nullptr>
+template <class T, typename std::enable_if<std::is_signed<T>::value && std::is_integral<T>::value>::type* = nullptr>
+constexpr T floor_mod(const T x, const T y) {
+ OPENVINO_ASSERT(y != T{0});
+ // Compute the floored remainder with exact integer arithmetic. Going through double (as the
+ // floating-point overload does) loses precision for large magnitudes, e.g. values near the
+ // int64 limits, so that FloorMod(INT64_MAX, 3) would wrongly yield 0 instead of 1.
+ // C++ division and remainder are undefined when the quotient is not representable.
+ if (x == std::numeric_limits<T>::min() && y == T{-1}) {
+ return T{0};
+ }
+ const T rem = static_cast<T>(x % y);
+ // When signs differ, |rem| < |y|, so rem + y stays between y and zero and cannot overflow.
+ return rem != T{0} && (rem < T{0}) != (y < T{0}) ? static_cast<T>(rem + y) : rem;
+}
+
+template <class T, typename std::enable_if<ov::is_floating_point<T>()>::type* = nullptr>
T floor_mod(const T x, const T y) {
- // Cast to double is needed for integer input (signed),
- // otherwise std::floor will act like std::trunc
+ // Cast to double avoids precision loss for reduced-precision floating-point types.
const double divisor = static_cast<double>(y);
return static_cast<T>(x - y * std::floor(x / divisor));
}
diff --git a/src/frontends/onnx/docs/supported_ops.md b/src/frontends/onnx/docs/supported_ops.md
index 5f701e3381a..7ea834064d9 100644
--- a/src/frontends/onnx/docs/supported_ops.md
+++ b/src/frontends/onnx/docs/supported_ops.md
@@ -199,6 +199,7 @@ OpenVINO provides support for operations of Default Opset (empty in table below)
| |Swish |24 |24 | |
| |Tan |7 |22, 7 | |
| |Tanh |1 |13, 6, 1 | |
+| |TensorScatter |24 |24 | |
| |TfIdfVectorizer | |9 |Supported through openvino_tokenizers|
| |ThresholdedRelu |10 |22, 10 | |
| |Tile |1 |13, 6, 1 | |
diff --git a/src/frontends/onnx/frontend/sources.cmake b/src/frontends/onnx/frontend/sources.cmake
index 08ed4ef0994..788be2a7c56 100644
--- a/src/frontends/onnx/frontend/sources.cmake
+++ b/src/frontends/onnx/frontend/sources.cmake
@@ -217,6 +217,7 @@ set(op_list
${CMAKE_CURRENT_LIST_DIR}/src/op/swish.cpp
${CMAKE_CURRENT_LIST_DIR}/src/op/tan.cpp
${CMAKE_CURRENT_LIST_DIR}/src/op/tanh.cpp
+ ${CMAKE_CURRENT_LIST_DIR}/src/op/tensor_scatter.cpp
${CMAKE_CURRENT_LIST_DIR}/src/op/thresholded_relu.cpp
${CMAKE_CURRENT_LIST_DIR}/src/op/tile.cpp
${CMAKE_CURRENT_LIST_DIR}/src/op/topk.cpp
diff --git a/src/frontends/onnx/frontend/src/op/tensor_scatter.cpp b/src/frontends/onnx/frontend/src/op/tensor_scatter.cpp
new file mode 100644
index 00000000000..d9136dd1c75
--- /dev/null
+++ b/src/frontends/onnx/frontend/src/op/tensor_scatter.cpp
@@ -0,0 +1,216 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <algorithm>
+#include <numeric>
+#include <string>
+#include <vector>
+
+#include "core/operator_set.hpp"
+#include "exceptions.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/floor_mod.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/greater_eq.hpp"
+#include "openvino/op/maximum.hpp"
+#include "openvino/op/range.hpp"
+#include "openvino/op/scatter_nd_update.hpp"
+#include "openvino/op/scatter_update.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 "utils/common.hpp"
+
+using namespace ov::op;
+
+namespace ov::frontend::onnx::ai_onnx::opset_24 {
+
+// TensorScatter (ONNX opset 24): a functional KV-cache update.
+// Spec: https://onnx.ai/onnx/operators/onnx__TensorScatter.html
+//
+// present_cache is a copy of past_cache in which, for every batch sample `b` and every
+// sequence position `s` of `update`, the slice at the sequence (`axis`) dimension is
+// overwritten:
+// present[*, write_indices[b] + s, *] = update[*, s, *]
+// For `circular` mode the write position wraps around: (write_indices[b] + s) % max_sequence_length.
+//
+// The decomposition moves the `axis` dimension next to the batch dimension (position 1) via an
+// optional Transpose, then performs the update with a v3::ScatterNDUpdate whose index tensor
+// carries (batch, cache_sequence) coordinates for each (b, s) position. All remaining dimensions
+// are copied wholesale by ScatterNDUpdate, so their relative order (and the transpose) is irrelevant.
+ov::OutputVector tensor_scatter(const ov::frontend::onnx::Node& node) {
+ const auto inputs = node.get_ov_inputs();
+ CHECK_VALID_NODE(node,
+ inputs.size() == 2 || inputs.size() == 3,
+ "TensorScatter expects 2 or 3 inputs (past_cache, update, [write_indices]). Got: ",
+ inputs.size());
+
+ ov::Output<ov::Node> past_cache = inputs[0];
+ ov::Output<ov::Node> update = inputs[1];
+
+ const auto past_rank = past_cache.get_partial_shape().rank();
+ const auto update_rank = update.get_partial_shape().rank();
+
+ const int64_t axis_attr = node.get_attribute_value<int64_t>("axis", -2);
+
+ const std::string mode = node.get_attribute_value<std::string>("mode", "linear");
+ CHECK_VALID_NODE(node,
+ mode == "linear" || mode == "circular",
+ "TensorScatter supports only `linear` and `circular` modes. Got: ",
+ mode);
+ const bool circular = (mode == "circular");
+
+ const auto i64 = ov::element::i64;
+ const auto zero = v0::Constant::create(i64, ov::Shape{}, {0});
+ const auto one = v0::Constant::create(i64, ov::Shape{}, {1});
+ const auto idx_batch_dim = v0::Constant::create(i64, ov::Shape{}, {0});
+ const auto idx_seq_dim = v0::Constant::create(i64, ov::Shape{}, {1});
+ const auto ax0 = v0::Constant::create(i64, ov::Shape{1}, {0});
+ const auto ax1 = v0::Constant::create(i64, ov::Shape{1}, {1});
+ const auto ax_last = v0::Constant::create(i64, ov::Shape{1}, {-1});
+
+ // Move the sequence (`axis`) dimension to position 1 so the scatter index needs only
+ // (batch, sequence) coordinates. The swap permutation is its own inverse, reused to
+ // transpose back. The ONNX loader may omit shape metadata, so the rank is not always known
+ // at conversion time: derive it from `past_cache`, fall back to `update`, and otherwise
+ // build the permutation at runtime.
+ ov::Output<ov::Node> transpose_perm;
+ bool do_transpose = false;
+ if (past_rank.is_static() || update_rank.is_static()) {
+ const int64_t r = past_rank.is_static() ? past_rank.get_length() : update_rank.get_length();
+ int64_t axis = axis_attr;
+ if (axis < 0) {
+ axis += r;
+ }
+ CHECK_VALID_NODE(node,
+ axis > 0 && axis < r,
+ "TensorScatter `axis` must select a non-batch dimension within rank bounds. "
+ "Got normalized axis ",
+ axis,
+ " for rank ",
+ r);
+
+ // The ONNX contract requires sequence_length <= max_sequence_length; the reference
+ // implementation rejects violations. When the cache rank is known and both the cache
+ // (`axis`) and update (`axis`) dimensions are static, enforce this at conversion time.
+ // Otherwise the dynamic runtime path keeps the behavior defined for valid models; in
+ // `circular` mode in particular the modulo must not be relied upon to silently legalize
+ // an oversized update.
+ if (past_rank.is_static()) {
+ const auto past_dim = past_cache.get_partial_shape()[axis];
+ const auto update_ps = update.get_partial_shape();
+ if (update_ps.rank().is_static() && update_ps.rank().get_length() == r) {
+ const auto update_dim = update_ps[axis];
+ if (past_dim.is_static() && update_dim.is_static()) {
+ CHECK_VALID_NODE(node,
+ update_dim.get_length() <= past_dim.get_length(),
+ "TensorScatter update sequence length (",
+ update_dim.get_length(),
+ ") must not exceed the cache sequence length (",
+ past_dim.get_length(),
+ ").");
+ }
+ }
+ }
+
+ if (axis != 1) {
+ std::vector<int64_t> perm(static_cast<size_t>(r));
+ std::iota(perm.begin(), perm.end(), 0);
+ std::swap(perm[1], perm[static_cast<size_t>(axis)]);
+ transpose_perm = v0::Constant::create(i64, ov::Shape{perm.size()}, perm);
+ do_transpose = true;
+ }
+ } else {
+ // Neither rank is known: build the swap permutation at runtime. The permutation is applied
+ // unconditionally; when `axis` resolves to 1 the swap is the identity, so this stays correct.
+ const auto past_data_shape = std::make_shared<v3::ShapeOf>(past_cache, i64); // (r,)
+ const auto rank_vec = std::make_shared<v3::ShapeOf>(past_data_shape, i64); // (1,) = [r]
+ const auto rank_scalar = std::make_shared<v8::Gather>(rank_vec, zero, zero); // scalar r
+ ov::Output<ov::Node> axis_scalar;
+ if (axis_attr >= 0) {
+ axis_scalar = v0::Constant::create(i64, ov::Shape{}, {axis_attr});
+ } else {
+ axis_scalar = std::make_shared<v1::Add>(rank_scalar, v0::Constant::create(i64, ov::Shape{}, {axis_attr}));
+ }
+ const auto base_perm = std::make_shared<v4::Range>(zero, rank_scalar, one, i64); // [0, 1, ..., r-1]
+ const auto axis_1d = std::make_shared<v0::Unsqueeze>(axis_scalar, ax0); // (1,)
+ const auto swap_idx = std::make_shared<v0::Concat>(ov::OutputVector{ax1, axis_1d}, 0); // [1, axis]
+ const auto swap_upd = std::make_shared<v0::Concat>(ov::OutputVector{axis_1d, ax1}, 0); // [axis, 1]
+ transpose_perm = std::make_shared<v3::ScatterUpdate>(base_perm, swap_idx, swap_upd, zero);
+ do_transpose = true;
+ }
+
+ if (do_transpose) {
+ past_cache = std::make_shared<v1::Transpose>(past_cache, transpose_perm);
+ update = std::make_shared<v1::Transpose>(update, transpose_perm);
+ }
+
+ // Shapes of the (possibly transposed) tensors: (batch, max_sequence_length, ...rest).
+ const auto past_shape = std::make_shared<v3::ShapeOf>(past_cache, i64);
+ const auto update_shape = std::make_shared<v3::ShapeOf>(update, i64);
+ const auto batch_size = std::make_shared<v8::Gather>(past_shape, idx_batch_dim, zero); // scalar
+ const auto max_seq_len = std::make_shared<v8::Gather>(past_shape, idx_seq_dim, zero); // scalar
+ const auto update_seq_len = std::make_shared<v8::Gather>(update_shape, idx_seq_dim, zero); // scalar
+
+ // seq_range = [0, 1, ..., sequence_length-1] -> (1, L)
+ const auto seq_range = std::make_shared<v4::Range>(zero, update_seq_len, one, i64);
+ const auto seq_row = std::make_shared<v0::Unsqueeze>(seq_range, ax0);
+
+ // write offsets per batch sample -> (batch, 1); defaults to zeros when write_indices is absent.
+ ov::Output<ov::Node> write_indices;
+ if (common::is_input_valid(inputs, 2)) {
+ write_indices = std::make_shared<v0::Convert>(inputs[2], i64);
+ } else {
+ const auto batch_shape = std::make_shared<v0::Unsqueeze>(batch_size, ax0); // (1,) = [batch]
+ write_indices = std::make_shared<v3::Broadcast>(zero, batch_shape);
+ }
+ // cache_seq[b, s] = write_indices[b] + s, optionally modulo max_sequence_length.
+ ov::Output<ov::Node> cache_seq;
+ if (circular) {
+ // Reduce the offset first, then conditionally wrap before adding. This avoids
+ // overflowing int64 when write_indices is large or when the cache length is large.
+ // A zero-length cache (max_sequence_length == 0) only occurs for an empty update, which
+ // is a no-op; clamp the divisor to 1 so FloorMod does not divide by zero and produce NaN.
+ const auto safe_seq_len = std::make_shared<v1::Maximum>(max_seq_len, one);
+ const auto normalized_write_indices = std::make_shared<v1::FloorMod>(write_indices, safe_seq_len);
+ const auto write_col = std::make_shared<v0::Unsqueeze>(normalized_write_indices, ax1);
+ const auto distance_to_wrap = std::make_shared<v1::Subtract>(max_seq_len, seq_row);
+ const auto wraps = std::make_shared<v1::GreaterEqual>(write_col, distance_to_wrap);
+ const auto wrapped_seq = std::make_shared<v1::Subtract>(write_col, distance_to_wrap);
+ const auto safe_write_col = std::make_shared<v1::Select>(wraps, zero, write_col);
+ const auto unwrapped_seq = std::make_shared<v1::Add>(safe_write_col, seq_row);
+ cache_seq = std::make_shared<v1::Select>(wraps, wrapped_seq, unwrapped_seq);
+ } else {
+ const auto write_col = std::make_shared<v0::Unsqueeze>(write_indices, ax1);
+ cache_seq = std::make_shared<v1::Add>(write_col, seq_row);
+ }
+
+ // batch coordinate grid -> (batch, L)
+ const auto batch_range = std::make_shared<v4::Range>(zero, batch_size, one, i64);
+ const auto batch_col = std::make_shared<v0::Unsqueeze>(batch_range, ax1);
+ const auto grid_shape = std::make_shared<v3::ShapeOf>(cache_seq, i64);
+ const auto batch_grid = std::make_shared<v3::Broadcast>(batch_col, grid_shape);
+
+ // indices[b, s] = [b, cache_seq[b, s]] -> (batch, L, 2)
+ const auto batch_idx = std::make_shared<v0::Unsqueeze>(batch_grid, ax_last);
+ const auto seq_idx = std::make_shared<v0::Unsqueeze>(cache_seq, ax_last);
+ const auto indices = std::make_shared<v0::Concat>(ov::OutputVector{batch_idx, seq_idx}, -1);
+
+ ov::Output<ov::Node> result = std::make_shared<v3::ScatterNDUpdate>(past_cache, indices, update);
+ if (do_transpose) {
+ result = std::make_shared<v1::Transpose>(result, transpose_perm);
+ }
+
+ return {result};
+}
+
+ONNX_OP("TensorScatter", OPSET_SINCE(1), ai_onnx::opset_24::tensor_scatter);
+
+} // namespace ov::frontend::onnx::ai_onnx::opset_24
diff --git a/src/frontends/onnx/tests/__init__.py b/src/frontends/onnx/tests/__init__.py
index 5ce757e6872..a6bf48d53b6 100644
--- a/src/frontends/onnx/tests/__init__.py
+++ b/src/frontends/onnx/tests/__init__.py
@@ -189,8 +189,6 @@ xfail_issue_onnx122_bitcast = xfail_test(
reason="OV ONNX FE does not support BitCast-26 (new in ONNX opset 26)")
xfail_issue_onnx122_cumprod = xfail_test(
reason="OV ONNX FE does not support CumProd-26 (new in ONNX opset 26)")
-xfail_issue_onnx122_tensorscatter = xfail_test(
- reason="OV ONNX FE does not support TensorScatter-24 (new in ONNX opset 24)")
xfail_issue_onnx122_attention = xfail_test(
reason="OV ONNX FE does not fully support Attention-24 for these cases (ONNX opset 24)")
xfail_issue_onnx122_newdtype = xfail_test(
diff --git a/src/frontends/onnx/tests/models/tensor_scatter_axis_transpose.prototxt b/src/frontends/onnx/tests/models/tensor_scatter_axis_transpose.prototxt
new file mode 100644
index 00000000000..5ca12f14580
--- /dev/null
+++ b/src/frontends/onnx/tests/models/tensor_scatter_axis_transpose.prototxt
@@ -0,0 +1,74 @@
+ir_version: 11
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "past_cache"
+ input: "update"
+ input: "write_indices"
+ output: "present_cache"
+ op_type: "TensorScatter"
+ attribute {
+ name: "axis"
+ i: -2
+ type: INT
+ }
+ }
+ name: "tensor_scatter_axis_transpose_graph"
+ input {
+ name: "past_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 2 }
+ dim { dim_value: 3 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "update"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 2 }
+ dim { dim_value: 1 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "write_indices"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ output {
+ name: "present_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 2 }
+ dim { dim_value: 3 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 24
+}
diff --git a/src/frontends/onnx/tests/models/tensor_scatter_circular.prototxt b/src/frontends/onnx/tests/models/tensor_scatter_circular.prototxt
new file mode 100644
index 00000000000..332f63d4177
--- /dev/null
+++ b/src/frontends/onnx/tests/models/tensor_scatter_circular.prototxt
@@ -0,0 +1,76 @@
+ir_version: 11
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "past_cache"
+ input: "update"
+ input: "write_indices"
+ output: "present_cache"
+ op_type: "TensorScatter"
+ attribute {
+ name: "axis"
+ i: -2
+ type: INT
+ }
+ attribute {
+ name: "mode"
+ s: "circular"
+ type: STRING
+ }
+ }
+ name: "tensor_scatter_circular_graph"
+ input {
+ name: "past_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 4 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "update"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 3 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "write_indices"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ output {
+ name: "present_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 4 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 24
+}
diff --git a/src/frontends/onnx/tests/models/tensor_scatter_circular_empty.prototxt b/src/frontends/onnx/tests/models/tensor_scatter_circular_empty.prototxt
new file mode 100644
index 00000000000..27a77e23ee0
--- /dev/null
+++ b/src/frontends/onnx/tests/models/tensor_scatter_circular_empty.prototxt
@@ -0,0 +1,76 @@
+ir_version: 11
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "past_cache"
+ input: "update"
+ input: "write_indices"
+ output: "present_cache"
+ op_type: "TensorScatter"
+ attribute {
+ name: "axis"
+ i: -2
+ type: INT
+ }
+ attribute {
+ name: "mode"
+ s: "circular"
+ type: STRING
+ }
+ }
+ name: "tensor_scatter_circular_empty_graph"
+ input {
+ name: "past_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 0 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "update"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 0 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "write_indices"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ output {
+ name: "present_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 0 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 24
+}
diff --git a/src/frontends/onnx/tests/models/tensor_scatter_circular_large_write_index.prototxt b/src/frontends/onnx/tests/models/tensor_scatter_circular_large_write_index.prototxt
new file mode 100644
index 00000000000..985d4746ff4
--- /dev/null
+++ b/src/frontends/onnx/tests/models/tensor_scatter_circular_large_write_index.prototxt
@@ -0,0 +1,76 @@
+ir_version: 11
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "past_cache"
+ input: "update"
+ input: "write_indices"
+ output: "present_cache"
+ op_type: "TensorScatter"
+ attribute {
+ name: "axis"
+ i: -2
+ type: INT
+ }
+ attribute {
+ name: "mode"
+ s: "circular"
+ type: STRING
+ }
+ }
+ name: "tensor_scatter_circular_large_write_index_graph"
+ input {
+ name: "past_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 3 }
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ input {
+ name: "update"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 3 }
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ input {
+ name: "write_indices"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ output {
+ name: "present_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 3 }
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 24
+}
diff --git a/src/frontends/onnx/tests/models/tensor_scatter_dynamic_rank.prototxt b/src/frontends/onnx/tests/models/tensor_scatter_dynamic_rank.prototxt
new file mode 100644
index 00000000000..d2d713c48e0
--- /dev/null
+++ b/src/frontends/onnx/tests/models/tensor_scatter_dynamic_rank.prototxt
@@ -0,0 +1,61 @@
+ir_version: 11
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "past_cache"
+ input: "update"
+ input: "write_indices"
+ output: "present_cache"
+ op_type: "TensorScatter"
+ attribute {
+ name: "axis"
+ i: 2
+ type: INT
+ }
+ attribute {
+ name: "mode"
+ s: "linear"
+ type: STRING
+ }
+ }
+ name: "tensor_scatter_dynamic_rank_graph"
+ input {
+ name: "past_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ }
+ }
+ }
+ input {
+ name: "update"
+ type {
+ tensor_type {
+ elem_type: 1
+ }
+ }
+ }
+ input {
+ name: "write_indices"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim { dim_value: 1 }
+ }
+ }
+ }
+ }
+ output {
+ name: "present_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 24
+}
diff --git a/src/frontends/onnx/tests/models/tensor_scatter_linear.prototxt b/src/frontends/onnx/tests/models/tensor_scatter_linear.prototxt
new file mode 100644
index 00000000000..1a1416fa503
--- /dev/null
+++ b/src/frontends/onnx/tests/models/tensor_scatter_linear.prototxt
@@ -0,0 +1,64 @@
+ir_version: 11
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "past_cache"
+ input: "update"
+ output: "present_cache"
+ op_type: "TensorScatter"
+ attribute {
+ name: "axis"
+ i: -2
+ type: INT
+ }
+ attribute {
+ name: "mode"
+ s: "linear"
+ type: STRING
+ }
+ }
+ name: "tensor_scatter_linear_graph"
+ input {
+ name: "past_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 4 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "update"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 2 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ output {
+ name: "present_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 1 }
+ dim { dim_value: 4 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 24
+}
diff --git a/src/frontends/onnx/tests/models/tensor_scatter_write_indices.prototxt b/src/frontends/onnx/tests/models/tensor_scatter_write_indices.prototxt
new file mode 100644
index 00000000000..08c6432bc9d
--- /dev/null
+++ b/src/frontends/onnx/tests/models/tensor_scatter_write_indices.prototxt
@@ -0,0 +1,71 @@
+ir_version: 11
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "past_cache"
+ input: "update"
+ input: "write_indices"
+ output: "present_cache"
+ op_type: "TensorScatter"
+ attribute {
+ name: "axis"
+ i: -2
+ type: INT
+ }
+ }
+ name: "tensor_scatter_write_indices_graph"
+ input {
+ name: "past_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 4 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "update"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 2 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ input {
+ name: "write_indices"
+ type {
+ tensor_type {
+ elem_type: 7
+ shape {
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+ output {
+ name: "present_cache"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim { dim_value: 2 }
+ dim { dim_value: 4 }
+ dim { dim_value: 2 }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ domain: ""
+ version: 24
+}
diff --git a/src/frontends/onnx/tests/onnx_import.in.cpp b/src/frontends/onnx/tests/onnx_import.in.cpp
index 7112010efff..5ff8248bd4b 100644
--- a/src/frontends/onnx/tests/onnx_import.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import.in.cpp
@@ -8378,6 +8378,132 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_rotary_embedding_identity) {
test_case.run_with_tolerance_as_fp(1e-5f);
}
+// TensorScatter (ONNX opset-24) functional KV-cache update, `linear` mode without
+// `write_indices`. The update is appended starting at position 0, so the first
+// `sequence_length` rows of the cache are overwritten and the rest kept.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_tensor_scatter_linear) {
+ const auto model = convert_model("tensor_scatter_linear.onnx");
+ auto test_case = test::TestCase(model, s_device);
+
+ // past_cache (1, 4, 2)
+ test_case.add_input<float>(Shape{1, 4, 2}, {10.f, 11.f, 12.f, 13.f, 14.f, 15.f, 16.f, 17.f});
+ // update (1, 2, 2)
+ test_case.add_input<float>(Shape{1, 2, 2}, {100.f, 101.f, 200.f, 201.f});
+ // write_indices default to 0 -> positions 0 and 1 are overwritten.
+ test_case.add_expected_output<float>(Shape{1, 4, 2}, {100.f, 101.f, 200.f, 201.f, 14.f, 15.f, 16.f, 17.f});
+ test_case.run_with_tolerance_as_fp(1e-5f);
+}
+
+// TensorScatter (ONNX opset-24) `linear` mode with per-batch `write_indices`.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_tensor_scatter_write_indices) {
+ const auto model = convert_model("tensor_scatter_write_indices.onnx");
+ auto test_case = test::TestCase(model, s_device);
+
+ // past_cache (2, 4, 2)
+ test_case.add_input<float>(Shape{2, 4, 2},
+ {0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 12.f, 13.f, 14.f, 15.f});
+ // update (2, 2, 2)
+ test_case.add_input<float>(Shape{2, 2, 2}, {100.f, 101.f, 110.f, 111.f, 200.f, 201.f, 210.f, 211.f});
+ // write_indices (2,) -> batch 0 writes at [1,2], batch 1 writes at [2,3].
+ test_case.add_input<int64_t>(Shape{2}, {1, 2});
+ test_case.add_expected_output<float>(
+ Shape{2, 4, 2},
+ {0.f, 1.f, 100.f, 101.f, 110.f, 111.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f, 200.f, 201.f, 210.f, 211.f});
+ test_case.run_with_tolerance_as_fp(1e-5f);
+}
+
+// TensorScatter (ONNX opset-24) `circular` mode: write positions wrap around
+// max_sequence_length. write_indices=[3], max_sequence_length=4, sequence_length=3
+// -> positions 3, 0, 1.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_tensor_scatter_circular) {
+ const auto model = convert_model("tensor_scatter_circular.onnx");
+ auto test_case = test::TestCase(model, s_device);
+
+ // past_cache (1, 4, 2)
+ test_case.add_input<float>(Shape{1, 4, 2}, {0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f});
+ // update (1, 3, 2)
+ test_case.add_input<float>(Shape{1, 3, 2}, {100.f, 101.f, 110.f, 111.f, 120.f, 121.f});
+ // write_indices (1,)
+ test_case.add_input<int64_t>(Shape{1}, {3});
+ // pos3 = update0, pos0 = update1, pos1 = update2, pos2 unchanged.
+ test_case.add_expected_output<float>(Shape{1, 4, 2}, {110.f, 111.f, 120.f, 121.f, 4.f, 5.f, 100.f, 101.f});
+ test_case.run_with_tolerance_as_fp(1e-5f);
+}
+
+// A large circular write index must be reduced before adding sequence offsets,
+// otherwise the int64 addition can overflow and wrap to the wrong cache position.
+// INT64_MAX % 3 == 1 (a non-zero remainder), so the reduced start is position 1.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_tensor_scatter_circular_large_write_index) {
+ if (s_device == "CPU") {
+ GTEST_SKIP() << "CPU FloorMod currently loses precision for large int64 operands";
+ }
+
+ const auto model = convert_model("tensor_scatter_circular_large_write_index.onnx");
+ auto test_case = test::TestCase(model, s_device);
+
+ // Cache length 3; INT64_MAX % 3 == 1, so updates land at positions 1, 2, 0.
+ test_case.add_input<float>(Shape{1, 3, 1}, {10.f, 20.f, 30.f});
+ test_case.add_input<float>(Shape{1, 3, 1}, {100.f, 200.f, 300.f});
+ test_case.add_input<int64_t>(Shape{1}, {9223372036854775807LL});
+ test_case.add_expected_output<float>(Shape{1, 3, 1}, {300.f, 100.f, 200.f});
+ test_case.run_with_tolerance_as_fp(1e-5f);
+}
+
+// TensorScatter (ONNX opset-24) with axis=-2 on a rank-4 KV-cache
+// (batch, num_heads, max_sequence_length, head_size). The sequence dimension is not
+// adjacent to batch, exercising the internal Transpose path. The write index depends
+// only on the batch sample, so all heads are updated at the same sequence position.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_tensor_scatter_axis_transpose) {
+ const auto model = convert_model("tensor_scatter_axis_transpose.onnx");
+ auto test_case = test::TestCase(model, s_device);
+
+ // past_cache (1, 2, 3, 2)
+ test_case.add_input<float>(Shape{1, 2, 3, 2}, {0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f});
+ // update (1, 2, 1, 2)
+ test_case.add_input<float>(Shape{1, 2, 1, 2}, {100.f, 101.f, 200.f, 201.f});
+ // write_indices (1,) -> sequence position 1 for every head.
+ test_case.add_input<int64_t>(Shape{1}, {1});
+ test_case.add_expected_output<float>(Shape{1, 2, 3, 2},
+ {0.f, 1.f, 100.f, 101.f, 4.f, 5.f, 6.f, 7.f, 200.f, 201.f, 10.f, 11.f});
+ test_case.run_with_tolerance_as_fp(1e-5f);
+}
+
+// TensorScatter (ONNX opset-24) `circular` mode with an empty update and a zero-length cache
+// sequence dimension. The modulo normalization must not divide by zero; the scatter is a no-op
+// that returns the cache unchanged.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_tensor_scatter_circular_empty) {
+ const auto model = convert_model("tensor_scatter_circular_empty.onnx");
+ auto test_case = test::TestCase(model, s_device);
+
+ test_case.add_input<float>(Shape{1, 0, 2}, {});
+ test_case.add_input<float>(Shape{1, 0, 2}, {});
+ test_case.add_input<int64_t>(Shape{1}, {0});
+ test_case.add_expected_output<float>(Shape{1, 0, 2}, {});
+ test_case.run_with_tolerance_as_fp(1e-5f);
+}
+
+// TensorScatter (ONNX opset-24) with inputs that carry no shape metadata, so their rank is
+// unknown at conversion time. The axis normalization and the transpose permutation are then
+// constructed at runtime. axis=2 on the rank-4 input requires an actual transpose.
+OPENVINO_TEST(${BACKEND_NAME}, onnx_tensor_scatter_dynamic_rank) {
+ if (s_device == "CPU" || s_device == "GPU") {
+ GTEST_SKIP() << "CPU and GPU plug-ins do not support Parameter operations with dynamic rank";
+ }
+
+ const auto model = convert_model("tensor_scatter_dynamic_rank.onnx");
+ auto test_case = test::TestCase(model, s_device);
+
+ // past_cache (1, 2, 3, 2)
+ test_case.add_input<float>(Shape{1, 2, 3, 2}, {0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f, 10.f, 11.f});
+ // update (1, 2, 1, 2)
+ test_case.add_input<float>(Shape{1, 2, 1, 2}, {100.f, 101.f, 200.f, 201.f});
+ // write_indices (1,) -> sequence position 1 for every head.
+ test_case.add_input<int64_t>(Shape{1}, {1});
+ test_case.add_expected_output<float>(Shape{1, 2, 3, 2},
+ {0.f, 1.f, 100.f, 101.f, 4.f, 5.f, 6.f, 7.f, 200.f, 201.f, 10.f, 11.f});
+ test_case.run_with_tolerance_as_fp(1e-5f);
+}
+
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_attention_mha_4d) {
auto model = convert_model("attention_mha_4d.onnx");
auto test_case = test::TestCase(model, s_device);
diff --git a/src/frontends/onnx/tests/tests_python/test_backend.py b/src/frontends/onnx/tests/tests_python/test_backend.py
index 58b95863ed4..e1a42957f9e 100644
--- a/src/frontends/onnx/tests/tests_python/test_backend.py
+++ b/src/frontends/onnx/tests/tests_python/test_backend.py
@@ -67,7 +67,6 @@ from tests import (
xfail_issue_onnx122_flexattention,
xfail_issue_onnx122_bitcast,
xfail_issue_onnx122_cumprod,
- xfail_issue_onnx122_tensorscatter,
xfail_issue_onnx122_attention,
xfail_issue_onnx122_newdtype,
xfail_issue_onnx122_accuracy,
@@ -646,12 +645,6 @@ tests_expected_to_fail = [
"OnnxBackendNodeModelTest.test_cumprod_2d_int32_cpu",
"OnnxBackendNodeModelTest.test_cumprod_2d_negative_axis_cpu",
),
- (
- xfail_issue_onnx122_tensorscatter,
- "OnnxBackendNodeModelTest.test_tensorscatter_3d_cpu",
- "OnnxBackendNodeModelTest.test_tensorscatter_circular_cpu",
- "OnnxBackendNodeModelTest.test_tensorscatter_cpu",
- ),
(
xfail_issue_onnx122_attention,
"OnnxBackendNodeModelTest.test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal_cpu",
diff --git a/src/plugins/template/tests/functional/op_reference/floor_mod.cpp b/src/plugins/template/tests/functional/op_reference/floor_mod.cpp
index 78cf8ea6081..af0682ce98d 100644
--- a/src/plugins/template/tests/functional/op_reference/floor_mod.cpp
+++ b/src/plugins/template/tests/functional/op_reference/floor_mod.cpp
@@ -6,7 +6,11 @@
#include <gtest/gtest.h>
+#include <cstdint>
+#include <limits>
+
#include "base_reference_test.hpp"
+#include "openvino/core/except.hpp"
using namespace ov;
using namespace reference_tests;
@@ -71,6 +75,25 @@ private:
}
};
+template <typename T>
+void expectIntegerFloorModByZeroToThrow(const element::Type& type) {
+ const auto lhs = std::make_shared<op::v0::Parameter>(type, Shape{1});
+ const auto rhs = std::make_shared<op::v0::Parameter>(type, Shape{1});
+ const auto floor_mod = std::make_shared<op::v1::FloorMod>(lhs, rhs);
+
+ TensorVector inputs{ov::Tensor(type, Shape{1}), ov::Tensor(type, Shape{1})};
+ inputs[0].data<T>()[0] = T{7};
+ inputs[1].data<T>()[0] = T{0};
+ TensorVector outputs{ov::Tensor(type, Shape{1})};
+
+ EXPECT_THROW(floor_mod->evaluate(outputs, inputs), ov::AssertFailure);
+}
+
+TEST(ReferenceFloorModLayerTest, IntegerZeroDivisorThrows) {
+ expectIntegerFloorModByZeroToThrow<int64_t>(element::i64);
+ expectIntegerFloorModByZeroToThrow<uint64_t>(element::u64);
+}
+
TEST_P(ReferenceFloorModLayerTest, DivideWithHardcodedRefs) {
Exec();
}
@@ -121,6 +144,25 @@ std::vector<FloorModParams> generateParamsForFloorModScalar() {
return params;
}
+template <element::Type_t IN_ET>
+std::vector<FloorModParams> generateParamsForFloorModLargeInteger() {
+ using T = typename element_type_traits<IN_ET>::value_type;
+ // Large operands whose remainder is non-zero. Evaluating the floored modulo via double would
+ // lose precision here (e.g. INT64_MAX / 3 rounds up), so only exact integer arithmetic keeps
+ // the result accurate.
+ return {FloorModParams(ov::PartialShape{5},
+ ov::PartialShape{5},
+ ov::Shape{5},
+ IN_ET,
+ std::vector<T>{9223372036854775807LL,
+ -9223372036854775807LL,
+ 9223372036854775806LL,
+ 100,
+ std::numeric_limits<T>::min()},
+ std::vector<T>{3, 3, 7, -7, -1},
+ std::vector<T>{1, 2, 6, -5, 0})};
+}
+
template <element::Type_t IN_ET>
std::vector<FloorModParams> generateParamsForFloorModNonIntegerDivisor() {
using T = typename element_type_traits<IN_ET>::value_type;
@@ -188,6 +230,10 @@ std::vector<FloorModParams> generateCombinedParamsForFloorModNonIntegerDivisor()
return generateParamsForFloorModNonIntegerDivisor<element::Type_t::f32>();
}
+std::vector<FloorModParams> generateCombinedParamsForFloorModLargeInteger() {
+ return generateParamsForFloorModLargeInteger<element::Type_t::i64>();
+}
+
INSTANTIATE_TEST_SUITE_P(smoke_FloorMod_With_Hardcoded_Refs,
ReferenceFloorModLayerTest,
::testing::ValuesIn(generateCombinedParamsForFloorMod()),
@@ -208,4 +254,9 @@ INSTANTIATE_TEST_SUITE_P(smoke_FloorMod_NonInteger_Divisor,
::testing::ValuesIn(generateCombinedParamsForFloorModNonIntegerDivisor()),
ReferenceFloorModLayerTest::getTestCaseName);
+INSTANTIATE_TEST_SUITE_P(smoke_FloorMod_Large_Integer,
+ ReferenceFloorModLayerTest,
+ ::testing::ValuesIn(generateCombinedParamsForFloorModLargeInteger()),
+ ReferenceFloorModLayerTest::getTestCaseName);
+
} // namespace