optirs-learned 0.3.2

OptiRS learned optimizers and meta-learning
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Online MAML for continuous task stream meta-learning.
//!
//! This module implements Online Model-Agnostic Meta-Learning (MAML) that
//! operates on a continuous stream of tasks, maintaining a task buffer with
//! staleness-weighted meta-updates. Unlike batch MAML which requires all tasks
//! upfront, Online MAML processes tasks as they arrive and applies exponential
//! decay to older tasks to prioritize recent experience.

use crate::error::{OptimError, Result};
use scirs2_core::ndarray::{Array1, ScalarOperand, Zip};
use scirs2_core::numeric::Float;
use std::collections::VecDeque;
use std::fmt::Debug;

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Configuration for Online MAML.
///
/// Controls the task buffer size, staleness decay, batch size for meta-updates,
/// and the inner/outer learning rates for bi-level optimization.
#[derive(Debug, Clone)]
pub struct OnlineMAMLConfig<T: Float + Debug + Send + Sync + 'static> {
    /// Maximum number of tasks kept in the rolling buffer.
    pub buffer_size: usize,
    /// Exponential decay factor applied to older tasks (0 < decay <= 1).
    pub staleness_decay: T,
    /// Number of tasks sampled per meta-update.
    pub online_batch_size: usize,
    /// Learning rate for the inner (task-level) adaptation loop.
    pub inner_lr: T,
    /// Learning rate for the outer (meta) parameter update.
    pub outer_lr: T,
    /// Number of gradient-descent steps in the inner loop.
    pub inner_steps: usize,
}

impl<T: Float + Debug + Send + Sync + 'static> Default for OnlineMAMLConfig<T> {
    fn default() -> Self {
        Self {
            buffer_size: 10,
            staleness_decay: T::from(0.95).unwrap_or_else(|| T::one()),
            online_batch_size: 5,
            inner_lr: T::from(0.01).unwrap_or_else(|| T::one()),
            outer_lr: T::from(0.001).unwrap_or_else(|| T::one()),
            inner_steps: 5,
        }
    }
}

impl<T: Float + Debug + Send + Sync + 'static> OnlineMAMLConfig<T> {
    /// Set the buffer size.
    pub fn buffer_size(mut self, size: usize) -> Self {
        self.buffer_size = size;
        self
    }

    /// Set the staleness decay factor.
    pub fn staleness_decay(mut self, decay: T) -> Self {
        self.staleness_decay = decay;
        self
    }

    /// Set the online batch size.
    pub fn online_batch_size(mut self, size: usize) -> Self {
        self.online_batch_size = size;
        self
    }

    /// Set the inner learning rate.
    pub fn inner_lr(mut self, lr: T) -> Self {
        self.inner_lr = lr;
        self
    }

    /// Set the outer learning rate.
    pub fn outer_lr(mut self, lr: T) -> Self {
        self.outer_lr = lr;
        self
    }

    /// Set the number of inner-loop steps.
    pub fn inner_steps(mut self, steps: usize) -> Self {
        self.inner_steps = steps;
        self
    }
}

// ---------------------------------------------------------------------------
// Task representation
// ---------------------------------------------------------------------------

/// A task received in the online stream.
///
/// Each task carries its own parameter snapshot, accumulated gradients, a
/// timestamp indicating when it was received, and the loss achieved.
#[derive(Debug, Clone)]
pub struct OnlineTask<T: Float + Debug + Send + Sync + 'static> {
    /// Unique identifier for this task.
    pub task_id: String,
    /// Parameter snapshot associated with the task.
    pub parameters: Array1<T>,
    /// Gradients collected during the task (one per inner step or evaluation).
    pub gradients: Vec<Array1<T>>,
    /// Logical timestamp (monotonically increasing counter).
    pub timestamp: usize,
    /// Loss value observed for this task.
    pub loss: T,
}

// ---------------------------------------------------------------------------
// Adaptation record
// ---------------------------------------------------------------------------

/// Record of a single task adaptation episode.
#[derive(Debug, Clone)]
pub struct AdaptationRecord<T: Float + Debug + Send + Sync + 'static> {
    /// Task that was adapted to.
    pub task_id: String,
    /// Loss before inner-loop adaptation.
    pub pre_adaptation_loss: T,
    /// Loss after inner-loop adaptation.
    pub post_adaptation_loss: T,
    /// Number of inner-loop steps executed.
    pub num_steps: usize,
}

// ---------------------------------------------------------------------------
// Builder
// ---------------------------------------------------------------------------

/// Builder for constructing an [`OnlineMAML`] instance with a fluent API.
#[derive(Debug)]
pub struct OnlineMAMLBuilder<T: Float + Debug + Send + Sync + 'static> {
    config: OnlineMAMLConfig<T>,
    initial_params: Array1<T>,
}

impl<T: Float + Debug + Send + Sync + 'static + ScalarOperand> OnlineMAMLBuilder<T> {
    /// Create a new builder with the given initial meta-parameters.
    pub fn new(initial_params: Array1<T>) -> Self {
        Self {
            config: OnlineMAMLConfig::default(),
            initial_params,
        }
    }

    /// Set the buffer size.
    pub fn buffer_size(mut self, size: usize) -> Self {
        self.config.buffer_size = size;
        self
    }

    /// Set the staleness decay.
    pub fn staleness_decay(mut self, decay: T) -> Self {
        self.config.staleness_decay = decay;
        self
    }

    /// Set the online batch size.
    pub fn online_batch_size(mut self, size: usize) -> Self {
        self.config.online_batch_size = size;
        self
    }

    /// Set the inner learning rate.
    pub fn inner_lr(mut self, lr: T) -> Self {
        self.config.inner_lr = lr;
        self
    }

    /// Set the outer learning rate.
    pub fn outer_lr(mut self, lr: T) -> Self {
        self.config.outer_lr = lr;
        self
    }

    /// Set the number of inner steps.
    pub fn inner_steps(mut self, steps: usize) -> Self {
        self.config.inner_steps = steps;
        self
    }

    /// Build the [`OnlineMAML`] instance.
    pub fn build(self) -> OnlineMAML<T> {
        OnlineMAML {
            config: self.config,
            task_buffer: VecDeque::new(),
            meta_parameters: self.initial_params,
            step_count: 0,
            adaptation_history: Vec::new(),
        }
    }
}

// ---------------------------------------------------------------------------
// Core Online MAML
// ---------------------------------------------------------------------------

/// Online Model-Agnostic Meta-Learning.
///
/// Maintains a rolling buffer of recent tasks and performs staleness-weighted
/// meta-updates. As new tasks arrive they are added to the buffer; old tasks
/// are evicted when capacity is exceeded. Meta-updates sample a batch from
/// the buffer, run inner-loop adaptation for each task, then compute an
/// outer (meta) gradient weighted by how recent each task is.
#[derive(Debug)]
pub struct OnlineMAML<T: Float + Debug + Send + Sync + 'static> {
    /// Configuration governing buffer size, learning rates, etc.
    config: OnlineMAMLConfig<T>,
    /// Rolling buffer of recently received tasks.
    task_buffer: VecDeque<OnlineTask<T>>,
    /// Current meta-parameters (shared initialisation).
    meta_parameters: Array1<T>,
    /// Number of meta-update steps performed so far.
    step_count: usize,
    /// History of adaptation episodes for analytics.
    adaptation_history: Vec<AdaptationRecord<T>>,
}

impl<T: Float + Debug + Send + Sync + 'static + ScalarOperand> OnlineMAML<T> {
    /// Create a new `OnlineMAML` with the given config and initial parameters.
    pub fn new(config: OnlineMAMLConfig<T>, initial_params: Array1<T>) -> Self {
        Self {
            config,
            task_buffer: VecDeque::new(),
            meta_parameters: initial_params,
            step_count: 0,
            adaptation_history: Vec::new(),
        }
    }

    /// Return a builder initialised with the given meta-parameters.
    pub fn builder(initial_params: Array1<T>) -> OnlineMAMLBuilder<T> {
        OnlineMAMLBuilder::new(initial_params)
    }

    /// Get the current meta-parameters.
    pub fn meta_parameters(&self) -> &Array1<T> {
        &self.meta_parameters
    }

    /// Get the number of tasks currently in the buffer.
    pub fn buffer_len(&self) -> usize {
        self.task_buffer.len()
    }

    /// Get the number of meta-update steps executed so far.
    pub fn step_count(&self) -> usize {
        self.step_count
    }

    /// Get the adaptation history.
    pub fn adaptation_history(&self) -> &[AdaptationRecord<T>] {
        &self.adaptation_history
    }

    // -----------------------------------------------------------------
    // Task management
    // -----------------------------------------------------------------

    /// Receive a new task into the buffer.
    ///
    /// If the buffer is at capacity the oldest task is evicted first.
    pub fn receive_task(&mut self, task: OnlineTask<T>) {
        if self.task_buffer.len() >= self.config.buffer_size {
            self.task_buffer.pop_front();
        }
        self.task_buffer.push_back(task);
    }

    // -----------------------------------------------------------------
    // Meta-update
    // -----------------------------------------------------------------

    /// Perform one online meta-update using the buffered tasks.
    ///
    /// # Algorithm
    /// 1. Select up to `online_batch_size` most-recent tasks from the buffer.
    /// 2. For each selected task run an inner-loop adaptation starting from the
    ///    current `meta_parameters`.
    /// 3. Compute the outer gradient as the staleness-weighted mean of the
    ///    difference between the meta-parameters and the adapted parameters.
    /// 4. Update `meta_parameters` with the outer gradient.
    ///
    /// # Errors
    /// Returns `OptimError::InsufficientData` if the buffer is empty.
    pub fn online_update(&mut self) -> Result<Array1<T>> {
        if self.task_buffer.is_empty() {
            return Err(OptimError::InsufficientData(
                "Task buffer is empty; cannot perform meta-update".into(),
            ));
        }

        let current_step = self.step_count;
        let batch_size = self.config.online_batch_size.min(self.task_buffer.len());

        // Take the most-recent `batch_size` tasks.
        let start_idx = self.task_buffer.len().saturating_sub(batch_size);
        let batch: Vec<&OnlineTask<T>> = self.task_buffer.iter().skip(start_idx).collect();

        let param_dim = self.meta_parameters.len();
        let mut meta_gradient = Array1::<T>::zeros(param_dim);
        let mut total_weight = T::zero();

        for task in &batch {
            // Staleness weight: decay ^ (current_step - task.timestamp)
            let age = current_step.saturating_sub(task.timestamp);
            let weight = self.config.staleness_decay.powi(age as i32);

            // Inner-loop adaptation
            let adapted = self.inner_adapt(&task.gradients)?;

            // Outer gradient contribution: (meta_params - adapted_params)
            // We want to move meta_parameters *towards* adapted, so gradient
            // direction is (adapted - meta_parameters).
            let mut diff = Array1::<T>::zeros(param_dim);
            Zip::from(&mut diff)
                .and(&adapted)
                .and(&self.meta_parameters)
                .for_each(|d, &a, &m| {
                    *d = a - m;
                });

            // Pre/post adaptation losses for the record
            let pre_loss = task.loss;
            let post_loss = self.approximate_post_loss(&task.gradients);

            self.adaptation_history.push(AdaptationRecord {
                task_id: task.task_id.clone(),
                pre_adaptation_loss: pre_loss,
                post_adaptation_loss: post_loss,
                num_steps: self.config.inner_steps.min(task.gradients.len()),
            });

            // Accumulate weighted gradient
            Zip::from(&mut meta_gradient).and(&diff).for_each(|mg, &d| {
                *mg = *mg + weight * d;
            });
            total_weight = total_weight + weight;
        }

        // Normalise by total weight
        if total_weight > T::zero() {
            meta_gradient.mapv_inplace(|v| v / total_weight);
        }

        // Outer update
        let lr = self.config.outer_lr;
        Zip::from(&mut self.meta_parameters)
            .and(&meta_gradient)
            .for_each(|p, &g| {
                *p = *p + lr * g;
            });

        self.step_count += 1;

        Ok(self.meta_parameters.clone())
    }

    /// Adapt meta-parameters to a specific task using its gradients.
    ///
    /// Runs the inner-loop gradient descent starting from the current
    /// `meta_parameters` and returns the adapted parameters.
    ///
    /// # Errors
    /// Returns `OptimError::InsufficientData` if `task_gradients` is empty.
    pub fn adapt_to_task(&self, task_gradients: &[Array1<T>]) -> Result<Array1<T>> {
        if task_gradients.is_empty() {
            return Err(OptimError::InsufficientData(
                "No gradients provided for task adaptation".into(),
            ));
        }
        self.inner_adapt(task_gradients)
    }

    /// Return the average adaptation efficiency (improvement ratio) over
    /// recorded history.
    ///
    /// Efficiency for a single record is defined as:
    ///   `(pre_loss - post_loss) / (pre_loss + epsilon)`
    ///
    /// The overall efficiency is the mean across all records.
    /// Returns `T::zero()` when there is no history.
    pub fn get_adaptation_efficiency(&self) -> T {
        if self.adaptation_history.is_empty() {
            return T::zero();
        }
        let epsilon = T::from(1e-10).unwrap_or_else(|| T::epsilon());
        let n = T::from(self.adaptation_history.len()).unwrap_or_else(|| T::one());
        let total: T = self.adaptation_history.iter().fold(T::zero(), |acc, rec| {
            let improvement = rec.pre_adaptation_loss - rec.post_adaptation_loss;
            let ratio = improvement / (rec.pre_adaptation_loss.abs() + epsilon);
            acc + ratio
        });
        total / n
    }

    // -----------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------

    /// Run inner-loop gradient descent using the supplied per-step gradients.
    fn inner_adapt(&self, gradients: &[Array1<T>]) -> Result<Array1<T>> {
        let steps = self.config.inner_steps.min(gradients.len());
        let mut params = self.meta_parameters.clone();
        let lr = self.config.inner_lr;

        for grad in gradients.iter().take(steps) {
            if grad.len() != params.len() {
                return Err(OptimError::ComputationError(format!(
                    "Gradient dimension {} does not match parameter dimension {}",
                    grad.len(),
                    params.len()
                )));
            }
            Zip::from(&mut params).and(grad).for_each(|p, &g| {
                *p = *p - lr * g;
            });
        }
        Ok(params)
    }

    /// Approximate the post-adaptation loss from gradient norms.
    ///
    /// Uses a simple heuristic: post_loss ~ mean gradient norm after the last
    /// inner step (smaller gradients indicate closer to optimum).
    fn approximate_post_loss(&self, gradients: &[Array1<T>]) -> T {
        let steps = self.config.inner_steps.min(gradients.len());
        if steps == 0 {
            return T::zero();
        }
        let last_grad = &gradients[steps - 1];
        let norm_sq = last_grad.iter().fold(T::zero(), |acc, &g| acc + g * g);
        norm_sq.sqrt()
    }
}

// ===========================================================================
// Tests
// ===========================================================================

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

    fn make_task(id: &str, dim: usize, timestamp: usize, loss: f64) -> OnlineTask<f64> {
        let params = Array1::from_elem(dim, 1.0);
        let gradients: Vec<Array1<f64>> = (0..5)
            .map(|i| Array1::from_elem(dim, 0.1 * (i as f64 + 1.0)))
            .collect();
        OnlineTask {
            task_id: id.to_string(),
            parameters: params,
            gradients,
            timestamp,
            loss,
        }
    }

    #[test]
    fn test_online_maml_receive_task() {
        let init = Array1::from_elem(4, 0.0_f64);
        let config = OnlineMAMLConfig::default().buffer_size(5);
        let mut maml = OnlineMAML::new(config, init);

        let task = make_task("t1", 4, 0, 1.0);
        maml.receive_task(task);

        assert_eq!(maml.buffer_len(), 1);
        assert_eq!(
            maml.task_buffer.back().map(|t| t.task_id.as_str()),
            Some("t1")
        );
    }

    #[test]
    fn test_online_maml_buffer_eviction() {
        let init = Array1::from_elem(4, 0.0_f64);
        let config = OnlineMAMLConfig::default().buffer_size(3);
        let mut maml = OnlineMAML::new(config, init);

        for i in 0..5 {
            let task = make_task(&format!("t{}", i), 4, i, 1.0);
            maml.receive_task(task);
        }

        // Buffer should contain only the 3 most-recent tasks.
        assert_eq!(maml.buffer_len(), 3);
        let ids: Vec<&str> = maml
            .task_buffer
            .iter()
            .map(|t| t.task_id.as_str())
            .collect();
        assert_eq!(ids, vec!["t2", "t3", "t4"]);
    }

    #[test]
    fn test_online_update_basic() {
        let dim = 4;
        let init = Array1::from_elem(dim, 0.0_f64);
        let config = OnlineMAMLConfig::default()
            .buffer_size(10)
            .online_batch_size(2)
            .inner_lr(0.01)
            .outer_lr(0.1)
            .inner_steps(3);
        let mut maml = OnlineMAML::new(config, init);

        // Add two tasks
        maml.receive_task(make_task("a", dim, 0, 2.0));
        maml.receive_task(make_task("b", dim, 1, 1.5));

        let updated = maml.online_update();
        assert!(updated.is_ok());
        let updated = updated.expect("online_update should succeed");

        // The meta-parameters should have moved from zero.
        let moved = updated.iter().any(|&v| v.abs() > 1e-12);
        assert!(moved, "Meta-parameters should change after an update");
        assert_eq!(maml.step_count(), 1);
    }

    #[test]
    fn test_adapt_to_task() {
        let dim = 4;
        let init = Array1::from_elem(dim, 1.0_f64);
        let config = OnlineMAMLConfig::default().inner_lr(0.1).inner_steps(3);
        let maml = OnlineMAML::new(config, init.clone());

        let grads: Vec<Array1<f64>> = (0..5).map(|_| Array1::from_elem(dim, 0.5)).collect();

        let adapted = maml.adapt_to_task(&grads);
        assert!(adapted.is_ok());
        let adapted = adapted.expect("adapt_to_task should succeed");

        // After 3 steps of lr=0.1 with gradient=0.5:
        // param = 1.0 - 3 * 0.1 * 0.5 = 0.85
        for &v in adapted.iter() {
            assert!((v - 0.85).abs() < 1e-10, "Expected 0.85 but got {}", v);
        }

        // Empty gradients should error.
        let empty_result = maml.adapt_to_task(&[]);
        assert!(empty_result.is_err());
    }

    #[test]
    fn test_adaptation_efficiency() {
        let dim = 4;
        let init = Array1::from_elem(dim, 0.0_f64);
        let config = OnlineMAMLConfig::default()
            .buffer_size(10)
            .online_batch_size(3)
            .inner_lr(0.01)
            .outer_lr(0.1)
            .inner_steps(3);
        let mut maml = OnlineMAML::new(config, init);

        // No history yet
        assert_eq!(maml.get_adaptation_efficiency(), 0.0);

        // Add tasks and run an update to generate history
        for i in 0..3 {
            maml.receive_task(make_task(&format!("t{}", i), dim, i, 2.0 + i as f64));
        }
        let _ = maml.online_update();

        assert!(!maml.adaptation_history().is_empty());
        // Efficiency should be a finite number
        let eff = maml.get_adaptation_efficiency();
        assert!(eff.is_finite(), "Efficiency should be finite, got {}", eff);
    }
}