[Core][Distributed] Add process-checkpoint lifecycle hooks for communicators (starting with Flashinfer) (#46877)
Signed-off-by: Schwinn Saereesitthipitak <schwinns@nvidia.com>
This commit is contained in:
@@ -885,6 +885,20 @@ class FlashInferNVLinkOneSidedManager(All2AllManagerBase):
|
||||
self.mapping = None
|
||||
self.initialized = False
|
||||
|
||||
def checkpoint_prepare(self) -> None:
|
||||
if self.initialized:
|
||||
assert self.moe_alltoall is not None
|
||||
self.moe_alltoall.checkpoint_prepare()
|
||||
|
||||
def checkpoint_restore(self) -> None:
|
||||
if self.initialized:
|
||||
assert self.moe_alltoall is not None
|
||||
from vllm.distributed.device_communicators.mnnvl_compat import (
|
||||
CustomCommunicator,
|
||||
)
|
||||
|
||||
self.moe_alltoall.checkpoint_restore(CustomCommunicator(self.cpu_group))
|
||||
|
||||
|
||||
class MoriAll2AllManager(All2AllManagerBase):
|
||||
def __init__(self, cpu_group, all2all_backend: str):
|
||||
|
||||
@@ -7,8 +7,11 @@ import torch
|
||||
import torch.distributed as dist
|
||||
from torch.distributed import ProcessGroup
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.utils import is_moe_layer
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
|
||||
class Cache:
|
||||
def __init__(self):
|
||||
@@ -137,6 +140,18 @@ class All2AllManagerBase:
|
||||
def max_sms_used(self) -> int | None:
|
||||
return None # None means it could use the whole GPU
|
||||
|
||||
def checkpoint_prepare(self) -> None:
|
||||
logger.warning_once(
|
||||
"%s.checkpoint_prepare is not implemented; skipping.",
|
||||
type(self).__name__,
|
||||
)
|
||||
|
||||
def checkpoint_restore(self) -> None:
|
||||
logger.warning_once(
|
||||
"%s.checkpoint_restore is not implemented; skipping.",
|
||||
type(self).__name__,
|
||||
)
|
||||
|
||||
def combine(self, hidden_states: torch.Tensor, is_sequence_parallel: bool = False):
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -216,6 +231,12 @@ class DeviceCommunicatorBase:
|
||||
dist.all_reduce(input_, group=self.device_group)
|
||||
return input_
|
||||
|
||||
def checkpoint_prepare(self) -> None:
|
||||
"""Prepare reclaimable communicator state for checkpoint (default: no-op)."""
|
||||
|
||||
def checkpoint_restore(self) -> None:
|
||||
"""Restore communicator state after checkpoint (default: no-op)."""
|
||||
|
||||
def all_gather(self, input_: torch.Tensor, dim: int = -1) -> torch.Tensor:
|
||||
if dim < 0:
|
||||
# Convert negative dim to positive.
|
||||
|
||||
@@ -571,6 +571,22 @@ class CudaCommunicator(DeviceCommunicatorBase):
|
||||
self.all2all_manager.destroy()
|
||||
self.all2all_manager = None # type: ignore[assignment]
|
||||
|
||||
def checkpoint_prepare(self) -> None:
|
||||
# Only FlashInfer all-reduce and FlashInfer all2all are supported for now.
|
||||
from .flashinfer_all_reduce import checkpoint_prepare_fi_ar_workspaces
|
||||
|
||||
checkpoint_prepare_fi_ar_workspaces(self.cpu_group)
|
||||
if self.all2all_manager is not None:
|
||||
self.all2all_manager.checkpoint_prepare()
|
||||
|
||||
def checkpoint_restore(self) -> None:
|
||||
# Only FlashInfer all-reduce and FlashInfer all2all are supported for now.
|
||||
from .flashinfer_all_reduce import checkpoint_restore_fi_ar_workspaces
|
||||
|
||||
checkpoint_restore_fi_ar_workspaces(self.cpu_group)
|
||||
if self.all2all_manager is not None:
|
||||
self.all2all_manager.checkpoint_restore()
|
||||
|
||||
def all_gatherv(
|
||||
self,
|
||||
input_: torch.Tensor | list[torch.Tensor],
|
||||
|
||||
@@ -6,6 +6,7 @@ import atexit
|
||||
import os
|
||||
import random
|
||||
import threading
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
@@ -39,6 +40,7 @@ _fi_ar_workspace = None
|
||||
# allreduce backend or a fallback backend when the primary workspace is not
|
||||
# available on the current topology.
|
||||
_fi_ar_quant_workspace = None
|
||||
_fi_ar_workspace_groups: dict[int, ProcessGroup] = {}
|
||||
|
||||
|
||||
def _create_workspace(
|
||||
@@ -81,6 +83,14 @@ def _create_workspace(
|
||||
return None
|
||||
finally:
|
||||
random.setstate(rng_state)
|
||||
workspace_id = id(workspace)
|
||||
workspace_group = _fi_ar_workspace_groups.get(workspace_id)
|
||||
if workspace_group is not None and workspace_group is not group:
|
||||
raise RuntimeError(
|
||||
"FlashInfer returned an all-reduce workspace already associated "
|
||||
"with a different process group"
|
||||
)
|
||||
_fi_ar_workspace_groups[workspace_id] = group
|
||||
logger.debug(
|
||||
"Initialized FlashInfer All Reduce workspace: backend=%s, "
|
||||
"world_size=%d, rank=%d, max_token_num=%d, hidden_dim=%d, dtype=%s",
|
||||
@@ -111,7 +121,7 @@ def _resolve_fi_ar_backend() -> tuple[str, bool]:
|
||||
# Default to mnnvl for both single- and multi-node setups. The mnnvl
|
||||
# cudagraph hang that previously forced single-node to trtllm
|
||||
# (https://github.com/vllm-project/vllm/issues/35772) was fixed upstream in
|
||||
# FlashInfer (>= 0.6.12, vLLM pins 0.6.13), so mnnvl is safe here. trtllm
|
||||
# FlashInfer (>= 0.6.12, vLLM pins 0.6.15), so mnnvl is safe here. trtllm
|
||||
# does not support multi-node allreduce, so mnnvl is required there anyway.
|
||||
# mnnvl needs NVSwitch multicast; on single-node topologies without it,
|
||||
# fall back to trtllm so fused allreduce stays enabled.
|
||||
@@ -268,6 +278,36 @@ def destroy_fi_ar_workspace():
|
||||
_fi_ar_quant_workspace.destroy()
|
||||
|
||||
_fi_ar_workspace = _fi_ar_quant_workspace = None
|
||||
_fi_ar_workspace_groups.clear()
|
||||
|
||||
|
||||
def _fi_ar_workspaces_for_group(group: ProcessGroup) -> list[Any]:
|
||||
workspaces = [_fi_ar_workspace]
|
||||
if _fi_ar_quant_workspace is not _fi_ar_workspace:
|
||||
workspaces.append(_fi_ar_quant_workspace)
|
||||
|
||||
group_workspaces = []
|
||||
for workspace in workspaces:
|
||||
if workspace is None:
|
||||
continue
|
||||
workspace_group = _fi_ar_workspace_groups.get(id(workspace))
|
||||
if workspace_group is None:
|
||||
raise RuntimeError(
|
||||
"FlashInfer all-reduce workspace process group was not retained"
|
||||
)
|
||||
if workspace_group is group:
|
||||
group_workspaces.append(workspace)
|
||||
return group_workspaces
|
||||
|
||||
|
||||
def checkpoint_prepare_fi_ar_workspaces(group: ProcessGroup) -> None:
|
||||
for workspace in _fi_ar_workspaces_for_group(group):
|
||||
workspace.checkpoint_prepare()
|
||||
|
||||
|
||||
def checkpoint_restore_fi_ar_workspaces(group: ProcessGroup) -> None:
|
||||
for workspace in _fi_ar_workspaces_for_group(group):
|
||||
workspace.checkpoint_restore(TorchDistBackend(group=group))
|
||||
|
||||
|
||||
atexit.register(destroy_fi_ar_workspace)
|
||||
|
||||
Reference in New Issue
Block a user