[TF FE][TF Hub] Support TruncateMod operation (#20468)

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

* Update truncate_mod.cpp

* fix
This commit is contained in:
Siddhant Chauhan 2023-10-20 00:10:38 +05:30 committed by GitHub
parent 3d5fe8d446
commit 070678fc19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 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)},
{"TruncateMod", CreatorFunction(translate_truncate_mod_op)},
{"Unpack", CreatorFunction(translate_unpack_op)},
{"UnravelIndex", CreatorFunction(translate_unravel_index_op)},
{"UnsortedSegmentSum", CreatorFunction(translate_unsorted_segment_sum_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_mod_op);
OP_CONVERTER(translate_unpack_op);
OP_CONVERTER(translate_unravel_index_op);
OP_CONVERTER(translate_unsorted_segment_sum_op);

View File

@ -0,0 +1,48 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "common_op_table.hpp"
#include "openvino/op/equal.hpp"
#include "openvino/op/floor_mod.hpp"
#include "openvino/op/less.hpp"
#include "openvino/op/negative.hpp"
#include "openvino/op/select.hpp"
#include "openvino/op/subtract.hpp"
using namespace std;
using namespace ov::opset10;
namespace ov {
namespace frontend {
namespace tensorflow {
namespace op {
OutputVector translate_truncate_mod_op(const NodeContext& node) {
default_op_checks(node, 2, {"TruncateMod"});
auto x = node.get_input(0);
auto y = node.get_input(1);
auto is_x_negative = make_shared<Less>(x, create_same_type_const_scalar(x, 0));
auto is_y_negative = make_shared<Less>(y, create_same_type_const_scalar(y, 0));
// if (y < 0) {y = -y}
auto negative_y = make_shared<Negative>(y);
y = make_shared<Select>(is_y_negative, negative_y, y);
// check if floor_mod == zero
auto floor_mod = make_shared<FloorMod>(x, y);
auto is_zero = make_shared<Equal>(floor_mod, create_same_type_const_scalar(floor_mod, 0));
// floor_mod - y
auto other_res = make_shared<Subtract>(floor_mod, y);
// select operation to handle the sign
auto result = make_shared<Select>(is_zero, floor_mod, make_shared<Select>(is_x_negative, other_res, floor_mod));
set_node_name(node.get_name(), result);
return result->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 TestTruncateMod(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_mod_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.TruncateMod(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_mod_basic(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend, use_old_api):
self._test(*self.create_truncate_mod_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend, use_old_api=use_old_api)