aprender-core 0.29.1

Next-generation machine learning library in pure Rust
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
#![allow(clippy::disallowed_methods)]
//! Sovereign AI Stack Integration Example
//!
//! Demonstrates the Pragmatic AI Labs Sovereign AI Stack integration:
//! - Stack components (alimentar, aprender, pacha, realizar, presentar, batuta)
//! - Model versioning and lifecycle management
//! - Derivation tracking (fine-tuning, distillation, quantization)
//! - Inference configuration
//! - Health monitoring
//!
//! Stack Architecture:
//! ```text
//! alimentar → aprender → pacha → realizar
//!     ↓           ↓          ↓         ↓
//!              presentar (WASM viz)
//!//!              batuta (orchestration)
//! ```
//!
//! Run with: `cargo run --example sovereign_stack`

use aprender::stack::{
    ComponentHealth, DerivationType, FormatCompatibility, HealthStatus, InferenceConfig,
    ModelStage, ModelVersion, QuantizationType, StackComponent, StackHealth,
};

fn main() {
    println!("=== Sovereign AI Stack Demo ===\n");

    // Part 1: Stack Components
    stack_components_demo();

    // Part 2: Model Lifecycle
    model_lifecycle_demo();

    // Part 3: Model Derivation (Lineage)
    derivation_demo();

    // Part 4: Inference Configuration
    inference_config_demo();

    // Part 5: Stack Health Monitoring
    health_monitoring_demo();

    // Part 6: Format Compatibility
    format_compatibility_demo();

    println!("\n=== Sovereign Stack Demo Complete! ===");
}

fn stack_components_demo() {
    println!("--- Part 1: Stack Components ---\n");

    println!(
        "{:<15} {:<15} {:<40} {:<8}",
        "Component", "Spanish", "Description", "Format"
    );
    println!("{}", "-".repeat(85));

    for component in StackComponent::all() {
        println!(
            "{:<15} {:<15} {:<40} {:<8}",
            component.name(),
            format!("({})", component.english()),
            component.description(),
            component.format().unwrap_or("-")
        );
    }

    println!("\nMagic Bytes (for format detection):");
    for component in StackComponent::all() {
        if let Some(magic) = component.magic() {
            println!(
                "  {}: {:02X} {:02X} {:02X} {:02X} (\"{}\")",
                component.name(),
                magic[0],
                magic[1],
                magic[2],
                magic[3],
                String::from_utf8_lossy(&magic)
            );
        }
    }

    println!("\nDisplay Format:");
    println!("  {}", StackComponent::Aprender);
    println!("  {}", StackComponent::Realizar);
    println!();
}

fn model_lifecycle_demo() {
    println!("--- Part 2: Model Lifecycle ---\n");

    // Show stage transitions
    println!("Model Stages and Valid Transitions:");
    let stages = [
        ModelStage::Development,
        ModelStage::Staging,
        ModelStage::Production,
        ModelStage::Archived,
    ];

    for from in &stages {
        let valid_to: Vec<_> = stages
            .iter()
            .filter(|to| from.can_transition_to(**to) && from != *to)
            .map(|s| s.name())
            .collect();
        println!("  {} -> {:?}", from, valid_to);
    }

    // Create and manage model versions
    println!("\nModel Version Examples:");

    let v1 = ModelVersion::new("1.0.0", [0xAB; 32])
        .with_stage(ModelStage::Production)
        .with_size(5_000_000)
        .with_quality_score(92.5)
        .with_tag("classification")
        .with_tag("iris");

    println!("\nVersion 1.0.0 (Production):");
    println!("  Version: {}", v1.version);
    println!("  Stage: {}", v1.stage);
    println!("  Size: {} bytes", v1.size_bytes);
    println!("  Quality: {:?}", v1.quality_score);
    println!("  Tags: {:?}", v1.tags);
    println!("  Hash: {}...", &v1.hash_hex()[..16]);
    println!("  Production Ready: {}", v1.is_production_ready());

    // Version not production ready
    let v2_dev = ModelVersion::new("2.0.0-beta", [0xCD; 32])
        .with_stage(ModelStage::Development)
        .with_quality_score(78.0);

    println!("\nVersion 2.0.0-beta (Development):");
    println!("  Stage: {}", v2_dev.stage);
    println!("  Quality: {:?}", v2_dev.quality_score);
    println!(
        "  Production Ready: {} (stage=dev)",
        v2_dev.is_production_ready()
    );

    let v2_low_quality = ModelVersion::new("2.0.0", [0xEF; 32])
        .with_stage(ModelStage::Production)
        .with_quality_score(70.0);

    println!("\nVersion 2.0.0 (Production, Low Quality):");
    println!("  Quality: {:?}", v2_low_quality.quality_score);
    println!(
        "  Production Ready: {} (quality < 85)",
        v2_low_quality.is_production_ready()
    );
    println!();
}

fn derivation_demo() {
    println!("--- Part 3: Model Derivation (Lineage) ---\n");

    let parent_hash = [0x11; 32];
    let teacher_hash = [0x22; 32];

    let derivations = [
        ("Original Training", DerivationType::Original),
        (
            "Fine-Tuned",
            DerivationType::FineTune {
                parent_hash,
                epochs: 10,
            },
        ),
        (
            "Distilled",
            DerivationType::Distillation {
                teacher_hash,
                temperature: 3.0,
            },
        ),
        (
            "Merged (TIES)",
            DerivationType::Merge {
                parent_hashes: vec![[0x33; 32], [0x44; 32]],
                method: "TIES".into(),
            },
        ),
        (
            "Quantized (INT8)",
            DerivationType::Quantize {
                parent_hash,
                quant_type: QuantizationType::Int8,
            },
        ),
        (
            "Pruned (50%)",
            DerivationType::Prune {
                parent_hash,
                sparsity: 0.5,
            },
        ),
    ];

    println!(
        "{:<20} {:<15} {:<10} {:>12}",
        "Description", "Type", "Derived?", "Parents"
    );
    println!("{}", "-".repeat(60));

    for (desc, deriv) in &derivations {
        println!(
            "{:<20} {:<15} {:<10} {:>12}",
            desc,
            deriv.type_name(),
            deriv.is_derived(),
            deriv.parent_hashes().len()
        );
    }

    // Quantization types
    println!("\nQuantization Types:");
    let quant_types = [
        QuantizationType::Int8,
        QuantizationType::Int4,
        QuantizationType::Float16,
        QuantizationType::BFloat16,
        QuantizationType::Dynamic,
        QuantizationType::QAT,
    ];

    println!("{:<12} {:>8} {:>20}", "Type", "Bits", "Name");
    println!("{}", "-".repeat(45));

    for qt in &quant_types {
        println!(
            "{:<12} {:>8} {:>20}",
            format!("{:?}", qt),
            qt.bits(),
            qt.name()
        );
    }

    // Model version with derivation
    println!("\nModel with Derivation Info:");
    let derived = ModelVersion::new("1.1.0", [0x55; 32])
        .with_stage(ModelStage::Staging)
        .with_derivation(DerivationType::FineTune {
            parent_hash,
            epochs: 5,
        })
        .with_quality_score(88.0)
        .with_tag("fine-tuned");

    println!("  Version: {}", derived.version);
    println!("  Derivation: {}", derived.derivation.type_name());
    println!(
        "  Parent Count: {}",
        derived.derivation.parent_hashes().len()
    );
    println!();
}

fn inference_config_demo() {
    println!("--- Part 4: Inference Configuration ---\n");

    // Default configuration
    let default = InferenceConfig::default();
    println!("Default Configuration:");
    println!("  Model: {:?}", default.model_path);
    println!("  Port: {}", default.port);
    println!("  Max Batch: {}", default.max_batch_size);
    println!("  Timeout: {} ms", default.timeout_ms);
    println!("  CORS: {}", default.enable_cors);
    println!("  Metrics: {:?}", default.metrics_path);
    println!("  Health: {:?}", default.health_path);

    // Custom configuration
    let custom = InferenceConfig::new("/models/iris_rf.apr")
        .with_port(9000)
        .with_batch_size(64)
        .with_timeout_ms(50)
        .without_cors();

    println!("\nCustom Configuration:");
    println!("  Model: {:?}", custom.model_path);
    println!("  Port: {}", custom.port);
    println!("  Max Batch: {}", custom.max_batch_size);
    println!("  Timeout: {} ms", custom.timeout_ms);
    println!("  CORS: {}", custom.enable_cors);

    // Endpoint URLs
    println!("\nInference Endpoints:");
    println!("  Predict: {}", custom.predict_url());
    println!("  Batch:   {}", custom.batch_predict_url());
    println!();
}

fn health_monitoring_demo() {
    println!("--- Part 5: Stack Health Monitoring ---\n");

    // Create stack health monitor
    let mut health = StackHealth::new();

    // Simulate component health checks
    health.set_component(
        StackComponent::Aprender,
        ComponentHealth::healthy("0.27.0").with_response_time(5),
    );

    health.set_component(
        StackComponent::Pacha,
        ComponentHealth::healthy("1.0.0").with_response_time(12),
    );

    health.set_component(
        StackComponent::Realizar,
        ComponentHealth::degraded("0.8.0", "high latency").with_response_time(250),
    );

    health.set_component(
        StackComponent::Presentar,
        ComponentHealth::unhealthy("connection refused"),
    );

    // Display health status
    println!("Stack Health Status:");
    println!(
        "  Overall: {} (operational: {})",
        health.overall,
        health.overall.is_operational()
    );

    println!("\nComponent Status:");
    println!(
        "{:<15} {:<12} {:<10} {:>12} {}",
        "Component", "Status", "Version", "Latency", "Error"
    );
    println!("{}", "-".repeat(70));

    for component in StackComponent::all() {
        if let Some(ch) = health.components.get(component) {
            println!(
                "{:<15} {:<12} {:<10} {:>10} {}",
                component.name(),
                ch.status.name(),
                ch.version.as_deref().unwrap_or("-"),
                ch.response_time_ms
                    .map(|ms| format!("{} ms", ms))
                    .unwrap_or_else(|| "-".into()),
                ch.error.as_deref().unwrap_or("")
            );
        }
    }

    // Health status types
    println!("\nHealth Status Types:");
    let statuses = [
        HealthStatus::Healthy,
        HealthStatus::Degraded,
        HealthStatus::Unhealthy,
        HealthStatus::Unknown,
    ];

    for status in &statuses {
        println!(
            "  {:<12}: operational={}",
            status.name(),
            status.is_operational()
        );
    }
    println!();
}

fn format_compatibility_demo() {
    println!("--- Part 6: Format Compatibility ---\n");

    let compat = FormatCompatibility::current();

    println!("Current Format Versions:");
    println!("  APR: {}.{}", compat.apr_version.0, compat.apr_version.1);
    println!("  ALD: {}.{}", compat.ald_version.0, compat.ald_version.1);
    println!("  Compatible: {}", compat.compatible);

    // Version compatibility checks
    println!("\nAPR Version Compatibility:");
    let apr_versions = [(1, 0), (1, 1), (2, 0)];
    for (major, minor) in apr_versions {
        println!(
            "  APR {}.{}: {}",
            major,
            minor,
            if compat.is_apr_compatible(major, minor) {
                "compatible"
            } else {
                "NOT compatible"
            }
        );
    }

    println!("\nALD Version Compatibility:");
    let ald_versions = [(1, 0), (1, 2), (1, 3), (2, 0)];
    for (major, minor) in ald_versions {
        println!(
            "  ALD {}.{}: {}",
            major,
            minor,
            if compat.is_ald_compatible(major, minor) {
                "compatible"
            } else {
                "NOT compatible"
            }
        );
    }

    // Stack architecture summary
    println!("\n=== Stack Architecture Summary ===\n");
    println!("Data Flow:");
    println!("  alimentar (.ald) - Load and transform data");
    println!("");
    println!("  aprender (.apr) - Train ML models");
    println!("");
    println!("  pacha - Registry with versioning and lineage");
    println!("");
    println!("  realizar - Pure Rust inference engine");
    println!("");
    println!("  presentar - WASM visualization");
    println!("");
    println!("  batuta (.bat) - Orchestration and oracle mode");

    println!("\nDesign Principles:");
    println!("  - Pure Rust: Zero cloud dependencies");
    println!("  - Format Independence: Each tool has its own binary format");
    println!("  - Toyota Way: Jidoka, Muda elimination, Kaizen");
    println!();
}