rlightning.engine¶
Engine module for reinforcement learning training loops.
This module provides various RL engine implementations including synchronous, asynchronous, and specialized engines for different training paradigms.
- Available engines:
SyncRLEngine: Synchronous reinforcement learning engine.
AsyncRLEngine: Asynchronous reinforcement learning engine.
EvaluationEngine: Engine for policy evaluation.
RSLRLEngine: Engine for RSL-RL based training.
- class rlightning.engine.AsyncRLEngine(config: MainConfig, env_group: EnvGroup, policy_group: PolicyGroup, buffer: DataBuffer)[source]¶
Bases:
BaseEngineAsynchronous reinforcement learning engine.
This engine implements asynchronous training where: - Rollout thread: Collects experience from environments. - Training thread: Updates policy using collected experience. - Weight update thread: Broadcasts updated weights to eval policies.
- rollout() None[source]¶
Perform rollout to collect experience from environments.
Runs the rollout loop, collecting experience by stepping through environments and storing transitions in the buffer. Runs until the done_flag is set.
- run() None[source]¶
Launch asynchronous training threads.
Starts three worker threads (rollout, train, weight update) and waits for them to complete. Prints timing summary when finished.
- class rlightning.engine.AsyncRSLRLEngine(config: MainConfig, env_group: EnvGroup, policy_group: PolicyGroup, buffer: DataBuffer)[source]¶
Bases:
AsyncRLEngineAsync RSL-RL engine.
- rollout(*args, **kwargs)[source]¶
Perform rollout to collect experience from environments.
Runs the rollout loop, collecting experience by stepping through environments and storing transitions in the buffer. Runs until the done_flag is set.
- sync_weights() None[source]¶
sync weights from train policy to eval policy.
- Parameters:
policy_group – Policy group containing train and eval policies.
- class rlightning.engine.BaseEngine(config: MainConfig, env_group: EnvGroup | None = None, policy_group: PolicyGroup | None = None, buffer: DataBuffer | None = None)[source]¶
Bases:
ABCAbstract base class for reinforcement learning engines.
This class defines the common interface for all RL engines, including methods for warm-up, training, rollout, weight updates, and timing.
- abstractmethod evaluate(*args: Any, **kwargs: Any) None[source]¶
User defined evaluation to collect experience.
- Parameters:
*args – Variable positional arguments.
**kwargs – Variable keyword arguments.
- iter_epochs(num_epochs: int) iter[source]¶
An iterator that yields epoch numbers up to num_epochs.
- Parameters:
num_epochs (int) – The number of epochs to iterate through.
- Returns:
An iterator over epoch numbers.
- Return type:
iter
- print_timing_summary(reset: bool = False) None[source]¶
Print timing summary for profiling.
- Parameters:
reset – If True, reset timing statistics after printing.
- abstractmethod rollout(*args: Any, **kwargs: Any) None[source]¶
User defined rollout to collect experience.
- Parameters:
*args – Variable positional arguments.
**kwargs – Variable keyword arguments.
- abstractmethod run() None[source]¶
Run the RL training flow.
This method should execute the complete RL training flow, coordinating rollout, training, and weight updates.
- sync_weights() None[source]¶
sync weights from train policy to eval policy.
- Parameters:
policy_group – Policy group containing train and eval policies.
- abstractmethod train() None[source]¶
User defined training on collected experience.
- Parameters:
*args – Variable positional arguments.
**kwargs – Variable keyword arguments.
- class rlightning.engine.EvaluationEngine(*args: Any, **kwargs: Any)[source]¶
Bases:
BaseEngineEvaluation engine for policy testing.
This engine runs policy evaluation without training, useful for testing trained policies on various environments.
- get_metrics(policy_resp: PolicyResponse) None[source]¶
Extract and process metrics from policy response.
- Parameters:
policy_resp – Policy response containing evaluation info.
- run = MockModule('torch.inference_mode')¶
- class rlightning.engine.RSLRLEngine(config, env_group=None, policy_group=None, buffer=None)[source]¶
Bases:
SyncRLEngineRSL-RL training engine.
This engine extends SyncRLEngine to support RSL-RL style policies, with modified rollout and training loops for on-policy learning.
- rollout(obj_set: str, prefix: str = '', is_eval: bool = False) None[source]¶
Perform rollout to collect experience from environments.
Collects experience by stepping through environments, applying policy post-processing before storing transitions in the buffer.
- Parameters:
obj_set – Object set identifier for environment reset options.
prefix – Prefix for logging metrics.
is_eval – If True, skips buffer storage (evaluation only).
- train() None[source]¶
Perform training on collected experience.
Samples data from the buffer, updates the dataset, and trains the policy. Designed for on-policy training with batch_size=-1.
- Raises:
ValueError – If buffer is empty when training.
- class rlightning.engine.SyncRLEngine(config: MainConfig, env_group: EnvGroup = None, policy_group: PolicyGroup | None = None, buffer: DataBuffer | None = None)[source]¶
Bases:
BaseEngineSynchronous reinforcement learning engine.
This engine implements synchronous training where rollout and training happen sequentially. Supports both on-policy and off-policy algorithms.
- evaluate(obj_set: str, prefix: str = '') None[source]¶
Perform evaluation to collect experience from environments.
Uses eval_env_list inside env_group if available, otherwise falls back to the training env_list.
- Parameters:
obj_set – Object set identifier for environment reset options.
prefix – Prefix for logging metrics.
- rollout(obj_set: str, prefix: str = '') None[source]¶
Perform rollout to collect experience from environments.
- Parameters:
obj_set – Object set identifier for environment reset options.
prefix – Prefix for logging metrics.
- run() None[source]¶
Run the main training loop.
Executes the training loop for the configured number of epochs, performing rollout, training, and periodic evaluation.
- train() None[source]¶
Perform training on collected experience.
Samples data from the buffer, updates the dataset, and trains the policy. Logs training info metrics.
- Raises:
ValueError – If buffer size is smaller than batch size when sampling without replacement.