RolloutBuffer
RolloutBuffer(rollout_length: int, num_envs: int, observation_shape: tuple[int, ...], observation_dtype: np.dtype) -> NoneFixed-size rollout buffer for PPO.
Pre-allocates numpy arrays for efficient storage. After a rollout is complete, computes GAE advantages and provides minibatch iteration for PPO updates.
Initialize the RolloutBuffer.
Parameters:
rollout_length(int) – Number of steps per environment per rollout.num_envs(int) – Number of parallel environments.observation_shape(tuple[int, ...]) – Shape of a single observation.observation_dtype(dtype) – Dtype of observations.
Properties
is_full
is_full: boolCheck if the rollout buffer is full.
memory_bytes
memory_bytes: intGet the total pre-allocated CPU memory of this buffer in bytes.
Returns:
int– Memory usage in bytes.
memory_gb
memory_gb: floatGet the total pre-allocated CPU memory of this buffer in GiB.
Returns:
float– Memory usage in GiB.
num_envs
num_envs: intGet the number of environments.
observation_dtype
observation_dtype: np.dtypeGet the dtype of observations stored in the rollout buffer.
observation_shape
observation_shape: tuple[int, ...]Get the shape of observations stored in the rollout buffer.
rollout_length
rollout_length: intGet the rollout length.
step
step: intGet the current step index within the rollout.
total_size
total_size: intGet the total number of transitions (rollout_length * num_envs).
trainable_size
trainable_size: intGet the number of rollout steps available to PPO updates.
Methods
add
add(observations: np.ndarray, actions: np.ndarray, log_probs: np.ndarray, values: np.ndarray, rewards: np.ndarray, dones: np.ndarray, trainable: np.ndarray | None = None) -> NoneAdd a step of data from all environments.
Parameters:
observations(ndarray) – Observations with shape (num_envs, *obs_shape).actions(ndarray) – Actions with shape (num_envs,).log_probs(ndarray) – Log probabilities with shape (num_envs,).values(ndarray) – Value estimates with shape (num_envs,).rewards(ndarray) – Rewards with shape (num_envs,).dones(ndarray) – Done flags with shape (num_envs,).trainable(ndarray | None) – Optional mask indicating which transitions should be used for PPO learning, for example random-controlled steps are stored for bookkeeping but marked non-trainable so PPO stays on-policy.
compute_gae
compute_gae(last_values: np.ndarray, last_dones: np.ndarray, gamma: float, gae_lambda: float) -> NoneCompute Generalized Advantage Estimation (GAE).
Delegates to GAE for the actual computation.
Parameters:
last_values(ndarray) – Value estimates for the state after the last step, shape (num_envs,).last_dones(ndarray) – Done flags for the state after the last step, shape (num_envs,).gamma(float) – Discount factor.gae_lambda(float) – GAE lambda parameter.
estimate_memory_pressure
estimate_memory_pressure(num_minibatches: int) -> list[RolloutBufferMemoryDiagnostic]Estimate CPU and GPU memory pressure for this rollout buffer.
Parameters:
num_minibatches(int) – Number of PPO minibatches per learner update.
Returns:
list[RolloutBufferMemoryDiagnostic]– Diagnostics describing projected memory pressure.
Raises:
ValueError– Ifnum_minibatchesis not positive.
get_minibatches
get_minibatches(num_minibatches: int, device: torch.device) -> list[RolloutMinibatch]Create shuffled minibatches from the rollout data.
Observations are uploaded to the device once per rollout (cached until the buffer changes) and minibatch rows are gathered on-device, so repeated epochs cost no further observation transfers. Scalar tensors (actions, log_probs, values, advantages, returns) are gathered to their valid rows on the CPU and uploaded whole once per call; minibatch slices are then indexed on-device.
Parameters:
num_minibatches(int) – Number of minibatches to split the data into.device(device) – The device to move tensors to.
Returns:
list[RolloutMinibatch]– A list of RolloutMinibatch instances.
reset
reset() -> NoneReset the buffer for a new rollout.
trainable_diagnostics
trainable_diagnostics() -> RolloutBufferTrainableDiagnostics | NoneReturn flattened trainable rollout arrays for learner diagnostics.
Returns:
RolloutBufferTrainableDiagnostics | None– Flattened trainable values, returns, and advantages, orNonewhen no trainable rollout items are currently available.