[Core][Model] Gemma4: Unified FA4 for all layers + FlashAttention mm_prefix support (#42175)
Signed-off-by: Luciano Martins <lucianommartins@users.noreply.github.com> Signed-off-by: Lucas Wilkinson <lwilkins@redhat.com> Signed-off-by: Matthew Bonanni <mbonanni@redhat.com> Co-authored-by: Luciano Martins <lucianommartins@users.noreply.github.com> Co-authored-by: Lucas Wilkinson <lwilkins@redhat.com> Co-authored-by: Matthew Bonanni <mbonanni@redhat.com>
This commit is contained in:
co-authored by
Luciano Martins
Lucas Wilkinson
Matthew Bonanni
parent
d82ac00923
commit
6deb05e0e4
@@ -2286,6 +2286,30 @@ class GPUModelRunner(
|
||||
seq_lens_cpu = None
|
||||
num_computed_tokens_cpu = None
|
||||
|
||||
# Compute mm_prefix bidirectional ranges before building
|
||||
# attention metadata so builders handle them during build().
|
||||
# Ranges exceeding sliding_window are skipped to prevent
|
||||
# early tokens from attending across the entire image span.
|
||||
req_doc_ranges: dict[int, list[tuple[int, int]]] | None = None
|
||||
if self.is_mm_prefix_lm:
|
||||
req_doc_ranges = {}
|
||||
hf_text_config = self.model_config.hf_text_config
|
||||
_bidi_sw = getattr(hf_text_config, "sliding_window", None)
|
||||
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:
|
||||
if mm_feature.modality == "audio":
|
||||
continue
|
||||
pos_info = mm_feature.mm_position
|
||||
img_doc_range = pos_info.extract_embeds_range()
|
||||
for r in img_doc_range:
|
||||
if _bidi_sw is not None and (r[1] - r[0] + 1) > _bidi_sw:
|
||||
continue
|
||||
image_doc_ranges.append(r)
|
||||
req_idx = self.input_batch.req_id_to_index[req_id]
|
||||
req_doc_ranges[req_idx] = image_doc_ranges
|
||||
|
||||
cm_base = CommonAttentionMetadata(
|
||||
query_start_loc=self.query_start_loc.gpu[: num_reqs_padded + 1],
|
||||
query_start_loc_cpu=self.query_start_loc.cpu[: num_reqs_padded + 1],
|
||||
@@ -2302,6 +2326,7 @@ class GPUModelRunner(
|
||||
causal=True,
|
||||
is_prefilling=is_prefilling,
|
||||
positions=self.positions[:num_tokens_padded],
|
||||
mm_req_doc_ranges=req_doc_ranges,
|
||||
)
|
||||
|
||||
if self.dcp_world_size > 1:
|
||||
@@ -2454,36 +2479,6 @@ class GPUModelRunner(
|
||||
else:
|
||||
_build_attn_group_metadata(kv_cache_gid, attn_gid, cm)
|
||||
|
||||
if self.is_mm_prefix_lm:
|
||||
req_doc_ranges = {}
|
||||
|
||||
# Gemma4 bidi: skip ranges that exceed the sliding
|
||||
# window. When image tokens > sliding_window, bidi causes
|
||||
# early image tokens to attend to the entire image
|
||||
# (e.g. 6 → 1092 targets), degrading spatial precision.
|
||||
# Per-range filtering keeps bidi for small images/video
|
||||
# frames while skipping oversized images.
|
||||
hf_text_config = self.model_config.hf_text_config
|
||||
_bidi_sw = getattr(hf_text_config, "sliding_window", None)
|
||||
|
||||
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:
|
||||
if mm_feature.modality == "audio":
|
||||
continue
|
||||
pos_info = mm_feature.mm_position
|
||||
img_doc_range = pos_info.extract_embeds_range()
|
||||
for r in img_doc_range:
|
||||
if _bidi_sw is not None and (r[1] - r[0] + 1) > _bidi_sw:
|
||||
continue
|
||||
image_doc_ranges.append(r)
|
||||
req_idx = self.input_batch.req_id_to_index[req_id]
|
||||
req_doc_ranges[req_idx] = image_doc_ranges
|
||||
|
||||
# Set mm_prefix_range for all attention metadata
|
||||
self._set_mm_prefix_range_for_metadata(attn_metadata, req_doc_ranges)
|
||||
|
||||
if spec_decode_common_attn_metadata is not None and (
|
||||
num_reqs != num_reqs_padded or num_tokens != num_tokens_padded
|
||||
):
|
||||
@@ -6891,46 +6886,6 @@ class GPUModelRunner(
|
||||
return
|
||||
self.reorder_batch_threshold = reduce(min_none_high, reorder_batch_thresholds) # type: ignore[assignment]
|
||||
|
||||
def _set_mm_prefix_range_for_metadata(
|
||||
self,
|
||||
attn_metadata: Any,
|
||||
req_doc_ranges: dict[int, list[tuple[int, int]]],
|
||||
) -> None:
|
||||
"""Set mm_prefix_range for all attention metadata objects.
|
||||
|
||||
This method handles both list and non-list attention metadata,
|
||||
computing mm_prefix_range_tensor once and sharing it across all
|
||||
metadata objects to avoid redundant host-to-device transfers.
|
||||
"""
|
||||
from vllm.v1.attention.backends.triton_attn import (
|
||||
TritonAttentionMetadata,
|
||||
)
|
||||
|
||||
# Get all metadata objects from either list or dict structure
|
||||
metadata_list = []
|
||||
if isinstance(attn_metadata, list):
|
||||
for ub_metadata in attn_metadata:
|
||||
metadata_list.extend(ub_metadata.values())
|
||||
else:
|
||||
metadata_list.extend(attn_metadata.values())
|
||||
|
||||
# Set mm_prefix_range for all metadata and compute tensor once
|
||||
shared_tensor = None
|
||||
for metadata in metadata_list:
|
||||
metadata.mm_prefix_range = req_doc_ranges # type: ignore[attr-defined]
|
||||
|
||||
# Only compute tensor for TritonAttentionMetadata
|
||||
if isinstance(metadata, TritonAttentionMetadata):
|
||||
if shared_tensor is None:
|
||||
shared_tensor = (
|
||||
TritonAttentionMetadata.compute_mm_prefix_range_tensor(
|
||||
req_doc_ranges,
|
||||
metadata.seq_lens.shape[0], # type: ignore[attr-defined]
|
||||
metadata.seq_lens.device, # type: ignore[attr-defined]
|
||||
)
|
||||
)
|
||||
metadata.mm_prefix_range_tensor = shared_tensor
|
||||
|
||||
def may_reinitialize_input_batch(
|
||||
self, kv_cache_config: KVCacheConfig, kernel_block_sizes: list[int]
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user