[rl] Stateful Trainer Send: New Abstractions [1/N] (#48042)
Signed-off-by: haoaaron <ahao@anyscale.com> Signed-off-by: Aaron Hao <ahao@anyscale.com> Co-authored-by: Sumanth R Hegde <39546518+SumanthRH@users.noreply.github.com>
This commit is contained in:
co-authored by
Sumanth R Hegde
parent
9354f22204
commit
fb1d8ccaf5
@@ -6,7 +6,10 @@ import importlib
|
||||
from collections.abc import Callable
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from vllm.distributed.weight_transfer.base import WeightTransferEngine
|
||||
from vllm.distributed.weight_transfer.base import (
|
||||
TrainerWeightTransferEngine,
|
||||
WeightTransferEngine,
|
||||
)
|
||||
from vllm.logger import init_logger
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -14,6 +17,11 @@ if TYPE_CHECKING:
|
||||
|
||||
from vllm.config import VllmConfig
|
||||
from vllm.config.weight_transfer import WeightTransferConfig
|
||||
from vllm.distributed.weight_transfer.base import (
|
||||
VLLMWeightSyncClient,
|
||||
WeightSource,
|
||||
WeightTransferInitInfo,
|
||||
)
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
@@ -111,6 +119,94 @@ class WeightTransferEngineFactory:
|
||||
return engine_cls(config, vllm_config, device, model)
|
||||
|
||||
|
||||
class WeightTransferTrainerFactory:
|
||||
"""Factory for creating trainer-side weight transfer engines.
|
||||
|
||||
Parallel to `WeightTransferEngineFactory`, with its own lazy-import
|
||||
registry. The trainer-side and worker-side registries are kept separate:
|
||||
they share backend names by convention, but the trainer process never
|
||||
instantiates a worker engine and vice versa, so unifying them would only
|
||||
couple the import graphs.
|
||||
"""
|
||||
|
||||
_registry: dict[str, Callable[[], type[TrainerWeightTransferEngine]]] = {}
|
||||
|
||||
@classmethod
|
||||
def register_engine(
|
||||
cls,
|
||||
name: str,
|
||||
module_path_or_cls: "str | type[TrainerWeightTransferEngine]",
|
||||
class_name: str | None = None,
|
||||
) -> None:
|
||||
"""Register a trainer engine. Same conventions as
|
||||
`WeightTransferEngineFactory.register_engine`."""
|
||||
if name in cls._registry:
|
||||
raise ValueError(
|
||||
f"Weight transfer trainer engine '{name}' is already registered."
|
||||
)
|
||||
|
||||
if isinstance(module_path_or_cls, str):
|
||||
module_path = module_path_or_cls
|
||||
if class_name is None:
|
||||
raise ValueError(
|
||||
"class_name is required when registering with module path"
|
||||
)
|
||||
|
||||
def loader() -> type[TrainerWeightTransferEngine]:
|
||||
module = importlib.import_module(module_path)
|
||||
return getattr(module, class_name)
|
||||
|
||||
cls._registry[name] = loader
|
||||
else:
|
||||
engine_cls = module_path_or_cls
|
||||
cls._registry[name] = lambda: engine_cls
|
||||
|
||||
@classmethod
|
||||
def trainer_init(
|
||||
cls,
|
||||
backend: str,
|
||||
config: "WeightTransferConfig",
|
||||
init_info: "WeightTransferInitInfo",
|
||||
*,
|
||||
client: "VLLMWeightSyncClient",
|
||||
source: "WeightSource",
|
||||
) -> TrainerWeightTransferEngine:
|
||||
"""Build and rendezvous a ready-to-send trainer engine.
|
||||
|
||||
Called on every trainer rank (multi-rank trainers construct on all
|
||||
ranks; the sender is resolved inside the engine's ``trainer_init``).
|
||||
|
||||
Args:
|
||||
backend: Backend name (must be registered).
|
||||
config: Backend-specific weight transfer config.
|
||||
init_info: Backend-specific trainer init info.
|
||||
client: Inference-side control-plane client.
|
||||
source: `WeightSource` of `(name, tensor)` pairs to send each round.
|
||||
|
||||
Raises:
|
||||
ValueError: If the backend is not registered.
|
||||
"""
|
||||
if backend not in cls._registry:
|
||||
available = list(cls._registry.keys())
|
||||
raise ValueError(
|
||||
f"Invalid weight transfer backend: {backend}. "
|
||||
f"Available trainer engines: {available}"
|
||||
)
|
||||
engine_cls = cls._registry[backend]()
|
||||
|
||||
logger.info(
|
||||
"Creating weight transfer trainer engine: %s",
|
||||
engine_cls.__name__,
|
||||
)
|
||||
|
||||
return engine_cls.trainer_init(
|
||||
config=config,
|
||||
init_info=init_info,
|
||||
client=client,
|
||||
source=source,
|
||||
)
|
||||
|
||||
|
||||
# Register built-in weight transfer engines here.
|
||||
# Registration should be centralized to ensure lazy loading -
|
||||
# engine modules are only imported when actually used.
|
||||
|
||||
Reference in New Issue
Block a user