axonml-cli 0.5.0

Command-line interface for the Axonml ML framework
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
//! Config - Configuration File Handling
//!
//! # File
//! `crates/axonml-cli/src/config.rs`
//!
//! # Author
//! Andrew Jewell Sr - AutomataNexus
//!
//! # Updated
//! March 8, 2026
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

use serde::{Deserialize, Serialize};
use std::path::Path;

use crate::error::{CliError, CliResult};

// =============================================================================
// Project Configuration
// =============================================================================

/// Project configuration (axonml.toml)
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct ProjectConfig {
    /// Project metadata
    pub project: ProjectMeta,

    /// Training configuration
    #[serde(default)]
    pub training: TrainingConfig,

    /// Model configuration
    #[serde(default)]
    pub model: ModelConfig,

    /// Data configuration
    #[serde(default)]
    pub data: DataConfig,
}

/// Project metadata
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct ProjectMeta {
    /// Project name
    pub name: String,

    /// Project version
    #[serde(default = "default_version")]
    pub version: String,

    /// Project description
    #[serde(default)]
    pub description: String,

    /// Authors
    #[serde(default)]
    pub authors: Vec<String>,
}

fn default_version() -> String {
    "0.1.0".to_string()
}

// =============================================================================
// Training Configuration
// =============================================================================

/// Training configuration
#[derive(Debug, Serialize, Deserialize)]
pub struct TrainingConfig {
    /// Number of epochs
    #[serde(default = "default_epochs")]
    pub epochs: usize,

    /// Batch size
    #[serde(default = "default_batch_size")]
    pub batch_size: usize,

    /// Learning rate
    #[serde(default = "default_lr")]
    pub learning_rate: f64,

    /// Optimizer configuration
    #[serde(default)]
    pub optimizer: OptimizerConfig,

    /// Learning rate scheduler
    #[serde(default)]
    pub scheduler: Option<SchedulerConfig>,

    /// Device to train on
    #[serde(default = "default_device")]
    pub device: String,

    /// Random seed
    #[serde(default)]
    pub seed: Option<u64>,

    /// Checkpoint save frequency (in epochs)
    #[serde(default = "default_checkpoint_freq")]
    pub checkpoint_frequency: usize,

    /// Output directory
    #[serde(default = "default_output_dir")]
    pub output_dir: String,

    /// Number of data loading workers
    #[serde(default = "default_workers")]
    pub num_workers: usize,

    /// Enable gradient clipping
    #[serde(default)]
    pub gradient_clip: Option<f64>,

    /// Mixed precision training
    #[serde(default)]
    pub mixed_precision: bool,
}

impl Default for TrainingConfig {
    fn default() -> Self {
        Self {
            epochs: default_epochs(),
            batch_size: default_batch_size(),
            learning_rate: default_lr(),
            optimizer: OptimizerConfig::default(),
            scheduler: None,
            device: default_device(),
            seed: None,
            checkpoint_frequency: default_checkpoint_freq(),
            output_dir: default_output_dir(),
            num_workers: default_workers(),
            gradient_clip: None,
            mixed_precision: false,
        }
    }
}

fn default_epochs() -> usize {
    10
}
fn default_batch_size() -> usize {
    32
}
fn default_lr() -> f64 {
    0.001
}
fn default_device() -> String {
    "cpu".to_string()
}
fn default_checkpoint_freq() -> usize {
    1
}
fn default_output_dir() -> String {
    "./output".to_string()
}
fn default_workers() -> usize {
    4
}

// =============================================================================
// Optimizer Configuration
// =============================================================================

/// Optimizer configuration
#[derive(Debug, Serialize, Deserialize)]
pub struct OptimizerConfig {
    /// Optimizer type (sgd, adam, adamw, rmsprop)
    #[serde(default = "default_optimizer")]
    pub name: String,

    /// Weight decay
    #[serde(default)]
    pub weight_decay: f64,

    /// Momentum (for SGD)
    #[serde(default)]
    pub momentum: f64,

    /// Beta1 (for Adam)
    #[serde(default = "default_beta1")]
    pub beta1: f64,

    /// Beta2 (for Adam)
    #[serde(default = "default_beta2")]
    pub beta2: f64,

    /// Epsilon (for Adam)
    #[serde(default = "default_eps")]
    pub eps: f64,

    /// Nesterov momentum (for SGD)
    #[serde(default)]
    pub nesterov: bool,
}

impl Default for OptimizerConfig {
    fn default() -> Self {
        Self {
            name: default_optimizer(),
            weight_decay: 0.0,
            momentum: 0.0,
            beta1: default_beta1(),
            beta2: default_beta2(),
            eps: default_eps(),
            nesterov: false,
        }
    }
}

fn default_optimizer() -> String {
    "adam".to_string()
}
fn default_beta1() -> f64 {
    0.9
}
fn default_beta2() -> f64 {
    0.999
}
fn default_eps() -> f64 {
    1e-8
}

// =============================================================================
// Scheduler Configuration
// =============================================================================

/// Learning rate scheduler configuration
#[derive(Debug, Serialize, Deserialize)]
pub struct SchedulerConfig {
    /// Scheduler type (step, cosine, exponential, plateau)
    pub name: String,

    /// Step size (for `StepLR`)
    #[serde(default)]
    pub step_size: Option<usize>,

    /// Gamma (decay factor)
    #[serde(default = "default_gamma")]
    pub gamma: f64,

    /// Milestones (for `MultiStepLR`)
    #[serde(default)]
    pub milestones: Vec<usize>,

    /// `T_max` (for `CosineAnnealing`)
    #[serde(default)]
    pub t_max: Option<usize>,

    /// Minimum learning rate
    #[serde(default)]
    pub eta_min: f64,

    /// Warmup epochs
    #[serde(default)]
    pub warmup_epochs: usize,
}

fn default_gamma() -> f64 {
    0.1
}

// =============================================================================
// Model Configuration
// =============================================================================

/// Model configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ModelConfig {
    /// Model architecture name
    #[serde(default)]
    pub architecture: String,

    /// Path to model definition file
    #[serde(default)]
    pub path: Option<String>,

    /// Number of input features/channels
    #[serde(default)]
    pub input_size: Option<usize>,

    /// Number of output classes
    #[serde(default)]
    pub num_classes: Option<usize>,

    /// Hidden layer sizes
    #[serde(default)]
    pub hidden_sizes: Vec<usize>,

    /// Dropout probability
    #[serde(default)]
    pub dropout: f64,

    /// Pretrained weights path
    #[serde(default)]
    pub pretrained: Option<String>,
}

// =============================================================================
// Data Configuration
// =============================================================================

/// Data configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DataConfig {
    /// Path to training data
    #[serde(default)]
    pub train_path: Option<String>,

    /// Path to validation data
    #[serde(default)]
    pub val_path: Option<String>,

    /// Path to test data
    #[serde(default)]
    pub test_path: Option<String>,

    /// Data format (csv, json, images, etc.)
    #[serde(default)]
    pub format: String,

    /// Train/validation split ratio
    #[serde(default = "default_val_split")]
    pub val_split: f64,

    /// Enable data augmentation
    #[serde(default)]
    pub augmentation: bool,

    /// Shuffle training data
    #[serde(default = "default_shuffle")]
    pub shuffle: bool,

    /// Normalize data
    #[serde(default)]
    pub normalize: bool,

    /// Normalization mean
    #[serde(default)]
    pub mean: Vec<f64>,

    /// Normalization std
    #[serde(default)]
    pub std: Vec<f64>,
}

fn default_val_split() -> f64 {
    0.1
}
fn default_shuffle() -> bool {
    true
}

// =============================================================================
// Configuration Loading
// =============================================================================

impl ProjectConfig {
    /// Load configuration from a TOML file
    pub fn load<P: AsRef<Path>>(path: P) -> CliResult<Self> {
        let content = std::fs::read_to_string(path.as_ref())?;
        let config: ProjectConfig = toml::from_str(&content)?;
        Ok(config)
    }

    /// Save configuration to a TOML file
    pub fn save<P: AsRef<Path>>(&self, path: P) -> CliResult<()> {
        let content =
            toml::to_string_pretty(self).map_err(|e| CliError::Serialization(e.to_string()))?;
        std::fs::write(path, content)?;
        Ok(())
    }

    /// Create a default configuration with the given project name
    pub fn new(name: &str) -> Self {
        Self {
            project: ProjectMeta {
                name: name.to_string(),
                version: "0.1.0".to_string(),
                description: String::new(),
                authors: vec![],
            },
            training: TrainingConfig::default(),
            model: ModelConfig::default(),
            data: DataConfig::default(),
        }
    }
}

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

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

    #[test]
    fn test_default_config() {
        let config = ProjectConfig::new("test-project");
        assert_eq!(config.project.name, "test-project");
        assert_eq!(config.training.epochs, 10);
        assert_eq!(config.training.batch_size, 32);
    }

    #[test]
    fn test_config_serialization() {
        let config = ProjectConfig::new("test-project");
        let toml_str = toml::to_string_pretty(&config).unwrap();
        let parsed: ProjectConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(parsed.project.name, "test-project");
    }
}