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][DOCS] Document Support of JAX/Flax models by OpenVINO (#26513)
Roman Kazantsev8a875c3c1201
diff --git a/docs/articles_en/openvino-workflow/model-preparation.rst b/docs/articles_en/openvino-workflow/model-preparation.rstindex bea0fcdba53..c23540874e9 100644--- a/docs/articles_en/openvino-workflow/model-preparation.rst+++ b/docs/articles_en/openvino-workflow/model-preparation.rst@@ -22,6 +22,7 @@ OpenVINO supports the following model formats:* TensorFlow Lite,* ONNX,* PaddlePaddle,+* JAX/Flax (experimental feature)* OpenVINO IR.The easiest way to obtain a model is to download it from an online database, such as@@ -61,7 +62,7 @@ The easiest way to obtain a model is to download it from an online database, sucCLI tool. For more details, see the:doc:`Model Conversion API Transition Guide <../documentation/legacy-features/transition-legacy-conversion-api>`.- For PyTorch models, `Python API <#convert-a-model-with-python-convert-model>`__ is the only+ For PyTorch and JAX/Flax models, `Python API <#convert-a-model-with-python-convert-model>`__ is the onlyconversion option.Different model representations@@ -280,7 +281,7 @@ formats towhich can then be read, compiled, and run by the final inference application... note::- PyTorch models cannot be converted with ``ovc``, use ``openvino.convert_model`` instead.+ PyTorch and JAX/Flax models cannot be converted with ``ovc``, use ``openvino.convert_model`` instead.Additional Resources####################diff --git a/docs/articles_en/openvino-workflow/model-preparation/conversion-parameters.rst b/docs/articles_en/openvino-workflow/model-preparation/conversion-parameters.rstindex d018b082f42..c4ad7187286 100644--- a/docs/articles_en/openvino-workflow/model-preparation/conversion-parameters.rst+++ b/docs/articles_en/openvino-workflow/model-preparation/conversion-parameters.rst@@ -47,9 +47,9 @@ which are described below.- ``example_input`` parameter available in Python ``openvino.convert_model`` only isintended to trace the model to obtain its graph representation. This parameter is crucial- for converting PyTorch models and may sometimes be required for TensorFlow models.- For more details, refer to the :doc:`PyTorch Model Conversion <convert-model-pytorch>`- or :doc:`TensorFlow Model Conversion <convert-model-tensorflow>`.+ for converting PyTorch and Flax models and may sometimes be required for TensorFlow models.+ For more details, refer to the :doc:`PyTorch Model Conversion <convert-model-pytorch>`,+ :doc:`TensorFlow Model Conversion <convert-model-tensorflow>`, or :doc:`JAX/Flax Model Conversion <convert-model-jax>`.- ``input`` parameter to set or override shapes for model inputs. It configures dynamicand static dimensions in model inputs depending on your inference requirements. For morediff --git a/docs/articles_en/openvino-workflow/model-preparation/convert-model-jax.rst b/docs/articles_en/openvino-workflow/model-preparation/convert-model-jax.rstnew file mode 100644index 00000000000..586e4127495--- /dev/null+++ b/docs/articles_en/openvino-workflow/model-preparation/convert-model-jax.rst@@ -0,0 +1,176 @@+Converting a JAX/Flax Model (Experimental)+==========================================+++.. meta::+ :description: Learn how to convert a model from the+ JAX/Flax format to the OpenVINO Model.+++The ``openvino.convert_model`` function supports the following JAX/Flax model object types:++* ``jax._src.core.ClosedJaxpr``+* ``flax.linen.Module``++The ``jax._src.core.ClosedJaxpr`` object is created by tracing a Python function using the ``jax.make_jaxpr`` function.+Here is an example of ``jax._src.core.ClosedJaxpr`` object creation and conversion to an OpenVINO model:++.. code-block:: py+ :force:++ import jax+ import jax.numpy as jnp+ import openvino as ov++ # let us have some JAX function+ def jax_func(x, y):+ return jax.lax.tanh(jax.lax.max(x, y))++ # 1. Create ClosedJaxpr object+ x = jnp.array([1.0, 2.0])+ y = jnp.array([-1.0, 10.0])+ jaxpr = jax.make_jaxpr(jax_func)(x, y)+ # 2. Convert to OpenVINO+ ov_model = ov.convert_model(jaxpr)+++Here is an example of the simplest ``flax.linen.Module`` model conversion:++.. code-block:: py+ :force:++ import flax.linen as nn+ import jax+ import jax.numpy as jnp+ import openvino as ov++ # let user have some Flax module+ class SimpleModule(nn.Module):+ features: int++ @nn.compact+ def __call__(self, x):+ return nn.Dense(features=self.features)(x)++ module = SimpleModule(features=4)++ # create example_input used for training+ example_input = jnp.ones((2, 3))++ # prepare parameters to initialize the module+ # they can be also loaded using pickle, flax.serialization+ key = jax.random.PRNGKey(0)+ params = module.init(key, example_input)+ module = module.bind(params)++ ov_model = ov.convert_model(module, example_input=example_input)+++When using ``flax.linen.Module`` as an input model, ``openvino.convert_model`` requires the+``example_input`` parameter to be specified. Internally, it triggers model tracing during+the model conversion process, using the capabilities of the ``jax.make_jaxpr`` function.++The ``__call__`` method of ``flax.linen.Module`` object can also have extra custom flags+, like ``training``, in the input signature. In this case, it is required to create a helper function+that has an input signature without any extra custom flags or parameters, not related to input data.+Here is an example of handling such a case:++.. code-block:: py+ :force:++ import jax+ import jax.numpy as jnp+ import openvino as ov+ from flax import linen as nn+ from flax.core import freeze, unfreeze++ class SimpleModuleWithExtraFlag(nn.Module):+ features: int++ @nn.compact+ def __call__(self, x, training):+ x = nn.Dense(self.features)(x)+ x = nn.BatchNorm(use_running_average=not training)(x)+ return x+++ # 1. Initialize the model+ module = SimpleModuleWithExtraFlag(features=10)+ key = jax.random.PRNGKey(0)+ input_data = jnp.ones((4, 5)) # Batch of 4 samples, each with 5 features+ params = module.init(key, input_data, training=False)++ # 2. Create helper function with only input data parameter+ def helper_function(x):+ return module.apply(params, x, training=False)++ # 3. Trace the helper function+ jaxpr = jax.make_jaxpr(helper_function)(input_data)++ # 4. Convert to OpenVINO+ ov_model = ov.convert_model(jaxpr)+++.. note::++ The resulting OpenVINO IR model can be saved to drive with no additional, JAX-specific steps.+ Use the standard ``ov.save_model(ov_model,'model.xml')`` command.++Exporting a JAX/Flax Model to TensorFlow SavedModel Format+##########################################################++An alternative method of converting JAX/Flax models is exporting them to the TensorFlow SavedModel format+first, with ``jax.experimental.jax2tf.convert``, and then converting the resulting SavedModel directory to OpenVINO IR,+with ``openvino.convert_model``. It can be considered a backup solution, if a model cannot be+converted directly, as described previously.++1. Refer to the `JAX and TensorFlow interoperation <https://github.com/google/jax/blob/main/jax/experimental/jax2tf/README.md>`__+ guide to learn how to export models from JAX/Flax to TensorFlow SavedModel format.+2. Follow :doc:`Convert a TensorFlow model <convert-model-tensorflow>` chapter to produce an OpenVINO IR model.++Here is an illustration of using these two steps together:++.. code-block:: py+ :force:++ import flax.linen as nn+ import jax+ import jax.experimental.jax2tf as jax2tf+ import jax.numpy as jnp+ import openvino as ov+ import openvino as ov+ import tensorflow as tf++ # let user have some Flax module+ class SimpleModule(nn.Module):+ features: int++ @nn.compact+ def __call__(self, x):+ return nn.Dense(features=self.features)(x)++ flax_module = SimpleModule(features=4)++ # prepare parameters to initialize the module+ # they can be also loaded using pickle, flax.serialization+ example_input = jnp.ones((2, 3))+ key = jax.random.PRNGKey(0)+ params = flax_module.init(key, example_input)+ module = flax_module.bind(params)++ # 1. Export to SavedModel+ # create TF function and wrap it into TF Module+ tf_function = tf.function(jax2tf.convert(flax_module, native_serialization=False), autograph=False,+ input_signature=[tf.TensorSpec(shape=[2, 3], dtype=tf.float32)])+ tf_module = tf.Module()+ tf_module.f = tf_function+ tf.saved_model.save(tf_module, './saved_model')++ # 2. Convert to OpenVINO+ ov_model = ov.convert_model('./saved_model')++.. note::++ As of version 0.4.15, it is required to pass the ``native_serialization=False`` parameter+ into ``jax2tf.convert`` for graph serialization mode. Without it, the created TensorFlow+ function will contain the embedded StableHLO modules that are not handled by the OpenVINO TensorFlow Frontend.diff --git a/docs/articles_en/openvino-workflow/model-preparation/convert-model-to-ir.rst b/docs/articles_en/openvino-workflow/model-preparation/convert-model-to-ir.rstindex 171422f932e..560b013301e 100644--- a/docs/articles_en/openvino-workflow/model-preparation/convert-model-to-ir.rst+++ b/docs/articles_en/openvino-workflow/model-preparation/convert-model-to-ir.rst@@ -13,6 +13,7 @@ Convert to OpenVINO IRConvert from ONNX <convert-model-onnx>Convert from TensorFlow Lite <convert-model-tensorflow-lite>Convert from PaddlePaddle <convert-model-paddle>+ Convert from JAX/Flax <convert-model-jax>@@ -572,12 +573,85 @@ used by OpenVINO, typically obtained by converting models of supported frameworkFor details on the conversion, refer to the:doc:`article <convert-model-paddle>`.+ .. tab-item:: JAX/Flax+ :sync: torch++ .. tab-set::++ .. tab-item:: Python+ :sync: py++ The ``convert_model()`` method is the only method applicable to JAX/Flax models.++ .. dropdown:: List of supported formats:++ * **Python objects**:++ * ``jax._src.core.ClosedJaxpr``+ * ``flax.linen.Module``++ * Conversion of the ``jax._src.core.ClosedJaxpr`` object++ .. code-block:: py+ :force:++ import jax+ import jax.numpy as jnp+ import openvino as ov++ # let user have some JAX function+ def jax_func(x, y):+ return jax.lax.tanh(jax.lax.max(x, y))++ # use example inputs for creation of ClosedJaxpr object+ x = jnp.array([1.0, 2.0])+ y = jnp.array([-1.0, 10.0])+ jaxpr = jax.make_jaxpr(jax_func)(x, y)++ ov_model = ov.convert_model(jaxpr)+ compiled_model = ov.compile_model(ov_model, "AUTO")++ * Conversion of the ``flax.linen.Module`` object++ .. code-block:: py+ :force:++ import flax.linen as nn+ import jax+ import jax.numpy as jnp+ import openvino as ov++ # let user have some Flax module+ class SimpleDenseModule(nn.Module):+ features: int++ @nn.compact+ def __call__(self, x):+ return nn.Dense(features=self.features)(x)++ module = SimpleDenseModule(features=4)++ # create example_input used in training+ example_input = jnp.ones((2, 3))++ # prepare parameters to initialize the module+ # they can be also loaded from a disk+ # using pickle, flax.serialization for deserialization+ key = jax.random.PRNGKey(0)+ params = module.init(key, example_input)+ module = module.bind(params)++ ov_model = ov.convert_model(module, example_input=example_input)+ compiled_model = ov.compile_model(ov_model, "AUTO")++ For more details on conversion, refer to the :doc:`conversion guide <convert-model-jax>`.+These are basic examples, for detailed conversion instructions, see the individual guides on:doc:`PyTorch <convert-model-pytorch>`, :doc:`ONNX <convert-model-onnx>`,:doc:`TensorFlow <convert-model-tensorflow>`, :doc:`TensorFlow Lite <convert-model-tensorflow-lite>`,-and :doc:`PaddlePaddle <convert-model-paddle>`.+:doc:`PaddlePaddle <convert-model-paddle>`, and :doc:`JAX/Flax <convert-model-jax>`.Refer to the list of all supported conversion options in :doc:`Conversion Parameters <conversion-parameters>`.@@ -596,7 +670,7 @@ IR Conversion Benefitsespecially useful for large models, like Llama2-7B.| **Saving to IR to avoid large dependencies in inference code**-| Frameworks such as TensorFlow and PyTorch tend to be large dependencies for applications+| Frameworks such as TensorFlow, PyTorch, and JAX/Flax tend to be large dependencies for applicationsrunning inference (multiple gigabytes). Converting models to OpenVINO IR removes thisdependency, as OpenVINO can run its inference with no additional components.This way, much less disk space is needed, while loading and compiling usually takes less