Skip to content

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

[TF FE] Support SparseSegmentSqrtN operation (#29960)

Roman Kazantsevc6a691205701
diff --git a/src/frontends/tensorflow/docs/supported_ops.md b/src/frontends/tensorflow/docs/supported_ops.md
index c9534c8aee0..7dfbb16123f 100644
--- a/src/frontends/tensorflow/docs/supported_ops.md
+++ b/src/frontends/tensorflow/docs/supported_ops.md
@@ -1134,7 +1134,7 @@ A "supported operation" is one that TensorFlow Frontend can convert to the OpenV
| SparseSegmentMeanGrad | NO | |
| SparseSegmentMeanGradV2 | NO | |
| SparseSegmentMeanWithNumSegments | NO | |
-| SparseSegmentSqrtN | NO | |
+| SparseSegmentSqrtN | YES | |
| SparseSegmentSqrtNGrad | NO | |
| SparseSegmentSqrtNGradV2 | NO | |
| SparseSegmentSqrtNWithNumSegments | NO | |
diff --git a/src/frontends/tensorflow/src/op_table.cpp b/src/frontends/tensorflow/src/op_table.cpp
index c1c430e92f0..d5a62632d94 100644
--- a/src/frontends/tensorflow/src/op_table.cpp
+++ b/src/frontends/tensorflow/src/op_table.cpp
@@ -381,7 +381,8 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"Softmax", CreatorFunction(translate_softmax_op)},
{"SpaceToDepth", CreatorFunction(translate_space_to_depth_op)},
{"SparseReshape", CreatorFunction(translate_sparse_reshape_op)},
- {"SparseSegmentMean", CreatorFunction(translate_sparse_segment_mean_op)},
+ {"SparseSegmentMean", CreatorFunction(translate_sparse_segment_op)},
+ {"SparseSegmentSqrtN", CreatorFunction(translate_sparse_segment_op)},
{"SparseTensorDenseAdd", CreatorFunction(translate_sparse_tensor_dense_add_op)},
{"SparseTensorDenseMatMul", CreatorFunction(translate_sparse_tensor_dense_mat_mul_op)},
{"SparseToDense", CreatorFunction(translate_sparse_to_dense_op)},
diff --git a/src/frontends/tensorflow_common/include/common_op_table.hpp b/src/frontends/tensorflow_common/include/common_op_table.hpp
index 8c4161f1d65..9621e087c36 100644
--- a/src/frontends/tensorflow_common/include/common_op_table.hpp
+++ b/src/frontends/tensorflow_common/include/common_op_table.hpp
@@ -151,7 +151,7 @@ OP_CONVERTER(translate_rsqrt_op);
OP_CONVERTER(translate_scatter_nd_op);
OP_CONVERTER(translate_segment_sum_op);
OP_CONVERTER(translate_space_to_batch_nd_op);
-OP_CONVERTER(translate_sparse_segment_mean_op);
+OP_CONVERTER(translate_sparse_segment_op);
OP_CONVERTER(translate_sparse_tensor_dense_add_op);
OP_CONVERTER(translate_sparse_tensor_dense_mat_mul_op);
OP_CONVERTER(translate_sparse_to_dense_op);
diff --git a/src/frontends/tensorflow_common/src/op/sparse_segment_mean.cpp b/src/frontends/tensorflow_common/src/op/sparse_segment_mean.cpp
deleted file mode 100644
index 7b51d9b5417..00000000000
--- a/src/frontends/tensorflow_common/src/op/sparse_segment_mean.cpp
+++ /dev/null
@@ -1,75 +0,0 @@
-// Copyright (C) 2018-2025 Intel Corporation
-// SPDX-License-Identifier: Apache-2.0
-//
-
-#include "common_op_table.hpp"
-#include "openvino/op/add.hpp"
-#include "openvino/op/broadcast.hpp"
-#include "openvino/op/constant.hpp"
-#include "openvino/op/convert.hpp"
-#include "openvino/op/divide.hpp"
-#include "openvino/op/embedding_segments_sum.hpp"
-#include "openvino/op/gather.hpp"
-#include "openvino/op/reshape.hpp"
-#include "openvino/op/scatter_update.hpp"
-#include "openvino/op/shape_of.hpp"
-#include "openvino/op/squeeze.hpp"
-#include "openvino/op/subtract.hpp"
-#include "openvino/op/transpose.hpp"
-#include "openvino/op/unique.hpp"
-#include "utils.hpp"
-
-using namespace std;
-using namespace ov::op;
-
-namespace ov {
-namespace frontend {
-namespace tensorflow {
-namespace op {
-OutputVector translate_sparse_segment_mean_op(const NodeContext& node) {
- default_op_checks(node, 3, {"SparseSegmentMean"});
- auto data = node.get_input(0);
- auto indices = std::make_shared<v0::Convert>(node.get_input(1), element::i64);
- auto segment_ids = std::make_shared<v0::Convert>(node.get_input(2), element::i64);
- auto data_rank = std::make_shared<v3::ShapeOf>(std::make_shared<v3::ShapeOf>(node.get_input(0)));
-
- // get the last index from segment_ids
- auto segments_ids_size = std::make_shared<v3::ShapeOf>(segment_ids, element::i64);
- auto const_one = create_same_type_const<int32_t>(indices, vector<int32_t>{1}, Shape{1});
- auto const_zero = create_same_type_const<int32_t>(indices, vector<int32_t>{0}, Shape{1});
- auto last_idx = std::make_shared<v1::Subtract>(segments_ids_size, const_one);
-
- // segment_ids are always sorted, so the last index from segment_ids can be used to determine the number of output
- // segments.
- auto last_segment_idx = std::make_shared<v8::Gather>(segment_ids, last_idx, const_zero);
- auto n_segments = std::make_shared<v1::Add>(last_segment_idx, const_one);
-
- // get sums of sparse segments
- auto embedding_segments_sum =
- make_shared<v3::EmbeddingSegmentsSum>(data, indices, segment_ids, std::make_shared<v0::Squeeze>(n_segments));
-
- // get the sizes of each segment
- auto unique_segment_ids = make_shared<v10::Unique>(segment_ids, true, element::i64, element::i64);
- auto broadcast = make_shared<v3::Broadcast>(const_one, n_segments);
- auto divisors = make_shared<v3::ScatterUpdate>(broadcast,
- unique_segment_ids->output(0),
- unique_segment_ids->output(3),
- const_zero);
- auto divisors_with_correct_type = make_shared<v1::ConvertLike>(divisors, data);
- auto divisors_shape = make_shared<v3::ScatterUpdate>(make_shared<v3::Broadcast>(const_one, data_rank),
- const_zero,
- n_segments,
- const_zero);
- auto divisors_with_correct_shape = std::make_shared<v1::Reshape>(divisors_with_correct_type, divisors_shape, false);
-
- // divide the sums by the size of the segments
- auto mean = std::make_shared<v1::Divide>(embedding_segments_sum, divisors_with_correct_shape);
-
- set_node_name(node.get_name(), mean);
- return {mean};
-}
-
-} // namespace op
-} // namespace tensorflow
-} // namespace frontend
-} // namespace ov
diff --git a/src/frontends/tensorflow_common/src/op/sparse_segment_op.cpp b/src/frontends/tensorflow_common/src/op/sparse_segment_op.cpp
new file mode 100644
index 00000000000..e74fbb05294
--- /dev/null
+++ b/src/frontends/tensorflow_common/src/op/sparse_segment_op.cpp
@@ -0,0 +1,83 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "common_op_table.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/broadcast.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/divide.hpp"
+#include "openvino/op/embedding_segments_sum.hpp"
+#include "openvino/op/gather.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/scatter_update.hpp"
+#include "openvino/op/shape_of.hpp"
+#include "openvino/op/sqrt.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/subtract.hpp"
+#include "openvino/op/unique.hpp"
+#include "utils.hpp"
+
+using namespace std;
+using namespace ov::op;
+
+namespace ov {
+namespace frontend {
+namespace tensorflow {
+namespace op {
+OutputVector translate_sparse_segment_op(const NodeContext& node) {
+ default_op_checks(node, 3, {"SparseSegmentMean", "SparseSegmentSqrtN"});
+ auto data = node.get_input(0);
+ auto op_type = node.get_op_type();
+ auto indices = node.get_input(1);
+ indices = std::make_shared<v0::Convert>(indices, element::i64);
+ auto segment_ids = node.get_input(2);
+ segment_ids = std::make_shared<v0::Convert>(segment_ids, element::i64);
+ auto data_rank = compute_subgraph_scalar_rank(data, element::i32, false);
+
+ // get the last index from segment_ids
+ auto segments_ids_size = std::make_shared<v3::ShapeOf>(segment_ids, element::i64);
+ auto const_one = make_shared<v0::Constant>(element::i64, Shape{1}, 1);
+ auto const_zero = make_shared<v0::Constant>(element::i64, Shape{1}, 0);
+ auto last_idx = std::make_shared<v1::Subtract>(segments_ids_size, const_one);
+
+ // segment_ids are always sorted
+ // so the last index from segment_ids can be used to determine the number of segments
+ auto last_segment_idx = std::make_shared<v8::Gather>(segment_ids, last_idx, const_zero);
+ auto n_segments = std::make_shared<v1::Add>(last_segment_idx, const_one);
+
+ // get sums of sparse segments
+ auto scalar_n_segments = make_shared<v0::Squeeze>(n_segments, const_zero);
+ Output<Node> result = make_shared<v3::EmbeddingSegmentsSum>(data, indices, segment_ids, scalar_n_segments);
+
+ // get the sizes of each segment
+ auto unique_segment_ids = make_shared<v10::Unique>(segment_ids, true, element::i64, element::i64);
+ Output<Node> divisors = make_shared<v3::Broadcast>(const_one, n_segments);
+ divisors = make_shared<v3::ScatterUpdate>(divisors,
+ unique_segment_ids->output(0),
+ unique_segment_ids->output(3),
+ const_zero);
+ divisors = make_shared<v1::ConvertLike>(divisors, data);
+
+ if (op_type == "SparseSegmentSqrtN") {
+ divisors = make_shared<v0::Sqrt>(divisors);
+ }
+
+ // since result has a shape [num_segments, s1, ..., st]
+ // it must reshape divisors to newshape [num_segments, 1, ..., 1]
+ Output<Node> divisors_shape = make_shared<v3::Broadcast>(const_one, data_rank);
+ divisors_shape = make_shared<v3::ScatterUpdate>(divisors_shape, const_zero, n_segments, const_zero);
+ divisors = std::make_shared<v1::Reshape>(divisors, divisors_shape, false);
+
+ // normalize result by a size of a segment or square-root of it
+ result = std::make_shared<v1::Divide>(result, divisors);
+
+ set_node_name(node.get_name(), result.get_node_shared_ptr());
+ return {result};
+}
+
+} // namespace op
+} // namespace tensorflow
+} // namespace frontend
+} // namespace ov
diff --git a/tests/layer_tests/tensorflow_tests/test_tf_SparseSegmentMean.py b/tests/layer_tests/tensorflow_tests/test_tf_SparseSegmentOp.py
similarity index 81%
rename from tests/layer_tests/tensorflow_tests/test_tf_SparseSegmentMean.py
rename to tests/layer_tests/tensorflow_tests/test_tf_SparseSegmentOp.py
index b69d5add3fb..763fb3b7a44 100644
--- a/tests/layer_tests/tensorflow_tests/test_tf_SparseSegmentMean.py
+++ b/tests/layer_tests/tensorflow_tests/test_tf_SparseSegmentOp.py
@@ -24,7 +24,8 @@ class TestSparseSegmentMean(CommonTFLayerTest):
all_indices = []
for row_ind in range(0, self.shape[0]):
all_indices.append(row_ind)
- inputs_data['indices:0'] = rng.choice(all_indices, self.indices_shape[0], replace=False).astype(self.indices_type)
+ inputs_data['indices:0'] = rng.choice(all_indices, self.indices_shape[0], replace=False).astype(
+ self.indices_type)
segment_ids = []
for ind in range(0, self.indices_shape[0]):
@@ -33,8 +34,12 @@ class TestSparseSegmentMean(CommonTFLayerTest):
return inputs_data
- def create_sparse_segment_mean(self, data_type, indices_type, segment_indices_type,
+ def create_sparse_segment_mean(self, op_type, data_type, indices_type, segment_indices_type,
shape, indices_shape, segments_num):
+ op_map = {
+ 'SparseSegmentMean': tf.raw_ops.SparseSegmentMean,
+ 'SparseSegmentSqrtN': tf.raw_ops.SparseSegmentSqrtN,
+ }
self.data_type = data_type
self.indices_type = indices_type
self.segment_indices_type = segment_indices_type
@@ -46,7 +51,7 @@ class TestSparseSegmentMean(CommonTFLayerTest):
values = tf.compat.v1.placeholder(data_type, shape, 'values')
indices = tf.compat.v1.placeholder(indices_type, indices_shape, 'indices')
segments_ids = tf.compat.v1.placeholder(segment_indices_type, indices_shape, 'segment_indices')
- tf.raw_ops.SparseSegmentMean(
+ op_map[op_type](
data=values,
indices=indices,
segment_ids=segments_ids)
@@ -55,6 +60,7 @@ class TestSparseSegmentMean(CommonTFLayerTest):
return tf_net, None
+ @pytest.mark.parametrize('op_type', ['SparseSegmentMean', 'SparseSegmentSqrtN'])
@pytest.mark.parametrize('data_type', [np.float16, np.float32, np.float64])
@pytest.mark.parametrize('indices_type', [np.int32, np.int64])
@pytest.mark.parametrize('segment_indices_type', [np.int32, np.int64])
@@ -67,16 +73,16 @@ class TestSparseSegmentMean(CommonTFLayerTest):
])
@pytest.mark.precommit
@pytest.mark.nightly
- def test_sparse_segment_mean(self, data_type, indices_type, segment_indices_type,
+ def test_sparse_segment_mean(self, op_type, data_type, indices_type, segment_indices_type,
shape, indices_shape, segments_num,
ie_device, precision, ir_version, temp_dir,
use_legacy_frontend):
kwargs = {}
if ie_device == 'GPU':
kwargs = {
- 'custom_eps': 1e-2,
+ 'custom_eps': 1e-2,
}
- self._test(*self.create_sparse_segment_mean(data_type, indices_type, segment_indices_type,
+ self._test(*self.create_sparse_segment_mean(op_type, data_type, indices_type, segment_indices_type,
shape, indices_shape, segments_num),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend, **kwargs)