lattice-tune 0.2.2

Training infrastructure for Lattice neural models
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
//! Registered model types

use crate::train::TrainingMetrics;
use chrono::{DateTime, Utc};
use uuid::Uuid;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Model status in the registry
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[non_exhaustive]
pub enum ModelStatus {
    /// Model is registered but not yet validated
    #[default]
    Pending,

    /// Model has passed validation
    Validated,

    /// Model is staged for deployment
    Staged,

    /// Model is in production
    Production,

    /// Model has been archived
    Archived,

    /// Model has been deprecated
    Deprecated,
}

impl std::fmt::Display for ModelStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ModelStatus::Pending => write!(f, "pending"),
            ModelStatus::Validated => write!(f, "validated"),
            ModelStatus::Staged => write!(f, "staged"),
            ModelStatus::Production => write!(f, "production"),
            ModelStatus::Archived => write!(f, "archived"),
            ModelStatus::Deprecated => write!(f, "deprecated"),
        }
    }
}

/// Metadata about a registered model
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ModelMetadata {
    /// Model architecture description
    pub architecture: String,

    /// Input dimension
    pub input_dim: usize,

    /// Output dimension (number of classes)
    pub output_dim: usize,

    /// Number of parameters
    pub num_parameters: usize,

    /// Training configuration hash (for reproducibility)
    pub config_hash: Option<String>,

    /// Training dataset ID
    pub dataset_id: Option<String>,

    /// Number of training examples
    pub num_training_examples: usize,

    /// Training metrics summary
    pub training_metrics: Option<TrainingMetrics>,

    /// Validation accuracy
    pub validation_accuracy: Option<f32>,

    /// Test accuracy (if evaluated)
    pub test_accuracy: Option<f32>,

    /// Custom tags
    pub tags: Vec<String>,

    /// Additional properties
    #[cfg(feature = "serde")]
    pub extra: Option<serde_json::Value>,

    /// Additional properties (non-serde fallback)
    #[cfg(not(feature = "serde"))]
    pub extra: Option<String>,
}

impl Default for ModelMetadata {
    fn default() -> Self {
        Self {
            architecture: "unknown".to_string(),
            input_dim: 0,
            output_dim: 0,
            num_parameters: 0,
            config_hash: None,
            dataset_id: None,
            num_training_examples: 0,
            training_metrics: None,
            validation_accuracy: None,
            test_accuracy: None,
            tags: Vec::new(),
            extra: None,
        }
    }
}

impl ModelMetadata {
    /// Create metadata for a classification model
    pub fn classifier(input_dim: usize, output_dim: usize, num_parameters: usize) -> Self {
        Self {
            architecture: format!("classifier_{input_dim}x{output_dim}"),
            input_dim,
            output_dim,
            num_parameters,
            ..Default::default()
        }
    }

    /// Set architecture description
    pub fn architecture(mut self, arch: impl Into<String>) -> Self {
        self.architecture = arch.into();
        self
    }

    /// Set dataset info
    pub fn dataset(mut self, id: impl Into<String>, num_examples: usize) -> Self {
        self.dataset_id = Some(id.into());
        self.num_training_examples = num_examples;
        self
    }

    /// Set training metrics
    pub fn training_metrics(mut self, metrics: TrainingMetrics) -> Self {
        self.validation_accuracy = metrics.final_val_loss.map(|_| {
            metrics
                .history
                .last()
                .and_then(|m| m.val_accuracy)
                .unwrap_or(0.0)
        });
        self.training_metrics = Some(metrics);
        self
    }

    /// Add a tag
    pub fn tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }
}

/// A registered model with versioning
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RegisteredModel {
    /// Unique model ID
    pub id: Uuid,

    /// Model name (e.g., "intent_classifier")
    pub name: String,

    /// Semantic version (e.g., "1.2.3")
    pub version: String,

    /// Model status
    pub status: ModelStatus,

    /// Model metadata
    pub metadata: ModelMetadata,

    /// When the model was registered
    pub registered_at: DateTime<Utc>,

    /// When the model was last updated
    pub updated_at: DateTime<Utc>,

    /// User/system that registered the model
    pub registered_by: Option<String>,

    /// Description
    pub description: Option<String>,

    /// Path to model weights (relative to registry root)
    pub weights_path: Option<String>,

    /// Size of weights file in bytes
    pub weights_size: Option<usize>,

    /// SHA256 hash of weights file
    pub weights_hash: Option<String>,

    /// Parent model (if fine-tuned from another)
    pub parent_id: Option<Uuid>,

    /// Child models (fine-tuned from this)
    pub children: Vec<Uuid>,
}

impl RegisteredModel {
    /// Create a new registered model
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            name: name.into(),
            version: version.into(),
            status: ModelStatus::Pending,
            metadata: ModelMetadata::default(),
            registered_at: now,
            updated_at: now,
            registered_by: None,
            description: None,
            weights_path: None,
            weights_size: None,
            weights_hash: None,
            parent_id: None,
            children: Vec::new(),
        }
    }

    /// Create with specific ID
    pub fn with_id(id: Uuid, name: impl Into<String>, version: impl Into<String>) -> Self {
        let mut model = Self::new(name, version);
        model.id = id;
        model
    }

    /// Set metadata
    pub fn with_metadata(mut self, metadata: ModelMetadata) -> Self {
        self.metadata = metadata;
        self.updated_at = Utc::now();
        self
    }

    /// Set status
    pub fn with_status(mut self, status: ModelStatus) -> Self {
        self.status = status;
        self.updated_at = Utc::now();
        self
    }

    /// Set description
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self.updated_at = Utc::now();
        self
    }

    /// Set registered by
    pub fn registered_by(mut self, user: impl Into<String>) -> Self {
        self.registered_by = Some(user.into());
        self
    }

    /// Set parent model
    pub fn with_parent(mut self, parent_id: Uuid) -> Self {
        self.parent_id = Some(parent_id);
        self.updated_at = Utc::now();
        self
    }

    /// Set weights information
    pub fn with_weights(
        mut self,
        path: impl Into<String>,
        size: usize,
        hash: impl Into<String>,
    ) -> Self {
        self.weights_path = Some(path.into());
        self.weights_size = Some(size);
        self.weights_hash = Some(hash.into());
        self.updated_at = Utc::now();
        self
    }

    /// Get full model identifier
    pub fn full_name(&self) -> String {
        format!("{}:{}", self.name, self.version)
    }

    /// Parse version components (major, minor, patch)
    pub fn version_tuple(&self) -> Option<(u32, u32, u32)> {
        let parts: Vec<&str> = self.version.split('.').collect();
        if parts.len() == 3 {
            let major = parts[0].parse().ok()?;
            let minor = parts[1].parse().ok()?;
            let patch = parts[2].parse().ok()?;
            Some((major, minor, patch))
        } else {
            None
        }
    }

    /// Check if this version is newer than another
    pub fn is_newer_than(&self, other: &RegisteredModel) -> Option<bool> {
        let self_ver = self.version_tuple()?;
        let other_ver = other.version_tuple()?;
        Some(self_ver > other_ver)
    }

    /// Validate the model
    pub fn validate(&self) -> Result<(), String> {
        if self.name.is_empty() {
            return Err("Model name cannot be empty".to_string());
        }
        if self.version.is_empty() {
            return Err("Model version cannot be empty".to_string());
        }
        Ok(())
    }

    /// Mark as validated
    pub fn mark_validated(&mut self) {
        self.status = ModelStatus::Validated;
        self.updated_at = Utc::now();
    }

    /// Mark as staged
    pub fn mark_staged(&mut self) {
        self.status = ModelStatus::Staged;
        self.updated_at = Utc::now();
    }

    /// Mark as production
    pub fn mark_production(&mut self) {
        self.status = ModelStatus::Production;
        self.updated_at = Utc::now();
    }

    /// Mark as archived
    pub fn mark_archived(&mut self) {
        self.status = ModelStatus::Archived;
        self.updated_at = Utc::now();
    }

    /// Mark as deprecated
    pub fn mark_deprecated(&mut self) {
        self.status = ModelStatus::Deprecated;
        self.updated_at = Utc::now();
    }

    /// Check if model is deployable (validated or staged)
    pub fn is_deployable(&self) -> bool {
        matches!(
            self.status,
            ModelStatus::Validated | ModelStatus::Staged | ModelStatus::Production
        )
    }

    /// Check if model is active (not archived or deprecated)
    pub fn is_active(&self) -> bool {
        !matches!(self.status, ModelStatus::Archived | ModelStatus::Deprecated)
    }
}

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

    #[test]
    fn test_model_creation() {
        let model = RegisteredModel::new("intent_classifier", "1.0.0");

        assert_eq!(model.name, "intent_classifier");
        assert_eq!(model.version, "1.0.0");
        assert_eq!(model.status, ModelStatus::Pending);
        assert!(model.validate().is_ok());
    }

    #[test]
    fn test_model_builder() {
        let metadata = ModelMetadata::classifier(768, 6, 1000);
        let model = RegisteredModel::new("intent_classifier", "1.0.0")
            .with_metadata(metadata)
            .with_description("Test model")
            .registered_by("test_user");

        assert_eq!(model.description, Some("Test model".to_string()));
        assert_eq!(model.registered_by, Some("test_user".to_string()));
        assert_eq!(model.metadata.input_dim, 768);
    }

    #[test]
    fn test_model_status_transitions() {
        let mut model = RegisteredModel::new("test", "1.0.0");

        assert_eq!(model.status, ModelStatus::Pending);
        assert!(!model.is_deployable());

        model.mark_validated();
        assert_eq!(model.status, ModelStatus::Validated);
        assert!(model.is_deployable());

        model.mark_staged();
        assert_eq!(model.status, ModelStatus::Staged);

        model.mark_production();
        assert_eq!(model.status, ModelStatus::Production);

        model.mark_deprecated();
        assert_eq!(model.status, ModelStatus::Deprecated);
        assert!(!model.is_active());
    }

    #[test]
    fn test_version_parsing() {
        let model = RegisteredModel::new("test", "1.2.3");
        assert_eq!(model.version_tuple(), Some((1, 2, 3)));

        let model2 = RegisteredModel::new("test", "invalid");
        assert_eq!(model2.version_tuple(), None);
    }

    #[test]
    fn test_version_comparison() {
        let older = RegisteredModel::new("test", "1.0.0");
        let newer = RegisteredModel::new("test", "1.1.0");

        assert_eq!(newer.is_newer_than(&older), Some(true));
        assert_eq!(older.is_newer_than(&newer), Some(false));
    }

    #[test]
    fn test_full_name() {
        let model = RegisteredModel::new("intent_classifier", "1.0.0");
        assert_eq!(model.full_name(), "intent_classifier:1.0.0");
    }

    #[test]
    fn test_model_metadata() {
        let metadata = ModelMetadata::classifier(768, 6, 10000)
            .architecture("MLP(768, 256, 6)")
            .dataset("train_v1", 50000)
            .tag("production")
            .tag("intent");

        assert_eq!(metadata.input_dim, 768);
        assert_eq!(metadata.output_dim, 6);
        assert_eq!(metadata.num_training_examples, 50000);
        assert_eq!(metadata.tags.len(), 2);
    }

    #[test]
    fn test_model_with_weights() {
        let model = RegisteredModel::new("test", "1.0.0").with_weights(
            "models/test/1.0.0/weights.bin",
            1024 * 1024,
            "abc123",
        );

        assert_eq!(
            model.weights_path,
            Some("models/test/1.0.0/weights.bin".to_string())
        );
        assert_eq!(model.weights_size, Some(1024 * 1024));
        assert_eq!(model.weights_hash, Some("abc123".to_string()));
    }

    #[test]
    fn test_model_with_parent() {
        let parent_id = Uuid::new_v4();
        let model = RegisteredModel::new("test", "1.0.0").with_parent(parent_id);

        assert_eq!(model.parent_id, Some(parent_id));
    }
}