[TF FE] Refactor Size translator and add layer test (#15653)

Signed-off-by: Kazantsev, Roman <roman.kazantsev@intel.com>
This commit is contained in:
Roman Kazantsev 2023-02-13 11:09:12 +04:00 committed by GitHub
parent 166566a777
commit a91f173166
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 7 deletions

View File

@ -6,6 +6,7 @@
#include "openvino/opsets/opset8.hpp"
using namespace std;
using namespace ov;
using namespace ov::opset8;
namespace ov {
@ -14,13 +15,23 @@ namespace tensorflow {
namespace op {
ov::OutputVector translate_size_op(const NodeContext& node) {
auto data = node.get_input(0);
auto out_type = node.get_attribute<ov::element::Type>("out_type", ov::element::i32);
auto shape_of = make_shared<ShapeOf>(data, out_type);
auto axis = make_shared<Constant>(ov::element::i64, Shape{}, 0);
auto res = make_shared<ReduceProd>(shape_of, axis);
set_node_name(node.get_name(), res);
return res->outputs();
// Size operation computes a number of elements in the input tensor
default_op_checks(node, 1, {"Size"});
auto input = node.get_input(0);
// retrive attribute of the output type
auto out_type = node.get_attribute<element::Type>("out_type", element::i32);
// introduce extra dimension in order to compute size in case of a scalar input
auto const_zero = make_shared<Constant>(element::i32, Shape{1}, 0);
input = make_shared<Unsqueeze>(input, const_zero);
// compute the input tensor size
auto shape_of = make_shared<ShapeOf>(input, out_type);
auto axis = make_shared<Constant>(element::i32, Shape{}, 0);
auto size = make_shared<ReduceProd>(shape_of, axis);
set_node_name(node.get_name(), size);
return {size};
}
} // namespace op

View File

@ -0,0 +1,45 @@
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import pytest
import tensorflow as tf
from common.tf_layer_test_class import CommonTFLayerTest
class TestSize(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
assert 'input' in inputs_info
input_shape = inputs_info['input']
input_type = self.input_type
inputs_data = {}
inputs_data['input'] = np.random.randint(-50, 50, input_shape).astype(input_type)
return inputs_data
def create_size_net(self, input_shape, input_type, out_type):
self.input_type = input_type
tf.compat.v1.reset_default_graph()
# Create the graph and model
with tf.compat.v1.Session() as sess:
input = tf.compat.v1.placeholder(input_type, input_shape, 'input')
tf.raw_ops.Size(input=input, out_type=out_type)
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
return tf_net, None
test_data_basic = [
dict(input_shape=[], input_type=np.float32, out_type=tf.int32),
dict(input_shape=[2, 3], input_type=np.int32, out_type=None),
dict(input_shape=[4, 1, 3], input_type=np.float32, out_type=tf.int64),
]
@pytest.mark.parametrize("params", test_data_basic)
@pytest.mark.precommit_tf_fe
@pytest.mark.nightly
def test_size_basic(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend, use_old_api):
self._test(*self.create_size_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend, use_old_api=use_old_api)