Repository navigation
feat(xla): add RWKV7 with a chunked WKV7 scan and token-shift state - #3609
Merged
Merged
Conversation
4 tasks done
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
force-pushed
the
feature/issue-3355-rwkv7-xla
branch
from
December 1, 2026 08:45
9b86cc1 to
ce64270
Compare
inureyes
marked this pull request as ready for review
December 1, 2026 08:47
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
26 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.MixerKind::Rwkv7andWeightScheme::Rwkv7on every block (has_kv_cache() == false), arwkv7_configreader for the fla-hub HF layout (the WKV head count ishidden_size / head_dim; HF'snum_headsis a different quantity and is ignored assrc/models/rwkv7.rsignores it), quantization and non-sqreluactivations rejected at parse time, and an optional power-of-twoxla_chunk_size(default 32).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^Tandy_t = S_t r_tin 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.wkv_s [L, H, hd, hd],shift_attn [L, hidden],shift_ffn [L, hidden], all f32. Prefill writes the shift rows from rowreal_len - 1throughdynamic_sliceand keeps the inputs whenreal_len == 0; single-sequence decode keeps them inutil.globalseeded by@state_import; ragged decode takes and returns them at[B, ...].x_r/x_w/x_k/x_v/x_a/x_gtoken shifts, decayexp(-0.606531 * sigmoid(.)), L2-normalized key (eps 1e-7), per-head group norm (eps 6.4e-4), the layer-0 value residual withv_lora, theg_lorasigmoid gate, and the token-shifted relu^2 channel mix.weight_namesplus the newweights::push_rwkv7_layer_specsstay in lock-step withtake_layer_weights; the checkpoint'sx_*[1,1,H]vectors are squeezed to[H]; the unused*.biasnorm tensors are ignored because the MLX reference never reads them.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-g1at revision7450ee04eb9c66baa7161b7a5c0fe09e5b2817d8, bf16 (399 tensors), inmodels/hf/rwkv7-0.1b. The repo ships notokenizer.json; the RWKV world tokenizer fromSmerkyG/RWKV7-Goose-0.1B-Pile-HFwas added so both sides tokenize identically. The oracle was recorded onmodels/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 atspike/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_RWKV7is 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) (mlxcelsha256aed05e626031c5a71cbd5b24c88b3fa7bd15572aa07ab93264dce54bf878af56,mlxcel-serversha2568ddc85656ad240c4da111a7a4914f5cc7bf8bab729ad086b981edea364b5e89f), every GPU command underscripts/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 24exited 0. The structural pre-gate passed, gate 1 printedRESULT: TOKEN-EXACT PASS (OpenXLA greedy == HF oracle), and gate 2 printedRESULT: REFERENCE-EXACT PASS (every request matches its single-seq reference)withBENCH 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.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].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 PASSandSERVER/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 inspike/openxla/rwkv7_reference/hip-cli-server/.Closes #3355