Port JAX workloads to Pathways

This document is for machine learning engineers porting JAX workloads to Pathways on Google Kubernetes Engine (GKE). It explains how single-controller execution affects device indexing, CPU host sizing, pathwaysutils integration, checkpointing, data loading, compilation caching, profiling, and XLA flags.

Before you begin

Complete the following prerequisites:

Process index

JAX with Pathways treats all devices across your Pathways cluster as local, which simplifies device management and lets JAX use all available resources. In practice, this means:

  • jax.process_index() always returns 0 for all devices.
  • jax.devices() and jax.local_devices() return all Cloud TPU devices across the entire job.

Hardware type and colocation

For best performance, place all Pathways components and your JAX job in the same Google Cloud zone. Run the CPU head node components, such as the IFRT proxy server, Pathways resource manager, and (for batch workloads) your JAX controller process, on a dedicated, high-capacity CPU VM to handle compilation and coordination overhead. Use at least an n2-standard-64 machine type, which provides 64 vCPUs and 256 GB of memory.

The pathwaysutils package

Pathways-utils is an open-source Python package that integrates JAX workloads with Pathways on Cloud. It provides the following features to help you focus on your machine learning workflows with minimal configuration changes:

  • Proxy JAX backend: Enables the Pathways backend when you set the JAX_PLATFORMS=proxy environment variable.
  • Integrated profiling utilities: Let you profile both your JAX code and the underlying Pathways components by using standard JAX profiling APIs such as jax.profiler.start_trace and jax.profiler.start_server.
  • Distributed checkpointing with Orbax: Registers custom Orbax checkpoint handlers when you import pathwaysutils, letting you save and restore distributed checkpoints without changing your Orbax code.
  • Elastic training primitives: Provide building blocks for elastic training so your training jobs can adapt dynamically to changes in available Cloud TPU slices.

Checkpointing

Orbax is tested with Pathways for distributed checkpointing and restoring with Cloud Storage. When you set the ENABLE_PATHWAYS_PERSISTENCE=1 environment variable and call import pathwaysutils; pathwaysutils.initialize() in your training script, Pathways registers a custom ArrayHandler. It runs checkpoint operations through the Interim Framework Runtime (IFRT) proxy, letting Pathways workers on accelerators save and restore data directly.

Colocated Python

Colocated Python is an open-source JAX API that lets you run user-specified Python code directly on host CPUs attached to accelerators. On GKE, Pathways runs colocated Python on Cloud TPU worker VMs to avoid transferring host data through the controller for data loading and checkpointing.

To build a custom sidecar container image and configure your Pathways JobSet with zero-copy shared host memory, see Run colocated Python with Pathways.

Data loading

During training, your workload repeatedly loads batches from a dataset to feed them into the model. Using an efficient, asynchronous data loader that shards each batch across hosts avoids starving accelerators of work. In standard Pathways training, the data loader runs on a CPU VM (rather than on Cloud TPU worker VMs as in multi-controller setups) and sends data to Cloud TPU VMs. Reading across the network adds latency, which Pathways partially mitigates by reading ahead multiple batches on the CPU host and dispatching the data asynchronously to the Cloud TPUs. This approach is sufficient at small to medium scale.

When you run a multiprocess data loader (such as Grain) in the head workload container on GKE, the container shares the head node with the pathways-proxy and pathways-rm containers. Because of this shared host, configure the following:

  • Set an explicit worker count: Inside a container, os.cpu_count() reports the host VM's total vCPUs rather than the container's CPU limit. Pass an explicit worker count (no more than the container's CPU limit) instead of deriving it from os.cpu_count().
  • Mount a memory-backed /dev/shm volume: Multiprocess data loaders use POSIX shared memory (/dev/shm) for inter-process communication. Mount a memory-backed emptyDir (medium: Memory) volume at /dev/shm so that prefetched batches don't exceed the default container shared-memory limit.

For optimal performance at scale, colocate your input data pipeline by using colocated Python so it runs directly on the Cloud TPU worker hosts. This eliminates the CPU bottleneck and uses Cloud TPU interconnects for fast data transfer.

For a reference example of migrating a TFDS-based input pipeline, see the RemoteIterator implementation in multihost_dataloading.py. This implementation works on both multi-controller JAX and Pathways by using the colocated Python JAX API.

JAX versioning

Pathways releases are tightly coupled with JAX versions for compatibility and stability. Verify that your Pathways container images and your JAX version are aligned. Each Pathways release specifies its compatible JAX version through an image tag of the form jax-<version>.

Compilation cache

The Pathways persistent compilation cache stores compiled XLA executables in a persistent location, such as Cloud Storage, to avoid redundant compilation. This feature is enabled by default when you pass --gcs_scratch_location to the pathways-proxy, pathways-rm, and pathways-worker (or worker) containers. To keep storage costs low, the cache attaches an object lifecycle policy to the Cloud Storage bucket. Because Cloud Storage limits buckets to 50 lifecycle policies, use a common Cloud Storage location across all workloads.

If you omit the --gcs_scratch_location flag or leave it empty, Pathways falls back to an in-memory compilation cache. This cache doesn't persist compiled XLA executables across Pathways deployments.

This cache is similar to the JAX compilation cache, which pathwaysutils.initialize() disables for Pathways workloads.

The compilation cache requires the following Cloud Storage permissions:

  • storage.buckets.get: Retrieves bucket metadata.
  • storage.buckets.update: Lets Pathways set up object lifecycle policies to enforce TTL for cache eviction.
  • storage.objects.list: Lists existing cache objects within the bucket.
  • storage.objects.create: Writes new compiled executables to the cache.
  • storage.objects.get: Reads cached executables from the bucket.

Profiling

You can use the JAX profiler to generate traces of a JAX program in two ways:

  • Programmatic: Capture profiles directly from your JAX code.
  • Manual: Capture profiles on demand after starting the profiler server in your JAX code.

In both cases, Pathways writes the profiles to a Cloud Storage bucket and creates multiple trace files, potentially under different timestamp directories—for example:

  • Main Python process that invoked the trace (typically your notebook VM): JAX_CLIENT_VM_NAME.xplane.pb
  • Pathways IFRT proxy: client.PATHWAYS_HEAD_NODE_NAME.xplane.pb
  • Pathways resource manager: server.*PATHWAYS_HEAD_NODE_NAME.xplane.pb
  • Pathways workers: server.*TPU_NODE_NAME.xplane.pb

You can analyze these trace files with TensorBoard by running the following commands. For more information about TensorBoard and its profiling tools, see Optimize TensorFlow performance using the Profiler.

# Verify that trace files are present
gcloud storage ls -l -r gs://BUCKET/PREFIX

# View on TensorBoard
tensorboard --load_fast=false --logdir=gs://BUCKET/PREFIX

Replace the following:

  • BUCKET: the Cloud Storage bucket that stores the trace files.
  • PREFIX: the path within your Cloud Storage bucket that stores the trace files.

Programmatic profile capture

Capture a profile from inside your code. Pathways saves the profiles in gs://BUCKET/PREFIX under a timestamp directory:

import jax
import pathwaysutils

pathwaysutils.initialize()

jax.profiler.start_trace("gs://BUCKET/PREFIX")

# Run the operations to be profiled
key = jax.random.key(0)
x = jax.random.normal(key, (5000, 5000))
y = x @ x
y.block_until_ready()

jax.profiler.stop_trace()

Manual profile capture

To capture profile information manually, start the profiler server from your Python code:

import jax
import pathwaysutils

pathwaysutils.initialize()

jax.profiler.start_server(JAX_PROFILER_PORT)

# Your JAX code
jax.profiler.stop_server()

Replace JAX_PROFILER_PORT with the port number for the JAX profiler server.

While the profiler server is running, capture a profile and export the data to the target Cloud Storage location. Run the following command from inside the workload container, or after forwarding JAX_PROFILER_PORT from the pathways-head Pod:

export DURATION_IN_SECS=6
export OUTPUT_DIR="gs://BUCKET/PREFIX/"

curl -X POST http://localhost:JAX_PROFILER_PORT/profiling \
     -H "Content-Type: application/json" \
     -d "{\"duration_ms\": $((DURATION_IN_SECS * 1000)), \"repository_path\": \"${OUTPUT_DIR}\"}"

You can find timing information for IFRT proxy client methods like Compile and Execute within your program's trace. These events, which detail the interactions with the IFRT proxy gRPC server during compilation and execution, appear on the thread named GrpcClientSessionUserFuturesWorkQueue. Examining this thread in your trace gives you insight into the performance of these operations.

XLA flags

When you use Pathways, set XLA flags in the pathways-proxy container.

When you use Cluster Toolkit (the gcluster job submit command), set XLA flags by using --pathways-proxy-args:

--pathways-proxy-args="--xla_flag_1=value1 --xla_flag_2=value2"

HLO dump

To inspect High-Level Optimizer (HLO) inputs passed to the XLA compiler, configure Pathways to dump HLO to a Cloud Storage location by using --pathways-proxy-args:

--pathways-proxy-args="--xla_dump_to=gs://BUCKET_NAME/HLO_PREFIX/"

Replace the following:

  • BUCKET_NAME: the Cloud Storage bucket for the HLO dump.
  • HLO_PREFIX: the path prefix within your bucket.

What's next