support UniqueWithCounts for TF FE (#21565)

* support UniqueWithCounts for TF FE

* set node name and fix code style
This commit is contained in:
Ahmad Chalhoub authored and GitHub committed 2023-12-15 18:33:29 +04:00
1 parent 4b66fbfe21
commit c087fb4206
4 files changed
+86

No files matched your search

@@ -327,6 +327,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"Transpose", CreatorFunction(translate_transpose_op)},
{"TruncateDiv", CreatorFunction(translate_truncate_div_op)},
{"TruncateMod", CreatorFunction(translate_truncate_mod_op)},
{"UniqueWithCounts", CreatorFunction(translate_unique_with_counts_op)},
{"Unpack", CreatorFunction(translate_unpack_op)},
{"UnravelIndex", CreatorFunction(translate_unravel_index_op)},
{"UnsortedSegmentSum", CreatorFunction(translate_unsorted_segment_sum_op)},
@@ -160,6 +160,7 @@ OP_CONVERTER_NAMED(translate_top_k_v2_op);
OP_CONVERTER(translate_transpose_op);
OP_CONVERTER(translate_truncate_div_op);
OP_CONVERTER(translate_truncate_mod_op);
OP_CONVERTER(translate_unique_with_counts_op);
OP_CONVERTER(translate_unpack_op);
OP_CONVERTER(translate_unravel_index_op);
OP_CONVERTER(translate_unsorted_segment_sum_op);
@@ -0,0 +1,37 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "common_op_table.hpp"
#include "openvino/op/unique.hpp"
using namespace std;
using namespace ov::op;
namespace ov {
namespace frontend {
namespace tensorflow {
namespace op {
OutputVector translate_unique_with_counts_op(const NodeContext& node) {
default_op_checks(node, 1, {"UniqueWithCounts"});
// get input 'x' from node and node name
auto x = node.get_input(0);
auto node_name = node.get_name();
auto unique = make_shared<v10::Unique>(x, false, ov::element::i32, ov::element::i32);
set_node_name(node_name, unique);
// story 'y', 'idx', and 'count' outputs from Unique in separate variables
auto y = unique->output(0);
auto idx = unique->output(2);
auto count = unique->output(3);
return {{y}, {idx}, {count}};
}
} // namespace op
} // namespace tensorflow
} // namespace frontend
} // namespace ov
@@ -0,0 +1,47 @@
# Copyright (C) 2018-2023 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
class TestUnique(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
assert 'x' in inputs_info, "Test error: inputs_info must contain `x`"
x_shape = inputs_info['x']
inputs_data = {}
inputs_data['x'] = np.random.randint(-10, 10, x_shape)
return inputs_data
def create_unique_net(self, x_shape, data_type, out_idx):
tf.compat.v1.reset_default_graph()
# Create the graph and model
with tf.compat.v1.Session() as sess:
x = tf.compat.v1.placeholder(data_type, x_shape, 'x')
unique = tf.raw_ops.UniqueWithCounts(x=x, out_idx=out_idx)
tf.identity(unique[0], name='unique_elements')
tf.identity(unique[1], name='unique_indices')
tf.identity(unique[2], name='unique_counts')
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
return tf_net, None
test_data_basic = [
dict(x_shape=[1], data_type=tf.float32, out_idx=tf.int32),
dict(x_shape=[50], data_type=tf.float32, out_idx=tf.int32),
dict(x_shape=[100], data_type=tf.float32, out_idx=tf.int64),
]
@pytest.mark.parametrize("params", test_data_basic)
@pytest.mark.precommit_tf_fe
@pytest.mark.nightly
def test_unique_basic(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend, use_old_api):
if not use_new_frontend:
pytest.skip("Unique operation is not supported via legacy frontend.")
self._test(*self.create_unique_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend, use_old_api=use_old_api)