Blog

Home / Blog

From Zero to Hero with Google's JAX, NumPy on accelerators

William Tsu
Data Analyst
Experienced data analyst working with data visualization, cloud computing and ETL solutions.
September 15, 2022


Google launched what it calls JAX, JavaScript Acceleration via Native Code on its cloud-based TPU product. This is a significant step forward for machine learning on mobile devices because it enables developers to deploy models directly into the hardware without having to translate the model into native code first.

This opens up several possibilities, including the use of custom data sets written in Python or other languages. Google Cloud Platform recently announced support for PyTorch, one of the most popular deep learning frameworks, on GCP. And now Google says it is supporting NumPy on GPUs.

For those unfamiliar with NumPy, it is a high-level scientific computing library and the standard in many deep learning libraries. However, it does not work directly with GPU memory. Instead, it converts data into a form that can be read by the GPU. By converting data into a format the GPU understands, you can leverage all the power of both the CPU and GPU in parallel.

Google’s JAX allows you to develop models in pure JavaScript, and they say that it works with almost every major browser. You can even run this locally on your desktop. When you want to deploy code that uses JAX to the cloud, JAX compiles the code down to native CUDA or OpenGL code that runs on either the TPU or GPU depending on whether you choose, respectively.

This article explains Google JAX, includes installation instructions and introduces the Google JAX quick start on Colab.

Google JAX combined Autograd

Project JAX combines Autograd with some additional features like pre-allocation, parallel execution, hot code reloading, etc. These features will make it easier to write high-performance applications for different languages including Go and Java. If you are interested in getting started building apps with Project JAX, check out their documentation.

Knowing XLA

For those who are working with Python Machine Learning APIs like TensorFlow, Apache Spark MLlib, Scikit-Learn, etc., there is a great library called XLA (eXtreme Large Arrays). This library enables you to train models and deploy them within minutes while reducing memory footprint and speeding up training times with no code changes required.

The best part about it is that the same model trained in XLA can be run on GPUs or CPUs without any modifications. So it works seamlessly across different data centers too.

XLA combined with Google JAX

Google says XLA could help make coding faster, simpler, and safer. The company says it has been working on XLA since 2010, although it was not ready to talk about what it does exactly until recently. Now that it is finally out of beta, Google hopes developers will start adopting it sooner rather than later.

The company notes that there are benefits to having multiple languages, like JavaScript. For instance, you can write code once and have it work across different devices without rewriting everything. However, Google thinks XLA might offer better performance and security than those alternatives while providing easier access to APIs and tools.

Working of JAX

The JAX framework consists of many different components. These components allow applications to define their business logic while providing a standard mechanism for accessing data and invoking actions. Once these components have been defined, they can be combined into a single application. To use JAX, a developer first creates a document type definition (DTD) file for the class he wants to model. A DTD file contains rules for each tag that will appear in the XML document. After the DTD file has been created, it can then be compiled into a Document Type Definition (DTD) module. This module is what represents a valid XML document when it comes time to parse the document. Finally, the DTD module can be included in a package. This package becomes the container for classes that will represent the application objects. When it comes time to serialize the object back to XML, the application can simply call the method that returns the serialized representation of the object. To deserialize the document, the application would call a method that takes the XML string as input and returns the deserialized object.

Google JAX Quickstart

JAX Quickstart on colab by default uses a GPUTo use a Colab TPU for Google JAX, you must first initialize it with a Special Initialization. The TPU has a limited amount of free monthly usage.

To walk through the process of connecting a hosted runtime to the notebook, click Open in Colab at the top of the Parallel Evaluation in JAX documentation page. This will switch you to the live notebook environment. Then, click the Connect button in the notebook to connect to a hosted runtime.

Benefits of using JAX

Java Server Pages (JSP) was designed to make writing web pages easier. The same concept can be applied to making APIs easier to write. By using JAX, developers do not need to worry about having to manually format dates and numbers. Instead, the date and number formatting occur automatically based on the specific tag being written. If you want to add a new field to a user account record, you can do it without worrying about how to get the value formatted correctly. You only need to tell the system how to retrieve the value instead of formatting it ourselves.

Installing JAX

To install Google JAX, first download the JAX software from the Google website. Then, unzip the file and open the resulting folder. Next, open the folder containing the JAX software and double-click on the "setup" file. Follow the steps to finish the installation. Finally, open the "JAX" folder and double-click on the "run" file. An example to run JAX on a laptop:

Pip install   --upgrade pip

Pip install   --upgrade “jax[cpu]”

GPUs require CUDA and CuDNN libraries, as well as compatible NVIDIA drivers. CUDA-compatible wheels are available from your distribution's repositories. Otherwise, you'll need to build from the source.

JAX pre-built wheels are extremely useful. They provide great support for Google's newer TPUs, but users can also choose to build their wheels if they have a compatible Colab environment.

JAX API

The first layer of JAX jax.NumPy is NumPy-like, but Jython based. This means that there are two types of arrays that are created mutable and immutable. Mutable arrays are created, modified, and deleted. Arrays are immutable, meaning that once created they cannot be modified, only copied.

The second layer is jax. lax is more authoritarian than the NumPy layer. Jax.NumPy operates by using functions defined in jax.lax. While jax.NumPy automatically converts data types to allow operations between different types jax.lax cannot, instead, it provides specific promotion functions.

The third layer of JAX API is XLA. The fundamental operations of XLA are wrapped by jax. lax operations in Python. All JAX operations are ultimately expressed in terms of these operations, enabling JIT compilation.

The entire purpose of a JAX transformation is to separate computation from data. However, this is not same with Python. The computational part of a JAX transformation is written in Python, but the data it is operating on is stored in a JSON file, and that JSON file is what determines how the transformation will be run. So essentially, a JAX transformation is a “pure function,” but it’s not functionally pure because Python uses print() statements, which therefore results in a different output from different executions.

Another limitation is that the JAX does not allow in-place mutations. NumPy defaults to double precision and JAX defaults to single precision (float64) and (float32) respectively. You can set JAX to jax_enable_x64 mode if you need a double precision but single precision usually runs faster and needs very less GPU memory.

With JAX’s accelerated neural networking, developers can develop even faster and more reliably

By now, you should have a good idea that accelerated neural networks can be implemented in JAX. There are certainly several neural network libraries out there - DeepMind is one of them. The advantage of using Flax over other libraries is that it is fully featured. You can train networks from inception, and it is incredibly easy to get started.

The good news

Google's JAX is a framework for using Java libraries on CPU, GPU, and TPUs. It is based on the open-source project NumPy which enables the use of Python libraries on massively parallel systems. The process of translating the model into native code is automatic. Google claims that there is no need to write any additional code to make the model work efficiently. All you do is upload the model and let JAX take care of the rest.