Skip to main content

qualia_core_db/specialized_libs/machine_learning/
inference.rs

1//! Inference engine, scheduling, batching, and tuning impls.
2
3use super::*;
4#[allow(unused_imports)]
5use serde::{Deserialize, Serialize};
6#[allow(unused_imports)]
7use std::collections::HashMap;
8
9impl InferenceEngine {
10    /// Construct a `InferenceEngine` with default field values.
11    pub fn new() -> Self {
12        Self {
13            inference_backends: HashMap::new(),
14            request_scheduler: RequestScheduler::new(),
15            batch_processor: BatchProcessor::new(),
16            performance_optimizer: InferenceOptimizer::new(),
17        }
18    }
19
20    /// Initialize `InferenceEngine`; `Ok` when ready.
21    pub fn initialize(&mut self) -> Result<(), MLError> {
22        self.request_scheduler.initialize()?;
23        self.batch_processor.initialize()?;
24        self.performance_optimizer.initialize()?;
25        Ok(())
26    }
27
28    /// Register an inference backend under its backend id.
29    pub fn register_backend(&mut self, backend: InferenceBackend) {
30        self.inference_backends
31            .insert(backend.backend_id.clone(), backend);
32    }
33
34    /// Get a registered inference backend by id.
35    pub fn get_backend(&self, backend_id: &str) -> Option<&InferenceBackend> {
36        self.inference_backends.get(backend_id)
37    }
38
39    /// List the ids of all registered inference backends.
40    pub fn list_backends(&self) -> Vec<String> {
41        self.inference_backends.keys().cloned().collect()
42    }
43
44    /// Remove a registered inference backend by id. Returns `true` if removed.
45    pub fn remove_backend(&mut self, backend_id: &str) -> bool {
46        self.inference_backends.remove(backend_id).is_some()
47    }
48
49    /// Real MLP forward pass: decodes `request.input_data` as LE f64s,
50    /// runs the model's `Linear`/`Activation`/`Dropout` layers via
51    /// `solvers::activation`, re-encodes the output. Errors on
52    /// unsupported layer types; `confidence: 1.0` marks a completed
53    /// deterministic pass, not calibrated confidence (use
54    /// `gguf_bridge` for LLM inference).
55    pub fn execute_inference(
56        &mut self,
57        request: &InferenceRequest,
58        model: &Model,
59    ) -> Result<InferenceResult, MLError> {
60        // Wired to a real (if basic) forward pass over the model's architecture, using the
61        // model's `weights` field as the flattened parameter buffer and the element-wise
62        // activation math from `crate::solvers::activation`. This is an MLP inference backend:
63        // it supports `Linear` (and pass-through `Activation`/`Dropout`) layers and returns a
64        // clear error for layer types it cannot yet evaluate (Convolutional, Attention, …).
65        // For production autoregressive LLM inference, route to the native gguf_bridge engine.
66        let start = std::time::Instant::now();
67
68        // Decode the request's byte payload as a little-endian f64 input vector.
69        let input = decode_f64_le(&request.input_data).ok_or_else(|| {
70            MLError::DataError(format!(
71                "input_data length ({}) is not a multiple of {} (f64 size)",
72                request.input_data.len(),
73                std::mem::size_of::<f64>()
74            ))
75        })?;
76
77        // Run the forward pass over the model's layers.
78        let output = Self::forward_pass(model, &input)?;
79
80        // Re-encode the output vector as little-endian f64 bytes.
81        let output_data = encode_f64_le(&output);
82
83        let inference_time = start.elapsed().as_millis() as u64;
84
85        Ok(InferenceResult {
86            result_id: format!("result_{}", request.request_id),
87            output_data,
88            inference_time,
89            // This backend computes a deterministic forward pass, not a probabilistic model,
90            // so there is no calibrated confidence to report. Surface 1.0 to indicate the pass
91            // completed successfully (callers needing real confidence should use gguf_bridge).
92            confidence: 1.0,
93            metadata: ResultMetadata {
94                model_id: model.model_id.clone(),
95                backend_id: "linear_algebra_mlp".to_string(),
96                batch_size: request.parameters.batch_size,
97                sequence_length: request.parameters.sequence_length,
98                tokens_generated: output.len(),
99            },
100        })
101    }
102
103    /// Run a basic MLP forward pass over the model's architecture.
104    ///
105    /// For each `Linear` layer the flattened `model.weights` buffer is consumed in order:
106    /// first the `output_size × input_size` weight matrix (row-major), then the `output_size`
107    /// bias vector. The layer output is `activation(W · x + b)`, with the activation drawn from
108    /// `crate::solvers::activation`. `Activation` layers apply their activation in place and
109    /// `Dropout` is the identity at inference time. All other layer types return a clear error.
110    pub(super) fn forward_pass(model: &Model, input: &[f64]) -> Result<Vec<f64>, MLError> {
111        let layers = &model.architecture.layers;
112        if layers.is_empty() {
113            return Err(MLError::InferenceError(
114                "model architecture has no layers".to_string(),
115            ));
116        }
117
118        let mut activations = input.to_vec();
119        let mut weight_offset = 0usize;
120
121        for (idx, layer) in layers.iter().enumerate() {
122            match layer.layer_type {
123                LayerType::Linear => {
124                    let in_size = layer.input_shape.first().copied().ok_or_else(|| {
125                        MLError::InferenceError(format!(
126                            "layer {} ({}): missing input dimension",
127                            idx, layer.layer_id
128                        ))
129                    })?;
130                    let out_size = layer.output_shape.first().copied().ok_or_else(|| {
131                        MLError::InferenceError(format!(
132                            "layer {} ({}): missing output dimension",
133                            idx, layer.layer_id
134                        ))
135                    })?;
136
137                    if activations.len() != in_size {
138                        return Err(MLError::InferenceError(format!(
139                            "layer {} ({}): expected input size {}, got {}",
140                            idx,
141                            layer.layer_id,
142                            in_size,
143                            activations.len()
144                        )));
145                    }
146
147                    let weight_count = in_size * out_size;
148                    let bias_count = out_size;
149                    let needed = weight_count + bias_count;
150                    if weight_offset + needed > model.weights.len() {
151                        return Err(MLError::InferenceError(format!(
152                            "layer {} ({}): not enough weights (need {} at offset {}, have {})",
153                            idx,
154                            layer.layer_id,
155                            needed,
156                            weight_offset,
157                            model.weights.len()
158                        )));
159                    }
160
161                    // output[j] = sum_i W[j*in_size + i] * x[i] + bias[j]
162                    let mut out = vec![0.0f64; out_size];
163                    for j in 0..out_size {
164                        let mut acc = 0.0;
165                        for i in 0..in_size {
166                            acc += model.weights[weight_offset + j * in_size + i] * activations[i];
167                        }
168                        acc += model.weights[weight_offset + weight_count + j];
169                        out[j] = acc;
170                    }
171                    weight_offset += needed;
172
173                    if let Some(act) = &layer.activation {
174                        apply_activation(&mut out, act)?;
175                    }
176                    activations = out;
177                }
178                LayerType::Activation => {
179                    if let Some(act) = &layer.activation {
180                        apply_activation(&mut activations, act)?;
181                    } else {
182                        return Err(MLError::InferenceError(format!(
183                            "layer {} ({}): Activation layer has no activation function set",
184                            idx, layer.layer_id
185                        )));
186                    }
187                }
188                LayerType::Dropout => {
189                    // Dropout is the identity at inference time.
190                }
191                ref other => {
192                    return Err(MLError::InferenceError(format!(
193                        "layer {} ({}): {:?} layers are not yet supported by the MLP inference \
194                         backend (only Linear/Activation/Dropout); use the native gguf_bridge \
195                         engine for transformer/cnn workloads",
196                        idx, layer.layer_id, other
197                    )));
198                }
199            }
200        }
201
202        Ok(activations)
203    }
204}
205
206/// Decode a byte slice as a little-endian `f64` vector. Returns `None` if the length is not a
207/// multiple of 8 (the size of `f64`).
208fn decode_f64_le(bytes: &[u8]) -> Option<Vec<f64>> {
209    if bytes.len() % std::mem::size_of::<f64>() != 0 {
210        return None;
211    }
212    Some(
213        bytes
214            .chunks_exact(std::mem::size_of::<f64>())
215            .map(|c| f64::from_le_bytes(c.try_into().unwrap()))
216            .collect(),
217    )
218}
219
220/// Encode an `f64` slice as a little-endian byte vector.
221fn encode_f64_le(values: &[f64]) -> Vec<u8> {
222    let mut out = Vec::with_capacity(values.len() * std::mem::size_of::<f64>());
223    for v in values {
224        out.extend_from_slice(&v.to_le_bytes());
225    }
226    out
227}
228
229/// Apply an activation function in place, dispatching to `crate::solvers::activation` for the
230/// standard element-wise maps.
231fn apply_activation(buf: &mut [f64], act: &ActivationFunction) -> Result<(), MLError> {
232    match act {
233        ActivationFunction::ReLU => crate::solvers::activation::relu(buf),
234        ActivationFunction::Sigmoid => crate::solvers::activation::sigmoid(buf),
235        ActivationFunction::Tanh => crate::solvers::activation::tanh(buf),
236        ActivationFunction::GELU => crate::solvers::activation::gelu(buf),
237        ActivationFunction::Softmax => crate::solvers::activation::softmax(buf),
238        ActivationFunction::Swish => crate::solvers::activation::silu(buf),
239        ActivationFunction::LeakyReLU => {
240            // Leaky ReLU: x if x >= 0 else 0.01·x, element-wise.
241            const SLOPE: f64 = 0.01;
242            for v in buf.iter_mut() {
243                if *v < 0.0 {
244                    *v *= SLOPE;
245                }
246            }
247        }
248        ActivationFunction::ELU => {
249            // ELU: x if x >= 0 else e^x − 1, element-wise (α = 1).
250            for v in buf.iter_mut() {
251                if *v < 0.0 {
252                    *v = (*v).exp() - 1.0;
253                }
254            }
255        }
256        ActivationFunction::Custom(name) => {
257            return Err(MLError::InferenceError(format!(
258                "custom activation '{}' is not supported by the MLP inference backend",
259                name
260            )));
261        }
262    }
263    Ok(())
264}
265
266impl InferenceBackend {
267    /// Construct a `InferenceBackend` with default field values.
268    pub fn new() -> Self {
269        Self {
270            backend_id: "backend_1".to_string(),
271            backend_type: InferenceBackendType::GPU,
272            capabilities: BackendCapabilities::new(),
273            current_load: 0.5,
274        }
275    }
276}
277
278impl BackendCapabilities {
279    /// Construct a `BackendCapabilities` with default field values.
280    pub fn new() -> Self {
281        Self {
282            supported_models: vec!["gpt-3".to_string(), "bert".to_string()],
283            max_batch_size: 32,
284            max_sequence_length: 2048,
285            supported_precisions: vec![Precision::FP16, Precision::FP32],
286            memory_limit: 8 * 1024 * 1024 * 1024, // 8GB
287            throughput: 100.0,
288        }
289    }
290}
291
292impl RequestScheduler {
293    /// Construct a `RequestScheduler` with default field values.
294    pub fn new() -> Self {
295        Self {
296            scheduling_policy: SchedulingPolicy::Priority,
297            queue_manager: QueueManager::new(),
298            load_balancer: LoadBalancer::new(),
299        }
300    }
301
302    /// Initialize `RequestScheduler`; `Ok` when ready.
303    pub fn initialize(&mut self) -> Result<(), MLError> {
304        Ok(())
305    }
306
307    /// Return the current scheduling policy.
308    pub fn scheduling_policy(&self) -> &SchedulingPolicy {
309        &self.scheduling_policy
310    }
311
312    /// Set the scheduling policy.
313    pub fn set_scheduling_policy(&mut self, policy: SchedulingPolicy) {
314        self.scheduling_policy = policy;
315    }
316
317    /// Return a reference to the queue manager.
318    pub fn queue_manager(&self) -> &QueueManager {
319        &self.queue_manager
320    }
321
322    /// Return a mutable reference to the queue manager.
323    pub fn queue_manager_mut(&mut self) -> &mut QueueManager {
324        &mut self.queue_manager
325    }
326
327    /// Return a reference to the load balancer.
328    pub fn load_balancer(&self) -> &LoadBalancer {
329        &self.load_balancer
330    }
331
332    /// Return a mutable reference to the load balancer.
333    pub fn load_balancer_mut(&mut self) -> &mut LoadBalancer {
334        &mut self.load_balancer
335    }
336
337    /// Schedule an inference request to an available backend according to the configured
338    /// load-balancing strategy and record it in the queue manager.
339    pub fn schedule_request(&mut self, request: &InferenceRequest) -> Result<String, MLError> {
340        let selected_backend = if !self.load_balancer.backend_metrics.is_empty() {
341            match self.load_balancer.balancing_strategy {
342                LoadBalancingStrategy::LeastConnections => self
343                    .load_balancer
344                    .backend_metrics
345                    .values()
346                    .min_by(|a, b| {
347                        a.current_load
348                            .partial_cmp(&b.current_load)
349                            .unwrap_or(std::cmp::Ordering::Equal)
350                    })
351                    .map(|m| m.backend_id.clone())
352                    .unwrap_or_else(|| "backend_1".to_string()),
353                LoadBalancingStrategy::ResponseTime => self
354                    .load_balancer
355                    .backend_metrics
356                    .values()
357                    .min_by(|a, b| {
358                        a.average_response_time
359                            .partial_cmp(&b.average_response_time)
360                            .unwrap_or(std::cmp::Ordering::Equal)
361                    })
362                    .map(|m| m.backend_id.clone())
363                    .unwrap_or_else(|| "backend_1".to_string()),
364                LoadBalancingStrategy::RoundRobin
365                | LoadBalancingStrategy::WeightedRoundRobin
366                | LoadBalancingStrategy::Custom(_) => {
367                    let mut keys: Vec<String> =
368                        self.load_balancer.backend_metrics.keys().cloned().collect();
369                    keys.sort();
370                    let idx = (self.queue_manager.running_count()
371                        + self.queue_manager.completed_requests.len())
372                        % keys.len();
373                    keys[idx].clone()
374                }
375            }
376        } else if !self.load_balancer.health_checker.health_checks.is_empty() {
377            let mut keys: Vec<String> = self
378                .load_balancer
379                .health_checker
380                .health_checks
381                .keys()
382                .cloned()
383                .collect();
384            keys.sort();
385            keys[0].clone()
386        } else {
387            "backend_1".to_string()
388        };
389
390        let now = std::time::SystemTime::now()
391            .duration_since(std::time::UNIX_EPOCH)
392            .map(|d| d.as_secs())
393            .unwrap_or(0);
394
395        self.queue_manager.start_request(RunningRequest {
396            request_id: request.request_id.clone(),
397            backend_id: selected_backend.clone(),
398            started_at: now,
399            progress: 0.0,
400        });
401
402        if let Some(metrics) = self.load_balancer.backend_metrics.get_mut(&selected_backend) {
403            metrics.current_load += 1.0;
404        }
405
406        Ok(selected_backend)
407    }
408}
409
410impl QueueManager {
411    /// Construct a `QueueManager` with default field values.
412    pub fn new() -> Self {
413        Self {
414            pending_requests: Vec::new(),
415            running_requests: HashMap::new(),
416            completed_requests: Vec::new(),
417        }
418    }
419
420    /// Enqueue a pending inference request.
421    pub fn enqueue(&mut self, request: InferenceRequest) {
422        self.pending_requests.push(request);
423    }
424
425    /// Dequeue the next pending request (FIFO order).
426    pub fn dequeue(&mut self) -> Option<InferenceRequest> {
427        if self.pending_requests.is_empty() {
428            None
429        } else {
430            Some(self.pending_requests.remove(0))
431        }
432    }
433
434    /// Mark a request as running on a given backend.
435    pub fn start_request(&mut self, running: RunningRequest) {
436        self.running_requests
437            .insert(running.request_id.clone(), running);
438    }
439
440    /// Mark a running request as completed, removing it from the running set
441    /// and appending it to the completed list.
442    pub fn complete_request(&mut self, request_id: &str, completed: CompletedRequest) {
443        self.running_requests.remove(request_id);
444        self.completed_requests.push(completed);
445    }
446
447    /// Return a reference to the pending requests queue.
448    pub fn pending_requests(&self) -> &[InferenceRequest] {
449        &self.pending_requests
450    }
451
452    /// Return a reference to the running requests map.
453    pub fn running_requests(&self) -> &HashMap<String, RunningRequest> {
454        &self.running_requests
455    }
456
457    /// Return a reference to the completed requests list.
458    pub fn completed_requests(&self) -> &[CompletedRequest] {
459        &self.completed_requests
460    }
461
462    /// Number of pending requests.
463    pub fn pending_count(&self) -> usize {
464        self.pending_requests.len()
465    }
466
467    /// Number of currently running requests.
468    pub fn running_count(&self) -> usize {
469        self.running_requests.len()
470    }
471}
472
473impl LoadBalancer {
474    /// Construct a `LoadBalancer` with default field values.
475    pub fn new() -> Self {
476        Self {
477            balancing_strategy: LoadBalancingStrategy::RoundRobin,
478            backend_metrics: HashMap::new(),
479            health_checker: HealthChecker::new(),
480        }
481    }
482
483    /// Return the current load-balancing strategy.
484    pub fn balancing_strategy(&self) -> &LoadBalancingStrategy {
485        &self.balancing_strategy
486    }
487
488    /// Set the load-balancing strategy.
489    pub fn set_balancing_strategy(&mut self, strategy: LoadBalancingStrategy) {
490        self.balancing_strategy = strategy;
491    }
492
493    /// Record or update metrics for a backend.
494    pub fn record_backend_metrics(&mut self, metrics: BackendMetrics) {
495        self.backend_metrics
496            .insert(metrics.backend_id.clone(), metrics);
497    }
498
499    /// Get metrics for a specific backend.
500    pub fn get_backend_metrics(&self, backend_id: &str) -> Option<&BackendMetrics> {
501        self.backend_metrics.get(backend_id)
502    }
503
504    /// List the ids of all backends with recorded metrics.
505    pub fn list_backend_metrics(&self) -> Vec<String> {
506        self.backend_metrics.keys().cloned().collect()
507    }
508
509    /// Return a reference to the health checker.
510    pub fn health_checker(&self) -> &HealthChecker {
511        &self.health_checker
512    }
513
514    /// Return a mutable reference to the health checker.
515    pub fn health_checker_mut(&mut self) -> &mut HealthChecker {
516        &mut self.health_checker
517    }
518}
519
520impl HealthChecker {
521    /// Construct a `HealthChecker` with default field values.
522    pub fn new() -> Self {
523        Self {
524            health_checks: HashMap::new(),
525            check_interval: 30, // 30 seconds
526            timeout: 5,         // 5 seconds
527        }
528    }
529
530    /// Register a health check under its check id.
531    pub fn register_health_check(&mut self, check: HealthCheck) {
532        self.health_checks.insert(check.check_id.clone(), check);
533    }
534
535    /// Get a registered health check by id.
536    pub fn get_health_check(&self, check_id: &str) -> Option<&HealthCheck> {
537        self.health_checks.get(check_id)
538    }
539
540    /// List the ids of all registered health checks.
541    pub fn list_health_checks(&self) -> Vec<String> {
542        self.health_checks.keys().cloned().collect()
543    }
544
545    /// Remove a registered health check by id. Returns `true` if removed.
546    pub fn remove_health_check(&mut self, check_id: &str) -> bool {
547        self.health_checks.remove(check_id).is_some()
548    }
549
550    /// Return the check interval (seconds).
551    pub fn check_interval(&self) -> u64 {
552        self.check_interval
553    }
554
555    /// Set the check interval (seconds).
556    pub fn set_check_interval(&mut self, interval: u64) {
557        self.check_interval = interval;
558    }
559
560    /// Return the timeout (seconds).
561    pub fn timeout(&self) -> u64 {
562        self.timeout
563    }
564
565    /// Set the timeout (seconds).
566    pub fn set_timeout(&mut self, timeout: u64) {
567        self.timeout = timeout;
568    }
569}
570
571impl HealthCheck {
572    /// Construct a `HealthCheck` with default field values.
573    pub fn new() -> Self {
574        Self {
575            check_id: "health_1".to_string(),
576            check_type: HealthCheckType::HTTP,
577            endpoint: "/health".to_string(),
578            expected_response: "OK".to_string(),
579        }
580    }
581}
582
583impl BatchProcessor {
584    /// Construct a `BatchProcessor` with default field values.
585    pub fn new() -> Self {
586        Self {
587            batching_strategy: BatchingStrategy::FixedSize,
588            batch_size: 32,
589            batch_timeout: 100, // 100ms
590            batch_optimizer: BatchOptimizer::new(),
591        }
592    }
593
594    /// Initialize `BatchProcessor`; `Ok` when ready.
595    pub fn initialize(&mut self) -> Result<(), MLError> {
596        self.batch_optimizer.initialize()?;
597        Ok(())
598    }
599
600    /// Return the current batching strategy.
601    pub fn batching_strategy(&self) -> &BatchingStrategy {
602        &self.batching_strategy
603    }
604
605    /// Set the batching strategy.
606    pub fn set_batching_strategy(&mut self, strategy: BatchingStrategy) {
607        self.batching_strategy = strategy;
608    }
609
610    /// Return the configured batch size.
611    pub fn batch_size(&self) -> usize {
612        self.batch_size
613    }
614
615    /// Set the batch size.
616    pub fn set_batch_size(&mut self, size: usize) {
617        self.batch_size = size;
618    }
619
620    /// Return the configured batch timeout (milliseconds).
621    pub fn batch_timeout(&self) -> u64 {
622        self.batch_timeout
623    }
624
625    /// Set the batch timeout (milliseconds).
626    pub fn set_batch_timeout(&mut self, timeout: u64) {
627        self.batch_timeout = timeout;
628    }
629}
630
631impl BatchOptimizer {
632    /// Construct a `BatchOptimizer` with default field values.
633    pub fn new() -> Self {
634        Self {
635            optimization_algorithms: HashMap::new(),
636            optimization_metrics: BatchOptimizationMetrics::new(),
637        }
638    }
639
640    /// Initialize `BatchOptimizer`; `Ok` when ready.
641    pub fn initialize(&mut self) -> Result<(), MLError> {
642        Ok(())
643    }
644
645    /// Register a batch optimization algorithm under the given name.
646    pub fn register_algorithm(&mut self, name: &str, algorithm: BatchOptimizationAlgorithm) {
647        self.optimization_algorithms
648            .insert(name.to_string(), algorithm);
649    }
650
651    /// Get a registered batch optimization algorithm by name.
652    pub fn get_algorithm(&self, name: &str) -> Option<&BatchOptimizationAlgorithm> {
653        self.optimization_algorithms.get(name)
654    }
655
656    /// List the names of all registered batch optimization algorithms.
657    pub fn list_algorithms(&self) -> Vec<String> {
658        self.optimization_algorithms.keys().cloned().collect()
659    }
660
661    /// Return a reference to the optimization metrics.
662    pub fn optimization_metrics(&self) -> &BatchOptimizationMetrics {
663        &self.optimization_metrics
664    }
665
666    /// Return a mutable reference to the optimization metrics.
667    pub fn optimization_metrics_mut(&mut self) -> &mut BatchOptimizationMetrics {
668        &mut self.optimization_metrics
669    }
670}
671
672impl BatchOptimizationMetrics {
673    /// Construct a `BatchOptimizationMetrics` with default field values.
674    pub fn new() -> Self {
675        Self {
676            average_batch_size: 32.0,
677            throughput: 100.0,
678            latency: 10.0,
679            memory_utilization: 0.5,
680        }
681    }
682}
683
684impl InferenceOptimizer {
685    /// Construct a `InferenceOptimizer` with default field values.
686    pub fn new() -> Self {
687        Self {
688            optimization_strategies: vec![InferenceOptimizationStrategy::ModelQuantization],
689            performance_analyzer: PerformanceAnalyzer::new(),
690            auto_tuner: AutoTuner::new(),
691        }
692    }
693
694    /// Initialize `InferenceOptimizer`; `Ok` when ready.
695    pub fn initialize(&mut self) -> Result<(), MLError> {
696        self.performance_analyzer.initialize()?;
697        self.auto_tuner.initialize()?;
698        Ok(())
699    }
700
701    /// Return a reference to the configured optimization strategies.
702    pub fn optimization_strategies(&self) -> &[InferenceOptimizationStrategy] {
703        &self.optimization_strategies
704    }
705
706    /// Add an optimization strategy to the configured set.
707    pub fn add_optimization_strategy(&mut self, strategy: InferenceOptimizationStrategy) {
708        self.optimization_strategies.push(strategy);
709    }
710
711    /// Replace the full set of optimization strategies.
712    pub fn set_optimization_strategies(&mut self, strategies: Vec<InferenceOptimizationStrategy>) {
713        self.optimization_strategies = strategies;
714    }
715}
716
717impl PerformanceAnalyzer {
718    /// Construct a `PerformanceAnalyzer` with default field values.
719    pub fn new() -> Self {
720        Self {
721            analysis_methods: vec![AnalysisMethod::Profiling],
722            performance_profiles: HashMap::new(),
723            bottleneck_detector: BottleneckDetector::new(),
724        }
725    }
726
727    /// Initialize `PerformanceAnalyzer`; `Ok` when ready.
728    pub fn initialize(&mut self) -> Result<(), MLError> {
729        self.bottleneck_detector.initialize()?;
730        Ok(())
731    }
732
733    /// Return a reference to the configured analysis methods.
734    pub fn analysis_methods(&self) -> &[AnalysisMethod] {
735        &self.analysis_methods
736    }
737
738    /// Add an analysis method to the configured set.
739    pub fn add_analysis_method(&mut self, method: AnalysisMethod) {
740        self.analysis_methods.push(method);
741    }
742
743    /// Register a performance profile under its profile id.
744    pub fn register_profile(&mut self, profile: PerformanceProfile) {
745        self.performance_profiles
746            .insert(profile.profile_id.clone(), profile);
747    }
748
749    /// Get a registered performance profile by id.
750    pub fn get_profile(&self, profile_id: &str) -> Option<&PerformanceProfile> {
751        self.performance_profiles.get(profile_id)
752    }
753
754    /// List the ids of all registered performance profiles.
755    pub fn list_profiles(&self) -> Vec<String> {
756        self.performance_profiles.keys().cloned().collect()
757    }
758}
759
760impl PerformanceProfile {
761    /// Construct a `PerformanceProfile` with default field values.
762    pub fn new() -> Self {
763        Self {
764            profile_id: "profile_1".to_string(),
765            model_id: "model_1".to_string(),
766            backend_id: "backend_1".to_string(),
767            metrics: PerformanceMetrics::new(),
768            characteristics: PerformanceCharacteristics::new(),
769        }
770    }
771}
772
773impl PerformanceMetrics {
774    /// Construct a `PerformanceMetrics` with default field values.
775    pub fn new() -> Self {
776        Self {
777            latency: 10.0,
778            throughput: 100.0,
779            accuracy: 0.0, // not measured (scaffold default; no evaluation performed)
780            memory_usage: 1024 * 1024, // 1MB
781        }
782    }
783}
784
785impl PerformanceCharacteristics {
786    /// Construct a `PerformanceCharacteristics` with default field values.
787    pub fn new() -> Self {
788        Self {
789            compute_bound: true,
790            memory_bound: false,
791            io_bound: false,
792            network_bound: false,
793        }
794    }
795}
796
797impl BottleneckDetector {
798    /// Construct a `BottleneckDetector` with default field values.
799    pub fn new() -> Self {
800        Self {
801            detection_algorithms: vec![BottleneckDetectionAlgorithm::Statistical],
802            detection_thresholds: DetectionThresholds::new(),
803        }
804    }
805
806    /// Initialize `BottleneckDetector`; `Ok` when ready.
807    pub fn initialize(&mut self) -> Result<(), MLError> {
808        Ok(())
809    }
810
811    /// Return a reference to the configured detection algorithms.
812    pub fn detection_algorithms(&self) -> &[BottleneckDetectionAlgorithm] {
813        &self.detection_algorithms
814    }
815
816    /// Add a detection algorithm to the configured set.
817    pub fn add_detection_algorithm(&mut self, algorithm: BottleneckDetectionAlgorithm) {
818        self.detection_algorithms.push(algorithm);
819    }
820
821    /// Return a reference to the detection thresholds.
822    pub fn detection_thresholds(&self) -> &DetectionThresholds {
823        &self.detection_thresholds
824    }
825
826    /// Return a mutable reference to the detection thresholds.
827    pub fn detection_thresholds_mut(&mut self) -> &mut DetectionThresholds {
828        &mut self.detection_thresholds
829    }
830}
831
832impl DetectionThresholds {
833    /// Construct a `DetectionThresholds` with default field values.
834    pub fn new() -> Self {
835        Self {
836            cpu_threshold: 0.8,
837            memory_threshold: 0.8,
838            io_threshold: 0.8,
839            network_threshold: 0.8,
840        }
841    }
842}
843
844impl AutoTuner {
845    /// Construct a `AutoTuner` with default field values.
846    pub fn new() -> Self {
847        Self {
848            tuning_algorithms: HashMap::new(),
849            tuning_objectives: Vec::new(),
850            tuning_history: TuningHistory::new(),
851        }
852    }
853
854    /// Initialize `AutoTuner`; `Ok` when ready.
855    pub fn initialize(&mut self) -> Result<(), MLError> {
856        Ok(())
857    }
858
859    /// Register a tuning algorithm under the given name.
860    pub fn register_tuning_algorithm(&mut self, name: &str, algorithm: TuningAlgorithm) {
861        self.tuning_algorithms.insert(name.to_string(), algorithm);
862    }
863
864    /// Get a registered tuning algorithm by name.
865    pub fn get_tuning_algorithm(&self, name: &str) -> Option<&TuningAlgorithm> {
866        self.tuning_algorithms.get(name)
867    }
868
869    /// List the names of all registered tuning algorithms.
870    pub fn list_tuning_algorithms(&self) -> Vec<String> {
871        self.tuning_algorithms.keys().cloned().collect()
872    }
873
874    /// Add a tuning objective to the configured set.
875    pub fn add_tuning_objective(&mut self, objective: TuningObjective) {
876        self.tuning_objectives.push(objective);
877    }
878
879    /// Return a reference to the configured tuning objectives.
880    pub fn tuning_objectives(&self) -> &[TuningObjective] {
881        &self.tuning_objectives
882    }
883
884    /// Return a reference to the tuning history.
885    pub fn tuning_history(&self) -> &TuningHistory {
886        &self.tuning_history
887    }
888
889    /// Return a mutable reference to the tuning history.
890    pub fn tuning_history_mut(&mut self) -> &mut TuningHistory {
891        &mut self.tuning_history
892    }
893}
894
895impl TuningHistory {
896    /// Construct a `TuningHistory` with default field values.
897    pub fn new() -> Self {
898        Self {
899            tuning_records: Vec::new(),
900            best_configurations: HashMap::new(),
901        }
902    }
903
904    /// Append a tuning record to the history.
905    pub fn add_record(&mut self, record: TuningRecord) {
906        self.tuning_records.push(record);
907    }
908
909    /// Return a reference to all tuning records.
910    pub fn records(&self) -> &[TuningRecord] {
911        &self.tuning_records
912    }
913
914    /// Record a best configuration for a given objective name.
915    pub fn record_best_configuration(&mut self, objective: &str, config: TuningConfiguration) {
916        self.best_configurations
917            .insert(objective.to_string(), config);
918    }
919
920    /// Get the best configuration for a given objective.
921    pub fn get_best_configuration(&self, objective: &str) -> Option<&TuningConfiguration> {
922        self.best_configurations.get(objective)
923    }
924
925    /// List the objective names that have a recorded best configuration.
926    pub fn list_best_configurations(&self) -> Vec<String> {
927        self.best_configurations.keys().cloned().collect()
928    }
929}
930
931impl TuningRecord {
932    /// Construct a `TuningRecord` with default field values.
933    pub fn new() -> Self {
934        Self {
935            record_id: "record_1".to_string(),
936            timestamp: 0,
937            configuration: TuningConfiguration::new(),
938            performance: PerformanceMetrics::new(),
939            improvement: 0.0,
940        }
941    }
942}
943
944impl TuningConfiguration {
945    /// Construct a `TuningConfiguration` with default field values.
946    pub fn new() -> Self {
947        Self {
948            configuration_id: "config_1".to_string(),
949            parameters: HashMap::new(),
950            metadata: HashMap::new(),
951        }
952    }
953}