[Core] Asynchronous Output Processor (#7049)
Co-authored-by: Alexander Matveev <alexm@neuralmagic.com>
This commit is contained in:
co-authored by
Alexander Matveev
parent
015e6cc252
commit
2eedede875
+118
-12
@@ -4,7 +4,8 @@ import random
|
||||
import time
|
||||
from collections import deque
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Deque, Dict, Iterable, List, Optional, Set, Tuple, Union
|
||||
from typing import (Callable, Deque, Dict, Iterable, List, Optional, Set,
|
||||
Tuple, Union)
|
||||
|
||||
from vllm.config import CacheConfig, LoRAConfig, SchedulerConfig
|
||||
from vllm.core.interfaces import AllocStatus, BlockSpaceManager
|
||||
@@ -299,6 +300,7 @@ class Scheduler:
|
||||
cache_config: CacheConfig,
|
||||
lora_config: Optional[LoRAConfig],
|
||||
pipeline_parallel_size: int = 1,
|
||||
output_proc_callback_fn: Optional[Callable] = None,
|
||||
) -> None:
|
||||
self.scheduler_config = scheduler_config
|
||||
self.cache_config = cache_config
|
||||
@@ -364,10 +366,36 @@ class Scheduler:
|
||||
self.num_cumulative_preemption: int = 0
|
||||
|
||||
# Used to cache python objects
|
||||
self._scheduler_running_outputs_cache: PyObjectCache = PyObjectCache(
|
||||
scheduler_running_outputs_builder)
|
||||
self._scheduled_seq_group_cache: PyObjectCache = PyObjectCache(
|
||||
scheduled_seq_group_builder)
|
||||
self._seq_group_metadata_cache: List[PyObjectCache] = []
|
||||
self._scheduler_running_outputs_cache: List[PyObjectCache] = []
|
||||
self._scheduled_seq_group_cache: List[PyObjectCache] = []
|
||||
|
||||
# For async output processing, we need to swap cache buffers between
|
||||
# iterations. I.e. since the output processing is lagged one step,
|
||||
# we cannot reuse the cached objects immediately when the schedule()
|
||||
# is called again, but only when schedule() is called the second time.
|
||||
self.output_proc_callback_fn = output_proc_callback_fn
|
||||
self.use_async_output_proc = self.output_proc_callback_fn is not None
|
||||
self.num_cache_iters = 2 if self.use_async_output_proc else 1
|
||||
|
||||
self.cache_id = 0
|
||||
for i in range(self.num_cache_iters):
|
||||
self._seq_group_metadata_cache.append(
|
||||
PyObjectCache(seq_group_metadata_builder))
|
||||
self._scheduler_running_outputs_cache.append(
|
||||
PyObjectCache(scheduler_running_outputs_builder))
|
||||
self._scheduled_seq_group_cache.append(
|
||||
PyObjectCache(scheduled_seq_group_builder))
|
||||
|
||||
# For async postprocessor, the extra decode run cannot be done
|
||||
# when the request reaches max_model_len. In this case, the request
|
||||
# will be stopped during schedule() call and added to this stop list
|
||||
# for processing and deallocation by the free_finished_seq_groups()
|
||||
self._async_stopped: List[SequenceGroup] = []
|
||||
|
||||
@property
|
||||
def next_cache_id(self):
|
||||
return (self.cache_id + 1) % self.num_cache_iters
|
||||
|
||||
@property
|
||||
def lora_enabled(self) -> bool:
|
||||
@@ -483,7 +511,7 @@ class Scheduler:
|
||||
SchedulerRunningOutputs.
|
||||
"""
|
||||
ret: SchedulerRunningOutputs = \
|
||||
self._scheduler_running_outputs_cache.get_object()
|
||||
self._scheduler_running_outputs_cache[self.cache_id].get_object()
|
||||
ret.blocks_to_swap_out.clear()
|
||||
ret.blocks_to_copy.clear()
|
||||
ret.decode_seq_groups.clear()
|
||||
@@ -510,8 +538,12 @@ class Scheduler:
|
||||
# NOTE(woosuk): Preemption happens only when there is no available slot
|
||||
# to keep all the sequence groups in the RUNNING state.
|
||||
|
||||
running_queue = self.running
|
||||
# Store original running requests for the case of async + preemption
|
||||
if self.use_async_output_proc:
|
||||
orig_running = self.running.copy()
|
||||
|
||||
running_queue = self.running
|
||||
assert len(self._async_stopped) == 0
|
||||
while running_queue:
|
||||
seq_group = running_queue[0]
|
||||
num_running_tokens = self._get_num_new_tokens(
|
||||
@@ -521,6 +553,28 @@ class Scheduler:
|
||||
break
|
||||
|
||||
running_queue.popleft()
|
||||
|
||||
# With async postprocessor, an extra decode run is done
|
||||
# to process the final tokens. The check below avoids this extra
|
||||
# decode run when the model max len is reached, in order to avoid
|
||||
# a memory overflow.
|
||||
if self.use_async_output_proc and seq_group.seqs[0].get_len(
|
||||
) > self.scheduler_config.max_model_len:
|
||||
self._async_stopped.append(seq_group)
|
||||
continue
|
||||
|
||||
# With async postprocessor, when preemption kicks in, we need
|
||||
# first to drain the async postprocessor, so that all async
|
||||
# block_table freeing is applied before the preemption freeing
|
||||
# is applied.
|
||||
if self.use_async_output_proc and not self._can_append_slots(
|
||||
seq_group):
|
||||
tmp = self.running
|
||||
self.running = orig_running
|
||||
assert self.output_proc_callback_fn is not None
|
||||
self.output_proc_callback_fn(is_async=True)
|
||||
self.running = tmp
|
||||
|
||||
while not self._can_append_slots(seq_group):
|
||||
budget.subtract_num_batched_tokens(seq_group.request_id,
|
||||
num_running_tokens)
|
||||
@@ -556,7 +610,7 @@ class Scheduler:
|
||||
is_prefill = seq_group.is_prefill()
|
||||
|
||||
scheduled_seq_group: ScheduledSequenceGroup = \
|
||||
self._scheduled_seq_group_cache.get_object()
|
||||
self._scheduled_seq_group_cache[self.cache_id].get_object()
|
||||
scheduled_seq_group.seq_group = seq_group
|
||||
if is_prefill:
|
||||
scheduled_seq_group.token_chunk_size = num_running_tokens
|
||||
@@ -579,8 +633,8 @@ class Scheduler:
|
||||
if curr_loras is not None and seq_group.lora_int_id > 0:
|
||||
curr_loras.add(seq_group.lora_int_id)
|
||||
|
||||
self._scheduler_running_outputs_cache.reset()
|
||||
self._scheduled_seq_group_cache.reset()
|
||||
self._scheduler_running_outputs_cache[self.next_cache_id].reset()
|
||||
self._scheduled_seq_group_cache[self.next_cache_id].reset()
|
||||
|
||||
return ret
|
||||
|
||||
@@ -1031,17 +1085,31 @@ class Scheduler:
|
||||
num_lookahead_slots=self._get_num_lookahead_slots(is_prefill),
|
||||
)
|
||||
|
||||
def schedule(self) -> Tuple[List[SequenceGroupMetadata], SchedulerOutputs]:
|
||||
def _allow_async_output_proc(self, seq_group: SequenceGroup) -> bool:
|
||||
no_beam_search = (seq_group.sampling_params.best_of == 1
|
||||
and not seq_group.sampling_params.use_beam_search)
|
||||
|
||||
return no_beam_search
|
||||
|
||||
def schedule(
|
||||
self
|
||||
) -> Tuple[List[SequenceGroupMetadata], SchedulerOutputs, bool]:
|
||||
# Schedule sequence groups.
|
||||
# This function call changes the internal states of the scheduler
|
||||
# such as self.running, self.swapped, and self.waiting.
|
||||
scheduler_start_time = time.perf_counter()
|
||||
|
||||
scheduler_outputs = self._schedule()
|
||||
now = time.time()
|
||||
|
||||
if not self.cache_config.enable_prefix_caching:
|
||||
common_computed_block_nums = []
|
||||
|
||||
# TODO: Combine multi-step and async postprocessor
|
||||
allow_async_output_proc: bool = (
|
||||
self.use_async_output_proc
|
||||
and not self.scheduler_config.is_multi_step)
|
||||
|
||||
# Create input data structures.
|
||||
seq_group_metadata_list: List[SequenceGroupMetadata] = []
|
||||
for i, scheduled_seq_group in enumerate(
|
||||
@@ -1050,6 +1118,11 @@ class Scheduler:
|
||||
token_chunk_size = scheduled_seq_group.token_chunk_size
|
||||
seq_group.maybe_set_first_scheduled_time(now)
|
||||
|
||||
seq_group_metadata = self._seq_group_metadata_cache[
|
||||
self.cache_id].get_object()
|
||||
seq_group_metadata.seq_data.clear()
|
||||
seq_group_metadata.block_tables.clear()
|
||||
|
||||
# seq_id -> SequenceData
|
||||
seq_data: Dict[int, SequenceData] = {}
|
||||
# seq_id -> physical block numbers
|
||||
@@ -1139,6 +1212,10 @@ class Scheduler:
|
||||
)
|
||||
seq_group_metadata_list.append(seq_group_metadata)
|
||||
|
||||
if allow_async_output_proc:
|
||||
allow_async_output_proc = self._allow_async_output_proc(
|
||||
seq_group)
|
||||
|
||||
# Now that the batch has been created, we can assume all blocks in the
|
||||
# batch will have been computed before the next scheduling invocation.
|
||||
# This is because the engine assumes that a failure in model execution
|
||||
@@ -1147,6 +1224,8 @@ class Scheduler:
|
||||
self.block_manager.mark_blocks_as_computed(
|
||||
scheduled_seq_group.seq_group)
|
||||
|
||||
self._seq_group_metadata_cache[self.next_cache_id].reset()
|
||||
|
||||
scheduler_time = time.perf_counter() - scheduler_start_time
|
||||
# Add this to scheduler time to all the sequences that are currently
|
||||
# running. This will help estimate if the scheduler is a significant
|
||||
@@ -1158,7 +1237,12 @@ class Scheduler:
|
||||
else:
|
||||
seq_group.metrics.scheduler_time = scheduler_time
|
||||
|
||||
return seq_group_metadata_list, scheduler_outputs
|
||||
# Move to next cache (if exists)
|
||||
self.cache_id = self.next_cache_id
|
||||
|
||||
# Return results
|
||||
return (seq_group_metadata_list, scheduler_outputs,
|
||||
allow_async_output_proc)
|
||||
|
||||
def fork_seq(self, parent_seq: Sequence, child_seq: Sequence) -> None:
|
||||
self.block_manager.fork(parent_seq, child_seq)
|
||||
@@ -1167,6 +1251,12 @@ class Scheduler:
|
||||
"""Free a sequence from a block table."""
|
||||
self.block_manager.free(seq)
|
||||
|
||||
def _free_finished_seqs(self, seq_group: SequenceGroup) -> None:
|
||||
"""Free finished seqs in a sequence group."""
|
||||
for seq in seq_group.get_seqs():
|
||||
if seq.is_finished():
|
||||
self.free_seq(seq)
|
||||
|
||||
def free_finished_seq_groups(self) -> None:
|
||||
remaining: Deque[SequenceGroup] = deque()
|
||||
for seq_group in self.running:
|
||||
@@ -1179,8 +1269,24 @@ class Scheduler:
|
||||
self._finished_requests_ids.append(seq_group.request_id)
|
||||
else:
|
||||
remaining.append(seq_group)
|
||||
|
||||
# Free finished seqs
|
||||
self._free_finished_seqs(seq_group)
|
||||
|
||||
self.running = remaining
|
||||
|
||||
# Handle async stopped sequence groups
|
||||
# (ones that reached max model len)
|
||||
if self._async_stopped:
|
||||
for seq_group in self._async_stopped:
|
||||
self._free_seq_group_cross_attn_blocks(seq_group)
|
||||
self._finished_requests_ids.append(seq_group.request_id)
|
||||
|
||||
# Free finished seqs
|
||||
self._free_finished_seqs(seq_group)
|
||||
|
||||
self._async_stopped.clear()
|
||||
|
||||
def _allocate_and_set_running(self, seq_group: SequenceGroup) -> None:
|
||||
self.block_manager.allocate(seq_group)
|
||||
for seq in seq_group.get_seqs(status=SequenceStatus.WAITING):
|
||||
|
||||
Reference in New Issue
Block a user