[Attention] Remove cascade attention (#61126)
Signed-off-by: Woosuk Kwon <woosuk@inferact.ai> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5b0b8f251e
commit
e1a5086256
@@ -115,7 +115,6 @@ from vllm.utils.gpu_sync_debug import gpu_sync_allowed
|
||||
from vllm.utils.math_utils import cdiv
|
||||
from vllm.utils.mem_utils import DeviceMemoryProfiler, format_gib
|
||||
from vllm.utils.nvtx_pytorch_hooks import PytHooks
|
||||
from vllm.utils.platform_utils import num_compute_units
|
||||
from vllm.utils.torch_utils import (
|
||||
PIN_MEMORY,
|
||||
async_tensor_h2d,
|
||||
@@ -145,15 +144,12 @@ from vllm.v1.core.sched.output import NewRequestData
|
||||
from vllm.v1.cudagraph_dispatcher import CudagraphDispatcher
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
AttentionSpec,
|
||||
ChunkedLocalAttentionSpec,
|
||||
CrossAttentionSpec,
|
||||
EncoderOnlyAttentionSpec,
|
||||
FullAttentionSpec,
|
||||
KVCacheConfig,
|
||||
KVCacheGroupSpec,
|
||||
KVCacheSpec,
|
||||
KVCacheSpecKind,
|
||||
SlidingWindowSpec,
|
||||
UniformTypeKVCacheSpecs,
|
||||
get_kv_cache_spec_kind,
|
||||
)
|
||||
@@ -530,13 +526,11 @@ class GPUModelRunner(
|
||||
)
|
||||
|
||||
# Model-related.
|
||||
self.num_query_heads = model_config.get_num_attention_heads(parallel_config)
|
||||
self.inputs_embeds_size = model_config.get_inputs_embeds_size()
|
||||
# Only relevant for models using ALiBi (e.g, MPT)
|
||||
self.use_alibi = model_config.uses_alibi
|
||||
|
||||
self.cascade_attn_enabled = not self.model_config.disable_cascade_attn
|
||||
self.is_mm_prefix_lm = self.model_config.is_mm_prefix_lm
|
||||
# Deprecated: cascade attention has been removed. Kept for
|
||||
# out-of-tree model runners.
|
||||
self.cascade_attn_enabled = False
|
||||
|
||||
# Multi-modal data support
|
||||
self.mm_registry = MULTIMODAL_REGISTRY
|
||||
@@ -769,9 +763,6 @@ class GPUModelRunner(
|
||||
else:
|
||||
self.cudagraph_batch_sizes = []
|
||||
|
||||
# Cache the device properties.
|
||||
self._init_device_properties()
|
||||
|
||||
# Encoder timing registry for observability
|
||||
self.encoder_timing_registry: dict[str, EncoderTimingStats] = {}
|
||||
self._encoder_timing_lock = threading.Lock()
|
||||
@@ -1151,11 +1142,6 @@ class GPUModelRunner(
|
||||
if hasattr(self, "_kv_block_zeroer"):
|
||||
self._kv_block_zeroer.zero_block_ids(block_ids)
|
||||
|
||||
# Note: used for model runner override.
|
||||
def _init_device_properties(self) -> None:
|
||||
"""Initialize attributes from torch.cuda.get_device_properties"""
|
||||
self.num_sms = num_compute_units(self.device.index)
|
||||
|
||||
# Note: used for model runner override.
|
||||
def _sync_device(self) -> None:
|
||||
torch.accelerator.synchronize()
|
||||
@@ -2273,7 +2259,6 @@ class GPUModelRunner(
|
||||
use_spec_decode: bool = False,
|
||||
for_cudagraph_capture: bool = False,
|
||||
num_scheduled_tokens: dict[str, int] | None = None,
|
||||
cascade_attn_prefix_lens: list[list[int]] | None = None,
|
||||
slot_mappings: dict[int, torch.Tensor] | None = None,
|
||||
) -> tuple[PerLayerAttnMetadata, CommonAttentionMetadata | None]:
|
||||
"""Returns:
|
||||
@@ -2472,12 +2457,6 @@ class GPUModelRunner(
|
||||
kv_cache_spec = kv_cache_spec.kv_cache_specs[attn_group.layer_names[0]]
|
||||
cache_key = (kv_cache_spec, type(builder))
|
||||
|
||||
cascade_attn_prefix_len = (
|
||||
cascade_attn_prefix_lens[kv_cache_gid][attn_gid]
|
||||
if cascade_attn_prefix_lens
|
||||
else 0
|
||||
)
|
||||
|
||||
extra_attn_metadata_args = {}
|
||||
if use_spec_decode and isinstance(
|
||||
builder,
|
||||
@@ -2517,7 +2496,6 @@ class GPUModelRunner(
|
||||
attn_metadata_i = attn_group.build_metadata(
|
||||
common_attn_metadata,
|
||||
ubid or 0,
|
||||
common_prefix_len=cascade_attn_prefix_len,
|
||||
**extra_attn_metadata_args,
|
||||
)
|
||||
if builder.supports_update_block_table:
|
||||
@@ -2595,144 +2573,6 @@ class GPUModelRunner(
|
||||
|
||||
return attn_metadata, spec_decode_common_attn_metadata
|
||||
|
||||
def _compute_cascade_attn_prefix_lens(
|
||||
self,
|
||||
num_scheduled_tokens: np.ndarray,
|
||||
num_computed_tokens: np.ndarray,
|
||||
num_common_prefix_blocks: list[int],
|
||||
) -> list[list[int]] | None:
|
||||
"""Returns:
|
||||
Optional[cascade_attn_prefix_lens]
|
||||
cascade_attn_prefix_lens is 2D:
|
||||
``[kv_cache_group_id][attn_group_idx]``,
|
||||
None if we should not use cascade attention
|
||||
|
||||
"""
|
||||
use_cascade_attn = False
|
||||
num_kv_cache_groups = len(self.kv_cache_config.kv_cache_groups)
|
||||
cascade_attn_prefix_lens: list[list[int]] = [
|
||||
[] for _ in range(num_kv_cache_groups)
|
||||
]
|
||||
|
||||
for kv_cache_gid in range(num_kv_cache_groups):
|
||||
for attn_group in self.attn_groups[kv_cache_gid]:
|
||||
if isinstance(attn_group.kv_cache_spec, EncoderOnlyAttentionSpec):
|
||||
cascade_attn_prefix_len = 0
|
||||
else:
|
||||
# 0 if cascade attention should not be used
|
||||
cascade_attn_prefix_len = self._compute_cascade_attn_prefix_len(
|
||||
num_scheduled_tokens,
|
||||
num_computed_tokens,
|
||||
num_common_prefix_blocks[kv_cache_gid],
|
||||
attn_group.kv_cache_spec,
|
||||
attn_group.get_metadata_builder(),
|
||||
)
|
||||
cascade_attn_prefix_lens[kv_cache_gid].append(cascade_attn_prefix_len)
|
||||
use_cascade_attn |= cascade_attn_prefix_len > 0
|
||||
|
||||
return cascade_attn_prefix_lens if use_cascade_attn else None
|
||||
|
||||
def _compute_cascade_attn_prefix_len(
|
||||
self,
|
||||
num_scheduled_tokens: np.ndarray,
|
||||
num_computed_tokens: np.ndarray,
|
||||
num_common_prefix_blocks: int,
|
||||
kv_cache_spec: KVCacheSpec,
|
||||
attn_metadata_builder: AttentionMetadataBuilder,
|
||||
) -> int:
|
||||
"""Compute the length of the common prefix for cascade attention.
|
||||
|
||||
NOTE(woosuk): The common prefix length returned by this function
|
||||
represents the length used specifically for cascade attention, not the
|
||||
actual number of tokens shared between requests. When cascade attention
|
||||
is disabled (use_cascade=False), this function returns 0 even if
|
||||
requests share common tokens. Additionally, the common prefix length is
|
||||
truncated to a multiple of the block size and may be further truncated
|
||||
due to implementation details explained below.
|
||||
|
||||
Args:
|
||||
num_scheduled_tokens: Number of tokens scheduled per request.
|
||||
num_computed_tokens: Number of tokens already computed per request.
|
||||
num_common_prefix_blocks: Number of shared KV cache blocks.
|
||||
kv_cache_spec: The KV cache spec of the group being considered.
|
||||
attn_metadata_builder: Builder used to decide whether cascade
|
||||
attention can be used.
|
||||
|
||||
Returns:
|
||||
int: Length of common prefix in tokens.
|
||||
|
||||
"""
|
||||
common_prefix_len = num_common_prefix_blocks * kv_cache_spec.block_size
|
||||
if common_prefix_len == 0:
|
||||
# Common case.
|
||||
return 0
|
||||
|
||||
# NOTE(woosuk): Cascade attention uses two attention kernels: one
|
||||
# for the common prefix and the other for the rest. For the first
|
||||
# kernel, we concatenate all the query tokens (possibly from
|
||||
# different requests) and treat them as if they are from the same
|
||||
# request. Then, we use bi-directional attention to process the
|
||||
# common prefix in the KV cache. Importantly, this means that the
|
||||
# first kernel does not do any masking.
|
||||
|
||||
# Consider the following example:
|
||||
# Request 1's input query: [D, E, X]
|
||||
# Request 1's kv cache: [A, B, C, D, E, X]
|
||||
# Request 1's num_computed_tokens: 3 (i.e., [A, B, C])
|
||||
# Request 2's input query: [E, Y]
|
||||
# Request 2's kv cache: [A, B, C, D, E, Y]
|
||||
# Request 2's num_computed_tokens: 4 (i.e., [A, B, C, D])
|
||||
|
||||
# If we use [A, B, C, D, E] as the common prefix, then the
|
||||
# first kernel will compute the bi-directional attention between
|
||||
# input query [D, E, X, E, Y] and common prefix [A, B, C, D, E].
|
||||
# However, this is wrong because D in Request 1 should not attend to
|
||||
# E in the common prefix (i.e., we need masking).
|
||||
# To avoid this, [A, B, C, D] should be the common prefix.
|
||||
# That is, the common prefix should be capped by the minimum
|
||||
# num_computed_tokens among the requests, and plus one to include
|
||||
# the first token of the query.
|
||||
|
||||
# In practice, we use [A, B, C] as the common prefix, instead of
|
||||
# [A, B, C, D] (i.e., the common prefix is capped by the minimum
|
||||
# num_computed_tokens, without plus one).
|
||||
# This is because of an implementation detail: We want to always
|
||||
# use two kernels for cascade attention. Let's imagine:
|
||||
# Request 3's input query: [D]
|
||||
# Request 3's kv cache: [A, B, C, D]
|
||||
# Request 3's num_computed_tokens: 3 (i.e., [A, B, C])
|
||||
# If we use [A, B, C, D] as the common prefix for Request 1-3,
|
||||
# then Request 3 will be processed only by the first kernel,
|
||||
# and the second kernel will get an empty input. While this is not
|
||||
# a fundamental problem, our current implementation does not support
|
||||
# this case.
|
||||
common_prefix_len = min(common_prefix_len, num_computed_tokens.min())
|
||||
# common_prefix_len should be a multiple of the block size.
|
||||
common_prefix_len = (
|
||||
common_prefix_len // kv_cache_spec.block_size * kv_cache_spec.block_size
|
||||
)
|
||||
use_sliding_window = isinstance(kv_cache_spec, SlidingWindowSpec) or (
|
||||
isinstance(kv_cache_spec, FullAttentionSpec)
|
||||
and kv_cache_spec.sliding_window is not None
|
||||
)
|
||||
use_local_attention = isinstance(kv_cache_spec, ChunkedLocalAttentionSpec) or (
|
||||
isinstance(kv_cache_spec, FullAttentionSpec)
|
||||
and kv_cache_spec.attention_chunk_size is not None
|
||||
)
|
||||
assert isinstance(kv_cache_spec, AttentionSpec)
|
||||
use_cascade = attn_metadata_builder.use_cascade_attention(
|
||||
common_prefix_len=common_prefix_len,
|
||||
query_lens=num_scheduled_tokens,
|
||||
num_query_heads=self.num_query_heads,
|
||||
num_kv_heads=kv_cache_spec.num_kv_heads,
|
||||
use_alibi=self.use_alibi,
|
||||
use_sliding_window=use_sliding_window,
|
||||
use_local_attention=use_local_attention,
|
||||
num_sms=self.num_sms,
|
||||
dcp_world_size=self.dcp_world_size,
|
||||
)
|
||||
return common_prefix_len if use_cascade else 0
|
||||
|
||||
def _calc_mrope_positions(self, scheduler_output: "SchedulerOutput"):
|
||||
mrope_pos_ptr = 0
|
||||
for index, req_id in enumerate(self.input_batch.req_ids):
|
||||
@@ -3881,7 +3721,8 @@ class GPUModelRunner(
|
||||
num_reqs: int,
|
||||
num_scheduled_tokens_np: np.ndarray,
|
||||
max_num_scheduled_tokens: int,
|
||||
use_cascade_attn: bool,
|
||||
# Deprecated and ignored. Kept for out-of-tree model runners.
|
||||
use_cascade_attn: bool = False,
|
||||
allow_microbatching: bool = True,
|
||||
force_eager: bool = False,
|
||||
# For cudagraph capture TODO(lucas): Refactor how we capture cudagraphs (will
|
||||
@@ -3929,7 +3770,7 @@ class GPUModelRunner(
|
||||
)
|
||||
|
||||
cudagraph_mode, batch_descriptor = dispatch_cudagraph(
|
||||
num_tokens, disable_full=use_cascade_attn or has_encoder_output
|
||||
num_tokens, disable_full=has_encoder_output
|
||||
)
|
||||
num_tokens_padded = batch_descriptor.num_tokens
|
||||
|
||||
@@ -4181,16 +4022,6 @@ class GPUModelRunner(
|
||||
self._prepare_inputs(scheduler_output, num_scheduled_tokens_np)
|
||||
)
|
||||
|
||||
cascade_attn_prefix_lens = None
|
||||
# Disable cascade attention when using microbatching (DBO)
|
||||
if self.cascade_attn_enabled and not self.parallel_config.use_ubatching:
|
||||
# Pre-compute cascade attention prefix lengths
|
||||
cascade_attn_prefix_lens = self._compute_cascade_attn_prefix_lens(
|
||||
num_scheduled_tokens_np,
|
||||
self.input_batch.num_computed_tokens_cpu[:num_reqs],
|
||||
scheduler_output.num_common_prefix_blocks,
|
||||
)
|
||||
|
||||
(
|
||||
cudagraph_mode,
|
||||
batch_desc,
|
||||
@@ -4202,7 +4033,7 @@ class GPUModelRunner(
|
||||
num_reqs=num_reqs,
|
||||
num_scheduled_tokens_np=num_scheduled_tokens_np,
|
||||
max_num_scheduled_tokens=max_num_scheduled_tokens,
|
||||
use_cascade_attn=cascade_attn_prefix_lens is not None,
|
||||
use_cascade_attn=False,
|
||||
num_encoder_reqs=len(scheduler_output.scheduled_encoder_inputs),
|
||||
allow_microbatching=self._allow_microbatching(
|
||||
num_reqs, num_scheduled_tokens_np
|
||||
@@ -4319,7 +4150,6 @@ class GPUModelRunner(
|
||||
max_num_sampled_tokens=max_num_sampled_tokens,
|
||||
use_spec_decode=use_spec_decode,
|
||||
num_scheduled_tokens=scheduler_output.num_scheduled_tokens,
|
||||
cascade_attn_prefix_lens=cascade_attn_prefix_lens,
|
||||
slot_mappings=slot_mappings_by_group,
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user