thrust-rl 0.3.0

High-performance reinforcement learning in Rust with the Burn tensor backend
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# PufferLib Buffer Implementation - Practical Guide for Thrust

## Key Insights for Your Codebase

This guide highlights specific design patterns from PufferLib that could be adopted in the Thrust project.

---

## 1. SEGMENTED BUFFER DESIGN

### The Insight
Instead of time-major layout `[time_steps, num_envs]`, PufferLib uses:
```python
# [segments, horizon] layout
segments = batch_size // horizon
buffers = {
    'observations': [segments, horizon, *obs_shape],
    'actions': [segments, horizon, *action_shape],
    'rewards': [segments, horizon],
    'values': [segments, horizon],
    'logprobs': [segments, horizon],
    'terminals': [segments, horizon],
    'truncations': [segments, horizon],
}
```

### Why This Matters
1. **Natural Episode Grouping**: Each segment is an independent trajectory
2. **Efficient Minibatching**: Sample full segments, no index juggling
3. **Better Cache Locality**: Contiguous memory for each trajectory
4. **Priority Sampling**: Easy to compute advantage sum per segment

### Implementation Pattern
```python
class RolloutBuffer:
    def __init__(self, num_segments, horizon, obs_shape, action_shape, device='cuda'):
        self.num_segments = num_segments
        self.horizon = horizon
        
        # All buffers follow [segment, time] pattern
        self.obs = torch.zeros(num_segments, horizon, *obs_shape, device=device)
        self.actions = torch.zeros(num_segments, horizon, *action_shape, device=device)
        self.rewards = torch.zeros(num_segments, horizon, device=device)
        self.values = torch.zeros(num_segments, horizon, device=device)
        self.logprobs = torch.zeros(num_segments, horizon, device=device)
        self.terminals = torch.zeros(num_segments, horizon, dtype=torch.bool, device=device)
        
        # Importance tracking for PPO
        self.policy_ratios = torch.ones(num_segments, horizon, device=device)
        
    def sample_minibatch(self, minibatch_segments):
        """Sample entire segments for minibatch"""
        indices = torch.randint(0, self.num_segments, (minibatch_segments,))
        return {
            'obs': self.obs[indices],          # [minibatch_segments, horizon, *obs_shape]
            'actions': self.actions[indices],  # [minibatch_segments, horizon, *action_shape]
            'rewards': self.rewards[indices],  # [minibatch_segments, horizon]
            'values': self.values[indices],
            'logprobs': self.logprobs[indices],
            'terminals': self.terminals[indices],
        }
```

---

## 2. VECTORIZED ADVANTAGE COMPUTATION

### The Insight
PufferLib's key innovation: Use C++/CUDA kernels instead of Python loops for GAE.

```cpp
void puff_advantage_row(float* values, float* rewards, float* dones,
                       float* importance, float* advantages,
                       float gamma, float lambda,
                       float rho_clip, float c_clip, int horizon) {
    float lastpufferlam = 0;
    
    for (int t = horizon-2; t >= 0; t--) {
        int t_next = t + 1;
        float nextnonterminal = 1.0 - dones[t_next];
        
        // V-trace clipping
        float rho_t = fminf(importance[t], rho_clip);
        float c_t = fminf(importance[t], c_clip);
        
        // TD error
        float delta = rho_t * (rewards[t_next] + gamma*values[t_next]*nextnonterminal - values[t]);
        
        // Accumulate with clipping
        lastpufferlam = delta + gamma*lambda*c_t*lastpufferlam*nextnonterminal;
        advantages[t] = lastpufferlam;
    }
}
```

### Why This Matters
- **1000x faster** than Python loop
- **GPU acceleration**: CUDA kernel for real-time computation
- **Memory efficient**: In-place advantage computation
- **V-trace stability**: Importance clipping prevents outliers

### Python Wrapper Pattern
```python
def compute_advantages(values, rewards, terminals, policy_ratios, 
                      gamma, gae_lambda, rho_clip, c_clip):
    """
    Compute advantages with V-trace stabilization.
    
    Args:
        values: [segments, horizon]
        rewards: [segments, horizon]
        terminals: [segments, horizon]
        policy_ratios: [segments, horizon] (exp(new_logprob - old_logprob))
        gamma, gae_lambda: float
        rho_clip, c_clip: float (typical: 1.0, 1.0)
    
    Returns:
        advantages: [segments, horizon]
    """
    advantages = torch.zeros_like(values)
    
    # If you implement C++ kernel:
    # torch.ops.thrust.compute_gae_advantages(...)
    
    # Otherwise, pure PyTorch version:
    batch_size, horizon = values.shape
    for segment in range(batch_size):
        lastgae = 0
        for t in reversed(range(horizon-1)):
            t_next = t + 1
            nextnonterminal = 1.0 - terminals[segment, t_next].float()
            
            # V-trace clipping
            rho_t = torch.clamp(policy_ratios[segment, t], max=rho_clip)
            c_t = torch.clamp(policy_ratios[segment, t], max=c_clip)
            
            # TD error
            delta = rho_t * (rewards[segment, t_next] + 
                           gamma * values[segment, t_next] * nextnonterminal - 
                           values[segment, t])
            
            # GAE
            lastgae = delta + gamma * gae_lambda * c_t * lastgae * nextnonterminal
            advantages[segment, t] = lastgae
    
    return advantages
```

---

## 3. IMPORTANCE WEIGHT TRACKING

### The Insight
PufferLib maintains policy ratios to enable V-trace and priority weighting:

```python
# During minibatch training:
new_logprobs = policy(observations, actions)
old_logprobs = mb_logprobs  # From buffer

# Compute importance weights
logratio = new_logprobs - old_logprobs
policy_ratios = logratio.exp()  # exp(new - old)

# Store for advantage computation
buffer.policy_ratios[idx] = policy_ratios.detach()

# Later, compute advantages with V-trace clipping
advantages = compute_advantages(
    buffer.values[idx],
    buffer.rewards[idx],
    buffer.terminals[idx],
    buffer.policy_ratios[idx],  # Use tracked ratios!
    gamma, gae_lambda,
    rho_clip=1.0, c_clip=1.0  # V-trace clipping parameters
)
```

### Why This Matters
- **V-trace Stability**: Clipping prevents overfitting to off-policy data
- **Priority Weighting**: Can sample high-advantage trajectories more
- **Convergence**: Better than standard GAE for continuous learning

---

## 4. EPISODE TRACKING FOR PARALLEL ROLLOUTS

### The Insight
PufferLib dynamically allocates segments as episodes complete:

```python
class EpisodeTracker:
    def __init__(self, num_agents, num_segments, horizon, device='cuda'):
        self.num_agents = num_agents
        self.num_segments = num_segments
        self.horizon = horizon
        
        # Track episode state for each agent
        self.ep_lengths = torch.zeros(num_agents, dtype=torch.int32, device=device)
        self.ep_indices = torch.arange(num_agents, dtype=torch.int32, device=device)
        self.free_idx = num_agents
        
    def step(self, env_ids, dones):
        """Update episode tracking after environment step"""
        # Increment length for active agents
        self.ep_lengths[env_ids] += 1
        
        # Check which agents completed an episode
        completed_mask = dones[env_ids]
        completed_agents = env_ids[completed_mask]
        
        # Mark completed episodes
        for agent in completed_agents:
            segment_idx = self.ep_indices[agent]
            # Mark segment as full in buffer
            self.ep_lengths[agent] = 0
            self.ep_indices[agent] = self.free_idx
            self.free_idx += 1
            
        return completed_agents
    
    def get_full_segments(self, horizon):
        """Get all segments that reached target horizon"""
        full_mask = self.ep_lengths >= horizon
        return torch.where(full_mask)[0]
```

### Why This Matters
- **Async Collection**: Collect at different rates without blocking
- **Memory Efficient**: Reuse segments as they complete
- **Natural Batching**: Ready-to-train batches form automatically

---

## 5. PRIORITY-WEIGHTED MINIBATCHING

### The Insight
Sample minibatches by advantage magnitude to focus on important transitions:

```python
class PrioritizedSampler:
    def __init__(self, num_segments, alpha=1.0, beta_start=0.0):
        self.num_segments = num_segments
        self.alpha = alpha  # Priority exponent
        self.beta_start = beta_start  # Importance sampling correction start
        
    def sample(self, advantages, num_samples, epoch, total_epochs):
        """
        Sample minibatches weighted by advantage magnitude.
        
        Args:
            advantages: [num_segments, horizon]
            num_samples: Number of segments to sample
            epoch, total_epochs: For annealing beta
        
        Returns:
            indices: [num_samples] segment indices
            weights: [num_samples] importance sampling weights
        """
        # Compute priority for each segment (sum across time)
        priorities = advantages.abs().sum(dim=1)  # [num_segments]
        
        # Convert to sampling probabilities
        probs = (priorities ** self.alpha) / (priorities ** self.alpha).sum()
        
        # Sample segments
        indices = torch.multinomial(probs, num_samples, replacement=True)
        
        # Importance sampling correction (annealing)
        beta = self.beta_start + (1.0 - self.beta_start) * epoch / total_epochs
        weights = (self.num_segments * probs[indices]) ** (-beta)
        weights = weights / weights.max()  # Normalize
        
        return indices, weights
    
    def apply_weights(self, advantages, weights):
        """Apply importance weights to advantages"""
        # Advantages should be normalized first
        adv_normalized = (advantages - advantages.mean()) / (advantages.std() + 1e-8)
        return weights[:, None] * adv_normalized
```

### Implementation in Training Loop
```python
sampler = PrioritizedSampler(num_segments=segments, alpha=1.0)

for epoch in range(num_epochs):
    # Compute advantages for all segments
    advantages = compute_advantages(
        buffer.values, buffer.rewards, buffer.terminals,
        buffer.policy_ratios, gamma, gae_lambda,
        rho_clip=1.0, c_clip=1.0
    )
    
    # Sample minibatches by priority
    for _ in range(num_minibatches):
        idx, weights = sampler.sample(
            advantages, minibatch_segments, epoch, num_epochs
        )
        
        # Get minibatch data
        mb_obs = buffer.obs[idx]
        mb_actions = buffer.actions[idx]
        mb_advantages = advantages[idx]
        
        # Apply importance weighting
        mb_advantages = sampler.apply_weights(mb_advantages, weights)
        
        # Training step
        loss = compute_loss(mb_obs, mb_actions, mb_advantages, ...)
        loss.backward()
```

---

## 6. MIXED CPU/GPU STORAGE

### The Insight
Store large observations on CPU, compute on GPU for memory efficiency:

```python
class HybridBuffer:
    def __init__(self, num_segments, horizon, obs_shape, action_shape,
                 obs_on_cpu=True, device='cuda'):
        self.obs_on_cpu = obs_on_cpu
        self.device = device
        
        # Observations: CPU with pinned memory for fast transfers
        if obs_on_cpu:
            self.obs = torch.zeros(
                num_segments, horizon, *obs_shape,
                dtype=torch.float32,
                pin_memory=True  # Pinned for DMA transfers
            )
        else:
            self.obs = torch.zeros(
                num_segments, horizon, *obs_shape,
                device=device
            )
        
        # Scalars: Always on GPU for training
        self.actions = torch.zeros(num_segments, horizon, *action_shape, device=device)
        self.rewards = torch.zeros(num_segments, horizon, device=device)
        self.values = torch.zeros(num_segments, horizon, device=device)
        self.logprobs = torch.zeros(num_segments, horizon, device=device)
        self.terminals = torch.zeros(num_segments, horizon, dtype=torch.bool, device=device)
        
    def get_minibatch(self, indices):
        """Get minibatch, transferring observations to GPU if needed"""
        mb_data = {
            'actions': self.actions[indices],
            'rewards': self.rewards[indices],
            'values': self.values[indices],
            'logprobs': self.logprobs[indices],
            'terminals': self.terminals[indices],
        }
        
        # Transfer observations to GPU
        if self.obs_on_cpu:
            mb_data['obs'] = self.obs[indices].to(self.device, non_blocking=True)
        else:
            mb_data['obs'] = self.obs[indices]
        
        return mb_data
```

### Why This Matters
- **Memory Efficiency**: Images (large) on CPU, scalars (small) on GPU
- **Throughput**: Pinned memory enables faster CPU-GPU transfers
- **Asynchronous**: Use `non_blocking=True` for overlap with computation

---

## 7. LSTM STATE MANAGEMENT

### The Insight
For recurrent policies, maintain hidden states indexed by segment:

```python
class LSTMBuffer:
    def __init__(self, num_agents, agents_per_batch, hidden_size, device='cuda'):
        self.agents_per_batch = agents_per_batch
        self.hidden_size = hidden_size
        self.device = device
        
        # Hidden states: one per batch of agents
        self.lstm_h = {}
        self.lstm_c = {}
        
        # Initialize for each potential batch starting position
        for i in range(0, num_agents, agents_per_batch):
            self.lstm_h[i] = torch.zeros(
                agents_per_batch, hidden_size, device=device
            )
            self.lstm_c[i] = torch.zeros(
                agents_per_batch, hidden_size, device=device
            )
    
    def get_state(self, batch_start_idx):
        """Get LSTM state for batch starting at agent index"""
        return {
            'h': self.lstm_h[batch_start_idx],
            'c': self.lstm_c[batch_start_idx],
        }
    
    def set_state(self, batch_start_idx, h, c):
        """Update LSTM state after forward pass"""
        self.lstm_h[batch_start_idx] = h
        self.lstm_c[batch_start_idx] = c
    
    def reset(self, agent_indices):
        """Reset LSTM state for completed episodes"""
        for idx in agent_indices:
            batch_start = (idx // self.agents_per_batch) * self.agents_per_batch
            self.lstm_h[batch_start][idx % self.agents_per_batch].zero_()
            self.lstm_c[batch_start][idx % self.agents_per_batch].zero_()
```

---

## 8. INTEGRATION CHECKLIST

When adapting PufferLib patterns to Thrust:

- [ ] **Buffer Layout**: Switch from `[time, env]` to `[segment, horizon]`
- [ ] **Advantage Computation**: Implement V-trace GAE (consider C++ kernel)
- [ ] **Importance Tracking**: Store policy ratios for stability
- [ ] **Episode Tracking**: Dynamic segment allocation as episodes complete
- [ ] **Priority Sampling**: Weight minibatches by advantage magnitude
- [ ] **Memory Layout**: Consider CPU observation storage for large obs
- [ ] **LSTM Support**: Index hidden states by batch if using RNNs
- [ ] **Vectorized Ops**: Use PyTorch operations, avoid Python loops in hot paths

---

## 9. PERFORMANCE EXPECTATIONS

After implementing these patterns, expect:

| Metric | Typical Improvement |
|--------|-------------------|
| Advantage Computation | 10-100x (with C++ kernel) |
| Memory Usage | 10-30% reduction (CPU offloading) |
| Sample Efficiency | 15-30% improvement (priority sampling) |
| Training Stability | Noticeably better (V-trace clipping) |
| Code Readability | Cleaner with segment-based design |

---

## 10. MINIMAL WORKING EXAMPLE

```python
import torch

class MinimalPufferBuffer:
    def __init__(self, batch_size, horizon, obs_shape, num_actions, device='cuda'):
        self.batch_size = batch_size
        self.horizon = horizon
        segments = batch_size // horizon
        
        # Pre-allocate buffers
        self.obs = torch.zeros(segments, horizon, *obs_shape, device=device)
        self.actions = torch.zeros(segments, horizon, dtype=torch.long, device=device)
        self.rewards = torch.zeros(segments, horizon, device=device)
        self.values = torch.zeros(segments, horizon, device=device)
        self.logprobs = torch.zeros(segments, horizon, device=device)
        self.terminals = torch.zeros(segments, horizon, device=device)
        self.policy_ratios = torch.ones(segments, horizon, device=device)
    
    def compute_advantages(self, gamma=0.99, gae_lambda=0.95, 
                          rho_clip=1.0, c_clip=1.0):
        """Simple advantage computation"""
        advantages = torch.zeros_like(self.rewards)
        
        for segment in range(self.obs.shape[0]):
            lastgae = 0
            for t in reversed(range(self.horizon - 1)):
                nextnonterminal = 1.0 - self.terminals[segment, t+1]
                rho = torch.clamp(self.policy_ratios[segment, t], max=rho_clip)
                c = torch.clamp(self.policy_ratios[segment, t], max=c_clip)
                
                delta = rho * (
                    self.rewards[segment, t+1] + 
                    gamma * self.values[segment, t+1] * nextnonterminal -
                    self.values[segment, t]
                )
                
                lastgae = delta + gamma * gae_lambda * c * lastgae * nextnonterminal
                advantages[segment, t] = lastgae
        
        return advantages
    
    def sample_minibatch(self, minibatch_size):
        """Random minibatch sampling"""
        indices = torch.randint(0, self.obs.shape[0], (minibatch_size,))
        return {
            'obs': self.obs[indices],
            'actions': self.actions[indices],
            'rewards': self.rewards[indices],
            'values': self.values[indices],
            'logprobs': self.logprobs[indices],
            'terminals': self.terminals[indices],
        }

# Usage
buffer = MinimalPufferBuffer(
    batch_size=128,
    horizon=32,
    obs_shape=(4, 84, 84),
    num_actions=18,
    device='cuda'
)

# Collect data (fill buffer)
# ... environment loop ...

# Compute advantages
advantages = buffer.compute_advantages()

# Sample and train
for _ in range(4):  # 4 epochs
    minibatch = buffer.sample_minibatch(minibatch_size=32)
    # ... training step ...
```