tensorlogic-train 0.1.0

Training loops, loss composition, and optimization schedules for TensorLogic
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
//! Gradient accumulation for effective large-batch training.
//!
//! Enables training with larger effective batch sizes than memory allows
//! by accumulating gradients over multiple micro-batches before applying
//! an optimizer update.

use std::collections::HashMap;
use thiserror::Error;

/// Errors that can occur during gradient accumulation.
#[derive(Debug, Error)]
pub enum AccumulationError {
    /// Gradient shape mismatch when accumulating into a buffer.
    #[error("Gradient shape mismatch for '{name}': expected {expected:?}, got {got:?}")]
    ShapeMismatch {
        /// Parameter name (may be empty for unnamed buffers).
        name: String,
        /// Expected shape of the gradient.
        expected: Vec<usize>,
        /// Actual shape of the gradient provided.
        got: Vec<usize>,
    },
    /// Attempted to retrieve gradients when none have been accumulated.
    #[error("No gradients accumulated")]
    NoGradients,
    /// The accumulator has already reached the configured number of micro-batches.
    #[error("Accumulator already full ({0} micro-batches)")]
    AccumulatorFull(usize),
}

/// Configuration for gradient accumulation.
#[derive(Debug, Clone)]
pub struct AccumulationConfig {
    /// Number of micro-batches to accumulate before triggering an update.
    pub accumulation_steps: usize,
    /// Whether to normalize (average) gradients across micro-batches.
    pub normalize: bool,
    /// Maximum gradient norm for clipping (None = no clipping).
    pub max_grad_norm: Option<f64>,
}

impl Default for AccumulationConfig {
    fn default() -> Self {
        AccumulationConfig {
            accumulation_steps: 4,
            normalize: true,
            max_grad_norm: None,
        }
    }
}

impl AccumulationConfig {
    /// Create a new config with the given number of accumulation steps.
    /// Clamps to a minimum of 1.
    pub fn new(steps: usize) -> Self {
        AccumulationConfig {
            accumulation_steps: steps.max(1),
            ..Default::default()
        }
    }

    /// Set whether to normalize (average) gradients.
    pub fn with_normalize(mut self, normalize: bool) -> Self {
        self.normalize = normalize;
        self
    }

    /// Set maximum gradient norm for clipping.
    pub fn with_max_grad_norm(mut self, norm: f64) -> Self {
        self.max_grad_norm = Some(norm);
        self
    }

    /// Compute the effective batch size given the micro-batch size.
    pub fn effective_batch_size(&self, micro_batch_size: usize) -> usize {
        micro_batch_size * self.accumulation_steps
    }
}

/// A single gradient buffer for one parameter.
#[derive(Debug, Clone)]
pub struct GradientBuffer {
    /// Accumulated gradient values (flattened).
    pub data: Vec<f64>,
    /// Shape of the gradient tensor.
    pub shape: Vec<usize>,
    /// Number of micro-batches accumulated so far.
    pub accumulated_count: usize,
}

impl GradientBuffer {
    /// Create a new gradient buffer initialized to zeros.
    pub fn new(shape: Vec<usize>) -> Self {
        let size: usize = shape.iter().product();
        GradientBuffer {
            data: vec![0.0; size],
            shape,
            accumulated_count: 0,
        }
    }

    /// Add a micro-batch gradient to the buffer.
    pub fn accumulate(&mut self, grad: &[f64]) -> Result<(), AccumulationError> {
        if grad.len() != self.data.len() {
            return Err(AccumulationError::ShapeMismatch {
                name: String::new(),
                expected: self.shape.clone(),
                got: vec![grad.len()],
            });
        }
        for (acc, &g) in self.data.iter_mut().zip(grad.iter()) {
            *acc += g;
        }
        self.accumulated_count += 1;
        Ok(())
    }

    /// Get the accumulated gradient, optionally normalized by the count.
    pub fn get(&self, normalize: bool) -> Vec<f64> {
        if normalize && self.accumulated_count > 0 {
            let scale = 1.0 / self.accumulated_count as f64;
            self.data.iter().map(|&v| v * scale).collect()
        } else {
            self.data.clone()
        }
    }

    /// Compute the L2 norm of the accumulated gradient.
    pub fn l2_norm(&self) -> f64 {
        self.data.iter().map(|v| v * v).sum::<f64>().sqrt()
    }

    /// Reset the buffer to zeros.
    pub fn reset(&mut self) {
        self.data.fill(0.0);
        self.accumulated_count = 0;
    }
}

/// Gradient accumulator managing multiple parameter gradients.
///
/// Provides micro-batching support by accumulating gradients across
/// multiple forward/backward passes before applying an optimizer step.
pub struct GradientAccumulator {
    config: AccumulationConfig,
    buffers: HashMap<String, GradientBuffer>,
    total_micro_batches: usize,
    total_updates: usize,
}

impl GradientAccumulator {
    /// Create a new gradient accumulator with the given configuration.
    pub fn new(config: AccumulationConfig) -> Self {
        GradientAccumulator {
            config,
            buffers: HashMap::new(),
            total_micro_batches: 0,
            total_updates: 0,
        }
    }

    /// Register a parameter with its gradient shape.
    ///
    /// If the parameter is already registered, this is a no-op.
    pub fn register(&mut self, name: impl Into<String>, shape: Vec<usize>) {
        let name = name.into();
        self.buffers
            .entry(name)
            .or_insert_with(|| GradientBuffer::new(shape));
    }

    /// Accumulate a gradient for a named parameter.
    ///
    /// Returns an error if the parameter has not been registered or if
    /// the gradient size does not match the registered shape.
    pub fn accumulate(&mut self, name: &str, grad: &[f64]) -> Result<(), AccumulationError> {
        if let Some(buf) = self.buffers.get_mut(name) {
            if buf.accumulated_count >= self.config.accumulation_steps {
                return Err(AccumulationError::AccumulatorFull(
                    self.config.accumulation_steps,
                ));
            }
            buf.accumulate(grad).map_err(|e| match e {
                AccumulationError::ShapeMismatch { expected, got, .. } => {
                    AccumulationError::ShapeMismatch {
                        name: name.to_string(),
                        expected,
                        got,
                    }
                }
                other => other,
            })
        } else {
            Err(AccumulationError::NoGradients)
        }
    }

    /// Check if enough micro-batches have been accumulated to trigger an update.
    pub fn should_update(&self) -> bool {
        self.buffers
            .values()
            .any(|b| b.accumulated_count >= self.config.accumulation_steps)
    }

    /// Get all accumulated gradients, optionally normalized and clipped.
    pub fn get_gradients(&self) -> Result<HashMap<String, Vec<f64>>, AccumulationError> {
        if self.buffers.is_empty() {
            return Err(AccumulationError::NoGradients);
        }
        let mut grads: HashMap<String, Vec<f64>> = self
            .buffers
            .iter()
            .map(|(name, buf)| (name.clone(), buf.get(self.config.normalize)))
            .collect();

        // Apply gradient clipping if configured
        if let Some(max_norm) = self.config.max_grad_norm {
            let total_norm: f64 = grads
                .values()
                .flat_map(|g| g.iter())
                .map(|v| v * v)
                .sum::<f64>()
                .sqrt();
            if total_norm > max_norm {
                let scale = max_norm / total_norm;
                for grad in grads.values_mut() {
                    for v in grad.iter_mut() {
                        *v *= scale;
                    }
                }
            }
        }
        Ok(grads)
    }

    /// Reset all buffers after an update step.
    pub fn reset(&mut self) {
        for buf in self.buffers.values_mut() {
            buf.reset();
        }
        self.total_updates += 1;
    }

    /// Accumulate a full micro-batch of gradients, returning `true` if an
    /// update should now be applied.
    pub fn step(
        &mut self,
        gradients: &HashMap<String, Vec<f64>>,
    ) -> Result<bool, AccumulationError> {
        for (name, grad) in gradients {
            self.accumulate(name, grad)?;
        }
        self.total_micro_batches += 1;
        Ok(self.should_update())
    }

    /// Get statistics about the accumulation state.
    pub fn stats(&self) -> AccumulationStats {
        AccumulationStats {
            total_micro_batches: self.total_micro_batches,
            total_updates: self.total_updates,
            accumulation_steps: self.config.accumulation_steps,
            registered_params: self.buffers.len(),
            total_param_elements: self.buffers.values().map(|b| b.data.len()).sum(),
        }
    }
}

/// Statistics from gradient accumulation.
#[derive(Debug, Clone)]
pub struct AccumulationStats {
    /// Total number of micro-batches processed.
    pub total_micro_batches: usize,
    /// Total number of optimizer updates applied.
    pub total_updates: usize,
    /// Configured number of accumulation steps.
    pub accumulation_steps: usize,
    /// Number of registered parameters.
    pub registered_params: usize,
    /// Total number of scalar gradient elements across all parameters.
    pub total_param_elements: usize,
}

impl AccumulationStats {
    /// The effective batch size multiplier (same as accumulation_steps).
    pub fn effective_batch_multiplier(&self) -> usize {
        self.accumulation_steps
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_default() {
        let config = AccumulationConfig::default();
        assert_eq!(config.accumulation_steps, 4);
        assert!(config.normalize);
        assert!(config.max_grad_norm.is_none());
    }

    #[test]
    fn test_config_effective_batch_size() {
        let config = AccumulationConfig::new(4);
        assert_eq!(config.effective_batch_size(32), 128);
    }

    #[test]
    fn test_buffer_new() {
        let buf = GradientBuffer::new(vec![3, 4]);
        assert_eq!(buf.data.len(), 12);
        assert!(buf.data.iter().all(|&v| v == 0.0));
        assert_eq!(buf.accumulated_count, 0);
    }

    #[test]
    fn test_buffer_accumulate() {
        let mut buf = GradientBuffer::new(vec![3]);
        let grad = vec![1.0, 2.0, 3.0];
        buf.accumulate(&grad).expect("accumulate should succeed");
        assert_eq!(buf.data, vec![1.0, 2.0, 3.0]);
        assert_eq!(buf.accumulated_count, 1);

        buf.accumulate(&grad)
            .expect("second accumulate should succeed");
        assert_eq!(buf.data, vec![2.0, 4.0, 6.0]);
        assert_eq!(buf.accumulated_count, 2);
    }

    #[test]
    fn test_buffer_accumulate_shape_mismatch() {
        let mut buf = GradientBuffer::new(vec![3]);
        let grad = vec![1.0, 2.0];
        let result = buf.accumulate(&grad);
        assert!(result.is_err());
        match result {
            Err(AccumulationError::ShapeMismatch { .. }) => {}
            _ => panic!("expected ShapeMismatch error"),
        }
    }

    #[test]
    fn test_buffer_get_normalized() {
        let mut buf = GradientBuffer::new(vec![2]);
        buf.accumulate(&[2.0, 4.0]).expect("accumulate");
        buf.accumulate(&[6.0, 8.0]).expect("accumulate");
        let normalized = buf.get(true);
        assert_eq!(normalized, vec![4.0, 6.0]); // (2+6)/2=4, (4+8)/2=6
    }

    #[test]
    fn test_buffer_get_unnormalized() {
        let mut buf = GradientBuffer::new(vec![2]);
        buf.accumulate(&[2.0, 4.0]).expect("accumulate");
        buf.accumulate(&[6.0, 8.0]).expect("accumulate");
        let raw = buf.get(false);
        assert_eq!(raw, vec![8.0, 12.0]); // 2+6=8, 4+8=12
    }

    #[test]
    fn test_buffer_l2_norm() {
        let mut buf = GradientBuffer::new(vec![2]);
        buf.accumulate(&[3.0, 4.0]).expect("accumulate");
        let norm = buf.l2_norm();
        assert!((norm - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_buffer_reset() {
        let mut buf = GradientBuffer::new(vec![3]);
        buf.accumulate(&[1.0, 2.0, 3.0]).expect("accumulate");
        assert_eq!(buf.accumulated_count, 1);
        buf.reset();
        assert!(buf.data.iter().all(|&v| v == 0.0));
        assert_eq!(buf.accumulated_count, 0);
    }

    #[test]
    fn test_accumulator_register() {
        let mut acc = GradientAccumulator::new(AccumulationConfig::default());
        acc.register("weight", vec![3, 4]);
        assert_eq!(acc.buffers.len(), 1);
        assert!(acc.buffers.contains_key("weight"));
    }

    #[test]
    fn test_accumulator_accumulate() {
        let mut acc = GradientAccumulator::new(AccumulationConfig::default());
        acc.register("w", vec![2]);
        acc.accumulate("w", &[1.0, 2.0])
            .expect("accumulate should succeed");
        let buf = acc.buffers.get("w").expect("buffer should exist");
        assert_eq!(buf.data, vec![1.0, 2.0]);
    }

    #[test]
    fn test_accumulator_should_update() {
        let config = AccumulationConfig::new(2);
        let mut acc = GradientAccumulator::new(config);
        acc.register("w", vec![2]);
        assert!(!acc.should_update());
        acc.accumulate("w", &[1.0, 1.0]).expect("accumulate");
        assert!(!acc.should_update());
        acc.accumulate("w", &[1.0, 1.0]).expect("accumulate");
        assert!(acc.should_update());
    }

    #[test]
    fn test_accumulator_get_gradients() {
        let config = AccumulationConfig::new(2).with_normalize(true);
        let mut acc = GradientAccumulator::new(config);
        acc.register("w", vec![2]);
        acc.accumulate("w", &[2.0, 4.0]).expect("accumulate");
        acc.accumulate("w", &[6.0, 8.0]).expect("accumulate");
        let grads = acc.get_gradients().expect("get_gradients");
        let w_grad = grads.get("w").expect("w gradient");
        assert_eq!(w_grad, &vec![4.0, 6.0]);
    }

    #[test]
    fn test_accumulator_grad_clipping() {
        let config = AccumulationConfig::new(1)
            .with_normalize(false)
            .with_max_grad_norm(5.0);
        let mut acc = GradientAccumulator::new(config);
        acc.register("w", vec![2]);
        // gradient [30, 40] has norm 50, clip to 5 => scale by 5/50 = 0.1
        acc.accumulate("w", &[30.0, 40.0]).expect("accumulate");
        let grads = acc.get_gradients().expect("get_gradients");
        let w_grad = grads.get("w").expect("w gradient");
        assert!((w_grad[0] - 3.0).abs() < 1e-10);
        assert!((w_grad[1] - 4.0).abs() < 1e-10);
    }

    #[test]
    fn test_accumulator_reset() {
        let config = AccumulationConfig::new(2);
        let mut acc = GradientAccumulator::new(config);
        acc.register("w", vec![2]);
        acc.accumulate("w", &[1.0, 2.0]).expect("accumulate");
        acc.reset();
        let buf = acc.buffers.get("w").expect("buffer");
        assert!(buf.data.iter().all(|&v| v == 0.0));
        assert_eq!(buf.accumulated_count, 0);
        assert_eq!(acc.total_updates, 1);
    }

    #[test]
    fn test_accumulator_step() {
        let config = AccumulationConfig::new(2);
        let mut acc = GradientAccumulator::new(config);
        acc.register("w", vec![2]);
        let mut grads = HashMap::new();
        grads.insert("w".to_string(), vec![1.0, 1.0]);

        let should = acc.step(&grads).expect("step 1");
        assert!(!should);
        let should = acc.step(&grads).expect("step 2");
        assert!(should);
    }

    #[test]
    fn test_accumulator_stats() {
        let config = AccumulationConfig::new(3);
        let mut acc = GradientAccumulator::new(config);
        acc.register("a", vec![2, 3]);
        acc.register("b", vec![4]);

        let stats = acc.stats();
        assert_eq!(stats.total_micro_batches, 0);
        assert_eq!(stats.total_updates, 0);
        assert_eq!(stats.accumulation_steps, 3);
        assert_eq!(stats.registered_params, 2);
        assert_eq!(stats.total_param_elements, 10); // 6 + 4
        assert_eq!(stats.effective_batch_multiplier(), 3);
    }

    #[test]
    fn test_accumulator_empty_no_gradients() {
        let acc = GradientAccumulator::new(AccumulationConfig::default());
        let result = acc.get_gradients();
        assert!(result.is_err());
        match result {
            Err(AccumulationError::NoGradients) => {}
            _ => panic!("expected NoGradients error"),
        }
    }
}