Skip to content

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

[TEMPLATE] Fix Gather f8e4m3/exotic-dtype shape-infer bug surfaced by GQA sliding-window cache (#37734)

Ankit Maheshkar57b3b5a379bf
diff --git a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
index 00c44482e43..a31ba77e7b1 100644
--- a/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
+++ b/src/frontends/onnx/tests/onnx_import_com_microsoft.in.cpp
@@ -4720,13 +4720,6 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_i4kv_sliding_window_cache) {
// last 2 f8 rows + the new token, tail zeroed. Runs on CPU and GPU (both now have an f8e4m3 Gather
// kernel for the windowed present assembly).
OPENVINO_TEST(${BACKEND_NAME}, onnx_model_gqa_f8e4m3fnkv_sliding_window_cache) {
- // TEMPLATE hits a pre-existing shape-inference quirk on this fully-static graph ("to_shape was
- // called on a dynamic shape"), the same class of INTERPRETER-only issue documented on
- // onnx_model_gqa_sliding_window_cache_static_staging below; verified correct on CPU/GPU.
- if (s_device == ov::test::utils::DEVICE_TEMPLATE) {
- GTEST_SKIP() << "TEMPLATE hits a pre-existing dynamic-shape quirk on this fully-static "
- "f8e4m3 windowed-cache graph; verified correct on CPU/GPU. Needs follow-up.";
- }
auto model = convert_model("com.microsoft/gqa_f8e4m3fnkv_swc.onnx");
model->reshape({{"query", ov::PartialShape{1, 1, 64}},
{"past_key", ov::PartialShape{1, 1, 4, 16}},
diff --git a/src/plugins/template/backend/ops/gather.cpp b/src/plugins/template/backend/ops/gather.cpp
index c6a4213ac29..29aeb5647ba 100644
--- a/src/plugins/template/backend/ops/gather.cpp
+++ b/src/plugins/template/backend/ops/gather.cpp
@@ -5,6 +5,7 @@
#include "openvino/reference/gather.hpp"
#include "evaluate_node.hpp"
+#include "gather_shape_inference.hpp"
#include "openvino/core/type/element_type_traits.hpp"
#include "openvino/op/gather.hpp"
@@ -13,33 +14,47 @@ bool evaluate(const std::shared_ptr<ov::op::v8::Gather>& op,
ov::TensorVector& outputs,
const ov::TensorVector& inputs) {
using T = typename ov::element_type_traits<ET>::value_type;
+ const auto& data_shape = inputs[0].get_shape();
+ const auto& indices_shape = inputs[1].get_shape();
+ // Shape-infer from actual tensor shapes (as GatherND's evaluate does), not op->get_output_shape()
+ // (throws if the node's static PartialShape is dynamic, e.g. indices from a data-dependent subgraph).
+ const auto output_shapes =
+ ov::op::shape_infer(op.get(), ov::util::get_tensors_partial_shapes(inputs), ov::make_tensor_accessor(inputs));
+ const auto output_shape = output_shapes[0].get_shape();
+ outputs[0].set_shape(output_shape);
+ // Normalize against runtime ranks, like GatherBase::evaluate - op->get_axis()/get_batch_dims() only
+ // normalize against the node's static IR shape, underflowing to size_t when that shape is dynamic.
+ const auto axis = static_cast<size_t>(
+ ov::util::normalize(ov::get_tensor_data_as<int64_t>(inputs[2])[0], static_cast<int64_t>(data_shape.size())));
+ const auto batch_dims =
+ static_cast<size_t>(ov::util::normalize(op->get_batch_dims(), static_cast<int64_t>(indices_shape.size())));
if (op->get_input_element_type(1) == ov::element::u64) {
ov::reference::gather<T, uint64_t>(inputs[0].data<T>(),
inputs[1].data<uint64_t>(),
outputs[0].data<T>(),
- op->get_input_shape(0),
- op->get_input_shape(1),
- op->get_output_shape(0),
- static_cast<size_t>(op->get_axis()),
- op->get_batch_dims());
+ data_shape,
+ indices_shape,
+ output_shape,
+ axis,
+ batch_dims);
} else if (op->get_input_element_type(1) == ov::element::i64) {
ov::reference::gather<T, int64_t>(inputs[0].data<T>(),
inputs[1].data<int64_t>(),
outputs[0].data<T>(),
- op->get_input_shape(0),
- op->get_input_shape(1),
- op->get_output_shape(0),
- static_cast<size_t>(op->get_axis()),
- op->get_batch_dims());
+ data_shape,
+ indices_shape,
+ output_shape,
+ axis,
+ batch_dims);
} else if (op->get_input_element_type(1) == ov::element::i32) {
ov::reference::gather<T, int32_t>(inputs[0].data<T>(),
inputs[1].data<int32_t>(),
outputs[0].data<T>(),
- op->get_input_shape(0),
- op->get_input_shape(1),
- op->get_output_shape(0),
- static_cast<size_t>(op->get_axis()),
- op->get_batch_dims());
+ data_shape,
+ indices_shape,
+ output_shape,
+ axis,
+ batch_dims);
} else {
OPENVINO_THROW("Unexpected indices type for Gather operation");
}
diff --git a/src/plugins/template/tests/functional/op_reference/gather.cpp b/src/plugins/template/tests/functional/op_reference/gather.cpp
index 69473f2c354..e6dbfc1bb1d 100644
--- a/src/plugins/template/tests/functional/op_reference/gather.cpp
+++ b/src/plugins/template/tests/functional/op_reference/gather.cpp
@@ -788,4 +788,21 @@ INSTANTIATE_TEST_SUITE_P(smoke_Gather_With_Hardcoded_Refs,
ReferenceGatherTestV8,
testing::ValuesIn(generateCombinedParamsV8()),
ReferenceGatherTestV8::getTestCaseName);
+
+class ReferenceGatherRegressionTest : public CommonReferenceTest, public testing::Test {};
+
+TEST_F(ReferenceGatherRegressionTest, ExoticDtypeDynamicShapeIndices) {
+ const auto P = std::make_shared<op::v0::Parameter>(element::f8e4m3, Shape{4, 2});
+ const auto I = std::make_shared<op::v0::Parameter>(element::i64, PartialShape::dynamic(1));
+ const auto A = op::v0::Constant::create(element::i64, Shape{}, {0});
+ const auto G = std::make_shared<op::v8::Gather>(P, I, A);
+ function = std::make_shared<Model>(G, ParameterVector{P, I});
+
+ inputData = {CreateTensor(Shape{4, 2},
+ element::f8e4m3,
+ std::vector<ov::float8_e4m3>{1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f}),
+ CreateTensor(Shape{2}, element::i64, std::vector<int64_t>{0, 2})};
+ refOutData = {CreateTensor(Shape{2, 2}, element::f8e4m3, std::vector<ov::float8_e4m3>{1.f, 2.f, 5.f, 6.f})};
+ Exec();
+}
} // namespace