Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions python/sglang/srt/managers/tokenizer_communicator_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import time
import uuid
from collections import deque
from contextlib import nullcontext
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -522,15 +521,15 @@ async def update_weights_from_distributed(
if obj.abort_all_requests:
self.abort_request(abort_all=True)

# Immediately update the weights if the engine is in paused state
# Hold is_pause_cond while updating to prevent unpause from racing.
async with self.is_pause_cond:
is_paused = self.is_pause
if is_paused:
results = await self.update_weights_from_distributed_communicator(obj)

lock_context = (
self.model_update_lock.writer_lock if not is_paused else nullcontext()
)
async with lock_context:
results = await self.update_weights_from_distributed_communicator(obj)
if not is_paused:
async with self.model_update_lock.writer_lock:
results = await self.update_weights_from_distributed_communicator(obj)

success, message = _Communicator.merge_results(results)
if success and obj.weight_version is not None:
Expand Down Expand Up @@ -580,15 +579,14 @@ async def update_weights_from_tensor(
if obj.abort_all_requests:
self.abort_request(abort_all=True)

# Immediately update the weights if the engine is in paused state
async with self.is_pause_cond:
is_paused = self.is_pause
if is_paused:
results = await self.update_weights_from_tensor_communicator(obj)

lock_context = (
self.model_update_lock.writer_lock if not is_paused else nullcontext()
)
async with lock_context:
results = await self.update_weights_from_tensor_communicator(obj)
if not is_paused:
async with self.model_update_lock.writer_lock:
results = await self.update_weights_from_tensor_communicator(obj)

success, message = _Communicator.merge_results(results)
if success and obj.weight_version is not None:
Expand All @@ -610,10 +608,17 @@ async def update_weights_from_ipc(
self.server_args.dp_size == 1 or self.server_args.enable_dp_attention
), "dp_size must be 1 or dp attention must be enabled for update weights from IPC"
logger.info("Starting IPC weight update")
# This means that weight sync cannot run while requests are in progress.
async with self.model_update_lock.writer_lock:
result = (await self.update_weights_from_ipc_communicator(obj))[0]
success, message = result.success, result.message

async with self.is_pause_cond:
is_paused = self.is_pause
if is_paused:
result = (await self.update_weights_from_ipc_communicator(obj))[0]
success, message = result.success, result.message

if not is_paused:
async with self.model_update_lock.writer_lock:
result = (await self.update_weights_from_ipc_communicator(obj))[0]
success, message = result.success, result.message
except Exception as e:
error_msg = f"IPC weight update failed: {str(e)}"
logger.error(error_msg)
Expand All @@ -632,9 +637,17 @@ async def post_process_weights(
) -> Tuple[bool, str]:
"""Trigger post-processing hooks for weights after loading (e.g., Marlin conversion)."""
self.auto_create_handle_loop()
async with self.model_update_lock.writer_lock:
results = await self.post_process_weights_communicator(obj)
return _Communicator.merge_results(results)

async with self.is_pause_cond:
is_paused = self.is_pause
if is_paused:
results = await self.post_process_weights_communicator(obj)

if not is_paused:
async with self.model_update_lock.writer_lock:
results = await self.post_process_weights_communicator(obj)

return _Communicator.merge_results(results)

async def _unload_lora_adapter_locked(
self: TokenizerManager,
Expand Down
Loading