Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[TF FE] Stabilize tf.keras.layers.Dot layer tests on all platforms (#26528)

Roman Kazantsev7192ce1b4158
diff --git a/tests/layer_tests/tensorflow2_keras_tests/test_tf2_keras_dot.py b/tests/layer_tests/tensorflow2_keras_tests/test_tf2_keras_dot.py
index c9a3363864a..fdb3b17ba33 100644
--- a/tests/layer_tests/tensorflow2_keras_tests/test_tf2_keras_dot.py
+++ b/tests/layer_tests/tensorflow2_keras_tests/test_tf2_keras_dot.py
@@ -1,105 +1,60 @@
# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
+import numpy as np
import pytest
import tensorflow as tf
-
from common.tf2_layer_test_class import CommonTF2LayerTest
+rng = np.random.default_rng(233534)
+
class TestKerasDot(CommonTF2LayerTest):
- def create_keras_dot_net(self, input_names, input_shapes, input_type, axes, normalize,
- ir_version):
- """
- create TensorFlow 2 model with Keras Dot operation
- """
- tf.keras.backend.clear_session() # For easy reset of notebook state
- inputs = [tf.keras.Input(shape=input_shapes[0][1:], name=input_names[0]),
- tf.keras.Input(shape=input_shapes[1][1:], name=input_names[1])]
+ def _prepare_input(self, inputs_info):
+ assert 'x' in inputs_info, "Test error: inputs_info must contain `x`"
+ assert 'y' in inputs_info, "Test error: inputs_info must contain `x`"
+ x_shape = inputs_info['x']
+ y_shape = inputs_info['y']
+ inputs_data = {}
+ if np.issubdtype(self.input_type, np.floating):
+ inputs_data['x'] = rng.uniform(-2.0, 2.0, x_shape).astype(self.input_type)
+ inputs_data['y'] = rng.uniform(-2.0, 2.0, y_shape).astype(self.input_type)
+ elif np.issubdtype(self.input_type, np.signedinteger):
+ inputs_data['x'] = rng.integers(-3, 3, x_shape).astype(self.input_type)
+ inputs_data['y'] = rng.integers(-3, 3, y_shape).astype(self.input_type)
+ else:
+ inputs_data['x'] = rng.integers(0, 3, x_shape).astype(self.input_type)
+ inputs_data['y'] = rng.integers(0, 3, y_shape).astype(self.input_type)
+ return inputs_data
+
+ def create_keras_dot_net(self, input_shapes, axes, input_type, normalize):
+ self.input_type = input_type
+ tf.keras.backend.clear_session()
+ inputs = [tf.keras.Input(shape=input_shapes[0][1:], dtype=input_type, name='x'),
+ tf.keras.Input(shape=input_shapes[1][1:], dtype=input_type, name='y')]
y = tf.keras.layers.Dot(axes, normalize=normalize)(inputs)
tf2_net = tf.keras.Model(inputs=inputs, outputs=[y])
- # TODO: add reference IR net. Now it is omitted since inference is more
- # important and needs to be checked in the first
- ref_net = None
-
- return tf2_net, ref_net
-
- test_data_normalize_float32 = [
- dict(input_names=["x", "y"], input_shapes=[[5, 4], [5, 4]], input_type=tf.float32, axes=1,
- normalize=False),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 4], [5, 1, 4]], input_type=tf.float32,
- axes=2,
- normalize=False),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 4], [5, 1, 4]], input_type=tf.float32,
- axes=1,
- normalize=False),
- dict(input_names=["x", "y"], input_shapes=[[5, 4], [5, 4]], input_type=tf.float32, axes=1,
- normalize=True),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 4], [5, 1, 4]], input_type=tf.float32,
- axes=1, normalize=True),
- ]
-
- @pytest.mark.parametrize("params", test_data_normalize_float32)
- @pytest.mark.nightly
- @pytest.mark.precommit
- def test_keras_dot_normalize_float32(self, params, ie_device, precision, temp_dir, ir_version,
- use_legacy_frontend):
- self._test(*self.create_keras_dot_net(**params, ir_version=ir_version),
- ie_device, precision, temp_dir=temp_dir, ir_version=ir_version,
- use_legacy_frontend=use_legacy_frontend, **params)
-
- test_data_difficult_axes_float32 = [
- dict(input_names=["x", "y"], input_shapes=[[5, 4, 4], [5, 4, 4]], input_type=tf.float32,
- axes=(1, 2),
- normalize=False),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 5, 2], [5, 1, 2, 5]],
- input_type=tf.float32, axes=(2, 3),
- normalize=False),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 64, 2], [5, 1, 16, 64]],
- input_type=tf.float32,
- axes=(2, 3), normalize=False),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 4], [5, 1, 4]], input_type=tf.float32,
- axes=-2, normalize=False),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 1024, 2], [5, 1, 8, 1024]],
- input_type=tf.float32,
- axes=(-2, -1), normalize=False),
- ]
-
- @pytest.mark.parametrize("params", test_data_difficult_axes_float32)
- @pytest.mark.nightly
- @pytest.mark.precommit
- def test_keras_dot_difficult_axes_float32(self, params, ie_device, precision, temp_dir,
- ir_version, use_legacy_frontend):
- self._test(*self.create_keras_dot_net(**params, ir_version=ir_version),
- ie_device, precision, temp_dir=temp_dir, ir_version=ir_version,
- use_legacy_frontend=use_legacy_frontend, **params)
-
- test_data_normalize_higher_rank = [
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 4], [5, 1, 4]], input_type=tf.float32,
- axes=2, normalize=True),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 1024, 2], [5, 1, 8, 1024]],
- input_type=tf.float32,
- axes=(2, 3), normalize=True),
- dict(input_names=["x", "y"], input_shapes=[[5, 4, 4], [5, 4, 4]], input_type=tf.float32,
- axes=(1, 2),
- normalize=True),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 5, 2], [5, 1, 2, 5]],
- input_type=tf.float32, axes=(2, 3),
- normalize=True),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 4], [5, 1, 4]], input_type=tf.float32,
- axes=-2, normalize=True),
- dict(input_names=["x", "y"], input_shapes=[[5, 1, 1024, 2], [5, 1, 4, 1024]],
- input_type=tf.float32,
- axes=(-2, -1), normalize=True),
-
- ]
-
- @pytest.mark.parametrize("params", test_data_normalize_higher_rank)
+ return tf2_net, None
+
+ @pytest.mark.parametrize('input_shapes,axes', [
+ ([[5, 4], [5, 4]], 1),
+ ([[5, 1, 4], [5, 1, 4]], 1),
+ ([[5, 1, 4], [5, 1, 4]], 2),
+ ([[5, 1, 5, 2], [5, 1, 2, 5]], (2, 3)),
+ ([[5, 1, 102, 2], [5, 1, 4, 102]], (-2, -1))
+ ])
+ @pytest.mark.parametrize('input_type', [np.float32, np.float64,
+ np.int8, np.int16, np.int32, np.int64,
+ np.uint8, np.uint16, np.uint32, np.uint64])
+ @pytest.mark.parametrize('normalize', [True, False])
@pytest.mark.nightly
@pytest.mark.precommit
- def test_keras_dot_normalize_higher_rank(self, params, ie_device, precision, temp_dir,
- ir_version, use_legacy_frontend):
- self._test(*self.create_keras_dot_net(**params, ir_version=ir_version),
+ def test_keras_dot(self, input_shapes, axes, input_type, normalize,
+ ie_device, precision, temp_dir, ir_version,
+ use_legacy_frontend):
+ params = {}
+ params['input_shapes'] = input_shapes
+ self._test(*self.create_keras_dot_net(input_shapes, axes, input_type, normalize),
ie_device, precision, temp_dir=temp_dir, ir_version=ir_version,
use_legacy_frontend=use_legacy_frontend, **params)