pulsatrix
Loading...
Searching...
No Matches
replay_buffer.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8#include <vector>
9
11#include "pulsatrix/tensor.hpp"
12
13namespace pulsatrix {
14
37
70public:
81 ReplayBuffer(int64_t capacity, int64_t observation_dim, int64_t action_dim, DeviceBackend* backend,
82 uint32_t seed = 42);
83
98 void add(const Tensor& observation, const Tensor& action, float reward, const Tensor& next_observation,
99 bool done);
100
118 [[nodiscard]] ReplayBatch sample(int64_t batch_size);
119
121 [[nodiscard]] int64_t size() const { return size_; }
122
124 [[nodiscard]] int64_t capacity() const { return capacity_; }
125
127 [[nodiscard]] int64_t observation_dim() const { return observation_dim_; }
128
130 [[nodiscard]] int64_t action_dim() const { return action_dim_; }
131
132private:
134 [[nodiscard]] int64_t next_index(int64_t bound);
135
136 int64_t capacity_;
137 int64_t observation_dim_;
138 int64_t action_dim_;
139 DeviceBackend* backend_;
140 uint32_t lcg_state_;
141
142 int64_t size_ = 0;
143 int64_t write_index_ = 0;
144
145 // Host-side (capacity, width) row-major storage -- see the class's host-boundary note.
146 std::vector<float> observations_;
147 std::vector<float> actions_;
148 std::vector<float> rewards_;
149 std::vector<float> next_observations_;
150 std::vector<float> dones_;
151};
152
153} // namespace pulsatrix
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:219
Fixed-capacity circular replay buffer of (observation, action, reward, next_observation,...
Definition replay_buffer.hpp:69
int64_t capacity() const
Maximum transitions retained before the oldest starts being overwritten.
Definition replay_buffer.hpp:124
int64_t size() const
Transitions currently stored – rises to capacity(), then stays there.
Definition replay_buffer.hpp:121
int64_t observation_dim() const
Width of an observation row, as passed to the constructor.
Definition replay_buffer.hpp:127
ReplayBuffer(int64_t capacity, int64_t observation_dim, int64_t action_dim, DeviceBackend *backend, uint32_t seed=42)
Constructs an empty buffer with all storage pre-allocated and zero-filled.
ReplayBatch sample(int64_t batch_size)
Draws batch_size transitions uniformly at random, with replacement.
int64_t action_dim() const
Width of an action row, as passed to the constructor.
Definition replay_buffer.hpp:130
void add(const Tensor &observation, const Tensor &action, float reward, const Tensor &next_observation, bool done)
Stores one transition at the current circular write index, overwriting the oldest transition once the...
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Abstract interface isolating vendor-specific memory/compute operations from Tensor/ComputationGraph.
Definition acquisition_functions.hpp:16
One uniformly-sampled minibatch of transitions, one Tensor per transition field.
Definition replay_buffer.hpp:30
Tensor actions
Definition replay_buffer.hpp:32
Tensor next_observations
Definition replay_buffer.hpp:34
Tensor rewards
Definition replay_buffer.hpp:33
Tensor observations
Definition replay_buffer.hpp:31
Tensor dones
Definition replay_buffer.hpp:35
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).