Skip to content

Commit

Permalink
[TF FE] Feat: implement complex type support for select (#28677)
Browse files Browse the repository at this point in the history
**Overview:**
- This pull request fixes #23243.

**Testing:**
- Tested the implementation & verified the results.

![Screenshot from 2025-01-25
14-21-47](https://github.com/user-attachments/assets/0cab7b27-727e-4085-8719-fda318eade16)

- No dependencies on other pull requests.
- TODO: Improve the test cases.

**CC:**
- @rkazants

---------

Signed-off-by: 11happy <soni5happy@gmail.com>
Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com>
  • Loading branch information
11happy and rkazants authored Jan 25, 2025
1 parent 33a616d commit 6929f7e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/frontends/tensorflow_common/src/op/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "openvino/op/select.hpp"

#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"
Expand Down Expand Up @@ -54,10 +55,25 @@ OutputVector translate_select_op(const NodeContext& node) {
// 1. Either the same shape (in which case the select is elementwise), or
// 2. condition must be Rank 1 and match over the first dimension, or
// 3. condition is scalar
default_op_checks(node, 3, {"Select", "SELECT"});
default_op_checks(node, 3, {"Select", "SELECT"}, true);
auto condition = node.get_input(0);
auto x = node.get_input(1);
auto y = node.get_input(2);
auto complex_type_mark_x = as_type_ptr<ComplexTypeMark>(x.get_node_shared_ptr());
auto complex_type_mark_y = as_type_ptr<ComplexTypeMark>(y.get_node_shared_ptr());

auto is_complex = (complex_type_mark_x || complex_type_mark_y);
element::Type complex_part_type;

if (complex_type_mark_x) {
x = complex_type_mark_x->input_value(0);
complex_part_type = complex_type_mark_x->get_complex_part_type();
}

if (complex_type_mark_y) {
y = complex_type_mark_y->input_value(0);
complex_part_type = complex_type_mark_y->get_complex_part_type();
}

// compute number of dimensions to unsqueeze the condition
auto cond_rank = compute_subgraph_scalar_rank(condition, element::i32);
Expand All @@ -78,7 +94,14 @@ OutputVector translate_select_op(const NodeContext& node) {
auto const_0 = make_shared<v0::Constant>(element::i32, Shape{1}, 0);
prep_cond = make_shared<v0::Squeeze>(prep_cond, const_0);

return translate_select_base_op(node, prep_cond, x, y);
auto result = translate_select_base_op(node, prep_cond, x, y);
if (is_complex) {
auto complex_result = make_shared<ComplexTypeMark>(result[0].get_node_shared_ptr(), complex_part_type);
return {complex_result->output(0)};

} else {
return result;
}
}
} // namespace op
} // namespace tensorflow
Expand Down
51 changes: 51 additions & 0 deletions tests/layer_tests/tensorflow_tests/test_tf_Select.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,54 @@ def test_select_basic(self, params, ie_device, precision, ir_version, temp_dir,
self._test(*self.create_select_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)

class TestComplexSelect(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
rng = np.random.default_rng()
assert 'cond:0' in inputs_info, "Test error: inputs_info must contain `cond`"
assert 'x_real:0' in inputs_info, "Test error: inputs_info must contain `x_real`"
assert 'x_imag:0' in inputs_info, "Test error: inputs_info must contain `x_imag`"
assert 'y_real:0' in inputs_info, "Test error: inputs_info must contain `y_real`"
assert 'y_imag:0' in inputs_info, "Test error: inputs_info must contain `y_imag`"
cond_shape = inputs_info['cond:0']
inputs_data = {}
inputs_data['cond:0'] = np.random.randint(0, 2, cond_shape).astype(bool)
for part in ['x_real:0', 'x_imag:0', 'y_real:0', 'y_imag:0']:
inputs_data[part] = 4 * rng.random(inputs_info[part]).astype(np.float32) - 2
return inputs_data

def create_complex_select_net(self, cond_shape, x_shape, y_shape):
tf.compat.v1.reset_default_graph()
# Create the graph and model
with tf.compat.v1.Session() as sess:
cond = tf.compat.v1.placeholder(tf.bool, cond_shape, 'cond')
x_real = tf.compat.v1.placeholder(tf.float32, x_shape, 'x_real')
x_imag = tf.compat.v1.placeholder(tf.float32, x_shape, 'x_imag')
y_real = tf.compat.v1.placeholder(tf.float32, y_shape, 'y_real')
y_imag = tf.compat.v1.placeholder(tf.float32, y_shape, 'y_imag')
complex_x = tf.raw_ops.Complex(real=x_real, imag=x_imag)
complex_y = tf.raw_ops.Complex(real=y_real, imag=y_imag)
complex_select = tf.raw_ops.Select(condition=cond, x=complex_x, y=complex_y)
tf.raw_ops.Real(input=complex_select)
tf.raw_ops.Imag(input=complex_select)
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
return tf_net, None

test_data_basic = [
dict(cond_shape=[], x_shape=[], y_shape=[]),
dict(cond_shape=[], x_shape=[3, 2, 4], y_shape=[3, 2, 4]),
dict(cond_shape=[2, 3, 4], x_shape=[2, 3, 4], y_shape=[2, 3, 4]),
]

@pytest.mark.parametrize("params", test_data_basic)
@pytest.mark.precommit
@pytest.mark.nightly

def test_complex_select(self, params, ie_device, precision, ir_version, temp_dir,
use_legacy_frontend):
if use_legacy_frontend:
pytest.skip("Select tests are not passing for the legacy frontend.")
self._test(*self.create_complex_select_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)

0 comments on commit 6929f7e

Please sign in to comment.