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
[TF FE] Fix LeakyRelu translator and stabilize tests for it (#26371)
Roman Kazantsev359943d68aca
diff --git a/src/frontends/tensorflow_common/src/op/leaky_relu.cpp b/src/frontends/tensorflow_common/src/op/leaky_relu.cppindex 8337b2f4012..8a87596eb7e 100644--- a/src/frontends/tensorflow_common/src/op/leaky_relu.cpp+++ b/src/frontends/tensorflow_common/src/op/leaky_relu.cpp@@ -5,6 +5,7 @@#include "common_op_table.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/prelu.hpp"+#include "utils.hpp"using namespace std;using namespace ov::op;@@ -17,7 +18,7 @@ ov::OutputVector translate_leaky_relu_op(const NodeContext& node) {default_op_checks(node, 1, {"LeakyRelu", "LEAKY_RELU"});auto features = node.get_input(0);auto alpha = node.get_attribute<float>("alpha", 0.2f);- auto alpha_const = make_shared<v0::Constant>(element::f32, Shape{1}, alpha);+ auto alpha_const = create_same_type_const_scalar<float>(features, alpha);auto leaky_relu = make_shared<v0::PRelu>(features, alpha_const);set_node_name(node.get_name(), leaky_relu);return {leaky_relu};diff --git a/tests/layer_tests/tensorflow_tests/test_tf_LeakyRelu.py b/tests/layer_tests/tensorflow_tests/test_tf_LeakyRelu.pyindex 460e3b2f0db..df67d7b63f2 100644--- a/tests/layer_tests/tensorflow_tests/test_tf_LeakyRelu.py+++ b/tests/layer_tests/tensorflow_tests/test_tf_LeakyRelu.py@@ -1,42 +1,43 @@# Copyright (C) 2018-2024 Intel Corporation# SPDX-License-Identifier: Apache-2.0-import platform-+import numpy as npimport pytestimport tensorflow as tffrom common.tf_layer_test_class import CommonTFLayerTest+rng = np.random.default_rng(54654675)+class TestLeakyRelu(CommonTFLayerTest):- def create_leaky_relu_net(self, x_shape, alpha_value):+ def _prepare_input(self, inputs_info):+ assert 'features:0' in inputs_info, "Test error: inputs_info must contain `features`"+ features_shape = inputs_info['features:0']+ inputs_data = {}+ inputs_data['features:0'] = rng.uniform(-5.0, 5.0, features_shape).astype(self.features_type)+ return inputs_data++ def create_leaky_relu_net(self, features_shape, features_type, alpha):+ self.features_type = features_typetf.compat.v1.reset_default_graph()# Create the graph and modelwith tf.compat.v1.Session() as sess:- x = tf.compat.v1.placeholder(tf.float32, x_shape, 'x')- if alpha_value is None:- tf.raw_ops.LeakyRelu(features=x)- else:- tf.raw_ops.LeakyRelu(features=x, alpha=alpha_value)+ features = tf.compat.v1.placeholder(features_type, features_shape, 'features')+ tf.raw_ops.LeakyRelu(features=features, alpha=alpha)tf.compat.v1.global_variables_initializer()tf_net = sess.graph_defreturn tf_net, None- test_data_basic = [- pytest.param(dict(x_shape=[], alpha_value=0.3), marks=pytest.mark.xfail(reason="98673")),- dict(x_shape=[2, 3], alpha_value=None),- dict(x_shape=[3, 4, 2], alpha_value=3),- ]-- @pytest.mark.parametrize("params", test_data_basic)+ @pytest.mark.parametrize('features_shape', [[], [2], [3, 1], [2, 4, 5]])+ @pytest.mark.parametrize('features_type', [np.float16, np.float32, np.float64])+ @pytest.mark.parametrize('alpha', [None, -2.0, 0.0, 1.0, 2.0])@pytest.mark.precommit@pytest.mark.nightly- @pytest.mark.xfail(condition=platform.system() == 'Darwin' and platform.machine() == 'arm64',- reason='Ticket - 122716')- def test_leaky_relu_basic(self, params, ie_device, precision, ir_version, temp_dir,- use_legacy_frontend):- self._test(*self.create_leaky_relu_net(**params),+ def test_leaky_relu(self, features_shape, features_type, alpha,+ ie_device, precision, ir_version,+ temp_dir, use_legacy_frontend):+ self._test(*self.create_leaky_relu_net(features_shape, features_type, alpha),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_Softmax.py b/tests/layer_tests/tensorflow_tests/test_tf_Softmax.pyindex 446b88b075b..ed296c29d66 100644--- a/tests/layer_tests/tensorflow_tests/test_tf_Softmax.py+++ b/tests/layer_tests/tensorflow_tests/test_tf_Softmax.py@@ -1,45 +1,44 @@# Copyright (C) 2018-2024 Intel Corporation# SPDX-License-Identifier: Apache-2.0-import platform-import numpy as npimport pytestimport tensorflow as tffrom common.tf_layer_test_class import CommonTFLayerTest+rng = np.random.default_rng(54654675)+class TestSoftmax(CommonTFLayerTest):def _prepare_input(self, inputs_info):- assert 'logits:0' in inputs_info+ assert 'logits:0' in inputs_info, "Test error: inputs_info must contain `logits`"logits_shape = inputs_info['logits:0']inputs_data = {}- inputs_data['logits:0'] = np.random.randint(-5, 5, logits_shape).astype(np.float32)-+ inputs_data['logits:0'] = rng.uniform(-5.0, 5.0, logits_shape).astype(self.input_type)return inputs_data- def create_softmax_net(self, input_shape):+ def create_softmax_net(self, input_shape, input_type):+ self.input_type = input_typetf.compat.v1.reset_default_graph()# Create the graph and modelwith tf.compat.v1.Session() as sess:- logits = tf.compat.v1.placeholder(tf.float32, input_shape, 'logits')+ logits = tf.compat.v1.placeholder(input_type, input_shape, 'logits')tf.raw_ops.Softmax(logits=logits)tf.compat.v1.global_variables_initializer()tf_net = sess.graph_defreturn tf_net, None- test_data_basic = [- dict(input_shape=[2, 6]),- ]-- @pytest.mark.parametrize("params", test_data_basic)+ @pytest.mark.parametrize('input_shape', [[2, 6], [1, 3]])+ @pytest.mark.parametrize('input_type', [np.float16, np.float32, np.float64])@pytest.mark.precommit@pytest.mark.nightly- @pytest.mark.xfail(condition=platform.system() == 'Darwin' and platform.machine() == 'arm64',- reason='Ticket - 122716')- def test_softmax_basic(self, params, ie_device, precision, ir_version, temp_dir,- use_legacy_frontend):- self._test(*self.create_softmax_net(**params),+ def test_softmax(self, input_shape, input_type,+ ie_device, precision, ir_version, temp_dir,+ use_legacy_frontend):+ custom_eps = None+ if input_type == np.float16:+ custom_eps = 1e-3+ self._test(*self.create_softmax_net(input_shape, input_type),ie_device, precision, ir_version, temp_dir=temp_dir,- use_legacy_frontend=use_legacy_frontend)+ use_legacy_frontend=use_legacy_frontend, custom_eps=custom_eps)