Read code changes as Git diffs. Identifying strings are masked; binary files are listed by their full relative names.
vllm_ascend · 1 changed files
Base commit: 7c254c14e2f2db1185d739075ee3cdc1da3cfde2
diff --git a/vllm_ascend/attention/attention_v1.py b/vllm_ascend/attention/attention_v1.pyindex 1f78d080a8161b72da835db2b3487a111c114eed..c9e282ffaa9f661d8bdb3e11c2023adca87430c0 100644--- a/vllm_ascend/attention/attention_v1.py+++ b/vllm_ascend/attention/attention_v1.py@@ -72,6 +72,9 @@ from vllm_ascend.worker.kvcomp_utils import KVCompMetaData # default max value of sliding window size SWA_INT_MAX = 2147483647 _ATTN_KEYS_BUFFER = None+# Number of reusable pinned staging buffers kept by AscendAttentionMetadataBuilder+# for the per-step ``query_start_loc`` host-to-device copy.+_QUERY_START_LOC_PINNED_RING_SIZE = 8 @register_backend(AttentionBackendEnum.CUSTOM, "ASCEND")@@ -259,6 +262,14 @@ class AscendAttentionMetadataBuilder(AttentionMetadataBuilder[AscendMetadata]): scheduler_config = vllm_config.scheduler_config self.chunked_prefill_enabled = scheduler_config.enable_chunked_prefill self.attn_mask_builder = AttentionMaskBuilder(self.device)+ # Reusable pinned staging buffers for ``query_start_loc``. Allocating+ # pinned host memory is expensive on NPU, and ``build()`` runs once per+ # engine step, so a small ring of buffers is allocated lazily at the+ # largest size seen and sliced per step instead of being (re)created by+ # ``pin_memory()`` every step. A ring is used because the host-side fill+ # is not stream-ordered with respect to the queued H2D copies.+ self._query_start_loc_pinned: list[torch.Tensor] = []+ self._query_start_loc_pinned_next = 0 @classmethod def get_cudagraph_support(@@ -312,7 +323,21 @@ class AscendAttentionMetadataBuilder(AttentionMetadataBuilder[AscendMetadata]): attn_mask = self.attn_mask_builder.get_attention_mask(common_attn_metadata.causal, self.model_config) # TODO: Yet another unnecessary H2D while we already have a query_start_loc on device- query_start_loc = query_start_loc_cpu.pin_memory().to(self.device, non_blocking=True)+ num_query_start_loc = query_start_loc_cpu.shape[0]+ if not self._query_start_loc_pinned or any(+ buffer.numel() < num_query_start_loc or buffer.dtype != query_start_loc_cpu.dtype+ for buffer in self._query_start_loc_pinned+ ):+ # (Re)create the whole ring if the required size or dtype changed.+ self._query_start_loc_pinned = [+ torch.empty(max(num_query_start_loc, 1), dtype=query_start_loc_cpu.dtype, pin_memory=True)+ for _ in range(_QUERY_START_LOC_PINNED_RING_SIZE)+ ]+ ring_index = self._query_start_loc_pinned_next % len(self._query_start_loc_pinned)+ self._query_start_loc_pinned_next += 1+ query_start_loc_pinned = self._query_start_loc_pinned[ring_index][:num_query_start_loc]+ query_start_loc_pinned.copy_(query_start_loc_cpu, non_blocking=True)+ query_start_loc = query_start_loc_pinned.to(self.device, non_blocking=True) actual_seq_lengths_q = query_start_loc_cpu[1:].tolist() seq_lens_list = seq_lens.tolist()