forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TF FE] Support ApproximateEqual operation for TensorFlow (openvinoto…
…olkit#23351) ### Details: - *Adding operation support for ApproximateEqual operation* - *Addresses issue openvinotoolkit#22082 * --------- Co-authored-by: Roman Kazantsev <[email protected]>
- Loading branch information
Showing
5 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/frontends/tensorflow_common/src/op/approximate_equal_op.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "common_op_table.hpp" | ||
#include "openvino/op/abs.hpp" | ||
#include "openvino/op/constant.hpp" | ||
#include "openvino/op/less.hpp" | ||
#include "openvino/op/subtract.hpp" | ||
|
||
using namespace std; | ||
using namespace ov::op; | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace tensorflow { | ||
namespace op { | ||
|
||
OutputVector translate_approximate_equal_op(const NodeContext& node) { | ||
default_op_checks(node, 2, {"ApproximateEqual"}); | ||
auto x = node.get_input(0); | ||
auto y = node.get_input(1); | ||
auto tolerance_value = node.get_attribute<float>("tolerance", 1e-5f); | ||
auto tolerance = create_same_type_const_scalar<float>(x, tolerance_value); | ||
// Implement the logic for ApproximateEqual | ||
auto difference = make_shared<v1::Subtract>(x, y); | ||
auto absolute = make_shared<v0::Abs>(difference); | ||
auto is_less = make_shared<v1::Less>(absolute, tolerance); | ||
|
||
// Create and return the corresponding OpenVINO operation | ||
set_node_name(node.get_name(), is_less); | ||
return {is_less}; | ||
} | ||
} // namespace op | ||
} // namespace tensorflow | ||
} // namespace frontend | ||
} // namespace ov |
45 changes: 45 additions & 0 deletions
45
tests/layer_tests/tensorflow_tests/test_tf_ApproximateEqual.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright (C) 2018-2024 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import numpy as np | ||
import tensorflow as tf | ||
import pytest | ||
from common.tf_layer_test_class import CommonTFLayerTest | ||
|
||
class TestApproximateEqual(CommonTFLayerTest): | ||
def _prepare_input(self, inputs_info): | ||
rng = np.random.default_rng() | ||
assert 'tensor1:0' in inputs_info | ||
assert 'tensor2:0' in inputs_info | ||
tensor1_shape = inputs_info['tensor1:0'] | ||
tensor2_shape = inputs_info['tensor2:0'] | ||
inputs_data = {} | ||
inputs_data['tensor1:0'] = 4 * rng.random(tensor1_shape).astype(np.float32) - 2 | ||
inputs_data['tensor2:0'] = 4 * rng.random(tensor2_shape).astype(np.float32) - 2 | ||
return inputs_data | ||
|
||
def create_approximate_equal_net(self, input1_shape, input2_shape): | ||
tf.compat.v1.reset_default_graph() | ||
# Create the graph and model | ||
with tf.compat.v1.Session() as sess: | ||
tensor1 = tf.compat.v1.placeholder(tf.float32, input1_shape, 'tensor1') | ||
tensor2 = tf.compat.v1.placeholder(tf.float32, input2_shape, 'tensor2') | ||
approx_equal_op = tf.raw_ops.ApproximateEqual(x=tensor1, y=tensor2, tolerance=0.01) | ||
tf.compat.v1.global_variables_initializer() | ||
tf_net = sess.graph_def | ||
|
||
return tf_net, None | ||
|
||
test_data_basic = [ | ||
dict(input1_shape=[2, 3], input2_shape=[2, 3]), | ||
dict(input1_shape=[3, 4, 5], input2_shape=[3, 4, 5]), | ||
dict(input1_shape=[1, 2, 3, 4], input2_shape=[1, 2, 3, 4]), | ||
] | ||
|
||
@pytest.mark.parametrize("params", test_data_basic) | ||
@pytest.mark.precommit_tf_fe | ||
@pytest.mark.nightly | ||
def test_approximate_equal_basic(self, params, ie_device, precision, ir_version, temp_dir, | ||
use_legacy_frontend): | ||
self._test(*self.create_approximate_equal_net(**params), | ||
ie_device, precision, ir_version, temp_dir=temp_dir, | ||
use_legacy_frontend=use_legacy_frontend) |