From 98a20998535641dd87aebb475476bdf6d50db71f Mon Sep 17 00:00:00 2001 From: MonalSD Date: Wed, 13 Mar 2024 23:10:31 +0530 Subject: [PATCH 1/7] Support complex tensors for OnesLike operation --- .../tensorflow_common/src/op/ones_like.cpp | 15 ++++++- .../tensorflow_tests/test_tf_OnesLike.py | 44 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/frontends/tensorflow_common/src/op/ones_like.cpp b/src/frontends/tensorflow_common/src/op/ones_like.cpp index 2084f3db1919cd..c5106764503259 100644 --- a/src/frontends/tensorflow_common/src/op/ones_like.cpp +++ b/src/frontends/tensorflow_common/src/op/ones_like.cpp @@ -21,6 +21,11 @@ namespace op { OutputVector translate_ones_like_op(const NodeContext& node) { default_op_checks(node, 1, {"OnesLike"}); auto x = node.get_input(0); + auto complex_type_mark_x = as_type_ptr(x.get_node_shared_ptr()); + if (complex_type_mark_x) { + x = complex_type_mark_x->input_value(0); + } + Output shape_of = make_shared(x, element::i32); auto one_const = create_same_type_const_scalar(x, 1); @@ -35,11 +40,17 @@ OutputVector translate_ones_like_op(const NodeContext& node) { // remove extra dimension by squeezing auto zero_dim_ind = make_shared(element::i32, Shape{1}, 0); ones_like = make_shared(ones_like, zero_dim_ind); - set_node_name(node.get_name(), ones_like.get_node_shared_ptr()); + + if (complex_type_mark_x) { + auto complex_ones_like = make_shared(ones_like, complex_type_mark_x->get_complex_part_type()); + return { + complex_ones_like + } + } + return {ones_like}; } - } // namespace op } // namespace tensorflow } // namespace frontend diff --git a/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py b/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py index 0b29db87e2a3b0..000e50925ae238 100644 --- a/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py +++ b/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py @@ -43,3 +43,47 @@ def test_ones_like(self, params, ie_device, precision, ir_version, temp_dir, self._test(*self.create_ones_like_net(**params), ie_device, precision, ir_version, temp_dir=temp_dir, use_legacy_frontend=use_legacy_frontend) + +class TestComplexOnesLike(CommonTFLayerTest): + def _prepare_input(self, inputs_info): + rng = np.random.default_rng() + assert 'x_real:0' in inputs_info + assert 'x_imag:0' in inputs_info + x_real_shape = inputs_info['x_real:0'] + x_imag_shape = inputs_info['x_imag:0'] + inputs_data = {} + inputs_data['x_real:0'] = 4 * rng.random(x_real_shape).astype(self.x_type) - 2 + inputs_data['x_imag:0'] = 4 * rng.random(x_imag_shape).astype(self.x_type) - 2 + return inputs_data + + def create_complex_ones_like_net(self, x_shape, x_type): + self.x_type = x_type + tf.compat.v1.reset_default_graph() + # Create the graph and model + with tf.compat.v1.Session() as sess: + x_real = tf.compat.v1.placeholder(tf.dtypes.as_dtype(x_type), x_shape, 'x_real') + x_imag = tf.compat.v1.placeholder(tf.dtypes.as_dtype(x_type), x_shape, 'x_imag') + x_complex = tf.raw_ops.Complex(real=x_real, imag=x_imag) + ones_like = tf.raw_ops.OnesLike(x=x_complex) + real = tf.raw_ops.Real(input=ones_like) + img = tf.raw_ops.Imag(input=ones_like) + tf.compat.v1.global_variables_initializer() + tf_net = sess.graph_def + + return tf_net, None + + test_data_basic = [ + dict(x_shape=[], x_type=np.float32), + dict(x_shape=[2], x_type=np.int32), + dict(x_shape=[2, 3, 4], x_type=np.float32), + dict(x_shape=[1, 4, 3, 1], x_type=np.int32), + ] + + @pytest.mark.parametrize("params", test_data_basic) + @pytest.mark.precommit_tf_fe + @pytest.mark.nightly + def test_complex_ones_like(self, params, ie_device, precision, ir_version, temp_dir, + use_legacy_frontend): + self._test(*self.create_complex_ones_like_net(**params), + ie_device, precision, ir_version, temp_dir=temp_dir, + use_legacy_frontend=use_legacy_frontend) From 84ea2531ddfbf9a6767fdedc7475fa058445629c Mon Sep 17 00:00:00 2001 From: MonalSD Date: Fri, 15 Mar 2024 00:32:44 +0530 Subject: [PATCH 2/7] Fixed logic for onesLike complex --- .../tensorflow_common/src/op/ones_like.cpp | 62 ++++++++++++------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/src/frontends/tensorflow_common/src/op/ones_like.cpp b/src/frontends/tensorflow_common/src/op/ones_like.cpp index 112e58d4d4040a..94b7bd90ef3f45 100644 --- a/src/frontends/tensorflow_common/src/op/ones_like.cpp +++ b/src/frontends/tensorflow_common/src/op/ones_like.cpp @@ -19,37 +19,55 @@ namespace tensorflow { namespace op { OutputVector translate_ones_like_op(const NodeContext& node) { - default_op_checks(node, 1, {"OnesLike"}); + default_op_checks(node, 1, {"OnesLike"}, true); auto x = node.get_input(0); auto complex_type_mark_x = as_type_ptr(x.get_node_shared_ptr()); if (complex_type_mark_x) { x = complex_type_mark_x->input_value(0); - } + auto gather_index_real = make_shared(element::i32, Shape{}, 0); + auto minus_one = make_shared(element::i32, Shape{1}, -1); + auto x_real = make_shared(x, gather_index_real, minus_one)->output(0); - Output shape_of = make_shared(x, element::i32); - auto one_const = create_same_type_const_scalar(x, 1); + Output shape_of_real = make_shared(x_real, element::i32); + auto one_const = create_same_type_const_scalar(x_real, 1); + auto one_dim = make_shared(element::i32, Shape{1}, 1); + shape_of_real = make_shared(OutputVector{one_dim, shape_of_real}, 0); + // create a tensor of zeros of shape with extra dimension + Output ones_like_one = make_shared(one_const, shape_of_real); + // remove extra dimension by squeezing + auto zero_dim_ind = make_shared(element::i32, Shape{1}, 0); + ones_like_one = make_shared(ones_like_one, zero_dim_ind); - // in case of x to be scalar, we need handle it more specifically - // since Broadcast supports only broadcasting to rank greater 0 - // we have to introduce extra dimension for input scalar case - auto one_dim = make_shared(element::i32, Shape{1}, 1); - shape_of = make_shared(OutputVector{one_dim, shape_of}, 0); + // For zeroes: + auto zero_const = create_same_type_const_scalar(x_real, 0); + // create a tensor of zeros of shape with extra dimension + Output ones_like_zero = make_shared(zero_const, shape_of_real); + // remove extra dimension by squeezing + ones_like_zero = make_shared(ones_like_zero, zero_dim_ind); - // create a tensor of zeros of shape with extra dimension - Output ones_like = make_shared(one_const, shape_of); - // remove extra dimension by squeezing - auto zero_dim_ind = make_shared(element::i32, Shape{1}, 0); - ones_like = make_shared(ones_like, zero_dim_ind); - set_node_name(node.get_name(), ones_like.get_node_shared_ptr()); + auto ones_like_output = make_shared(OutputVector{ones_like_one, ones_like_zero}, -1); + auto ones_like_output_complex = + make_shared(ones_like_output, ones_like_complex->get_complex_part_type()) + set_node_name(node.get_name(), ones_like_output.get_node_shared_ptr()); + return {ones_like_output_complex}; + } else { + Output shape_of = make_shared(x, element::i32); + auto one_const = create_same_type_const_scalar(x, 1); - if (complex_type_mark_x) { - auto complex_ones_like = make_shared(ones_like, complex_type_mark_x->get_complex_part_type()); - return { - complex_ones_like - } - } + // in case of x to be scalar, we need handle it more specifically + // since Broadcast supports only broadcasting to rank greater 0 + // we have to introduce extra dimension for input scalar case + auto one_dim = make_shared(element::i32, Shape{1}, 1); + shape_of = make_shared(OutputVector{one_dim, shape_of}, 0); - return {ones_like}; + // create a tensor of zeros of shape with extra dimension + Output ones_like = make_shared(one_const, shape_of); + // remove extra dimension by squeezing + auto zero_dim_ind = make_shared(element::i32, Shape{1}, 0); + ones_like = make_shared(ones_like, zero_dim_ind); + set_node_name(node.get_name(), ones_like.get_node_shared_ptr()); + return {ones_like}; + } } } // namespace op } // namespace tensorflow From 8191af0499905caff6ab2b4c65d50deaae9f7b84 Mon Sep 17 00:00:00 2001 From: MonalSD Date: Fri, 15 Mar 2024 19:02:11 +0530 Subject: [PATCH 3/7] Addressed changes in ones_like --- .../tensorflow_common/src/op/ones_like.cpp | 59 ++++++++----------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/src/frontends/tensorflow_common/src/op/ones_like.cpp b/src/frontends/tensorflow_common/src/op/ones_like.cpp index 94b7bd90ef3f45..074dfd457450c8 100644 --- a/src/frontends/tensorflow_common/src/op/ones_like.cpp +++ b/src/frontends/tensorflow_common/src/op/ones_like.cpp @@ -3,9 +3,11 @@ // #include "common_op_table.hpp" +#include "helper_ops/complex_type_mark.hpp" #include "openvino/op/broadcast.hpp" #include "openvino/op/concat.hpp" #include "openvino/op/constant.hpp" +#include "openvino/op/gather.hpp" #include "openvino/op/shape_of.hpp" #include "openvino/op/squeeze.hpp" #include "utils.hpp" @@ -24,50 +26,37 @@ OutputVector translate_ones_like_op(const NodeContext& node) { auto complex_type_mark_x = as_type_ptr(x.get_node_shared_ptr()); if (complex_type_mark_x) { x = complex_type_mark_x->input_value(0); - auto gather_index_real = make_shared(element::i32, Shape{}, 0); + auto gather_index_real = make_shared(element::i32, Shape{1}, 0); auto minus_one = make_shared(element::i32, Shape{1}, -1); auto x_real = make_shared(x, gather_index_real, minus_one)->output(0); - Output shape_of_real = make_shared(x_real, element::i32); + auto one_const = create_same_type_const_scalar(x_real, 1); - auto one_dim = make_shared(element::i32, Shape{1}, 1); - shape_of_real = make_shared(OutputVector{one_dim, shape_of_real}, 0); - // create a tensor of zeros of shape with extra dimension - Output ones_like_one = make_shared(one_const, shape_of_real); - // remove extra dimension by squeezing - auto zero_dim_ind = make_shared(element::i32, Shape{1}, 0); - ones_like_one = make_shared(ones_like_one, zero_dim_ind); + Output ones_like = make_shared(one_const, shape_of_real); - // For zeroes: auto zero_const = create_same_type_const_scalar(x_real, 0); - // create a tensor of zeros of shape with extra dimension - Output ones_like_zero = make_shared(zero_const, shape_of_real); - // remove extra dimension by squeezing - ones_like_zero = make_shared(ones_like_zero, zero_dim_ind); - - auto ones_like_output = make_shared(OutputVector{ones_like_one, ones_like_zero}, -1); - auto ones_like_output_complex = - make_shared(ones_like_output, ones_like_complex->get_complex_part_type()) - set_node_name(node.get_name(), ones_like_output.get_node_shared_ptr()); - return {ones_like_output_complex}; - } else { - Output shape_of = make_shared(x, element::i32); - auto one_const = create_same_type_const_scalar(x, 1); + Output zeros_like = make_shared(zero_const, shape_of_real); + auto result = make_shared(OutputVector{ones_like, zeros_like}, -1); + set_node_name(node.get_name(), result); - // in case of x to be scalar, we need handle it more specifically - // since Broadcast supports only broadcasting to rank greater 0 - // we have to introduce extra dimension for input scalar case - auto one_dim = make_shared(element::i32, Shape{1}, 1); - shape_of = make_shared(OutputVector{one_dim, shape_of}, 0); + auto ones_like_complex = make_shared(result, complex_type_mark_x->get_complex_part_type()); - // create a tensor of zeros of shape with extra dimension - Output ones_like = make_shared(one_const, shape_of); - // remove extra dimension by squeezing - auto zero_dim_ind = make_shared(element::i32, Shape{1}, 0); - ones_like = make_shared(ones_like, zero_dim_ind); - set_node_name(node.get_name(), ones_like.get_node_shared_ptr()); - return {ones_like}; + return {ones_like_complex}; } + + Output shape_of = make_shared(x, element::i32); + auto one_const = create_same_type_const_scalar(x, 1); + + // in case of x to be scalar, we need handle it more specifically + // since Broadcast supports only broadcasting to rank greater 0 + // we have to introduce extra dimension for input scalar case + auto one_dim = make_shared(element::i32, Shape{1}, 1); + shape_of = make_shared(OutputVector{one_dim, shape_of}, 0); + + // create a tensor of zeros of shape with extra dimension + Output ones_like = make_shared(one_const, shape_of); + set_node_name(node.get_name(), ones_like.get_node_shared_ptr()); + return {ones_like}; } } // namespace op } // namespace tensorflow From 3b258d61accea4a9b6bad683b8b748a6fdc95285 Mon Sep 17 00:00:00 2001 From: Roman Kazantsev Date: Tue, 19 Mar 2024 13:41:53 +0400 Subject: [PATCH 4/7] Update src/frontends/tensorflow_common/src/op/ones_like.cpp --- src/frontends/tensorflow_common/src/op/ones_like.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/frontends/tensorflow_common/src/op/ones_like.cpp b/src/frontends/tensorflow_common/src/op/ones_like.cpp index 074dfd457450c8..b2cf12e37876e8 100644 --- a/src/frontends/tensorflow_common/src/op/ones_like.cpp +++ b/src/frontends/tensorflow_common/src/op/ones_like.cpp @@ -38,7 +38,6 @@ OutputVector translate_ones_like_op(const NodeContext& node) { Output zeros_like = make_shared(zero_const, shape_of_real); auto result = make_shared(OutputVector{ones_like, zeros_like}, -1); set_node_name(node.get_name(), result); - auto ones_like_complex = make_shared(result, complex_type_mark_x->get_complex_part_type()); return {ones_like_complex}; From d8eebed1ab09995ea7a8c31e8e5e005b9ca6f6c2 Mon Sep 17 00:00:00 2001 From: Roman Kazantsev Date: Tue, 19 Mar 2024 14:28:31 +0400 Subject: [PATCH 5/7] Update tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py --- tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py b/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py index ac87bcc2ba40f8..a25494f2a82ec7 100644 --- a/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py +++ b/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py @@ -74,9 +74,9 @@ def create_complex_ones_like_net(self, x_shape, x_type): test_data_basic = [ dict(x_shape=[], x_type=np.float32), - dict(x_shape=[2], x_type=np.int32), + dict(x_shape=[2], x_type=np.float32), dict(x_shape=[2, 3, 4], x_type=np.float32), - dict(x_shape=[1, 4, 3, 1], x_type=np.int32), + dict(x_shape=[1, 4, 3, 1], x_type=np.float32), ] @pytest.mark.parametrize("params", test_data_basic) From b41e972de50aa23e939195191e392aa8eae26409 Mon Sep 17 00:00:00 2001 From: MonalSD Date: Wed, 20 Mar 2024 15:12:15 +0530 Subject: [PATCH 6/7] Changes --- src/frontends/tensorflow_common/src/op/ones_like.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/frontends/tensorflow_common/src/op/ones_like.cpp b/src/frontends/tensorflow_common/src/op/ones_like.cpp index b2cf12e37876e8..6003f26ca3e34c 100644 --- a/src/frontends/tensorflow_common/src/op/ones_like.cpp +++ b/src/frontends/tensorflow_common/src/op/ones_like.cpp @@ -54,6 +54,9 @@ OutputVector translate_ones_like_op(const NodeContext& node) { // create a tensor of zeros of shape with extra dimension Output ones_like = make_shared(one_const, shape_of); + // remove extra dimension by squeezing + auto zero_dim_ind = make_shared(element::i32, Shape{1}, 0); + ones_like = make_shared(ones_like, zero_dim_ind); set_node_name(node.get_name(), ones_like.get_node_shared_ptr()); return {ones_like}; } From 44c36a8ea9f82595e7034a0bc16a0d45aaae7acd Mon Sep 17 00:00:00 2001 From: Roman Kazantsev Date: Thu, 21 Mar 2024 12:51:45 +0400 Subject: [PATCH 7/7] Update tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py --- tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py b/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py index a25494f2a82ec7..0da2822155c8d9 100644 --- a/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py +++ b/tests/layer_tests/tensorflow_tests/test_tf_OnesLike.py @@ -44,6 +44,7 @@ def test_ones_like(self, params, ie_device, precision, ir_version, temp_dir, ie_device, precision, ir_version, temp_dir=temp_dir, use_legacy_frontend=use_legacy_frontend) + class TestComplexOnesLike(CommonTFLayerTest): def _prepare_input(self, inputs_info): rng = np.random.default_rng()