rlightning.utils.placement¶
Placement module for global resource management and component scheduling.
This module provides: - GlobalResourceManager: Singleton manager for resource-aware component scheduling - ResourcePoolPlanner: Plans resource pools based on placement strategy (auto/manual) - ComponentScheduling: Defines resource requirements for each component type - PlacementStrategy: Strategies for creating Ray placement groups
- class rlightning.utils.placement.ColocatedPlacementStrategy(scheduling: ComponentScheduling)[source]¶
Bases:
ResourcePoolPlacementStrategyColocated placement strategy.
All components share a global resource pool. Workers are distributed across nodes with consideration for resource utilization.
- create_placement_groups(resource_pools: Dict[str, Dict[str, Any]] | None = None, max_colocate_count: int = 10, **kwargs) Dict[str, MockModule('ray.util.placement_group.PlacementGroup')][source]¶
Create placement groups for colocated components.
- Parameters:
resource_pools – Dictionary with “global_pool” configuration.
max_colocate_count – Maximum number of components per node.
**kwargs – Additional strategy options kept for interface compatibility.
- class rlightning.utils.placement.ComponentAllocation(component_type: str, node_to_gpu_indices: Dict[str, List[str]], total_gpus: int, worker_count: int)[source]¶
Bases:
objectRepresents resource allocation for a specific component type.
- gpu_indices¶
List of GPU index ranges, e.g., [“0-7”, “8-15”]
- class rlightning.utils.placement.ComponentScheduling(env_worker: List[Scheduling] | None = None, train_worker: Scheduling | None = None, eval_worker: Scheduling | None = None, buffer_worker: Scheduling | None = None)[source]¶
Bases:
objectScheduling configuration for all components.
This class holds the resource requirements for each component type: - env_worker: Environment workers (can have multiple groups) - train_worker: Training policy workers - eval_worker: Evaluation policy workers (rollout) - buffer_worker: Data buffer storage workers
- adjust_buffer_worker_num(train_node_count: int) None[source]¶
Adjust the buffer worker number to match the train worker number.
- buffer_worker: Scheduling | None = None¶
- env_worker: List[Scheduling] | None = None¶
- eval_worker: Scheduling | None = None¶
- get_component_requirements(component_type: str) Tuple[float, int][source]¶
Get resource requirements for all components.
- Returns:
Tuple of (total_gpus, total_cpus) for the specified component type.
- Raises:
ValueError – If component type is invalid or the worker is not configured.
- infer_auto_buffer_worker_num(cluster_info: Dict[str, Any]) None[source]¶
Infer the auto buffer worker number based on the train worker number.
- rollout_pool_requirements() Tuple[float, int][source]¶
Calculate resource requirements for rollout pool (eval + env).
- Returns:
Tuple of (total_gpus, total_cpus).
- train_pool_requirements() Tuple[float, int][source]¶
Calculate resource requirements for train pool (train + buffer).
- Returns:
Tuple of (total_gpus, total_cpus).
- train_worker: Scheduling | None = None¶
- class rlightning.utils.placement.DefaultPlacementStrategy(scheduling: ComponentScheduling)[source]¶
Bases:
PlacementStrategyDefault placement strategy with no specific placement groups.
Uses node affinity for buffer workers when multiple are needed, but otherwise relies on Ray’s default scheduling.
- buffer_strategys¶
List of scheduling strategies for buffer workers.
- _node_info¶
Node information for resource-aware choices.
- create_placement_groups() Dict[str, MockModule('ray.util.placement_group.PlacementGroup')][source]¶
Create placement groups based on the strategy.
For multiple buffer storages, creates node affinity strategies to place each buffer on a different node.
- Returns:
Empty dictionary (no placement groups created).
- class rlightning.utils.placement.DisaggregatePlacementStrategy(scheduling: ComponentScheduling)[source]¶
Bases:
ResourcePoolPlacementStrategyDisaggregate placement strategy.
Allocates separate resource pools for: - train_pool: Train workers + Buffer workers (colocated) - rollout_pool: Eval workers + Env workers (colocated)
This provides resource isolation between training and evaluation.
- create_placement_groups(resource_pools: Dict[str, Dict[str, Any]] | None = None, **kwargs) Dict[str, MockModule('ray.util.placement_group.PlacementGroup')][source]¶
Create placement groups based on resource pools.
- Parameters:
resource_pools – Dictionary with “train_pool” and “rollout_pool” configurations.
**kwargs – Additional strategy options kept for interface compatibility.
- class rlightning.utils.placement.GlobalResourceManager[source]¶
Bases:
objectSingleton resource manager for global resource management.
This manager orchestrates the following workflow: 1. Initialize with configuration and scheduling requirements 2. Discover cluster resources via ResourcePoolPlanner 3. Plan resource pools based on placement strategy 4. Create placement groups via the selected strategy 5. Track component distribution for monitoring
- Usage:
manager = GlobalResourceManager.get_instance() manager.initialize(placement_config, scheduling) strategy = manager.get_scheduling_strategy(“train”, worker_index=0)
- get_component_distribution() Dict[str, Dict[str, Dict[str, Any]]][source]¶
Return mapping of node_id -> {component_type: {“count”: N, “ids”: […]}}.
Format is aligned with ResourcePoolPlacementStrategy._node_component_distribution.
- classmethod get_instance() GlobalResourceManager[source]¶
Get the singleton instance..
- Returns:
The singleton GlobalResourceManager instance.
- get_placement_config() Config | None[source]¶
Get the placement configuration.
- Returns:
The placement configuration object.
- get_pool_for_component(component_type: str) ResourcePool | None[source]¶
Get the resource pool containing a specific component type.
- get_resource_planner() ResourcePoolPlanner | None[source]¶
Get the resource planner instance.
- get_resource_pools(pool_name: str = None) Dict[str, ResourcePool][source]¶
Get the planned resource pool by pool_name.
- get_scheduling() ComponentScheduling | None[source]¶
Get the component scheduling requirements.
- Returns:
The component scheduling object.
- get_scheduling_strategy(component_type: str, worker_index: int = 0) Any[source]¶
Get Ray scheduling strategy for a component.
- Parameters:
component_type – Type of component (“train”, “eval”, “buffer”, “env”).
worker_index – Index of the worker within its type.
- Returns:
Ray scheduling strategy (PlacementGroupSchedulingStrategy or “DEFAULT”).
- get_storage_to_train_workers() Dict[int, List[int]] | None[source]¶
Get storage -> train worker mapping from the active strategy.
- Returns:
Dictionary mapping storage indices to train worker indices.
- Raises:
RuntimeError – If PlacementManager is not initialized.
- initialize(cluster_cfg: ClusterConfig, scheduling: ComponentScheduling, config_path: str | None = None) None[source]¶
Initialize the placement manager with configuration.
This method performs the following steps: 1. Store configuration and scheduling requirements 2. Discover cluster resources 3. Validate scheduling requirements against available resources 4. Plan resource pools based on placement strategy 5. Create placement groups via the selected strategy 6. Track initial component distribution
- Parameters:
cluster_cfg – Cluster configuration, including placement settings.
scheduling – Component scheduling requirements.
config_path – Optional path to the configuration file.
- property is_initialized: bool¶
Check if the placement manager is initialized with a strategy..
- Returns:
True if a strategy has been set.
- refresh_component_distribution() Dict[str, Dict[str, int]][source]¶
Refresh and return the current component distribution across nodes.
This queries Ray’s actor registry to get real-time distribution.
- save_yaml_config(cluster_config_path: str, filename: str = 'resource_pool_auto.yaml', subdir: str = 'resource_pool') str[source]¶
Save current resource_pool yaml to disk.
- property strategy: PlacementStrategy | None¶
Get current placement strategy.
- Returns:
The current PlacementStrategy instance or None.
- class rlightning.utils.placement.NodeResource(node_id: str, ip: str, total_cpus: int, total_gpus: int, allocations: Dict[str, ~typing.List[~typing.Tuple[int, int]]]=<factory>, gpu_cursor: int = 0, max_allocated_gpus: int = 0)[source]¶
Bases:
objectRepresents a node’s resources.
This class is used in two contexts: - Cluster discovery: total_* reflects node total capacity; available_* is computed. - ResourcePool membership: total_* represents the node’s capacity,
and allocations records per-component GPU index ranges within the node’s index space.
Resource tracking uses gpu_cursor to track the next available GPU index. available_gpus is computed as total_gpus - gpu_cursor.
- allocate(gpus: int | List[int], component_types: List[str] | None = None, consume: bool = True) None[source]¶
Allocate resources directly on this node.
Modifies self.allocations and advances gpu_cursor.
- Parameters:
gpus – GPU units to allocate. Can be int (same for all) or list (per-component).
component_types – Components to record allocations for. If None, just advances cursor.
consume – If True, consume the GPUs from the node.
- property component_types: List[str]¶
Get list of component types that have allocations on this node.
- copy() NodeResource[source]¶
Create a deep copy of this node resource.
- class rlightning.utils.placement.PlacementStrategy(scheduling: ComponentScheduling)[source]¶
Bases:
ABCAbstract base class for placement strategies.
Defines the interface for creating Ray placement groups and determining scheduling strategies for different component types.
- scheduling¶
Cluster scheduling configuration.
- placement_groups¶
Created placement groups by name.
- storage_to_train_workers¶
Mapping from storage index to train worker indices.
- abstractmethod create_placement_groups() Dict[str, MockModule('ray.util.placement_group.PlacementGroup')][source]¶
Create placement groups based on the strategy.
- Returns:
Dictionary mapping group names to PlacementGroup instances.
- abstractmethod get_scheduling_strategy(component_type: str, worker_index: int = 0) Any[source]¶
Get scheduling strategy for a specific component.
- Parameters:
component_type – Type of component (‘env’, ‘train’, ‘eval’, ‘buffer’).
worker_index – Index of the worker within its type.
- Returns:
Scheduling strategy (PlacementGroupSchedulingStrategy or ‘DEFAULT’).
- class rlightning.utils.placement.ResourcePool(name: str, nodes: ~typing.List[~rlightning.utils.placement.resource_pool.NodeResource], _component_types: ~typing.List[str] | None = None, component_allocations: ~typing.Dict[str, ~rlightning.utils.placement.resource_pool.ComponentAllocation] = <factory>)[source]¶
Bases:
objectA resource pool containing allocated nodes for specific components.
This class tracks: - Which nodes belong to this pool - Which component types use this pool - Fine-grained GPU index allocations per component per node
component_types is auto-inferred from node allocations if not provided. If ‘train’ exists in allocations, ‘buffer’ is automatically added.
- component_allocations: Dict[str, ComponentAllocation]¶
- property component_types: List[str]¶
Get component types for this pool.
Auto-infers from node allocations if not explicitly set. Auto-adds ‘buffer’ if ‘train’ exists.
- classmethod from_yaml_dict(pool_cfg: Dict[str, Any], cluster_nodes: Dict[str, NodeResource], *, used_node_ids: set[str] | None = None) ResourcePool[source]¶
Build a ResourcePool from a YAML pool dict.
- Supported input fields:
name: str
num_node: int
num_gpus: int (per-node) OR List[int] (per-node gpus list)
optional node_ids: List[str] to pin pools to specific nodes
component keys: train/eval/env/buffer/… with values like “0-7, 8-15”
component_types is auto-inferred from allocations if not explicitly listed. ‘buffer’ is auto-added if ‘train’ exists.
- get_component_indices(component_type: str) str[source]¶
Get the GPU index string for a component type across all nodes.
- Returns:
Index string like “0-7” or “0-7, 8-15” for multi-node.
- get_component_node_count(component_type: str) int[source]¶
Get the number of nodes that have the component type.
- nodes: List[NodeResource]¶
- class rlightning.utils.placement.ResourcePoolPlanner(scheduling: ComponentScheduling, cluster_info: Dict[str, Any] | None = None)[source]¶
Bases:
objectPlans and manages resource pools for component placement.
This planner: 1. Discovers cluster resources 2. Validates scheduling requirements against available resources 3. Allocates resources to pools based on the placement strategy 4. Tracks fine-grained component-to-GPU mappings
- discover_cluster_resources() Dict[str, NodeResource][source]¶
Discover and cache cluster node resources.
- Returns:
Dictionary mapping node_id to NodeResource.
- get_component_node_count(component_type: str) int[source]¶
Get the number of nodes that have the component type.
- get_node_resources() Dict[str, NodeResource][source]¶
Get node resources dictionary.
- get_pool_for_component(component_type: str) ResourcePool | None[source]¶
Get the resource pool that contains a specific component type.
- get_resource_pools(pool_name: str = None) Dict[str, ResourcePool][source]¶
Get the planned resource pools.
- load_manual_resource_pools(resource_pool_cfg: List[Dict[str, Any]]) Dict[str, ResourcePool][source]¶
Load resource pools from a manual YAML config list.
- plan_resource_pools(strategy: str = 'disaggregate', env_strategy: str = 'default') Dict[str, ResourcePool][source]¶
Plan resource pools based on placement strategy.
- Parameters:
strategy – The placement strategy to use.
env_strategy – The environment placement strategy.
- Returns:
Dictionary mapping pool name to ResourcePool.
- to_yaml_config() Dict[str, Any][source]¶
Generate a YAML-compatible configuration representing the resource pools.
- Output format:
- resource_pool:
name: “train_pool” num_node: 1 num_gpus: 8 # per-node GPUs train: “0-7”
name: “rollout_pool” num_node: 2 num_gpus: 8 # per-node GPUs eval: “0-7, 8-15” env: “0-7, 8-15”
- validate_scheduling(strategy: str = 'disaggregate', env_strategy: str = 'default') Tuple[bool, str][source]¶
Validate that cluster resources can satisfy scheduling requirements.
- Parameters:
strategy – The placement strategy to use.
env_strategy – The environment strategy to use.
- Returns:
Tuple of (is_valid, error_message).
- class rlightning.utils.placement.Scheduling(worker_num: int, num_cpus: int, num_gpus: float, node_list: str | None = None)[source]¶
Bases:
objectScheduling configuration for a single worker type.
- rlightning.utils.placement.get_global_resource_manager() GlobalResourceManager | None[source]¶
Get the initialized global resource manager singleton instance.
- Returns:
GlobalResourceManager if initialized, None otherwise.
- rlightning.utils.placement.setup_cluster_scheduling(cfg: MainConfig) ComponentScheduling¶
Set up the scheduling configuration for all components from MainConfig.
This function: 1. Reads cluster configuration 2. Validates buffer storage configuration 3. Creates Scheduling objects for each component type
- Parameters:
cfg – The main configuration object.
- Returns:
ComponentScheduling with all component requirements.
- Raises:
ValueError – If configuration is invalid.
- rlightning.utils.placement.setup_component_scheduling(cfg: MainConfig) ComponentScheduling[source]¶
Set up the scheduling configuration for all components from MainConfig.
This function: 1. Reads cluster configuration 2. Validates buffer storage configuration 3. Creates Scheduling objects for each component type
- Parameters:
cfg – The main configuration object.
- Returns:
ComponentScheduling with all component requirements.
- Raises:
ValueError – If configuration is invalid.