Repository navigation
feat(xla): carry per-slot recurrent state in continuous batching - #3372
Conversation
Mamba2 now serves through mlxcel-server on the xla backend. The ragged decode emitter takes and returns one [B, ...dims] tensor per StateTensorSpec entry and splits and joins rows with dynamic_update_slice, so an attention-free model batches without KV buffers. Hybrids (KV plus recurrent state) stay refused in the emitter and in IreeRaggedLlama::load until #3344. The IREE shim keeps lazily allocated per-slot state buffers, zeroed on each ragged reset and released with the rest of the state. A slot prefill invalidates the slot first, copies KV only when the model has KV, copies each state row into its slot, and marks the slot valid only after every copy succeeds. A failed ragged decode on a stateful model invalidates every slot. Rust slot entries call a new xla_llama_slot_invalidate before validating, so a refused request also leaves no stale state behind. Embeddings and diagnostics entries still refuse attention-free models. The Gemma3n audio config probe now uses the tolerant HF JSON parser, so a config with a bare Infinity (Mamba2) no longer fails server startup. New device tests in tests/xla_ssm_state_carry.rs cover mid-stream admits into recycled slots, half and full batches, torn state stores, refused requests and failed decodes, and one state allocation per reset.
At the f16 hip default the single-sequence Mamba2 decode departs from the HF fp32 oracle after about 16 tokens on some prompts ("In computer science, a linked list is"), while f32 and the batched path match it. That made the serve gate and the server check compare batched output against a drifting reference (7/8 and 3/4 at f16).
`effective_precision` now pins F32 for recurrent configs on every device, as it already did for MLA, for both the single-sequence and the ragged bundles. The decision lives in `Config::pins_f32_precision` and `resolve_pinned_precision_with_override`, so it is unit-tested without the IREE runtime; a recognised explicit `MLXCEL_XLA_PRECISION` still overrides the pin. Docs state that f16 is not token-exact for recurrent models.
Validated on hip at the default precision: CLI 5/5 prompts token-exact vs HF fp32, validate_arch serve gate PASS, xla_batch_bench (64-token prompt, cancel, buckets, B=8) all reference-exact, mlxcel-server 4/4 concurrent requests equal the CLI, xla_ssm_state_carry, xla_ssm_single_seq and xla_ragged_hardening pass.
Refs #3340
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
Re-run at the default precision after the pin, all on hip: CLI 5/5 token-exact vs HF fp32, |
Security and performance review (head 0807595)No CRITICAL, HIGH or MEDIUM findings; no commits pushed. Cross-request isolation holds on every path checked. Every Rust slot entry calls Bounds: the slot is range-checked before any offset; Resource use: the state set is fixed per bucket at LOW, left as is: with a pinned config (MLA, recurrent), an unrecognised Verified on gfx1151 hip: |
Summary
Epic #564 sub-issue U4. Per-slot recurrent state carry in the continuous-batching engine, so Mamba2 serves through
mlxcel-serveron the xla backend.[B, ...dims]tensor perStateTensorSpecentry (ssm_h,ssm_conv).RecurrentLayers::split_rows/join_rowsslice and write back rows withdynamic_update_slice. With no KV, RoPE gathers and masks are skipped. Dense ragged MLIR is byte-identical (validation goldens pass). Hybrids are still refused.xla_iree.c): lazily allocated per-slotsscache_b[]buffers, zeroed on ragged reset and released with the state.xla_store_prefill_slotinvalidates the slot, copies KV only whenhas_kv, copies each state row atslot * row, and sets the slot valid only after every copy succeeds. A failed ragged decode on a stateful model invalidates every slot. New exportxla_llama_slot_invalidate.decode_step.state_importis optional in a bundle; the single-sequence path still refuses at import time.iree.rs):IreeRaggedLlama::loadrefuses only hybrids. Every slot entry goes throughbegin_slot_prefill, which validates the slot and invalidates it before any other check, so a refused request leaves no stale state. Embeddings and diagnostics entries still refuse attention-free models viaxla_require_kv. Test-support counters report batch state allocations.XlaBatchEngine'sSlot,XlaBucketSetand the serve worker need no new fields: the state lives on the device keyed by slot, like the KV.Config::pins_f32_precision). At the old f16 hip default the single-sequence decode drifts from the HF fp32 oracle after about 16 tokens on some prompts ("In computer science, a linked list is"), so f16 is not token-exact for recurrent models. An explicitMLXCEL_XLA_PRECISIONstill overrides the pin.gemma3n_audio_config: uses the tolerant HF JSON parser. Server startup on Mamba2 failed before this withparse config JSON: expected value at line 25(bareInfinity). Non-Mamba2 configs with a bareInfinity/NaNare still refused by the dense and Gemma3n config parsers.docs/supported-models.md,docs/environment-variables.md, mlxcel-xla README,validate_arch.shhelp, and a new section inspike/openxla/FINDINGS_ssm.md.Verification (hip, gfx1151, default precision after the pin)
mlxcel generatemamba2-130m, 40 greedy tokens: 5/5 prompts token-exact vs the HF fp32 oracle ("The capital of France is", "In computer science, a linked list is", "Once upon a time, in a small village by the sea,", "The three primary colors are", "Water boils at a temperature of");xla_oracle_check5/5.validate_arch.sh --batch 4 --requests 8 --maxcap 24: PASS (structural, token-exact, serve reference-exact; 123.7 vs 75.0 tok/s).xla_batch_bench: B=4 with the 64-token prompt PASS (8/8, was 7/8 at f16; 122.8 tok/s);--cancel 2PASS;--buckets 256,512PASS; B=8, 16 requests PASS.mlxcel-server, 4 concurrent temperature-0/v1/completions: 4/4 equal the CLI (was 3/4 at f16).xla_ssm_state_carry5/5,xla_ssm_single_seq6/6,xla_ragged_hardening6/6. Each carry fix reverted fails its test (Rust slot invalidation or decode-failure invalidate-all:refused_requests_and_failed_decodes_invalidate_slot_state; the state row copy: 4 tests; the release in reset:state_allocation_counters_show_one_allocation_per_reset).xla_batch_benchB4 N8 REFERENCE-EXACT PASS both, 64.56 tok/s before and 63.53 tok/s after.cargo fmt --check,cargo clippy --features xla-iree --all-targets -- -D warnings, and mlxcel-xla clippy with and withoutiree: clean.Changes during review
MLXCEL_XLA_PRECISIONstill overrides it.recurrent_config_pins_f32_unless_precision_is_explicitfails with the pin removed. All gates were re-run at the new default (numbers below). Cost: the serve gate's batched throughput on mamba2-130m went from 285.6 to 123.7 tok/s. Left (LOW): a single-sequence bundle withoutdecode_step.state_importnow fails at the first decode rather than at load; the import stays pending, so stale globals are never used.MLXCEL_XLA_PRECISIONkeeps f32 silently where dense configs reject it, as MLA already behaved. Pre-existing, outside this PR: any prefill or decode error stops the whole serve worker, and the request popped in a failed pump is dropped.Follow-ups
has_kvtogether with state; the emitter andIreeRaggedLlama::loadhybrid refusals are the points to lift. Hybrids inherit the f32 pin.Closes #3340