Skip to content

feat(xla): add RecurrentGemma RG-LRU and sliding attention - #3606

Merged
inureyes merged 3 commits into
mainfrom
feature/issue-3356-recurrent-gemma
Dec 1, 2026
Merged

inureyes merged 3 commits into
mainfrom
feature/issue-3356-recurrent-gemma

Conversation

@inureyes

@inureyes inureyes commented Dec 1, 2026

Copy link
Copy Markdown
Member

Summary

RecurrentGemma (Griffin) now runs on the OpenXLA/IREE backend: block_types selects per-layer mixers (RG-LRU recurrent blocks interleaved with local sliding-window attention), and every block ends in the same gated GELU MLP built at intermediate_size // 2.

  • emitter/recurrent_gemma_config.rs parses model_type = "recurrent_gemma" (accepting the checkpoint's _block_types spelling), maps the schedule to Config::layer_mixers, derives head_dim as hidden_size / num_attention_heads, folds partial_rotary_factor into the rotary width, and validates every checkpoint tensor shape.
  • emitter/ssm_rglru.rs reuses issue feat(xla): Mamba1 selective scan; Mamba and Falcon-Mamba end to end #3348's diagonal scan with N = 1: diag_scan_prefill for the prompt, diag_scan_step for the decode, over log_a = -8 * recurrent_gate * softplus(recurrent_param) and bx = input_gate * conv1d(linear_x(x)) * sqrt(1 - exp(2 log_a)). Position 0 is HF's reset (zero decay, unit multiplier); padded prefill rows are neutral; the per-head block-diagonal gates are one batched f32 dot_general.
  • State ABI: rglru_h [L_rec, lru_width, 1] then rglru_conv [L_rec, conv1d_width - 1, lru_width]. The KV cache is compacted over attention layers only, and is_sliding_layer marks every attention layer local on the original layer index. Gemma (1 + w) norm, the bf16-rounded sqrt(hidden) embedding scale, and logit soft-capping reuse the existing Gemma paths.
  • RecurrentLayers derives per-layer slice shapes from Config::state_tensors, so SSM and RG-LRU share one threading path; previous goldens stay byte-identical. Weight names and weight_specs_q stay in lock-step, and a synthetic tiny fixture joins the byte-exact structural gate.

Test plan

  • cargo test -p mlxcel-xla --lib recurrent_gemma and emitter:: (360 passed)
  • cargo test -p mlxcel-xla --lib validation::tests: new fixture byte-exact, all previous goldens unchanged
  • cargo fmt --check; cargo clippy -p mlxcel-xla --lib --tests -- -D warnings
  • scripts/xla/validate_arch.sh --model models/hf/recurrentgemma-2b --device hip --batch 4 --requests 8 --maxcap 24 --skip-structural on halo: token-exact single-seq PASS (40 tokens vs the HF fp32 oracle) and serve reference-exact PASS (8 requests over 4 slots). The structural pre-gate was skipped there because it passed locally.

Not run: the standalone *_cli_server_check.py HTTP/SSE gate and a separate synthetic-CLI oracle. The real-checkpoint gates above exercise the same engine on hip and are stronger.

Closes #3356

@inureyes inureyes added the status:review Under review label Dec 1, 2026
@inureyes
inureyes force-pushed the feature/issue-3356-recurrent-gemma branch from 8fbc620 to 9dfcbc9 Compare December 1, 2026 07:32
RecurrentGemma (Griffin) interleaves RG-LRU recurrent blocks with local sliding-window attention (block_types, default recurrent, recurrent, attention), and every block ends in the same gated GELU MLP built at intermediate_size // 2. The config reader maps block_types (and the checkpoint's underscore-prefixed _block_types) onto Config::layer_mixers, derives head_dim as hidden_size / num_attention_heads like HF, halves intermediate_size, folds partial_rotary_factor into the rotary width, and marks every attention layer local through Config::is_sliding_layer evaluated on the original layer index.

The recurrent mixer is issue #3348's diagonal scan with N = 1: emitter/ssm_rglru.rs reuses diag_scan_prefill for the prompt and diag_scan_step for the decode over log_a = -8 * recurrent_gate * softplus(recurrent_param) and bx = input_gate * conv1d(linear_x(x)) * sqrt(1 - exp(2 log_a)). The per-head block-diagonal gates run a batched f32 dot_general. HF's position-0 reset is folded in as a zero decay (which makes the multiplier one), and padded prefill rows get a unit decay and a zero input term. The state ABI is rglru_h [L_rec, lru_width, 1] then rglru_conv [L_rec, conv1d_width - 1, lru_width]. The KV cache is compacted over attention layers only. Gemma RMSNorm (1 + w), the sqrt(hidden) embedding scale (rounded to bf16 as HF keeps its normalizer buffer), and logit soft-capping reuse the existing Gemma paths.

RecurrentLayers now derives its per-layer slice shapes from Config::state_tensors instead of the SsmConfig, so the same threading serves the SSM and RG-LRU families byte-identically for the existing ones. Weight names and weight_specs_q stay in lock-step, the checkpoint cross-check validates every RecurrentGemma tensor shape, and a synthetic tiny fixture with frozen decode/prefill/ragged goldens joins the byte-exact structural gate.

Validated with the emitter library tests, the byte-exact structural gate (all previous goldens unchanged), clippy -D warnings, cargo fmt, and scripts/xla/validate_arch.sh on hip against google/recurrentgemma-2b (token-exact single sequence and reference-exact ragged serve).

Refs #3356
MLXCEL_FREEZE_RECURRENT_GEMMA and MLXCEL_RECURRENTGEMMA_MODEL are read only
from #[cfg(test)] code in emitter/recurrent_gemma_tests.rs, matching the
existing Falcon-H1, PLaMo 2, Jamba and Nemotron-H entries. verify-env-var-docs
fails without them.
@inureyes
inureyes force-pushed the feature/issue-3356-recurrent-gemma branch from 1904d72 to 0179f20 Compare December 1, 2026 08:55
@inureyes
inureyes marked this pull request as ready for review December 1, 2026 08:56
@inureyes
inureyes merged commit fed36d6 into main Dec 1, 2026
35 of 36 checks passed
@inureyes
inureyes deleted the feature/issue-3356-recurrent-gemma branch December 1, 2026 08:56
@inureyes inureyes added status:done Completed area:architecture Architecture and code structure changes area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:low Low priority type:enhancement New features, capabilities, or significant additions and removed status:review Under review labels Dec 1, 2026
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): RecurrentGemma (RG-LRU + local sliding attention)

1 participant