Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF FE] Support complex type for Inv #23085

Merged
merged 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/frontends/tensorflow_common/src/op/inv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
//
rkazants marked this conversation as resolved.
Show resolved Hide resolved

#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
47 changes: 46 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,49 @@ 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' in inputs_info
assert 'param_imag' in inputs_info
param_real_shape_1 = inputs_info['param_real']
param_imag_shape_1 = inputs_info['param_imag']
inputs_data = {}
inputs_data['param_real'] = 4 * rng.random(param_real_shape_1).astype(np.float32) - 2
inputs_data['param_imag'] = 4 * rng.random(param_imag_shape_1).astype(np.float32) - 2
return inputs_data

def create_complex_inv_net(self, input_shape):
import tensorflow as tf
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)
Loading