[!IMPORTANT]
TPU Raiden codebase has graduated into TPU Sync.
[!IMPORTANT]
TPU Sync is under active development and its APIs are evolving rapidly. For stable deployments, we recommend pinning to a tagged release or the verified commit in lkg.version rather than tracking main directly.
If you are integrating TPU Sync into a custom serving or RL stack, feel free to open a GitHub issue to discuss compatibility and workload requirements with the maintainers.
🚀 What is TPU Sync?
TPU Sync is a high-performance Key-Value (KV) cache orchestration, multi-tiered memory management, and cross-node data transfer library purpose-built to accelerate large-scale Large Language Model (LLM) inference and Reinforcement Learning (RL) training loops on Cloud TPU infrastructure.
As LLMs scale toward massive context windows and complex, multi-turn AI agents, the bottleneck in the underlying AI infrastructure has shifted from raw compute power to memory orchestration. Managing the key-value (KV) cache has become one of the most prominent bottlenecks driving up latency and leaving expensive compute cycles underutilized.
To address these bottlenecks, we are introducing TPU Sync — an open-source, native C++ framework for high-performance data transfer and KV cache management on TPUs. TPU Sync functions as the low-level, zero-copy memory and DMA foundation executing directly beneath Python-based ML orchestrators. By mapping directly onto hardware-accelerated memory layers, TPU Sync bridges HBM, Local System Host DRAM, and Remote Disaggregated TPU Nodes, unlocking bare-metal throughput and context capacity with minimal latency overhead.
💡 Key Architectural Features
Zero-Copy PJRT Memory Aliasing: TPU Sync accesses internal JAX arrays and PyTorch tensors by extracting native PjRtBuffer hardware descriptors. This bypasses intermediary CPU bounce-allocation, establishing true zero-copy H2D, D2H, and H2H (reshard) tensor transmission.
GIL-Independent Asynchronous Pipelines: Dispatches high-volume tensor transfers, networking routines, and multi-tier memory allocations entirely onto native C++ background thread pools. By eliminating the Python Global Interpreter Lock (GIL) and Python-level object serialization, memory routing executes in parallel with active TPU Prefill and Decode computation.
Multi-Tiered System DRAM Host Offloading: Establishes host system CPU RAM as a massive, ultra-fast "Tier 2" prefix cache. When accelerator HBM reaches capacity limits, LRU-eviction blocks stream continuously to Host DRAM and are retrieved via page-locked, asynchronous DMA on-demand, maximizing RadixAttention and Prefix-Cache hit rates across thousands of concurrent prompt contexts.
Warm-Boot Crash Persistence via /dev/shm: Backs Host-DRAM KV caches and LRU metadata using OS-level POSIX Shared Memory. If an inference server container restarts, upgrades, or crashes, terabytes of active, offloaded KV-cache remain fully intact in host DRAM. On reboot, the system instantly re-attaches to the warm cache, avoiding catastrophic cold-start re-prefill computation.
Accelerated RL Training Support: Facilitates zero-copy, highly parallelized tensor weight and activation migrations between Reinforcement Learning trainers and samplers, maximizing TPU cluster compute utilization.
🛠️ Get Started
Latest Known Good (LKG) Revision
Due to fast-paced active development, the main branch may occasionally contain temporary unstable changes. We publish the latest verified stable commit hash in the lkg.version file at the root of the repository.
To check out the latest verified stable revision that passes all E2E functional and performance test criteria:
git checkout $(cat lkg.version)
Supported Hardware
TPU Sync is actively validated and optimized for the following accelerator generations:
Cloud TPU v7x
Cloud TPU v6e
Supported JAX versions
TPU Sync builds against one JAX version at a time, chosen at build time (defaults to 0.11.2). All
seven are validated the same way: the extension builds, raw_transfer_test
passes, and the full run_tests.sh jax suite passes on TPU.
When build.sh finishes it installs that version's requirements -- the
top-level requirements.txt with jax, jaxlib and libtpu swapped for the
version's -- so the environment matches what the extension was compiled
against, and the tests then run against whatever the build left behind.
That matching is not cosmetic. TPU Sync's extension and jaxlib load into the same
process, and an XLA or abseil that disagrees with the installed jaxlib does not
fail to compile -- it crashes at run time, usually far from the cause. pip
installs into whatever python3 resolves to, so activate the environment you
intend to test in before you build.
Anything outside the table is rejected; the accepted list is
SUPPORTED_VERSIONS in third_party/jax/versions.bzl. See
third_party/jax/README.md for what each version
directory holds and how to add another.
Prerequisites
You will need a python environment to run the JAX or torch code. Our codebase is validated against Python 3.12. Execute the following sequence to provision a compatible local environment:
cd
python3.12 -m venv .venv312
source .venv312/bin/activate
Installing Bazel
To compile the tpu_sync C++ extension binaries, you will need Bazel 8.6.0.
Option 2: Install via Bazelisk (npm)
Bazelisk is a wrapper that will automatically read the .bazelversion file in the project and download the correct version (8.6.0).
npm install -g @bazel/bazelisk
Verify the installation:
bazel --version
Installing Patchelf (Required for PyTorch)
To compile and link the PyTorch C++ extension (_tpu_raiden_torch.so), you MUST install patchelf:
sudo apt-get install -y patchelf
Why this is necessary: PyTorch's compiled extension requires patchelf to inject a NEEDED link on libpywrap_torch_tpu_common.so at build time. This ensures TPU backend symbols resolve locally during import without triggering fatal duplicate XLA allocator registration crashes.
TPUVM Development Notes
Disk Space: Remote Bazel builds on standard TPUVMs can exhaust disk space in /tmp. Always point Bazel output to a directory that has enough disk space:
PyTorch Wheel Compatibility: Ensure your environment aligns with torch_tpu's pinned C++ ABI expectations (e.g., torch==2.11.0+cpu).
Installing or Building tpu_sync
Option 1: Install from PyPI
Pre-built wheels are published to PyPI as
tpu-sync-jax and
tpu-sync-torch. Until the first
stable release is cut, only .dev nightlies exist, so pass --pre:
For JAX version (self-contained; pulls the pinned jax/jaxlib/libtpu stack):
pip install --pre tpu-sync-jax
For PyTorch version:
pip install --pre tpu-sync-torch
[!IMPORTANT]
Unlike the JAX wheel, the torch wheel does not pull torch or torch_tpu
(they are not on a public index). Install a matching torch_tpu and its exact
torch pin (e.g. torch==2.11.0) first — the TPU Sync torch extension is
ABI-locked to the torch/torch_tpu build it was compiled against. See
RELEASING.md for the torch_tpu
registry and the version paired with each release.
[!NOTE]
The torch wheel's release process is not yet gated on end-to-end
performance tests (the torch path is still maturing).
[!WARNING]
The former distribution names tpu-raiden-jax and tpu-raiden-torch are
retired and are not controlled by this project on PyPI. Do not install
them. Do not combine a PyPI install with --extra-index-url pointing at a
private registry: pip resolves across all configured indexes and will pick
the highest version from any of them, which enables dependency-confusion
attacks.
Option 2: Building from source
We provide a script to handle the build process and compile extension binaries locally. You can scope compilation to specific frameworks:
./build.sh [jax|torch|both]
What this script does:
Navigates to the workspace directory.
Compiles the selected extension modules (_tpu_raiden_jax.so and/or _tpu_raiden_torch.so) using Bazel.
For PyTorch builds, executes patchelf --add-needed on the generated shared library.
Installs the Python dependencies for the JAX version it built against -- requirements.txt with jax, jaxlib and libtpu set to that version's.
Copies compiled .so extension binaries directly into their respective framework source packages.
Building against a specific JAX version
build.sh builds against DEFAULT_VERSION in
third_party/jax/versions.bzl. To build against another, set
RAIDEN_JAX_VERSION to any version in the
table above:
That one variable moves everything that has to move together. The build takes
its jax, xla, rules_ml_toolchain and abseil revisions from
third_party/jax/0.10.2/deps.bzl, and the install step at the end puts the
matching jax, jaxlib and libtpu into the active Python environment. Run
the tests with no variable -- they use whatever the build installed.
Three things worth knowing:
Rebuild when you switch. The extension is compiled against one jaxlib's
headers and loaded into a process with whatever jaxlib is installed. If those
disagree it does not fail to import -- it corrupts, and the crash comes later
and somewhere else. Changing RAIDEN_JAX_VERSION means running build.sh
again, in the environment you intend to use.
Only listed versions are accepted.SUPPORTED_VERSIONS in
third_party/jax/versions.bzl is the list; anything else is refused, and the
error names the versions that are accepted.
This is the JAX stack only. A torch-only build takes its xla and
rules_ml_toolchain revisions from the torch_tpu checkout, so
RAIDEN_JAX_VERSION does not apply and build.sh torch rejects it.
These are the core functional unit tests designed to verify the correctness of the foundational components and APIs. Once the build is complete, you can run the test suite across JAX and PyTorch:
./run_tests.sh [jax|torch|both]
What this script does:
Sets up PYTHONPATH so Python can locate the compiled bazel-bin and framework wrapper modules.
Executes the selected unit test suites across JAX and/or PyTorch directly via python.
Playing with TPU Sync
If you'd like to try out TPU Sync and see it in action, please refer to the examples/ directory. This folder contains a collection of hands-on scripts designed for users to interact with the library, including:
TPU Sync supports allocating host memory staging buffers in POSIX Shared Memory (/dev/shm). This allows preserving the KV cache in DRAM when the model serving process terminates (e.g., during serving binary updates), preventing cold starts on process restarts.
1. Enabling Shared Memory
To enable shared memory, set the following environment variables before starting the model serving process:
# Enable shared memory by specifying a base namespace key
export RAIDEN_SHM_KEY="raiden_cache"
# Specify a unique identifier of the current model config for validation safety
export RAIDEN_SHM_MODEL_UID="model_v1_architecture_config_hash"
# [Optional] Set a server name if running multiple serving instances on the same host
export RAIDEN_SHM_SERVER_NAME="server_8000"
When these variables are active, TPU Sync will automatically check for compatible shared memory segments:
Cold Boot (First run): TPU Sync creates /dev/shm/raiden_cache_<server_name>_dev_<local_dev_id> files, initializes layout validation metadata headers, and sets up mappings.
Warm Boot (Restarts): TPU Sync automatically re-attaches to the existing shared memory files, verifies that the model configuration (RAIDEN_SHM_MODEL_UID and caching dimensions) matches, and re-registers the pages with the TPU DMA engine without re-allocation.
2. Running Multiple Servers on the Same Host
If you are running multiple model servers on the same TPU VM, you can avoid namespace collisions by specifying a unique RAIDEN_SHM_SERVER_NAME for each server instance (e.g. server_8000 and server_8008). If specified, TPU Sync automatically namespaces the file paths as /dev/shm/<base_key>_<server_name>_dev_<dev_id>.
3. Disabling Shared Memory
To disable shared memory and fall back to standard anonymous private memory allocations, simply unset the environment variables:
Because POSIX shared memory files survive process termination, you may need to clean them up manually to free up host DRAM on the TPUVM.
To see currently allocated shared memory files:
ls -la /dev/shm/ | grep raiden_cache
To reclaim memory, unlink/delete the shared memory files:
rm -f /dev/shm/raiden_cache_*
(Note: unlinking deletes the filenames immediately, and the physical host DRAM pages are freed by the kernel as soon as all active serving processes detach or exit).