OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[JAX FE] Support operations for Vision Transformer model (#26254)
Roman Kazantsev7b6c048f7a22
diff --git a/src/bindings/python/src/openvino/frontend/jax/utils.py b/src/bindings/python/src/openvino/frontend/jax/utils.pyindex 0ba59634b9f..936c86c2e28 100644--- a/src/bindings/python/src/openvino/frontend/jax/utils.py+++ b/src/bindings/python/src/openvino/frontend/jax/utils.py@@ -13,26 +13,32 @@ from openvino.runtime import op, Type as OVType, Shape, OVAnynumpy_to_ov_type_map = {np.float32: OVType.f32,bool: OVType.boolean,- jax.dtypes.bfloat16: OVType.bf16, # TODO: check this+ jax.dtypes.bfloat16: OVType.bf16, # TODO: check thisnp.float16: OVType.f16,np.float32: OVType.f32,np.float64: OVType.f64,np.uint8: OVType.u8,np.int8: OVType.i8,+ np.uint16: OVType.u16,np.int16: OVType.i16,+ np.uint32: OVType.u32,np.int32: OVType.i32,+ np.uint64: OVType.u64,np.int64: OVType.i64,}jax_to_ov_type_map = {jnp.float32: OVType.f32,- jnp.bfloat16: OVType.bf16, # TODO: check this+ jnp.bfloat16: OVType.bf16, # TODO: check thisjnp.float16: OVType.f16,jnp.float64: OVType.f64,jnp.uint8: OVType.u8,jnp.int8: OVType.i8,+ jnp.uint16: OVType.u16,jnp.int16: OVType.i16,+ jnp.uint32: OVType.u32,jnp.int32: OVType.i32,+ jnp.uint64: OVType.u64,jnp.int64: OVType.i64,}diff --git a/src/frontends/jax/src/op_table.cpp b/src/frontends/jax/src/op_table.cppindex 2400084578d..00e40b1dfa5 100644--- a/src/frontends/jax/src/op_table.cpp+++ b/src/frontends/jax/src/op_table.cpp@@ -4,9 +4,18 @@#include "op_table.hpp"-#include "openvino/opsets/opset14.hpp"+#include "openvino/op/add.hpp"+#include "openvino/op/divide.hpp"+#include "openvino/op/exp.hpp"+#include "openvino/op/maximum.hpp"+#include "openvino/op/multiply.hpp"+#include "openvino/op/sqrt.hpp"+#include "openvino/op/subtract.hpp"+#include "openvino/op/tanh.hpp"#include "utils.hpp"+using namespace ov::op;+namespace ov {namespace frontend {namespace jax {@@ -32,24 +41,29 @@ OP_CONVERTER(translate_transpose);// Supported ops for Jaxprconst std::map<std::string, CreatorFunction> get_supported_ops_jaxpr() {- return {{"add", op::translate_1to1_match_2_inputs<opset14::Add>},- {"sub", op::translate_1to1_match_2_inputs<opset14::Subtract>},- {"mul", op::translate_1to1_match_2_inputs<opset14::Multiply>},- {"div", op::translate_1to1_match_2_inputs<opset14::Divide>},+ return {{"add", op::translate_1to1_match_2_inputs<v1::Add>},{"broadcast_in_dim", op::translate_broadcast_in_dim},{"constant", op::translate_constant},{"convert_element_type", op::translate_convert},{"conv_general_dilated", op::translate_convolution},{"copy", op::skip_node},+ {"device_put", op::skip_node},+ {"div", op::translate_1to1_match_2_inputs<v1::Divide>},{"dot_general", op::translate_dot_general},- {"max", op::translate_1to1_match_2_inputs<opset14::Maximum>},+ {"exp", op::translate_1to1_match_1_input<v0::Exp>},+ {"max", op::translate_1to1_match_2_inputs<v1::Maximum>},+ {"mul", op::translate_1to1_match_2_inputs<v1::Multiply>},{"reduce_window_max", op::translate_reduce_window_max},{"reduce_window_sum", op::translate_reduce_window_sum},{"transpose", op::translate_transpose},{"rsqrt", op::translate_rsqrt},{"reshape", op::translate_reshape},{"slice", op::translate_slice},- {"squeeze", op::translate_squeeze}};+ {"sqrt", op::translate_1to1_match_1_input<v0::Sqrt>},+ {"squeeze", op::translate_squeeze},+ {"stop_gradient", op::skip_node},+ {"sub", op::translate_1to1_match_2_inputs<v1::Subtract>},+ {"tanh", op::translate_1to1_match_1_input<v0::Tanh>}};};} // namespace jaxdiff --git a/src/frontends/jax/src/utils.hpp b/src/frontends/jax/src/utils.hppindex 518489ed681..ab806bfc61d 100644--- a/src/frontends/jax/src/utils.hpp+++ b/src/frontends/jax/src/utils.hpp@@ -46,6 +46,11 @@ element::Type convert_dtype(int64_t pt_type);OutputVector make_framework_node(const NodeContext& context, const std::string& exception);namespace op {+template <typename T>+OutputVector translate_1to1_match_1_input(const NodeContext& context) {+ num_inputs_check(context, 1, 1);+ return {std::make_shared<T>(context.get_input(0))};+}template <typename T>OutputVector translate_1to1_match_2_inputs(const NodeContext& context) {diff --git a/tests/layer_tests/jax_tests/jax_layer_test_class.py b/tests/layer_tests/jax_tests/jax_layer_test_class.pyindex b1f2a03682c..7fdea94b62c 100644--- a/tests/layer_tests/jax_tests/jax_layer_test_class.py+++ b/tests/layer_tests/jax_tests/jax_layer_test_class.py@@ -2,25 +2,31 @@# SPDX-License-Identifier: Apache-2.0import itertools-from copy import deepcopy-import os-import numpy as np+import osfrom common.constants import test_device, test_precision+from copy import deepcopyfrom jax import numpy as jnpfrom openvino.runtime import Coreclass JaxLayerTest:-++ @staticmethod+ def _check_kind_exist(jaxpr, kind):+ for node in jaxpr.eqns:+ if node.primitive.name == kind:+ return True+ return False+@staticmethoddef use_jaxpr_tracing():jax_compile_env = os.getenv("JAX_TRACE_MODE")if jax_compile_env is not None:return jax_compile_env == "JAXPR"return False-- def _test(self, model, ref_net, ie_device, precision, ir_version, infer_timeout=60, dynamic_shapes=True,++ def _test(self, model, ref_net, kind, ie_device, precision, ir_version, infer_timeout=60, dynamic_shapes=True,**kwargs):""":param enabled_transforms/disabled_transforms: string with idxs of transforms that should be enabled/disabled.@@ -28,7 +34,10 @@ class JaxLayerTest:"""inputs = self._prepare_input()if self.use_jaxpr_tracing():- converted_model = self.convert_via_jaxpr(model, inputs)+ converted_model, jaxpr = self.convert_via_jaxpr(model, inputs)+ # check that expected operation presents in a graph+ if kind is not None:+ assert self._check_kind_exist(jaxpr, kind), '{} operation does not present in a graph'.format(kind)else:converted_model = self.convert_via_tensorflow_function(model, inputs)@@ -121,15 +130,15 @@ class JaxLayerTest:input_signature=function_signature)converted_model = convert_model(f)return converted_model-+def convert_via_jaxpr(self, model, inputs):import jaxfrom openvino.tools.ovc import convert_model-+jaxpr = jax.make_jaxpr(model)(*inputs)converted_model = convert_model(jaxpr)- return converted_model+ return converted_model, jaxprdef get_params(ie_device=None, precision=None):diff --git a/tests/layer_tests/jax_tests/test_add.py b/tests/layer_tests/jax_tests/test_add.pyindex f6762a6679e..26375256ca2 100644--- a/tests/layer_tests/jax_tests/test_add.py+++ b/tests/layer_tests/jax_tests/test_add.py@@ -1,11 +1,12 @@# Copyright (C) 2018-2024 Intel Corporation# SPDX-License-Identifier: Apache-2.0+import jax.numpy as jnpimport numpy as npimport pytestfrom jax_layer_test_class import JaxLayerTest-import jax.numpy as jnp+def get_rand_jnp_array(shape, dtype):if dtype in [np.float32, np.float16, np.float64]:@@ -13,6 +14,7 @@ def get_rand_jnp_array(shape, dtype):else:return jnp.array(np.random.randint(-10, 10, shape).astype(dtype))+class TestAdd(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -28,14 +30,14 @@ class TestAdd(JaxLayerTest):def jax_add(lhs, rhs):return lhs + rhs- return jax_add, None+ return jax_add, None, 'add'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),@@ -51,6 +53,7 @@ class TestAdd(JaxLayerTest):self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,ir_version)+class TestAddWithConstant(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -67,14 +70,14 @@ class TestAddWithConstant(JaxLayerTest):def jax_add_with_constant(lhs, rhs):return lhs + rhs + self.const- return jax_add_with_constant, None+ return jax_add_with_constant, None, 'add'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),@@ -89,7 +92,8 @@ class TestAddWithConstant(JaxLayerTest):def test_add_with_constant(self, ie_device, precision, ir_version, params, input_type):self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,ir_version)-++class TestAddWithLiteralInvar(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -106,14 +110,14 @@ class TestAddWithLiteralInvar(JaxLayerTest):x = lhs + 5return x + rhs- return jax_add_with_constant, None+ return jax_add_with_constant, None, 'add'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),diff --git a/tests/layer_tests/jax_tests/test_broadcast_in_dim.py b/tests/layer_tests/jax_tests/test_broadcast_in_dim.pyindex a4d64f2013f..08c8e06a3d0 100644--- a/tests/layer_tests/jax_tests/test_broadcast_in_dim.py+++ b/tests/layer_tests/jax_tests/test_broadcast_in_dim.py@@ -21,7 +21,7 @@ class TestBroadcastInDim(JaxLayerTest):out = lax.broadcast_in_dim(inp, target_shape, dimensions)return out- return jax_broadcast_in_dim, None+ return jax_broadcast_in_dim, None, 'broadcast_in_dim'test_data_basic = [dict(input_shape=[2, 3], target_shape=[1, 2, 3], dimensions=[1, 2]),diff --git a/tests/layer_tests/jax_tests/test_conv_general_dilated.py b/tests/layer_tests/jax_tests/test_conv_general_dilated.pyindex 1ec32db9ed6..a29fa592ef3 100644--- a/tests/layer_tests/jax_tests/test_conv_general_dilated.py+++ b/tests/layer_tests/jax_tests/test_conv_general_dilated.py@@ -26,7 +26,7 @@ class TestConvGeneralDilated(JaxLayerTest):feature_group_count=feature_group_count)return out- return jax_conv_general_dilated, None+ return jax_conv_general_dilated, None, 'conv_general_dilated'test_data_basic = [# regular convolution with NCHW layout for inputs and NHWC layout for outputdiff --git a/tests/layer_tests/jax_tests/test_copy.py b/tests/layer_tests/jax_tests/test_copy.pyindex 2c5f4c91f62..b9623bc333e 100644--- a/tests/layer_tests/jax_tests/test_copy.py+++ b/tests/layer_tests/jax_tests/test_copy.py@@ -23,7 +23,7 @@ class TestCopy(JaxLayerTest):copy = jnp.array(add)return lax.rsqrt(copy)- return jax_copy, None+ return jax_copy, None, 'copy'@pytest.mark.parametrize("input_shape", [[10],diff --git a/tests/layer_tests/jax_tests/test_device_put.py b/tests/layer_tests/jax_tests/test_device_put.pynew file mode 100644index 00000000000..5e01b934d7f--- /dev/null+++ b/tests/layer_tests/jax_tests/test_device_put.py@@ -0,0 +1,44 @@+# Copyright (C) 2018-2024 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import jax+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(233454)+++class TestDevicePut(JaxLayerTest):+ def _prepare_input(self):+ if np.issubdtype(self.input_type, np.floating):+ x = rng.uniform(-5.0, 5.0, self.input_shape).astype(self.input_type)+ elif np.issubdtype(self.input_type, np.signedinteger):+ x = rng.integers(-8, 8, self.input_shape).astype(self.input_type)+ else:+ x = rng.integers(0, 8, self.input_shape).astype(self.input_type)++ x = jnp.array(x)+ return [x]++ def create_model(self, input_shape, input_type):+ self.input_shape = input_shape+ self.input_type = input_type++ def jax_device_put(x):+ x = x + 1+ return jax.device_put(x)++ return jax_device_put, None, 'device_put'++ @pytest.mark.parametrize("input_shape", [[2], [3, 4]])+ @pytest.mark.parametrize("input_type", [np.float32, np.int32])+ @pytest.mark.nightly+ @pytest.mark.precommit_jax_fe+ def test_device_put(self, ie_device, precision, ir_version, input_shape, input_type):+ self._test(*self.create_model(input_shape, input_type),+ ie_device, precision,+ ir_version)diff --git a/tests/layer_tests/jax_tests/test_div.py b/tests/layer_tests/jax_tests/test_div.pyindex 5a565f6d258..cb9a75efc32 100644--- a/tests/layer_tests/jax_tests/test_div.py+++ b/tests/layer_tests/jax_tests/test_div.py@@ -1,11 +1,12 @@# Copyright (C) 2018-2024 Intel Corporation# SPDX-License-Identifier: Apache-2.0+import jax.numpy as jnpimport numpy as npimport pytestfrom jax_layer_test_class import JaxLayerTest-import jax.numpy as jnp+def get_rand_jnp_array(shape, dtype):if dtype in [np.float32, np.float16, np.float64]:@@ -15,6 +16,7 @@ def get_rand_jnp_array(shape, dtype):res[res == 0] = 1e-6return jnp.array(res)+class TestDiv(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -30,14 +32,14 @@ class TestDiv(JaxLayerTest):def jax_div(lhs, rhs):return lhs / rhs- return jax_div, None+ return jax_div, None, 'div'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),@@ -53,6 +55,7 @@ class TestDiv(JaxLayerTest):self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,ir_version)+class TestDivWithConstant(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -69,14 +72,14 @@ class TestDivWithConstant(JaxLayerTest):def jax_div_with_constant(lhs, rhs):return lhs / rhs / self.const- return jax_div_with_constant, None+ return jax_div_with_constant, None, 'div'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),@@ -91,7 +94,8 @@ class TestDivWithConstant(JaxLayerTest):def test_div_with_constant(self, ie_device, precision, ir_version, params, input_type):self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,ir_version)-++class TestDivWithLiteralInvar(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -108,14 +112,14 @@ class TestDivWithLiteralInvar(JaxLayerTest):x = lhs / 5return x / rhs- return jax_div_with_constant, None+ return jax_div_with_constant, None, 'div'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),diff --git a/tests/layer_tests/jax_tests/test_dot_general.py b/tests/layer_tests/jax_tests/test_dot_general.pyindex 11315ebab26..4372bd6097c 100644--- a/tests/layer_tests/jax_tests/test_dot_general.py+++ b/tests/layer_tests/jax_tests/test_dot_general.py@@ -23,7 +23,7 @@ class TestDotGeneral(JaxLayerTest):out = lax.dot_general(lhs, rhs, dimension_numbers)return out- return jax_dot_general, None+ return jax_dot_general, None, 'dot_general'test_data = [# 1D vector dot 1D vectordiff --git a/tests/layer_tests/jax_tests/test_exp.py b/tests/layer_tests/jax_tests/test_exp.pynew file mode 100644index 00000000000..2c187774d78--- /dev/null+++ b/tests/layer_tests/jax_tests/test_exp.py@@ -0,0 +1,45 @@+# Copyright (C) 2018-2024 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import jax+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(109734)+++class TestExp(JaxLayerTest):+ def _prepare_input(self):+ if np.issubdtype(self.input_type, np.floating):+ x = rng.uniform(-5.0, 5.0, self.input_shape).astype(self.input_type)+ elif np.issubdtype(self.input_type, np.signedinteger):+ x = rng.integers(-8, 8, self.input_shape).astype(self.input_type)+ else:+ x = rng.integers(0, 8, self.input_shape).astype(self.input_type)++ x = jnp.array(x)+ return [x]++ def create_model(self, input_shape, input_type):+ self.input_shape = input_shape+ self.input_type = input_type++ def jax_exp(x):+ return jax.numpy.exp(x)++ return jax_exp, None, 'exp'++ @pytest.mark.parametrize("input_shape", [[2], [3, 4]])+ @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_jax_fe+ def test_exp(self, ie_device, precision, ir_version, input_shape, input_type):+ self._test(*self.create_model(input_shape, input_type),+ ie_device, precision,+ ir_version)diff --git a/tests/layer_tests/jax_tests/test_mul.py b/tests/layer_tests/jax_tests/test_mul.pyindex f507e655052..e13aa1e6ef4 100644--- a/tests/layer_tests/jax_tests/test_mul.py+++ b/tests/layer_tests/jax_tests/test_mul.py@@ -1,11 +1,12 @@# Copyright (C) 2018-2024 Intel Corporation# SPDX-License-Identifier: Apache-2.0+import jax.numpy as jnpimport numpy as npimport pytestfrom jax_layer_test_class import JaxLayerTest-import jax.numpy as jnp+def get_rand_jnp_array(shape, dtype):if dtype in [np.float32, np.float16, np.float64]:@@ -13,6 +14,7 @@ def get_rand_jnp_array(shape, dtype):else:return jnp.array(np.random.randint(-10, 10, shape).astype(dtype))+class TestMul(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -28,14 +30,14 @@ class TestMul(JaxLayerTest):def jax_mul(lhs, rhs):return lhs * rhs- return jax_mul, None+ return jax_mul, None, 'mul'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),@@ -51,6 +53,7 @@ class TestMul(JaxLayerTest):self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,ir_version)+class TestMulWithConstant(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -67,14 +70,14 @@ class TestMulWithConstant(JaxLayerTest):def jax_mul_with_constant(lhs, rhs):return lhs * rhs * self.const- return jax_mul_with_constant, None+ return jax_mul_with_constant, None, 'mul'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),@@ -89,7 +92,8 @@ class TestMulWithConstant(JaxLayerTest):def test_mul_with_constant(self, ie_device, precision, ir_version, params, input_type):self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,ir_version)-++class TestMulWithLiteralInvar(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -106,14 +110,14 @@ class TestMulWithLiteralInvar(JaxLayerTest):x = lhs * 5return x * rhs- return jax_mul_with_constant, None+ return jax_mul_with_constant, None, 'mul'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),diff --git a/tests/layer_tests/jax_tests/test_reduce_window_max.py b/tests/layer_tests/jax_tests/test_reduce_window_max.pyindex e9d054abbb7..39282b84d72 100644--- a/tests/layer_tests/jax_tests/test_reduce_window_max.py+++ b/tests/layer_tests/jax_tests/test_reduce_window_max.py@@ -14,21 +14,21 @@ class TestReduceWindowMax(JaxLayerTest):inp = jnp.array(np.random.rand(*self.input_shape).astype(np.float32))return [inp]- def create_model(self,input_shape, window_dimensions, window_strides, padding):+ def create_model(self, input_shape, window_dimensions, window_strides, padding):self.input_shape = input_shapedef jax_reduce_window_max(inp):out = lax.reduce_window(- inp,- -jnp.inf,- lax.max,- window_dimensions=window_dimensions,+ inp,+ -jnp.inf,+ lax.max,+ window_dimensions=window_dimensions,window_strides=window_strides,padding=padding)return out- return jax_reduce_window_max, None+ return jax_reduce_window_max, None, 'reduce_window_max'test_data_basic = [dict(input_shape=[1, 112, 112, 64], window_dimensions=[1, 3, 3, 1]),diff --git a/tests/layer_tests/jax_tests/test_reduce_window_sum.py b/tests/layer_tests/jax_tests/test_reduce_window_sum.pyindex 012bb1ebab7..0e2e0da4508 100644--- a/tests/layer_tests/jax_tests/test_reduce_window_sum.py+++ b/tests/layer_tests/jax_tests/test_reduce_window_sum.py@@ -14,21 +14,21 @@ class TestReduceWindoSum(JaxLayerTest):inp = jnp.array(np.random.rand(*self.input_shape).astype(np.float32))return [inp]- def create_model(self,input_shape, window_dimensions, window_strides, padding):+ def create_model(self, input_shape, window_dimensions, window_strides, padding):self.input_shape = input_shapedef jax_reduce_window_sum(inp):out = lax.reduce_window(- inp,- 0.0,- lax.add,- window_dimensions=window_dimensions,+ inp,+ 0.0,+ lax.add,+ window_dimensions=window_dimensions,window_strides=window_strides,padding=padding)return out- return jax_reduce_window_sum, None+ return jax_reduce_window_sum, None, 'reduce_window_sum'test_data_basic = [dict(input_shape=[1, 4, 4, 1], window_dimensions=[1, 3, 3, 1]),diff --git a/tests/layer_tests/jax_tests/test_reshape.py b/tests/layer_tests/jax_tests/test_reshape.pyindex 7ce87025989..ecefe4f29e1 100644--- a/tests/layer_tests/jax_tests/test_reshape.py+++ b/tests/layer_tests/jax_tests/test_reshape.py@@ -21,7 +21,11 @@ class TestReshape(JaxLayerTest):out = lax.reshape(inp, new_sizes=new_sizes)return out- return jax_reshape, None+ expected_op = 'reshape'+ if input_shape == new_sizes:+ expected_op = None++ return jax_reshape, None, expected_op@pytest.mark.parametrize("input_shape", [[64],@@ -38,7 +42,8 @@ class TestReshape(JaxLayerTest):self._test(*self.create_model(input_shape=input_shape, new_sizes=new_sizes),ie_device, precision,ir_version)-++class TestReshapeWithDimensions(JaxLayerTest):def _prepare_input(self):inp = jnp.array(np.random.rand(*self.input_shape).astype(np.float32))@@ -51,7 +56,11 @@ class TestReshapeWithDimensions(JaxLayerTest):out = lax.reshape(inp, new_sizes=new_sizes, dimensions=dimensions)return out- return jax_reshape_with_dimensions, None+ expected_op = 'reshape'+ if input_shape == new_sizes:+ expected_op = None++ return jax_reshape_with_dimensions, None, expected_op@pytest.mark.parametrize("input_shape", [[4, 16, 1],diff --git a/tests/layer_tests/jax_tests/test_rsqrt.py b/tests/layer_tests/jax_tests/test_rsqrt.pyindex 4f9062e573d..83ca53bcce6 100644--- a/tests/layer_tests/jax_tests/test_rsqrt.py+++ b/tests/layer_tests/jax_tests/test_rsqrt.py@@ -21,7 +21,7 @@ class TestRsqrt(JaxLayerTest):out = lax.rsqrt(inp)return out- return jax_rsqrt, None+ return jax_rsqrt, None, 'rsqrt'@pytest.mark.parametrize("input_shape", [[10],diff --git a/tests/layer_tests/jax_tests/test_slice.py b/tests/layer_tests/jax_tests/test_slice.pyindex a71bbf35a45..e62d3e5ba91 100644--- a/tests/layer_tests/jax_tests/test_slice.py+++ b/tests/layer_tests/jax_tests/test_slice.py@@ -14,14 +14,14 @@ class TestSlice(JaxLayerTest):inp = jnp.array(np.random.rand(*self.input_shape).astype(np.float32))return [inp]- def create_model(self,input_shape, start_indices, limit_indices):+ def create_model(self, input_shape, start_indices, limit_indices):self.input_shape = input_shapedef jax_slice(inp):out = lax.slice(inp, start_indices, limit_indices)return out- return jax_slice, None+ return jax_slice, None, 'slice'@pytest.mark.parametrize("input_shape", [[16, 16, 16, 16],@@ -36,23 +36,25 @@ class TestSlice(JaxLayerTest):@pytest.mark.nightly@pytest.mark.precommit_jax_fedef test_slice(self, ie_device, precision, ir_version, input_shape, start_indices, limit_indices):- self._test(*self.create_model(input_shape=input_shape, start_indices=start_indices, limit_indices=limit_indices),- ie_device, precision,- ir_version)-+ self._test(+ *self.create_model(input_shape=input_shape, start_indices=start_indices, limit_indices=limit_indices),+ ie_device, precision,+ ir_version)++class TestSliceWithStrides(JaxLayerTest):def _prepare_input(self):inp = jnp.array(np.random.rand(*self.input_shape).astype(np.float32))return [inp]- def create_model(self,input_shape, start_indices, limit_indices, strides):+ def create_model(self, input_shape, start_indices, limit_indices, strides):self.input_shape = input_shapedef jax_slice_with_strides(inp):out = lax.slice(inp, start_indices, limit_indices, strides)return out- return jax_slice_with_strides, None+ return jax_slice_with_strides, None, 'slice'@pytest.mark.parametrize("input_shape", [[16, 16, 16, 16],@@ -69,8 +71,9 @@ class TestSliceWithStrides(JaxLayerTest):])@pytest.mark.nightly@pytest.mark.precommit_jax_fe- def test_slice_with_strides(self, ie_device, precision, ir_version, input_shape, start_indices, limit_indices, strides):- self._test(*self.create_model(input_shape=input_shape, start_indices=start_indices,- limit_indices=limit_indices, strides=strides),+ def test_slice_with_strides(self, ie_device, precision, ir_version, input_shape, start_indices, limit_indices,+ strides):+ self._test(*self.create_model(input_shape=input_shape, start_indices=start_indices,+ limit_indices=limit_indices, strides=strides),ie_device, precision,ir_version)diff --git a/tests/layer_tests/jax_tests/test_sqrt.py b/tests/layer_tests/jax_tests/test_sqrt.pynew file mode 100644index 00000000000..a19836c3c87--- /dev/null+++ b/tests/layer_tests/jax_tests/test_sqrt.py@@ -0,0 +1,43 @@+# Copyright (C) 2018-2024 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import jax+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(34455)+++class TestSqrt(JaxLayerTest):+ def _prepare_input(self):+ if np.issubdtype(self.input_type, np.floating):+ x = rng.uniform(0, 8.0, self.input_shape).astype(self.input_type)+ else:+ x = rng.integers(0, 8, self.input_shape).astype(self.input_type)++ x = jnp.array(x)+ return [x]++ def create_model(self, input_shape, input_type):+ self.input_shape = input_shape+ self.input_type = input_type++ def jax_sqrt(x):+ return jax.numpy.sqrt(x)++ return jax_sqrt, None, 'sqrt'++ @pytest.mark.parametrize("input_shape", [[2], [3, 4]])+ @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_jax_fe+ def test_sqrt(self, ie_device, precision, ir_version, input_shape, input_type):+ self._test(*self.create_model(input_shape, input_type),+ ie_device, precision,+ ir_version)diff --git a/tests/layer_tests/jax_tests/test_squeeze.py b/tests/layer_tests/jax_tests/test_squeeze.pyindex 5b121c1b66c..f8043c189be 100644--- a/tests/layer_tests/jax_tests/test_squeeze.py+++ b/tests/layer_tests/jax_tests/test_squeeze.py@@ -21,7 +21,7 @@ class TestSqueeze(JaxLayerTest):out = lax.squeeze(inp, dimensions=dimensions)return out- return jax_squeeze, None+ return jax_squeeze, None, 'squeeze'test_data_basic = [dict(input_shape=[1, 10, 1, 1], dimensions=[0]),diff --git a/tests/layer_tests/jax_tests/test_stop_gradient.py b/tests/layer_tests/jax_tests/test_stop_gradient.pynew file mode 100644index 00000000000..b8173f81cd3--- /dev/null+++ b/tests/layer_tests/jax_tests/test_stop_gradient.py@@ -0,0 +1,48 @@+# Copyright (C) 2018-2024 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import jax+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(233454)+++class TestStopGradient(JaxLayerTest):+ def _prepare_input(self):+ if np.issubdtype(self.input_type, np.floating):+ x = rng.uniform(-5.0, 5.0, self.input_shape).astype(self.input_type)+ elif np.issubdtype(self.input_type, np.signedinteger):+ x = rng.integers(-8, 8, self.input_shape).astype(self.input_type)+ else:+ x = rng.integers(0, 8, self.input_shape).astype(self.input_type)++ x = jnp.array(x)+ return [x]++ def create_model(self, input_shape, input_type):+ self.input_shape = input_shape+ self.input_type = input_type++ def jax_stop_gradient(x):+ x = x + 1+ x = jax.lax.stop_gradient(x)+ return x / 3++ return jax_stop_gradient, None, 'stop_gradient'++ @pytest.mark.parametrize("input_shape", [[2], [3, 4]])+ @pytest.mark.parametrize("input_type", [np.float16, np.float32, np.float64])+ @pytest.mark.nightly+ @pytest.mark.precommit_jax_fe+ def test_jax_stop_gradient(self, ie_device, precision, ir_version, input_shape, input_type):+ kwargs = {}+ if input_type == np.float16:+ kwargs["custom_eps"] = 1e-3+ self._test(*self.create_model(input_shape, input_type),+ ie_device, precision,+ ir_version, **kwargs)diff --git a/tests/layer_tests/jax_tests/test_sub.py b/tests/layer_tests/jax_tests/test_sub.pyindex 6df07cc6689..0f34b0cfe62 100644--- a/tests/layer_tests/jax_tests/test_sub.py+++ b/tests/layer_tests/jax_tests/test_sub.py@@ -1,11 +1,12 @@# Copyright (C) 2018-2024 Intel Corporation# SPDX-License-Identifier: Apache-2.0+import jax.numpy as jnpimport numpy as npimport pytestfrom jax_layer_test_class import JaxLayerTest-import jax.numpy as jnp+def get_rand_jnp_array(shape, dtype):if dtype in [np.float32, np.float16, np.float64]:@@ -13,6 +14,7 @@ def get_rand_jnp_array(shape, dtype):else:return jnp.array(np.random.randint(-10, 10, shape).astype(dtype))+class TestSub(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -28,14 +30,14 @@ class TestSub(JaxLayerTest):def jax_sub(lhs, rhs):return lhs - rhs- return jax_sub, None+ return jax_sub, None, 'sub'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),@@ -51,6 +53,7 @@ class TestSub(JaxLayerTest):self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,ir_version)+class TestSubWithConstant(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -67,14 +70,14 @@ class TestSubWithConstant(JaxLayerTest):def jax_sub_with_constant(lhs, rhs):return lhs - rhs - self.const- return jax_sub_with_constant, None+ return jax_sub_with_constant, None, 'sub'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),@@ -89,7 +92,8 @@ class TestSubWithConstant(JaxLayerTest):def test_sub_with_constant(self, ie_device, precision, ir_version, params, input_type):self._test(*self.create_model(**params, lhs_type=input_type[0], rhs_type=input_type[1]), ie_device, precision,ir_version)-++class TestSubWithLiteralInvar(JaxLayerTest):def _prepare_input(self):lhs = get_rand_jnp_array(self.lhs_shape, self.lhs_type)@@ -106,14 +110,14 @@ class TestSubWithLiteralInvar(JaxLayerTest):x = lhs - 5return x - rhs- return jax_sub_with_constant, None+ return jax_sub_with_constant, None, 'sub'test_data = [dict(lhs_shape=[4], rhs_shape=[4]),dict(lhs_shape=[2, 5], rhs_shape=[2, 5]),dict(lhs_shape=[2, 3, 4, 5], rhs_shape=[2, 3, 4, 5]),]-+input_types = [(np.float32, np.float32),(np.int32, np.float32),diff --git a/tests/layer_tests/jax_tests/test_tanh.py b/tests/layer_tests/jax_tests/test_tanh.pynew file mode 100644index 00000000000..edd82326c07--- /dev/null+++ b/tests/layer_tests/jax_tests/test_tanh.py@@ -0,0 +1,45 @@+# Copyright (C) 2018-2024 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++import jax+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(109734)+++class TestTanh(JaxLayerTest):+ def _prepare_input(self):+ if np.issubdtype(self.input_type, np.floating):+ x = rng.uniform(-5.0, 5.0, self.input_shape).astype(self.input_type)+ elif np.issubdtype(self.input_type, np.signedinteger):+ x = rng.integers(-8, 8, self.input_shape).astype(self.input_type)+ else:+ x = rng.integers(0, 8, self.input_shape).astype(self.input_type)++ x = jnp.array(x)+ return [x]++ def create_model(self, input_shape, input_type):+ self.input_shape = input_shape+ self.input_type = input_type++ def jax_tanh(x):+ return jax.numpy.tanh(x)++ return jax_tanh, None, 'tanh'++ @pytest.mark.parametrize("input_shape", [[2], [3, 4]])+ @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_jax_fe+ def test_tanh(self, ie_device, precision, ir_version, input_shape, input_type):+ self._test(*self.create_model(input_shape, input_type),+ ie_device, precision,+ ir_version)diff --git a/tests/layer_tests/jax_tests/test_transpose.py b/tests/layer_tests/jax_tests/test_transpose.pyindex d9ce55bf451..6e263e508ed 100644--- a/tests/layer_tests/jax_tests/test_transpose.py+++ b/tests/layer_tests/jax_tests/test_transpose.py@@ -14,14 +14,18 @@ class TestTranspose(JaxLayerTest):inp = jnp.array(np.random.rand(*self.input_shape).astype(np.float32))return [inp]- def create_model(self,input_shape, permutation):+ def create_model(self, input_shape, permutation):self.input_shape = input_shapedef jax_transpose(inp):out = lax.transpose(inp, permutation)return out- return jax_transpose, None+ expected_op = 'transpose'+ if permutation == [0, 1, 2, 3]:+ expected_op = None++ return jax_transpose, None, expected_op@pytest.mark.parametrize("input_shape", [[7, 16, 11, 20],