Skip to content

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

[TF FE] Extend TF FE with `ov::op::SparseFillEmptyRows-16` (#30504)

Przemyslaw Wysockic98dd8f0703c
diff --git a/src/core/src/op/sparse_fill_empty_rows.cpp b/src/core/src/op/sparse_fill_empty_rows.cpp
index 87a9d2addad..ab53fa494eb 100644
--- a/src/core/src/op/sparse_fill_empty_rows.cpp
+++ b/src/core/src/op/sparse_fill_empty_rows.cpp
@@ -34,7 +34,7 @@ void SparseFillEmptyRows::validate_and_infer_types() {
indices_element_type);
NODE_VALIDATION_CHECK(this,
- merged_type == element::i32 || merged_type == element::i64,
+ merged_type == element::i32 || merged_type == element::i64 || merged_type.is_dynamic(),
"The element type of the indices and dense_shape inputs must be i32 or i64. Got: ",
merged_type);
diff --git a/src/frontends/tensorflow/README.md b/src/frontends/tensorflow/README.md
index 6713b5a2b8f..1b17ead9608 100644
--- a/src/frontends/tensorflow/README.md
+++ b/src/frontends/tensorflow/README.md
@@ -141,9 +141,9 @@ In rare cases, TensorFlow operation conversion requires two transformations (`Lo
In the first step, `Loader` must convert a TF operation into [Internal Operation](../tensorflow_common/helper_ops) that is used temporarily by the conversion pipeline.
The internal operation implementation must also contain the `validate_and_infer_types()` method as similar to [OpenVINO Core](https://docs.openvino.ai/2025/api/c_cpp_api/group__ov__ops__cpp__api.html) operations.
-Here is an example of an implementation for the internal operation `SparseFillEmptyRows` used to convert Wide and Deep models.
+Here is an example of an implementation for the internal operation `SparseSegmentOps`:
-https://github.com/openvinotoolkit/openvino/blob/7f3c95c161bc78ab2aefa6eab8b008142fb945bc/src/frontends/tensorflow/src/helper_ops/sparse_fill_empty_rows.hpp#L17-L55
+https://github.com/openvinotoolkit/openvino/blob/994ac20d7fd7fc3ec415dbfa5f0bf6ad886a8162/src/frontends/tensorflow_common/include/helper_ops/sparse_segment_ops.hpp#L32-L67
In the second step, `Internal Transformation` based on `ov::pass::MatcherPass` must convert sub-graphs with internal operations into sub-graphs consisting only of the OpenVINO opset.
For more information about `ov::pass::MatcherPass` based transformations and their development, read [Overview of Transformations API](https://docs.openvino.ai/2025/documentation/openvino-extensibility/transformation-api.html)
diff --git a/src/frontends/tensorflow/src/op/sparse_reshape.cpp b/src/frontends/tensorflow/src/op/sparse_reshape.cpp
index 3182ca276a1..ec5b9f4b952 100644
--- a/src/frontends/tensorflow/src/op/sparse_reshape.cpp
+++ b/src/frontends/tensorflow/src/op/sparse_reshape.cpp
@@ -3,7 +3,6 @@
//
#include "common_op_table.hpp"
-#include "helper_ops/sparse_fill_empty_rows.hpp"
#include "helper_ops/sparse_segment_ops.hpp"
#include "openvino/frontend/tensorflow/node_context.hpp"
@@ -72,31 +71,6 @@ NamedOutputVector translate_sparse_reshape_op(const ov::frontend::tensorflow::No
return {{"output_indices", output_indices}, {"output_shape", output_shape}};
}
-NamedOutputVector translate_sparse_fill_empty_rows_op(const ov::frontend::tensorflow::NodeContext& node) {
- default_op_checks(node, 3, {"SparseFillEmptyRows"});
- auto input_indices = node.get_input(0);
- auto input_values = node.get_input(1);
- auto dense_shape = node.get_input(2);
- auto default_value = node.get_input(3);
- auto node_name = node.get_name();
-
- auto sparse_fill_empty_rows = make_shared<ov::frontend::tensorflow::SparseFillEmptyRows>(input_indices,
- input_values,
- dense_shape,
- default_value,
- node.get_decoder());
- sparse_fill_empty_rows->set_friendly_name(node_name);
- set_out_name(node_name + ":0", sparse_fill_empty_rows->output(0));
- set_out_name(node_name + ":1", sparse_fill_empty_rows->output(1));
- set_out_name(node_name + ":2", sparse_fill_empty_rows->output(2));
- set_out_name(node_name + ":3", sparse_fill_empty_rows->output(3));
-
- return {{"output_indices", sparse_fill_empty_rows->output(0)},
- {"output_values", sparse_fill_empty_rows->output(1)},
- {"empty_row_indicator", sparse_fill_empty_rows->output(2)},
- {"reverse_index_map", sparse_fill_empty_rows->output(3)}};
-}
-
OutputVector translate_sparse_segment_sum_op(const ov::frontend::tensorflow::NodeContext& node) {
auto input_size = node.get_input_size();
TENSORFLOW_OP_VALIDATION(node,
diff --git a/src/frontends/tensorflow/src/op_table.cpp b/src/frontends/tensorflow/src/op_table.cpp
index bca1275228a..72befc5e2b4 100644
--- a/src/frontends/tensorflow/src/op_table.cpp
+++ b/src/frontends/tensorflow/src/op_table.cpp
@@ -62,6 +62,7 @@
#include "openvino/op/sinh.hpp"
#include "openvino/op/softplus.hpp"
#include "openvino/op/softsign.hpp"
+#include "openvino/op/sparse_fill_empty_rows.hpp"
#include "openvino/op/squared_difference.hpp"
#include "openvino/op/swish.hpp"
#include "openvino/op/tan.hpp"
@@ -108,7 +109,6 @@ TF_OP_CONVERTER(translate_queue_dequeue_op);
TF_OP_CONVERTER(translate_queue_dequeue_many_op);
TF_OP_CONVERTER(translate_readvariable_op);
TF_OP_CONVERTER(translate_restorev2_op);
-TF_OP_CONVERTER_NAMED(translate_sparse_fill_empty_rows_op);
TF_OP_CONVERTER_NAMED(translate_sparse_reshape_op);
TF_OP_CONVERTER(translate_sparse_segment_sum_op);
TF_OP_CONVERTER(translate_staticregexfullmatch_op);
@@ -381,6 +381,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"Snapshot", CreatorFunction(translate_identity_op)},
{"Softmax", CreatorFunction(translate_softmax_op)},
{"SpaceToDepth", CreatorFunction(translate_space_to_depth_op)},
+ {"SparseFillEmptyRows", CreatorFunction(translate_sparse_fill_empty_rows_op)},
{"SparseReshape", CreatorFunction(translate_sparse_reshape_op)},
{"SparseSegmentMean", CreatorFunction(translate_sparse_segment_op)},
{"SparseSegmentSqrtN", CreatorFunction(translate_sparse_segment_op)},
@@ -462,7 +463,6 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
// Translators for internal operations
{"BlockLSTM", CreatorFunction(translate_block_lstm_op)},
{"GRUBlockCell", CreatorFunction(translate_gru_block_cell_op)},
- {"SparseFillEmptyRows", CreatorFunction(translate_sparse_fill_empty_rows_op)},
{"SparseSegmentSum", CreatorFunction(translate_sparse_segment_sum_op)},
{"Unique", CreatorFunction(translate_unique_op)},
diff --git a/src/frontends/tensorflow_common/include/common_op_table.hpp b/src/frontends/tensorflow_common/include/common_op_table.hpp
index 6f838ed1205..cf51a0ef9ca 100644
--- a/src/frontends/tensorflow_common/include/common_op_table.hpp
+++ b/src/frontends/tensorflow_common/include/common_op_table.hpp
@@ -152,6 +152,7 @@ OP_CONVERTER(translate_scatter_nd_op);
OP_CONVERTER(translate_segment_max_op);
OP_CONVERTER(translate_segment_sum_op);
OP_CONVERTER(translate_space_to_batch_nd_op);
+OP_CONVERTER_NAMED(translate_sparse_fill_empty_rows_op);
OP_CONVERTER(translate_sparse_segment_op);
OP_CONVERTER(translate_sparse_tensor_dense_add_op);
OP_CONVERTER(translate_sparse_tensor_dense_mat_mul_op);
diff --git a/src/frontends/tensorflow_common/include/helper_ops/sparse_fill_empty_rows.hpp b/src/frontends/tensorflow_common/include/helper_ops/sparse_fill_empty_rows.hpp
deleted file mode 100644
index 0affd4053b8..00000000000
--- a/src/frontends/tensorflow_common/include/helper_ops/sparse_fill_empty_rows.hpp
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright (C) 2018-2025 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#pragma once
-
-#include <string>
-#include <vector>
-
-#include "helper_ops/internal_operation.hpp"
-#include "openvino/frontend/decoder.hpp"
-
-namespace ov {
-namespace frontend {
-namespace tensorflow {
-
-class SparseFillEmptyRows : public ov::frontend::tensorflow::InternalOperation {
-public:
- OPENVINO_OP("SparseFillEmptyRows", "ov::frontend::tensorflow::util", ov::frontend::tensorflow::InternalOperation);
-
- SparseFillEmptyRows(const Output<Node>& indices,
- const Output<Node>& values,
- const Output<Node>& dense_shape,
- const Output<Node>& default_value,
- const std::shared_ptr<DecoderBase>& decoder = nullptr)
- : ov::frontend::tensorflow::InternalOperation(decoder,
- OutputVector{indices, values, dense_shape, default_value},
- 4,
- "SparseFillEmptyRows") {
- validate_and_infer_types();
- }
-
- void validate_and_infer_types() override {
- // SparseFillEmptyRows fills empty rows in the input 2-D SparseTensor with a default value.
- // Inputs:
- // 0) indices: 2D. the indices of the sparse tensor
- // 1) values: 1D. the values of the sparse tensor
- // 2) dense_shape: 1D. the shape of the sparse tensor
- // 3) default_value: 0D. default value to insert into location [row, 0, ..., 0] for rows missing from the input
- // sparse tensor. output indices: 2-D. the indices of the filled sparse tensor
- // Outputs:
- // 0) output_indices
- // 1) output_values, 1D the values of the filled sparse tensor
- // 2) empty_row_indicator, 1D whether the dense row was missing in the input sparse tensor
- // 3) reverse_index_map, 1D a map from the input indices to the output indices
- ov::PartialShape output_indices_shape({ov::Dimension::dynamic(), 2});
- ov::PartialShape output_values_shape({ov::Dimension::dynamic()});
- ov::PartialShape empty_row_indicator_shape({ov::Dimension::dynamic()});
- ov::PartialShape reverse_index_map_shape({ov::Dimension::dynamic()});
-
- set_output_type(0, get_input_element_type(0), output_indices_shape);
- set_output_type(1, get_input_element_type(1), output_values_shape);
- set_output_type(2, ov::element::boolean, empty_row_indicator_shape);
- set_output_type(3, get_input_element_type(0), reverse_index_map_shape);
- }
-
- std::shared_ptr<Node> clone_with_new_inputs(const OutputVector& inputs) const override {
- FRONT_END_OP_CONVERSION_CHECK(inputs.size() == 4,
- "[TensorFlow Frontend] internal error: SparseFillEmptyRows expects 4 inputs");
- auto sparse_fill_empty_rows_node =
- std::make_shared<SparseFillEmptyRows>(inputs[0], inputs[1], inputs[2], inputs[3], m_decoder);
- sparse_fill_empty_rows_node->set_attrs(get_attrs());
- return sparse_fill_empty_rows_node;
- }
-};
-} // namespace tensorflow
-} // namespace frontend
-} // namespace ov
diff --git a/src/frontends/tensorflow_common/src/helper_transforms/embedding_segments_feature_fusing.cpp b/src/frontends/tensorflow_common/src/helper_transforms/embedding_segments_feature_fusing.cpp
index 18f794c9927..26999fb4758 100644
--- a/src/frontends/tensorflow_common/src/helper_transforms/embedding_segments_feature_fusing.cpp
+++ b/src/frontends/tensorflow_common/src/helper_transforms/embedding_segments_feature_fusing.cpp
@@ -7,7 +7,6 @@
#include <memory>
#include <vector>
-#include "helper_ops/sparse_fill_empty_rows.hpp"
#include "helper_ops/sparse_segment_ops.hpp"
#include "openvino/core/graph_util.hpp"
#include "openvino/op/broadcast.hpp"
@@ -21,6 +20,7 @@
#include "openvino/op/reshape.hpp"
#include "openvino/op/select.hpp"
#include "openvino/op/shape_of.hpp"
+#include "openvino/op/sparse_fill_empty_rows.hpp"
#include "openvino/op/split.hpp"
#include "openvino/op/squeeze.hpp"
#include "openvino/op/strided_slice.hpp"
@@ -66,11 +66,10 @@ ov::frontend::tensorflow::pass::EmbeddingSegmentSingleFeatureFusion::EmbeddingSe
// SparseFillEmptyRows outputs segment ids along with indices for each segment. Indices correspond to vectors from
// embedding table if some segment ids are not specified, SparseFillEmptyRows generate default indice for this
// segment
- auto sparse_fill_empty_rows = make_shared<SparseFillEmptyRows>(gather0_1->output(0),
- gather0_2->output(0),
- dense_shape_pattern->output(0),
- default_value_pattern->output(0));
-
+ auto sparse_fill_empty_rows = make_shared<v16::SparseFillEmptyRows>(gather0_2->output(0),
+ dense_shape_pattern->output(0),
+ gather0_1->output(0),
+ default_value_pattern->output(0));
auto strided_slice =
make_shared<v1::StridedSlice>(sparse_fill_empty_rows->output(0),
make_shared<v0::Constant>(element::i32, Shape{2}, vector<int32_t>{0, 0}),
diff --git a/src/frontends/tensorflow_common/src/op/sparse_fill_empty_rows.cpp b/src/frontends/tensorflow_common/src/op/sparse_fill_empty_rows.cpp
new file mode 100644
index 00000000000..f075508890d
--- /dev/null
+++ b/src/frontends/tensorflow_common/src/op/sparse_fill_empty_rows.cpp
@@ -0,0 +1,31 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/op/sparse_fill_empty_rows.hpp"
+
+#include "common_op_table.hpp"
+#include "openvino/op/convert.hpp"
+#include "tf_framework_node.hpp"
+#include "utils.hpp"
+
+namespace ov::frontend::tensorflow::op {
+NamedOutputVector translate_sparse_fill_empty_rows_op(const NodeContext& node) {
+ default_op_checks(node, 4, {"SparseFillEmptyRows"});
+ auto indices = node.get_input(0);
+ auto values = node.get_input(1);
+ auto dense_shape = node.get_input(2);
+ auto default_value = node.get_input(3);
+ TENSORFLOW_OP_VALIDATION(node,
+ values.get_element_type() != element::string,
+ "TensorFlow Frontend does not support string inputs for SparseFillEmptyRows.");
+
+ auto sparse_fill_empty_rows =
+ std::make_shared<ov::op::v16::SparseFillEmptyRows>(values, dense_shape, indices, default_value);
+
+ set_node_name(node.get_name(), sparse_fill_empty_rows);
+ auto outputs = sparse_fill_empty_rows->outputs();
+
+ return {{"output_indices", outputs[0]}, {"output_values", outputs[1]}, {"empty_row_indicator", outputs[2]}};
+}
+} // namespace ov::frontend::tensorflow::op
diff --git a/tests/layer_tests/tensorflow_tests/test_tf_SparseFillEmptyRows.py b/tests/layer_tests/tensorflow_tests/test_tf_SparseFillEmptyRows.py
new file mode 100644
index 00000000000..eec0d33b2d4
--- /dev/null
+++ b/tests/layer_tests/tensorflow_tests/test_tf_SparseFillEmptyRows.py
@@ -0,0 +1,94 @@
+# Copyright (C) 2018-2025 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import numpy as np
+import pytest
+import tensorflow as tf
+from common.tf_layer_test_class import CommonTFLayerTest
+
+rng = np.random.default_rng(475912)
+
+
+class TestSparseFillEmptyRows(CommonTFLayerTest):
+ def _prepare_input(self, inputs_info):
+ assert 'indices:0' in inputs_info
+ assert 'values:0' in inputs_info
+ assert 'dense_shape:0' in inputs_info
+ assert 'default_value:0' in inputs_info
+
+ values_shape = inputs_info['values:0']
+
+ inputs_data = {}
+ if np.issubdtype(self.data_type, np.floating):
+ inputs_data['values:0'] = rng.uniform(-5.0, 5.0, values_shape).astype(self.data_type)
+ inputs_data['default_value:0'] = np.array(42.0, dtype=self.data_type)
+ elif np.issubdtype(self.data_type, np.signedinteger):
+ inputs_data['values:0'] = rng.integers(-8, 8, values_shape).astype(self.data_type)
+ inputs_data['default_value:0'] = np.array(99, dtype=self.data_type)
+ else:
+ inputs_data['values:0'] = rng.integers(0, 8, values_shape).astype(self.data_type)
+ inputs_data['default_value:0'] = np.array(99, dtype=self.data_type)
+
+ inputs_data['dense_shape:0'] = np.array(self.dense_shape, dtype=np.int64)
+ if values_shape[0] == 0:
+ inputs_data['indices:0'] = np.zeros((0, 2), dtype=np.int64)
+ return inputs_data
+
+ num_rows = self.dense_shape[0]
+ num_cols = self.dense_shape[1]
+ values_count = values_shape[0]
+
+ available_rows = list(range(num_rows))
+ if self.empty_rows:
+ # Remove specific rows to ensure they're empty
+ empty_row_indices = sorted(rng.choice(available_rows, size=self.empty_rows, replace=False))
+ for row in empty_row_indices:
+ available_rows.remove(row)
+
+ row_indices = rng.choice(available_rows, size=values_count, replace=True)
+ col_indices = rng.integers(0, num_cols, size=values_count)
+
+ indices = np.column_stack((row_indices, col_indices))
+ # Sort indices by row, then by column for consistent ordering
+ indices = indices[np.lexsort((indices[:, 1], indices[:, 0]))]
+ inputs_data['indices:0'] = indices.astype(np.int64)
+
+ return inputs_data
+
+ def create_sparse_fill_empty_rows_net(self, data_type, dense_shape, values_count, empty_rows=2):
+ self.data_type = data_type
+ self.dense_shape = dense_shape
+ self.empty_rows = empty_rows
+
+ tf.compat.v1.reset_default_graph()
+ with tf.compat.v1.Session() as sess:
+ indices = tf.compat.v1.placeholder(tf.int64, [values_count, 2], 'indices')
+ values = tf.compat.v1.placeholder(data_type, [values_count], 'values')
+ dense_shape = tf.compat.v1.placeholder(tf.int64, [2], 'dense_shape')
+ default_value = tf.compat.v1.placeholder(data_type, [], 'default_value')
+ tf.raw_ops.SparseFillEmptyRows(
+ indices=indices,
+ values=values,
+ dense_shape=dense_shape,
+ default_value=default_value)
+
+ tf.compat.v1.global_variables_initializer()
+ tf_net = sess.graph_def
+ return tf_net, None
+
+ @pytest.mark.parametrize('data_type', [np.float32, np.int32])
+ @pytest.mark.parametrize('dense_shape, values_count, empty_rows', [
+ [[5, 6], 4, 2], # Basic case with 2 empty rows
+ [[10, 10], 8, 3], # Larger case with 3 empty rows
+ [[3, 4], 0, 3], # All rows empty
+ ])
+ @pytest.mark.precommit
+ @pytest.mark.nightly
+ def test_sparse_fill_empty_rows(self, data_type,
+ dense_shape, values_count, empty_rows,
+ ie_device, precision, ir_version, temp_dir):
+ if ie_device == 'GPU':
+ pytest.skip("operation extension is not supported on GPU")
+ self._test(*self.create_sparse_fill_empty_rows_net(data_type,
+ dense_shape, values_count, empty_rows),
+ ie_device, precision, ir_version, temp_dir=temp_dir)