Skip to content

feat(xla): add RWKV7 with a chunked WKV7 scan and token-shift state - #3609

Merged
inureyes merged 2 commits into
mainfrom
feature/issue-3355-rwkv7-xla
Dec 1, 2026
Merged

inureyes merged 2 commits into
mainfrom
feature/issue-3355-rwkv7-xla

Conversation

@inureyes

@inureyes inureyes commented Dec 1, 2026

Copy link
Copy Markdown
Member

Summary

Adds RWKV7 (model_type: rwkv7) to the OpenXLA/IREE backend: the attention-free time mix whose state is a per-head matrix advanced by the WKV7 generalized delta rule, the two token-shift vectors, and the layer-0 value residual the family threads through its blocks.

  • Config and weights: MixerKind::Rwkv7 and WeightScheme::Rwkv7 on every block (has_kv_cache() == false), a rwkv7_config reader for the fla-hub HF layout (the WKV head count is hidden_size / head_dim; HF's num_heads is a different quantity and is ignored as src/models/rwkv7.rs ignores it), quantization and non-sqrelu activations rejected at parse time, and an optional power-of-two xla_chunk_size (default 32).
  • WKV7 kernel in emitter/ssm_rwkv7.rs: per head, S_t = S_{t-1} diag(w_t) + (S_{t-1} a_t) b_t^T + v_t k_t^T and y_t = S_t r_t in f32. Prefill emits a chunked inclusive scan of affine pairs (a row contributes (diag(w_t) + a_t b_t^T, v_t k_t^T), composed as (A1 A2, B1 A2 + B2); padded rows compose the identity), decode emits one step, and ragged decode advances each row's own state, so continuous batching needs no per-slot special case.
  • State ABI (three tensors, in order): wkv_s [L, H, hd, hd], shift_attn [L, hidden], shift_ffn [L, hidden], all f32. Prefill writes the shift rows from row real_len - 1 through dynamic_slice and keeps the inputs when real_len == 0; single-sequence decode keeps them in util.global seeded by @state_import; ragged decode takes and returns them at [B, ...].
  • Block semantics follow the MLX reference exactly: layer-0 pre-norm, x_r/x_w/x_k/x_v/x_a/x_g token shifts, decay exp(-0.606531 * sigmoid(.)), L2-normalized key (eps 1e-7), per-head group norm (eps 6.4e-4), the layer-0 value residual with v_lora, the g_lora sigmoid gate, and the token-shifted relu^2 channel mix.
  • Weights: weight_names plus the new weights::push_rwkv7_layer_specs stay in lock-step with take_layer_weights; the checkpoint's x_* [1,1,H] vectors are squeezed to [H]; the unused *.bias norm tensors are ignored because the MLX reference never reads them.
  • Tests: byte-exact goldens in assets/rwkv7/, config/weight-order/state-ABI unit tests, and two ignored gates (the committed float64 synthetic fixture, and the real checkpoint against the MLX eager oracle).

Test plan

Checkpoint: fla-hub/rwkv7-0.1B-g1 at revision 7450ee04eb9c66baa7161b7a5c0fe09e5b2817d8, bf16 (399 tensors), in models/hf/rwkv7-0.1b. The repo ships no tokenizer.json; the RWKV world tokenizer from SmerkyG/RWKV7-Goose-0.1B-Pile-HF was added so both sides tokenize identically. The oracle was recorded on models/hf/rwkv7-0.1b-f32, a byte-identical bf16 -> f32 widening, because the XLA loader widens bf16 exactly and MLX should then run the same f32 values.

Local (this worktree, base 45ee31712):

  • cargo fmt --check: clean.
  • cargo clippy -p mlxcel-xla --all-targets --features iree -- -D warnings: clean (exit 0).
  • cargo test -p mlxcel-xla --lib: 602 passed, 0 failed, 23 ignored; every pre-existing golden byte-identical.
  • MLXCEL_RWKV7_FIXTURES=spike/openxla/rwkv7_reference/synthetic-fixture MLXCEL_XLA_PRECISION=f32 cargo test --features xla-iree,test-utils --test xla_rwkv7 -- --ignored rwkv7_synthetic_parity: synthetic: tokens exact; worst ragged |logit delta| 1.192e-7 (log at spike/openxla/rwkv7_reference/local-task-synthetic.log); the fixture is committed, so this gate needs neither a GPU nor a checkpoint.
  • make verify-env-var-docs (with its 15 unittest cases), make verify-versions, make verify-kernel-dtype-keys: pass. MLXCEL_FREEZE_RWKV7 is allowlisted as fixture-authoring, following the Falcon-H1 and PLaMo 2 entries.

ROCm gfx1151 (hip), release binaries built from this branch (a later rebuild after the fixture re-freeze produced the same sha256, so the shas below pin the code that ran) (mlxcel sha256 aed05e626031c5a71cbd5b24c88b3fa7bd15572aa07ab93264dce54bf878af56, mlxcel-server sha256 8ddc85656ad240c4da111a7a4914f5cc7bf8bab729ad086b981edea364b5e89f), every GPU command under scripts/rocm_gpu_guard.sh --hold --:

  • scripts/xla/validate_arch.sh --model models/hf/rwkv7-0.1b --device hip --oracle spike/openxla/rwkv7_reference/mlx-eager-oracle.json --batch 4 --requests 8 --maxcap 24 exited 0. The structural pre-gate passed, gate 1 printed RESULT: TOKEN-EXACT PASS (OpenXLA greedy == HF oracle), and gate 2 printed RESULT: REFERENCE-EXACT PASS (every request matches its single-seq reference) with BENCH device=hip B=4 N=8 tok_s=85.367 pass=true. The example labels its reference "HF oracle"; the file is the MLX eager oracle the issue's user decision requires, written in the same JSON shape. Full log: spike/openxla/rwkv7_reference/hip-validate-arch.log.
  • Gate 1 detail: device = hip, prompt = 5 tokens, reference = 40 tokens; the 40 continuations match the oracle ids exactly. Oracle prompt ids [510, 5347, 273, 6181, 310], continuation [3600, 1704, 3600, 1704, 35985, 1704, 1704, 43241, 53792, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704, 1704].
  • Gate 2 detail: phase 2: 46 pump steps, 8 requests served, 170 tokens, throughput: 85.37 tok/s batched (vs 66.01 tok/s sequential baseline, 1.3x).
  • spike/openxla/falcon_h1_cli_server_check.py (the shared CLI and concurrent-server harness used for Falcon-H1 and Jamba): CLI/HF ORACLE: 40/40 TOKEN-EXACT PASS and SERVER/CLI: 8/8 EXACT-TEXT AND TOKEN-COUNT PASS (['nonstream', 'sse']); all four non-streaming and four SSE completions overlapped and equal their separate CLI references in text and token counts. Evidence in spike/openxla/rwkv7_reference/hip-cli-server/.

Closes #3355

@inureyes inureyes added type:enhancement New features, capabilities, or significant additions status:review Under review priority:low Low priority area:architecture Architecture and code structure changes area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Dec 1, 2026
Add model_type rwkv7 to the OpenXLA emitter: MixerKind::Rwkv7 and WeightScheme::Rwkv7 on every block, a config reader for the fla-hub HF layout, and the emitter's WKV7 kernel, block semantics and weight order in new modules (rwkv7_config, ssm_rwkv7, model_rwkv7).

The WKV7 recurrence is emitted as a chunked inclusive scan for prefill and as one step for decode. Each row contributes the affine pair (diag(w_t) + a_t b_t^T, v_t k_t^T), chunks compose as (A1 A2, B1 A2 + B2), and padded rows compose the identity, so a chunk costs log2 batched [head_dim, head_dim] products instead of a per-row unroll. Ragged decode advances each row's own state, so per-slot state carry needs no special case.

The three threaded state tensors are wkv_s [L, H, hd, hd], shift_attn [L, hidden] and shift_ffn [L, hidden], all f32, in that order. Prefill writes the shift rows from real_len - 1 through dynamic_slice and keeps its inputs when real_len is 0; single-sequence decode keeps them in util.globals seeded by @state_import; ragged decode takes and returns them at [B, ...].

Weight names and weight_specs_q stay in lock-step with take_layer_weights, the checkpoint's [1, 1, hidden] shift vectors are squeezed to rank 1 at load, and the norm biases the MLX reference never reads are ignored. Goldens are registered under assets/rwkv7; the unit tests cover the config, the weight order and the state ABI.

Gates: byte-exact goldens and the 602-test mlxcel-xla lib suite, a committed float64 synthetic fixture on local-task (tokens exact, worst ragged logit delta 1.19e-7), and on ROCm gfx1151 the fla-hub rwkv7-0.1B-g1 checkpoint through validate_arch.sh on hip, the CLI against its MLX eager oracle, and four concurrent non-streaming plus four SSE server completions against their CLI references.
@inureyes
inureyes force-pushed the feature/issue-3355-rwkv7-xla branch from 9b86cc1 to ce64270 Compare December 1, 2026 08:45
@inureyes
inureyes marked this pull request as ready for review December 1, 2026 08:47
@inureyes
inureyes merged commit ddff4a3 into main Dec 1, 2026
35 of 36 checks passed
@inureyes
inureyes deleted the feature/issue-3355-rwkv7-xla branch December 1, 2026 08:47
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 1, 2026
inureyes pushed a commit that referenced this pull request Dec 1, 2026
Two seeded synthetic checkpoints made main fail the binary-assets gate. The RWKV7 one from #3609 is removed from Git and ignored: spike/openxla/rwkv7_oracle.py regenerates it from seed 3355, and the two #3609 reports now show how to build the fixture outside the checkout before the parity gate. Regeneration on GB10 (aarch64) reproduced SHA-256 6b3b9811...d828c byte for byte, with identical greedy tokens.

The Kimi-Linear checkpoint from #3612 is declared instead, with a 448 KiB ceiling and the total budget raised by its 390,720 bytes, because regenerating it needs a full MLX build and its committed oracle and gate logs were measured against these exact bytes.

Refs #3609, #3612
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:architecture Architecture and code structure changes area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(xla): RWKV7 (WKV7 generalized delta kernel + token-shift state)

1 participant