Skip to main content

JAX Scalify: end-to-end scaled arithmetic

JAX Scalify is a library implementing end-to-end scale propation and scaled arithmetic, allowing easy training and inference of deep neural networks in low precision (BF16, FP16, FP8).

Loss scaling, tensor scaling and block scaling have been widely used in the deep learning literature to unlock training and inference at lower precision. Most of these works focus on ad-hoc approaches around scaling of matrix multiplications (and sometimes reduction operations). Scalify is adopting a more systematic approach with end-to-end scale propagation, i.e. transforming the full computational graph into a ScaledArray graph where every operation has ScaledArray inputs and returns ScaledArray:

@dataclass
class ScaledArray:
    # Main data component, in low precision.
    data: Array
    # Scale, usually scalar, in FP32 or E8M0.
    scale: Array

    def __array__(self) -> Array:
        # Tensor represented as a `ScaledArray`.
        return data * scale.astype(self.data.dtype)

The main benefits of the scalify approach are:

  • Agnostic to neural-net model definition;
  • Decoupling scaling from low-precision, reducing the computational overhead of dynamic rescaling;
  • FP8 matrix multiplications and reductions as simple as a cast;
  • Out-of-the-box support of FP16 (scaled) master weights and optimizer state;
  • Composable with JAX ecosystem: Flax, Optax, ...

Scalify training loop example

A typical JAX training loop just requires a couple of modifications to take advantage of scalify. More specifically:

  • Represent input and state as ScaledArray using the as_scaled_array method (or variations of it);
  • End-to-end scale propagation in update training method using scalify decorator;
  • (Optionally) add dynamic_rescale calls to improve low-precision accuracy and stability;

The following (simplified) example presents how to scalify can be incorporated into a JAX training loop.

import jax_scalify as jsa

# Scalify transform on FWD + BWD + optimizer.
# Propagating scale in the computational graph.
@jsa.scalify
def update(state, data, labels):
    # Forward and backward pass on the NN model.
    loss, grads =
        jax.grad(model)(state, data, labels)
    # Optimizer applied on scaled state.
    state = optimizer.apply(state, grads)
    return loss, state

# Model + optimizer state.
state = (model.init(...), optimizer.init(...))
# Transform state to scaled array(s)
sc_state = jsa.as_scaled_array(state)

for (data, labels) in dataset:
    # If necessary (e.g. images), scale input data.
    data = jsa.as_scaled_array(data)
    # State update, with full scale propagation.
    sc_state = update(sc_state, data, labels)
    # Optional dynamic rescaling of state.
    sc_state = jsa.ops.dynamic_rescale_l2(sc_state)

As presented in the code above, the model state is represented as a JAX PyTree of ScaledArray, propagated end-to-end through the model (forward and backward passes) as well as the optimizer.

A full collection of examples is available:

Installation

JAX Scalify can be directly installed from the github repository in Python virtual environment:

pip install git+https://github.com/graphcore-research/jax-scalify.git@main

Alternatively, for a local development setup:

git clone git@github.com:graphcore-research/jax-scalify.git
pip install -e ./

The major dependencies are numpy, jax and chex libraries.

Documentation

Development

Running pre-commit and pytest on the JAX Scalify repository:

pip install pre-commit
pre-commit run --all-files
pytest -v ./tests

Python wheel can be built with the usual command python -m build.

Graphcore IPU support

JAX Scalify v0.1 is compatible with experimental JAX on IPU, which can be installed in a Graphcore Poplar Python environnment:

pip install jax==0.3.16+ipu jaxlib==0.3.15+ipu.sdk320 -f https://graphcore-research.github.io/jax-experimental/wheels.html

Here are the common JAX libraries compatible with IPU:

pip install chex==0.1.6 flax==0.6.4 equinox==0.7.0 jaxtyping==0.2.8

Metadata

Release files for jax-scalify 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jax-scalify 0.1.1
File Size Uploaded Provenance Actions
jax_scalify-0.1.1.tar.gz 69.2 kB Without provenance Details for jax_scalify-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jax-scalify 0.1.1
File Interpreter ABI Platform Actions
jax_scalify-0.1.1-py3-none-any.whl Python 3 none any Details for jax_scalify-0.1.1-py3-none-any.whl

Total release size: 105.2 kB

Release files / jax_scalify-0.1.1.tar.gz

Download URL jax_scalify-0.1.1.tar.gz
Size 69.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9eb2126ad41393785f850b1b186cec5048a39896769981e32336a27fb2b05fde
BLAKE2b-256 checksum
How to use checksums
eec2064ee948ec445716d44e1a9b9805bb6cd1caddd16d1fd1be05c1a4679540
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / jax_scalify-0.1.1-py3-none-any.whl

Download URL jax_scalify-0.1.1-py3-none-any.whl
Size 36.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a93fd90cffd301294df650b1c5eabfd29551c86151674477bdb11d1c2b94f314
BLAKE2b-256 checksum
How to use checksums
fdea14d94a28d2ef8d222739f1471b56a839fadd84d873da74a9a01bf0e6cf79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.4

Security for jax-scalify 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page