|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the MIT license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +"""Base classes for TorchRL SGLang backends.""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import abc |
| 11 | +from collections.abc import Iterator |
| 12 | + |
| 13 | +import torch |
| 14 | + |
| 15 | + |
| 16 | +class RLSGLangEngine(abc.ABC): |
| 17 | + """Abstract base class for TorchRL SGLang engines that support weight updates. |
| 18 | +
|
| 19 | + All TorchRL SGLang engines should inherit from this class and implement |
| 20 | + the required methods for weight synchronization. |
| 21 | +
|
| 22 | + The SGLang backend uses HTTP-based communication with the SGLang server |
| 23 | + for generation, and NCCL for weight synchronization in RL training workflows. |
| 24 | +
|
| 25 | + Example: |
| 26 | + >>> # All SGLang engines implement the same interface |
| 27 | + >>> class MySGLangEngine(RLSGLangEngine): |
| 28 | + ... def get_tp_size(self) -> int: |
| 29 | + ... return self._tp_size |
| 30 | + ... |
| 31 | + ... def get_model_metadata(self) -> dict[str, tuple[torch.dtype, torch.Size]]: |
| 32 | + ... return self._model_metadata |
| 33 | + ... |
| 34 | + ... # ... implement other abstract methods |
| 35 | + """ |
| 36 | + |
| 37 | + @abc.abstractmethod |
| 38 | + def get_tp_size(self) -> int: |
| 39 | + """Get the tensor parallel size for this engine. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + int: Tensor parallel size |
| 43 | + """ |
| 44 | + |
| 45 | + @abc.abstractmethod |
| 46 | + def get_dp_size(self) -> int: |
| 47 | + """Get the data parallel size for this engine. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + int: Data parallel size |
| 51 | + """ |
| 52 | + |
| 53 | + @abc.abstractmethod |
| 54 | + def get_model_metadata(self) -> dict[str, tuple[torch.dtype, torch.Size]]: |
| 55 | + """Get model parameter metadata. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + dict: Mapping of parameter names to (dtype, shape) tuples |
| 59 | + """ |
| 60 | + |
| 61 | + @abc.abstractmethod |
| 62 | + def get_master_address(self) -> str: |
| 63 | + """Get the master address for weight synchronization. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + str: Master address (e.g., "localhost") |
| 67 | + """ |
| 68 | + |
| 69 | + @abc.abstractmethod |
| 70 | + def get_master_port(self) -> int: |
| 71 | + """Get the master port for weight synchronization. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + int: Master port number |
| 75 | + """ |
| 76 | + |
| 77 | + @abc.abstractmethod |
| 78 | + def init_weight_update_group( |
| 79 | + self, |
| 80 | + master_address: str | None = None, |
| 81 | + master_port: int | None = None, |
| 82 | + ) -> None: |
| 83 | + """Initialize the weight update communication group. |
| 84 | +
|
| 85 | + This should set up NCCL communication for weight broadcasting |
| 86 | + via the SGLang server's /init_weights_update_group API. |
| 87 | +
|
| 88 | + Args: |
| 89 | + master_address: Override for master address. If None, uses default. |
| 90 | + master_port: Override for master port. If None, uses default. |
| 91 | + """ |
| 92 | + |
| 93 | + @abc.abstractmethod |
| 94 | + def update_weights_from_distributed( |
| 95 | + self, |
| 96 | + name: str, |
| 97 | + dtype: torch.dtype, |
| 98 | + shape: tuple[int, ...], |
| 99 | + ) -> None: |
| 100 | + """Signal the server to receive a weight update via NCCL broadcast. |
| 101 | +
|
| 102 | + This coordinates with the SGLang server's /update_weights_from_distributed API |
| 103 | + to receive a single weight tensor broadcasted from the trainer. |
| 104 | +
|
| 105 | + Args: |
| 106 | + name: Name of the parameter to update |
| 107 | + dtype: Data type of the tensor |
| 108 | + shape: Shape of the tensor |
| 109 | + """ |
| 110 | + |
| 111 | + @abc.abstractmethod |
| 112 | + def update_weights(self, weights: Iterator[tuple[str, torch.Tensor]]) -> None: |
| 113 | + """Update model weights from an iterator. |
| 114 | +
|
| 115 | + This method should handle the actual weight broadcasting/updating |
| 116 | + using NCCL communication. |
| 117 | +
|
| 118 | + Args: |
| 119 | + weights: Iterator yielding (parameter_name, tensor) tuples |
| 120 | + """ |
0 commit comments