Merge origin/main into dev/vllm-align
Resolved conflicts: - vllm_omni/inputs/preprocess.py: took origin/main's elif mm_processor_kwargs branch, fixed undefined mm_uuids reference - vllm_omni/model_executor/models/qwen3_tts/qwen3_tts_code_predictor_vllm.py: took origin/main's NPU support Made-with: Cursor Signed-off-by: tzhouam <tzhouam@connect.ust.hk>
This commit is contained in:
commit
26744bcb35
240 files changed
+25869
-3091
No files matched your search
@@ -3,7 +3,7 @@
|
||||
|
||||
import fcntl
|
||||
import os
|
||||
import time
|
||||
from multiprocessing import shared_memory as shm_pkg
|
||||
from typing import Any
|
||||
|
||||
from vllm_omni.entrypoints.stage_utils import shm_read_bytes, shm_write_bytes
|
||||
@@ -52,7 +52,7 @@ class SharedMemoryConnector(OmniConnectorBase):
|
||||
if True:
|
||||
# Use Shared Memory
|
||||
lock_file = f"/dev/shm/shm_{put_key}_lockfile.lock"
|
||||
with open(lock_file, "w") as lockf:
|
||||
with open(lock_file, "wb+") as lockf:
|
||||
fcntl.flock(lockf, fcntl.LOCK_EX)
|
||||
meta = shm_write_bytes(payload, name=put_key)
|
||||
fcntl.flock(lockf, fcntl.LOCK_UN)
|
||||
@@ -76,6 +76,23 @@ class SharedMemoryConnector(OmniConnectorBase):
|
||||
logger.error(f"SharedMemoryConnector put failed for req {put_key}: {e}")
|
||||
return False, 0, None
|
||||
|
||||
def _get_data_with_lock(self, lock_file: str, shm_handle: dict):
|
||||
obj = None
|
||||
try:
|
||||
with open(lock_file, "rb+") as lockf:
|
||||
fcntl.flock(lockf, fcntl.LOCK_EX)
|
||||
data_bytes = shm_read_bytes(shm_handle)
|
||||
fcntl.flock(lockf, fcntl.LOCK_UN)
|
||||
obj = self.deserialize_obj(data_bytes)
|
||||
return obj, int(shm_handle.get("size", 0))
|
||||
except Exception as e:
|
||||
logger.error(f"SharedMemoryConnector shm get failed for req : {e}")
|
||||
return None
|
||||
finally:
|
||||
# If data has been received, delete lock_file.
|
||||
if obj and os.path.exists(lock_file):
|
||||
os.remove(lock_file)
|
||||
|
||||
def get(
|
||||
self,
|
||||
from_stage: str,
|
||||
@@ -100,60 +117,24 @@ class SharedMemoryConnector(OmniConnectorBase):
|
||||
return None
|
||||
|
||||
if "shm" in metadata:
|
||||
try:
|
||||
shm_handle = metadata["shm"]
|
||||
lock_file = f"/dev/shm/shm_{shm_handle['name']}_lockfile.lock"
|
||||
with open(lock_file, "w") as lockf:
|
||||
fcntl.flock(lockf, fcntl.LOCK_SH)
|
||||
data_bytes = shm_read_bytes(shm_handle)
|
||||
fcntl.flock(lockf, fcntl.LOCK_UN)
|
||||
if os.path.exists(lock_file):
|
||||
os.remove(lock_file)
|
||||
obj = self.deserialize_obj(data_bytes)
|
||||
return obj, int(metadata.get("size", 0))
|
||||
except Exception as e:
|
||||
logger.error(f"SharedMemoryConnector shm get failed for req {get_key}: {e}")
|
||||
return None
|
||||
shm_handle = metadata["shm"]
|
||||
lock_file = f"/dev/shm/shm_{shm_handle['name']}_lockfile.lock"
|
||||
return self._get_data_with_lock(lock_file, shm_handle)
|
||||
|
||||
return None
|
||||
|
||||
from multiprocessing import shared_memory as shm_pkg
|
||||
|
||||
# Wait for shared memory to be available (with retry logic)
|
||||
max_retries = 30
|
||||
retry_delay = 0.1 # 100ms between retries
|
||||
shm = None
|
||||
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
shm = shm_pkg.SharedMemory(name=get_key)
|
||||
break # Successfully opened, exit retry loop
|
||||
except FileNotFoundError:
|
||||
if attempt < max_retries - 1:
|
||||
time.sleep(retry_delay)
|
||||
else:
|
||||
# Max retries reached, return None
|
||||
logger.warning(f"Shared memory '{get_key}' not found after {max_retries} retries")
|
||||
return None
|
||||
|
||||
if shm is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
shm = shm_pkg.SharedMemory(name=get_key)
|
||||
if shm is None or shm.size == 0:
|
||||
return None
|
||||
lock_file = f"/dev/shm/shm_{get_key}_lockfile.lock"
|
||||
with open(lock_file) as lockf:
|
||||
fcntl.flock(lockf, fcntl.LOCK_SH)
|
||||
data_bytes = shm_read_bytes({"name": get_key, "size": shm.size})
|
||||
fcntl.flock(lockf, fcntl.LOCK_UN)
|
||||
# Clean up the temporary file if it still exists.
|
||||
if os.path.exists(lock_file):
|
||||
os.remove(lock_file)
|
||||
obj = self.deserialize_obj(data_bytes)
|
||||
return obj, shm.size
|
||||
shm_handle = {"name": get_key, "size": shm.size}
|
||||
return self._get_data_with_lock(lock_file, shm_handle)
|
||||
except Exception:
|
||||
return None
|
||||
finally:
|
||||
shm.close()
|
||||
|
||||
# TODO: update another read method
|
||||
if shm:
|
||||
shm.close()
|
||||
|
||||
def cleanup(self, request_id: str) -> None:
|
||||
# SHM segments are automatically unlinked during 'get' (shm_read_bytes).
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import threading
|
||||
import time
|
||||
from collections import deque
|
||||
from typing import Any
|
||||
|
||||
from ..utils.logging import get_connector_logger
|
||||
@@ -22,17 +23,16 @@ class OmniTransferAdapterBase:
|
||||
if not hasattr(self, "connector"):
|
||||
self.connector = None
|
||||
# Requests that are waiting to be polled
|
||||
self._pending_load_reqs = {}
|
||||
self._pending_load_reqs = deque()
|
||||
# Requests that have successfully retrieved data
|
||||
self._finished_load_reqs = set()
|
||||
|
||||
# Requests that are waiting to be saved
|
||||
self._pending_save_reqs = {}
|
||||
self._pending_save_reqs = deque()
|
||||
# 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()
|
||||
@@ -48,37 +48,30 @@ class OmniTransferAdapterBase:
|
||||
"""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:
|
||||
while self._pending_load_reqs:
|
||||
request = self._pending_load_reqs.popleft()
|
||||
request_id = request.request_id
|
||||
self.request_ids_mapping[request_id] = request.external_req_id
|
||||
try:
|
||||
self._poll_single_request(req_id)
|
||||
is_success = self._poll_single_request(request)
|
||||
if not is_success:
|
||||
self._pending_load_reqs.append(request)
|
||||
except Exception as e:
|
||||
logger.warning(f"Error receiving data for {req_id}: {e}")
|
||||
self._pending_load_reqs.append(request)
|
||||
logger.warning(f"Error receiving data for {request_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:
|
||||
while self._pending_save_reqs:
|
||||
task = self._pending_save_reqs.popleft()
|
||||
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)
|
||||
logger.warning(f"Error saving data for {task.get('request_id')}: {e}")
|
||||
time.sleep(0.001)
|
||||
|
||||
def _poll_single_request(self, *args, **kwargs):
|
||||
"""Poll connector for a single request task.
|
||||
|
||||
@@ -89,113 +89,84 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
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
|
||||
self._pending_load_reqs.append(request)
|
||||
|
||||
def save_async(self, pooling_output: torch.Tensor | None = None, request: Request | None = None):
|
||||
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``
|
||||
Payload extraction happens in ``_send_single_request`` on the
|
||||
background save_loop thread.
|
||||
|
||||
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,
|
||||
"pooling_output": pooling_output,
|
||||
"request": request,
|
||||
"is_finished": request.is_finished(),
|
||||
}
|
||||
self._pending_save_reqs.append(task)
|
||||
|
||||
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):
|
||||
def _poll_single_request(self, request: Request):
|
||||
stage_id = self.connector.stage_id
|
||||
target_stage_id = stage_id - 1
|
||||
req_id = request.request_id
|
||||
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,
|
||||
)
|
||||
try:
|
||||
result = self.connector.get(
|
||||
str(target_stage_id),
|
||||
str(stage_id),
|
||||
connector_get_key,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"SharedMemoryConnector get failed for req {connector_get_key}: {e}")
|
||||
return False
|
||||
|
||||
if result is None:
|
||||
return
|
||||
|
||||
return False
|
||||
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
|
||||
request.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.prompt_token_ids = payload_data.get("code_predictor_codes", [])
|
||||
# req.num_computed_tokens = 0
|
||||
new_ids = payload_data.get("code_predictor_codes", [])
|
||||
req.prompt_token_ids = new_ids
|
||||
req.num_computed_tokens = 0
|
||||
request.prompt_token_ids = new_ids
|
||||
request.num_computed_tokens = 0
|
||||
|
||||
# Empty chunk with more data expected: keep polling.
|
||||
if not new_ids and not payload_data.get("finished"):
|
||||
return
|
||||
return True
|
||||
|
||||
# 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]
|
||||
self._finished_load_reqs.add(req_id)
|
||||
logger.debug(f"[Stage-{stage_id}] Received one chunk for key {connector_get_key}")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
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.
|
||||
@@ -221,10 +192,30 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
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"]
|
||||
pooling_output = task["pooling_output"]
|
||||
request = task["request"]
|
||||
is_finished = task["is_finished"]
|
||||
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]
|
||||
connector_put_key = f"{request_id}_{stage_id}_{chunk_id}"
|
||||
# Process payload in save_loop thread
|
||||
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,
|
||||
is_finished=is_finished,
|
||||
)
|
||||
|
||||
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 = self.connector.put(
|
||||
from_stage=str(stage_id),
|
||||
@@ -234,8 +225,43 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
)
|
||||
|
||||
if success:
|
||||
self.put_req_chunk[request_id] += 1
|
||||
logger.debug(f"[Stage-{stage_id}] Sent {connector_put_key}")
|
||||
|
||||
########################################################################
|
||||
# Cleanup
|
||||
########################################################################
|
||||
|
||||
def cleanup(
|
||||
self,
|
||||
request_id: str,
|
||||
external_req_id: str | None = None,
|
||||
) -> None:
|
||||
"""Reclaim all per-request state after a request finishes.
|
||||
|
||||
Idempotent: calling with an already-cleaned or unknown id is safe.
|
||||
|
||||
Args:
|
||||
request_id: Internal request id (receive / scheduler side key).
|
||||
external_req_id: External request id (send / payload side key).
|
||||
When *None*, looked up from ``request_ids_mapping``.
|
||||
"""
|
||||
if external_req_id is None:
|
||||
external_req_id = self.request_ids_mapping.get(request_id, request_id)
|
||||
|
||||
self.finished_requests.discard(request_id)
|
||||
self.get_req_chunk.pop(request_id, None)
|
||||
self.requests_with_ready_chunks.discard(request_id)
|
||||
self.request_ids_mapping.pop(request_id, None)
|
||||
|
||||
remaining = deque(r for r in self._pending_load_reqs if getattr(r, "request_id", None) != request_id)
|
||||
self._pending_load_reqs = remaining
|
||||
self._finished_load_reqs.discard(request_id)
|
||||
|
||||
self.put_req_chunk.pop(external_req_id, None)
|
||||
self.request_payload.pop(external_req_id, None)
|
||||
self.code_prompt_token_ids.pop(external_req_id, None)
|
||||
|
||||
########################################################################
|
||||
# Schedule Helper
|
||||
########################################################################
|
||||
@@ -250,12 +276,11 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
"""
|
||||
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
|
||||
waiting_queue, self.waiting_for_chunk_waiting_requests, RequestStatus.WAITING, self._finished_load_reqs
|
||||
)
|
||||
self._process_chunk_queue(
|
||||
running_queue, self.waiting_for_chunk_running_requests, RequestStatus.RUNNING, finished_load_reqs
|
||||
running_queue, self.waiting_for_chunk_running_requests, RequestStatus.RUNNING, self._finished_load_reqs
|
||||
)
|
||||
while len(running_queue) > self.scheduler_max_num_seqs:
|
||||
request = running_queue.pop()
|
||||
@@ -321,6 +346,7 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
else:
|
||||
if request.request_id in finished_load_reqs:
|
||||
request.status = target_status
|
||||
finished_load_reqs.remove(request.request_id)
|
||||
self.requests_with_ready_chunks.add(request.request_id)
|
||||
continue
|
||||
queue.remove(request)
|
||||
@@ -336,9 +362,3 @@ class OmniChunkTransferAdapter(OmniTransferAdapterBase):
|
||||
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