Storage

Rollout Storage

class rsl_rl.storage.rollout_storage.RolloutStorage[source]

Storage for the data collected during a rollout.

The rollout storage is populated by adding transitions during the rollout phase. It then returns a generator for learning, depending on the algorithm and the policy architecture.

class Transition[source]

Storage for a single state transition.

This class is populated incrementally during the rollout phase and then passed to RolloutStorage.add_transition() to record the data.

__init__()[source]

Initialize an empty transition container.

Return type:

None

observations: TensorDict | None

Observations at the current step.

actions: torch.Tensor | None

Actions taken at the current step.

rewards: torch.Tensor | None

Rewards received after the action.

dones: torch.Tensor | None

Done flags indicating episode termination.

values: torch.Tensor | None

Value estimates at the current step (RL only).

actions_log_prob: torch.Tensor | None

Log probability of the taken actions (RL only).

distribution_params: tuple[torch.Tensor, ...] | None

Parameters of the action distribution (RL only).

privileged_actions: torch.Tensor | None

Privileged (teacher) actions (distillation only).

hidden_states: tuple[HiddenState, HiddenState]

Hidden states for recurrent networks, e.g., (actor, critic).

clear()[source]

Reset all transition fields to None.

Return type:

None

class Batch[source]

A batch of data yielded by the rollout storage generators.

This class provides named access to mini-batch fields. Fields are optional to support different training modes (RL vs distillation) and architectures (feedforward vs recurrent).

__init__(observations=None, actions=None, values=None, advantages=None, returns=None, old_actions_log_prob=None, old_distribution_params=None, hidden_states=(None, None), masks=None, privileged_actions=None, dones=None)[source]

Initialize a batch container over rollout data.

Parameters:
  • observations (TensorDict | None)

  • actions (torch.Tensor | None)

  • values (torch.Tensor | None)

  • advantages (torch.Tensor | None)

  • returns (torch.Tensor | None)

  • old_actions_log_prob (torch.Tensor | None)

  • old_distribution_params (tuple[torch.Tensor, ...] | None)

  • hidden_states (tuple[HiddenState, HiddenState])

  • masks (torch.Tensor | None)

  • privileged_actions (torch.Tensor | None)

  • dones (torch.Tensor | None)

Return type:

None

observations: TensorDict | None

Batch of observations.

actions: torch.Tensor | None

Batch of actions.

values: torch.Tensor | None

Batch of value estimates (RL only).

advantages: torch.Tensor | None

Batch of advantage estimates (RL only).

returns: torch.Tensor | None

Batch of return targets (RL only).

old_actions_log_prob: torch.Tensor | None

Batch of log probabilities of the old actions (RL only).

old_distribution_params: tuple[torch.Tensor, ...] | None

Batch of parameters of the old action distribution (RL only).

privileged_actions: torch.Tensor | None

Batch of privileged (teacher) actions (distillation only).

dones: torch.Tensor | None

Batch of done flags (distillation only).

hidden_states: tuple[HiddenState, HiddenState]

Batch of hidden states for recurrent networks (RL recurrent only).

masks: torch.Tensor | None

Batch of trajectory masks for recurrent networks (RL recurrent only).

__init__(training_type, num_envs, num_transitions_per_env, obs, actions_shape, device='cpu')[source]

Allocate rollout buffers for a specific training mode and batch shape.

Parameters:
  • training_type (str)

  • num_envs (int)

  • num_transitions_per_env (int)

  • obs (tensordict.TensorDict)

  • actions_shape (tuple[int, ...] | list[int])

  • device (str)

Return type:

None

add_transition(transition)[source]

Add one transition to the storage at the current step index.

Parameters:

transition (Transition)

Return type:

None

clear()[source]

Reset the write cursor for the next rollout.

Return type:

None

generator()[source]

Yield per-timestep batches for distillation training.

Return type:

Generator[Batch, None, None]

mini_batch_generator(num_mini_batches, num_epochs=8)[source]

Yield shuffled flat mini-batches for feedforward RL updates.

Parameters:
  • num_mini_batches (int)

  • num_epochs (int)

Return type:

Generator[Batch, None, None]

recurrent_mini_batch_generator(num_mini_batches, num_epochs=8)[source]

Yield trajectory mini-batches with masks and recurrent hidden states.

Parameters:
  • num_mini_batches (int)

  • num_epochs (int)

Return type:

Generator[Batch, None, None]

Replay Buffer

class rsl_rl.storage.replay_buffer.ReplayBuffer[source]

Storage for transitions collected from the environment.

The replay buffer stores past transitions and returns random batches for learning. New transitions replace the oldest ones when the buffer is full.

class Transition[source]

Storage for a single state transition.

This class is populated during collection and passed to ReplayBuffer.add_transition().

__init__()[source]

Initialize an empty transition container.

Return type:

None

observations: TensorDict | None

Observations at the current step.

actions: torch.Tensor | None

Actions taken at the current step.

rewards: torch.Tensor | None

Rewards received after the action.

next_observations: TensorDict | None

Observations after the action, before any environment reset.

dones: torch.Tensor | None

(num_envs,).

Type:

Boolean flags indicating episode boundaries. Shape

terminated: torch.Tensor | None

(num_envs,).

Type:

Boolean flags indicating terminal states, excluding timeouts. Shape

clear()[source]

Reset all transition fields to None.

Return type:

None

class Batch[source]

A batch of data returned by ReplayBuffer.sample().

Each batch entry contains one or more consecutive transitions from the same environment. Observations and actions are taken from the first step. Next observations and termination flags are taken from the last valid step. Rewards are included for every step, with a mask marking the valid entries.

observations: tensordict.TensorDict

Observations at the first transition of each window.

actions: torch.Tensor

Actions at the first transition of each window.

rewards: torch.Tensor

(batch_size, length).

Type:

Raw rewards over the sampled windows. Shape

valid: torch.Tensor

(batch_size, length).

Type:

Validity masks for window entries. Shape

next_observations: tensordict.TensorDict

Observations after the last valid transition of each window.

terminated: torch.Tensor

Termination flags at the last valid transition of each window.

__init__(observations, actions, rewards, valid, next_observations, terminated)
Parameters:
  • observations (tensordict.TensorDict)

  • actions (torch.Tensor)

  • rewards (torch.Tensor)

  • valid (torch.Tensor)

  • next_observations (tensordict.TensorDict)

  • terminated (torch.Tensor)

Return type:

None

__init__(num_envs, capacity, min_steps_per_env=1, max_memory_fraction=None, device='cpu')[source]

Initialize the replay buffer without allocating transition tensors.

Parameters:
  • num_envs (int) – Number of environments.

  • capacity (int) – Maximum number of transitions across all environments. Rounded down to complete environment steps.

  • min_steps_per_env (int) – Minimum number of transitions per environment that must fit in the buffer.

  • max_memory_fraction (float | None) – Maximum fraction of free GPU memory available for replay allocation. None disables the memory limit. Only applies to CUDA devices.

  • device (str) – Device on which to store and sample transitions.

Return type:

None

property size: int

Return the number of stored transitions across all environments.

property write_index: torch.Tensor

Device tensor with the slot that the next write_transition() writes to.

add_transition(transition)

Copy one transition per environment into the replay buffer.

Pass a Transition or a TensorDict of per-environment fields with boolean dones flags. An optional boolean window_break flag cuts forward windows at external state edits. The first transition defines the stored fields, shapes and dtypes.

Parameters:

transition (ReplayBuffer.Transition | TensorDict)

Return type:

None

write_transition(transition)

Copy one transition into the slot at the device-side write cursor and advance that cursor.

After the first call, which allocates the storage, this method only runs device operations, so it can be captured in a CUDA graph. Each call must be followed by advance().

Parameters:

transition (ReplayBuffer.Transition | TensorDict)

Return type:

None

advance()[source]

Advance the host-side write cursor after write_transition().

Return type:

None

clear()[source]

Discard stored transitions while retaining allocated memory.

Return type:

None

latest_indices(num_steps)[source]

Return the slots of the latest num_steps transitions, oldest first.

Parameters:

num_steps (int)

Return type:

torch.Tensor

latest_observations(num_steps)[source]

Return the observations of the latest num_steps transitions, flattened over steps and environments.

Parameters:

num_steps (int)

Return type:

tensordict.TensorDict

sample(batch_size, length=1)[source]

Sample windows of consecutive transitions for independently selected environments.

Windows stop at the first episode boundary or the newest stored transition. Invalid positions repeat the last valid transition and are excluded by ReplayBuffer.Batch.valid. Sampling reads the fill level from device tensors, so it can be captured in a CUDA graph.

Parameters:
  • batch_size (int) – Number of windows to sample.

  • length (int) – Maximum number of consecutive transitions in each window.

Return type:

Batch

sample_sequence(batch_size, keys, length=1, history_length=0, complete_window_counts=None)[source]

Return selected fields and their validity over a history and forward window.

History stops at resets. Forward windows also stop at external state edits flagged by window_break. Padding repeats the nearest valid transition. Pass counts from compute_complete_window_counts() from the current replay contents to sample eligible environments and then their starts uniformly. The length used to compute the counts may include extra lookahead beyond the requested window.

Parameters:
  • batch_size (int)

  • keys (Sequence[str | tuple[str, ...]])

  • length (int)

  • history_length (int)

  • complete_window_counts (torch.Tensor | None)

Return type:

tuple[TensorDict, torch.Tensor]

compute_complete_window_counts(length)[source]

Return cumulative counts of valid starts ordered by environment and then age.

Parameters:

length (int)

Return type:

torch.Tensor