@@ -6425,6 +6425,31 @@ def profile_run(self) -> None:
64256425 self .encoder_cache .clear ()
64266426 gc .collect ()
64276427
6428+ @staticmethod
6429+ def _get_minimal_kv_cache_blocks_for_cudagraph_profiling (
6430+ kv_cache_groups : list [KVCacheGroupSpec ],
6431+ max_capture_tokens : int | None ,
6432+ max_num_reqs : int ,
6433+ cp_size : int ,
6434+ ) -> tuple [int , bool ]:
6435+ if max_capture_tokens is None :
6436+ return 1 , False
6437+
6438+ block_requirements : list [int ] = []
6439+ has_mamba_cache = False
6440+ for group in kv_cache_groups :
6441+ kv_cache_spec = group .kv_cache_spec
6442+ if isinstance (kv_cache_spec , EncoderOnlyAttentionSpec ):
6443+ continue
6444+ if isinstance (kv_cache_spec , MambaSpec ):
6445+ has_mamba_cache = True
6446+ block_requirements .append (max_num_reqs )
6447+ else :
6448+ block_requirements .append (
6449+ cdiv (max_capture_tokens , kv_cache_spec .block_size * cp_size )
6450+ )
6451+ return max (1 , * block_requirements ), has_mamba_cache
6452+
64286453 def _init_minimal_kv_cache_for_profiling (self ) -> None :
64296454 from vllm .v1 .core .kv_cache_utils import (
64306455 get_kv_cache_config_from_groups ,
@@ -6435,27 +6460,16 @@ def _init_minimal_kv_cache_for_profiling(self) -> None:
64356460 KVCacheSpecRegistry .check_kv_cache_spec_registry (kv_cache_spec )
64366461 kv_cache_groups = get_kv_cache_groups (self .vllm_config , kv_cache_spec )
64376462 max_capture_tokens = self .compilation_config .max_cudagraph_capture_size
6438- if max_capture_tokens is None :
6439- min_blocks = 1
6440- else :
6441- cp_size = get_total_cp_world_size ()
6442- block_requirements : list [int ] = []
6443- has_mamba_cache = False
6444- for group in kv_cache_groups :
6445- kv_cache_spec = group .kv_cache_spec
6446- if isinstance (kv_cache_spec , EncoderOnlyAttentionSpec ):
6447- continue
6448- if isinstance (kv_cache_spec , MambaSpec ):
6449- has_mamba_cache = True
6450- block_requirements .append (self .max_num_reqs )
6451- else :
6452- block_requirements .append (
6453- cdiv (max_capture_tokens , kv_cache_spec .block_size * cp_size )
6454- )
6455- min_blocks = max (
6456- 1 ,
6457- * block_requirements ,
6463+ cp_size = get_total_cp_world_size ()
6464+ min_blocks , has_mamba_cache = (
6465+ self ._get_minimal_kv_cache_blocks_for_cudagraph_profiling (
6466+ kv_cache_groups ,
6467+ max_capture_tokens ,
6468+ self .max_num_reqs ,
6469+ cp_size ,
64586470 )
6471+ )
6472+ if max_capture_tokens is not None :
64596473 logger .info (
64606474 "Using %d KV blocks for CUDA graph profiling "
64616475 "(max_capture_tokens=%d, cp_size=%d, has_mamba_cache=%s)" ,
0 commit comments