Merge origin/main into dev/rebase-v0.16.0.
Resolve arg_utils merge conflict and align CI base image tag to v0.16.0 for the vLLM rebase track. Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Taichang Zhou <tzhouam@connect.ust.hk>
This commit is contained in:
commit
e2ced80d59
64 files changed
+5186
-1228
No files matched your search
@@ -5,14 +5,7 @@
|
||||
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
import torch
|
||||
from vllm.v1.core.sched.output import SchedulerOutput
|
||||
from vllm.v1.request import Request, RequestStatus
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .connectors.base import OmniConnectorBase
|
||||
from typing import Any
|
||||
|
||||
from vllm_omni.entrypoints.stage_utils import OmniStageTaskType
|
||||
from vllm_omni.metrics import OrchestratorAggregator
|
||||
@@ -180,178 +173,6 @@ def try_recv_via_connector(
|
||||
return None, None
|
||||
|
||||
|
||||
def update_request_payload(connector: "OmniConnectorBase", req_id: str, payload_data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Update the payload data for a request in the connector.
|
||||
|
||||
Args:
|
||||
connector: OmniConnectorBase instance
|
||||
req_id: Request ID to update
|
||||
payload_data: New payload data to store
|
||||
"""
|
||||
origin_payload = connector.request_payload[req_id]
|
||||
for key, value in payload_data.items():
|
||||
if key == "finished":
|
||||
continue
|
||||
elif isinstance(value, torch.Tensor) and key in origin_payload:
|
||||
payload_data[key] = torch.cat([origin_payload[key], value], dim=0)
|
||||
elif isinstance(value, list) and key in origin_payload:
|
||||
payload_data[key] = origin_payload[key] + value
|
||||
|
||||
connector.request_payload[req_id] = payload_data
|
||||
return payload_data
|
||||
|
||||
|
||||
def get_chunk(
|
||||
connector: "OmniConnectorBase",
|
||||
scheduler_output: SchedulerOutput,
|
||||
) -> None:
|
||||
"""Retrieve a chunk of pooling output.
|
||||
|
||||
Args:
|
||||
connector: OmniConnectorBase instance
|
||||
scheduler_output: Partial scheduler output dictionary
|
||||
|
||||
Returns:
|
||||
None: This function modifies scheduler_output in place
|
||||
"""
|
||||
stage_id = connector.stage_id
|
||||
if stage_id == 0:
|
||||
return
|
||||
|
||||
target_stage_id = stage_id - 1
|
||||
# Handle new requests
|
||||
for new_req_data in scheduler_output.scheduled_new_reqs:
|
||||
connector.request_ids_mapping[new_req_data.req_id] = new_req_data.external_req_id
|
||||
req_id = new_req_data.external_req_id
|
||||
chunk_id = connector.get_requests[req_id]
|
||||
connector_get_key = f"{req_id}_{target_stage_id}_{chunk_id}"
|
||||
payload_data = get_through_connector(connector, target_stage_id, stage_id, req_id, connector_get_key)
|
||||
if payload_data:
|
||||
new_req_data.additional_information = payload_data
|
||||
connector.request_payload[req_id] = payload_data
|
||||
if payload_data.get("finished"):
|
||||
connector.finished_requests.add(req_id)
|
||||
|
||||
# Handle cached/running requests
|
||||
cached_reqs = scheduler_output.scheduled_cached_reqs
|
||||
if not hasattr(cached_reqs, "additional_information"):
|
||||
cached_reqs.additional_information = {}
|
||||
|
||||
for i, cached_req_id in enumerate(cached_reqs.req_ids):
|
||||
req_id = connector.request_ids_mapping.get(cached_req_id, cached_req_id)
|
||||
if req_id in connector.finished_requests:
|
||||
continue
|
||||
chunk_id = connector.get_requests[req_id]
|
||||
connector_get_key = f"{req_id}_{target_stage_id}_{chunk_id}"
|
||||
payload_data = get_through_connector(connector, target_stage_id, stage_id, req_id, connector_get_key)
|
||||
if payload_data:
|
||||
payload_data = update_request_payload(connector, req_id, payload_data)
|
||||
cached_reqs.additional_information[cached_req_id] = payload_data
|
||||
if payload_data.get("finished"):
|
||||
connector.finished_requests.add(req_id)
|
||||
|
||||
|
||||
def get_through_connector(connector, target_stage_id, stage_id, req_id, connector_get_key):
|
||||
# TODO: add correct check mechanism for the payload_data
|
||||
max_wait = 300
|
||||
for _ in range(max_wait):
|
||||
result = connector.get(
|
||||
from_stage=str(target_stage_id),
|
||||
to_stage=str(stage_id),
|
||||
get_key=connector_get_key,
|
||||
)
|
||||
payload_data = None
|
||||
if result:
|
||||
payload_data, size = result
|
||||
if payload_data:
|
||||
connector.request_prompt_token_ids[req_id] = payload_data.get("thinker_input_ids", [])
|
||||
connector.get_requests[req_id] += 1
|
||||
logger.debug("[Stage-%d] Received one chunk for request %s", stage_id, connector_get_key)
|
||||
break
|
||||
time.sleep(0.01)
|
||||
return payload_data
|
||||
|
||||
|
||||
def get_chunk_for_generation(
|
||||
connector: "OmniConnectorBase",
|
||||
request: Request,
|
||||
) -> None:
|
||||
"""Retrieve a chunk of pooling output.
|
||||
|
||||
Args:
|
||||
connector: OmniConnectorBase instance
|
||||
request: Request object
|
||||
|
||||
Returns:
|
||||
None: This function modifies request in place
|
||||
"""
|
||||
stage_id = connector.stage_id
|
||||
target_stage_id = stage_id - 1
|
||||
request_id = request.external_req_id
|
||||
|
||||
if request_id in connector.finished_requests:
|
||||
return
|
||||
|
||||
chunk_id = connector.get_requests[request_id]
|
||||
connector_get_key = f"{request_id}_{target_stage_id}_{chunk_id}"
|
||||
payload_data = get_through_connector(connector, target_stage_id, stage_id, request_id, connector_get_key)
|
||||
if not payload_data:
|
||||
return
|
||||
|
||||
if payload_data.get("finished"):
|
||||
connector.finished_requests.add(request_id)
|
||||
request.status = RequestStatus.FINISHED_STOPPED
|
||||
request.prompt_token_ids = payload_data.get("code_predictor_codes", [])
|
||||
request.num_computed_tokens = 0
|
||||
|
||||
|
||||
def put_chunk(
|
||||
connector: "OmniConnectorBase",
|
||||
pooling_output: dict[str, Any],
|
||||
request: Request,
|
||||
custom_process_input_func: Callable[[dict[str, Any], Request], dict[str, Any] | None] | None = None,
|
||||
) -> None:
|
||||
"""Store a chunk of pooling output.
|
||||
|
||||
Args:
|
||||
connector: OmniConnectorBase instance
|
||||
pooling_output: Partial pooling output dictionary
|
||||
request: Request object
|
||||
custom_process_input_func: Optional custom function to process input
|
||||
|
||||
Returns:
|
||||
None: This function sends data via connector
|
||||
"""
|
||||
stage_id = connector.stage_id
|
||||
next_stage_id = stage_id + 1
|
||||
request_id = request.external_req_id
|
||||
chunk_id = connector.put_requests[request_id]
|
||||
connector_put_key = f"{request_id}_{stage_id}_{chunk_id}"
|
||||
payload_data = None
|
||||
|
||||
# TODO: add default process_input_func to handle the payload_data ?
|
||||
if custom_process_input_func:
|
||||
try:
|
||||
payload_data = custom_process_input_func(
|
||||
connector=connector,
|
||||
pooling_output=pooling_output,
|
||||
request=request,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to use custom_process_input_func for payload extraction: {e}")
|
||||
|
||||
if not payload_data:
|
||||
return
|
||||
|
||||
success, size, metadata = connector.put(
|
||||
from_stage=str(stage_id), to_stage=str(next_stage_id), put_key=connector_put_key, data=payload_data
|
||||
)
|
||||
|
||||
if success:
|
||||
connector.put_requests[request_id] += 1
|
||||
logger.debug("[Stage-%d] Sent %s", stage_id, connector_put_key)
|
||||
|
||||
|
||||
def compute_talker_prompt_ids_length(prompt_ids: list[int]) -> int:
|
||||
"""Compute the length of the talker prompt ids.
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
import fcntl
|
||||
import os
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from typing import Any
|
||||
|
||||
from vllm_omni.entrypoints.stage_utils import shm_read_bytes, shm_write_bytes
|
||||
@@ -25,14 +24,6 @@ class SharedMemoryConnector(OmniConnectorBase):
|
||||
self.config = config
|
||||
self.stage_id = config.get("stage_id", -1)
|
||||
self.device = config.get("device", "cuda:0")
|
||||
self.put_requests: dict[str, int] = defaultdict(int)
|
||||
self.get_requests: dict[str, int] = defaultdict(int)
|
||||
self.finished_requests: set[str] = set()
|
||||
self.request_payload = {}
|
||||
self.request_prompt_token_ids: dict[str, list[int]] = defaultdict(list)
|
||||
self.code_prompt_token_ids: dict[str, list[list[int]]] = defaultdict(list)
|
||||
self.request_ids_mapping: dict[str, str] = {}
|
||||
# Default threshold matches legacy behavior (64KB)
|
||||
self.threshold = int(config.get("shm_threshold_bytes", 65536))
|
||||
self._metrics = {
|
||||
"puts": 0,
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
@@ -0,0 +1,115 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import threading
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from ..utils.logging import get_connector_logger
|
||||
|
||||
logger = get_connector_logger(__name__)
|
||||
|
||||
|
||||
class OmniTransferAdapterBase:
|
||||
"""Base class for managing data transfer via OmniConnector.
|
||||
|
||||
This class handles the core loop logic and connector interactions, but
|
||||
leaves the specific data processing (chunks, KV cache, etc.) to subclasses.
|
||||
"""
|
||||
|
||||
def __init__(self, config: Any):
|
||||
self.config = config
|
||||
if not hasattr(self, "connector"):
|
||||
self.connector = None
|
||||
# Requests that are waiting to be polled
|
||||
self._pending_load_reqs = {}
|
||||
# Requests that have successfully retrieved data
|
||||
self._finished_load_reqs = set()
|
||||
|
||||
# Requests that are waiting to be saved
|
||||
self._pending_save_reqs = {}
|
||||
# Requests that have successfully saved data
|
||||
self._finished_save_reqs = set()
|
||||
|
||||
self.stop_event = threading.Event()
|
||||
self.lock = threading.Lock()
|
||||
|
||||
self.recv_thread = threading.Thread(target=self.recv_loop, daemon=True)
|
||||
self.recv_thread.start()
|
||||
|
||||
self.save_thread = threading.Thread(target=self.save_loop, daemon=True)
|
||||
self.save_thread.start()
|
||||
|
||||
@classmethod
|
||||
def create_connector(cls, model_config: Any):
|
||||
raise NotImplementedError
|
||||
|
||||
def recv_loop(self):
|
||||
"""Loop to poll for incoming data."""
|
||||
while not self.stop_event.is_set():
|
||||
# Iterate over a snapshot of pending requests
|
||||
with self.lock:
|
||||
pending_reqs_ids = list(self._pending_load_reqs.keys())
|
||||
|
||||
for req_id in pending_reqs_ids:
|
||||
try:
|
||||
self._poll_single_request(req_id)
|
||||
except Exception as e:
|
||||
logger.warning(f"Error receiving data for {req_id}: {e}")
|
||||
|
||||
time.sleep(0.001)
|
||||
|
||||
def save_loop(self):
|
||||
"""Loop to send outgoing data."""
|
||||
while not self.stop_event.is_set():
|
||||
task = None
|
||||
with self.lock:
|
||||
pending_save_reqs_ids = list(self._pending_save_reqs.keys())
|
||||
for req_id in pending_save_reqs_ids:
|
||||
if self._pending_save_reqs[req_id]:
|
||||
task = self._pending_save_reqs[req_id].popleft()
|
||||
if not self._pending_save_reqs[req_id]:
|
||||
del self._pending_save_reqs[req_id]
|
||||
break
|
||||
|
||||
if task:
|
||||
try:
|
||||
self._send_single_request(task)
|
||||
except Exception as e:
|
||||
logger.error(f"Error saving data for {task.get('request_id')}: {e}")
|
||||
else:
|
||||
time.sleep(0.001)
|
||||
|
||||
def _poll_single_request(self, *args, **kwargs):
|
||||
"""Poll connector for a single request task.
|
||||
Subclasses should implement request-specific receive behavior."""
|
||||
raise NotImplementedError
|
||||
|
||||
def _send_single_request(self, *args, **kwargs):
|
||||
"""Send one pending save request task to the connector.
|
||||
Subclasses should implement task-specific handling logic."""
|
||||
raise NotImplementedError
|
||||
|
||||
def load_async(self, *args, **kwargs):
|
||||
"""Register a request to load data. To be implemented by subclasses."""
|
||||
raise NotImplementedError
|
||||
|
||||
def save_async(self, *args, **kwargs):
|
||||
"""Submit data to be saved. To be implemented by subclasses."""
|
||||
raise NotImplementedError
|
||||
|
||||
def load(self, *args, **kwargs):
|
||||
"""Load request data from connector synchronously. To be implemented by subclasses."""
|
||||
raise NotImplementedError
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""Save data to connector synchronously. To be implemented by subclasses."""
|
||||
raise NotImplementedError
|
||||
|
||||
def get_finished_requests(self):
|
||||
"""Get finished loaded or saved requests"""
|
||||
raise NotImplementedError
|
||||
|
||||
def shutdown(self):
|
||||
"""Stop background loops and close the connector."""
|
||||
raise NotImplementedError
|
||||
@@ -0,0 +1,339 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
|
||||
import importlib
|
||||
from collections import defaultdict, deque
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
from vllm.v1.request import Request, RequestStatus
|
||||
|
||||
from ..factory import OmniConnectorFactory
|
||||
from ..utils.config import ConnectorSpec
|
||||
from ..utils.logging import get_connector_logger
|
||||
from .base import OmniTransferAdapterBase
|
||||
|
||||
logger = get_connector_logger(__name__)
|
||||
|
||||
|
||||
class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
"""Chunk-level transfer adapter for Omni connector pipelines.
|
||||
|
||||
This class coordinates per-request chunk exchange between adjacent stages,
|
||||
and implements asynchronous get/put of chunks via background threads.
|
||||
It tracks per-request chunk indices for put/get, and accumulates
|
||||
payloads across chunks (concatenating tensors/lists in AR mode). It also
|
||||
caches prompt token ids and additional information for scheduler use.
|
||||
|
||||
Scheduler integration is handled via WAITING_FOR_CHUNK transitions:
|
||||
requests are moved to waiting for chunk deque while polling, then restored
|
||||
to waiting/running queues once a chunk arrives. The requests will finish
|
||||
loading chunk util detecting the payload "finished" flag.
|
||||
|
||||
The base class owns background recv/save loops; load/save only enqueue
|
||||
work and return immediately.
|
||||
"""
|
||||
|
||||
def __init__(self, vllm_config: Any):
|
||||
model_config = vllm_config.model_config
|
||||
self.scheduler_max_num_seqs = vllm_config.scheduler_config.max_num_seqs
|
||||
self.connector = self.create_connector(model_config)
|
||||
super().__init__(model_config)
|
||||
self.model_mode = getattr(model_config, "worker_type", "ar")
|
||||
# State specific to Chunk management
|
||||
self.custom_process_next_stage_input_func = None
|
||||
custom_process_next_stage_input_func = getattr(model_config, "custom_process_next_stage_input_func", None)
|
||||
if custom_process_next_stage_input_func:
|
||||
module_path, func_name = custom_process_next_stage_input_func.rsplit(".", 1)
|
||||
module = importlib.import_module(module_path)
|
||||
self.custom_process_next_stage_input_func = getattr(module, func_name)
|
||||
# mapping for request id and chunk id
|
||||
self.put_req_chunk: dict[str, int] = defaultdict(int)
|
||||
self.get_req_chunk: dict[str, int] = defaultdict(int)
|
||||
self.finished_requests: set[str] = set()
|
||||
self.request_payload = {}
|
||||
self.code_prompt_token_ids: dict[str, list[list[int]]] = defaultdict(list)
|
||||
self.request_ids_mapping: dict[str, str] = {}
|
||||
|
||||
self.waiting_for_chunk_waiting_requests: deque[Any] = deque()
|
||||
self.waiting_for_chunk_running_requests: deque[Any] = deque()
|
||||
self.requests_with_ready_chunks = set()
|
||||
|
||||
@classmethod
|
||||
def create_connector(cls, model_config: Any):
|
||||
connector_config = getattr(model_config, "stage_connector_config", None)
|
||||
if connector_config is None:
|
||||
connector_config = {}
|
||||
elif not isinstance(connector_config, dict):
|
||||
connector_config = {
|
||||
"name": getattr(connector_config, "name", None),
|
||||
"extra": getattr(connector_config, "extra", {}),
|
||||
}
|
||||
|
||||
connector_specs = ConnectorSpec(
|
||||
name=connector_config.get("name", "SharedMemoryConnector"),
|
||||
extra=connector_config.get("extra", {}),
|
||||
)
|
||||
return OmniConnectorFactory.create_connector(connector_specs)
|
||||
|
||||
def load_async(self, request: Request):
|
||||
"""Register a request for asynchronous chunk retrieval.
|
||||
|
||||
This method does not read from the connector directly. It records
|
||||
request metadata and enqueues the request id for the background
|
||||
receive loop to poll.
|
||||
|
||||
Stage-0 has no upstream producer, so this call is a no-op there.
|
||||
|
||||
Args:
|
||||
request: The request object needing data.
|
||||
"""
|
||||
stage_id = self.connector.stage_id
|
||||
request_id = request.request_id
|
||||
self.request_ids_mapping[request_id] = request.external_req_id
|
||||
|
||||
if stage_id == 0:
|
||||
return
|
||||
if not hasattr(request, "additional_information"):
|
||||
request.additional_information = None
|
||||
with self.lock:
|
||||
self._pending_load_reqs[request_id] = request
|
||||
|
||||
def save_async(self, pooling_output: torch.Tensor | None = None, request: Request | None = None):
|
||||
"""Build and enqueue one chunk for asynchronous sending.
|
||||
|
||||
Payload extraction is executed in the caller thread via
|
||||
``custom_process_next_stage_input_func``
|
||||
|
||||
Args:
|
||||
pooling_output: Partial pooling output dictionary
|
||||
request: Request object
|
||||
"""
|
||||
stage_id = self.connector.stage_id
|
||||
next_stage_id = stage_id + 1
|
||||
request_id = request.external_req_id
|
||||
chunk_id = self.put_req_chunk[request_id]
|
||||
|
||||
# Process payload in main thread to avoid race conditions on request state
|
||||
payload_data = None
|
||||
if self.custom_process_next_stage_input_func:
|
||||
try:
|
||||
payload_data = self.custom_process_next_stage_input_func(
|
||||
transfer_manager=self,
|
||||
pooling_output=pooling_output,
|
||||
request=request,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to use custom_process_input_func for payload extraction: {e}")
|
||||
|
||||
if not payload_data:
|
||||
return
|
||||
|
||||
# Increment chunk_id
|
||||
self.put_req_chunk[request_id] += 1
|
||||
connector_put_key = f"{request_id}_{stage_id}_{chunk_id}"
|
||||
|
||||
task = {
|
||||
"stage_id": stage_id,
|
||||
"next_stage_id": next_stage_id,
|
||||
"put_key": connector_put_key,
|
||||
"data": payload_data,
|
||||
"request_id": request_id,
|
||||
}
|
||||
|
||||
with self.lock:
|
||||
if request_id not in self._pending_save_reqs:
|
||||
self._pending_save_reqs[request_id] = deque()
|
||||
self._pending_save_reqs[request_id].append(task)
|
||||
|
||||
def _poll_single_request(self, req_id: str):
|
||||
stage_id = self.connector.stage_id
|
||||
target_stage_id = stage_id - 1
|
||||
chunk_id = self.get_req_chunk[req_id]
|
||||
external_req_id = self.request_ids_mapping.get(req_id, req_id)
|
||||
connector_get_key = f"{external_req_id}_{target_stage_id}_{chunk_id}"
|
||||
|
||||
# Use timeout=0 for non-blocking poll
|
||||
result = self.connector.get(
|
||||
str(target_stage_id),
|
||||
str(stage_id),
|
||||
connector_get_key,
|
||||
)
|
||||
|
||||
if result is None:
|
||||
return
|
||||
|
||||
payload_data, size = result
|
||||
|
||||
if payload_data:
|
||||
# Update connector state
|
||||
self.get_req_chunk[req_id] += 1
|
||||
req = self._pending_load_reqs[req_id]
|
||||
|
||||
if self.model_mode == "ar":
|
||||
self._update_request_payload(external_req_id, payload_data)
|
||||
req.additional_information = payload_data
|
||||
if payload_data.get("finished"):
|
||||
self.finished_requests.add(req_id)
|
||||
else:
|
||||
if payload_data.get("finished"):
|
||||
self.finished_requests.add(req_id)
|
||||
req.status = RequestStatus.FINISHED_STOPPED
|
||||
|
||||
req.prompt_token_ids = payload_data.get("code_predictor_codes", [])
|
||||
req.num_computed_tokens = 0
|
||||
|
||||
# Mark as finished for consumption
|
||||
with self.lock:
|
||||
self._finished_load_reqs.add(req_id)
|
||||
if req_id in self._pending_load_reqs:
|
||||
del self._pending_load_reqs[req_id]
|
||||
logger.debug(f"[Stage-{stage_id}] Received one chunk for key {connector_get_key}")
|
||||
|
||||
def _update_request_payload(self, req_id: str, payload_data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Update the payload data for a request in the connector.
|
||||
|
||||
Args:
|
||||
connector: OmniConnectorBase instance
|
||||
req_id: Request ID to update
|
||||
payload_data: New payload data to store
|
||||
"""
|
||||
if req_id not in self.request_payload:
|
||||
self.request_payload[req_id] = payload_data
|
||||
return payload_data
|
||||
origin_payload = self.request_payload[req_id]
|
||||
for key, value in payload_data.items():
|
||||
if key == "finished":
|
||||
continue
|
||||
elif isinstance(value, torch.Tensor) and key in origin_payload:
|
||||
payload_data[key] = torch.cat([origin_payload[key], value], dim=0)
|
||||
elif isinstance(value, list) and key in origin_payload:
|
||||
payload_data[key] = origin_payload[key] + value
|
||||
|
||||
self.request_payload[req_id] = payload_data
|
||||
return payload_data
|
||||
|
||||
def _send_single_request(self, task: dict):
|
||||
connector_put_key = task["put_key"]
|
||||
stage_id = task["stage_id"]
|
||||
next_stage_id = task["next_stage_id"]
|
||||
payload_data = task["data"]
|
||||
|
||||
success, size, metadata = self.connector.put(
|
||||
from_stage=str(stage_id),
|
||||
to_stage=str(next_stage_id),
|
||||
put_key=connector_put_key,
|
||||
data=payload_data,
|
||||
)
|
||||
|
||||
if success:
|
||||
logger.debug(f"[Stage-{stage_id}] Sent {connector_put_key}")
|
||||
|
||||
########################################################################
|
||||
# Schedule Helper
|
||||
########################################################################
|
||||
|
||||
def process_pending_chunks(
|
||||
self,
|
||||
waiting_queue: Any,
|
||||
running_queue: list[Request],
|
||||
) -> None:
|
||||
"""
|
||||
Process pending chunks for waiting and running queues.
|
||||
"""
|
||||
if self.connector.stage_id == 0:
|
||||
return
|
||||
finished_load_reqs = self.get_finished_requests()
|
||||
self._process_chunk_queue(
|
||||
waiting_queue, self.waiting_for_chunk_waiting_requests, RequestStatus.WAITING, finished_load_reqs
|
||||
)
|
||||
self._process_chunk_queue(
|
||||
running_queue, self.waiting_for_chunk_running_requests, RequestStatus.RUNNING, finished_load_reqs
|
||||
)
|
||||
while len(running_queue) > self.scheduler_max_num_seqs:
|
||||
request = running_queue.pop()
|
||||
waiting_queue.prepend_requests([request])
|
||||
|
||||
def restore_queues(self, waiting_queue: Any, running_queue: list[Request]) -> None:
|
||||
"""
|
||||
Restore requests waiting for chunk to the waiting and running queues.
|
||||
"""
|
||||
# Add request waiting for chunk to the waiting and running queue
|
||||
for request in self.waiting_for_chunk_waiting_requests:
|
||||
waiting_queue.add_request(request)
|
||||
self.waiting_for_chunk_waiting_requests = deque()
|
||||
|
||||
if self.waiting_for_chunk_running_requests:
|
||||
running_queue.extend(self.waiting_for_chunk_running_requests)
|
||||
self.waiting_for_chunk_running_requests = deque()
|
||||
|
||||
def postprocess_scheduler_output(
|
||||
self,
|
||||
scheduler_output: Any,
|
||||
requests: dict[str, Request] | None = None,
|
||||
) -> None:
|
||||
"""
|
||||
Add additional info for cached requests and
|
||||
clean up ready chunks from scheduler output.
|
||||
"""
|
||||
if requests is not None:
|
||||
self.attach_cached_additional_information(scheduler_output, requests)
|
||||
self._clear_chunk_ready(scheduler_output)
|
||||
|
||||
@staticmethod
|
||||
def attach_cached_additional_information(scheduler_output: Any, requests: dict[str, Request]) -> None:
|
||||
cached_reqs = getattr(scheduler_output, "scheduled_cached_reqs", None)
|
||||
if not cached_reqs:
|
||||
return
|
||||
if not hasattr(cached_reqs, "additional_information"):
|
||||
cached_reqs.additional_information = {}
|
||||
for req_id in cached_reqs.req_ids:
|
||||
request = requests.get(req_id) if req_id else None
|
||||
additional_info = getattr(request, "additional_information", None) if request else None
|
||||
cached_reqs.additional_information[req_id] = additional_info
|
||||
|
||||
def _process_chunk_queue(
|
||||
self,
|
||||
queue: Any,
|
||||
waiting_for_chunk_list: deque[Any],
|
||||
target_status: RequestStatus,
|
||||
finished_load_reqs: set[str],
|
||||
) -> None:
|
||||
queue_snapshot = list(queue)
|
||||
for request in queue_snapshot:
|
||||
if request.status != RequestStatus.WAITING_FOR_CHUNK:
|
||||
if request.request_id in self.requests_with_ready_chunks:
|
||||
# Requests that have loaded chunk from last round
|
||||
# of schedule, but have not scheduled
|
||||
continue
|
||||
if request.request_id in self.finished_requests:
|
||||
request.additional_information = {}
|
||||
continue
|
||||
# Requests that waiting for chunk
|
||||
self.load_async(request)
|
||||
request.status = RequestStatus.WAITING_FOR_CHUNK
|
||||
else:
|
||||
if request.request_id in finished_load_reqs:
|
||||
request.status = target_status
|
||||
self.requests_with_ready_chunks.add(request.request_id)
|
||||
continue
|
||||
queue.remove(request)
|
||||
waiting_for_chunk_list.append(request)
|
||||
|
||||
def _clear_chunk_ready(self, scheduler_output: Any) -> None:
|
||||
if scheduler_output.scheduled_new_reqs:
|
||||
for req_data in scheduler_output.scheduled_new_reqs:
|
||||
if req_data.req_id in self.requests_with_ready_chunks:
|
||||
self.requests_with_ready_chunks.remove(req_data.req_id)
|
||||
|
||||
if scheduler_output.scheduled_cached_reqs:
|
||||
for req_id in scheduler_output.scheduled_cached_reqs.req_ids:
|
||||
if req_id in self.requests_with_ready_chunks:
|
||||
self.requests_with_ready_chunks.remove(req_id)
|
||||
|
||||
def get_finished_requests(self):
|
||||
with self.lock:
|
||||
finished_load = set(self._finished_load_reqs)
|
||||
self._finished_load_reqs = set()
|
||||
return finished_load
|
||||
Reference in new issue
Block a user