OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Good First Issue][TF FE]: Support complex tensors for Pack operation (#25193)
Hiba Hassan3983c35c0b5d
diff --git a/src/frontends/tensorflow_common/src/op/pack.cpp b/src/frontends/tensorflow_common/src/op/pack.cppindex a6d872fce91..8f1bf3309df 100644--- a/src/frontends/tensorflow_common/src/op/pack.cpp+++ b/src/frontends/tensorflow_common/src/op/pack.cpp@@ -3,8 +3,10 @@//#include "common_op_table.hpp"+#include "helper_ops/complex_type_mark.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/constant.hpp"+#include "openvino/op/shape_of.hpp"#include "openvino/op/unsqueeze.hpp"using namespace std;@@ -16,20 +18,40 @@ namespace tensorflow {namespace op {OutputVector translate_pack_op(const NodeContext& node) {- default_op_checks(node, 1, {"Pack", "PACK"});+ default_op_checks(node, 1, {"Pack", "PACK"}, true);auto num_size = static_cast<int>(node.get_input_size());auto axis = node.get_attribute<int64_t>("axis", 0);+ if (axis < 0 && as_type_ptr<ComplexTypeMark>(node.get_input(0).get_node_shared_ptr())) {+ // need to account auxiliary dimension for real and imaginary parts+ axis -= 1;+ }+auto axis_const = make_shared<v0::Constant>(element::i64, Shape{}, axis);OutputVector concat_inputs;+ bool has_complex_input = false;+ element::Type complex_part_type;+for (int ind = 0; ind < num_size; ++ind) {auto in = node.get_input(ind);+ auto complex_type_mark = as_type_ptr<ComplexTypeMark>(in.get_node_shared_ptr());+ if (complex_type_mark) {+ has_complex_input = true;+ complex_part_type = complex_type_mark->get_complex_part_type();+ in = complex_type_mark->input_value(0);+ }concat_inputs.push_back(make_shared<v0::Unsqueeze>(in, axis_const));}auto pack = make_shared<v0::Concat>(concat_inputs, axis);set_node_name(node.get_name(), pack);++ if (has_complex_input) {+ auto complex_pack = make_shared<ComplexTypeMark>(pack, complex_part_type);+ return {complex_pack->output(0)};+ }+return {pack};}} // namespace opdiff --git a/tests/layer_tests/tensorflow_tests/test_tf_Pack.py b/tests/layer_tests/tensorflow_tests/test_tf_Pack.pyindex d1dc7a2a2a1..39073eea9c5 100644--- a/tests/layer_tests/tensorflow_tests/test_tf_Pack.py+++ b/tests/layer_tests/tensorflow_tests/test_tf_Pack.py@@ -51,3 +51,73 @@ class TestPack(CommonTFLayerTest):self._test(*self.create_pack_net(**params),ie_device, precision, ir_version, temp_dir=temp_dir,use_legacy_frontend=use_legacy_frontend)++ test_data_negative_axis = [+ dict(input_shape=[2, 4], input_num=2, axis=-1, input_type=np.float32),+ dict(input_shape=[3, 1, 2], input_num=3, axis=-2, input_type=np.int32),+ ]++ @pytest.mark.parametrize("params", test_data_negative_axis)+ @pytest.mark.precommit+ @pytest.mark.nightly+ def test_pack_negative_axis(self, params, ie_device, precision, ir_version, temp_dir,+ use_legacy_frontend):+ self._test(*self.create_pack_net(**params),+ ie_device, precision, ir_version, temp_dir=temp_dir,+ use_legacy_frontend=use_legacy_frontend)+++class TestComplexPack(CommonTFLayerTest):+ def _prepare_input(self, inputs_info):+ inputs_data = {}+ for input_name, input_shape in inputs_info.items():+ inputs_data[input_name] = np.random.randint(-5, 5, input_shape).astype(np.float32)+ return inputs_data++ def create_complex_pack_net(self, input_shape, input_num, axis):+ tf.compat.v1.reset_default_graph()+ with tf.compat.v1.Session() as sess:+ inputs_real = []+ inputs_imag = []+ for ind in range(input_num):+ input_real = tf.compat.v1.placeholder(tf.float32, input_shape, 'input' + str(ind) + '_real')+ input_imag = tf.compat.v1.placeholder(tf.float32, input_shape, 'input' + str(ind) + '_imag')+ inputs_real.append(input_real)+ inputs_imag.append(input_imag)+ if axis is not None:+ tf.raw_ops.Pack(values=inputs_real + inputs_imag, axis=axis)+ else:+ tf.raw_ops.Pack(values=inputs_real + inputs_imag)+ tf.compat.v1.global_variables_initializer()++ tf_net = sess.graph_def++ return tf_net, None++ test_data_basic = [+ dict(input_shape=[2, 4], input_num=2, axis=None),+ dict(input_shape=[3, 1, 2], input_num=3, axis=1),+ ]++ @pytest.mark.parametrize("params", test_data_basic)+ @pytest.mark.precommit+ @pytest.mark.nightly+ def test_complex_pack_basic(self, params, ie_device, precision, ir_version, temp_dir,+ use_legacy_frontend):+ self._test(*self.create_complex_pack_net(**params),+ ie_device, precision, ir_version, temp_dir=temp_dir,+ use_legacy_frontend=use_legacy_frontend)++ test_data_negative_axis = [+ dict(input_shape=[2, 4], input_num=2, axis=-1),+ dict(input_shape=[3, 1, 2], input_num=3, axis=-2),+ ]++ @pytest.mark.parametrize("params", test_data_negative_axis)+ @pytest.mark.precommit+ @pytest.mark.nightly+ def test_complex_pack_negative_axis(self, params, ie_device, precision, ir_version, temp_dir,+ use_legacy_frontend):+ self._test(*self.create_complex_pack_net(**params),+ ie_device, precision, ir_version, temp_dir=temp_dir,+ use_legacy_frontend=use_legacy_frontend)