Skip to main content

FlyWheel

| Documentation | Paper | Blog | Latest Release | PyPI |

CI PyPI License

FlyWheel is a kernel library for TPU, written in JAX Pallas by heavyball-research. It implements softmax attention (MHA and MQA/GQA) behind flash-attn's API, the fused linear attention layers of hybrid models (GDN and KDA), DeepSeek sparse attention (DSA), and the routed experts of MoE layers as one fused MLP kernel, on TPU v6e and v7x.

Why FlyWheel?

  • Fast: up to 2.32x over RPA v3 on chunked prefill over a paged KV cache (v7x), and 2.25x over RPA v3 and 1.47x over Splash Attention on 16K sliding window prefill (v6e). See the benchmarks.
  • flash-attn API: flash_attn_func, flash_attn_varlen_func and flash_attn_with_kvcache keep flash-attn's names and arguments, and take and return JAX arrays.
  • Serving-ready: paged bf16 and fp8 KV caches, chunked prefill, prefix caching and speculative verify windows.
  • Official backend: an official attention backend of tpu-inference (vLLM on TPU) and SGLang-TorchTPU (SGLang on TPU), as --attention-backend flywheel. See Adoption.

Core Features

Softmax Attention

  • Prefill: dense, varlen (packed sequences), and chunked prefill over a paged KV cache
  • Decode: paged or contiguous KV cache, with the K/V append fused into the kernel; speculative verify windows run on the decode kernel
  • MLA: prefill and decode for DeepSeek-V2/V3/R1 and Kimi-K2, with bf16/fp8 paged latent caches
  • KV cache formats: bf16, or an fp8 (e4m3, e5m2) paged cache with per-tensor descales
  • Attention variants: causal, sliding window that skips fully masked tiles, softcap, attention sinks (learnable_sink), and fused rotary embedding
  • Backward: jax.grad runs a Pallas backward kernel for MHA and MQA/GQA with causal or full masks

Sparse Attention

  • DSA in one call (v7x): dsa_attention scores with the lightning indexer on the TensorCore, takes an exact top-k and gathers the selected latents on SparseCore, and runs the sparse MLA, over paged fp8 caches; the SparseCore selects one chunk of tokens while the TensorCore scores the next
  • Ragged serving batches: each request attends sparse, masked-dense or in unions by its lengths, all in one call; a union gathers once the positions that any of a run of nearby tokens selects
  • Shared selections: dsa_indices and dsa_attention_from_indices split a layer into selection and attention, for models whose layers share indices; all three live in flywheel_tpu.sparse_attention.dsa.attention

Linear Attention

  • Fused GDN and KDA: causal conv1d + silu and the chunked delta rule in one kernel, for Gated DeltaNet (e.g. Qwen3-Next) and Kimi Delta Attention (e.g. Kimi Linear)
  • Ragged serving batches: decodes and prefills in one call, with per-request conv and recurrent states in slot caches, prefix caching and speculative verify windows
  • ReplaySSM decode: decode from a state checkpoint plus the records of recent tokens, as in ReplaySSM

MoE

  • Fused expert MLP: the token gather, both matmuls and the gated activation in one kernel, with the intermediate kept in VMEM
  • Any routing: softmax top-k from the router logits, or topk_weights and topk_ids from any other routing; on v7x the top-k, token sort and combine run on SparseCore
  • Any expert size: tensor-parallel slices of the intermediate are padded at weight load time, and experts larger than VMEM stream in blocks

News

Latest: GitHub Release

  • [2026/11] v0.5.0: MLA decode and chunked prefill on a paged latent cache, DeepSeek sparse attention (DSA), and fused MoE
  • [2026/11] v0.4.1: optimized decode kernel and GDN conv pools
  • [2026/11] Paper on arXiv: FlyWheel: Attention Kernels Built for the TPU
  • [2026/10] v0.4.0: fp8 paged KV cache, speculative verify windows on the decode kernel, and attention sinks
  • [2026/10] tpu-inference adds FlyWheel as --attention-backend flywheel (#3837)
  • [2026/10] v0.3.0: sliding window prefill that skips masked tiles, sliding window and softcap on the paged KV cache, and ReplaySSM decode for GDN/KDA
  • [2026/10] v0.2.0: faster chunked prefill, prefix caching and decode over a paged KV cache
  • [2026/10] TPU v7x results. Thanks to lk-chen for testing and contributing them!
  • [2026/09] v0.1.0: flash-attn-style softmax attention (forward and backward, varlen, KV cache) and fused GDN/KDA linear attention

Getting Started

Installation

pip install flywheel-tpu            # CPU, Pallas interpret mode
pip install "flywheel-tpu[tpu]"     # TPU VM, adds jax[tpu]>=0.11.2

From source, with uv (needed for the benchmarks and the end-to-end evaluation):

git clone https://github.com/heavyball-research/flywheel-tpu.git && cd flywheel-tpu
uv sync --extra tpu     # plain `uv sync` off TPU
source .venv/bin/activate

Quick Start

import jax
import jax.numpy as jnp
from flywheel_tpu import flash_attn_func, flash_attn_with_kvcache

kq, kk, kv = jax.random.split(jax.random.key(0), 3)

# Causal GQA prefill: 32 query heads share 4 KV heads.
# Layout is head-major, (batch, heads, seqlen, head_dim).
q = jax.random.normal(kq, (1, 32, 4096, 128), jnp.bfloat16)
k = jax.random.normal(kk, (1, 4, 4096, 128), jnp.bfloat16)
v = jax.random.normal(kv, (1, 4, 4096, 128), jnp.bfloat16)
out = flash_attn_func(q, k, v, causal=True)  # (1, 32, 4096, 128)

# One decode step for 8 sequences: append the new K/V to the cache and attend,
# in one kernel. The cache is token-major, (batch, max_len, heads_k, head_dim),
# and is updated in place: rebind the returned caches.
k_cache = jax.random.normal(kk, (8, 8192, 4, 128), jnp.bfloat16)
v_cache = jax.random.normal(kv, (8, 8192, 4, 128), jnp.bfloat16)
cache_seqlens = jnp.array([4096, 100, 2000, 8000, 1, 512, 3000, 7000], jnp.int32)
q_new = jax.random.normal(kq, (8, 1, 32, 128), jnp.bfloat16)
k_new = jax.random.normal(kk, (8, 1, 4, 128), jnp.bfloat16)
v_new = jax.random.normal(kv, (8, 1, 4, 128), jnp.bfloat16)
out, k_cache, v_cache = flash_attn_with_kvcache(
    q_new, k_cache, v_cache, k_new, v_new, cache_seqlens=cache_seqlens, causal=True
)  # out: (8, 1, 32, 128)

Off TPU, pass interpret=True to run the Pallas TPU interpreter.

Documentation

Adoption

FlyWheel powers attention in:

  • tpu-inference (vLLM on TPU), as --attention-backend flywheel:

    VLLM_WORKER_MULTIPROC_METHOD=fork vllm serve Qwen/Qwen3.5-0.8B --attention-backend flywheel
    
  • SGLang-TorchTPU (SGLang on TPU), as --attention-backend flywheel with the sglang[flywheel] extra:

    sglang serve --model-path Qwen/Qwen3-0.6B --device tpu --enable-torch-compile --attention-backend flywheel
    

Acknowledgement

FlyWheel's API follows FlashAttention. Its GDN and KDA kernels start from tpu-inference's GDN v3 kernel, its MoE kernels from tpu-inference's megablox gmm_v2 by way of sglang-jax, and its design draws on tpu-inference's RPA v3, Tokamax's Splash Attention and ReplaySSM.

Citation

If you find FlyWheel helpful in your project or research, please cite our paper:

@misc{bao2026flywheelattentionkernelsbuilt,
      title={FlyWheel: Attention Kernels Built for the TPU},
      author={Zishuo Bao and Zeshen Zhang and Linkun Chen and Yucheng Lu},
      year={2026},
      eprint={2611.01426},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2611.01426},
}

Metadata

Release files for flywheel-tpu 0.5.0

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

Source distribution (sdist)

Source distribution for flywheel-tpu 0.5.0
File Size Uploaded Provenance Actions
flywheel_tpu-0.5.0.tar.gz 547.8 kB Without provenance Details for flywheel_tpu-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flywheel-tpu 0.5.0
File Interpreter ABI Platform Actions
flywheel_tpu-0.5.0-py3-none-any.whl Python 3 none any Details for flywheel_tpu-0.5.0-py3-none-any.whl

Total release size: 1.2 MB

Release files / flywheel_tpu-0.5.0.tar.gz

Download URL flywheel_tpu-0.5.0.tar.gz
Size 547.8 kB
Tags Source
SHA-256 checksum
How to use checksums
95efbc8ec05bdbe2bdebd27f3a47eed321d73dd3f8648c8914f4f0e730398789
BLAKE2b-256 checksum
How to use checksums
6ba80d5813375b651d5a38f7391ef64d051eb2200ac0e13893aa8ddd3a137228
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / flywheel_tpu-0.5.0-py3-none-any.whl

Download URL flywheel_tpu-0.5.0-py3-none-any.whl
Size 635.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
24679cea583021ca37d84dcb4008d1340138b7da60ead327cc797738729471b4
BLAKE2b-256 checksum
How to use checksums
a046f2403fd031d33159a31d3bc4d4ad763773993b3787e940439cad474c4417
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.10 {"installer":{"name":"uv","version":"0.13.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for flywheel-tpu 0.5.0

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.

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