Skip to main content

optirs_core/streaming/adaptive_streaming/
mod.rs

1// Adaptive Streaming Optimization Module
2//
3// This module provides comprehensive adaptive streaming optimization for ML workloads.
4
5pub mod anomaly_detection;
6pub mod anomaly_ensemble;
7pub mod anomaly_ml;
8pub mod anomaly_scoring;
9pub mod anomaly_statistical;
10pub mod buffering;
11pub mod config;
12pub mod drift_detection;
13pub mod drift_models;
14pub mod drift_tests;
15pub mod meta_bandit;
16pub mod meta_learning;
17pub mod meta_transfer;
18pub mod optimizer;
19pub mod performance;
20pub mod resource_management;
21pub mod statistics;
22
23#[cfg(test)]
24mod config_wiring_tests;
25
26// NOTE: `anomaly_ensemble`, `anomaly_ml`, `anomaly_scoring`,
27// `anomaly_statistical`, `drift_models`, `drift_tests`, `meta_bandit` and
28// `statistics` are deliberately NOT glob-re-exported. The glob exports below
29// already collide across modules (see the aliased re-exports further down), and
30// adding more globs would reintroduce ambiguous names for every downstream
31// consumer. Reach for them through their module path instead.
32
33// Selective exports to avoid import conflicts
34pub use buffering::*;
35pub use config::*;
36pub use meta_learning::*;
37pub use optimizer::*;
38pub use resource_management::*;
39
40// Selective re-exports to avoid conflicts
41// Anomaly detection module exports
42pub use anomaly_detection::{
43    AdaptiveThresholdManager, AnomalyContext, AnomalyDetectionResult, AnomalyDetector,
44    AnomalyEvent, AnomalyResponseSystem, AnomalySeverity as AnomalyDetectionSeverity,
45    AnomalyType as AnomalyDetectionType, ContextPattern,
46    DataStatistics as AnomalyDetectionDataStatistics, DetectionResult, DetectorPerformance,
47    EffectivenessMetrics, EnsembleAnomalyDetector, EnsembleConfig, EnsembleVotingStrategy,
48    EscalationCondition, EscalationRule, FPMitigationStrategy, FPRateCalculator,
49    FalsePositiveEvent, FalsePositivePatterns, FalsePositiveTracker as AnomalyDetectionFPTracker,
50    MLModelMetrics, OutcomeMeasurement, PendingResponse, ResponseAction, ResponseExecution,
51    ResponseExecutor, ResponseOutcome, ResponsePriority, ResponseResourceLimits, TemporalPattern,
52    TemporalPatternType, ThresholdAdaptationParams, ThresholdAdaptationStrategy,
53    ThresholdPerformanceFeedback, TrendAnalysis, TrendDirection,
54};
55
56// Drift detection module exports
57pub use drift_detection::{
58    DistributionComparison, DriftDiagnostics, DriftEvent, DriftSeverity, DriftState,
59    DriftTestResult, EnhancedDriftDetector, FalsePositiveTracker as DriftDetectionFPTracker,
60    ModelDriftResult,
61};
62
63// Performance module exports
64pub use performance::{
65    AnomalySeverity as PerformanceAnomalySeverity, AnomalyType as PerformanceAnomalyType,
66    DataStatistics as PerformanceDataStatistics, ImprovementEvent, MetricStatistics,
67    PerformanceAnomaly, PerformanceAnomalyDetector, PerformanceContext, PerformanceDiagnostics,
68    PerformanceImprovementTracker, PerformanceMetric, PerformancePredictor, PerformanceSnapshot,
69    PerformanceTracker, PerformanceTrendAnalyzer, PlateauDetector, PredictionMethod,
70    PredictionResult, TrendData, TrendMethod,
71};
72
73// Utility functions for common configurations
74pub fn create_default_optimizer<A, D>(
75) -> StreamingResult<AdaptiveStreamingOptimizer<crate::optimizers::Adam<A>, A, D>>
76where
77    A: scirs2_core::ndarray::ScalarOperand
78        + Clone
79        + Default
80        + Send
81        + Sync
82        + 'static
83        + scirs2_core::numeric::Float
84        + std::iter::Sum
85        + std::fmt::Debug
86        + std::ops::DivAssign,
87    // `Data` is ndarray's *storage* trait, not a dimension trait: no type
88    // implements both it and `Dimension`, so this bound was unsatisfiable and
89    // neither factory could ever be instantiated by any caller.
90    D: scirs2_core::ndarray::Dimension + Send + Sync + 'static,
91{
92    let config = StreamingConfig::default();
93    let default_learning_rate = A::from(DEFAULT_LEARNING_RATE).ok_or_else(|| {
94        format!("element type cannot represent the default learning rate {DEFAULT_LEARNING_RATE}")
95    })?;
96    let base_optimizer = crate::optimizers::Adam::new(default_learning_rate);
97    Ok(AdaptiveStreamingOptimizer::new(base_optimizer, config)?)
98}
99
100pub fn create_optimizer_with_config<A, D>(
101    config: StreamingConfig,
102) -> StreamingResult<AdaptiveStreamingOptimizer<crate::optimizers::Adam<A>, A, D>>
103where
104    A: scirs2_core::ndarray::ScalarOperand
105        + Clone
106        + Default
107        + Send
108        + Sync
109        + 'static
110        + scirs2_core::numeric::Float
111        + std::iter::Sum
112        + std::fmt::Debug
113        + std::ops::DivAssign,
114    // `Data` is ndarray's *storage* trait, not a dimension trait: no type
115    // implements both it and `Dimension`, so this bound was unsatisfiable and
116    // neither factory could ever be instantiated by any caller.
117    D: scirs2_core::ndarray::Dimension + Send + Sync + 'static,
118{
119    let default_learning_rate = A::from(DEFAULT_LEARNING_RATE).ok_or_else(|| {
120        format!("element type cannot represent the default learning rate {DEFAULT_LEARNING_RATE}")
121    })?;
122    let base_optimizer = crate::optimizers::Adam::new(default_learning_rate);
123    Ok(AdaptiveStreamingOptimizer::new(base_optimizer, config)?)
124}
125
126/// Learning rate used by the convenience constructors above when the caller
127/// does not supply one.
128pub const DEFAULT_LEARNING_RATE: f64 = 0.001;
129
130// Result type alias
131pub type StreamingResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync>>;