Repository navigation
feat(xla): add RecurrentGemma RG-LRU and sliding attention - #3606
Merged
Merged
Conversation
4 tasks
inureyes
force-pushed
the
feature/issue-3356-recurrent-gemma
branch
from
December 1, 2026 07:32
8fbc620 to
9dfcbc9
Compare
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
force-pushed
the
feature/issue-3356-recurrent-gemma
branch
from
December 1, 2026 08:55
1904d72 to
0179f20
Compare
inureyes
marked this pull request as ready for review
December 1, 2026 08:56
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
RecurrentGemma (Griffin) now runs on the OpenXLA/IREE backend:
block_typesselects per-layer mixers (RG-LRU recurrent blocks interleaved with local sliding-window attention), and every block ends in the same gated GELU MLP built atintermediate_size // 2.emitter/recurrent_gemma_config.rsparsesmodel_type = "recurrent_gemma"(accepting the checkpoint's_block_typesspelling), maps the schedule toConfig::layer_mixers, deriveshead_dimashidden_size / num_attention_heads, foldspartial_rotary_factorinto the rotary width, and validates every checkpoint tensor shape.emitter/ssm_rglru.rsreuses issue feat(xla): Mamba1 selective scan; Mamba and Falcon-Mamba end to end #3348's diagonal scan withN = 1:diag_scan_prefillfor the prompt,diag_scan_stepfor the decode, overlog_a = -8 * recurrent_gate * softplus(recurrent_param)andbx = 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 f32dot_general.rglru_h [L_rec, lru_width, 1]thenrglru_conv [L_rec, conv1d_width - 1, lru_width]. The KV cache is compacted over attention layers only, andis_sliding_layermarks every attention layer local on the original layer index. Gemma(1 + w)norm, the bf16-roundedsqrt(hidden)embedding scale, and logit soft-capping reuse the existing Gemma paths.RecurrentLayersderives per-layer slice shapes fromConfig::state_tensors, so SSM and RG-LRU share one threading path; previous goldens stay byte-identical. Weight names andweight_specs_qstay in lock-step, and a synthetic tiny fixture joins the byte-exact structural gate.Test plan
cargo test -p mlxcel-xla --lib recurrent_gemmaandemitter::(360 passed)cargo test -p mlxcel-xla --lib validation::tests: new fixture byte-exact, all previous goldens unchangedcargo fmt --check;cargo clippy -p mlxcel-xla --lib --tests -- -D warningsscripts/xla/validate_arch.sh --model models/hf/recurrentgemma-2b --device hip --batch 4 --requests 8 --maxcap 24 --skip-structuralon 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.pyHTTP/SSE gate and a separate synthetic-CLI oracle. The real-checkpoint gates above exercise the same engine onhipand are stronger.Closes #3356