Reduce the kernel overhead when num of active loras is smaller than max loras. Multiple cuda graphs are captured for each num of active-loras. (#32005)
Signed-off-by: Yu Gong <yu3.gong@gmail.com>
This commit is contained in:
@@ -5,6 +5,7 @@ from itertools import product
|
||||
from vllm.config import CUDAGraphMode, VllmConfig
|
||||
from vllm.forward_context import BatchDescriptor
|
||||
from vllm.logger import init_logger
|
||||
from vllm.lora.utils import get_captured_lora_counts
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -57,6 +58,11 @@ class CudagraphDispatcher:
|
||||
)
|
||||
|
||||
self.keys_initialized = False
|
||||
self.specialize_lora_count = (
|
||||
self.vllm_config.lora_config.specialize_active_lora
|
||||
if self.vllm_config.lora_config is not None
|
||||
else False
|
||||
)
|
||||
# Default cudagraph_mode to NONE until initialize_cudagraph_keys is called
|
||||
self.cudagraph_mode = CUDAGraphMode.NONE
|
||||
|
||||
@@ -92,8 +98,33 @@ class CudagraphDispatcher:
|
||||
"Use values from cudagraph_capture_sizes."
|
||||
)
|
||||
|
||||
def _get_lora_cases(self) -> list[int]:
|
||||
"""
|
||||
Returns list of has_lora values for CUDA graph capture.
|
||||
This is the single source of truth for LoRA capture cases.
|
||||
"""
|
||||
lora_config = self.vllm_config.lora_config
|
||||
if lora_config is None:
|
||||
# No LoRA configured - single case with no LoRA
|
||||
return [0]
|
||||
|
||||
# LoRA is enabled - capture graphs based on cudagraph_specialize_lora
|
||||
if self.compilation_config.cudagraph_specialize_lora:
|
||||
captured_counts = get_captured_lora_counts(
|
||||
lora_config.max_loras, self.specialize_lora_count
|
||||
)
|
||||
# Specialize: capture separate graphs for with and without LoRA
|
||||
return [0] + captured_counts
|
||||
else:
|
||||
# No specialization: only capture graphs with LoRA active
|
||||
return [lora_config.max_loras + 1]
|
||||
|
||||
def _create_padded_batch_descriptor(
|
||||
self, num_tokens: int, uniform_decode: bool, has_lora: bool
|
||||
self,
|
||||
num_tokens: int,
|
||||
uniform_decode: bool,
|
||||
has_lora: bool,
|
||||
num_active_loras: int = 0,
|
||||
) -> BatchDescriptor:
|
||||
max_num_seqs = self.vllm_config.scheduler_config.max_num_seqs
|
||||
uniform_decode_query_len = self.uniform_decode_query_len
|
||||
@@ -111,6 +142,7 @@ class CudagraphDispatcher:
|
||||
num_reqs=num_reqs,
|
||||
uniform=uniform_decode,
|
||||
has_lora=has_lora,
|
||||
num_active_loras=num_active_loras,
|
||||
)
|
||||
|
||||
def add_cudagraph_key(
|
||||
@@ -135,26 +167,23 @@ class CudagraphDispatcher:
|
||||
|
||||
self._compute_bs_to_padded_graph_size()
|
||||
|
||||
# LoRA activation cases to specialize the cuda graphs on
|
||||
if self.vllm_config.lora_config:
|
||||
if self.compilation_config.cudagraph_specialize_lora:
|
||||
lora_cases = [True, False]
|
||||
else:
|
||||
lora_cases = [True]
|
||||
else:
|
||||
lora_cases = [False]
|
||||
# Get LoRA cases to capture
|
||||
lora_cases = self._get_lora_cases()
|
||||
self.captured_lora_counts = [
|
||||
lora_count for lora_count in lora_cases if lora_count
|
||||
]
|
||||
|
||||
# Note: we create all valid keys for cudagraph here but do not
|
||||
# guarantee all keys would be used. For example, if we allow lazy
|
||||
# capturing in future PR, some keys may never be triggered.
|
||||
if cudagraph_mode.mixed_mode() != CUDAGraphMode.NONE:
|
||||
for bs, has_lora in product(
|
||||
for bs, num_active_loras in product(
|
||||
self.compilation_config.cudagraph_capture_sizes, lora_cases
|
||||
):
|
||||
self.add_cudagraph_key(
|
||||
cudagraph_mode.mixed_mode(),
|
||||
self._create_padded_batch_descriptor(
|
||||
bs, False, has_lora
|
||||
bs, False, num_active_loras > 0, num_active_loras
|
||||
).relax_for_mixed_batch_cudagraphs(),
|
||||
)
|
||||
|
||||
@@ -173,10 +202,14 @@ class CudagraphDispatcher:
|
||||
for x in self.compilation_config.cudagraph_capture_sizes
|
||||
if x <= max_num_tokens and x >= uniform_decode_query_len
|
||||
]
|
||||
for bs, has_lora in product(cudagraph_capture_sizes_for_decode, lora_cases):
|
||||
for bs, num_active_loras in product(
|
||||
cudagraph_capture_sizes_for_decode, lora_cases
|
||||
):
|
||||
self.add_cudagraph_key(
|
||||
CUDAGraphMode.FULL,
|
||||
self._create_padded_batch_descriptor(bs, True, has_lora),
|
||||
self._create_padded_batch_descriptor(
|
||||
bs, True, num_active_loras > 0, num_active_loras
|
||||
),
|
||||
)
|
||||
|
||||
self.keys_initialized = True
|
||||
@@ -187,6 +220,7 @@ class CudagraphDispatcher:
|
||||
uniform_decode: bool = False,
|
||||
has_lora: bool = False,
|
||||
disable_full: bool = False,
|
||||
num_active_loras: int = 0,
|
||||
) -> tuple[CUDAGraphMode, BatchDescriptor]:
|
||||
"""
|
||||
Given conditions(e.g.,batch descriptor and if using piecewise only),
|
||||
@@ -202,6 +236,7 @@ class CudagraphDispatcher:
|
||||
disable_full: If True, skip FULL cudagraph checks and
|
||||
return PIECEWISE or NONE only. (can be used for features like
|
||||
cascade attention that are not supported by full cudagraphs)
|
||||
num_active_loras: Number of distinct active LoRA adapters.
|
||||
"""
|
||||
if (
|
||||
not self.keys_initialized
|
||||
@@ -210,8 +245,24 @@ class CudagraphDispatcher:
|
||||
):
|
||||
return CUDAGraphMode.NONE, BatchDescriptor(num_tokens)
|
||||
|
||||
effective_num_active_loras = num_active_loras
|
||||
if has_lora and num_active_loras > 0:
|
||||
if self.specialize_lora_count:
|
||||
# Find the smallest captured `num_active_loras` that is >= the current
|
||||
# `num_active_loras`. This is because we only capture graphs for
|
||||
# a subset of possible `num_active_loras` values (powers of 2).
|
||||
import bisect
|
||||
|
||||
idx = bisect.bisect_left(self.captured_lora_counts, num_active_loras)
|
||||
if idx < len(self.captured_lora_counts):
|
||||
effective_num_active_loras = self.captured_lora_counts[idx]
|
||||
else:
|
||||
# When not specializing, graphs are captured only with max_loras + 1,
|
||||
# so we must use max_loras + 1 for dispatch to find a matching graph.
|
||||
effective_num_active_loras = self.vllm_config.lora_config.max_loras + 1
|
||||
|
||||
batch_desc = self._create_padded_batch_descriptor(
|
||||
num_tokens, uniform_decode, has_lora
|
||||
num_tokens, uniform_decode, has_lora, effective_num_active_loras
|
||||
)
|
||||
relaxed_batch_desc = batch_desc.relax_for_mixed_batch_cudagraphs()
|
||||
|
||||
|
||||
@@ -3082,6 +3082,7 @@ class GPUModelRunner(
|
||||
# be improved in model runner v2)
|
||||
force_uniform_decode: bool | None = None,
|
||||
force_has_lora: bool | None = None,
|
||||
force_num_active_loras: int | None = None,
|
||||
num_encoder_reqs: int = 0,
|
||||
) -> tuple[
|
||||
CUDAGraphMode,
|
||||
@@ -3103,11 +3104,13 @@ class GPUModelRunner(
|
||||
self.model_config.is_encoder_decoder and num_encoder_reqs > 0
|
||||
)
|
||||
|
||||
has_lora = (
|
||||
len(self.input_batch.lora_id_to_lora_request) > 0
|
||||
if force_has_lora is None
|
||||
else force_has_lora
|
||||
# Compute LoRA state for cudagraph dispatch
|
||||
num_active_loras = (
|
||||
force_num_active_loras
|
||||
if force_num_active_loras is not None
|
||||
else len(self.input_batch.lora_id_to_lora_request)
|
||||
)
|
||||
has_lora = num_active_loras > 0 if force_has_lora is None else force_has_lora
|
||||
|
||||
num_tokens_padded = self._pad_for_sequence_parallelism(num_tokens)
|
||||
dispatch_cudagraph = (
|
||||
@@ -3116,6 +3119,7 @@ class GPUModelRunner(
|
||||
has_lora=has_lora,
|
||||
uniform_decode=uniform_decode,
|
||||
disable_full=disable_full,
|
||||
num_active_loras=num_active_loras,
|
||||
)
|
||||
if not force_eager
|
||||
else (CUDAGraphMode.NONE, BatchDescriptor(num_tokens_padded))
|
||||
@@ -4606,8 +4610,8 @@ class GPUModelRunner(
|
||||
is_profile: bool = False,
|
||||
create_mixed_batch: bool = False,
|
||||
remove_lora: bool = True,
|
||||
activate_lora: bool = False,
|
||||
is_graph_capturing: bool = False,
|
||||
num_active_loras: int = 0,
|
||||
) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
"""
|
||||
Run a dummy forward pass to warm up/profile run or capture the
|
||||
@@ -4630,7 +4634,8 @@ class GPUModelRunner(
|
||||
create_mixed_batch: If True, create a mixed batch with both decode
|
||||
(1 token) and prefill (multiple tokens) requests.
|
||||
remove_lora: If False, dummy LoRAs are not destroyed after the run
|
||||
activate_lora: If False, dummy_run is performed without LoRAs.
|
||||
num_active_loras: Number of distinct active LoRAs to capture for.
|
||||
LoRA is activated when num_active_loras > 0.
|
||||
"""
|
||||
mm_config = self.vllm_config.model_config.multimodal_config
|
||||
if mm_config and mm_config.mm_encoder_only:
|
||||
@@ -4712,7 +4717,10 @@ class GPUModelRunner(
|
||||
# `force_has_lora` is used for cudagraph capture; because LoRA is
|
||||
# activated later in the context manager, but we need to know the
|
||||
# LoRA state when determining the batch descriptor for capture
|
||||
force_has_lora=activate_lora,
|
||||
force_has_lora=num_active_loras > 0,
|
||||
# `force_num_active_loras` is used for cudagraph capture; because we
|
||||
# need to capture graphs for specific num_active_loras counts
|
||||
force_num_active_loras=num_active_loras,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -4782,8 +4790,8 @@ class GPUModelRunner(
|
||||
self.lora_config,
|
||||
num_scheduled_tokens,
|
||||
num_sampled_tokens,
|
||||
activate_lora,
|
||||
remove_lora,
|
||||
num_active_loras,
|
||||
):
|
||||
# Make sure padding doesn't exceed max_num_tokens
|
||||
assert num_tokens_padded <= self.max_num_tokens
|
||||
@@ -4884,7 +4892,10 @@ class GPUModelRunner(
|
||||
# lora cases when cudagraph_specialize_lora is enabled. This is a
|
||||
# short term mitigation for issue mentioned in
|
||||
# https://github.com/vllm-project/vllm/issues/28334
|
||||
if self.compilation_config.cudagraph_specialize_lora and activate_lora:
|
||||
if (
|
||||
self.compilation_config.cudagraph_specialize_lora
|
||||
and num_active_loras > 0
|
||||
):
|
||||
use_cudagraphs = False
|
||||
|
||||
self.drafter.dummy_run(
|
||||
@@ -5259,7 +5270,7 @@ class GPUModelRunner(
|
||||
# We skip EPLB here since we don't want to record dummy metrics
|
||||
for batch_desc in batch_descriptors:
|
||||
num_tokens = batch_desc.num_tokens
|
||||
activate_lora = batch_desc.has_lora
|
||||
num_active_loras = batch_desc.num_active_loras
|
||||
|
||||
# We currently only capture ubatched graphs when its a FULL
|
||||
# cudagraph, a uniform decode batch, and the number of tokens
|
||||
@@ -5286,7 +5297,7 @@ class GPUModelRunner(
|
||||
num_tokens,
|
||||
cudagraph_runtime_mode=CUDAGraphMode.NONE,
|
||||
allow_microbatching=allow_microbatching,
|
||||
activate_lora=activate_lora,
|
||||
num_active_loras=num_active_loras,
|
||||
)
|
||||
|
||||
# Capture run
|
||||
@@ -5294,7 +5305,7 @@ class GPUModelRunner(
|
||||
num_tokens,
|
||||
cudagraph_runtime_mode=cudagraph_runtime_mode,
|
||||
allow_microbatching=allow_microbatching,
|
||||
activate_lora=activate_lora,
|
||||
num_active_loras=num_active_loras,
|
||||
is_graph_capturing=True,
|
||||
)
|
||||
self.maybe_remove_all_loras(self.lora_config)
|
||||
|
||||
@@ -133,11 +133,23 @@ class LoRAModelRunnerMixin:
|
||||
num_scheduled_tokens: np.ndarray,
|
||||
mapping_type: LoRAMappingType = LoRAMappingType.LANGUAGE,
|
||||
num_sampled_tokens: np.ndarray | None = None,
|
||||
activate_lora: bool = True,
|
||||
num_active_loras: int = 0,
|
||||
):
|
||||
"""
|
||||
Context manager to select dummy LoRAs for capture/warmup.
|
||||
|
||||
Args:
|
||||
lora_config: LoRA configuration, or None if LoRA is disabled.
|
||||
num_scheduled_tokens: Array of scheduled token counts per request.
|
||||
num_sampled_tokens: Array of sampled token counts per request.
|
||||
num_active_loras: Number of distinct active LoRAs to use.
|
||||
- 0: No LoRA active (set up zero mappings).
|
||||
- >0: Use exactly this many distinct LoRAs.
|
||||
"""
|
||||
if num_sampled_tokens is None:
|
||||
num_sampled_tokens = np.ones_like(num_scheduled_tokens, dtype=np.int32)
|
||||
|
||||
# Skip LoRA setup entirely only if no LoRA config
|
||||
if lora_config is None:
|
||||
yield
|
||||
else:
|
||||
@@ -145,15 +157,52 @@ class LoRAModelRunnerMixin:
|
||||
assert self.lora_manager is not None, "LoRA is not enabled"
|
||||
|
||||
num_reqs = len(num_scheduled_tokens)
|
||||
num_loras = lora_config.max_loras
|
||||
max_loras = lora_config.max_loras
|
||||
|
||||
# Determine how many distinct LoRAs to use and whether to include
|
||||
# no-LoRA tokens (-1 entries).
|
||||
# When num_active_loras > max_loras (e.g., max_loras + 1), we need
|
||||
# to include -1 entries to simulate batches with both LoRA and
|
||||
# no-LoRA tokens. This ensures prepare_tensors computes the correct
|
||||
# num_active_loras that matches the cudagraph capture key.
|
||||
if num_active_loras == 0:
|
||||
# No LoRA active - use 0 mappings like the original code
|
||||
effective_num_loras = 0
|
||||
include_no_lora = False
|
||||
elif num_active_loras > max_loras:
|
||||
# num_active_loras > max_loras means we want max_loras adapters
|
||||
# PLUS no-LoRA tokens (-1). This is the max_loras + 1 case.
|
||||
effective_num_loras = max_loras
|
||||
include_no_lora = True
|
||||
else:
|
||||
# Specific number of active LoRAs requested
|
||||
effective_num_loras = min(num_active_loras, max_loras)
|
||||
include_no_lora = False
|
||||
|
||||
# Make prompt lora mapping
|
||||
# Assign LoRA IDs cyclically to simulate a worst-case scenario.
|
||||
if activate_lora:
|
||||
prompt_lora_mapping = (
|
||||
np.arange(num_reqs, dtype=np.int32) % num_loras
|
||||
) + 1
|
||||
# LoRA IDs are 1-indexed (1 to max_loras) as required by LoRARequest.
|
||||
# convert_mapping() will convert these to 0-indexed slot indices.
|
||||
if effective_num_loras > 0:
|
||||
if include_no_lora:
|
||||
# Include -1 (no-LoRA) entries by cycling through
|
||||
# -1, 1, 2, ..., effective_num_loras
|
||||
# This ensures prepare_tensors sees both LoRA and no-LoRA
|
||||
# tokens, computing num_active_loras = effective_num_loras+1
|
||||
cycle_values = np.array(
|
||||
list(range(1, effective_num_loras + 1)),
|
||||
dtype=np.int32,
|
||||
)
|
||||
prompt_lora_mapping = cycle_values[
|
||||
np.arange(num_reqs, dtype=np.int32) % len(cycle_values)
|
||||
]
|
||||
else:
|
||||
# Use 1 to effective_num_loras (1-indexed lora IDs)
|
||||
prompt_lora_mapping = (
|
||||
np.arange(num_reqs, dtype=np.int32) % effective_num_loras
|
||||
) + 1
|
||||
else:
|
||||
# No LoRA active - use 0 for all tokens (original behavior)
|
||||
prompt_lora_mapping = np.zeros(num_reqs, dtype=np.int32)
|
||||
|
||||
# Make sample lora mapping
|
||||
@@ -162,14 +211,14 @@ class LoRAModelRunnerMixin:
|
||||
# Make token lora mapping
|
||||
token_lora_mapping = np.repeat(prompt_lora_mapping, num_scheduled_tokens)
|
||||
|
||||
# Make dummy lora requests
|
||||
# Make dummy lora requests (only for the active LoRAs)
|
||||
lora_requests: set[LoRARequest] = {
|
||||
LoRARequest(
|
||||
lora_name=f"warmup_{lora_id}",
|
||||
lora_int_id=lora_id,
|
||||
lora_path="/not/a/real/path",
|
||||
)
|
||||
for lora_id in range(1, num_loras + 1)
|
||||
for lora_id in range(1, effective_num_loras + 1)
|
||||
}
|
||||
|
||||
self._set_active_loras(
|
||||
@@ -187,10 +236,21 @@ class LoRAModelRunnerMixin:
|
||||
lora_config: LoRAConfig | None,
|
||||
num_scheduled_tokens: np.ndarray,
|
||||
num_sampled_tokens: np.ndarray,
|
||||
activate_lora: bool = True,
|
||||
remove_lora: bool = True,
|
||||
num_active_loras: int = 0,
|
||||
mapping_type: LoRAMappingType = LoRAMappingType.LANGUAGE,
|
||||
):
|
||||
"""
|
||||
Context manager for dummy runs with LoRA.
|
||||
|
||||
Args:
|
||||
lora_config: LoRA configuration.
|
||||
num_scheduled_tokens: Array of scheduled token counts per request.
|
||||
num_sampled_tokens: Array of sampled token counts per request.
|
||||
remove_lora: Whether to remove LoRAs after the context exits.
|
||||
num_active_loras: Number of distinct active LoRAs to use.
|
||||
LoRA is activated when num_active_loras > 0.
|
||||
"""
|
||||
with (
|
||||
self.maybe_setup_dummy_loras(lora_config, remove_lora),
|
||||
self.maybe_select_dummy_loras(
|
||||
@@ -198,7 +258,7 @@ class LoRAModelRunnerMixin:
|
||||
num_scheduled_tokens,
|
||||
mapping_type,
|
||||
num_sampled_tokens,
|
||||
activate_lora,
|
||||
num_active_loras,
|
||||
),
|
||||
):
|
||||
yield
|
||||
|
||||
Reference in New Issue
Block a user