[v1] Add PrefixLM support to FlexAttention backend (#27938)
Signed-off-by: Isotr0py <mozf@mail2.sysu.edu.cn>
This commit is contained in:
@@ -17,6 +17,7 @@ from torch.nn.attention.flex_attention import (
|
||||
and_masks,
|
||||
create_block_mask,
|
||||
flex_attention,
|
||||
or_masks,
|
||||
)
|
||||
|
||||
from vllm.attention.backends.abstract import (
|
||||
@@ -42,6 +43,7 @@ from vllm.v1.kv_cache_interface import AttentionSpec
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
torch._dynamo.config.recompile_limit = 16
|
||||
create_block_mask_compiled = torch.compile(
|
||||
create_block_mask, fullgraph=True, mode="reduce-overhead"
|
||||
)
|
||||
@@ -91,6 +93,11 @@ class FlexAttentionBackend(AttentionBackend):
|
||||
"""FlexAttention supports both decoder and encoder-only attention."""
|
||||
return attn_type in (AttentionType.DECODER, AttentionType.ENCODER_ONLY)
|
||||
|
||||
@classmethod
|
||||
def supports_mm_prefix(cls) -> bool:
|
||||
"""FlexAttention supports full attention for image tokens."""
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def get_impl_cls() -> type["FlexAttentionImpl"]:
|
||||
return FlexAttentionImpl
|
||||
@@ -316,6 +323,7 @@ class FlexAttentionMetadata:
|
||||
kv_block_size: int = 16
|
||||
transformed_score_mod: _score_mod_signature | None = None
|
||||
sliding_window: int | None = None
|
||||
mm_prefix_range: dict[int, list[tuple[int, int]]] | None = None
|
||||
|
||||
@cached_property
|
||||
def logical_block_ids(self):
|
||||
@@ -443,6 +451,45 @@ class FlexAttentionMetadata:
|
||||
|
||||
return final_mask_mod if self.causal else sliding_window_mask_mod
|
||||
|
||||
def get_prefix_lm_mask_mod(self) -> _mask_mod_signature:
|
||||
"""Creates the prefix LM mask_mod function for FlexAttention."""
|
||||
|
||||
assert self.doc_ids is not None
|
||||
request_lookup = self.doc_ids
|
||||
|
||||
def prefix_lm_mask_mod(
|
||||
b: torch.Tensor,
|
||||
h: torch.Tensor,
|
||||
cu_q_idx: torch.Tensor,
|
||||
q_idx: torch.Tensor,
|
||||
kv_idx: torch.Tensor,
|
||||
):
|
||||
mask = torch.zeros_like(q_idx, dtype=torch.bool)
|
||||
for req, doc_range_lst in (self.mm_prefix_range or {}).items():
|
||||
req_mask = request_lookup[cu_q_idx] == req
|
||||
for start, end in doc_range_lst:
|
||||
doc_mask_q = (q_idx >= start) & (q_idx <= end)
|
||||
doc_mask_kv = (kv_idx >= start) & (kv_idx <= end)
|
||||
mask = mask | (req_mask & doc_mask_q & doc_mask_kv)
|
||||
return mask
|
||||
|
||||
def final_mask_mod(
|
||||
b: torch.Tensor,
|
||||
h: torch.Tensor,
|
||||
q_idx: torch.Tensor,
|
||||
physical_kv_idx: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
(is_valid, logical_q_idx, logical_kv_idx) = (
|
||||
self._convert_physical_to_logical(self.doc_ids, q_idx, physical_kv_idx)
|
||||
)
|
||||
return torch.where(
|
||||
is_valid,
|
||||
prefix_lm_mask_mod(b, h, q_idx, logical_q_idx, logical_kv_idx),
|
||||
False,
|
||||
)
|
||||
|
||||
return final_mask_mod
|
||||
|
||||
def get_mask_mod(self):
|
||||
# Stage-1: initialize the base mask_mod
|
||||
# (causal mask for decoder or bidirectional mask for encoder)
|
||||
@@ -456,6 +503,10 @@ class FlexAttentionMetadata:
|
||||
# Add sliding window mask for sliding window attention
|
||||
sliding_window_mask_mod = self.get_sliding_window_mask_mod()
|
||||
mask_mod = and_masks(mask_mod, sliding_window_mask_mod)
|
||||
if self.mm_prefix_range:
|
||||
# Add prefix LM mask for vision-language prefix LM attention
|
||||
prefix_lm_mask_mod = self.get_prefix_lm_mask_mod()
|
||||
mask_mod = or_masks(mask_mod, prefix_lm_mask_mod)
|
||||
return mask_mod
|
||||
|
||||
def get_transformed_score_mod(self) -> _score_mod_signature | None:
|
||||
@@ -709,6 +760,7 @@ class FlexAttentionImpl(AttentionImpl):
|
||||
sliding_window: int | None
|
||||
alibi_slopes: torch.Tensor | None
|
||||
logits_soft_cap: float | None
|
||||
mm_prefix_range: dict[int, list[tuple[int, int]]] | None = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -810,11 +862,21 @@ class FlexAttentionImpl(AttentionImpl):
|
||||
|
||||
num_actual_tokens = attn_metadata.num_actual_tokens
|
||||
|
||||
needs_rebuild_block_mask = False
|
||||
if attn_metadata.sliding_window != self.sliding_window:
|
||||
attn_metadata.sliding_window = self.sliding_window
|
||||
if attn_metadata.direct_build:
|
||||
# update mask mod in attention metadata
|
||||
attn_metadata.mask_mod = attn_metadata.get_mask_mod()
|
||||
needs_rebuild_block_mask = True
|
||||
|
||||
if self.mm_prefix_range != getattr(attn_metadata, "mm_prefix_range", None):
|
||||
self.mm_prefix_range = attn_metadata.mm_prefix_range
|
||||
attn_metadata.mask_mod = attn_metadata.get_mask_mod()
|
||||
needs_rebuild_block_mask = True
|
||||
|
||||
if needs_rebuild_block_mask:
|
||||
if attn_metadata.direct_build and attn_metadata.causal:
|
||||
attn_metadata.block_mask = attn_metadata._build_block_mask_direct()
|
||||
else:
|
||||
attn_metadata.block_mask = attn_metadata.build_block_mask()
|
||||
|
||||
@@ -48,7 +48,10 @@ from vllm.distributed.parallel_state import (
|
||||
is_global_first_rank,
|
||||
prepare_communication_buffer_for_model,
|
||||
)
|
||||
from vllm.forward_context import BatchDescriptor, set_forward_context
|
||||
from vllm.forward_context import (
|
||||
BatchDescriptor,
|
||||
set_forward_context,
|
||||
)
|
||||
from vllm.logger import init_logger
|
||||
from vllm.model_executor.layers.attention_layer_base import AttentionLayerBase
|
||||
from vllm.model_executor.layers.rotary_embedding import (
|
||||
@@ -329,6 +332,7 @@ class GPUModelRunner(
|
||||
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
|
||||
|
||||
# Multi-modal data support
|
||||
self.mm_registry = MULTIMODAL_REGISTRY
|
||||
@@ -1700,6 +1704,26 @@ class GPUModelRunner(
|
||||
for layer_name in attn_group.layer_names:
|
||||
attn_metadata[layer_name] = attn_metadata_i
|
||||
|
||||
if self.is_mm_prefix_lm:
|
||||
req_doc_ranges = {}
|
||||
for req_id in self.input_batch.req_ids:
|
||||
image_doc_ranges = []
|
||||
req_state = self.requests[req_id]
|
||||
for mm_feature in req_state.mm_features:
|
||||
pos_info = mm_feature.mm_position
|
||||
img_doc_range = pos_info.extract_embeds_range()
|
||||
image_doc_ranges.extend(img_doc_range)
|
||||
req_idx = self.input_batch.req_id_to_index[req_id]
|
||||
req_doc_ranges[req_idx] = image_doc_ranges
|
||||
|
||||
if isinstance(attn_metadata, list):
|
||||
for ub_metadata in attn_metadata:
|
||||
for _metadata in ub_metadata.values():
|
||||
_metadata.mm_prefix_range = req_doc_ranges # type: ignore[attr-defined]
|
||||
else:
|
||||
for _metadata in attn_metadata.values():
|
||||
_metadata.mm_prefix_range = req_doc_ranges # type: ignore[attr-defined]
|
||||
|
||||
if spec_decode_common_attn_metadata is not None and (
|
||||
num_reqs != num_reqs_padded or num_tokens != num_tokens_padded
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user