Skip to content

Commit

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

### Tickets:
 - [None](#22950)
  • Loading branch information
hub-bla authored Oct 3, 2024
1 parent 4254c13 commit 2fc0fae
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/frontends/tensorflow_common/src/op/expand_dims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
//

#include "common_op_table.hpp"
#include "helper_ops/complex_type_mark.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/less.hpp"
#include "openvino/op/select.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "utils.hpp"

using namespace std;
using namespace ov::op;
Expand All @@ -14,9 +20,31 @@ namespace tensorflow {
namespace op {

OutputVector translate_expand_dims_op(const NodeContext& node) {
default_op_checks(node, 2, {"ExpandDims", "EXPAND_DIMS"});
default_op_checks(node, 2, {"ExpandDims", "EXPAND_DIMS"}, true);
auto input = node.get_input(0);
auto axis = node.get_input(1);
auto complex_type_mark = as_type_ptr<ComplexTypeMark>(input.get_node_shared_ptr());

if (complex_type_mark) {
element::Type complex_part_type = complex_type_mark->get_complex_part_type();
input = complex_type_mark->input_value(0);

auto const_zero = create_same_type_const_scalar<int32_t>(axis, 0);

auto is_axis_neg = make_shared<v1::Less>(axis, const_zero);

auto const_one = create_same_type_const_scalar<int32_t>(axis, 1);
auto axis_min_one = make_shared<v1::Subtract>(axis, const_one);

auto new_axis = make_shared<v1::Select>(is_axis_neg, axis_min_one, axis);

auto unsqueeze = make_shared<v0::Unsqueeze>(input, new_axis);

set_node_name(node.get_name(), unsqueeze);
auto complex_result = make_shared<ComplexTypeMark>(unsqueeze, complex_part_type);
return {complex_result};
}

auto unsqueeze = make_shared<v0::Unsqueeze>(input, axis);
set_node_name(node.get_name(), unsqueeze);
return {unsqueeze};
Expand Down
52 changes: 52 additions & 0 deletions tests/layer_tests/tensorflow_tests/test_tf_ExpandDims.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tensorflow as tf
from common.tf_layer_test_class import CommonTFLayerTest

rng = np.random.default_rng(62362)

class TestExpandDims(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
Expand Down Expand Up @@ -40,3 +41,54 @@ def test_expand_dims_basic(self, params, ie_device, precision, ir_version, temp_
self._test(*self.create_expand_dims_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)


class TestExpandDimsComplex(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
# generate elements so that the input tensor may contain repeating elements
assert 'param_real:0' in inputs_info
assert 'param_imag:0' in inputs_info

input_shape = inputs_info['param_real:0']

inputs_data = {}
inputs_data['param_real:0'] = rng.integers(-10.0, 10.0, input_shape).astype(np.float32)
inputs_data['param_imag:0'] = rng.integers(-10.0, 10.0, input_shape).astype(np.float32)

return inputs_data

def create_expand_dims_complex_net(self, axis_dtype, input_shape, axis):
tf.compat.v1.reset_default_graph()
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)

axis = tf.constant(axis, dtype=axis_dtype)

result = tf.raw_ops.ExpandDims(input=complex, axis=axis)

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

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

return tf_net, None

test_basic = [
dict(input_shape=[], axis=0),
dict(input_shape=[2, 3], axis=1),
dict(input_shape=[2, 3, 4], axis=-1),
dict(input_shape=[2, 6, 5], axis=-2),
]

@pytest.mark.parametrize("axis_dtype", [np.int32, np.int64])
@pytest.mark.parametrize("op_args", test_basic)
@pytest.mark.nightly
@pytest.mark.precommit
def test_expand_dims_basic_complex(self, axis_dtype, op_args, ie_device, precision, ir_version, temp_dir, use_legacy_frontend):
self._test(*self.create_expand_dims_complex_net(axis_dtype, **op_args),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)

0 comments on commit 2fc0fae

Please sign in to comment.