Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF FE] Support ApproximateEqual operation for TensorFlow #23351

Merged
merged 14 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 37 additions & 0 deletions src/frontends/tensorflow/src/op/approximate_equal_op.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "common_op_table.hpp"
rkazants marked this conversation as resolved.
Show resolved Hide resolved
#include "openvino/frontend/tensorflow/node_context.hpp"
#include "openvino/frontend/tensorflow/variable.hpp"
rkazants marked this conversation as resolved.
Show resolved Hide resolved
#include "openvino/op/abs.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/less.hpp"
#include "openvino/op/select.hpp"
rkazants marked this conversation as resolved.
Show resolved Hide resolved
#include "openvino/op/subtract.hpp"
#include "utils.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 = make_shared<v0::Constant>(element::i32, Shape{}, node.get_attribute<float>("tolerance"));
rkazants marked this conversation as resolved.
Show resolved Hide resolved
// 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
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 @@ -421,6 +421,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 @@ -31,8 +31,8 @@ OP_T_CONVERTER(translate_unary_op);
OP_CONVERTER(translate_selu_op);
OP_T_CONVERTER(translate_binary_op);
OP_T_CONVERTER(translate_direct_reduce_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
42 changes: 42 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,42 @@
import numpy as np
rkazants marked this conversation as resolved.
Show resolved Hide resolved
import tensorflow as tf
import pytest
from common.tf_layer_test_class import CommonTFLayerTest

rkazants marked this conversation as resolved.
Show resolved Hide resolved
class TestApproximateEqual(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
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'] = np.random.uniform(-10, 10, tensor1_shape).astype(np.float32)
inputs_data['tensor2:0'] = np.random.uniform(-10, 10, tensor2_shape).astype(np.float32)
rkazants marked this conversation as resolved.
Show resolved Hide resolved
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)
Loading