Skip to content

Commit

Permalink
[TF FE] Support ApproximateEqual operation for TensorFlow (openvinoto…
Browse files Browse the repository at this point in the history
…olkit#23351)

### Details:
 - *Adding operation support for ApproximateEqual operation*
 - *Addresses issue openvinotoolkit#22082 *

---------

Co-authored-by: Roman Kazantsev <[email protected]>
  • Loading branch information
2 people authored and alvoron committed Apr 29, 2024
1 parent d34487a commit 007c350
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/frontends/tensorflow/docs/supported_ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ A "supported operation" is one that TensorFlow Frontend can convert to the OpenV
| ApplyProximalGradientDescent | NO | |
| ApplyRMSProp | NO | |
| ApproxTopK | NO | |
| ApproximateEqual | NO | |
| ApproximateEqual | YES | |
| ArgMax | YES | |
| ArgMin | YES | |
| AsString | NO | |
Expand Down
1 change: 1 addition & 0 deletions src/frontends/tensorflow/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"AssignVariableOp", CreatorFunction(translate_assignvariable_op)},
{"AssignAddVariableOp", CreatorFunction(translate_add_variable_op)},
{"AssignSubVariableOp", CreatorFunction(translate_sub_variable_op)},
{"ApproximateEqual", CreatorFunction(translate_approximate_equal_op)},
{"IsVariableInitialized", CreatorFunction(translate_varisinitialized_op)},
{"MergeV2Checkpoints", CreatorFunction(translate_identity_op)},
{"ReadVariableOp", CreatorFunction(translate_readvariable_op)},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ OP_T_CONVERTER(translate_binary_op);
OP_T_CONVERTER(translate_direct_reduce_op);
OP_CONVERTER(translate_addv2_op);
OP_CONVERTER(translate_add_n_op);
OP_CONVERTER(translate_approximate_equal_op);
OP_CONVERTER(translate_adjust_contrast_op);
OP_CONVERTER(translate_arg_max_op);
OP_CONVERTER(translate_arg_min_op);
Expand Down
37 changes: 37 additions & 0 deletions src/frontends/tensorflow_common/src/op/approximate_equal_op.cpp
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 tests/layer_tests/tensorflow_tests/test_tf_ApproximateEqual.py
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)

0 comments on commit 007c350

Please sign in to comment.