Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

FlashRWKV2

High-Performance TileLang Operator Library for RWKV-7 — Unified Training, RL Rollout & Packed Serving

PyPI Python PyTorch TileLang License: MIT

English | 简体中文


FlashRWKV2 is the community operator backend for RWKV-7, built on TileLang for both training and high-throughput serving. It exposes RWKV-7's highest semantic fusion islands (tokenshift, wkv_prepare, wkv7, readout, cmix, head, sampling, mtp) while keeping tiling, dispatch, and hardware specialization clean and internal.

Complete models, schedulers, and training frameworks stay lightweight in downstream projects — FlashRWKV2 handles the fast, numerically exact kernels underneath.


Why FlashRWKV2?

Choosing an RWKV-7 kernel backend usually involves trade-offs across backend portability, full-layer serving features, PEFT integration, and train–inference parity. FlashRWKV2 unifies these capabilities in one TileLang codebase:

Capability FlashRWKV2 (Ours) RWKV-CUDA (train_temp) Albatross FLA (flash-linear-attention)
Kernel Backend TileLang (maintainable, GPU-specialized DSL) Handwritten CUDA only Handwritten CUDA only Triton
WKV Formulation Exact recurrence (BF16 IO + FP32 accum) Exact recurrence FP16 inference recurrence Chunk-form & fused recurrent (cu_seqlens + final state)
Train–Infer Numerical Parity Bitwise-exact with FP32 state (fp32io16); optional FP16 serving state (fp16) Training-only FP16-only inference (differs from FP32 training) Separate chunk / recurrent Triton paths
Full-Layer Serving Islands End-to-end (tokenshift, wkv_prepare, wkv7, readout, cmix, head, sampling) WKV training kernel Full FP16 inference engine (fixed-batch, no varlen) WKV-focused operators
Serving State Pool & Live Graphs Built-in (FP16 + DeltaLog, COW slots, live CUDA Graph metadata validation) No Fixed-batch state (no scheduler / varlen) Functional state I/O (caller manages pools & graphs)
PEFT (LoRA / NoRA) Built-in (TMix, CMix, LM Head; skips dW when frozen) Limited / manual No External wrappers
RL Rollout & InfCtx Bitwise learner–rollout parity (fp32io16 + differentiable rl_infctx) No serving rollout Train–infer FP16 mismatch Functional recurrent state
Speculative Decoding Built-in (batched LightMTP DraftLoop, scratch verification, state-only commit and optional per-token checkpoints) No No No

Key Highlights

  • Bitwise Train–Infer Parity on FP32 State: Training, prefill, and decode share the same BF16 forward and FP32 accumulation order (fixed key reduction, no split-K). With FP32 state (fp32io16), RL rollouts, evaluation, and sampling search reproduce the learner's outputs, states, and selected logprobs bitwise.
  • Two Serving State Modes: Use FP16 state with DeltaLog (fp16) on SM89 (RTX 4090) and SM120 (PRO 6000) to halve state VRAM/bandwidth for general serving (per-token FP16 state rounding means outputs can differ from the FP32 learner), or use FP32 state (fp32io16) whenever exact training/RL parity is required.
  • First-Class LoRA & NoRA: Built directly into TMix (receptance, key, value, output), CMix (key, value), and LM Head projections, skipping dW automatically when base weights are frozen.
  • Full-Graph torch.compile & CUDA Graph: All training operators support torch.compile(fullgraph=True) (including backward and LoRA/NoRA); all serving operators support live-capacity CUDA Graph capture and zero-overhead replay.
  • Universal Source Wheel: Ships as a lightweight py3-none-any wheel with no precompiled binaries or architecture fallbacks — TileLang specializes kernels for your active GPU and caches them on disk.

Architecture at a Glance

flowchart LR
    Emb["Embedding LN0"] --> TS["TMix: tokenshift<br/>(PostNorm + 6-way Shift)"]
    TS --> Prep["TMix: wkv_prepare<br/>(R/K/V + LowRank W/A/G/V + VRes)"]
    Prep --> WKV["TMix: wkv7<br/>(FP32 Train / FP16+DeltaLog Serving)"]
    WKV --> RO["TMix: readout<br/>(Head LN + RKV Res + Gate + Out)"]
    RO --> CMix["CMix<br/>(PostNorm + Shift + ReLU² FFN)"]
    CMix --> PN["PostNorm"]
    PN --> Head["LM Head<br/>(FP32 Logits / Chunked CE / L2Wrap)"]
    Head --> Sample["6-Param Sampling<br/>& LightMTP DraftLoop"]

Requirements & Installation

Requirements

  • Linux with a CUDA GPU of Compute Capability 8.0+ (Ampere, Ada SM89, Hopper SM90, Blackwell SM120)
  • Python 3.10+, PyTorch 2.11–2.14, TileLang 0.1.15
  • System CUDA Toolkit matching PyTorch, a C++20 host compiler, and Ninja

Install from PyPI

python -m pip install --pre FlashRWKV2

Optional — prewarm the small native extension used for packed metadata validation and sampling:

python -m flashrwkv2.compile

Development Install (uv)

git clone https://github.com/rwkv-rs/FlashRWKV2.git
cd FlashRWKV2
uv sync --group dev

Importing flashrwkv2 never compiles kernels or creates caches on CPU. TileLang and the native extension compile lazily on first CUDA invocation and persist to disk (~/.cache/flashrwkv2 or $FLASHRWKV2_CACHE_DIR).


Quickstart

1. Training WKV-7 with torch.compile(fullgraph=True)

import torch
import flashrwkv2

B, T, H, D = 2, 1024, 64, 64
C = H * D
device = "cuda"

r, w, k, v, a, b = (
    torch.randn(B, T, C, device=device, dtype=torch.bfloat16, requires_grad=True)
    for _ in range(6)
)
state = torch.zeros(B, H, D, D, device=device, dtype=torch.float32, requires_grad=True)

# Dense [B, T, C] forward + backward with differentiable FP32 state
y, final_state = flashrwkv2.pretrain_tmix_wkv7_recurrent_bf16(
    r, w, k, v, a, b,
    head_size=D,
    initial_state=state,
    return_final_state=True,
)
y.sum().backward()

To preserve exact BF16 intermediate rounding under torch.compile(fullgraph=True):

compiled_model = torch.compile(
    model,
    fullgraph=True,
    options={"emulate_precision_casts": True},
)

2. Packed Inference with FP16 Serving State

import torch
import flashrwkv2

pool_slots, H, D = 64, 64, 64
device = "cuda"

# Bind a contiguous FP16 state pool (preferred for SM89 / SM120 serving)
raw_pool = torch.zeros(pool_slots, H, D, D, device=device, dtype=torch.float16)
state_handle = flashrwkv2.prepare_tmix_wkv7_recurrent_fp16_state_from_tensor(raw_pool)

# Packed batch: seq 0 has 128 tokens (prefill), seq 1 has 1 token (decode)
cu_seqlens = torch.tensor([0, 128, 129], device=device, dtype=torch.int32)
state_indices = torch.tensor([3, 7], device=device, dtype=torch.int32)
N = int(cu_seqlens[-1])

r, w, k, v, a, b = (
    torch.randn(N, H, D, device=device, dtype=torch.bfloat16) for _ in range(6)
)

# Validate once and reuse the ticket across all layers in the step
ticket = flashrwkv2.prepare_tmix_wkv7_recurrent_metadata(
    cu_seqlens,
    state_indices,
    state_pool_size=pool_slots,
    total_tokens=N,
)

y = flashrwkv2.infer_tmix_wkv7_recurrent_fp16_forward_varlen(
    r, w, k, v, a, b,
    state=state_handle,
    cu_seqlens=cu_seqlens,
    state_indices=state_indices,
    validated_metadata=ticket,
)

# Always materialize before inspecting or copying the bound raw tensor directly
state_handle.materialize_slots_(state_indices)

3. Speculative Drafting with LightMTP DraftLoop

from flashrwkv2.mtp import DraftLoop

loop = DraftLoop.from_state_dict(mtp_weights, lm_head_weight, depth=4)

# CUDA BF16 hidden [B, C] and CUDA int64 token_ids [B]
eager_ids = loop(hidden, token_ids, embed).clone()  # [B, 4]

# Capture separate CUDA Graphs for the batch sizes used by the scheduler
for batch_size in (1, 4, 64, 320, 512):
    loop.capture(embed, batch_size=batch_size)
graph_ids = loop.replay(hidden, token_ids)

Drafting retains only the token IDs across steps. Each step releases its FP32 [B, V] logits after argmax. The loader supports canonical mtp.* weights, legacy block.* sidecars, and optional LoRA/NoRA parameters for the LM head.

For low-memory verification, copy active WKV slots into a single-layer scratch pool and run Verify with save=False. Pack each sequence's accepted_b + 1 tokens for Commit and advance the main pool with r=None. A live metadata ticket limits each graph replay's slot copy to the active rows. See the WKV7 recurrence API.


Choosing a Precision & State Mode

Workload Recommended WKV API State Dtype Why
Pretraining / SFT / LoRA / NoRA pretrain_tmix_wkv7_recurrent_bf16 FP32 Differentiable state, dense or packed, torch.compile(fullgraph=True)
RL Rollout, Eval & Sampling Search infer_tmix_wkv7_recurrent_fp32io16_forward_varlen + rl_infctx_tmix_wkv7_recurrent_bf16 FP32 Bitwise identical outputs, states, and selected logprobs vs learner
General Serving (Prefill + Decode) infer_tmix_wkv7_recurrent_fp16_forward_varlen FP16 (+ DeltaLog) Halved state bandwidth & VRAM, BF16 IO + FP32 math, per-token FP16 rounding

Historical Acceptance & Performance

The figures below are historical acceptance records measured on SM89 (RTX 4090, 128 SMs) and SM120 (RTX PRO 6000, 188 SMs) at commits a239bc3 and 07c7913 (raw paired samples and numerical evidence are archived in artifacts/cmix-acceptance-a239bc3.json and artifacts/sm120-remaining-acceptance-results-07c7913.json). Validate runtime changes on their own commits using the release acceptance procedure.

Historical Acceptance Metric (a239bc3 / 07c7913) SM89 (RTX 4090) SM120 (PRO 6000)
Full block GPU test suite (numerics, state, Graph, compile) 330 / 330 passed 330 / 330 passed
Training workloads within 2% of best internal comparison 9 / 9 9 / 9
Fresh-process disk-cache reuse (cold / warm compilations) 139 cold / 0 warm 137 cold / 0 warm

Selected SM120 training latency from commit 07c7913 (B=1, Layer + LM Head, forward + backward + grad zero, paired ABBA CUDA Graph timing; comparison is the fastest of baseline, earlier offline reference, and previous FlashRWKV2 strategy):

Model & Workload Best Internal Comparison (ms) Prepared Strategy (ms) Change
RWKV-7 1.5B T=128 (torch.compile) 3.603 3.577 -0.71%
RWKV-7 7.2B T=1024 (eager) 32.786 32.196 -1.80%
RWKV-7 13.3B T=128 (eager) 7.399 7.311 -1.20%
RWKV-7 13.3B T=1024 (eager) 32.897 32.267 -1.91%

Running Tests & Benchmarks

# Run full test suite
./.venv/bin/python -m pytest -q

# Benchmark WKV7 across model geometries (with bitwise verification + CUDA Graph timing)
PYTHONPATH=benchmarks ./.venv/bin/python benchmarks/tmix/wkv7/bench.py \
  --shapes h40d64 h64d64 --state-dtype float16 --timing-mode graph \
  --metadata live --slot-order random --state-pool-size 320 \
  --cases 255x1+1x4 255x1+1x256 1x1024

# Benchmark LightMTP DraftLoop (RWKV-7 G1k 7.2B draft shape, B=1/4/64/320/512)
PYTHONPATH=benchmarks ./.venv/bin/python benchmarks/mtp/bench.py --depth 4

Documentation

Document Audience Contents
Quickstart & Recipes Users End-to-end training block, LoRA/NoRA, RL rollout, MTP rewind, benchmarks
Serving & State Guide Serving Engineers FP16 vs FP32 state, DeltaLog memory budgeting, COW pools, live CUDA Graphs
Kernel API & Numerical Contract Integrators Complete operator table, math definitions, shapes, dtypes, compilation rules
Release Acceptance Maintainers Dual-GPU SM89/SM120 verification and manual PyPI publishing
PR Review Guide Contributors Review checklist and upstream consistency rules

Acknowledgements & License

FlashRWKV2 is distributed under the MIT License.

Metadata

Release files for FlashRWKV2 0.2.0a5

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

Source distribution (sdist)

Source distribution for FlashRWKV2 0.2.0a5
File Size Uploaded Provenance Actions
flashrwkv2-0.2.0a5.tar.gz 103.5 kB Without provenance Details for flashrwkv2-0.2.0a5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for FlashRWKV2 0.2.0a5
File Interpreter ABI Platform Actions
flashrwkv2-0.2.0a5-py3-none-any.whl Python 3 none any Details for flashrwkv2-0.2.0a5-py3-none-any.whl

Total release size: 213.8 kB

Release files / flashrwkv2-0.2.0a5.tar.gz

Download URL flashrwkv2-0.2.0a5.tar.gz
Size 103.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4a9858c644665027c7b0c32d34873f6a2ea4704ef2118866722dcdcae80f39c9
BLAKE2b-256 checksum
How to use checksums
d5d8eafc52e463e87101cdf47118a86fd6c81fc604910c95eede53065e0e9242
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / flashrwkv2-0.2.0a5-py3-none-any.whl

Download URL flashrwkv2-0.2.0a5-py3-none-any.whl
Size 110.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db1e9f7650b0a582f62d3259ee8ff38dd0e156697ad19ea881983344b629e544
BLAKE2b-256 checksum
How to use checksums
f055c075b791d3bf94bf1f688336905786cfc301d71dacd4e529e8ab1cbbbffc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for FlashRWKV2 0.2.0a5

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
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