[Bugfix] Avoid FlexAttention recompiles when request counts change (#55888)

Signed-off-by: Andreas Karatzas <Andreas.Karatzas@amd.com>
This commit is contained in:
Andreas Karatzas
2026-09-09 08:10:25 -07:00
committed by GitHub
parent d8d53f17c2
commit 7e95735eb9
2 changed files with 105 additions and 11 deletions
+18 -11
View File
@@ -909,6 +909,12 @@ class FlexAttentionMetadataBuilder(AttentionMetadataBuilder[FlexAttentionMetadat
self.persistent_offset_tensor = torch.empty(
max_num_seqs, dtype=torch.int32, device=device
)
self.persistent_query_start_loc = torch.empty(
max_num_seqs + 1, dtype=torch.int32, device=device
)
self.persistent_seq_lens = torch.empty(
max_num_seqs, dtype=torch.int32, device=device
)
# Persistent buffer for R-SWA per-request prefix lengths so the device
# address stays stable across steps (required for CUDA graph replay).
self.rswa_window: int | None = self.model_config.rswa_window
@@ -1079,19 +1085,18 @@ class FlexAttentionMetadataBuilder(AttentionMetadataBuilder[FlexAttentionMetadat
else self.persistent_kv_num_blocks
)
inverse_block_table = copy_to_persistent(
self.persistent_physical_to_logical, inverse_block_table
)
copy_to_persistent(self.persistent_physical_to_logical, inverse_block_table)
offset_tensor = common_attn_metadata.compute_num_computed_tokens()
offset_tensor = copy_to_persistent(self.persistent_offset_tensor, offset_tensor)
copy_to_persistent(self.persistent_offset_tensor, offset_tensor)
copy_to_persistent(self.persistent_query_start_loc, query_start_loc)
copy_to_persistent(self.persistent_seq_lens, seq_lens)
rswa_prefix_lens = common_attn_metadata.rswa_prefix_lens
if use_rswa and rswa_prefix_lens is not None:
assert self.persistent_rswa_prefix_lens is not None
rswa_prefix_lens = copy_to_persistent(
self.persistent_rswa_prefix_lens, rswa_prefix_lens
)
copy_to_persistent(self.persistent_rswa_prefix_lens, rswa_prefix_lens)
rswa_prefix_lens = self.persistent_rswa_prefix_lens
uses_paged_kv = not isinstance(self.kv_cache_spec, EncoderOnlyAttentionSpec)
logical_mask_mod = (
@@ -1112,10 +1117,12 @@ class FlexAttentionMetadataBuilder(AttentionMetadataBuilder[FlexAttentionMetadat
sliding_window=sliding_window,
num_actual_tokens=num_actual_tokens,
max_query_len=max_query_len,
query_start_loc=query_start_loc,
# Mask closures only index active requests, so retain full-capacity
# tensor shapes to avoid recompiling when the batch shrinks to one.
query_start_loc=self.persistent_query_start_loc,
query_start_loc_cpu=query_start_loc_cpu,
max_seq_len=max_seq_len,
seq_lens=seq_lens,
seq_lens=self.persistent_seq_lens,
block_table=block_table_tensor,
slot_mapping=slot_mapping,
use_cascade=use_cascade,
@@ -1126,9 +1133,9 @@ class FlexAttentionMetadataBuilder(AttentionMetadataBuilder[FlexAttentionMetadat
block_size=block_size,
max_possible_sequence_length=max_possible_seq_len,
num_reqs=num_reqs,
physical_to_logical=inverse_block_table,
physical_to_logical=self.persistent_physical_to_logical,
total_cache_tokens=total_cache_tokens,
decode_offset=offset_tensor,
decode_offset=self.persistent_offset_tensor,
num_blocks_per_seq=num_blocks_per_seq,
uses_paged_kv=uses_paged_kv,
# FIXME(Isotr0py): direct build has issue to build bidirectional