optirs-learned 0.3.1

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
// Performance prediction for neural architectures

use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::numeric::Float;
use std::fmt::Debug;
use std::collections::HashMap;
use crate::error::Result;
use super::evaluation::EvaluationMetrics;
use super::config::ArchitectureSpec;

/// Performance predictor using surrogate models
pub struct PerformancePredictor<T: Float + Debug + Send + Sync + 'static> {
    /// Prediction model
    model: PredictionModel<T>,
    /// Training data
    training_data: Vec<(String, EvaluationMetrics)>,
    /// Model configuration
    config: PredictorConfig,
    /// Feature extractor
    feature_extractor: FeatureExtractor,
    /// Performance metrics for the predictor
    predictor_metrics: PredictorMetrics<T>,
}

impl<T: Float + Debug + Send + Sync + 'static> PerformancePredictor<T> {
    /// Create new performance predictor
    pub fn new(config: &super::config::NASConfig) -> Result<Self> {
        let predictor_config = PredictorConfig::from_nas_config(config);
        let model = PredictionModel::new(&predictor_config)?;
        let feature_extractor = FeatureExtractor::new();
        let predictor_metrics = PredictorMetrics::new();

        Ok(Self {
            model,
            training_data: Vec::new(),
            config: predictor_config,
            feature_extractor,
            predictor_metrics,
        })
    }

    /// Predict performance for architecture
    pub fn predict(&mut self, architecture: &str) -> Result<Option<EvaluationMetrics>> {
        if !self.model.is_trained() {
            return Ok(None);
        }

        let features_f64 = self.feature_extractor.extract_features(architecture)?;
        let features: Array1<T> = features_f64.mapv(|x| scirs2_core::numeric::NumCast::from(x).unwrap_or_else(|| T::zero()));
        let prediction = self.model.predict(&features)?;

        let metrics = self.prediction_to_metrics(prediction)?;
        self.predictor_metrics.record_prediction();

        Ok(Some(metrics))
    }

    /// Train predictor on new sample
    pub fn train_on_sample(&mut self, architecture: &str, metrics: &EvaluationMetrics) -> Result<()> {
        self.training_data.push((architecture.to_string(), metrics.clone()));

        // Retrain model if enough samples accumulated
        if self.training_data.len() % self.config.retrain_interval == 0 {
            self.retrain_model()?;
        }

        Ok(())
    }

    /// Retrain the prediction model
    fn retrain_model(&mut self) -> Result<()> {
        if self.training_data.len() < self.config.min_training_samples {
            return Ok(());
        }

        // Extract features and targets
        let mut features = Vec::new();
        let mut targets = Vec::new();

        for (architecture, metrics) in &self.training_data {
            let feature_vec_f64 = self.feature_extractor.extract_features(architecture)?;
            let feature_vec: Array1<T> = feature_vec_f64.mapv(|x| scirs2_core::numeric::NumCast::from(x).unwrap_or_else(|| T::zero()));
            let target_vec = self.metrics_to_target(metrics)?;

            features.push(feature_vec);
            targets.push(target_vec);
        }

        // Train model
        self.model.train(features, targets)?;
        self.predictor_metrics.record_training();

        Ok(())
    }

    /// Convert prediction to evaluation metrics
    fn prediction_to_metrics(&self, prediction: Array1<T>) -> Result<EvaluationMetrics> {
        if prediction.len() < 8 {
            return Err(crate::error::OptimError::Other(
                "Invalid prediction vector".to_string()
            ));
        }

        Ok(EvaluationMetrics {
            accuracy: prediction[0].to_f64().unwrap_or(0.0),
            training_time_seconds: prediction[1].to_f64().unwrap_or(0.0),
            inference_time_ms: prediction[2].to_f64().unwrap_or(0.0),
            memory_usage_mb: prediction[3].to_usize().unwrap_or(0),
            flops: prediction[4].to_u64().unwrap_or(0),
            parameters: prediction[5].to_usize().unwrap_or(0),
            energy_consumption: prediction[6].to_f64().unwrap_or(0.0),
            convergence_rate: prediction[7].to_f64().unwrap_or(0.0),
            robustness_score: 0.7, // Default values for non-predicted metrics
            generalization_score: prediction[0].to_f64().unwrap_or(0.0) * 0.9,
            efficiency_score: 0.6,
            valid: true,
        })
    }

    /// Convert evaluation metrics to target vector
    fn metrics_to_target(&self, metrics: &EvaluationMetrics) -> Result<Array1<T>> {
        let target = Array1::from(vec![
            scirs2_core::numeric::NumCast::from(metrics.accuracy).unwrap_or_else(|| T::zero()),
            scirs2_core::numeric::NumCast::from(metrics.training_time_seconds).unwrap_or_else(|| T::zero()),
            scirs2_core::numeric::NumCast::from(metrics.inference_time_ms).unwrap_or_else(|| T::zero()),
            scirs2_core::numeric::NumCast::from(metrics.memory_usage_mb as f64).unwrap_or_else(|| T::zero()),
            scirs2_core::numeric::NumCast::from(metrics.flops as f64).unwrap_or_else(|| T::zero()),
            scirs2_core::numeric::NumCast::from(metrics.parameters as f64).unwrap_or_else(|| T::zero()),
            scirs2_core::numeric::NumCast::from(metrics.energy_consumption).unwrap_or_else(|| T::zero()),
            scirs2_core::numeric::NumCast::from(metrics.convergence_rate).unwrap_or_else(|| T::zero()),
        ]);

        Ok(target)
    }

    /// Get predictor accuracy
    pub fn get_accuracy(&self) -> f64 {
        self.predictor_metrics.get_accuracy()
    }

    /// Get predictor statistics
    pub fn get_statistics(&self) -> &PredictorMetrics<T> {
        &self.predictor_metrics
    }
}

/// Prediction model abstraction
pub struct PredictionModel<T: Float + Debug + Send + Sync + 'static> {
    /// Model type
    model_type: ModelType,
    /// Model parameters
    parameters: Option<ModelParameters<T>>,
    /// Training configuration
    config: PredictorConfig,
}

impl<T: Float + Debug + Send + Sync + 'static> PredictionModel<T> {
    pub fn new(config: &PredictorConfig) -> Result<Self> {
        Ok(Self {
            model_type: config.model_type,
            parameters: None,
            config: config.clone(),
        })
    }

    pub fn is_trained(&self) -> bool {
        self.parameters.is_some()
    }

    pub fn train(&mut self, features: Vec<Array1<T>>, targets: Vec<Array1<T>>) -> Result<()> {
        match self.model_type {
            ModelType::LinearRegression => self.train_linear_regression(features, targets),
            ModelType::RandomForest => self.train_random_forest(features, targets),
            ModelType::NeuralNetwork => self.train_neural_network(features, targets),
            ModelType::GaussianProcess => self.train_gaussian_process(features, targets),
        }
    }

    pub fn predict(&self, features: &Array1<T>) -> Result<Array1<T>> {
        match &self.parameters {
            Some(params) => match self.model_type {
                ModelType::LinearRegression => self.predict_linear_regression(features, params),
                ModelType::RandomForest => self.predict_random_forest(features, params),
                ModelType::NeuralNetwork => self.predict_neural_network(features, params),
                ModelType::GaussianProcess => self.predict_gaussian_process(features, params),
            },
            None => Err(crate::error::OptimError::Other("Model not trained".to_string())),
        }
    }

    fn train_linear_regression(&mut self, features: Vec<Array1<T>>, targets: Vec<Array1<T>>) -> Result<()> {
        // Simplified linear regression training
        if features.is_empty() || targets.is_empty() {
            return Err(crate::error::OptimError::Other("Empty training data".to_string()));
        }

        let input_dim = features[0].len();
        let output_dim = targets[0].len();

        // Initialize weights and bias
        let mut weights = Array2::<T>::zeros((input_dim, output_dim));
        let mut bias = Array1::<T>::zeros(output_dim);

        // Simple gradient descent (placeholder implementation)
        let learning_rate = scirs2_core::numeric::NumCast::from(0.01).unwrap_or_else(|| T::zero());
        let epochs = 100;

        for _ in 0..epochs {
            for (feature, target) in features.iter().zip(targets.iter()) {
                let prediction = weights.t().dot(feature) + &bias;
                let error = target - &prediction;

                // Update weights and bias
                for i in 0..input_dim {
                    for j in 0..output_dim {
                        weights[[i, j]] = weights[[i, j]] + learning_rate * feature[i] * error[j];
                    }
                }

                for j in 0..output_dim {
                    bias[j] = bias[j] + learning_rate * error[j];
                }
            }
        }

        self.parameters = Some(ModelParameters::LinearRegression { weights, bias });
        Ok(())
    }

    fn predict_linear_regression(&self, features: &Array1<T>, params: &ModelParameters<T>) -> Result<Array1<T>> {
        match params {
            ModelParameters::LinearRegression { weights, bias } => {
                Ok(weights.t().dot(features) + bias)
            }
            _ => Err(crate::error::OptimError::Other("Invalid parameters for linear regression".to_string())),
        }
    }

    fn train_random_forest(&mut self, _features: Vec<Array1<T>>, _targets: Vec<Array1<T>>) -> Result<()> {
        // Placeholder implementation
        self.parameters = Some(ModelParameters::RandomForest);
        Ok(())
    }

    fn predict_random_forest(&self, features: &Array1<T>, _params: &ModelParameters<T>) -> Result<Array1<T>> {
        // Placeholder prediction
        Ok(Array1::from(vec![scirs2_core::numeric::NumCast::from(0.5).unwrap_or_else(|| T::zero()); 8]))
    }

    fn train_neural_network(&mut self, _features: Vec<Array1<T>>, _targets: Vec<Array1<T>>) -> Result<()> {
        // Placeholder implementation
        self.parameters = Some(ModelParameters::NeuralNetwork);
        Ok(())
    }

    fn predict_neural_network(&self, features: &Array1<T>, _params: &ModelParameters<T>) -> Result<Array1<T>> {
        // Placeholder prediction
        Ok(Array1::from(vec![scirs2_core::numeric::NumCast::from(0.6).unwrap_or_else(|| T::zero()); 8]))
    }

    fn train_gaussian_process(&mut self, _features: Vec<Array1<T>>, _targets: Vec<Array1<T>>) -> Result<()> {
        // Placeholder implementation
        self.parameters = Some(ModelParameters::GaussianProcess);
        Ok(())
    }

    fn predict_gaussian_process(&self, features: &Array1<T>, _params: &ModelParameters<T>) -> Result<Array1<T>> {
        // Placeholder prediction
        Ok(Array1::from(vec![scirs2_core::numeric::NumCast::from(0.7).unwrap_or_else(|| T::zero()); 8]))
    }
}

/// Model types for performance prediction
#[derive(Debug, Clone, Copy)]
pub enum ModelType {
    LinearRegression,
    RandomForest,
    NeuralNetwork,
    GaussianProcess,
}

/// Model parameters
pub enum ModelParameters<T: Float + Debug + Send + Sync + 'static> {
    LinearRegression {
        weights: Array2<T>,
        bias: Array1<T>,
    },
    RandomForest,
    NeuralNetwork,
    GaussianProcess,
}

/// Feature extractor for architectures
pub struct FeatureExtractor {
    feature_dim: usize,
}

impl FeatureExtractor {
    pub fn new() -> Self {
        Self {
            feature_dim: 20, // Fixed feature dimension
        }
    }

    pub fn extract_features(&self, architecture: &str) -> Result<Array1<f64>> {
        // Simple feature extraction based on architecture string
        let mut features = vec![0.0; self.feature_dim];

        // Basic features from architecture string
        features[0] = architecture.len() as f64; // Architecture complexity
        features[1] = architecture.matches("Dense").count() as f64;
        features[2] = architecture.matches("Conv").count() as f64;
        features[3] = architecture.matches("LSTM").count() as f64;
        features[4] = architecture.matches("Attention").count() as f64;

        // Parse numeric values if possible
        if let Ok(spec) = serde_json::from_str::<ArchitectureSpec>(architecture) {
            features[5] = spec.layers.len() as f64;
            features[6] = (spec.estimated_params as f64).log10();
            features[7] = (spec.estimated_flops as f64).log10();
            features[8] = spec.estimated_memory_mb as f64;
        }

        // Fill remaining features with derived values
        for i in 9..self.feature_dim {
            features[i] = (features[i % 9] * (i as f64 + 1.0)).sin();
        }

        Ok(Array1::from(features))
    }
}

/// Predictor configuration
#[derive(Debug, Clone)]
pub struct PredictorConfig {
    pub model_type: ModelType,
    pub min_training_samples: usize,
    pub retrain_interval: usize,
    pub validation_split: f64,
    pub enable_cross_validation: bool,
}

impl PredictorConfig {
    pub fn from_nas_config(config: &super::config::NASConfig) -> Self {
        Self {
            model_type: if config.enable_performance_prediction {
                ModelType::LinearRegression
            } else {
                ModelType::LinearRegression
            },
            min_training_samples: 20,
            retrain_interval: 10,
            validation_split: 0.2,
            enable_cross_validation: false,
        }
    }
}

/// Performance metrics for the predictor itself
#[derive(Debug)]
pub struct PredictorMetrics<T: Float + Debug + Send + Sync + 'static> {
    total_predictions: usize,
    total_trainings: usize,
    prediction_errors: Vec<T>,
    training_times: Vec<std::time::Duration>,
}

impl<T: Float + Debug + Send + Sync + 'static> PredictorMetrics<T> {
    pub fn new() -> Self {
        Self {
            total_predictions: 0,
            total_trainings: 0,
            prediction_errors: Vec::new(),
            training_times: Vec::new(),
        }
    }

    pub fn record_prediction(&mut self) {
        self.total_predictions += 1;
    }

    pub fn record_training(&mut self) {
        self.total_trainings += 1;
    }

    pub fn record_prediction_error(&mut self, error: T) {
        self.prediction_errors.push(error);
        if self.prediction_errors.len() > 1000 {
            self.prediction_errors.remove(0);
        }
    }

    pub fn get_accuracy(&self) -> f64 {
        if self.prediction_errors.is_empty() {
            return 0.0;
        }

        let avg_error: T = self.prediction_errors.iter().cloned().fold(T::zero(), |acc, x| acc + x)
            / T::from(self.prediction_errors.len()).expect("unwrap failed");

        (T::one() - avg_error).to_f64().unwrap_or(0.0).max(0.0).min(1.0)
    }
}

/// Performance metrics structure
pub struct PerformanceMetrics {
    pub accuracy: f64,
    pub latency: f64,
    pub memory: f64,
    pub energy: f64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::learned_optimizers::neural_architecture_search::config::NASConfig;

    #[test]
    fn test_performance_predictor_creation() {
        let config = NASConfig::new();
        let predictor = PerformancePredictor::<f32>::new(&config);
        assert!(predictor.is_ok());
    }

    #[test]
    fn test_feature_extractor() {
        let extractor = FeatureExtractor::new();
        let features = extractor.extract_features("test_architecture");
        assert!(features.is_ok());

        let feature_vec = features.expect("unwrap failed");
        assert_eq!(feature_vec.len(), 20);
    }

    #[test]
    fn test_prediction_model() {
        let config = PredictorConfig {
            model_type: ModelType::LinearRegression,
            min_training_samples: 5,
            retrain_interval: 2,
            validation_split: 0.2,
            enable_cross_validation: false,
        };

        let mut model = PredictionModel::<f32>::new(&config).expect("unwrap failed");
        assert!(!model.is_trained());
    }
}