[MRV2] Buffer util simplifications (#56888)
Signed-off-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
@@ -76,14 +76,17 @@ import vllm.utils.torch_utils as torch_utils
|
||||
|
||||
def async_tensor_h2d(
|
||||
data: list | np.ndarray | torch.Tensor,
|
||||
device: str | torch.device,
|
||||
device: str | torch.device | None = None,
|
||||
dtype: torch.dtype | None = None,
|
||||
out: torch.Tensor | None = None,
|
||||
) -> torch.Tensor:
|
||||
if isinstance(data, np.ndarray):
|
||||
data = torch.from_numpy(data)
|
||||
if isinstance(data, torch.Tensor):
|
||||
if not isinstance(data, torch.Tensor):
|
||||
data = torch.tensor(data, dtype=dtype, device="cpu")
|
||||
elif out is None:
|
||||
return data.to(dtype=dtype)
|
||||
return torch.tensor(data, dtype=dtype, device="cpu")
|
||||
return data if out is None else out.copy_(data)
|
||||
|
||||
|
||||
torch_utils.async_tensor_h2d = async_tensor_h2d
|
||||
|
||||
@@ -10,7 +10,6 @@ from vllm.logger import init_logger
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.utils.platform_utils import is_uva_available
|
||||
from vllm.utils.torch_utils import (
|
||||
PIN_MEMORY,
|
||||
async_tensor_h2d,
|
||||
get_accelerator_view_from_cpu_tensor,
|
||||
)
|
||||
@@ -27,24 +26,6 @@ def set_default_max_concurrency(n: int) -> None:
|
||||
_DEFAULT_MAX_CONCURRENCY = max(2, n)
|
||||
|
||||
|
||||
def async_copy_to_gpu(
|
||||
x: torch.Tensor | np.ndarray,
|
||||
out: torch.Tensor | None = None,
|
||||
device: torch.device | None = None,
|
||||
) -> torch.Tensor:
|
||||
if isinstance(x, np.ndarray):
|
||||
x = torch.from_numpy(x)
|
||||
assert x.is_cpu
|
||||
|
||||
if out is None:
|
||||
assert device is not None
|
||||
out = torch.empty_like(x, device=device)
|
||||
|
||||
# pin_memory() is no-op if the memory is already pinned.
|
||||
pinned = x.pin_memory() if PIN_MEMORY else x
|
||||
return out.copy_(pinned, non_blocking=True)
|
||||
|
||||
|
||||
class UvaBuffer:
|
||||
def __init__(self, size: int | Sequence[int], dtype: torch.dtype):
|
||||
if not is_uva_available():
|
||||
|
||||
@@ -64,7 +64,7 @@ from vllm.sequence import IntermediateTensors
|
||||
from vllm.tasks import SupportedTask
|
||||
from vllm.utils.gc_utils import freeze_gc_for_cudagraph_capture
|
||||
from vllm.utils.mem_utils import DeviceMemoryProfiler, format_gib
|
||||
from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE
|
||||
from vllm.utils.torch_utils import STR_DTYPE_TO_TORCH_DTYPE, async_tensor_h2d
|
||||
from vllm.v1.core.sched.output import GrammarOutput, SchedulerOutput
|
||||
from vllm.v1.kv_cache_interface import (
|
||||
KVCacheConfig,
|
||||
@@ -99,10 +99,7 @@ from vllm.v1.worker.gpu.attn_utils import (
|
||||
init_kv_cache,
|
||||
)
|
||||
from vllm.v1.worker.gpu.block_table import BlockTables
|
||||
from vllm.v1.worker.gpu.buffer_utils import (
|
||||
async_copy_to_gpu,
|
||||
set_default_max_concurrency,
|
||||
)
|
||||
from vllm.v1.worker.gpu.buffer_utils import set_default_max_concurrency
|
||||
from vllm.v1.worker.gpu.cp_utils import maybe_prepare_dcp_local_seq_lens
|
||||
from vllm.v1.worker.gpu.cudagraph_utils import (
|
||||
BatchExecutionDescriptor,
|
||||
@@ -1289,7 +1286,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
req_ids = batch_req_state.req_ids
|
||||
num_scheduled_tokens_np = batch_req_state.num_scheduled_tokens
|
||||
idx_mapping_np = batch_req_state.idx_mapping_np
|
||||
idx_mapping = async_copy_to_gpu(idx_mapping_np, device=self.device)
|
||||
idx_mapping = async_tensor_h2d(idx_mapping_np, device=self.device)
|
||||
num_reqs = len(req_ids)
|
||||
|
||||
# Get the number of draft tokens for each request.
|
||||
@@ -1324,7 +1321,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
cu_num_logits_np = np.empty(num_reqs + 1, dtype=np.int32)
|
||||
cu_num_logits_np[0] = 0
|
||||
np.cumsum(num_logits, out=cu_num_logits_np[1:])
|
||||
cu_num_logits = async_copy_to_gpu(cu_num_logits_np, device=self.device)
|
||||
cu_num_logits = async_tensor_h2d(cu_num_logits_np, device=self.device)
|
||||
|
||||
adaptive_verification = (
|
||||
self.adaptive_verification if num_draft_tokens_per_req is not None else None
|
||||
@@ -1352,7 +1349,7 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
# Some attention backends like FA3 require query_start_loc to be non-decreasing.
|
||||
query_start_loc_np[num_reqs + 1 :] = num_tokens
|
||||
query_start_loc = self.input_buffers.query_start_loc
|
||||
async_copy_to_gpu(query_start_loc_np, out=query_start_loc)
|
||||
async_tensor_h2d(query_start_loc_np, out=query_start_loc)
|
||||
if adaptive_verification is not None:
|
||||
cu_num_logits, query_start_loc, total_num_draft_tokens = (
|
||||
adaptive_verification.reallocate_drafts(req_ids, idx_mapping)
|
||||
|
||||
@@ -10,9 +10,9 @@ import torch
|
||||
from vllm.config import CUDAGraphMode, VllmConfig
|
||||
from vllm.distributed.parallel_state import get_dcp_group, get_pcp_group
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.torch_utils import async_tensor_h2d
|
||||
from vllm.v1.attention.backends.utils import PAD_SLOT_ID, get_dcp_local_seq_lens
|
||||
from vllm.v1.worker.gpu.block_table import BlockTables
|
||||
from vllm.v1.worker.gpu.buffer_utils import async_copy_to_gpu
|
||||
from vllm.v1.worker.gpu.input_batch import (
|
||||
InputBatch,
|
||||
InputBuffers,
|
||||
@@ -358,13 +358,13 @@ class PCPManager:
|
||||
dtype=np.int64,
|
||||
)
|
||||
|
||||
self._hidden_restore_idx = async_copy_to_gpu(
|
||||
self._hidden_restore_idx = async_tensor_h2d(
|
||||
hidden_restore_idx, device=self.device
|
||||
)
|
||||
self._padded_gather_idx = async_copy_to_gpu(
|
||||
self._padded_gather_idx = async_tensor_h2d(
|
||||
padded_gather_idx, device=self.device
|
||||
)
|
||||
self._gathered_kv_write_mask = async_copy_to_gpu(
|
||||
self._gathered_kv_write_mask = async_tensor_h2d(
|
||||
gathered_kv_write_mask, device=self.device
|
||||
)
|
||||
return segments_by_rank, per_rank_num_tokens
|
||||
@@ -536,12 +536,12 @@ class PCPManager:
|
||||
local_query_start_loc_out = local_query_start_loc_np[1 : num_local_reqs + 1]
|
||||
np.cumsum(local_num_scheduled_tokens, out=local_query_start_loc_out)
|
||||
local_query_start_loc_np[num_local_reqs + 1 :] = num_local_tokens
|
||||
async_copy_to_gpu(local_query_start_loc_np, out=input_buffers.query_start_loc)
|
||||
async_tensor_h2d(local_query_start_loc_np, out=input_buffers.query_start_loc)
|
||||
local_query_start_loc = input_buffers.query_start_loc[
|
||||
: num_reqs_after_padding + 1
|
||||
]
|
||||
|
||||
local_to_global_req_idx = async_copy_to_gpu(
|
||||
local_to_global_req_idx = async_tensor_h2d(
|
||||
local_to_global_req_idx_np, device=self.device
|
||||
)
|
||||
seq_lens = input_buffers.seq_lens[:num_reqs_after_padding]
|
||||
|
||||
@@ -11,7 +11,7 @@ import torch
|
||||
from vllm.distributed.parallel_state import get_pp_group
|
||||
from vllm.platforms import current_platform
|
||||
from vllm.sequence import IntermediateTensors
|
||||
from vllm.v1.worker.gpu.buffer_utils import async_copy_to_gpu
|
||||
from vllm.utils.torch_utils import async_tensor_h2d
|
||||
from vllm.v1.worker.gpu.input_batch import InputBatch
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ class PPHandler:
|
||||
return None
|
||||
# Filter excluded request indices.
|
||||
idx_mapping_np = np.where(exclude_mask, -1, slot.idx_mapping_np)
|
||||
idx_mapping = async_copy_to_gpu(idx_mapping_np, device=self.device)
|
||||
idx_mapping = async_tensor_h2d(idx_mapping_np, device=self.device)
|
||||
|
||||
self.main_stream.wait_event(slot.event)
|
||||
if slot.draft_tokens is not None and draft_tokens_to_update is not None:
|
||||
@@ -148,7 +148,7 @@ class PPHandler:
|
||||
keep = ~exclude_mask
|
||||
keep_t = torch.as_tensor(keep, device=self.device)
|
||||
draft_tokens = draft_tokens[keep_t]
|
||||
draft_idx_mapping = async_copy_to_gpu(
|
||||
draft_idx_mapping = async_tensor_h2d(
|
||||
slot.idx_mapping_np[keep], device=self.device
|
||||
)
|
||||
draft_tokens_to_update[draft_idx_mapping] = draft_tokens
|
||||
|
||||
@@ -14,6 +14,7 @@ from vllm.config.compilation import CUDAGraphMode
|
||||
from vllm.distributed.parallel_state import get_tp_group
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils.gpu_sync_debug import gpu_sync_allowed
|
||||
from vllm.utils.torch_utils import async_tensor_h2d
|
||||
from vllm.v1.attention.backend import AttentionCGSupport
|
||||
from vllm.v1.utils import CpuGpuBuffer
|
||||
from vllm.v1.worker.gpu.async_utils import StepTimingSample, stream
|
||||
@@ -21,7 +22,6 @@ from vllm.v1.worker.gpu.attn_utils import (
|
||||
get_attn_cg_support,
|
||||
get_query_lens_mismatch_unsupported_backend,
|
||||
)
|
||||
from vllm.v1.worker.gpu.buffer_utils import async_copy_to_gpu
|
||||
|
||||
logger = init_logger(__name__)
|
||||
_PROFILE_REPLAYS = 5
|
||||
@@ -413,7 +413,7 @@ class AdaptiveVerificationManager:
|
||||
if draft_budget == 0:
|
||||
capacities.zero_()
|
||||
else:
|
||||
async_copy_to_gpu(scheduled_drafts, out=capacities)
|
||||
async_tensor_h2d(scheduled_drafts, out=capacities)
|
||||
if draft_budget < int(scheduled_drafts.sum()):
|
||||
_assign_draft_token_budget_compiled(
|
||||
self._confidence_probs,
|
||||
@@ -424,10 +424,7 @@ class AdaptiveVerificationManager:
|
||||
)
|
||||
|
||||
num_non_draft_tokens_gpu = self._num_non_draft_tokens[:num_reqs]
|
||||
async_copy_to_gpu(
|
||||
num_non_draft_tokens,
|
||||
out=num_non_draft_tokens_gpu,
|
||||
)
|
||||
async_tensor_h2d(num_non_draft_tokens, out=num_non_draft_tokens_gpu)
|
||||
self._cu_num_logits[:1].zero_()
|
||||
torch.cumsum(
|
||||
capacities + self.num_bonus_tokens,
|
||||
|
||||
@@ -5,8 +5,7 @@ import torch
|
||||
|
||||
from vllm.triton_utils import tl, triton
|
||||
from vllm.utils.math_utils import cdiv
|
||||
from vllm.utils.torch_utils import PIN_MEMORY
|
||||
from vllm.v1.worker.gpu.buffer_utils import async_copy_to_gpu
|
||||
from vllm.utils.torch_utils import PIN_MEMORY, async_tensor_h2d
|
||||
from vllm.v1.worker.gpu.input_batch import InputBatch
|
||||
|
||||
|
||||
@@ -68,7 +67,7 @@ class StructuredOutputsWorker:
|
||||
|
||||
# Asynchronously copy the bitmask to GPU.
|
||||
with torch.cuda.stream(self.copy_stream):
|
||||
bitmask = async_copy_to_gpu(
|
||||
bitmask = async_tensor_h2d(
|
||||
grammar_bitmask, out=self.grammar_bitmask[: grammar_bitmask.shape[0]]
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user