aprender-orchestrate 0.31.2

Sovereign AI orchestration: autonomous agents, ML serving, code analysis, and transpilation pipelines
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! NumPy to Trueno conversion module (BATUTA-008)
//!
//! Converts Python NumPy operations to Rust Trueno operations with
//! automatic backend selection via MoE routing.
//!
//! # Conversion Strategy
//!
//! NumPy operations are mapped to equivalent Trueno operations:
//! - `np.array(...)` → `Vector::from_slice(...)` or `Matrix::from_slice(...)`
//! - `np.add(a, b)` → `a.add(&b)`
//! - `np.dot(a, b)` → `a.dot(&b)` or `a.matmul(&b)`
//! - `np.sum(a)` → `a.sum()`
//! - Element-wise ops automatically use MoE routing
//!
//! # Example
//!
//! ```python
//! # Python NumPy code
//! import numpy as np
//! a = np.array([1.0, 2.0, 3.0])
//! b = np.array([4.0, 5.0, 6.0])
//! c = np.add(a, b)
//! ```
//!
//! Converts to:
//!
//! ```rust,ignore
//! use trueno::Vector;
//! let a = Vector::from_slice(&[1.0, 2.0, 3.0]);
//! let b = Vector::from_slice(&[4.0, 5.0, 6.0]);
//! let c = a.add(&b).unwrap();
//! ```

use std::collections::HashMap;

/// NumPy operation types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum NumPyOp {
    /// Array creation: np.array, np.zeros, np.ones
    Array,
    /// Element-wise addition: np.add, a + b
    Add,
    /// Element-wise subtraction: np.subtract, a - b
    Subtract,
    /// Element-wise multiplication: np.multiply, a * b
    Multiply,
    /// Element-wise division: np.divide, a / b
    Divide,
    /// Dot product / matrix multiply: np.dot, np.matmul, a @ b
    Dot,
    /// Sum reduction: np.sum
    Sum,
    /// Mean reduction: np.mean
    Mean,
    /// Max reduction: np.max
    Max,
    /// Min reduction: np.min
    Min,
    /// Reshape: np.reshape
    Reshape,
    /// Transpose: np.transpose, a.T
    Transpose,
}

impl NumPyOp {
    /// Get the operation complexity for MoE routing
    pub fn complexity(&self) -> crate::backend::OpComplexity {
        use crate::backend::OpComplexity;

        match self {
            // Element-wise operations are Low complexity (memory-bound)
            NumPyOp::Add | NumPyOp::Subtract | NumPyOp::Multiply | NumPyOp::Divide => {
                OpComplexity::Low
            }
            // Reductions are Medium complexity
            NumPyOp::Sum | NumPyOp::Mean | NumPyOp::Max | NumPyOp::Min => OpComplexity::Medium,
            // Dot product and matrix ops are High complexity
            NumPyOp::Dot => OpComplexity::High,
            // Structural operations don't need backend selection
            NumPyOp::Array | NumPyOp::Reshape | NumPyOp::Transpose => OpComplexity::Low,
        }
    }
}

/// Trueno equivalent operation
#[derive(Debug, Clone)]
pub struct TruenoOp {
    /// Rust code template for the operation
    pub code_template: String,
    /// Required imports
    pub imports: Vec<String>,
    /// Operation complexity
    pub complexity: crate::backend::OpComplexity,
}

/// NumPy to Trueno converter
pub struct NumPyConverter {
    /// Operation mapping
    op_map: HashMap<NumPyOp, TruenoOp>,
    /// Backend selector for MoE routing
    backend_selector: crate::backend::BackendSelector,
}

impl Default for NumPyConverter {
    fn default() -> Self {
        Self::new()
    }
}

impl NumPyConverter {
    /// Create a new NumPy converter with default mappings
    pub fn new() -> Self {
        let mut op_map = HashMap::new();

        // Array operations
        op_map.insert(
            NumPyOp::Array,
            TruenoOp {
                code_template: "Vector::from_slice(&[{values}])".to_string(),
                imports: vec!["use trueno::Vector;".to_string()],
                complexity: crate::backend::OpComplexity::Low,
            },
        );

        // Element-wise operations
        op_map.insert(
            NumPyOp::Add,
            TruenoOp {
                code_template: "{lhs}.add(&{rhs}).unwrap()".to_string(),
                imports: vec!["use trueno::Vector;".to_string()],
                complexity: crate::backend::OpComplexity::Low,
            },
        );

        op_map.insert(
            NumPyOp::Subtract,
            TruenoOp {
                code_template: "{lhs}.sub(&{rhs}).unwrap()".to_string(),
                imports: vec!["use trueno::Vector;".to_string()],
                complexity: crate::backend::OpComplexity::Low,
            },
        );

        op_map.insert(
            NumPyOp::Multiply,
            TruenoOp {
                code_template: "{lhs}.mul(&{rhs}).unwrap()".to_string(),
                imports: vec!["use trueno::Vector;".to_string()],
                complexity: crate::backend::OpComplexity::Low,
            },
        );

        // Reductions
        op_map.insert(
            NumPyOp::Sum,
            TruenoOp {
                code_template: "{array}.sum()".to_string(),
                imports: vec!["use trueno::Vector;".to_string()],
                complexity: crate::backend::OpComplexity::Medium,
            },
        );

        op_map.insert(
            NumPyOp::Dot,
            TruenoOp {
                code_template: "{lhs}.dot(&{rhs}).unwrap()".to_string(),
                imports: vec!["use trueno::Vector;".to_string()],
                complexity: crate::backend::OpComplexity::High,
            },
        );

        Self { op_map, backend_selector: crate::backend::BackendSelector::new() }
    }

    /// Convert a NumPy operation to Trueno
    pub fn convert(&self, op: &NumPyOp) -> Option<&TruenoOp> {
        self.op_map.get(op)
    }

    /// Get recommended backend for an operation
    pub fn recommend_backend(&self, op: &NumPyOp, data_size: usize) -> crate::backend::Backend {
        self.backend_selector.select_with_moe(op.complexity(), data_size)
    }

    /// Get all available conversions
    pub fn available_ops(&self) -> Vec<&NumPyOp> {
        self.op_map.keys().collect()
    }

    /// Generate conversion report
    pub fn conversion_report(&self) -> String {
        let mut report = String::from("NumPy → Trueno Conversion Map\n");
        report.push_str("================================\n\n");

        for (op, trueno_op) in &self.op_map {
            report.push_str(&format!("{:?}:\n", op));
            report.push_str(&format!("  Complexity: {:?}\n", trueno_op.complexity));
            report.push_str(&format!("  Template: {}\n", trueno_op.code_template));
            report.push_str(&format!("  Imports: {}\n\n", trueno_op.imports.join(", ")));
        }

        report
    }
}

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

    #[test]
    fn test_converter_creation() {
        let converter = NumPyConverter::new();
        assert!(!converter.available_ops().is_empty());
    }

    #[test]
    fn test_operation_complexity() {
        assert_eq!(NumPyOp::Add.complexity(), crate::backend::OpComplexity::Low);
        assert_eq!(NumPyOp::Sum.complexity(), crate::backend::OpComplexity::Medium);
        assert_eq!(NumPyOp::Dot.complexity(), crate::backend::OpComplexity::High);
    }

    #[test]
    fn test_add_conversion() {
        let converter = NumPyConverter::new();
        let trueno_op = converter.convert(&NumPyOp::Add).expect("conversion failed");
        assert!(trueno_op.code_template.contains("add"));
        assert!(trueno_op.imports.iter().any(|i| i.contains("Vector")));
    }

    #[test]
    fn test_backend_recommendation() {
        let converter = NumPyConverter::new();

        // Small element-wise operation should use Scalar
        let backend = converter.recommend_backend(&NumPyOp::Add, 100);
        assert_eq!(backend, crate::backend::Backend::Scalar);

        // Large element-wise should use SIMD
        let backend = converter.recommend_backend(&NumPyOp::Add, 2_000_000);
        assert_eq!(backend, crate::backend::Backend::SIMD);

        // Large matrix operation should use GPU
        let backend = converter.recommend_backend(&NumPyOp::Dot, 50_000);
        assert_eq!(backend, crate::backend::Backend::GPU);
    }

    #[test]
    fn test_conversion_report() {
        let converter = NumPyConverter::new();
        let report = converter.conversion_report();
        assert!(report.contains("NumPy → Trueno"));
        assert!(report.contains("Add"));
        assert!(report.contains("Complexity"));
    }

    // ============================================================================
    // NUMPY OP ENUM TESTS
    // ============================================================================

    #[test]
    fn test_all_numpy_ops_exist() {
        // Test all 13 variants can be constructed
        let ops = vec![
            NumPyOp::Array,
            NumPyOp::Add,
            NumPyOp::Subtract,
            NumPyOp::Multiply,
            NumPyOp::Divide,
            NumPyOp::Dot,
            NumPyOp::Sum,
            NumPyOp::Mean,
            NumPyOp::Max,
            NumPyOp::Min,
            NumPyOp::Reshape,
            NumPyOp::Transpose,
        ];
        assert_eq!(ops.len(), 12); // 12 operations tested
    }

    #[test]
    fn test_op_equality() {
        assert_eq!(NumPyOp::Add, NumPyOp::Add);
        assert_ne!(NumPyOp::Add, NumPyOp::Multiply);
    }

    #[test]
    fn test_op_clone() {
        let op1 = NumPyOp::Dot;
        let op2 = op1.clone();
        assert_eq!(op1, op2);
    }

    #[test]
    fn test_complexity_low_ops() {
        let low_ops = vec![
            NumPyOp::Add,
            NumPyOp::Subtract,
            NumPyOp::Multiply,
            NumPyOp::Divide,
            NumPyOp::Array,
            NumPyOp::Reshape,
            NumPyOp::Transpose,
        ];

        for op in low_ops {
            assert_eq!(op.complexity(), crate::backend::OpComplexity::Low);
        }
    }

    #[test]
    fn test_complexity_medium_ops() {
        let medium_ops = vec![NumPyOp::Sum, NumPyOp::Mean, NumPyOp::Max, NumPyOp::Min];

        for op in medium_ops {
            assert_eq!(op.complexity(), crate::backend::OpComplexity::Medium);
        }
    }

    #[test]
    fn test_complexity_high_ops() {
        let high_ops = vec![NumPyOp::Dot];

        for op in high_ops {
            assert_eq!(op.complexity(), crate::backend::OpComplexity::High);
        }
    }

    // ============================================================================
    // TRUENO OP STRUCT TESTS
    // ============================================================================

    #[test]
    fn test_trueno_op_construction() {
        let op = TruenoOp {
            code_template: "test_template".to_string(),
            imports: vec!["use test;".to_string()],
            complexity: crate::backend::OpComplexity::Medium,
        };

        assert_eq!(op.code_template, "test_template");
        assert_eq!(op.imports.len(), 1);
        assert_eq!(op.complexity, crate::backend::OpComplexity::Medium);
    }

    #[test]
    fn test_trueno_op_clone() {
        let op1 = TruenoOp {
            code_template: "template".to_string(),
            imports: vec!["import".to_string()],
            complexity: crate::backend::OpComplexity::High,
        };

        let op2 = op1.clone();
        assert_eq!(op1.code_template, op2.code_template);
        assert_eq!(op1.imports, op2.imports);
        assert_eq!(op1.complexity, op2.complexity);
    }

    // ============================================================================
    // NUMPY CONVERTER TESTS
    // ============================================================================

    #[test]
    fn test_converter_default() {
        let converter = NumPyConverter::default();
        assert!(!converter.available_ops().is_empty());
    }

    #[test]
    fn test_convert_all_mapped_ops() {
        let converter = NumPyConverter::new();

        // Test all operations that should have mappings
        let mapped_ops = vec![
            NumPyOp::Array,
            NumPyOp::Add,
            NumPyOp::Subtract,
            NumPyOp::Multiply,
            NumPyOp::Sum,
            NumPyOp::Dot,
        ];

        for op in mapped_ops {
            assert!(converter.convert(&op).is_some(), "Missing mapping for {:?}", op);
        }
    }

    #[test]
    fn test_convert_unmapped_op() {
        let converter = NumPyConverter::new();

        // Divide, Mean, etc. might not be mapped
        // Just verify the function handles missing ops gracefully
        let result = converter.convert(&NumPyOp::Divide);
        // It's ok if this is None - we're testing the API works
        let _ = result;
    }

    #[test]
    fn test_array_conversion() {
        let converter = NumPyConverter::new();
        let op = converter.convert(&NumPyOp::Array).expect("conversion failed");

        assert!(op.code_template.contains("Vector"));
        assert!(op.code_template.contains("from_slice"));
        assert!(op.imports.iter().any(|i| i.contains("Vector")));
        assert_eq!(op.complexity, crate::backend::OpComplexity::Low);
    }

    #[test]
    fn test_subtract_conversion() {
        let converter = NumPyConverter::new();
        let op = converter.convert(&NumPyOp::Subtract).expect("conversion failed");

        assert!(op.code_template.contains("sub"));
        assert!(op.imports.iter().any(|i| i.contains("Vector")));
        assert_eq!(op.complexity, crate::backend::OpComplexity::Low);
    }

    #[test]
    fn test_multiply_conversion() {
        let converter = NumPyConverter::new();
        let op = converter.convert(&NumPyOp::Multiply).expect("conversion failed");

        assert!(op.code_template.contains("mul"));
        assert!(op.imports.iter().any(|i| i.contains("Vector")));
    }

    #[test]
    fn test_sum_conversion() {
        let converter = NumPyConverter::new();
        let op = converter.convert(&NumPyOp::Sum).expect("conversion failed");

        assert!(op.code_template.contains("sum"));
        assert_eq!(op.complexity, crate::backend::OpComplexity::Medium);
    }

    #[test]
    fn test_dot_conversion() {
        let converter = NumPyConverter::new();
        let op = converter.convert(&NumPyOp::Dot).expect("conversion failed");

        assert!(op.code_template.contains("dot"));
        assert_eq!(op.complexity, crate::backend::OpComplexity::High);
    }

    #[test]
    fn test_available_ops() {
        let converter = NumPyConverter::new();
        let ops = converter.available_ops();

        assert!(!ops.is_empty());
        // Should have at least the mapped operations
        assert!(ops.len() >= 6);
    }

    #[test]
    fn test_recommend_backend_element_wise_small() {
        let converter = NumPyConverter::new();

        // Small element-wise operations should use Scalar
        let backend = converter.recommend_backend(&NumPyOp::Add, 10);
        assert_eq!(backend, crate::backend::Backend::Scalar);
    }

    #[test]
    fn test_recommend_backend_element_wise_large() {
        let converter = NumPyConverter::new();

        // Large element-wise operations should use SIMD
        let backend = converter.recommend_backend(&NumPyOp::Multiply, 2_000_000);
        assert_eq!(backend, crate::backend::Backend::SIMD);
    }

    #[test]
    fn test_recommend_backend_reduction_medium() {
        let converter = NumPyConverter::new();

        // Medium-sized reductions should use SIMD
        let backend = converter.recommend_backend(&NumPyOp::Sum, 50_000);
        assert_eq!(backend, crate::backend::Backend::SIMD);
    }

    #[test]
    fn test_recommend_backend_reduction_large() {
        let converter = NumPyConverter::new();

        // Large reductions should use GPU
        let backend = converter.recommend_backend(&NumPyOp::Sum, 500_000);
        assert_eq!(backend, crate::backend::Backend::GPU);
    }

    #[test]
    fn test_recommend_backend_dot_product() {
        let converter = NumPyConverter::new();

        // Dot product with large data should use GPU
        let backend = converter.recommend_backend(&NumPyOp::Dot, 100_000);
        assert_eq!(backend, crate::backend::Backend::GPU);
    }

    #[test]
    fn test_conversion_report_structure() {
        let converter = NumPyConverter::new();
        let report = converter.conversion_report();

        // Check report contains expected sections
        assert!(report.contains("NumPy → Trueno"));
        assert!(report.contains("==="));
        assert!(report.contains("Complexity:"));
        assert!(report.contains("Template:"));
        assert!(report.contains("Imports:"));
    }

    #[test]
    fn test_conversion_report_has_all_ops() {
        let converter = NumPyConverter::new();
        let report = converter.conversion_report();

        // Spot check a few operations appear in report
        assert!(report.contains("Add") || report.contains("Sum") || report.contains("Dot"));
    }

    #[test]
    fn test_all_conversions_not_empty() {
        let converter = NumPyConverter::new();

        for op in converter.available_ops() {
            if let Some(trueno_op) = converter.convert(op) {
                assert!(!trueno_op.code_template.is_empty(), "Empty code template for {:?}", op);
                assert!(!trueno_op.imports.is_empty(), "Empty imports for {:?}", op);
            }
        }
    }

    #[test]
    fn test_imports_are_valid_rust() {
        let converter = NumPyConverter::new();

        for op in converter.available_ops() {
            if let Some(trueno_op) = converter.convert(op) {
                for import in &trueno_op.imports {
                    assert!(import.starts_with("use "), "Invalid import syntax: {}", import);
                    assert!(import.ends_with(';'), "Import missing semicolon: {}", import);
                }
            }
        }
    }

    #[test]
    fn test_all_ops_use_vector_import() {
        let converter = NumPyConverter::new();

        for op in converter.available_ops() {
            if let Some(trueno_op) = converter.convert(op) {
                assert!(
                    trueno_op.imports.iter().any(|i| i.contains("Vector")),
                    "Operation {:?} should import Vector",
                    op
                );
            }
        }
    }

    #[test]
    fn test_element_wise_ops_have_unwrap() {
        let converter = NumPyConverter::new();

        let element_wise = vec![NumPyOp::Add, NumPyOp::Subtract, NumPyOp::Multiply];

        for op in element_wise {
            if let Some(trueno_op) = converter.convert(&op) {
                assert!(
                    trueno_op.code_template.contains("unwrap"),
                    "Element-wise op {:?} should have unwrap() for error handling",
                    op
                );
            }
        }
    }

    #[test]
    fn test_complexity_matches_enum() {
        let converter = NumPyConverter::new();

        // Test that TruenoOp complexity matches NumPyOp complexity
        if let Some(add_op) = converter.convert(&NumPyOp::Add) {
            assert_eq!(add_op.complexity, NumPyOp::Add.complexity());
        }

        if let Some(sum_op) = converter.convert(&NumPyOp::Sum) {
            assert_eq!(sum_op.complexity, NumPyOp::Sum.complexity());
        }

        if let Some(dot_op) = converter.convert(&NumPyOp::Dot) {
            assert_eq!(dot_op.complexity, NumPyOp::Dot.complexity());
        }
    }
}