Skip to main content

ai3_lib/
lib.rs

1pub mod esp_compat;
2pub mod mining;
3pub mod operations;
4pub mod tensor;
5
6pub use esp_compat::{ESPCompatibility, ESPDeviceType, ESPMiningConfig};
7pub use mining::{MinerCapabilities, MinerStats, MiningResult, MiningTask, TaskDistributor};
8pub use operations::{ActivationFunction, Convolution, MatrixMultiply, TensorOp, VectorOp};
9pub use tensor::{Tensor, TensorData, TensorShape};
10
11use pot_o_core::TribeResult;
12use std::sync::{Arc, Mutex};
13use std::time::{Duration, Instant};
14
15/// Main AI3 Engine -- coordinates tensor operations and mining tasks.
16/// Ported from .AI3 ai3-lib with PoT-O extensions.
17pub struct AI3Engine {
18    pub task_distributor: TaskDistributor,
19    performance_stats: Arc<Mutex<EngineStats>>,
20    config: EngineConfig,
21}
22
23#[derive(Debug, Clone)]
24pub struct EngineConfig {
25    pub max_concurrent_tasks: usize,
26    pub task_timeout: Duration,
27    pub enable_esp_support: bool,
28    pub auto_optimize_tensors: bool,
29}
30
31impl Default for EngineConfig {
32    fn default() -> Self {
33        Self {
34            max_concurrent_tasks: 10,
35            task_timeout: Duration::from_secs(30),
36            enable_esp_support: true,
37            auto_optimize_tensors: true,
38        }
39    }
40}
41
42#[derive(Debug, Clone)]
43pub struct EngineStats {
44    pub total_tasks_processed: u64,
45    pub successful_tasks: u64,
46    pub failed_tasks: u64,
47    pub average_task_time: Duration,
48    pub total_compute_time: Duration,
49    pub start_time: Instant,
50}
51
52impl Default for EngineStats {
53    fn default() -> Self {
54        Self {
55            total_tasks_processed: 0,
56            successful_tasks: 0,
57            failed_tasks: 0,
58            average_task_time: Duration::ZERO,
59            total_compute_time: Duration::ZERO,
60            start_time: Instant::now(),
61        }
62    }
63}
64
65impl AI3Engine {
66    pub fn new() -> Self {
67        Self::with_config(EngineConfig::default())
68    }
69
70    pub fn with_config(config: EngineConfig) -> Self {
71        Self {
72            task_distributor: TaskDistributor::new(),
73            performance_stats: Arc::new(Mutex::new(EngineStats::default())),
74            config,
75        }
76    }
77
78    /// Execute a tensor operation from a mining task and return the result tensor.
79    pub fn execute_task(&self, task: &MiningTask) -> TribeResult<Tensor> {
80        let op = operations::parse_operation(&task.operation_type)?;
81        let input = task
82            .input_tensors
83            .first()
84            .ok_or_else(|| pot_o_core::TribeError::TensorError("No input tensors".into()))?;
85
86        let result = op.execute(input)?;
87
88        if self.config.auto_optimize_tensors && self.config.enable_esp_support {
89            let max_dim = pot_o_core::ESP_MAX_TENSOR_DIM;
90            Ok(result.clamp_dimensions(max_dim))
91        } else {
92            Ok(result)
93        }
94    }
95
96    pub fn get_stats(&self) -> EngineStats {
97        self.performance_stats
98            .lock()
99            .map(|s| s.clone())
100            .unwrap_or_default()
101    }
102
103    pub fn record_result(&self, success: bool, duration: Duration) {
104        if let Ok(mut stats) = self.performance_stats.lock() {
105            stats.total_tasks_processed += 1;
106            if success {
107                stats.successful_tasks += 1;
108            } else {
109                stats.failed_tasks += 1;
110            }
111            stats.total_compute_time += duration;
112            if stats.total_tasks_processed > 0 {
113                stats.average_task_time =
114                    stats.total_compute_time / stats.total_tasks_processed as u32;
115            }
116        }
117    }
118}
119
120impl Default for AI3Engine {
121    fn default() -> Self {
122        Self::new()
123    }
124}