Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[JAX FE]:Support jax.lax.ge and jax.lax.gt operation for JAX (#26640)

Hubert Błaszczyk6c9d7962aa79
diff --git a/src/frontends/jax/src/op/binary_op.cpp b/src/frontends/jax/src/op/binary_op.cpp
new file mode 100644
index 00000000000..9fd5cc12525
--- /dev/null
+++ b/src/frontends/jax/src/op/binary_op.cpp
@@ -0,0 +1,34 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/frontend/jax/node_context.hpp"
+#include "openvino/op/greater.hpp"
+#include "openvino/op/greater_eq.hpp"
+#include "utils.hpp"
+
+using namespace std;
+using namespace ov;
+using namespace ov::op;
+
+namespace ov {
+namespace frontend {
+namespace jax {
+namespace op {
+
+template <typename T>
+OutputVector translate_binary_op(const NodeContext& context) {
+ num_inputs_check(context, 2, 2);
+ auto lhs = context.get_input(0);
+ auto rhs = context.get_input(1);
+ Output<Node> binary_op = make_shared<T>(lhs, rhs);
+ return {binary_op};
+}
+
+template OutputVector translate_binary_op<v1::GreaterEqual>(const NodeContext& context);
+template OutputVector translate_binary_op<v1::Greater>(const NodeContext& context);
+
+} // namespace op
+} // namespace jax
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/jax/src/op_table.cpp b/src/frontends/jax/src/op_table.cpp
index f7db07afe67..c0bcb85c724 100644
--- a/src/frontends/jax/src/op_table.cpp
+++ b/src/frontends/jax/src/op_table.cpp
@@ -8,6 +8,8 @@
#include "openvino/op/divide.hpp"
#include "openvino/op/erf.hpp"
#include "openvino/op/exp.hpp"
+#include "openvino/op/greater.hpp"
+#include "openvino/op/greater_eq.hpp"
#include "openvino/op/maximum.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/reduce_max.hpp"
@@ -29,6 +31,7 @@ namespace op {
template <class T> \
OutputVector op(const ov::frontend::jax::NodeContext& node)
+OP_T_CONVERTER(translate_binary_op);
OP_CONVERTER(translate_broadcast_in_dim);
OP_CONVERTER(translate_concatenate);
OP_CONVERTER(translate_constant);
@@ -62,6 +65,8 @@ const std::map<std::string, CreatorFunction> get_supported_ops_jaxpr() {
{"dot_general", op::translate_dot_general},
{"erf", op::translate_1to1_match_1_input<v0::Erf>},
{"exp", op::translate_1to1_match_1_input<v0::Exp>},
+ {"ge", op::translate_binary_op<v1::GreaterEqual>},
+ {"gt", op::translate_binary_op<v1::Greater>},
{"integer_pow", op::translate_integer_pow},
{"max", op::translate_1to1_match_2_inputs<v1::Maximum>},
{"mul", op::translate_1to1_match_2_inputs<v1::Multiply>},
diff --git a/tests/layer_tests/jax_tests/test_binary_comparison.py b/tests/layer_tests/jax_tests/test_binary_comparison.py
new file mode 100644
index 00000000000..0a85b1e4a55
--- /dev/null
+++ b/tests/layer_tests/jax_tests/test_binary_comparison.py
@@ -0,0 +1,55 @@
+# Copyright (C) 2018-2024 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import numpy as np
+import pytest
+from jax import lax
+from jax import numpy as jnp
+
+from jax_layer_test_class import JaxLayerTest
+
+rng = np.random.default_rng(56342)
+
+class TestBinaryComparison(JaxLayerTest):
+ def _prepare_input(self):
+ if np.issubdtype(self.input_type, np.floating):
+ x = rng.uniform(-5.0, 5.0, self.input_shapes[0]).astype(self.input_type)
+ y = rng.uniform(-5.0, 5.0, self.input_shapes[1]).astype(self.input_type)
+ elif np.issubdtype(self.input_type, np.signedinteger):
+ x = rng.integers(-8, 8, self.input_shapes[0]).astype(self.input_type)
+ y = rng.integers(-8, 8, self.input_shapes[1]).astype(self.input_type)
+ else:
+ x = rng.integers(0, 8, self.input_shapes[0]).astype(self.input_type)
+ y = rng.integers(0, 8, self.input_shapes[1]).astype(self.input_type)
+ x = jnp.array(x)
+ y = jnp.array(y)
+ return [x, y]
+
+ def create_model(self, input_shapes, binary_op, input_type):
+ reduce_map = {
+ 'ge': lax.ge,
+ 'gt': lax.gt
+ }
+
+ self.input_shapes = input_shapes
+ self.input_type = input_type
+
+ def jax_binary(x, y):
+ return reduce_map[binary_op](x, y)
+
+ return jax_binary, None, binary_op
+
+ @pytest.mark.parametrize('input_shapes', [[[5], [1]], [[1], [5]], [[2, 2, 4], [1, 1, 4]],
+ [[5, 10], [5, 10]], [[2, 4, 6], [1, 4, 6]],
+ [[5, 8, 10, 128], [5, 1, 10, 128]]])
+ @pytest.mark.parametrize('binary_op', ['ge', 'gt'])
+ @pytest.mark.parametrize('input_type', [np.int8, np.uint8, np.int16, np.uint16,
+ np.int32, np.uint32, np.int64, np.uint64,
+ np.float16, np.float32, np.float64])
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_jax_fe
+ def test_binary(self, input_shapes, binary_op, input_type,
+ ie_device, precision, ir_version):
+ self._test(*self.create_model(input_shapes, binary_op, input_type),
+ ie_device, precision, ir_version)