| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | from collections.abc import Callable, Mapping |
| 4 | |
| 5 | import torch |
| 6 | |
| 7 | from vllm.config.compilation import CUDAGraphMode |
| 8 | from vllm.v1.kv_cache_interface import KVCacheConfig |
| 9 | from vllm.v1.worker.gpu.attn_utils import ( |
| 10 | build_attn_metadata, |
| 11 | build_slot_mappings_by_layer, |
| 12 | ) |
| 13 | from vllm.v1.worker.gpu.block_table import BlockTables |
| 14 | from vllm.v1.worker.gpu.cp_utils import prepare_dcp_local_seq_lens |
| 15 | from vllm.v1.worker.gpu.cudagraph_utils import ( |
| 16 | AttentionState, |
| 17 | BatchExecutionDescriptor, |
| 18 | CudaGraphManager, |
| 19 | ) |
| 20 | from vllm.v1.worker.gpu.input_batch import InputBatch, InputBuffers |
| 21 | from vllm.v1.worker.utils import AttentionGroup |
| 22 | |
| 23 | |
| 24 | def _prepare_dflash_inputs_to_capture( |
| 25 | num_reqs: int, |
| 26 | num_tokens: int, |
| 27 | input_buffers: InputBuffers, |
| 28 | block_tables: BlockTables, |
| 29 | attn_groups: list[list[AttentionGroup]], |
| 30 | kv_cache_config: KVCacheConfig, |
| 31 | max_model_len: int, |
| 32 | skip_attn: bool, |
| 33 | causal: bool | Mapping[int, bool], |
| 34 | ) -> AttentionState: |
| 35 | input_batch = InputBatch.make_dummy(num_reqs, num_tokens, input_buffers) |
| 36 | input_block_tables = block_tables.get_dummy_block_tables(num_reqs) |
| 37 | slot_mappings = block_tables.get_dummy_slot_mappings(num_tokens) |
| 38 | slot_mappings_by_layer = build_slot_mappings_by_layer( |
| 39 | slot_mappings, kv_cache_config |
| 40 | ) |
| 41 | |
| 42 | attn_metadata = None |
| 43 | if not skip_attn: |
| 44 | query_start_loc_cpu = torch.from_numpy(input_batch.query_start_loc_np) |
| 45 | if block_tables.cp_size > 1: |
| 46 | input_batch.dcp_local_seq_lens = prepare_dcp_local_seq_lens( |
| 47 | input_buffers.dcp_local_seq_lens, |
| 48 | input_batch.seq_lens, |
| 49 | input_batch.num_reqs, |
| 50 | block_tables.cp_size, |
| 51 | block_tables.cp_rank, |
| 52 | block_tables.cp_interleave, |
| 53 | num_reqs_padded=input_batch.num_reqs_after_padding, |
| 54 | ) |
| 55 | attn_metadata = build_attn_metadata( |
| 56 | attn_groups=attn_groups, |
| 57 | num_reqs=num_reqs, |
| 58 | num_tokens=num_tokens, |
| 59 | query_start_loc_gpu=input_batch.query_start_loc, |
| 60 | query_start_loc_cpu=query_start_loc_cpu, |
| 61 | max_query_len=num_tokens // num_reqs, |
| 62 | seq_lens=input_batch.seq_lens, |
| 63 | dcp_local_seq_lens=input_batch.dcp_local_seq_lens, |
| 64 | max_seq_len=max_model_len, |
| 65 | block_tables=input_block_tables, |
| 66 | slot_mappings=slot_mappings, |
| 67 | kv_cache_config=kv_cache_config, |
| 68 | for_cudagraph_capture=True, |
| 69 | causal=causal, |
| 70 | ) |
| 71 | return AttentionState(attn_metadata, slot_mappings_by_layer) |
| 72 | |
| 73 | |
| 74 | class DFlashCudaGraphManager(CudaGraphManager): |
| 75 | """DFlash CudaGraphManager for the parallel-drafting query forward, |
| 76 | building its own attention metadata from scratch.""" |
| 77 | |
| 78 | def capture( |
| 79 | self, |
| 80 | forward_fn: Callable, |
| 81 | input_buffers: InputBuffers, |
| 82 | block_tables: BlockTables, |
| 83 | attn_groups: list[list[AttentionGroup]], |
| 84 | kv_cache_config: KVCacheConfig, |
| 85 | max_model_len: int, |
| 86 | causal: bool | Mapping[int, bool], |
| 87 | precompute_context_kv: Callable[[int], None], |
| 88 | progress_bar_desc: str = "Capturing CUDA graphs", |
| 89 | ) -> None: |
| 90 | """``precompute_context_kv(num_reqs)`` is captured ahead of the query |
| 91 | forward.""" |
| 92 | |
| 93 | def create_forward_fn( |
| 94 | desc: BatchExecutionDescriptor, |
| 95 | warmup: bool, |
| 96 | ) -> Callable[[CUDAGraphMode], None]: |
| 97 | num_tokens = desc.num_tokens |
| 98 | num_reqs = desc.num_reqs or min(num_tokens, self.max_num_reqs) |
| 99 | num_tokens_across_dp = ( |
| 100 | torch.full((self.dp_size,), num_tokens, dtype=torch.int32, device="cpu") |
| 101 | if self.dp_size > 1 |
| 102 | else None |
| 103 | ) |
| 104 | attn_state = _prepare_dflash_inputs_to_capture( |
| 105 | num_reqs, |
| 106 | num_tokens, |
| 107 | input_buffers, |
| 108 | block_tables, |
| 109 | attn_groups, |
| 110 | kv_cache_config, |
| 111 | max_model_len, |
| 112 | skip_attn=(desc.cg_mode == CUDAGraphMode.PIECEWISE), |
| 113 | causal=causal, |
| 114 | ) |
| 115 | attn_metadata, slot_mappings = attn_state |
| 116 | |
| 117 | def forward(cg_mode: CUDAGraphMode) -> None: |
| 118 | precompute_context_kv(num_reqs) |
| 119 | forward_fn( |
| 120 | num_reqs, |
| 121 | num_tokens, |
| 122 | attn_metadata, |
| 123 | slot_mappings, |
| 124 | num_tokens_across_dp, |
| 125 | cg_mode, |
| 126 | ) |
| 127 | |
| 128 | return forward |
| 129 | |
| 130 | super().capture(create_forward_fn, progress_bar_desc) |