OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Add `SparseFillEmptyRows-16` reference implementation (#30307)
Przemyslaw Wysockib85136f3216e
diff --git a/src/core/include/openvino/opsets/opset16_tbl.hpp b/src/core/include/openvino/opsets/opset16_tbl.hppindex 6c19ed12121..e1917c64f9a 100644--- a/src/core/include/openvino/opsets/opset16_tbl.hpp+++ b/src/core/include/openvino/opsets/opset16_tbl.hpp@@ -17,3 +17,4 @@ _OPENVINO_OP_REG(ShapeOf, ov::op::v3)_OPENVINO_OP_REG(Identity, ov::op::v16)_OPENVINO_OP_REG(ISTFT, ov::op::v16)_OPENVINO_OP_REG(SegmentMax, ov::op::v16)+_OPENVINO_OP_REG(SparseFillEmptyRows, ov::op::v16)diff --git a/src/core/reference/include/openvino/reference/sparse_fill_empty_rows.hpp b/src/core/reference/include/openvino/reference/sparse_fill_empty_rows.hppnew file mode 100644index 00000000000..37d2426204e--- /dev/null+++ b/src/core/reference/include/openvino/reference/sparse_fill_empty_rows.hpp@@ -0,0 +1,77 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include <algorithm>+#include <unordered_set>+#include <vector>++#include "openvino/core/shape.hpp"++namespace ov::reference {++template <typename T, typename T_IDX>+void sparse_fill_empty_rows(const T* values,+ const size_t values_size,+ const T_IDX* dense_shape,+ const T_IDX* indices,+ const size_t indices_size,+ const T default_value,+ T_IDX* output_indices,+ T* output_values,+ bool* empty_row_indicator) {+ const auto num_rows = dense_shape[0];++ std::unordered_set<T_IDX> existing_rows;+ for (size_t i = 0, idx = 0; i < values_size; i++, idx += 2) {+ existing_rows.insert(indices[idx]);+ }++ std::vector<T_IDX> empty_rows;+ empty_rows.reserve(num_rows - existing_rows.size());+ for (T_IDX i = 0; i < num_rows; i++) {+ const bool is_empty = (existing_rows.find(i) == existing_rows.end());+ empty_row_indicator[i] = is_empty;+ if (is_empty) {+ empty_rows.push_back(i);+ }+ }++ // Vector of pairs containing ((row, column), source_index) for+ // both existing values and new empty rows to be added+ const size_t total_rows = values_size + empty_rows.size();+ std::vector<std::pair<std::pair<T_IDX, T_IDX>, size_t>> row_col_pairs(total_rows);++ // Add existing values and then empty rows+ for (size_t i = 0, idx = 0; i < values_size; i++, idx += 2) {+ row_col_pairs[i] = {{indices[idx], indices[idx + 1]}, i};+ }+ for (size_t i = 0; i < empty_rows.size(); i++) {+ row_col_pairs[values_size + i] = {{empty_rows[i], 0}, values_size + i};+ }++ std::sort(row_col_pairs.begin(), row_col_pairs.end(), [](const auto& a, const auto& b) {+ if (a.first.first != b.first.first) {+ return a.first.first < b.first.first;+ }+ return a.first.second < b.first.second;+ });++ for (size_t i = 0, out_idx = 0; i < total_rows; i++, out_idx += 2) {+ const auto& [row_col, src_idx] = row_col_pairs[i];+ const auto& [row, col] = row_col;++ output_indices[out_idx] = row;+ output_indices[out_idx + 1] = col;++ if (src_idx < values_size) {+ output_values[i] = values[src_idx];+ } else {+ output_values[i] = default_value;+ }+ }+}++} // namespace ov::referencediff --git a/src/core/tests/opset.cpp b/src/core/tests/opset.cppindex 3006e04a02f..ba3a22f149e 100644--- a/src/core/tests/opset.cpp+++ b/src/core/tests/opset.cpp@@ -77,7 +77,7 @@ INSTANTIATE_TEST_SUITE_P(opset,OpsetTestParams{ov::get_opset13, 186},OpsetTestParams{ov::get_opset14, 188},OpsetTestParams{ov::get_opset15, 199},- OpsetTestParams{ov::get_opset16, 6}),+ OpsetTestParams{ov::get_opset16, 7}),OpsetTestNameGenerator{});class MyOpOld : public ov::op::Op {diff --git a/src/plugins/template/backend/ops/ops_evaluates.hpp b/src/plugins/template/backend/ops/ops_evaluates.hppindex 61471f81321..b0047da122f 100644--- a/src/plugins/template/backend/ops/ops_evaluates.hpp+++ b/src/plugins/template/backend/ops/ops_evaluates.hpp@@ -563,3 +563,7 @@ extern template bool evaluate_node<ov::op::v16::Identity>(std::shared_ptr<ov::Noextern template bool evaluate_node<ov::op::v16::SegmentMax>(std::shared_ptr<ov::Node> node,ov::TensorVector& outputs,const ov::TensorVector& inputs);++extern template bool evaluate_node<ov::op::v16::SparseFillEmptyRows>(std::shared_ptr<ov::Node> node,+ ov::TensorVector& outputs,+ const ov::TensorVector& inputs);diff --git a/src/plugins/template/backend/ops/sparse_fill_empty_rows.cpp b/src/plugins/template/backend/ops/sparse_fill_empty_rows.cppnew file mode 100644index 00000000000..3b9ad2b9263--- /dev/null+++ b/src/plugins/template/backend/ops/sparse_fill_empty_rows.cpp@@ -0,0 +1,99 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/reference/sparse_fill_empty_rows.hpp"++#include "element_visitor.hpp"+#include "evaluate_node.hpp"+#include "sparse_fill_empty_rows_shape_inference.hpp"++template <ov::element::Type_t ET_data, ov::element::Type_t ET_idx>+bool evaluate_index_type(const std::shared_ptr<ov::op::v16::SparseFillEmptyRows>& op,+ ov::TensorVector& outputs,+ const ov::TensorVector& inputs) {+ using T_data = typename ov::element_type_traits<ET_data>::value_type;+ using T_idx = typename ov::element_type_traits<ET_idx>::value_type;++ auto input_shapes = std::vector<ov::PartialShape>{+ op->get_input_shape(0), // values+ op->get_input_shape(1), // dense_shape+ op->get_input_shape(2), // indices+ op->get_input_shape(3) // default_value+ };++ const auto output_shapes = ov::op::v16::shape_infer(op.get(), input_shapes, make_tensor_accessor(inputs));+ outputs[0].set_shape(output_shapes[0].to_shape()); // output_indices+ outputs[1].set_shape(output_shapes[1].to_shape()); // output_values+ outputs[2].set_shape(output_shapes[2].to_shape()); // empty_row_indicator++ auto values = inputs[0].data<const T_data>();+ const T_idx* dense_shape = inputs[1].data<const T_idx>();+ const T_idx* indices = inputs[2].data<const T_idx>();+ const T_data default_value = *inputs[3].data<const T_data>();++ T_idx* output_indices = outputs[0].data<T_idx>();+ T_data* output_values = outputs[1].data<T_data>();+ bool* empty_row_indicator = outputs[2].data<bool>();++ const size_t values_size = inputs[0].get_shape()[0];+ const size_t indices_size = ov::shape_size(inputs[2].get_shape());++ ov::reference::sparse_fill_empty_rows(values,+ values_size,+ dense_shape,+ indices,+ indices_size,+ default_value,+ output_indices,+ output_values,+ empty_row_indicator);+ return true;+}++template <ov::element::Type_t ET_data>+bool evaluate_data_type(const std::shared_ptr<ov::op::v16::SparseFillEmptyRows>& op,+ ov::TensorVector& outputs,+ const ov::TensorVector& inputs) {+ const auto& index_type = op->get_input_element_type(1);+ using ov::op::v16::SparseFillEmptyRows;+ using namespace ov::element;++ switch (index_type) {+ case i32:+ return evaluate_index_type<ET_data, i32>(ov::as_type_ptr<SparseFillEmptyRows>(op), outputs, inputs);+ case i64:+ return evaluate_index_type<ET_data, i64>(ov::as_type_ptr<SparseFillEmptyRows>(op), outputs, inputs);+ default:+ OPENVINO_THROW("Unhandled index type ", index_type, " in evaluate_node() for SparseFillEmptyRows");+ }+}++template <>+bool evaluate_node<ov::op::v16::SparseFillEmptyRows>(std::shared_ptr<ov::Node> node,+ ov::TensorVector& outputs,+ const ov::TensorVector& inputs) {+ using ov::op::v16::SparseFillEmptyRows;+ using namespace ov::element;++ switch (const auto& element_type = node->get_output_element_type(1); element_type) {+ case i8:+ return evaluate_data_type<i8>(ov::as_type_ptr<SparseFillEmptyRows>(node), outputs, inputs);+ case i32:+ return evaluate_data_type<i32>(ov::as_type_ptr<SparseFillEmptyRows>(node), outputs, inputs);+ case i64:+ return evaluate_data_type<i64>(ov::as_type_ptr<SparseFillEmptyRows>(node), outputs, inputs);+ case u8:+ return evaluate_data_type<u8>(ov::as_type_ptr<SparseFillEmptyRows>(node), outputs, inputs);+ case u32:+ return evaluate_data_type<u32>(ov::as_type_ptr<SparseFillEmptyRows>(node), outputs, inputs);+ case u64:+ return evaluate_data_type<u64>(ov::as_type_ptr<SparseFillEmptyRows>(node), outputs, inputs);+ case f16:+ return evaluate_data_type<f16>(ov::as_type_ptr<SparseFillEmptyRows>(node), outputs, inputs);+ case f32:+ return evaluate_data_type<f32>(ov::as_type_ptr<SparseFillEmptyRows>(node), outputs, inputs);+ default:+ OPENVINO_THROW("Unhandled data type ", element_type, " in evaluate_node() for SparseFillEmptyRows");+ }+}diff --git a/src/plugins/template/backend/opset_int_tbl.hpp b/src/plugins/template/backend/opset_int_tbl.hppindex 7486421ba85..ee295d92da5 100644--- a/src/plugins/template/backend/opset_int_tbl.hpp+++ b/src/plugins/template/backend/opset_int_tbl.hpp@@ -178,6 +178,7 @@ _OPENVINO_OP_REG(SearchSorted, ov::op::v15)_OPENVINO_OP_REG(Identity, ov::op::v16)_OPENVINO_OP_REG(ISTFT, ov::op::v16)_OPENVINO_OP_REG(SegmentMax, ov::op::v16)+_OPENVINO_OP_REG(SparseFillEmptyRows, ov::op::v16)_OPENVINO_OP_REG(AUGRUCell, ov::op::internal)_OPENVINO_OP_REG(AUGRUSequence, ov::op::internal)diff --git a/src/plugins/template/tests/functional/op_reference/sparse_fill_empty_rows.cpp b/src/plugins/template/tests/functional/op_reference/sparse_fill_empty_rows.cppnew file mode 100644index 00000000000..9418ae28f29--- /dev/null+++ b/src/plugins/template/tests/functional/op_reference/sparse_fill_empty_rows.cpp@@ -0,0 +1,180 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/op/sparse_fill_empty_rows.hpp"++#include <gtest/gtest.h>++#include "base_reference_test.hpp"+#include "openvino/core/type/element_type_traits.hpp"+#include "openvino/op/constant.hpp"++namespace {+struct SparseFillEmptyRowsParams {+ SparseFillEmptyRowsParams(const reference_tests::Tensor& valuesTensor,+ const reference_tests::Tensor& denseShapeTensor,+ const reference_tests::Tensor& indicesTensor,+ const reference_tests::Tensor& defaultValueTensor,+ const reference_tests::Tensor& expectedIndicesTensor,+ const reference_tests::Tensor& expectedValuesTensor,+ const reference_tests::Tensor& expectedEmptyRowIndicatorTensor)+ : valuesTensor(valuesTensor),+ denseShapeTensor(denseShapeTensor),+ indicesTensor(indicesTensor),+ defaultValueTensor(defaultValueTensor),+ expectedIndicesTensor(expectedIndicesTensor),+ expectedValuesTensor(expectedValuesTensor),+ expectedEmptyRowIndicatorTensor(expectedEmptyRowIndicatorTensor) {}++ reference_tests::Tensor valuesTensor;+ reference_tests::Tensor denseShapeTensor;+ reference_tests::Tensor indicesTensor;+ reference_tests::Tensor defaultValueTensor;+ reference_tests::Tensor expectedIndicesTensor;+ reference_tests::Tensor expectedValuesTensor;+ reference_tests::Tensor expectedEmptyRowIndicatorTensor;+};++class ReferenceSparseFillEmptyRowsV16LayerTest : public testing::TestWithParam<SparseFillEmptyRowsParams>,+ public reference_tests::CommonReferenceTest {+protected:+ void SetUp() override {+ const auto& params = GetParam();+ function = CreateFunction(params);+ inputData = {params.valuesTensor.data,+ params.denseShapeTensor.data,+ params.indicesTensor.data,+ params.defaultValueTensor.data};+ refOutData = {params.expectedIndicesTensor.data,+ params.expectedValuesTensor.data,+ params.expectedEmptyRowIndicatorTensor.data};+ }++public:+ static std::string getTestCaseName(const testing::TestParamInfo<SparseFillEmptyRowsParams>& obj) {+ auto param = obj.param;+ std::ostringstream result;+ result << "valuesType=" << param.valuesTensor.type;+ result << "_valuesShape=" << param.valuesTensor.shape;+ result << "_denseShapeType=" << param.denseShapeTensor.type;+ result << "_denseShapeValues=" << testing::PrintToString(param.denseShapeTensor.data);+ result << "_indicesType=" << param.indicesTensor.type;+ result << "_indicesShape=" << param.indicesTensor.shape;+ result << "_defaultValue=" << testing::PrintToString(param.defaultValueTensor.data);+ return result.str();+ }++private:+ static std::shared_ptr<ov::Model> CreateFunction(const SparseFillEmptyRowsParams& params) {+ using ov::op::v0::Constant;+ using ov::op::v0::Parameter;++ const auto values = std::make_shared<Parameter>(params.valuesTensor.type, params.valuesTensor.shape);+ const auto dense_shape =+ std::make_shared<Parameter>(params.denseShapeTensor.type, params.denseShapeTensor.shape);+ const auto indices = std::make_shared<Parameter>(params.indicesTensor.type, params.indicesTensor.shape);+ const auto default_value =+ std::make_shared<Parameter>(params.defaultValueTensor.type, params.defaultValueTensor.shape);++ const auto sparseFillEmptyRows =+ std::make_shared<ov::op::v16::SparseFillEmptyRows>(values, dense_shape, indices, default_value);++ return std::make_shared<ov::Model>(ov::OutputVector{sparseFillEmptyRows->output(0),+ sparseFillEmptyRows->output(1),+ sparseFillEmptyRows->output(2)},+ ov::ParameterVector{values, dense_shape, indices, default_value});+ }+};++TEST_P(ReferenceSparseFillEmptyRowsV16LayerTest, CompareWithRefs) {+ Exec();+}++template <ov::element::Type_t T, ov::element::Type_t T_idx>+std::vector<SparseFillEmptyRowsParams> generateSparseFillEmptyRowsParams() {+ using T_D = typename ov::element_type_traits<T>::value_type;+ using T_I = typename ov::element_type_traits<T_idx>::value_type;+ using reference_tests::Tensor;++ std::vector<SparseFillEmptyRowsParams> params{+ // No empty rows+ SparseFillEmptyRowsParams(+ Tensor({3}, T, std::vector<T_D>{1, 2, 3}), // values+ Tensor({2}, T_idx, std::vector<T_I>{3, 4}), // dense_shape+ Tensor({3, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0}), // indices+ Tensor({}, T, std::vector<T_D>{-1}), // default_value+ Tensor({3, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0}), // expected_indices+ Tensor({3}, T, std::vector<T_D>{1, 2, 3}), // expected_values+ Tensor({3}, ov::element::boolean, std::vector<uint8_t>{0, 0, 0}) // expected_empty_row_indicator+ ),++ // One empty row in the middle+ SparseFillEmptyRowsParams(+ Tensor({3}, T, std::vector<T_D>{1, 2, 3}), // values+ Tensor({2}, T_idx, std::vector<T_I>{4, 4}), // dense_shape+ Tensor({3, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 3, 0}), // indices+ Tensor({}, T, std::vector<T_D>{-1}), // default_value+ Tensor({4, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0, 3, 0}), // expected_indices+ Tensor({4}, T, std::vector<T_D>{1, 2, -1, 3}), // expected_values+ Tensor({4}, ov::element::boolean, std::vector<uint8_t>{0, 0, 1, 0}) // expected_empty_row_indicator+ ),++ // Multiple empty rows+ SparseFillEmptyRowsParams(+ Tensor({2}, T, std::vector<T_D>{1, 2}), // values+ Tensor({2}, T_idx, std::vector<T_I>{5, 3}), // dense_shape+ Tensor({2, 2}, T_idx, std::vector<T_I>{0, 0, 4, 0}), // indices+ Tensor({}, T, std::vector<T_D>{-1}), // default_value+ Tensor({5, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0, 3, 0, 4, 0}), // expected_indices+ Tensor({5}, T, std::vector<T_D>{1, -1, -1, -1, 2}), // expected_values+ Tensor({5}, ov::element::boolean, std::vector<uint8_t>{0, 1, 1, 1, 0}) // expected_empty_row_indicator+ ),++ // All rows empty+ SparseFillEmptyRowsParams(+ Tensor({0}, T, std::vector<T_D>{}), // values+ Tensor({2}, T_idx, std::vector<T_I>{3, 4}), // dense_shape+ Tensor({0, 2}, T_idx, std::vector<T_I>{}), // indices+ Tensor({}, T, std::vector<T_D>{-1}), // default_value+ Tensor({3, 2}, T_idx, std::vector<T_I>{0, 0, 1, 0, 2, 0}), // expected_indices+ Tensor({3}, T, std::vector<T_D>{-1, -1, -1}), // expected_values+ Tensor({3}, ov::element::boolean, std::vector<uint8_t>{1, 1, 1}) // expected_empty_row_indicator+ ),++ // Non-zero column indices for empty rows+ SparseFillEmptyRowsParams(+ Tensor({4}, T, std::vector<T_D>{1, 2, 3, 4}), // values+ Tensor({2}, T_idx, std::vector<T_I>{5, 6}), // dense_shape+ Tensor({4, 2}, T_idx, std::vector<T_I>{0, 1, 1, 2, 3, 3, 4, 4}), // indices+ Tensor({}, T, std::vector<T_D>{99}), // default_value+ Tensor({5, 2}, T_idx, std::vector<T_I>{0, 1, 1, 2, 2, 0, 3, 3, 4, 4}), // expected_indices+ Tensor({5}, T, std::vector<T_D>{1, 2, 99, 3, 4}), // expected_values+ Tensor({5}, ov::element::boolean, std::vector<uint8_t>{0, 0, 1, 0, 0}) // expected_empty_row_indicator+ )};++ return params;+}++std::vector<SparseFillEmptyRowsParams> generateSparseFillEmptyRowsV16CombinedParams() {+ using ov::element::Type_t;+ const std::vector<std::vector<SparseFillEmptyRowsParams>> SparseFillEmptyRowsTypeParams{+ generateSparseFillEmptyRowsParams<Type_t::i32, Type_t::i32>(),+ generateSparseFillEmptyRowsParams<Type_t::i64, Type_t::i32>(),+ generateSparseFillEmptyRowsParams<Type_t::f32, Type_t::i32>(),+ generateSparseFillEmptyRowsParams<Type_t::i32, Type_t::i64>(),+ generateSparseFillEmptyRowsParams<Type_t::i64, Type_t::i64>(),+ generateSparseFillEmptyRowsParams<Type_t::f32, Type_t::i64>()};++ std::vector<SparseFillEmptyRowsParams> combinedParams;+ for (const auto& params : SparseFillEmptyRowsTypeParams) {+ combinedParams.insert(combinedParams.end(), params.begin(), params.end());+ }+ return combinedParams;+}++INSTANTIATE_TEST_SUITE_P(smoke_SparseFillEmptyRows_With_Hardcoded_Refs,+ ReferenceSparseFillEmptyRowsV16LayerTest,+ testing::ValuesIn(generateSparseFillEmptyRowsV16CombinedParams()),+ ReferenceSparseFillEmptyRowsV16LayerTest::getTestCaseName);+} // namespacediff --git a/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/src/op_impl_check/single_op_graph.cpp b/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/src/op_impl_check/single_op_graph.cppindex 5e4761acc9e..4432acd7c03 100644--- a/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/src/op_impl_check/single_op_graph.cpp+++ b/src/tests/functional/plugin/conformance/test_runner/op_conformance_runner/src/op_impl_check/single_op_graph.cpp@@ -542,6 +542,20 @@ std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v16::SegmentMareturn std::make_shared<ov::Model>(results, ov::ParameterVector{data}, "SegmentMaxGraph");}+std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v16::SparseFillEmptyRows> &node) {+ const auto values = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{5});+ const auto dense_shape = ov::op::v0::Constant::create<int32_t>(ov::element::i32, {2}, {8, 5});+ const auto indices = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::PartialShape{5, 2});+ const auto default_value = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{});+ const auto sparseFillEmptyRowsNode = std::make_shared<ov::op::v16::SparseFillEmptyRows>(values, dense_shape, indices, default_value);+ ov::ResultVector results{+ std::make_shared<ov::op::v0::Result>(sparseFillEmptyRowsNode->output(0)),+ std::make_shared<ov::op::v0::Result>(sparseFillEmptyRowsNode->output(1)),+ std::make_shared<ov::op::v0::Result>(sparseFillEmptyRowsNode->output(2))+ };+ return std::make_shared<ov::Model>(results, ov::ParameterVector{values, indices, default_value}, "SparseFillEmptyRowsGraph");+}+std::shared_ptr<ov::Model> generate(const std::shared_ptr<ov::op::v4::Interpolate> &node) {using InterpolateAttrs = op::v4::Interpolate::InterpolateAttrs;using InterpolateMode = op::v4::Interpolate::InterpolateMode;