Skip to content

Commit

Permalink
[TF FE]: Support complex tensors for Sub operation (#26342)
Browse files Browse the repository at this point in the history
### Details:
 - Support complex tensors for Sub operation + tests

### Tickets:
 - [None](#22948)
  • Loading branch information
hub-bla authored Sep 1, 2024
1 parent 3983c35 commit a6d92e3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/frontends/tensorflow/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
#include "openvino/op/softplus.hpp"
#include "openvino/op/softsign.hpp"
#include "openvino/op/squared_difference.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/swish.hpp"
#include "openvino/op/tan.hpp"
#include "openvino/op/tanh.hpp"
Expand Down Expand Up @@ -195,7 +194,6 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"Pow", CreatorFunction(translate_binary_op<v1::Power>)},
{"RealDiv", CreatorFunction(translate_binary_op<v1::Divide>)},
{"SquaredDifference", CreatorFunction(translate_binary_op<v0::SquaredDifference>)},
{"Sub", CreatorFunction(translate_binary_op<v1::Subtract>)},

// note: ReduceOp translator declaration for each op must to be added in reduce.cpp file
{"Any", CreatorFunction(translate_direct_reduce_op<v1::ReduceLogicalOr>)},
Expand Down Expand Up @@ -396,6 +394,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"StatelessIf", CreatorFunction(translate_if_op)},
{"StatelessWhile", CreatorFunction(translate_while_op)},
{"StridedSlice", CreatorFunction(translate_strided_slice_op)},
{"Sub", CreatorFunction(translate_sub_op)},
{"Switch", CreatorFunction(translate_switch_op)},
{"TensorArrayCloseV3", CreatorFunction(translate_tensor_array_close_v3_op)},
{"TensorArrayConcatV3", CreatorFunction(translate_tensor_array_concat_v3_op)},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ OP_CONVERTER(translate_split_v_op);
OP_CONVERTER(translate_square_op);
OP_CONVERTER(translate_squeeze_op);
OP_CONVERTER(translate_strided_slice_op);
OP_CONVERTER(translate_sub_op);
OP_CONVERTER(translate_sqrt_op);
OP_CONVERTER(translate_empty_tensor_list_op);
OP_CONVERTER(translate_tensor_list_from_tensor_op);
Expand Down
27 changes: 27 additions & 0 deletions src/frontends/tensorflow_common/src/op/binary_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,33 @@ OutputVector translate_addv2_op(const NodeContext& node) {
return {result};
}

OutputVector translate_sub_op(const NodeContext& node) {
default_op_checks(node, 2, {"Sub"}, true);
auto lhs = node.get_input(0);
auto rhs = node.get_input(1);

auto complex_type_mark_lhs = as_type_ptr<ComplexTypeMark>(lhs.get_node_shared_ptr());
auto complex_type_mark_rhs = as_type_ptr<ComplexTypeMark>(rhs.get_node_shared_ptr());
auto complex_type_inputs = (complex_type_mark_lhs && complex_type_mark_rhs);

if (complex_type_inputs) {
lhs = complex_type_mark_lhs->input_value(0);
rhs = complex_type_mark_rhs->input_value(0);
}

// performing an actual operation
auto result = make_shared<v1::Subtract>(lhs, rhs);

if (complex_type_inputs) {
auto complex_result = make_shared<ComplexTypeMark>(result, complex_type_mark_lhs->get_complex_part_type());
set_node_name(node.get_name(), result);

return {complex_result};
}
set_node_name(node.get_name(), result);
return {result};
}

template OutputVector translate_binary_op<v1::Add>(const NodeContext& node);
template OutputVector translate_binary_op<v13::BitwiseAnd>(const NodeContext& node);
template OutputVector translate_binary_op<v13::BitwiseOr>(const NodeContext& node);
Expand Down
67 changes: 67 additions & 0 deletions tests/layer_tests/tensorflow_tests/test_tf_Sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,70 @@ def test_sub_placeholder_const_broadcast_5D(self, params, ie_device, precision,
use_legacy_frontend=use_legacy_frontend),
ie_device, precision, ir_version,
temp_dir=temp_dir, use_legacy_frontend=use_legacy_frontend)


class TestComplexSub(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
rng = np.random.default_rng(84821)

assert 'param_real_x:0' in inputs_info
assert 'param_imag_x:0' in inputs_info

assert 'param_real_y:0' in inputs_info
assert 'param_imag_y:0' in inputs_info

param_real_shape_x = inputs_info['param_real_x:0']
param_imag_shape_x = inputs_info['param_imag_x:0']

param_real_shape_y = inputs_info['param_real_y:0']
param_imag_shape_y = inputs_info['param_imag_y:0']

inputs_data = {}
inputs_data['param_real_x:0'] = rng.uniform(-10.0, 10.0, param_real_shape_x).astype(np.float32)
inputs_data['param_imag_x:0'] = rng.uniform(-10.0, 10.0, param_imag_shape_x).astype(np.float32)

inputs_data['param_real_y:0'] = rng.uniform(-10.0, 10.0, param_real_shape_y).astype(np.float32)
inputs_data['param_imag_y:0'] = rng.uniform(-10.0, 10.0, param_imag_shape_y).astype(np.float32)

return inputs_data

def create_complex_sub_net(self, x_shape, y_shape, ir_version, use_legacy_frontend):
import tensorflow as tf

tf.compat.v1.reset_default_graph()
with tf.compat.v1.Session() as sess:
param_real_x = tf.compat.v1.placeholder(np.float32, x_shape, 'param_real_x')
param_imag_x = tf.compat.v1.placeholder(np.float32, x_shape, 'param_imag_x')

param_real_y = tf.compat.v1.placeholder(np.float32, y_shape, 'param_real_y')
param_imag_y = tf.compat.v1.placeholder(np.float32, y_shape, 'param_imag_y')

x = tf.raw_ops.Complex(real=param_real_x, imag=param_imag_x)
y = tf.raw_ops.Complex(real=param_real_y, imag=param_imag_y)

result = tf.raw_ops.Sub(x=x, y=y, name='Sub')

tf.raw_ops.Real(input=result)
tf.raw_ops.Imag(input=result)

tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def

ref_net = None

return tf_net, ref_net

@pytest.mark.parametrize('x_shape, y_shape', [
[[5, 5], [5]],
[[4, 10], [4, 1]],
[[1, 3, 50, 224], [1]],
[[10, 10, 10], [10, 10, 10]],
])
@pytest.mark.precommit
@pytest.mark.nightly
def test_complex_sub(self, x_shape, y_shape,
ie_device, precision, ir_version, temp_dir, use_legacy_frontend):
self._test(*self.create_complex_sub_net(x_shape, y_shape, ir_version=ir_version,
use_legacy_frontend=use_legacy_frontend),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)

0 comments on commit a6d92e3

Please sign in to comment.