pub struct ReplayBuffer { /* private fields */ }Expand description
Circular fixed-capacity owner for off-policy transitions.
Batched append and deterministic uniform sampling are recorded on the one owning runtime. Cursor and size are host control metadata and never require a Matrix readback.
Implementations§
Source§impl ReplayBuffer
impl ReplayBuffer
Sourcepub fn new(runtime: &Runtime, config: ReplayConfig) -> Result<Self>
pub fn new(runtime: &Runtime, config: ReplayConfig) -> Result<Self>
Allocate empty circular replay storage on runtime.
§Errors
Returns an error for zero capacity, non-F32/I32 actions, invalid field ranks or extents, GPU-index overflow, or allocation failure.
Sourcepub fn append(&self, transition: &ReplayTransition) -> Result<()>
pub fn append(&self, transition: &ReplayTransition) -> Result<()>
Record one batched circular append.
§Errors
Returns an error unless the batch is nonempty, no larger than capacity, all fields match the configured shape/dtype/runtime contract, source and retained storage do not alias, and GPU recording succeeds.
Sourcepub fn sample(&self, batch_size: usize, seed: u64) -> Result<ReplayBatch>
pub fn sample(&self, batch_size: usize, seed: u64) -> Result<ReplayBatch>
Record deterministic uniform sampling with replacement.
The returned U32 index vector identifies every sampled physical replay slot exactly.
§Errors
Returns an error while storage is empty, for a zero sample count, on GPU indexing/allocation failure, or when recording fails.
Sourcepub fn reset(&self)
pub fn reset(&self)
Forget all retained transitions without recording device work.
Existing bytes remain inaccessible until overwritten by later appends.
Sourcepub const fn config(&self) -> &ReplayConfig
pub const fn config(&self) -> &ReplayConfig
Return the validated replay configuration.
Sourcepub fn capture_state(&self) -> Result<ReplayBufferState>
pub fn capture_state(&self) -> Result<ReplayBufferState>
Capture the complete active replay contents, size, and circular write cursor into a host-owned snapshot.
This is an explicit host-observation boundary: pending GPU appends on
the owning runtime are flushed before reading the active 0..len()
slots.
§Errors
Returns an error when any storage readback fails or element counts overflow host indexing.
Sourcepub fn restore_state(&self, state: &ReplayBufferState) -> Result<()>
pub fn restore_state(&self, state: &ReplayBufferState) -> Result<()>
Restore a captured replay snapshot into this buffer fail-closed.
Validates schema equality, circular-cursor invariants, and payload
lengths before uploading active transitions into physical slots 0..len
and flushing the owning runtime.
§Errors
Returns an error if state has a mismatched configuration or malformed
fields, or if device upload or synchronization fails.
Sourcepub fn save(&self, path: impl AsRef<Path>) -> Result<()>
pub fn save(&self, path: impl AsRef<Path>) -> Result<()>
Atomically persist the active replay contents, size, and cursor to path.
§Errors
Returns an error when readback, encoding, temporary write, flush, or rename fails.
Sourcepub fn load_into(&self, path: impl AsRef<Path>) -> Result<()>
pub fn load_into(&self, path: impl AsRef<Path>) -> Result<()>
Load a persisted replay file from path into this buffer fail-closed.
§Errors
Returns crate::ErrorKind::CheckpointCorrupt for malformed, truncated,
or checksum-invalid files, or another error on schema mismatch or device
restore failure.