Skip to main content

entrenar/config/cli/
core.rs

1//! Core CLI types - Cli, Command, and basic argument structs
2
3use clap::{Parser, Subcommand};
4use std::path::PathBuf;
5
6use super::extended::{
7    AuditArgs, BenchArgs, CompletionArgs, ExperimentsArgs, FinetuneArgs, InspectArgs, MonitorArgs,
8    PublishArgs,
9};
10use super::init::InitArgs;
11use super::quant_merge::{MergeArgs, QuantizeArgs};
12use super::research::ResearchArgs;
13use super::types::OutputFormat;
14
15/// Entrenar: Training & Optimization Library
16#[derive(Parser, Debug, Clone, PartialEq)]
17#[command(name = "entrenar")]
18#[command(author = "PAIML")]
19#[command(version)]
20#[command(
21    about = "Training & Optimization Library with autograd, LoRA, quantization, and model merging"
22)]
23pub struct Cli {
24    /// Subcommand to execute
25    #[command(subcommand)]
26    pub command: Command,
27
28    /// Enable verbose output
29    #[arg(short, long, global = true)]
30    pub verbose: bool,
31
32    /// Suppress all output except errors
33    #[arg(short, long, global = true)]
34    pub quiet: bool,
35}
36
37/// Available commands
38#[derive(Subcommand, Debug, Clone, PartialEq)]
39pub enum Command {
40    /// Train a model from YAML configuration
41    Train(TrainArgs),
42
43    /// Validate a configuration file without training
44    Validate(ValidateArgs),
45
46    /// Display information about a configuration
47    Info(InfoArgs),
48
49    /// Initialize a new YAML Mode training manifest
50    Init(InitArgs),
51
52    /// Quantize a model
53    Quantize(QuantizeArgs),
54
55    /// Merge multiple models
56    Merge(MergeArgs),
57
58    /// Academic research artifacts and workflows
59    Research(ResearchArgs),
60
61    /// Generate shell completions
62    Completion(CompletionArgs),
63
64    /// Run inference benchmarks
65    Bench(BenchArgs),
66
67    /// Inspect data or model statistics
68    Inspect(InspectArgs),
69
70    /// Audit model for bias and fairness
71    Audit(AuditArgs),
72
73    /// Monitor model for drift
74    Monitor(MonitorArgs),
75
76    /// Publish a trained model to HuggingFace Hub
77    Publish(PublishArgs),
78
79    /// Fine-tune a classification model (plan/apply workflow)
80    Finetune(FinetuneArgs),
81
82    /// Query the experiment store (runs, metrics, artifacts)
83    Experiments(ExperimentsArgs),
84}
85
86/// Arguments for the train command
87#[derive(Parser, Debug, Clone, PartialEq)]
88pub struct TrainArgs {
89    /// Path to YAML configuration file
90    #[arg(value_name = "CONFIG")]
91    pub config: PathBuf,
92
93    /// Override output directory
94    #[arg(short, long)]
95    pub output_dir: Option<PathBuf>,
96
97    /// Resume training from checkpoint
98    #[arg(short, long)]
99    pub resume: Option<PathBuf>,
100
101    /// Override number of epochs
102    #[arg(short, long)]
103    pub epochs: Option<usize>,
104
105    /// Override batch size
106    #[arg(short, long)]
107    pub batch_size: Option<usize>,
108
109    /// Override learning rate
110    #[arg(short, long)]
111    pub lr: Option<f32>,
112
113    /// Dry run (validate config but don't train)
114    #[arg(long)]
115    pub dry_run: bool,
116
117    /// Save checkpoint every N steps
118    #[arg(long)]
119    pub save_every: Option<usize>,
120
121    /// Log metrics every N steps
122    #[arg(long)]
123    pub log_every: Option<usize>,
124
125    /// Random seed for reproducibility
126    #[arg(long)]
127    pub seed: Option<u64>,
128}
129
130/// Arguments for the validate command
131#[derive(Parser, Debug, Clone, PartialEq)]
132pub struct ValidateArgs {
133    /// Path to YAML configuration file
134    #[arg(value_name = "CONFIG")]
135    pub config: PathBuf,
136
137    /// Show detailed validation report
138    #[arg(short, long)]
139    pub detailed: bool,
140}
141
142/// Arguments for the info command
143#[derive(Parser, Debug, Clone, PartialEq)]
144pub struct InfoArgs {
145    /// Path to YAML configuration file
146    #[arg(value_name = "CONFIG")]
147    pub config: PathBuf,
148
149    /// Output format (text, json, yaml)
150    #[arg(short, long, default_value = "text")]
151    pub format: OutputFormat,
152}
153
154/// Parse CLI arguments from a string slice (for testing)
155pub fn parse_args<I, T>(args: I) -> Result<Cli, clap::Error>
156where
157    I: IntoIterator<Item = T>,
158    T: Into<std::ffi::OsString> + Clone,
159{
160    Cli::try_parse_from(args)
161}
162
163/// Apply command-line overrides to a TrainSpec
164pub fn apply_overrides(spec: &mut crate::config::TrainSpec, args: &TrainArgs) {
165    if let Some(output_dir) = &args.output_dir {
166        spec.training.output_dir = output_dir.clone();
167    }
168    if let Some(epochs) = args.epochs {
169        spec.training.epochs = epochs;
170    }
171    if let Some(batch_size) = args.batch_size {
172        spec.data.batch_size = batch_size;
173    }
174    if let Some(lr) = args.lr {
175        spec.optimizer.lr = lr;
176    }
177    if let Some(save_every) = args.save_every {
178        spec.training.save_interval = save_every;
179    }
180    // Note: log_every and seed are CLI-only options not persisted in config
181}