support MulNoNan for TF (#21459)

* support MulNoNan for TF

* update test_tf_MulNoNan

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

* update zeros const method

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

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

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

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

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

* Update tests/layer_tests/tensorflow_tests/test_tf_MulNoNan.py

---------

Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com>
This commit is contained in:
Ahmad ChalhoubandRoman Kazantsev authored and GitHub committed 2023-12-07 16:31:41 +00:00
1 parent 056282b7ef
commit aa69f00e8d
4 files changed
+92

No files matched your search

@@ -232,6 +232,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"MaxPoolWithArgmax", CreatorFunction(translate_max_pool_with_argmax)},
{"Merge", CreatorFunction(translate_merge_op)},
{"MirrorPad", CreatorFunction(translate_mirror_pad_op)},
{"MulNoNan", CreatorFunction(translate_mul_no_nan_op)},
{"MutableHashTable", CreatorFunction(translate_hash_table_op)},
{"MutableHashTableV2", CreatorFunction(translate_hash_table_op)},
{"NonMaxSuppression", CreatorFunction(translate_non_max_suppression_op)},
@@ -101,6 +101,7 @@ OP_CONVERTER(translate_matrix_diag_op);
OP_CONVERTER(translate_max_pool_op);
OP_CONVERTER_NAMED(translate_max_pool_with_argmax);
OP_CONVERTER(translate_mirror_pad_op);
OP_CONVERTER(translate_mul_no_nan_op);
OP_CONVERTER_NAMED(translate_non_max_suppression_op);
OP_CONVERTER(translate_parallel_dynamic_stitch_op);
OP_CONVERTER(translate_placeholder_op);
@@ -0,0 +1,43 @@
// 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/multiply.hpp"
#include "openvino/op/select.hpp"
#include "utils.hpp"
using namespace std;
using namespace ov::op;
namespace ov {
namespace frontend {
namespace tensorflow {
namespace op {
OutputVector translate_mul_no_nan_op(const NodeContext& node) {
default_op_checks(node, 2, {"MulNoNan"});
// first = x, second = y
auto x = node.get_input(0);
auto y = node.get_input(1);
// prepare zero constant of the same type as the inputs
auto const_zero = create_same_type_const_scalar<int32_t>(x, 0);
// get mask where y equals 0
auto is_zero = make_shared<v1::Equal>(y, const_zero);
// replace all values in x at is_zero mask with zeros
auto x_zeros = make_shared<v1::Select>(is_zero, const_zero, x);
// multiply y with the updated x
auto mul_no_nan = make_shared<v1::Multiply>(x_zeros, y);
set_node_name(node.get_name(), mul_no_nan);
return mul_no_nan->outputs();
}
} // namespace op
} // namespace tensorflow
} // namespace frontend
} // namespace ov
@@ -0,0 +1,47 @@
# 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
from common.utils.tf_utils import mix_array_with_value
class TestMulNoNan(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 = {}
inputs_data['x'] = np.random.randint(-10, 10, x_shape).astype(self.input_type)
inputs_data['x'] = mix_array_with_value(inputs_data['x'], np.inf)
inputs_data['x'] = mix_array_with_value(inputs_data['x'], np.nan)
inputs_data['y'] = np.random.randint(-10, 10, y_shape).astype(self.input_type) * \
np.random.choice([0.0], y_shape).astype(self.input_type)
return inputs_data
def create_mul_no_nan_net(self, input_shape, input_type):
self.input_type = input_type
tf.compat.v1.reset_default_graph()
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.MulNoNan(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, 10], input_type=np.float32),
dict(input_shape=[2, 3, 4], input_type=np.float32),
]
@pytest.mark.parametrize("params", test_data_basic)
@pytest.mark.precommit_tf_fe
@pytest.mark.nightly
def test_mul_no_nan_basic(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend, use_old_api):
self._test(*self.create_mul_no_nan_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend, use_old_api=use_old_api)