Skip to content

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

[TF FE] Provide better support for TopKV2 and stabilize test (#27374)

Roman Kazantsev892eea46bc96
diff --git a/src/frontends/tensorflow_common/src/op/top_k.cpp b/src/frontends/tensorflow_common/src/op/top_k.cpp
index c916fe5b746..4d3bfdcf64b 100644
--- a/src/frontends/tensorflow_common/src/op/top_k.cpp
+++ b/src/frontends/tensorflow_common/src/op/top_k.cpp
@@ -3,6 +3,7 @@
//
#include "common_op_table.hpp"
+#include "openvino/op/convert.hpp"
#include "openvino/op/topk.hpp"
using namespace std;
@@ -14,22 +15,35 @@ namespace tensorflow {
namespace op {
NamedOutputVector translate_top_k_base_op(const NodeContext& node,
const ov::Output<ov::Node>& k_input,
- int min_input_size) {
+ int min_input_size,
+ const ov::element::Type& index_type = ov::element::i32) {
default_op_checks(node, min_input_size, {"TopK", "TopKV2", "TOPK_V2"});
auto input = node.get_input(0);
// retrieve k attribute
bool sorted = node.get_attribute<bool>("sorted", true);
+ auto topk_index_type = index_type;
+ if (index_type == ov::element::i16) {
+ // v11::TopK supports only int32 and int64 output index type
+ topk_index_type = ov::element::i32;
+ }
auto top_k = make_shared<v11::TopK>(input,
k_input,
-1,
ov::op::v11::TopK::Mode::MAX,
sorted ? v11::TopK::SortType::SORT_VALUES : v11::TopK::SortType::SORT_INDICES,
- ov::element::i32,
+ topk_index_type,
true);
+ auto values = top_k->output(0);
+ auto indices = top_k->output(1);
+ if (index_type != topk_index_type) {
+ // satisfy the requested output index type
+ indices = make_shared<v0::Convert>(indices, index_type)->output(0);
+ }
set_node_name(node.get_name(), top_k);
- return {{"values", top_k->output(0)}, {"indices", top_k->output(1)}};
+ return {{"values", values}, {"indices", indices}};
}
+
NamedOutputVector translate_top_k_op(const NodeContext& node) {
// retrieve k attribute
auto k = node.get_attribute<int64_t>("k");
@@ -39,8 +53,9 @@ NamedOutputVector translate_top_k_op(const NodeContext& node) {
NamedOutputVector translate_top_k_v2_op(const NodeContext& node) {
default_op_checks(node, 2, {"TopKV2", "TOPK_V2"});
+ auto index_type = node.get_attribute<ov::element::Type>("index_type", ov::element::i32);
auto k_input = node.get_input(1);
- return translate_top_k_base_op(node, k_input, 1);
+ return translate_top_k_base_op(node, k_input, 1, index_type);
}
} // namespace op
} // namespace tensorflow
diff --git a/tests/layer_tests/tensorflow_tests/test_tf_TopKV2.py b/tests/layer_tests/tensorflow_tests/test_tf_TopKV2.py
index 08540a1d7fe..23d5c6bf2c2 100644
--- a/tests/layer_tests/tensorflow_tests/test_tf_TopKV2.py
+++ b/tests/layer_tests/tensorflow_tests/test_tf_TopKV2.py
@@ -1,58 +1,66 @@
# Copyright (C) 2018-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
-import platform
-
import numpy as np
+import platform
import pytest
import tensorflow as tf
from common.tf_layer_test_class import CommonTFLayerTest
+rng = np.random.default_rng(235436)
+
class TestTopKV2(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
# generate elements so that the input tensor may contain repeating elements
- assert 'input:0' in inputs_info, "Test error: inputs_info must contain `input`"
- x_shape = inputs_info['input:0']
+ assert 'input:0' in inputs_info, "Test error: inputs_info must contain `x`"
+ input_shape = inputs_info['input:0']
inputs_data = {}
- inputs_data['input:0'] = np.random.randint(-10, 10, x_shape)
+ # need to generate data with repeated elements so the range is narrow from 30 elements
+ if np.issubdtype(self.input_type, np.floating):
+ inputs_data['input:0'] = rng.integers(-15, 15, input_shape).astype(self.input_type)
+ elif np.issubdtype(self.input_type, np.signedinteger):
+ inputs_data['input:0'] = rng.integers(-15, 15, input_shape).astype(self.input_type)
+ else:
+ inputs_data['input:0'] = rng.integers(0, 30, input_shape).astype(self.input_type)
return inputs_data
- def create_topk_v2_net(self, input_shape, input_type, k, sorted, is_first_output, is_second_output):
+ def create_topk_v2_net(self, input_shape, input_type, k, k_type, sorted, index_type):
+ self.input_type = input_type
tf.compat.v1.reset_default_graph()
with tf.compat.v1.Session() as sess:
input = tf.compat.v1.placeholder(input_type, input_shape, 'input')
- k = tf.constant(k, dtype=tf.int32, shape=[])
- topk = tf.raw_ops.TopKV2(input=input, k=k, sorted=sorted)
- if is_first_output:
- tf.identity(topk[0], name='topk_values')
- if is_second_output:
- tf.identity(topk[1], name='unique_indices')
+ k = tf.constant(k, dtype=k_type, shape=[])
+ topk = tf.raw_ops.TopKV2(input=input, k=k, sorted=sorted, index_type=index_type)
+ tf.identity(topk[0], name='topk_values')
+ tf.identity(topk[1], name='unique_indices')
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
- ref_net = None
return tf_net, None
- test_basic = [
- dict(input_shape=[10], input_type=tf.float32, k=5, sorted=True, is_first_output=True, is_second_output=False),
- dict(input_shape=[2, 3, 10], input_type=tf.int32, k=10, sorted=True, is_first_output=True,
- is_second_output=False),
- dict(input_shape=[4, 12], input_type=tf.float32, k=10, sorted=True, is_first_output=True,
- is_second_output=True),
- # Expect stable mode implementation for sort_type=indices in OpenVINO. See 101503
- pytest.param(dict(input_shape=[5, 10], input_type=tf.int32, k=8, sorted=False, is_first_output=True,
- is_second_output=True), marks=pytest.mark.xfail(reason="101503")),
- ]
-
- @pytest.mark.parametrize("params", test_basic)
+ @pytest.mark.parametrize('input_shape', [[30], [4, 30], [4, 2, 70]])
+ @pytest.mark.parametrize('input_type', [np.float16, np.float32, np.float64,
+ np.int32, np.uint8, np.int16, np.int8, np.int64,
+ np.uint16, np.uint32, np.uint64])
+ @pytest.mark.parametrize('k', [5, 10, 20])
+ @pytest.mark.parametrize('k_type', [np.int16, np.int32, np.int64])
+ @pytest.mark.parametrize('sorted', [True,
+ # TensorFlow has undefined behaviour (or poorly described in the spec)
+ # for sorted = False
+ ])
+ @pytest.mark.parametrize('index_type', [np.int16, np.int32, np.int64])
@pytest.mark.precommit
@pytest.mark.nightly
- @pytest.mark.xfail(condition=platform.system() in ('Linux', 'Darwin') and platform.machine() in ('arm', 'armv7l',
- 'aarch64',
- 'arm64', 'ARM64'),
- reason='Ticket - 126314, 122716')
- def test_topk_v2_basic(self, params, ie_device, precision, ir_version, temp_dir, use_legacy_frontend):
- self._test(*self.create_topk_v2_net(**params),
+ def test_topk_v2(self, input_shape, input_type, k, k_type, sorted, index_type,
+ ie_device, precision, ir_version,
+ temp_dir, use_legacy_frontend):
+ if ie_device == 'GPU' and input_type == np.uint8:
+ pytest.skip('156587: Check correct_layout_selected failed for input uint8 on GPU')
+ if platform.machine() in ['arm', 'armv7l', 'aarch64', 'arm64', 'ARM64'] and \
+ input_type in [np.int32, np.uint8, np.int16, np.int8, np.int64,
+ np.uint16, np.uint32, np.uint64]:
+ pytest.skip('156588: Segmenation fault or OpenVINO hangs for TopKV2 on ARM')
+ self._test(*self.create_topk_v2_net(input_shape, input_type, k, k_type, sorted, index_type),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)