From 5d9c59560b240af244f85c1d1f2f0554c8ec2ac5 Mon Sep 17 00:00:00 2001 From: Roman Kazantsev Date: Tue, 12 Mar 2024 21:35:18 +0400 Subject: [PATCH] [TF FE] Test RaggedTensorToTensor for rowids format and Equal for string tensors (#23410) **Details:** Test RaggedTensorToTensor for rowids format and Equal for string tensors. Needs to be merged after https://github.com/openvinotoolkit/openvino_tokenizers/pull/70 **Tickets:** TBD --------- Signed-off-by: Kazantsev, Roman --- .../tensorflow_tests/test_tf_Equal.py | 47 +++++++++++++++- .../test_tf_RaggedTensorToTensor.py | 56 +++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/tests/layer_tests/tensorflow_tests/test_tf_Equal.py b/tests/layer_tests/tensorflow_tests/test_tf_Equal.py index def3d07ad90ab9..b785e4ce440d95 100644 --- a/tests/layer_tests/tensorflow_tests/test_tf_Equal.py +++ b/tests/layer_tests/tensorflow_tests/test_tf_Equal.py @@ -1,14 +1,17 @@ # Copyright (C) 2022 Intel Corporation # SPDX-License-Identifier: Apache-2.0 +import platform + import numpy as np import pytest import tensorflow as tf from common.tf_layer_test_class import CommonTFLayerTest - # Testing operation Equal # Documentation: https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/math/equal +rng = np.random.default_rng() + class TestTFEqual(CommonTFLayerTest): output_type = np.float32 @@ -210,3 +213,45 @@ def test_tf_equal_float64(self, params, ie_device, precision, ir_version, temp_d ie_device, precision, temp_dir=temp_dir, ir_version=ir_version, use_legacy_frontend=use_legacy_frontend, **params) + + +class TestEqualStr(CommonTFLayerTest): + def _prepare_input(self, inputs_info): + assert 'x:0' in inputs_info + assert 'y:0' in inputs_info + x_shape = inputs_info['x:0'] + y_shape = inputs_info['y:0'] + inputs_data = {} + strings_dictionary = ['UPPER<>CASE SENTENCE', 'lower case\n\s sentence', ' UppEr LoweR CAse SENtence \t\n', + ' some sentence', 'another sentence HERE '] + inputs_data['x:0'] = rng.choice(strings_dictionary, x_shape) + inputs_data['y:0'] = rng.choice(strings_dictionary, y_shape) + return inputs_data + + def create_equal_net(self, x_shape, y_shape): + tf.compat.v1.reset_default_graph() + with tf.compat.v1.Session() as sess: + x = tf.compat.v1.placeholder(tf.string, x_shape, 'x') + y = tf.compat.v1.placeholder(tf.string, x_shape, 'y') + tf.raw_ops.Equal(x=x, y=y) + tf.compat.v1.global_variables_initializer() + tf_net = sess.graph_def + + ref_net = None + + return tf_net, ref_net + + @pytest.mark.parametrize('x_shape', [[1], [5]]) + @pytest.mark.parametrize('y_shape', [[1], [5]]) + @pytest.mark.precommit_tf_fe + @pytest.mark.nightly + @pytest.mark.xfail(condition=platform.system() in ('Darwin', 'Linux') and platform.machine() in ['arm', 'armv7l', + 'aarch64', + 'arm64', 'ARM64'], + reason='126314, 132699: Build tokenizers for ARM and MacOS') + def test_equal_str(self, x_shape, y_shape, + ie_device, precision, ir_version, temp_dir, + use_legacy_frontend): + self._test(*self.create_equal_net(x_shape=x_shape, y_shape=y_shape), + ie_device, precision, ir_version, temp_dir=temp_dir, + use_legacy_frontend=use_legacy_frontend) diff --git a/tests/layer_tests/tensorflow_tests/test_tf_RaggedTensorToTensor.py b/tests/layer_tests/tensorflow_tests/test_tf_RaggedTensorToTensor.py index 3888898013563d..2f44ba5d1c9439 100644 --- a/tests/layer_tests/tensorflow_tests/test_tf_RaggedTensorToTensor.py +++ b/tests/layer_tests/tensorflow_tests/test_tf_RaggedTensorToTensor.py @@ -65,3 +65,59 @@ def test_ragged_tensor_to_tensor(self, shape_type, shape_value, values_shape, va row_partition_types=row_partition_types), ie_device, precision, ir_version, temp_dir=temp_dir, use_legacy_frontend=use_legacy_frontend) + + +class TestRaggedTensorToTensorRowIds(CommonTFLayerTest): + def _prepare_input(self, inputs_info): + assert 'values:0' in inputs_info, "Test error: inputs_info must contain `values`" + values_shape = inputs_info['values:0'] + inputs_data = {} + if np.issubdtype(self.values_type, np.floating): + inputs_data['values:0'] = rng.uniform(-5.0, 5.0, values_shape).astype(self.values_type) + elif np.issubdtype(self.values_type, np.signedinteger): + inputs_data['values:0'] = rng.integers(-8, 8, values_shape).astype(self.values_type) + else: + inputs_data['values:0'] = rng.integers(0, 8, values_shape).astype(self.values_type) + return inputs_data + + def create_ragged_tensor_to_tensor_net(self, shape_type, shape_value, values_shape, values_type, default_value, + row_partition_tensors, row_partition_types): + self.values_type = values_type + tf.compat.v1.reset_default_graph() + + # Create the graph and model + with tf.compat.v1.Session() as sess: + values = tf.compat.v1.placeholder(values_type, values_shape, 'values') + shape = tf.constant(shape_value, dtype=shape_type) + default_value = tf.constant(default_value, dtype=values_type) + tf.raw_ops.RaggedTensorToTensor(shape=shape, values=values, default_value=default_value, + row_partition_tensors=row_partition_tensors, + row_partition_types=row_partition_types) + tf.compat.v1.global_variables_initializer() + tf_net = sess.graph_def + + return tf_net, None + + @pytest.mark.parametrize('shape_type', [np.int32, np.int64]) + @pytest.mark.parametrize('shape_value', [[4, 8], [-1, 64], [5, -1], [-1, -1]]) + @pytest.mark.parametrize('values_shape', [[10]]) + @pytest.mark.parametrize('values_type', [np.float32, np.int32, np.int64]) + @pytest.mark.parametrize('default_value', [-1, 0]) + @pytest.mark.parametrize('row_partition_tensors', [[20, [1, 2, 3, 3, 4, 8, 8, 9, 9, 9]]]) + @pytest.mark.parametrize('row_partition_types', [["FIRST_DIM_SIZE", "VALUE_ROWIDS"]]) + @pytest.mark.precommit_tf_fe + @pytest.mark.nightly + @pytest.mark.xfail(condition=platform.system() in ('Darwin', 'Linux') and platform.machine() in ['arm', 'armv7l', + 'aarch64', + 'arm64', 'ARM64'], + reason='126314, 132699: Build tokenizers for ARM and MacOS') + def test_ragged_tensor_to_tensor(self, shape_type, shape_value, values_shape, values_type, default_value, + row_partition_tensors, row_partition_types, + ie_device, precision, ir_version, temp_dir, use_legacy_frontend): + self._test(*self.create_ragged_tensor_to_tensor_net(shape_type=shape_type, shape_value=shape_value, + values_shape=values_shape, values_type=values_type, + default_value=default_value, + row_partition_tensors=row_partition_tensors, + row_partition_types=row_partition_types), + ie_device, precision, ir_version, temp_dir=temp_dir, + use_legacy_frontend=use_legacy_frontend)