-
-
Notifications
You must be signed in to change notification settings - Fork 17.7k
[Core][Model] Gemma4: Unified FA4 for all layers + FlashAttention mm_prefix support #42175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lucianommartins
wants to merge
1
commit into
vllm-project:main
Choose a base branch
from
lucianommartins:lucianommartins/gemma4-fa4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+221
−38
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -222,6 +222,7 @@ | |
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from vllm.v1.attention.backends.flash_attn import FlashAttentionMetadata | ||
| from vllm.v1.core.sched.output import GrammarOutput, SchedulerOutput | ||
| from vllm.v1.spec_decode.ngram_proposer import NgramProposer | ||
| from vllm.v1.worker.encoder_cudagraph import EncoderCudaGraphManager | ||
|
|
@@ -6777,6 +6778,9 @@ def _set_mm_prefix_range_for_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.flash_attn import ( | ||
| FlashAttentionMetadata, | ||
| ) | ||
| from vllm.v1.attention.backends.triton_attn import ( | ||
| TritonAttentionMetadata, | ||
| ) | ||
|
|
@@ -6794,8 +6798,7 @@ def _set_mm_prefix_range_for_metadata( | |
| 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 isinstance(metadata, (TritonAttentionMetadata, FlashAttentionMetadata)): | ||
| if shared_tensor is None: | ||
| shared_tensor = ( | ||
| TritonAttentionMetadata.compute_mm_prefix_range_tensor( | ||
|
|
@@ -6806,6 +6809,77 @@ def _set_mm_prefix_range_for_metadata( | |
| ) | ||
| metadata.mm_prefix_range_tensor = shared_tensor | ||
|
|
||
| # Precompute mm_prefix correction indices for FlashAttention | ||
| # on CPU to avoid GPU-tensor .item() calls in the forward pass. | ||
| if isinstance(metadata, FlashAttentionMetadata): | ||
| self._precompute_mm_prefix_indices(metadata, req_doc_ranges) | ||
|
|
||
| def _precompute_mm_prefix_indices( | ||
| self, | ||
| metadata: "FlashAttentionMetadata", | ||
| req_doc_ranges: dict[int, list[tuple[int, int]]], | ||
| ) -> None: | ||
|
Comment on lines
+6817
to
+6821
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is too FA specific, which should not be placed at model runner. |
||
| """Precompute mm_prefix correction indices on CPU. | ||
|
|
||
| Reads query_start_loc and seq_lens from CPU-side buffers | ||
| (no GPU sync) and stores the result as GPU tensors on the | ||
| metadata for use in _apply_mm_prefix_correction. | ||
| """ | ||
| num_reqs = self.input_batch.num_reqs | ||
| qsl = self.query_start_loc.np | ||
| seq_lens_cpu = self.seq_lens.cpu() | ||
|
|
||
| mm_token_indices: list[int] = [] | ||
| mm_cu_seqlens = [0] | ||
| mm_seqlens_k: list[int] = [] | ||
| mm_bt_indices: list[int] = [] | ||
|
|
||
| for seq_idx in range(num_reqs): | ||
| ranges = req_doc_ranges.get(seq_idx, []) | ||
| if not ranges: | ||
| continue | ||
| q_start = int(qsl[seq_idx]) | ||
| q_end = int(qsl[seq_idx + 1]) | ||
| query_len = q_end - q_start | ||
| seq_len = int(seq_lens_cpu[seq_idx]) | ||
| context_len = seq_len - query_len | ||
|
|
||
| for r_start, r_end in ranges: | ||
| if r_start >= r_end: | ||
| continue | ||
| tokens = [ | ||
| q_start + off | ||
| for off in range(query_len) | ||
| if r_start <= context_len + off <= r_end | ||
| ] | ||
| if tokens: | ||
| mm_token_indices.extend(tokens) | ||
| mm_cu_seqlens.append(mm_cu_seqlens[-1] + len(tokens)) | ||
| mm_seqlens_k.append(r_end - r_start + 1) | ||
| mm_bt_indices.append(seq_idx) | ||
|
|
||
| if not mm_token_indices: | ||
| return | ||
|
|
||
| device = metadata.seq_lens.device # type: ignore[union-attr] | ||
| metadata.mm_prefix_indices = torch.tensor( | ||
| mm_token_indices, dtype=torch.long, device=device | ||
| ) | ||
| metadata.mm_prefix_cu_seqlens = torch.tensor( | ||
| mm_cu_seqlens, dtype=torch.int32, device=device | ||
| ) | ||
| metadata.mm_prefix_seqlens_k = torch.tensor( | ||
| mm_seqlens_k, dtype=torch.int32, device=device | ||
| ) | ||
| metadata.mm_prefix_bt_indices = torch.tensor( | ||
| mm_bt_indices, dtype=torch.long, device=device | ||
| ) | ||
| metadata.mm_prefix_max_seqlen_q = max( | ||
| mm_cu_seqlens[i + 1] - mm_cu_seqlens[i] | ||
| for i in range(len(mm_cu_seqlens) - 1) | ||
| ) | ||
| metadata.mm_prefix_max_seqlen_k = max(mm_seqlens_k) | ||
|
|
||
| def may_reinitialize_input_batch( | ||
| self, kv_cache_config: KVCacheConfig, kernel_block_sizes: list[int] | ||
| ) -> None: | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I worry that this can affect model's accuracy through numeric difference from overlayed weights...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that's what I explained better on this #42175 (comment)