[TF FE][TF Hub] Support TruncateDiv operation (#20615)

* [TF FE][TF Hub] Support TruncateDiv operation

* [TF FE][TF Hub] Support TruncateDiv operation

* Update src/frontends/tensorflow_common/src/op/truncate_div.cpp

---------

Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com>
This commit is contained in:
Siddhant Chauhan 2023-10-20 03:28:58 +05:30 committed by GitHub
parent cb8fac1576
commit ec2ae003aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 0 deletions

View File

@ -281,6 +281,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"TopK", CreatorFunction(translate_top_k_op)},
{"TopKV2", CreatorFunction(translate_top_k_v2_op)},
{"Transpose", CreatorFunction(translate_transpose_op)},
{"TruncateDiv", CreatorFunction(translate_truncate_div_op)},
{"TruncateMod", CreatorFunction(translate_truncate_mod_op)},
{"Unpack", CreatorFunction(translate_unpack_op)},
{"UnravelIndex", CreatorFunction(translate_unravel_index_op)},

View File

@ -145,6 +145,7 @@ OP_CONVERTER(translate_tile_op);
OP_CONVERTER_NAMED(translate_top_k_op);
OP_CONVERTER_NAMED(translate_top_k_v2_op);
OP_CONVERTER(translate_transpose_op);
OP_CONVERTER(translate_truncate_div_op);
OP_CONVERTER(translate_truncate_mod_op);
OP_CONVERTER(translate_unpack_op);
OP_CONVERTER(translate_unravel_index_op);

View File

@ -0,0 +1,36 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "common_op_table.hpp"
#include "openvino/op/ceiling.hpp"
#include "openvino/op/divide.hpp"
#include "openvino/op/equal.hpp"
#include "openvino/op/floor.hpp"
#include "openvino/op/less.hpp"
#include "openvino/op/mod.hpp"
#include "openvino/op/select.hpp"
using namespace std;
using namespace ov::opset10;
namespace ov {
namespace frontend {
namespace tensorflow {
namespace op {
OutputVector translate_truncate_div_op(const NodeContext& node) {
default_op_checks(node, 2, {"TruncateDiv"});
auto x = node.get_input(0);
auto y = node.get_input(1);
auto res = make_shared<Divide>(x, y);
auto is_res_negative = make_shared<Less>(res, create_same_type_const_scalar(x, 0));
auto final_res = make_shared<Select>(is_res_negative, make_shared<Ceiling>(res), make_shared<Floor>(res));
set_node_name(node.get_name(), final_res);
return final_res->outputs();
}
} // namespace op
} // namespace tensorflow
} // namespace frontend
} // namespace ov

View File

@ -0,0 +1,49 @@
# 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 TestTruncateDiv(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
assert 'x' in inputs_info
assert 'y' in inputs_info
x_shape = inputs_info['x']
y_shape = inputs_info['y']
inputs_data = {}
# generate x and y to ensure truncation
inputs_data['x'] = np.random.randint(-10, 10, x_shape).astype(self.input_type)
inputs_data['y'] = np.random.randint(1, 10, y_shape).astype(self.input_type)
return inputs_data
def create_truncate_div_net(self, input_shape, input_type):
self.input_type = input_type
tf.compat.v1.reset_default_graph()
# Create the graph and model
with tf.compat.v1.Session() as sess:
x = tf.compat.v1.placeholder(input_type, input_shape, 'x')
y = tf.compat.v1.placeholder(input_type, input_shape, 'y')
tf.raw_ops.TruncateDiv(x=x, y=y)
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
return tf_net, None
test_data_basic = [
dict(input_shape=[10, 20], input_type=np.float32),
dict(input_shape=[8, 5], input_type=np.float32),
dict(input_shape=[5, 3], input_type=np.int32),
dict(input_shape=[6, 4], input_type=np.int32),
]
@pytest.mark.parametrize("params", test_data_basic)
@pytest.mark.precommit_tf_fe
@pytest.mark.nightly
def test_truncate_div_basic(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend, use_old_api):
self._test(*self.create_truncate_div_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend, use_old_api=use_old_api)