refactor(attention): add default get_kv_cache_stride_order implementation#34742
Open
timon0305 wants to merge 1 commit intovllm-project:mainfrom
Open
refactor(attention): add default get_kv_cache_stride_order implementation#34742timon0305 wants to merge 1 commit intovllm-project:mainfrom
timon0305 wants to merge 1 commit intovllm-project:mainfrom
Conversation
…tion Fixes vllm-project#33951 Signed-off-by: timon0305 <timon0305@outlook.com>
Contributor
There was a problem hiding this comment.
Code Review
The pull request refactors the AttentionBackend.get_kv_cache_stride_order method to provide a default implementation, eliminating the need for try/except blocks at call sites. This change simplifies the codebase and improves clarity by centralizing the default behavior. The modifications correctly remove redundant error handling and align with the new default implementation. The changes are well-tested and address the identified issue effectively.
Comment on lines
+336
to
+337
| stride_order = backend_class.get_kv_cache_stride_order() | ||
| assert len(stride_order) == len(cache_shape) |
Contributor
Comment on lines
+111
to
+112
| kv_cache_stride_order = attn_backend.get_kv_cache_stride_order() | ||
| assert len(kv_cache_stride_order) == len(kv_cache_shape) |
Contributor
Comment on lines
+345
to
+347
| kv_cache_stride_order = self.attn_backend.get_kv_cache_stride_order( | ||
| include_num_layers_dimension=self._cross_layers_blocks | ||
| ) |
Contributor
Comment on lines
+101
to
+104
| The default implementation returns the identity permutation (physical | ||
| layout matches the logical shape) for the standard 5-dimensional | ||
| KV cache shape. Backends that need a custom memory layout should | ||
| override this method. |
Contributor
Comment on lines
+120
to
+124
| # Standard KV cache has 5 dimensions. | ||
| num_dims = 5 | ||
| if include_num_layers_dimension: | ||
| num_dims += 1 | ||
| return tuple(range(num_dims)) |
Contributor
There was a problem hiding this comment.
Comment on lines
+102
to
+103
| kv_cache_stride_order = attn_backend.get_kv_cache_stride_order() | ||
| assert len(kv_cache_stride_order) == len(kv_cache_shape) |
Contributor
Comment on lines
+5880
to
+5881
| kv_cache_stride_order = attn_backend.get_kv_cache_stride_order() | ||
| assert len(kv_cache_stride_order) == len(kv_cache_shape) |
Contributor
| 2. A KV connector is configured, and the KV connector instance prefers | ||
| to use this layout (prefer_cross_layer_blocks() returns True) | ||
| 2. The flash attention backend supports this layout | ||
| 3. The attention backend supports this layout |
Contributor
Comment on lines
+169
to
+171
| kv_cache_stride_order = attn_backend.get_kv_cache_stride_order( | ||
| include_num_layers_dimension=True | ||
| ) |
Contributor
Comment on lines
+236
to
+239
| kv_cache_stride_order = attn_backend.get_kv_cache_stride_order( | ||
| include_num_layers_dimension=True | ||
| ) | ||
| assert len(kv_cache_stride_order) == len(kv_cache_shape) |
Contributor
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.
Purpose
Provide a default implementation for
AttentionBackend.get_kv_cache_stride_order()that returns the identity permutation, eliminating the need fortry/except (AttributeError, NotImplementedError)fallbacks at every call site.Previously, the base method raised
NotImplementedError, forcing 7 call sites across the codebase to wrap calls intry/exceptblocks with manual fallback totuple(range(len(shape))). This was flagged in #33951 and the related FIXME comment inattn_utils.py.The default returns the identity permutation for the standard 5-dimensional KV cache shape (or 6 with layers dimension). Backends that need custom memory layouts (FlashAttention, FlashInfer, Triton, MLA, etc.) already override this method and are unaffected.
Fixes #33951
Changes
vllm/v1/attention/backend.py: Replaceraise NotImplementedErrorwith a default identity permutationvllm/v1/worker/gpu_model_runner.py: Remove try/except fallbackvllm/v1/worker/kv_connector_model_runner_mixin.py: Remove 2 try/except fallbacks, fix numbering in docstringvllm/v1/worker/gpu/attn_utils.py: Remove try/except fallback and FIXME commentvllm/v1/kv_offload/worker/cpu_gpu.py: Remove try/except fallbackvllm/distributed/kv_transfer/kv_connector/utils.py: Remove try/except fallbackbenchmarks/attention_benchmarks/runner.py: Remove try/except fallbackTest Plan
Test Result
Essential Elements of an Effective PR Description Checklist
supported_models.mdandexamplesfor a new model.