Skip to content

fix(xla): keep recurrent models out of prefix-hash caches - #3379

Merged
inureyes merged 9 commits into
mainfrom
fix/issue-3341-recurrent-no-prefix-share
Nov 24, 2026
Merged

inureyes merged 9 commits into
mainfrom
fix/issue-3341-recurrent-no-prefix-share

Conversation

@inureyes

@inureyes inureyes commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

Epic #564 U5. Recurrent models now provably never take part in prefix-hash sharing, on OpenXLA and on MLX.

  • OpenXLA guard (src/server/xla_prompt_cache_guard.rs). The decision is Config::has_recurrent_state() (new mlxcel_xla::config_json_has_recurrent_state) or membership in HYBRID_SSM_MODEL_TYPES, so families the emitter cannot load yet are refused too. It is enforced in two places. startup::resolve_prompt_cache_store builds no store and clears prompt_cache.enabled and apc.enabled, logging one info line that names the model type. Because of that, neither AppState nor the provider holds a store, and /v1/cache/stats reports enabled: false. ModelProvider's OpenXLA arm also drops a store that any other caller passes in. The PromptCacheWarmup drop in xla_worker_admission.rs is unchanged and now points at the guard.
  • Widened MLX list. Adds falcon_h1, plamo2 (+plamo2vl), qwen3_5 (+qwen3_5_text, qwen3_5_moe, qwen3_5_moe_text) and granitemoehybrid, plus architecture fragments for each.
  • The startup detector now parses configs that contain HF's bare Infinity. Before this, HF Mamba2 and Nemotron-H configs failed the strict parse and kept APC on.

What list membership does on MLX

The list has one consumer, the startup APC opt-out (now disable_apc_for_hybrid_ssm). It sets apc.enabled = false and does nothing else. APC is read only by the dense KV store: the block-hash chain on insert, partial-match lookup, apc_stats and /v1/cache/stats. It does not touch snapshots, #3047 state checkpoints, paged KV or batching.

The four families already keep their state outside the KV slice. Falcon-H1, PLaMo 2 and Granite 4.0-H are ModelOwned, and Qwen3.5 is SchedulerOwned. So the scheduler skips the dense lookup and the dense donate for them, whether or not they batch. In practice the widening changes the startup warning and the reported apc_enabled: false, because --apc-enabled defaults to true. #3047 checkpoints keep working: state_checkpoints_do_not_depend_on_apc gets the same checkpoint hit with APC on and off.

Changes during review

  • Review (2bf5a57): the startup wiring was untested (disabling the guard call in startup::resolve_prompt_cache_store left every guard test green). startup_resolver_builds_no_store_for_an_xla_recurrent_model runs the resolver under MLXCEL_BACKEND=xla and fails with the call removed. Left (LOW): the guard logs its disable line even when the prompt cache was never enabled; the ModelProvider OpenXLA-arm drop has no direct test (it needs a real XLA worker and startup already passes None).
  • Security review (c0566f6): xla_recurrent_family returned "may share" when config.json was missing or unparseable, so the store stayed attached (fail-open; not exploitable because such a model cannot load and the XLA worker never reads the store). It now fails closed and disables the cache and APC. an_unreadable_or_unparseable_config_fails_closed fails without the fix. Cross-tenant paths (startup, in-process, router children, runtime settings PATCH, /v1/cache), crafted configs and env mismatches were checked with no further finding.
  • Finalize (58ed302, a95dd22): merged origin/main without conflict; the resolver test restores MLXCEL_BACKEND through a drop guard so a panic cannot leak it into later tests.

Test plan

  • cargo test --profile test-fast --lib prompt_cache (382 pass) and the same with --features xla-iree plus xla_prompt_cache_guard (386 pass), run with MLXCEL_REQUIRE_MODELS=1. Covers the 13 epic types through the XLA guard and the shared list, tiny and local HF mamba2, and the local qwen3.5-0.8b and granite-4.0-h-tiny configs.
  • mlxcel-xla test config_json_has_recurrent_state_matches_the_config
  • XLA server (hip), mamba2-130m with --prompt-cache-enabled=true --apc-enabled=true --metrics, two completions sharing all but the last few of their 68 prompt tokens. Prompt-cache hits, misses and snapshot hits are all 0, /v1/cache/stats reports enabled: false, and both outputs equal mlxcel generate.
  • MLX ROCm server, qwen3.5-0.8b-4bit and granite-4.0-h-tiny-4bit, with three completions each (two that share a prefix, then one that extends request 1). Startup logs the APC opt-out, and the stats show apc_enabled: false, 0 dense lookups and 1 snapshot hit (68 tokens). Every output is byte-identical to origin/main with APC on. Requests 1 and 2 equal --no-prompt-cache and the CLI. Granite request 3 also matches. Qwen3.5 request 3 (the snapshot hit) parts from the cold run at about token 9 on both main and this branch. That is the known two-forward partition effect, not this change.
  • Falcon-H1 and PLaMo 2 have no local checkpoint; they are covered by the config-detection unit tests only.
  • cargo fmt --check, cargo clippy --all-targets -D warnings, --features xla-iree --all-targets, and -p mlxcel-xla with and without iree

Closes #3341

A saved token prefix cannot restore recurrent state. On the OpenXLA backend this held only because the worker drops prompt-cache warmups, while ModelProvider still attached the store. A new guard (server::xla_prompt_cache_guard) decides from the emitter's Config::has_recurrent_state() (new mlxcel_xla::config_json_has_recurrent_state) or the shared hybrid list: startup builds no store and turns APC off with one info line, and the provider's OpenXLA arm drops any store passed to it.

HYBRID_SSM_MODEL_TYPES now also names qwen3_5 (with _moe and _text spellings), granitemoehybrid, falcon_h1 and plamo2/plamo2vl. On MLX membership only turns APC off at startup; these families keep state outside the KV slice, so they never reached the block-hash store, and snapshots and #3047 state checkpoints do not read the APC flag (a new test shows the same checkpoint hit with APC on and off). The startup detector now tolerates HF's bare Infinity, which left APC on for HF Mamba2 and Nemotron-H configs.

Validated with the prompt_cache and guard unit tests, and an XLA mamba2-130m server with prompt cache and APC requested: zero cache lookups and hits, both outputs equal the CLI.

Closes #3341
The emitter module only compiles with the iree feature or in tests, so mlxcel-xla without iree failed to build. The root crate calls the function only under xla-iree.
@inureyes inureyes added status:review Under review type:test Test related changes priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 24, 2026
The guard tests exercised only the helpers in xla_prompt_cache_guard, so dropping the call from startup::resolve_prompt_cache_store left every test green while a recurrent OpenXLA model got a store again. The new test runs the resolver under MLXCEL_BACKEND=xla (env lock held): tiny Mamba2 and a listed granitemoehybrid config get no store with prompt cache and APC cleared, and a dense Llama config keeps its store and APC. With the startup call disabled it fails with (true, true, false) for Mamba2.

Refs #3341
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Keep every recurrent model out of prefix-hash sharing: an OpenXLA guard plus the widened shared HYBRID_SSM_MODEL_TYPES list (#3341).

Findings Addressed

  • The startup wiring of the XLA guard was untested: removing the call from resolve_prompt_cache_store left all guard tests green. Added startup_resolver_builds_no_store_for_an_xla_recurrent_model (2bf5a57), which fails with that call disabled. (MEDIUM)

Remaining Items

  • The XLA guard logs "prompt cache and APC disabled" even when the operator never enabled the prompt cache. Cosmetic. (LOW)
  • ModelProvider's OpenXLA-arm drop has no direct test; it needs a real XLA worker, and startup already passes it None. (LOW)

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass

APC is read only by the dense store (insert chain, lookup_longest_prefix, apc_stats, /v1/cache/stats). The snapshot and checkpoint lookups never read it, and the list has a single consumer. All four new families report ModelOwned or SchedulerOwned layouts, so the scheduler already skipped dense adopt and donate for them. The new fragments match no attention-only config among the local checkpoints. Store construction goes only through resolve_prompt_cache_store (server and in-process). Router mode passes None, and no runtime endpoint enables the cache.

I reverted each change locally. Without the list and sanitizer changes, 6 tests fail. With the guard neutralized, 3 guard tests fail. Run on gfx1151: --features xla-iree --lib prompt_cache xla_prompt_cache_guard (386 pass with MLXCEL_REQUIRE_MODELS=1, then all 5 guard tests with the new one), plus fmt and clippy -D warnings for default, xla-iree, and -p mlxcel-xla with and without iree.

The OpenXLA prompt-cache guard returned "may share prefixes" when config.json was missing or did not parse, so startup and the provider's OpenXLA arm kept a store attached to a model whose recurrence the guard never checked. The serve engine parses the same text with the same parser and never consumes the store today, so nothing leaked, but an ambiguous config now leaves the cache and APC off instead of on.

New test an_unreadable_or_unparseable_config_fails_closed covers a missing and a truncated config.json; it fails on the previous guard. The guard and hybrid_ssm tests pass under --features xla-iree.

Refs #3341
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review

No CRITICAL, HIGH or MEDIUM findings. One LOW hardening fixed in c0566f6.

  • LOW, fixed (c0566f6): xla_recurrent_family treated an unreadable or unparseable config.json as "may share prefixes", so startup and the provider's OpenXLA arm kept the store attached. It now fails closed: cache and APC off. Not exploitable before, because the serve engine parses the same text with the same parser (RuntimeConfig::from_json -> Config::from_json_str), so such a model cannot load. The new test an_unreadable_or_unparseable_config_fails_closed fails on the previous guard.

Checked, no change needed:

  • Cross-tenant leakage on XLA: the XLA worker never reads the store, and it drops PromptCacheWarmup. Startup and in-process both go through resolve_prompt_cache_store, so AppState and the provider get the same None. prompt_cache is read-only in runtime settings, so a PATCH cannot rebuild a store. The /v1/cache routes see None.
  • Detection cannot fail open on a crafted config: has_recurrent_state() comes from the same Config the engine serves. Layer counts are capped (MAX_CONFIG_LAYERS), and conv_kernel < 2 is rejected before conv_kernel - 1. sanitize_config_json only replaces substrings that cannot appear in any listed family name.
  • Env: the guard and the OpenXLA arm call the same xla_serve_backend_selected(), and non-test code never sets MLXCEL_BACKEND. A mismatch at worst turns the cache off on MLX.
  • MLX: this PR only narrows APC. Snapshot and feat(prompt-cache): checkpoint only recurrent state at turn boundaries for hybrid models #3047 checkpoint lookups are unchanged and keyed as before.
  • Performance: detection runs at startup and at provider construction only, and reads one small file each time.

Validation: guard and hybrid_ssm tests, prompt_cache lib tests (--features xla-iree, MLXCEL_REQUIRE_MODELS=1), and clippy -D warnings (default and xla-iree, --all-targets).

…r test

startup_resolver_builds_no_store_for_an_xla_recurrent_model restored MLXCEL_BACKEND by hand after the assertions' inputs were computed, so a panic in the resolver would leave the variable set for later tests. A BackendEnvGuard now sets it and restores the previous value on drop, under the crate-wide env lock.

Refs #3341
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 24, 2026
@inureyes
inureyes merged commit c5479db into main Nov 24, 2026
64 of 65 checks passed
@inureyes
inureyes deleted the fix/issue-3341-recurrent-no-prefix-share branch November 24, 2026 06:45
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:test Test related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(xla): recurrent models never share prefix-hash caches (XLA + MLX)

1 participant