-
Notifications
You must be signed in to change notification settings - Fork 130
Add support for chunked attention (#597) #809
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
Merged
mgawarkiewicz-intel
merged 6 commits into
vllm-project:releases/v0.13.0
from
kfojcik-intel:dev/kfojcik/chunked_attn_0_13_0
Jan 15, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b5e8e5b
Add support for chunked attention (#597)
jkaniecki ca1878d
Add chunked_block args to hpu eagle
kfojcik-intel 80c3fd5
Fix to attn chunk size check
kfojcik-intel 0ec773f
Refactor
kfojcik-intel a52b271
Refactor
kfojcik-intel eddea52
Merge branch 'releases/v0.13.0' into dev/kfojcik/chunked_attn_0_13_0
iboiko-habana 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -508,10 +508,66 @@ def _set_attn_bias_for_sliding_window(self, attn_metadata, batch_size, seq_len, | |||||
| attn_metadata = prefill_metadata._replace(window_attn_bias=attn_bias) | ||||||
| return attn_metadata | ||||||
|
|
||||||
| def _set_block_mapping(self, metadata, batch_size, device, dtype, is_window_block=False): | ||||||
| def _set_attn_bias_for_chunked_attention(self, attn_metadata, batch_size, seq_len, chunk_size, device, dtype): | ||||||
| if (attn_metadata is None or not attn_metadata.is_prompt): | ||||||
| return attn_metadata | ||||||
|
|
||||||
| prefill_metadata = attn_metadata | ||||||
| shift = 0 | ||||||
|
|
||||||
| if self.prefill_use_fusedsdpa and attn_metadata.block_list is not None: | ||||||
|
|
||||||
| context_lens_t = prefill_metadata.context_lens_tensor | ||||||
| block_list = prefill_metadata.block_list | ||||||
| max_context_len = (block_list.size(-1) // batch_size if block_list is not None else 0) | ||||||
|
ksmusz marked this conversation as resolved.
|
||||||
| max_context_len = max_context_len * self.block_size | ||||||
| query_positions = torch.arange(seq_len, device=device) | ||||||
| total_token_positions = context_lens_t.unsqueeze(-1) + query_positions.unsqueeze(0) | ||||||
| which_chunk = (total_token_positions // chunk_size) | ||||||
| chunk_start_positions = which_chunk * chunk_size | ||||||
| invalid_lens_t = chunk_start_positions - 1 | ||||||
|
|
||||||
| past_indices = torch.arange(max_context_len, device=device) | ||||||
| past_mask = ( | ||||||
| (past_indices.unsqueeze(0).unsqueeze(0) > invalid_lens_t.unsqueeze(-1)) & | ||||||
| (past_indices.unsqueeze(0).unsqueeze(0) < context_lens_t.unsqueeze(-1).unsqueeze(-1))).unsqueeze(1) | ||||||
|
|
||||||
| causal_mask = torch.tril(torch.ones(seq_len, seq_len, dtype=torch.bool, device=device), diagonal=shift) | ||||||
| query_chunk_ids = which_chunk[0] | ||||||
| same_chunk_mask = query_chunk_ids.unsqueeze(0) == query_chunk_ids.unsqueeze(1) | ||||||
|
|
||||||
| causal_mask = causal_mask & same_chunk_mask | ||||||
| causal_mask = causal_mask.unsqueeze(0).unsqueeze(0).expand(batch_size, 1, seq_len, seq_len) | ||||||
|
|
||||||
| mask = torch.concat((past_mask, causal_mask), dim=-1) | ||||||
| attn_bias = torch.where(mask, torch.tensor(0.0, dtype=dtype, device=device), | ||||||
| torch.tensor(float('-inf'), dtype=dtype, device=device)) | ||||||
| else: | ||||||
| tensor = torch.full((batch_size, 1, seq_len, seq_len), device=device, dtype=dtype, fill_value=1) | ||||||
| mask = torch.tril(tensor, diagonal=shift) | ||||||
| idx = torch.arange(seq_len, device=device) | ||||||
| chunk_id = idx // chunk_size | ||||||
| same_chunk = chunk_id.unsqueeze(0) == chunk_id.unsqueeze(1) | ||||||
| same_chunk = same_chunk.unsqueeze(0).unsqueeze(0) | ||||||
| mask = torch.where(same_chunk, mask, torch.tensor(0.0, dtype=dtype, device=device)) | ||||||
| attn_bias = torch.log(mask) | ||||||
|
|
||||||
| attn_metadata = custom_tuple_replace(prefill_metadata, "TrimmedAttentionMetadata", chunked_attn_bias=attn_bias) | ||||||
| return attn_metadata | ||||||
|
|
||||||
| def _set_block_mapping(self, | ||||||
| metadata, | ||||||
| batch_size, | ||||||
| device, | ||||||
| dtype, | ||||||
| is_window_block=False, | ||||||
| update_for_chunked_attention=False): | ||||||
| if is_window_block: | ||||||
| block_usage = metadata.window_block_usage | ||||||
| block_groups = metadata.window_block_groups | ||||||
| elif update_for_chunked_attention: | ||||||
| block_usage = metadata.chunked_block_usage | ||||||
| block_groups = metadata.chunked_block_groups | ||||||
| else: | ||||||
| block_usage = metadata.block_usage | ||||||
| block_groups = metadata.block_groups | ||||||
|
|
@@ -542,21 +598,36 @@ def _set_block_mapping(self, metadata, batch_size, device, dtype, is_window_bloc | |||||
| "TrimmedAttentionMetadata", | ||||||
| window_block_mapping=block_mapping, | ||||||
| window_attn_bias=attn_bias) | ||||||
| elif update_for_chunked_attention: | ||||||
| metadata = custom_tuple_replace(metadata, | ||||||
| "TrimmedAttentionMetadata", | ||||||
| chunked_block_mapping=block_mapping, | ||||||
| chunked_attn_bias=attn_bias) | ||||||
| else: | ||||||
| metadata = custom_tuple_replace(metadata, | ||||||
| "TrimmedAttentionMetadata", | ||||||
| block_mapping=block_mapping, | ||||||
| attn_bias=attn_bias) | ||||||
| return metadata | ||||||
|
|
||||||
| def _update_metadata(self, attn_metadata, batch_size, seq_len, device, dtype): | ||||||
| def _update_metadata(self, attn_metadata, batch_size, seq_len, device, dtype, model_has_chunked_attention=False): | ||||||
| if attn_metadata.is_prompt: | ||||||
| attn_metadata = self._set_attn_bias(attn_metadata, batch_size, seq_len, device, dtype) | ||||||
| if self.interleaved_sliding_window and self.sliding_window is not None: | ||||||
| attn_metadata = self._set_attn_bias_for_sliding_window(attn_metadata, batch_size, seq_len, | ||||||
| self.sliding_window, device, dtype) | ||||||
| if model_has_chunked_attention: | ||||||
| attn_metadata = self._set_attn_bias_for_chunked_attention( | ||||||
| attn_metadata, batch_size, seq_len, self.model.config.text_config.attention_chunk_size, device, | ||||||
| dtype) | ||||||
| else: | ||||||
| attn_metadata = self._set_block_mapping(attn_metadata, batch_size, device, dtype) | ||||||
| if model_has_chunked_attention: | ||||||
| attn_metadata = self._set_block_mapping(attn_metadata, | ||||||
| batch_size, | ||||||
| device, | ||||||
| dtype, | ||||||
| update_for_chunked_attention=True) | ||||||
| if self.interleaved_sliding_window and self.sliding_window is not None: | ||||||
| attn_metadata = self._set_block_mapping(attn_metadata, batch_size, device, dtype, True) | ||||||
| return attn_metadata | ||||||
|
|
@@ -573,9 +644,11 @@ def forward(self, *args, **kwargs): | |||||
| if 'warmup_mode' in kwargs: | ||||||
| kwargs.pop('warmup_mode') | ||||||
| input_ids = kwargs['input_ids'] | ||||||
| model_has_chunked_attention = kwargs.pop('model_has_chunked_attention', False) | ||||||
| if not self.unified_attn: | ||||||
| kwargs['attn_metadata'] = self._update_metadata(kwargs['attn_metadata'], input_ids.size(0), | ||||||
| input_ids.size(1), input_ids.device, self.dtype) | ||||||
| input_ids.size(1), input_ids.device, self.dtype, | ||||||
| model_has_chunked_attention) | ||||||
| if self._rotary_prepare_cos_sin is not None: | ||||||
| self._rotary_prepare_cos_sin(kwargs['positions'], recompute_cos_sin=self.recompute_cos_sin) | ||||||
| attn_meta = kwargs.pop('attn_metadata') | ||||||
|
|
@@ -700,7 +773,12 @@ def trim_attn_metadata(metadata: HPUAttentionMetadataV1) -> object: | |||||
| 'window_block_usage', | ||||||
| 'window_block_groups', | ||||||
| 'window_attn_bias', | ||||||
| ]) | ||||||
| 'chunked_block_mapping', | ||||||
| 'chunked_attn_bias', | ||||||
| 'chunked_block_list', | ||||||
| 'chunked_block_usage', | ||||||
| 'chunked_block_groups' | ||||||
| ]) # yapf: disable | ||||||
| return attention_metadata | ||||||
|
|
||||||
|
|
||||||
|
|
@@ -944,6 +1022,8 @@ def __init__( | |||||
| self.scheduler_output: SchedulerOutput | None = None | ||||||
| self.warmup_mode: bool = False | ||||||
| self.batch_changed: bool = False | ||||||
| # WA for chunked attention support | ||||||
|
||||||
| # WA for chunked attention support | |
| # Workaround flag for chunked attention support; toggled when special handling is required |
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.
Uh oh!
There was an error while loading. Please reload this page.