pub struct RolloutBuffer { /* private fields */ }Expand description
Rollout buffer for storing trajectories
Stores trajectories collected from environment interactions and computes advantages using Generalized Advantage Estimation (GAE).
§Buffer Layout
The buffer uses a [num_steps, num_envs] layout where:
num_steps: Number of timesteps per rollout (typically 128-2048)num_envs: Number of parallel environments
This layout provides good cache locality for forward passes and efficient computation of advantages.
Implementations§
Source§impl RolloutBuffer
impl RolloutBuffer
Sourcepub fn new(num_steps: usize, num_envs: usize, obs_dim: usize) -> Self
pub fn new(num_steps: usize, num_envs: usize, obs_dim: usize) -> Self
Create a new rollout buffer
§Arguments
num_steps- Number of timesteps per rolloutnum_envs- Number of parallel environmentsobs_dim- Dimensionality of observations
Sourcepub fn add(
&mut self,
step: usize,
env_id: usize,
observation: &[f32],
action: i64,
reward: f32,
value: f32,
log_prob: f32,
terminated: bool,
truncated: bool,
)
pub fn add( &mut self, step: usize, env_id: usize, observation: &[f32], action: i64, reward: f32, value: f32, log_prob: f32, terminated: bool, truncated: bool, )
Add a transition to the buffer
§Arguments
step- Timestep within the rollout (0 to num_steps-1)env_id- Environment ID (0 to num_envs-1)observation- Current observationaction- Action takenreward- Reward receivedvalue- Value estimate for current statelog_prob- Log probability of the actionterminated- Whether the episode terminatedtruncated- Whether the episode was truncated
Sourcepub fn obs_shape(&self) -> (usize, usize)
pub fn obs_shape(&self) -> (usize, usize)
Get observations tensor shape for neural network input
Sourcepub fn observations(&self) -> &[Vec<Vec<f32>>]
pub fn observations(&self) -> &[Vec<Vec<f32>>]
Borrow the per-step observations, indexed [step][env] and
then by observation dimension (final inner Vec<f32> is one
observation vector of length obs_dim).
Sourcepub fn actions(&self) -> &[Vec<i64>]
pub fn actions(&self) -> &[Vec<i64>]
Borrow the per-step discrete actions, indexed [step][env].
Sourcepub fn rewards(&self) -> &[Vec<f32>]
pub fn rewards(&self) -> &[Vec<f32>]
Borrow the per-step rewards (raw, un-discounted), indexed
[step][env].
Sourcepub fn values(&self) -> &[Vec<f32>]
pub fn values(&self) -> &[Vec<f32>]
Borrow the per-step bootstrap value estimates V(s_t) produced
by the policy at collection time, indexed [step][env]. Used by
compute_advantages as the value
baseline.
Sourcepub fn log_probs(&self) -> &[Vec<f32>]
pub fn log_probs(&self) -> &[Vec<f32>]
Borrow the per-step action log-probabilities under the
behavior policy at collection time, indexed [step][env]. PPO
uses these as the denominator of the importance-sampling ratio.
Sourcepub fn terminated(&self) -> &[Vec<bool>]
pub fn terminated(&self) -> &[Vec<bool>]
Borrow the per-step terminal-state flags (true on episode end),
indexed [step][env]. GAE zeroes the bootstrap across terminal
transitions but keeps the realized reward.
Sourcepub fn truncated(&self) -> &[Vec<bool>]
pub fn truncated(&self) -> &[Vec<bool>]
Borrow the per-step truncation flags (true on time-limit / external
reset that is not a terminal state), indexed [step][env]. GAE
retains the bootstrap value across truncated transitions because the
trajectory is still “alive” in the value-function sense.
Sourcepub fn advantages(&self) -> &[Vec<f32>]
pub fn advantages(&self) -> &[Vec<f32>]
Borrow the per-step GAE advantages computed by
compute_advantages, indexed
[step][env]. Zero-initialized until GAE has run.
Sourcepub fn returns(&self) -> &[Vec<f32>]
pub fn returns(&self) -> &[Vec<f32>]
Borrow the per-step value-function targets (advantages + values),
indexed [step][env]. Zero-initialized until GAE has run.
Sourcepub fn advantages_mut(&mut self) -> &mut [Vec<f32>]
pub fn advantages_mut(&mut self) -> &mut [Vec<f32>]
Mutable view of the advantages buffer, indexed [step][env].
Used when in-place normalizing advantages or applying a custom
advantage estimator that does not touch returns; use
Self::advantages_and_returns_mut when both must be borrowed
at the same time (e.g. inside
compute_advantages).
Sourcepub fn returns_mut(&mut self) -> &mut [Vec<f32>]
pub fn returns_mut(&mut self) -> &mut [Vec<f32>]
Mutable view of the returns / value-target buffer, indexed
[step][env]. Use Self::advantages_and_returns_mut instead
when both must be borrowed at the same time.
Source§impl RolloutBuffer
impl RolloutBuffer
Sourcepub fn compute_advantages(
&mut self,
last_values: &[f32],
gamma: f32,
gae_lambda: f32,
)
pub fn compute_advantages( &mut self, last_values: &[f32], gamma: f32, gae_lambda: f32, )
Compute advantages using Generalized Advantage Estimation
This is a convenience method that calls the module-level function.
Iterates the full [num_steps, num_envs] capacity; use
Self::compute_advantages_partial when the buffer is only
partially filled.
§Arguments
last_values- Value estimates for the final states[num_envs]gamma- Discount factorgae_lambda- GAE lambda parameter
Sourcepub fn compute_advantages_partial(
&mut self,
valid_steps: usize,
last_values: &[f32],
gamma: f32,
gae_lambda: f32,
)
pub fn compute_advantages_partial( &mut self, valid_steps: usize, last_values: &[f32], gamma: f32, gae_lambda: f32, )
Compute advantages over the first valid_steps rows only.
Convenience wrapper around the module-level
compute_advantages_partial. Use this when the rollout buffer
has been partially filled (valid_steps < num_steps) to prevent
zero-padded tail rows from contaminating GAE on the real prefix.
§Arguments
valid_steps- Number of filled rows at the start of the bufferlast_values- BootstrapV(s_{T+1})for the state after rowvalid_steps - 1, per environment[num_envs]gamma- Discount factorgae_lambda- GAE lambda parameter
Sourcepub fn compute_vtrace_advantages(
&mut self,
target_log_probs: &[Vec<f32>],
last_values: &[f32],
gamma: f32,
rho_bar: f32,
c_bar: f32,
)
pub fn compute_vtrace_advantages( &mut self, target_log_probs: &[Vec<f32>], last_values: &[f32], gamma: f32, rho_bar: f32, c_bar: f32, )
Compute V-trace targets and advantages (Espeholt et al. 2018) for off-policy correction.
Convenience wrapper around the module-level
compute_vtrace_advantages. The buffer’s stored log_probs are
treated as the behavior-policy log-probs; target_log_probs
([num_steps][num_envs]) are the current target policy’s log-probs
reevaluated over the stored observations/actions. Results are
written into advantages/returns exactly like
Self::compute_advantages, so get_batch() works as usual.
§Arguments
target_log_probs- Target-policy log-probs[num_steps][num_envs]last_values- BootstrapV(s_{T+1})per environment[num_envs]gamma- Discount factorrho_bar- IS ratio clip for the TD target (typically 1.0)c_bar- IS ratio clip for the trace coefficient (typically 1.0)
Sourcepub fn get_batch(&self) -> RolloutBatch
pub fn get_batch(&self) -> RolloutBatch
Get a batch of all data from the buffer
This is a convenience method that calls the module-level function.
Returns the full [num_steps, num_envs] capacity, including any
zero-padded unwritten tail; use Self::get_filled_batch when
the buffer is only partially filled.
Sourcepub fn get_filled_batch(&self, valid_steps: usize) -> RolloutBatch
pub fn get_filled_batch(&self, valid_steps: usize) -> RolloutBatch
Get a batch covering only the first valid_steps rows.
Use this when the buffer was filled with fewer than num_steps
transitions (e.g. an early-terminating rollout). It prevents the
zero-initialized tail of the buffer from being handed to PPO as
fake training data.
§Panics
Panics if valid_steps > self.shape().0.
Trait Implementations§
Source§impl Clone for RolloutBuffer
impl Clone for RolloutBuffer
Source§fn clone(&self) -> RolloutBuffer
fn clone(&self) -> RolloutBuffer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for RolloutBuffer
impl RefUnwindSafe for RolloutBuffer
impl Send for RolloutBuffer
impl Sync for RolloutBuffer
impl Unpin for RolloutBuffer
impl UnsafeUnpin for RolloutBuffer
impl UnwindSafe for RolloutBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more