Skip to content

Commit

Permalink
[TF FE] Support complex type for Inv (openvinotoolkit#23085)
Browse files Browse the repository at this point in the history
**Ticket:** openvinotoolkit#22952

---------

Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com>
  • Loading branch information
2 people authored and Pranshu-S committed Mar 7, 2024
1 parent 939c74d commit 3b150f8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
40 changes: 38 additions & 2 deletions src/frontends/tensorflow_common/src/op/inv.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// Copyright (C) 2018-2023 Intel Corporation
// Copyright (C) 2018-2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "common_op_table.hpp"
#include "helper_ops/complex_type_mark.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/divide.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/negative.hpp"
#include "openvino/op/unsqueeze.hpp"

using namespace std;
using namespace ov::op;
Expand All @@ -14,9 +21,38 @@ namespace frontend {
namespace tensorflow {
namespace op {
OutputVector translate_inv_op(const NodeContext& node) {
default_op_checks(node, 1, {"Inv"});
default_op_checks(node, 1, {"Inv"}, true);
auto x = node.get_input(0);

auto complex_type_mark = as_type_ptr<ComplexTypeMark>(x.get_node_shared_ptr());
if (complex_type_mark) {
x = complex_type_mark->input_value(0);
element::Type complex_part_type = complex_type_mark->get_complex_part_type();

auto gather_index_real = make_shared<v0::Constant>(element::i32, Shape{}, 0);
auto gather_index_imag = make_shared<v0::Constant>(element::i32, Shape{}, 1);

auto minus_one = make_shared<v0::Constant>(element::i32, Shape{1}, -1);

auto x_real = make_shared<v8::Gather>(x, gather_index_real, minus_one)->output(0);
auto x_imag = make_shared<v8::Gather>(x, gather_index_imag, minus_one)->output(0);

auto scale =
make_shared<v1::Add>(make_shared<v1::Multiply>(x_real, x_real), make_shared<v1::Multiply>(x_imag, x_imag));

auto y_real = make_shared<v1::Divide>(x_real, scale);
auto y_imag = make_shared<v1::Divide>(make_shared<v0::Negative>(x_imag), scale);

auto real_unsqueeze = make_shared<v0::Unsqueeze>(y_real, minus_one);
auto imag_unsqueeze = make_shared<v0::Unsqueeze>(y_imag, minus_one);

auto concat_result = make_shared<v0::Concat>(OutputVector{real_unsqueeze, imag_unsqueeze}, -1);
set_node_name(node.get_name(), concat_result);

auto complex_result = make_shared<ComplexTypeMark>(concat_result->output(0), complex_part_type);
return {complex_result};
}

// prepare auxiliary one constants of the same type as the inputs
auto one = create_same_type_const_scalar<int32_t>(x, 1);

Expand Down
46 changes: 45 additions & 1 deletion tests/layer_tests/tensorflow_tests/test_tf_Inv.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,48 @@ def test_inv_basic(self, params, ie_device, precision, ir_version, temp_dir,
use_legacy_frontend):
self._test(*self.create_inv_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)
use_legacy_frontend=use_legacy_frontend)

class TestComplexInv(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
rng = np.random.default_rng()
assert 'param_real:0' in inputs_info
assert 'param_imag:0' in inputs_info
param_real_shape_1 = inputs_info['param_real:0']
param_imag_shape_1 = inputs_info['param_imag:0']
inputs_data = {}
inputs_data['param_real:0'] = 4 * rng.random(param_real_shape_1).astype(np.float32) - 2
inputs_data['param_imag:0'] = 4 * rng.random(param_imag_shape_1).astype(np.float32) - 2
return inputs_data

def create_complex_inv_net(self, input_shape):
tf.compat.v1.reset_default_graph()
# Create the graph and model
with tf.compat.v1.Session() as sess:
param_real = tf.compat.v1.placeholder(np.float32, input_shape, 'param_real')
param_imag = tf.compat.v1.placeholder(np.float32, input_shape, 'param_imag')
complex = tf.raw_ops.Complex(real=param_real, imag=param_imag)
inv = tf.raw_ops.Inv(x=complex, name="complex_inv")
real = tf.raw_ops.Real(input=inv)
img = tf.raw_ops.Imag(input=inv)
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def

return tf_net, None

test_data_basic = [
dict(input_shape=[]),
dict(input_shape=[2]),
dict(input_shape=[1, 3]),
dict(input_shape=[2, 3, 4]),
dict(input_shape=[3, 4, 5, 6]),
]
@pytest.mark.parametrize("params", test_data_basic)
@pytest.mark.precommit_tf_fe
@pytest.mark.nightly
def test_complex_inv(self, params, ie_device, precision, ir_version, temp_dir,
use_legacy_frontend):
self._test(
*self.create_complex_inv_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)

0 comments on commit 3b150f8

Please sign in to comment.