diff --git a/src/frontends/tensorflow_common/src/op/list_diff.cpp b/src/frontends/tensorflow_common/src/op/list_diff.cpp index 1a321aea754..c433b884650 100644 --- a/src/frontends/tensorflow_common/src/op/list_diff.cpp +++ b/src/frontends/tensorflow_common/src/op/list_diff.cpp @@ -6,6 +6,7 @@ #include "openvino/opsets/opset8.hpp" using namespace std; +using namespace ov; using namespace ov::opset8; namespace ov { @@ -19,35 +20,35 @@ OutputVector translate_list_diff_op(const NodeContext& node) { auto y = node.get_input(1); // retrieve attribute - auto out_idx = node.get_attribute("out_idx", ov::element::i32); + auto out_idx = node.get_attribute("out_idx", element::i32); // unsqueeze both operand to make comparison elements between each other - auto unsqueeze_x = - make_shared(x, make_shared(ov::element::i64, ov::Shape{}, std::vector{1})); - auto unsqueeze_y = - make_shared(y, make_shared(ov::element::i64, ov::Shape{}, std::vector{0})); + auto const_zero = make_shared(element::i32, Shape{1}, 0); + auto const_one = make_shared(element::i32, Shape{1}, 1); + auto unsqueeze_x = make_shared(x, const_one); + auto unsqueeze_y = make_shared(y, const_zero); // generate a mask where elements x and y are different - auto x_is_non_equal_y = - make_shared(make_shared(unsqueeze_x, unsqueeze_y), - make_shared(ov::element::i64, ov::Shape{}, std::vector{1}), - false); - auto mask01_x_is_non_equal_y = - make_shared(mask_01, const_zero, const_one); // compute indices of x elements different from elements of y - auto diff_output_indices = - make_shared(make_shared(mask01_x_is_non_equal_y, out_idx), - make_shared(ov::element::i64, ov::Shape{1}, std::vector{-1}), - false); + // compute indices of elements in x that are absent in y + Output idx = make_shared(mask_01, out_idx); + auto new_shape = make_shared(element::i32, Shape{1}, -1); + idx = make_shared(idx, new_shape, false); - // gather elements from x that occur in y - auto diff_x = make_shared(x, - diff_output_indices, - make_shared(ov::element::i64, ov::Shape{}, std::vector{0})); - return {diff_x, diff_output_indices}; + // gather elements from x that are absent in y + auto out = make_shared(x, idx, const_zero); + + // set tensor names + set_out_name({node.get_name() + ":0"}, out); + set_out_name({node.get_name() + ":1"}, idx); + return {out, idx}; } } // namespace op } // namespace tensorflow diff --git a/tests/layer_tests/tensorflow_tests/test_tf_ListDiff.py b/tests/layer_tests/tensorflow_tests/test_tf_ListDiff.py new file mode 100644 index 00000000000..ff9389db41b --- /dev/null +++ b/tests/layer_tests/tensorflow_tests/test_tf_ListDiff.py @@ -0,0 +1,48 @@ +# 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 TestListDiff(CommonTFLayerTest): + def _prepare_input(self, inputs_info): + assert 'x' in inputs_info, "Test error: inputs_info must contain `x`" + assert 'y' in inputs_info, "Test error: inputs_info must contain `y`" + x_shape = inputs_info['x'] + y_shape = inputs_info['y'] + inputs_data = {} + inputs_data['x'] = np.random.randint(-5, 5, x_shape) + inputs_data['y'] = np.random.randint(-5, 5, y_shape) + return inputs_data + + def create_list_diff_net(self, x_shape, y_shape, 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(tf.int32, x_shape, 'x') + y = tf.compat.v1.placeholder(tf.int32, y_shape, 'y') + listdiff = tf.raw_ops.ListDiff(x=x, y=y, out_idx=out_idx) + tf.identity(listdiff[0], name='out') + tf.identity(listdiff[1], name='idx') + tf.compat.v1.global_variables_initializer() + + tf_net = sess.graph_def + + return tf_net, None + + test_data_basic = [ + dict(x_shape=[7], y_shape=[2], out_idx=tf.int32), + dict(x_shape=[4], y_shape=[8], out_idx=tf.int64), + ] + + @pytest.mark.parametrize("params", test_data_basic) + @pytest.mark.precommit_tf_fe + @pytest.mark.nightly + def test_list_diff_basic(self, params, ie_device, precision, ir_version, temp_dir, + use_new_frontend, use_old_api): + self._test(*self.create_list_diff_net(**params), + ie_device, precision, ir_version, temp_dir=temp_dir, + use_new_frontend=use_new_frontend, use_old_api=use_old_api)