[Core] Remove busy loop from idle buffer readers (#28053)
Signed-off-by: Joe Runde <Joseph.Runde@ibm.com> Signed-off-by: Travis Johnson <tsjohnso@us.ibm.com> Signed-off-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Travis Johnson <tsjohnso@us.ibm.com> Co-authored-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
co-authored by
Travis Johnson
Nick Hill
parent
5d199ac8f2
commit
6f0dd93801
@@ -2,13 +2,13 @@
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
import functools
|
||||
import pickle
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass, field
|
||||
from multiprocessing import shared_memory
|
||||
from pickle import PickleBuffer
|
||||
from threading import Event
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -18,6 +18,7 @@ import zmq
|
||||
from torch.distributed import ProcessGroup
|
||||
from zmq import ( # type: ignore
|
||||
IPV6, # type: ignore
|
||||
PUB,
|
||||
SUB,
|
||||
SUBSCRIBE,
|
||||
XPUB,
|
||||
@@ -32,6 +33,7 @@ from vllm.platforms import current_platform
|
||||
from vllm.utils.network_utils import (
|
||||
get_ip,
|
||||
get_open_port,
|
||||
get_open_zmq_inproc_path,
|
||||
get_open_zmq_ipc_path,
|
||||
is_valid_ipv6_address,
|
||||
)
|
||||
@@ -78,50 +80,125 @@ def to_bytes_big(value: int, size: int) -> bytes:
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
def long_wait_time_msg(threshold: int) -> str:
|
||||
return (
|
||||
"No available shared memory broadcast block found "
|
||||
f"in {threshold} seconds. This typically happens "
|
||||
"when some processes are hanging or doing some "
|
||||
"time-consuming work (e.g. compilation, "
|
||||
"weight/kv cache quantization)."
|
||||
)
|
||||
LONG_WAIT_TIME_LOG_MSG = (
|
||||
"No available shared memory broadcast block found "
|
||||
"in %d seconds. This typically happens "
|
||||
"when some processes are hanging or doing some "
|
||||
"time-consuming work (e.g. compilation, "
|
||||
"weight/kv cache quantization)."
|
||||
)
|
||||
|
||||
|
||||
class SpinTimer:
|
||||
def record_activity(self):
|
||||
pass
|
||||
|
||||
def spin(self):
|
||||
sched_yield()
|
||||
|
||||
|
||||
class SpinSleepTimer(SpinTimer):
|
||||
class SpinCondition:
|
||||
"""
|
||||
In setups which have long inactivity periods it is desirable to reduce
|
||||
system power consumption when vllm does nothing. This would lead to more
|
||||
CPU thermal headroom when a request eventually comes, especially when
|
||||
multiple GPUs are connected as each GPU would otherwise pin one thread at
|
||||
100% CPU usage.
|
||||
This class implements an interface similar to a threading.Condition. It
|
||||
allows a writer to notify readers to wake up and read from the shared memory
|
||||
buffer. This notification is done over a zmq socket.
|
||||
|
||||
The simplest solution is to reduce polling frequency when there is no
|
||||
activity for a certain period of time.
|
||||
For optimal performance under load we don't want the readers to need to poll
|
||||
the zmq socket for every read. So the `wait` method here will return
|
||||
immediately when reads are frequent, and will only enter "idle mode" and
|
||||
await a notification on the zmq socket after a period of inactivity. This
|
||||
allows the readers to spin quickly, hence "SpinCondition".
|
||||
|
||||
To support clean shutdown, a separate thread in the reader's process must be
|
||||
able to wake the reader so that it can exit. A separate cancel() method is
|
||||
implemented with an in-process socket to allow this interruption.
|
||||
"""
|
||||
|
||||
def __init__(self, busy_loop_s: float = 3.0, wait_sleep_s: float = 0.1):
|
||||
self.last_activity = time.monotonic()
|
||||
self.busy_loop_s = busy_loop_s
|
||||
self.wait_sleep_s = wait_sleep_s
|
||||
def __init__(
|
||||
self,
|
||||
is_reader: bool,
|
||||
context: zmq.Context,
|
||||
notify_address: str,
|
||||
busy_loop_s: float = 1,
|
||||
):
|
||||
self.is_reader = is_reader
|
||||
|
||||
def record_activity(self):
|
||||
self.last_activity = time.monotonic()
|
||||
if is_reader:
|
||||
# Time of last shm buffer read
|
||||
self.last_read = time.monotonic()
|
||||
|
||||
def spin(self):
|
||||
curr_time = time.monotonic()
|
||||
if curr_time >= self.last_activity + self.busy_loop_s:
|
||||
time.sleep(self.wait_sleep_s)
|
||||
# Time to keep busy-looping on the shm buffer before going idle
|
||||
self.busy_loop_s = busy_loop_s
|
||||
|
||||
# Readers subscribe to write notifications
|
||||
self.local_notify_socket: zmq.Socket = context.socket(SUB)
|
||||
# Set zmq.CONFLATE to only keep the last message that the socket
|
||||
# receives. This prevents us from piling up notification messages
|
||||
# under high load when we aren't polling the socket.
|
||||
self.local_notify_socket.setsockopt(zmq.CONFLATE, 1)
|
||||
# Subscribe to all messages on the socket
|
||||
self.local_notify_socket.setsockopt_string(SUBSCRIBE, "")
|
||||
self.local_notify_socket.connect(notify_address)
|
||||
|
||||
# Readers require a process-local socket to poll for cancellation
|
||||
cancel_path = get_open_zmq_inproc_path()
|
||||
self.write_cancel_socket: zmq.Socket = context.socket(zmq.PAIR)
|
||||
self.write_cancel_socket.bind(cancel_path)
|
||||
self.read_cancel_socket: zmq.Socket = context.socket(zmq.PAIR)
|
||||
self.read_cancel_socket.connect(cancel_path)
|
||||
|
||||
# Poller allows waiting on either `.notify()` or `.cancel()`
|
||||
self.poller = zmq.Poller()
|
||||
self.poller.register(self.read_cancel_socket, zmq.POLLIN)
|
||||
self.poller.register(self.local_notify_socket, zmq.POLLIN)
|
||||
else:
|
||||
# Writer side publishes write notifications
|
||||
self.local_notify_socket: zmq.Socket = context.socket(PUB) # type: ignore
|
||||
# Set high water mark to 1 - we don't need to send a massive amount of
|
||||
# pings during busy operation. PUB sockets will silently drop subsequent
|
||||
# messages after the high water mark is reached.
|
||||
self.local_notify_socket.setsockopt(zmq.SNDHWM, 1)
|
||||
self.local_notify_socket.bind(notify_address)
|
||||
|
||||
self.last_read = 0
|
||||
self.busy_loop_s = 0
|
||||
self.read_cancel_socket = None
|
||||
self.write_cancel_socket = None
|
||||
self.poller = None
|
||||
|
||||
def record_read(self):
|
||||
self.last_read = time.monotonic()
|
||||
|
||||
def cancel(self):
|
||||
# Sends cancellation ping that will cause the reader to wake up.
|
||||
# This is done from a monitor thread in the same process as the reader.
|
||||
if self.is_reader:
|
||||
logger.debug("Canceling waiting reads on SHM Buffer")
|
||||
self.write_cancel_socket.send(b"\x00")
|
||||
|
||||
def wait(self, timeout_ms: int | None = None) -> None:
|
||||
"""Wait for data on the shared memory buffer.
|
||||
|
||||
Yields the scheduler then returns immediately if it has been less than
|
||||
self.busy_loop_s since the last read.
|
||||
|
||||
Otherwise, enters idle mode and awaits a socket ping for at most
|
||||
`timeout_ms` milliseconds, or indefinitely if timeout_ms is None.
|
||||
"""
|
||||
assert self.is_reader, "Only readers can wait"
|
||||
|
||||
current_time = time.monotonic()
|
||||
if current_time <= self.last_read + self.busy_loop_s:
|
||||
sched_yield()
|
||||
else:
|
||||
events = dict(self.poller.poll(timeout=timeout_ms))
|
||||
|
||||
if self.read_cancel_socket in events:
|
||||
logger.debug("Poller received cancel event")
|
||||
elif self.local_notify_socket in events:
|
||||
logger.debug("Poller received notify event")
|
||||
# Since zmq.CONFLATE is set, there will only be one notification
|
||||
# to read from the socket
|
||||
self.local_notify_socket.recv(flags=zmq.NOBLOCK, copy=False)
|
||||
else:
|
||||
logger.debug("Poller timed out")
|
||||
|
||||
def notify(self):
|
||||
"""Notifies all readers to wake up"""
|
||||
assert not self.is_reader, "Only writers can notify"
|
||||
self.local_notify_socket.send(b"\x00")
|
||||
|
||||
|
||||
class ShmRingBuffer:
|
||||
@@ -265,6 +342,7 @@ class Handle:
|
||||
|
||||
buffer_handle: tuple[int, int, int, str] | None = None
|
||||
local_subscribe_addr: str | None = None
|
||||
local_notify_addr: str | None = None
|
||||
remote_subscribe_addr: str | None = None
|
||||
remote_addr_ipv6: bool = False
|
||||
|
||||
@@ -288,7 +366,7 @@ class MessageQueue:
|
||||
self.n_local_reader = n_local_reader
|
||||
n_remote_reader = n_reader - n_local_reader
|
||||
self.n_remote_reader = n_remote_reader
|
||||
|
||||
self.shutting_down = False
|
||||
context = Context()
|
||||
|
||||
if n_local_reader > 0:
|
||||
@@ -310,11 +388,19 @@ class MessageQueue:
|
||||
self.local_socket.bind(local_subscribe_addr)
|
||||
|
||||
self.current_idx = 0
|
||||
|
||||
# Create the notification side of the SpinCondition
|
||||
local_notify_addr = get_open_zmq_ipc_path()
|
||||
self._spin_condition = SpinCondition(
|
||||
is_reader=False, context=context, notify_address=local_notify_addr
|
||||
)
|
||||
else:
|
||||
self.buffer = None # type: ignore
|
||||
local_subscribe_addr = None
|
||||
self.local_socket = None
|
||||
self.current_idx = -1
|
||||
local_notify_addr = None
|
||||
self._spin_condition = None # type: ignore
|
||||
|
||||
remote_addr_ipv6 = False
|
||||
if n_remote_reader > 0:
|
||||
@@ -341,12 +427,12 @@ class MessageQueue:
|
||||
self.local_reader_rank = -1
|
||||
# rank does not matter for remote readers
|
||||
self._is_remote_reader = False
|
||||
self._read_spin_timer = SpinTimer()
|
||||
|
||||
self.handle = Handle(
|
||||
local_reader_ranks=local_reader_ranks,
|
||||
buffer_handle=self.buffer.handle() if self.buffer is not None else None,
|
||||
local_subscribe_addr=local_subscribe_addr,
|
||||
local_notify_addr=local_notify_addr,
|
||||
remote_subscribe_addr=remote_subscribe_addr,
|
||||
remote_addr_ipv6=remote_addr_ipv6,
|
||||
)
|
||||
@@ -379,9 +465,9 @@ class MessageQueue:
|
||||
self.local_socket.connect(socket_addr)
|
||||
|
||||
self.remote_socket = None
|
||||
|
||||
self._read_spin_timer = (
|
||||
SpinSleepTimer() if envs.VLLM_SLEEP_WHEN_IDLE else SpinTimer()
|
||||
assert isinstance(handle.local_notify_addr, str)
|
||||
self._spin_condition = SpinCondition(
|
||||
is_reader=True, context=context, notify_address=handle.local_notify_addr
|
||||
)
|
||||
else:
|
||||
self.buffer = None # type: ignore
|
||||
@@ -399,7 +485,9 @@ class MessageQueue:
|
||||
socket_addr = handle.remote_subscribe_addr
|
||||
logger.debug("Connecting to %s", socket_addr)
|
||||
self.remote_socket.connect(socket_addr)
|
||||
self._spin_condition = None # type: ignore
|
||||
|
||||
self.shutting_down = False
|
||||
return self
|
||||
|
||||
def wait_until_ready(self):
|
||||
@@ -435,6 +523,13 @@ class MessageQueue:
|
||||
recv = self.remote_socket.recv()
|
||||
assert recv == b"READY"
|
||||
|
||||
def shutdown(self):
|
||||
"""If this is an idle reader, wakes it up so it can clean up and shut
|
||||
down"""
|
||||
self.shutting_down = True
|
||||
if self._spin_condition is not None:
|
||||
self._spin_condition.cancel()
|
||||
|
||||
@contextmanager
|
||||
def acquire_write(self, timeout: float | None = None):
|
||||
assert self._is_writer, "Only writers can acquire write"
|
||||
@@ -465,7 +560,7 @@ class MessageQueue:
|
||||
# if we wait for a long time, log a message
|
||||
if elapsed > VLLM_RINGBUFFER_WARNING_INTERVAL * n_warning:
|
||||
logger.info(
|
||||
long_wait_time_msg(VLLM_RINGBUFFER_WARNING_INTERVAL)
|
||||
LONG_WAIT_TIME_LOG_MSG, VLLM_RINGBUFFER_WARNING_INTERVAL
|
||||
)
|
||||
n_warning += 1
|
||||
|
||||
@@ -503,16 +598,60 @@ class MessageQueue:
|
||||
self.current_idx = (self.current_idx + 1) % self.buffer.max_chunks
|
||||
break
|
||||
|
||||
class ReadTimeoutWithWarnings:
|
||||
def __init__(self, timeout: float | None, should_warn: bool) -> None:
|
||||
self.started = time.monotonic()
|
||||
self.deadline = sys.maxsize if timeout is None else self.started + timeout
|
||||
|
||||
# if should_warn, we need to wake up periodically to log
|
||||
self.warning_wait_time_ms: int | None = (
|
||||
VLLM_RINGBUFFER_WARNING_INTERVAL * 1000 if should_warn else None
|
||||
)
|
||||
|
||||
self._should_warn = should_warn
|
||||
self.n_warning = 1
|
||||
self.timeout = timeout
|
||||
|
||||
def timeout_ms(self) -> int | None:
|
||||
"""Returns a timeout that is:
|
||||
- min(time to deadline, time to next warning) if we're logging warnings
|
||||
- time to deadline, if we're not logging warnings
|
||||
- None if the timeout is None and we're not logging warnings
|
||||
- raise TimeoutError if we are past the deadline
|
||||
"""
|
||||
warning_wait_time = self.warning_wait_time_ms
|
||||
if self.timeout is None:
|
||||
return warning_wait_time
|
||||
|
||||
time_left_ms = int((self.deadline - time.monotonic()) * 1000)
|
||||
if time_left_ms <= 0:
|
||||
raise TimeoutError
|
||||
|
||||
if warning_wait_time and warning_wait_time < time_left_ms:
|
||||
return warning_wait_time
|
||||
|
||||
return time_left_ms
|
||||
|
||||
def should_warn(self) -> bool:
|
||||
"""Returns true if it's time to log a warning for a timeout that is not
|
||||
indefinite"""
|
||||
if self._should_warn:
|
||||
elapsed = time.monotonic() - self.started
|
||||
if elapsed >= VLLM_RINGBUFFER_WARNING_INTERVAL * self.n_warning:
|
||||
self.n_warning += 1
|
||||
return True
|
||||
return False
|
||||
|
||||
@contextmanager
|
||||
def acquire_read(
|
||||
self,
|
||||
timeout: float | None = None,
|
||||
cancel: Event | None = None,
|
||||
indefinite: bool = False,
|
||||
):
|
||||
assert self._is_local_reader, "Only readers can acquire read"
|
||||
start_time = time.monotonic()
|
||||
n_warning = 1
|
||||
read_timeout = self.ReadTimeoutWithWarnings(
|
||||
timeout=timeout, should_warn=not indefinite
|
||||
)
|
||||
with self.buffer.get_metadata(self.current_idx) as metadata_buffer:
|
||||
while True:
|
||||
# Memory fence ensures we see the latest writes from the writer.
|
||||
@@ -529,26 +668,16 @@ class MessageQueue:
|
||||
# for readers, `self.current_idx` is the next block to read
|
||||
# if this block is not ready,
|
||||
# we need to wait until it is written
|
||||
self._spin_condition.wait(timeout_ms=read_timeout.timeout_ms())
|
||||
|
||||
# Release the processor to other threads
|
||||
self._read_spin_timer.spin()
|
||||
|
||||
if cancel is not None and cancel.is_set():
|
||||
if self.shutting_down:
|
||||
raise RuntimeError("cancelled")
|
||||
|
||||
# if we time out, raise an exception
|
||||
elapsed = time.monotonic() - start_time
|
||||
if timeout is not None and elapsed > timeout:
|
||||
raise TimeoutError
|
||||
|
||||
# if we wait for a long time, log a message
|
||||
if not indefinite and (
|
||||
elapsed > VLLM_RINGBUFFER_WARNING_INTERVAL * n_warning
|
||||
):
|
||||
if read_timeout.should_warn():
|
||||
logger.info(
|
||||
long_wait_time_msg(VLLM_RINGBUFFER_WARNING_INTERVAL)
|
||||
LONG_WAIT_TIME_LOG_MSG, VLLM_RINGBUFFER_WARNING_INTERVAL
|
||||
)
|
||||
n_warning += 1
|
||||
|
||||
continue
|
||||
# found a block that is not read by this reader
|
||||
@@ -565,7 +694,7 @@ class MessageQueue:
|
||||
memory_fence()
|
||||
self.current_idx = (self.current_idx + 1) % self.buffer.max_chunks
|
||||
|
||||
self._read_spin_timer.record_activity()
|
||||
self._spin_condition.record_read()
|
||||
break
|
||||
|
||||
def enqueue(self, obj, timeout: float | None = None):
|
||||
@@ -608,18 +737,19 @@ class MessageQueue:
|
||||
buf[offset:buf_offset] = to_bytes_big(buf_len, 4)
|
||||
buf[buf_offset : (offset := buf_offset + buf_len)] = buffer
|
||||
|
||||
self._spin_condition.notify()
|
||||
|
||||
if self.n_remote_reader > 0:
|
||||
self.remote_socket.send_multipart(all_buffers, copy=False)
|
||||
|
||||
def dequeue(
|
||||
self,
|
||||
timeout: float | None = None,
|
||||
cancel: Event | None = None,
|
||||
indefinite: bool = False,
|
||||
):
|
||||
"""Read from message queue with optional timeout (in seconds)"""
|
||||
if self._is_local_reader:
|
||||
with self.acquire_read(timeout, cancel, indefinite) as buf:
|
||||
with self.acquire_read(timeout, indefinite) as buf:
|
||||
overflow = buf[0] == 1
|
||||
if not overflow:
|
||||
offset = 3
|
||||
|
||||
Reference in New Issue
Block a user