vLLM adapter: index kv_caches through layer_name_to_kvcache_index - #5139
Open
sierraisland wants to merge 2 commits into
Open
vLLM adapter: index kv_caches through layer_name_to_kvcache_index#5139sierraisland wants to merge 2 commits into
sierraisland wants to merge 2 commits into
Conversation
tpu-inference allocates one physical cache per unique slot and reports the layer -> cache mapping via layer_name_to_kvcache_index, which the runner passes to every JAX model as a static positional argument. The native models (e.g. Gemma4) look their caches up through it; the MaxText adapter dropped it into *args and let the decoder index the physical list positionally (kv_caches[lyr]). That assumption breaks whenever slot order differs from layer order. With the current vLLM hybrid tensor layout (tpu-inference AI-Hypercomputer#3481) all Mamba/GDN caches are allocated before the attention caches, so on Qwen3.5 an attention layer received a 3D conv state and the RPA shard_map failed with a rank mismatch. It is also wrong for KV-sharing layers and the legacy aliased layout, where the list is shorter than the layer count. The adapter now names inputs_embeds / input_positions / layer_name_to_kvcache_index explicitly, gathers a layer-ordered view of the cache list before the forward pass, and scatters the updated caches back into the runner's physical order afterwards (same length and order, so donation and out_shardings are unchanged). With no mapping it keeps positional indexing. The gather/scatter is Python list indexing on traced values and adds no XLA ops. Adds CPU unit tests for the remapping helpers.
sierraisland
requested review from
A9isha,
NuojCheng,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
khatwanimohit,
richjames0,
shralex,
shuningjin,
vipannalla and
xibinliu
as code owners
September 4, 2026 02:18
There was a problem hiding this comment.
Code Review
This pull request implements mapping, gathering, and scattering utilities to translate between the physical slot order of KV caches from the tpu-inference runner and the layer-ordered view expected by MaxText decoders. It updates the vLLM adapter's forward pass to use these utilities and adds comprehensive unit tests to verify the remapping logic. There are no review comments, so I have no feedback to provide.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
khatwanimohit
approved these changes
Sep 4, 2026
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.
Description
This PR ensures MaxText's vLLM adapter correctly maps physical KV caches to decoder layers via
layer_name_to_kvcache_index, rather than assuming physical slot order matches layer order.Related PRs & Context:
Background & Problem
tpu-inferenceallocates one physical KV cache per unique slot and reports which cache each layer uses vialayer_name_to_kvcache_index, which the runner passes to every JAX model as a static positional argument (kv_caches, input_ids, attention_metadata, inputs_embeds, input_positions, layer_name_to_kvcache_index, ...). Native models (e.g. Gemma4) consume it directly; the MaxText adapter previously dropped the map into*argsand let the decoder index the physical list positionally (kv_caches[lyr]).That assumption broke with the vLLM hybrid tensor layout (tpu-inference #3481): all Mamba/GDN caches are allocated before the attention caches. Consequently, on Qwen3.5, an attention layer received a 3D recurrent conv state and the RPA
shard_mapfailed with:While vllm-project/tpu-inference#3512 provided a temporary workaround by re-sorting slots in the cache manager, honoring
layer_name_to_kvcache_indexin MaxText is the principled solution. It also correctly handles cases that sorting cannot fix (e.g., KV-sharing layers where multiple layers redirect to the same physical cache, and lists shorter than the layer count).Changes in this PR:
MaxTextForCausalLM.__call__explicitly acceptsinputs_embeds,_input_positions, and_layer_name_to_kvcache_indexin positional alignment with the runner contract, with# pylint: disable=keyword-arg-before-vararg.hybrid_cache_utils.py):resolve_layer_kv_cache_indices: Parseslayer.{i}entries into per-layer physical indices with contiguity, string type, and range validation.gather_layer_kv_caches: Builds the layer-ordered view the MaxText decoder expects.scatter_layer_kv_caches: Validates length consistency and writes updated caches back into the runner's physical slot order (preserving runner donation andout_shardings).Tests
MaxTextForCausalLMon TPU with atpu-inferencebuild without the [JAX] Replace jnp.clip(..., a_min=..., a_max=...) with jnp.clip(..., min=..., max=...). #3512 slot-sorting workaround (reproducing and verifying the root fix)Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.