Policy & PolicyGroup¶
Policy encapsulates a complete RL algorithm: the neural network, optimizer, rollout logic, and training step. PolicyGroup manages collections of Policy workers and handles weight synchronization between them.
Overview¶
At runtime, RLightning runs two roles of the same Policy class:
Role |
Responsibilities |
Key Methods Called |
|---|---|---|
EVAL policy |
Inference during rollout; pulls updated weights from train policy in background |
|
TRAIN policy |
Gradient updates; signals eval policy after each epoch |
|
Both roles are instances of the same user-defined subclass of BasePolicy.
The role_type attribute (PolicyRole.EVAL or PolicyRole.TRAIN)
determines which initialization path and methods are active.
sequenceDiagram
participant TP as Train Policy
participant WB as WeightBuffer
participant EP as Eval Policy
loop Each Epoch
EP->>EP: rollout_step (inference)
TP->>TP: train (gradient update)
TP->>WB: send_weights() — pushes state_dict to WeightBuffer
TP->>EP: notify_update_weights() — sets update signal
Note over EP: Background daemon thread wakes up
EP->>WB: update_weights_from_buffer() — loads WeightBuffer into model
end
Weight transfer runs in a background daemon thread on the eval side, so rollout
and weight updates overlap without stalling the main training loop. The updater
thread is only started when weight_buffer_strategy is not "None".
BasePolicy Interface¶
BasePolicy (rlightning/policy/base_policy.py) is the abstract base
class that all policies must subclass.
Method |
Override? |
Description |
|---|---|---|
|
Required |
Create model(s) and append to |
|
Required |
Create optimizer; store as |
|
Required |
Pure inference: |
|
Required |
Post-process environment and policy outputs (e.g., value bootstrapping, advantage computation). Called by RSL-RL style engines after each rollout step. |
|
Required |
Receive a |
|
Required |
Run one training epoch using |
|
Required |
Return |
|
Required |
Load weights into model(s). Default handles |
|
Optional |
Persist weights to disk. Default saves all models in
|
|
Optional |
Wrap models with |
|
Rarely needed |
Called by the framework on EVAL workers. Default handles model creation, eval mode, and weight updater thread. |
|
Rarely needed |
Called by the framework on TRAIN workers. Default handles model creation, train mode, DDP wrapping, and optimizer setup. |
Note
self.model_list is a list of (name: str, model: nn.Module)
tuples. Populate it in construct_network so the framework can
automatically handle weight extraction, DDP wrapping, and checkpointing.
Weight Synchronization¶
After each training epoch, train policy pushes weights to eval policy via
a WeightBuffer. Three buffer strategies trade memory for latency:
Strategy |
Behavior |
|---|---|
|
Two weight buffers per eval policy; writer alternates between them. Reader always gets the latest complete snapshot. Default for most cases. |
|
One shared buffer per node, shared by all eval policies on the same node. Reduces memory usage in colocated deployments. |
|
Weights are split across buffer shards; used with
|
Configure the strategy in policy.weight_buffer:
policy:
weight_buffer:
type: WeightBuffer # or CPUWeightBuffer, ShardedWeightBuffer
buffer_strategy: Double # None, Double, Shared, Sharded
PolicyGroup¶
PolicyGroup is the WorkerGroup wrapper around Policy workers. It:
Maintains separate pools of EVAL and TRAIN workers.
Routes async rollout requests across EVAL workers via
BatchRouterfor load balancing.Broadcasts weight sync from the TRAIN worker to all EVAL workers after each epoch.
The number of workers in each pool is set in cluster:
cluster:
train_worker_num: 2 # number of TRAIN policy actors
eval_worker_num: 4 # number of EVAL policy actors
Configuration Reference¶
Key fields in the policy config section (PolicyConfig):
Field |
Default |
Description |
|---|---|---|
|
(required) |
Registered name of the Policy class, e.g. |
|
|
Rollout mode: |
|
|
Weight buffer class. |
|
|
Weight transfer strategy. |
|
(varies) |
Passed to |
See Also¶
Customize Policy — Step-by-step guide to implementing a custom policy.
Engine — How engines call Policy methods in the training loop.
Placement Strategy — How TRAIN and EVAL workers are placed.