Skip to content

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

[TF FE] Fix translator for LinSpace operation and stabilize tests for it on all platforms (#26477)

Roman Kazantsevd25367ce05a1
diff --git a/src/frontends/tensorflow_common/src/op/linspace.cpp b/src/frontends/tensorflow_common/src/op/linspace.cpp
index 0b023c168f1..d4a9571a696 100644
--- a/src/frontends/tensorflow_common/src/op/linspace.cpp
+++ b/src/frontends/tensorflow_common/src/op/linspace.cpp
@@ -6,8 +6,10 @@
#include "openvino/op/add.hpp"
#include "openvino/op/convert_like.hpp"
#include "openvino/op/divide.hpp"
+#include "openvino/op/equal.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/range.hpp"
+#include "openvino/op/select.hpp"
#include "openvino/op/subtract.hpp"
#include "utils.hpp"
@@ -28,14 +30,21 @@ OutputVector translate_linspace_op(const NodeContext& node) {
// compute delta value, i.e. distance between neighbor values of the result
auto const_one = create_same_type_const_scalar<int32_t>(num, 1);
+ auto const_zero = create_same_type_const_scalar<int32_t>(num, 0);
Output<Node> num_minus_one = make_shared<v1::Subtract>(num, const_one);
+
+ // prevent to have num_minus_one to be equal to zero to avoid division by zero
+ // that leads to nan values in output
+ auto is_num_minus_one_zero = make_shared<v1::Equal>(num_minus_one, const_zero);
+ num_minus_one = make_shared<v1::Select>(is_num_minus_one_zero, const_one, num_minus_one);
+
+ // compute delta for evenly-spaced split
num_minus_one = make_shared<v1::ConvertLike>(num_minus_one, start);
Output<Node> delta = make_shared<v1::Subtract>(stop, start);
delta = make_shared<v1::Divide>(delta, num_minus_one);
// generate a range of numbers [0, 1, ..., num)
// to have exact numbers of elements equal to num
- auto const_zero = create_same_type_const_scalar<int32_t>(num, 0);
Output<Node> range0_n = make_shared<v4::Range>(const_zero, num, const_one, ov::element::f32);
range0_n = make_shared<v1::ConvertLike>(range0_n, start);
diff --git a/tests/layer_tests/tensorflow_tests/test_tf_LinSpace.py b/tests/layer_tests/tensorflow_tests/test_tf_LinSpace.py
index f3e521faade..c3e9548f442 100644
--- a/tests/layer_tests/tensorflow_tests/test_tf_LinSpace.py
+++ b/tests/layer_tests/tensorflow_tests/test_tf_LinSpace.py
@@ -1,39 +1,50 @@
# Copyright (C) 2018-2024 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
class TestLinSpace(CommonTFLayerTest):
- def create_lin_space_net(self, num_value):
+ def _prepare_input(self, _):
+ inputs_data = {}
+ inputs_data['start:0'] = np.array(self.start_value, dtype=self.input_type)
+ inputs_data['stop:0'] = np.array(self.stop_value, dtype=self.input_type)
+ inputs_data['num:0'] = np.array(self.num_value, dtype=self.num_type)
+ return inputs_data
+
+ def create_lin_space_net(self, start_value, stop_value, num_value, input_type, num_type):
+ self.start_value = start_value
+ self.stop_value = stop_value
+ self.num_value = num_value
+ self.input_type = input_type
+ self.num_type = num_type
+
tf.compat.v1.reset_default_graph()
# Create the graph and model
with tf.compat.v1.Session() as sess:
- start = tf.compat.v1.placeholder(tf.float32, [], 'start')
- stop = tf.compat.v1.placeholder(tf.float32, [], 'stop')
- tf.raw_ops.LinSpace(start=start, stop=stop, num=num_value)
+ start = tf.compat.v1.placeholder(input_type, [], 'start')
+ stop = tf.compat.v1.placeholder(input_type, [], 'stop')
+ num = tf.compat.v1.placeholder(num_type, [], 'num')
+ tf.raw_ops.LinSpace(start=start, stop=stop, num=num)
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
return tf_net, None
- test_data_basic = [
- dict(num_value=2),
- dict(num_value=10),
- ]
-
- @pytest.mark.parametrize("params", test_data_basic)
+ @pytest.mark.parametrize('start_value', [-10.0, -5.0, 0.0, 5.0, 10.0])
+ @pytest.mark.parametrize('stop_value', [-10.0, -5.0, 0.0, 5.0, 10.0])
+ @pytest.mark.parametrize('num_value', [1, 5, 100])
+ @pytest.mark.parametrize('input_type', [np.float32, np.float64])
+ @pytest.mark.parametrize('num_type', [np.int32, np.int64])
@pytest.mark.precommit
@pytest.mark.nightly
- @pytest.mark.xfail(condition=platform.system() == 'Darwin' and platform.machine() == 'arm64',
- reason='Ticket - 122716')
- def test_lin_space_basic(self, params, ie_device, precision, ir_version, temp_dir,
+ def test_lin_space_basic(self, start_value, stop_value, num_value, input_type, num_type,
+ ie_device, precision, ir_version, temp_dir,
use_legacy_frontend):
- self._test(*self.create_lin_space_net(**params),
+ self._test(*self.create_lin_space_net(start_value, stop_value, num_value, input_type, num_type),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)