Weight sync refactor + move sparse nccl engine (#44353)
Signed-off-by: hao-aaron <ahao@anyscale.com> Signed-off-by: haoaaron <ahao@anyscale.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
f651a8a9a4
commit
77a9c5ae28
@@ -377,12 +377,6 @@ class GPUModelRunner(LoRAModelRunnerMixin):
|
||||
|
||||
GPUModelRunnerV1.reload_weights(self, *args, **kwargs) # type: ignore[arg-type]
|
||||
|
||||
def apply_sparse_weight_patches(self, *args, **kwargs) -> None:
|
||||
# TODO: Use full version instead of import when fully migrated to v2
|
||||
from vllm.v1.worker.gpu_model_runner import GPUModelRunner as GPUModelRunnerV1
|
||||
|
||||
GPUModelRunnerV1.apply_sparse_weight_patches(self, *args, **kwargs) # type: ignore[arg-type]
|
||||
|
||||
def update_config(self, *args, **kwargs) -> None:
|
||||
# TODO(Wentao): Use full version instead of import when fully migrated to v2
|
||||
from vllm.v1.worker.gpu_model_runner import GPUModelRunner as GPUModelRunnerV1
|
||||
|
||||
@@ -49,7 +49,6 @@ from vllm.distributed.parallel_state import (
|
||||
is_global_first_rank,
|
||||
prepare_communication_buffer_for_model,
|
||||
)
|
||||
from vllm.distributed.weight_transfer.base import SparseWeightPatch
|
||||
from vllm.forward_context import (
|
||||
BatchDescriptor,
|
||||
set_forward_context,
|
||||
@@ -3244,44 +3243,6 @@ class GPUModelRunner(
|
||||
return self.model.unwrap()
|
||||
return self.model
|
||||
|
||||
def apply_sparse_weight_patches(self, patches: Iterable[SparseWeightPatch]) -> None:
|
||||
"""Apply sparse flat-index patches directly to existing model params."""
|
||||
model = self.get_model()
|
||||
for patch in patches:
|
||||
param = model.get_parameter(patch.name)
|
||||
if not param.data.is_contiguous():
|
||||
raise NotImplementedError(
|
||||
"Sparse weight updates currently require contiguous params: "
|
||||
f"{patch.name}"
|
||||
)
|
||||
|
||||
if patch.indices.dtype != torch.int32:
|
||||
raise ValueError(
|
||||
"Sparse weight updates currently require int32 indices: "
|
||||
f"{patch.name}"
|
||||
)
|
||||
if patch.indices.ndim != 1 or patch.values.ndim != 1:
|
||||
raise ValueError(
|
||||
f"Sparse weight patches must be 1D flattened updates: {patch.name}"
|
||||
)
|
||||
if patch.indices.numel() != patch.values.numel():
|
||||
raise ValueError(
|
||||
"`indices` and `values` must have matching lengths for "
|
||||
f"{patch.name}"
|
||||
)
|
||||
if patch.values.dtype != param.dtype:
|
||||
raise ValueError(
|
||||
f"Sparse values dtype {patch.values.dtype} does not match "
|
||||
f"parameter dtype {param.dtype} for {patch.name}"
|
||||
)
|
||||
|
||||
flat_param = param.data.view(-1)
|
||||
flat_param.index_copy_(
|
||||
0,
|
||||
patch.indices.to(device=flat_param.device, dtype=torch.long),
|
||||
patch.values.to(device=flat_param.device),
|
||||
)
|
||||
|
||||
def get_supported_generation_tasks(self) -> list[GenerationTask]:
|
||||
model = self.get_model()
|
||||
supported_tasks = list[GenerationTask]()
|
||||
|
||||
@@ -155,7 +155,6 @@ class Worker(WorkerBase):
|
||||
# is available, since the engine needs a reference to the model.
|
||||
self.weight_transfer_engine: WeightTransferEngine | None = None
|
||||
self._weight_update_active = False
|
||||
self._is_checkpoint_format = True
|
||||
|
||||
# Torch/CUDA profiler. Enabled and configured through profiler_config.
|
||||
# Profiler wrapper is created lazily in profile() when start is called,
|
||||
@@ -416,7 +415,8 @@ class Worker(WorkerBase):
|
||||
if self.vllm_config.weight_transfer_config is not None:
|
||||
self.weight_transfer_engine = WeightTransferEngineFactory.create_engine(
|
||||
self.vllm_config.weight_transfer_config,
|
||||
self.vllm_config.parallel_config,
|
||||
self.vllm_config,
|
||||
self.device,
|
||||
self.model_runner.get_model(),
|
||||
)
|
||||
|
||||
@@ -1161,16 +1161,16 @@ class Worker(WorkerBase):
|
||||
typed_init_info = self.weight_transfer_engine.parse_init_info(init_info)
|
||||
self.weight_transfer_engine.init_transfer_engine(typed_init_info)
|
||||
|
||||
def start_weight_update(self, is_checkpoint_format: bool = True) -> None:
|
||||
def start_weight_update(self) -> None:
|
||||
"""
|
||||
Start a new weight update session.
|
||||
|
||||
Args:
|
||||
is_checkpoint_format: Whether incoming weights are in checkpoint
|
||||
format (need layerwise processing) or kernel format (direct
|
||||
copy / sparse patch application).
|
||||
Delegates engine-specific preparation (e.g. layerwise reload setup) to
|
||||
the configured weight transfer engine. The worker only tracks that a
|
||||
session is active.
|
||||
"""
|
||||
self._check_weight_transfer_engine()
|
||||
assert self.weight_transfer_engine is not None
|
||||
|
||||
if self._weight_update_active:
|
||||
raise RuntimeError(
|
||||
@@ -1178,16 +1178,7 @@ class Worker(WorkerBase):
|
||||
"active. Call finish_weight_update first."
|
||||
)
|
||||
|
||||
if is_checkpoint_format:
|
||||
from vllm.model_executor.model_loader.reload import (
|
||||
initialize_layerwise_reload,
|
||||
)
|
||||
|
||||
model = self.model_runner.model
|
||||
with torch.device(self.device):
|
||||
initialize_layerwise_reload(model)
|
||||
|
||||
self._is_checkpoint_format = is_checkpoint_format
|
||||
self.weight_transfer_engine.start_weight_update()
|
||||
self._weight_update_active = True
|
||||
|
||||
def update_weights(self, update_info: dict) -> None:
|
||||
@@ -1208,82 +1199,24 @@ class Worker(WorkerBase):
|
||||
"start_weight_update must be called before update_weights."
|
||||
)
|
||||
|
||||
update_succeeded = False
|
||||
try:
|
||||
# Parse dict into backend-specific typed dataclass
|
||||
typed_update_info = self.weight_transfer_engine.parse_update_info(
|
||||
update_info
|
||||
)
|
||||
|
||||
with torch.device(self.device):
|
||||
if self._is_checkpoint_format:
|
||||
if typed_update_info.update_kind != "dense":
|
||||
raise ValueError(
|
||||
"Sparse weight updates require "
|
||||
"`start_weight_update(is_checkpoint_format=False)`."
|
||||
)
|
||||
|
||||
model = self.model_runner.model
|
||||
|
||||
# Use layerwise reload pattern for checkpoint format weights
|
||||
self.weight_transfer_engine.receive_weights(
|
||||
typed_update_info,
|
||||
load_weights=model.load_weights,
|
||||
)
|
||||
elif typed_update_info.update_kind == "sparse_flat":
|
||||
if self.parallel_config.world_size != 1:
|
||||
raise NotImplementedError(
|
||||
"Sparse weight updates currently require TP=1 and PP=1"
|
||||
)
|
||||
self.weight_transfer_engine.receive_sparse_weights(
|
||||
typed_update_info,
|
||||
apply_patches=self.model_runner.apply_sparse_weight_patches,
|
||||
)
|
||||
else:
|
||||
model = self.model_runner.model
|
||||
|
||||
# Weights are already in kernel format, copy directly.
|
||||
def load_weights_direct(
|
||||
weights: list[tuple[str, torch.Tensor]],
|
||||
) -> None:
|
||||
for name, weight in weights:
|
||||
param = model.get_parameter(name)
|
||||
param.copy_(weight)
|
||||
|
||||
self.weight_transfer_engine.receive_weights(
|
||||
typed_update_info,
|
||||
load_weights=load_weights_direct,
|
||||
)
|
||||
|
||||
# NCCL broadcast/packed path are asynchronous.
|
||||
# Sync here so the next step uses the new weights.
|
||||
torch.accelerator.synchronize()
|
||||
update_succeeded = True
|
||||
finally:
|
||||
if not update_succeeded:
|
||||
self._weight_update_active = False
|
||||
self._is_checkpoint_format = True
|
||||
self.weight_transfer_engine.update_weights(update_info)
|
||||
except BaseException:
|
||||
self._weight_update_active = False
|
||||
raise
|
||||
|
||||
def finish_weight_update(self) -> None:
|
||||
"""Finish the current weight update session."""
|
||||
self._check_weight_transfer_engine()
|
||||
assert self.weight_transfer_engine is not None
|
||||
|
||||
if not self._weight_update_active:
|
||||
raise RuntimeError(
|
||||
"finish_weight_update called without a matching start_weight_update."
|
||||
)
|
||||
|
||||
if self._is_checkpoint_format:
|
||||
from vllm.model_executor.model_loader.reload import (
|
||||
finalize_layerwise_reload,
|
||||
)
|
||||
|
||||
model = self.model_runner.model
|
||||
with torch.device(self.device):
|
||||
finalize_layerwise_reload(model, self.model_config)
|
||||
|
||||
self.weight_transfer_engine.finish_weight_update()
|
||||
self._weight_update_active = False
|
||||
self._is_checkpoint_format = True
|
||||
|
||||
def shutdown(self) -> None:
|
||||
gc.unfreeze()
|
||||
|
||||
Reference in New Issue
Block a user