Skip to main content

trustformers_mobile/
lib.rs

1//! TrustformeRS Mobile Deployment
2//!
3//! This crate provides optimized mobile deployment support for TrustformeRS,
4//! including iOS and Android platform integrations with platform-specific
5//! optimizations and inference backends.
6
7#![allow(deprecated)]
8#![allow(dead_code)]
9#![allow(unused_variables)]
10#![allow(unused_imports)]
11// Allow large error types in Result (TrustformersError is large by design)
12#![allow(clippy::result_large_err)]
13// Allow common patterns in mobile deployment code
14#![allow(clippy::too_many_arguments)]
15#![allow(clippy::type_complexity)]
16#![allow(clippy::excessive_nesting)]
17#![allow(unused_assignments)]
18#![allow(private_interfaces)]
19#![allow(unused_must_use)]
20// Allow FFI patterns - raw pointer handling in Unity interop is intentional
21#![allow(clippy::not_unsafe_ptr_arg_deref)]
22#![allow(clippy::manual_memcpy)]
23#![allow(clippy::useless_vec)]
24// Allow mobile-specific patterns
25#![allow(clippy::await_holding_lock)]
26#![allow(clippy::arc_with_non_send_sync)]
27#![allow(clippy::enum_variant_names)]
28#![allow(clippy::manual_clamp)]
29#![allow(clippy::borrowed_box)]
30#![allow(clippy::field_reassign_with_default)]
31#![allow(clippy::needless_range_loop)]
32#![allow(clippy::redundant_pattern_matching)]
33#![allow(clippy::upper_case_acronyms)]
34#![allow(clippy::ptr_arg)]
35#![allow(clippy::explicit_counter_loop)]
36#![allow(clippy::missing_safety_doc)]
37#![allow(clippy::vec_init_then_push)]
38#![allow(clippy::to_string_trait_impl)]
39#![allow(clippy::match_like_matches_macro)]
40#![allow(clippy::format_in_format_args)]
41#![allow(clippy::should_implement_trait)]
42#![allow(clippy::empty_line_after_doc_comments)]
43
44pub mod abi_checker;
45pub mod benchmarks;
46pub mod hardware;
47pub mod inference;
48pub mod optimization;
49pub mod ui_testing;
50
51// SciRS2 compatibility layer
52pub mod scirs2_compat;
53
54// Re-export SciRS2 compatibility types for seamless integration
55pub use scirs2_compat::{DefaultRng, DistributionOps, LinalgOps, SimdOps, StatisticalOps, Tensor};
56
57// C API for FFI interoperability
58pub mod c_api;
59
60#[cfg(target_os = "ios")]
61pub mod ios;
62
63#[cfg(target_os = "ios")]
64pub mod ios_background;
65
66#[cfg(target_os = "ios")]
67pub mod ios_app_extensions;
68
69#[cfg(target_os = "ios")]
70pub mod ios_icloud;
71
72#[cfg(target_os = "android")]
73pub mod android;
74
75#[cfg(target_os = "android")]
76pub mod android_renderscript;
77
78#[cfg(target_os = "android")]
79pub mod android_work_manager;
80
81#[cfg(target_os = "android")]
82pub mod android_content_provider;
83
84#[cfg(target_os = "android")]
85pub mod android_doze_compatibility;
86
87#[cfg(feature = "coreml")]
88pub mod coreml;
89
90#[cfg(feature = "coreml")]
91pub mod coreml_converter;
92
93#[cfg(feature = "nnapi")]
94pub mod nnapi;
95
96#[cfg(feature = "nnapi")]
97pub mod nnapi_converter;
98
99#[cfg(feature = "on-device-training")]
100pub mod training;
101
102#[cfg(feature = "on-device-training")]
103pub mod federated;
104
105#[cfg(feature = "on-device-training")]
106pub mod differential_privacy;
107
108#[cfg(feature = "on-device-training")]
109pub mod advanced_training;
110
111#[cfg(feature = "on-device-training")]
112pub mod advanced_privacy_mechanisms;
113
114pub mod advanced_security;
115pub mod crash_reporter;
116pub mod inference_visualizer;
117pub mod integration_testing;
118pub mod memory_leak_detector;
119pub mod mobile_performance_profiler;
120pub mod mobile_performance_profiler_legacy;
121pub mod mobile_testing;
122pub mod model_debugger;
123pub mod privacy_preserving_inference;
124
125#[cfg(target_os = "ios")]
126pub mod arkit_integration;
127
128#[cfg(target_os = "android")]
129pub mod edge_tpu_support;
130
131pub mod battery;
132pub mod compression;
133pub mod device_info;
134pub mod lifecycle;
135pub mod model_management;
136pub mod network_adaptation;
137pub mod network_optimization;
138pub mod profiler;
139pub mod thermal_power;
140
141#[cfg(target_arch = "wasm32")]
142pub mod wasm;
143
144// WebAssembly SIMD optimization module
145pub mod wasm_simd;
146
147// Apple Neural Engine V3 optimization module
148#[cfg(target_os = "ios")]
149pub mod neural_engine_v3;
150
151// Advanced Neural Engine V4 optimization for latest Apple hardware
152#[cfg(target_os = "ios")]
153pub mod advanced_neural_engine_v4;
154
155// MLX framework integration for Apple Silicon
156#[cfg(any(target_os = "ios", target_os = "macos"))]
157pub mod mlx_integration;
158
159// Federated Learning v2.0 with advanced differential privacy (moved to federated module)
160// pub mod federated_learning_v2;
161
162#[cfg(feature = "react-native")]
163pub mod react_native;
164
165#[cfg(feature = "react-native")]
166pub mod react_native_turbo;
167
168#[cfg(feature = "react-native")]
169pub mod react_native_fabric;
170
171#[cfg(feature = "flutter")]
172pub mod flutter;
173
174#[cfg(feature = "expo")]
175pub mod expo_plugin;
176
177#[cfg(feature = "unity")]
178pub mod unity_interop;
179
180// WebNN integration for web/hybrid platforms
181pub mod webnn_integration;
182
183// Advanced thermal management with predictive algorithms
184pub mod advanced_thermal;
185
186use serde::{Deserialize, Serialize};
187use trustformers_core::errors::{Result, TrustformersError};
188
189// Re-export AI-powered optimization types
190pub use optimization::{
191    ActivationType, ArchitectureMetrics, ConnectionType, DeviceConstraints, DeviceEnvironment,
192    EarlyStoppingConfig, LayerConfig, LayerType, MobileArchitecture, MobileNAS, NASConfig,
193    OptimizationTarget, PerformanceRecord, QualityTradeoffs,
194    QuantizationConfig as NASQuantizationConfig, QuantizationScheme as NASQuantizationScheme,
195    ReinforcementLearningAgent, SearchStrategy, SkipConnection, UsagePattern, UserContext,
196    UserPreferences,
197};
198
199// Re-export GGUF mobile optimization types
200pub use optimization::{
201    MobileGGUFConfig, MobileGGUFQuantizer, MobileGGUFStats, MobileGGUFType, MobileGGUFUtils,
202};
203
204// Re-export WebNN integration types
205pub use webnn_integration::{
206    BrowserInfo, WebNNBackend, WebNNCapabilities, WebNNCompiledGraph, WebNNDataType, WebNNDevice,
207    WebNNExecutionContext, WebNNGraphConfig, WebNNOperation, WebNNPowerPreference,
208    WebNNSupportLevel, WebNNTensorDescriptor, WebNNUtils,
209};
210
211// Re-export advanced thermal management types
212pub use advanced_thermal::{
213    AdaptiveCoolingStrategy, AdvancedThermalManager, CoolingMode, MultiSensorThermalFusion,
214    PlannedWorkload, SensorWeights, ThermalCoefficients, ThermalHotspot, ThermalPredictionModel,
215    WorkloadExecutionPlan, WorkloadPriority, WorkloadThermalPlanner,
216};
217
218// Re-export key device info types
219pub use device_info::{
220    BasicDeviceInfo, ChargingStatus, CpuInfo, GpuInfo, MemoryInfo, MobileDeviceDetector,
221    MobileDeviceInfo, NpuInfo, PerformanceScores, PerformanceTier, PowerInfo, SimdSupport,
222    ThermalInfo, ThermalState,
223};
224
225// Re-export key thermal and power management types
226pub use thermal_power::{
227    InferencePriority, PowerOptimizationStrategy, PowerThresholds, ThermalPowerConfig,
228    ThermalPowerManager, ThermalPowerStats, ThermalPowerUtils, ThermalThresholds, ThrottleLevel,
229    ThrottlingStrategy,
230};
231
232// Re-export key compression types
233pub use compression::{
234    CompressionBenefits, CompressionConfig, CompressionStats, CompressionUtils, DistillationConfig,
235    DistillationStrategy, MobileCompressionEngine, PruningStrategy, QualityMetric,
236    QualityRecoveryStrategy, QuantizationPrecision, QuantizationStrategy,
237};
238
239// Re-export key battery management types
240pub use battery::{
241    BatteryConfig, BatteryLevel, BatteryReading, BatteryStats, BatteryThresholds, BatteryUtils,
242    DailySummary, MobileBatteryManager, OptimizationRecommendation, PowerPrediction,
243    PowerUsageLimits, UsageSession, WeeklyUsagePattern,
244};
245
246// Re-export key profiler types
247pub use profiler::{
248    BottleneckConfig, BottleneckSeverity, BottleneckType, CpuMetrics, GpuMetrics, InferenceMetrics,
249    MemoryMetrics, MetricsConfig, MetricsSnapshot, MobileProfilerUtils, NetworkMetrics,
250    OptimizationCategory, OptimizationSuggestion, PerformanceBottleneck, ProfileSnapshot,
251    ProfilerCapability, ProfilerConfig, ProfilingStatistics,
252};
253
254// Re-export key network adaptation types
255pub use network_adaptation::{NetworkAdaptationManager, NetworkAdaptationStats};
256
257// Re-export key network optimization types
258pub use network_optimization::{
259    AdaptiveBehaviorConfig, BandwidthAdaptationConfig, BandwidthThresholds, BandwidthTier,
260    CacheEvictionStrategy, CompressionAlgorithm, DownloadCompressionConfig, DownloadConstraints,
261    DownloadOptimizationConfig, DownloadPriority, DownloadProgress, DownloadRetryConfig,
262    DownloadStatus, EdgeCachingConfig, EdgeFailoverConfig, EdgeLoadBalancingStrategy,
263    EdgeServerConfig, EdgeServerEndpoint, NetworkMetric, NetworkOptimizationConfig,
264    NetworkOptimizationManager, NetworkQualityConfig, OfflineFirstConfig, OfflineRetentionPolicy,
265    OfflineSyncStrategy, P2PConfig, P2PProtocol, P2PResourceLimits, P2PSecurityConfig,
266    P2PSecurityLevel, P2PSharingPolicy, P2PTimeRestrictions, QualityAdaptationConfig,
267    QualityAdaptationStrategy, QualityLevel, QualityThresholds, ResumableDownloadRequest,
268    TimeWindow,
269};
270
271// Re-export privacy-preserving inference types
272pub use privacy_preserving_inference::{
273    AggregationMethod, InferenceBudgetConfig, InferencePrivacyConfig, InferencePrivacyGuarantees,
274    InferenceQualityMetrics, InferenceUseCase, InputPerturbationMethod, InputPrivacyConfig,
275    OutputPrivacyConfig, OutputPrivacyMethod, PrivacyPreservingInferenceEngine,
276    PrivacyPreservingInferenceUtils, PrivacyUtilityTradeoff, PrivateInferenceResult,
277    SecureAggregationConfig,
278};
279
280// Re-export advanced security types
281pub use advanced_security::{
282    AdvancedSecurityConfig, AdvancedSecurityManager, EncryptedTensor, EncryptionOptimization,
283    HomomorphicConfig, HomomorphicEncryptionEngine, HomomorphicScheme, MPCCommunication,
284    MPCProtocol, QuantumResistantAlgorithm, QuantumResistantConfig, QuantumResistantEngine,
285    QuantumResistantKeyExchange, QuantumResistantSignature, SecureInferenceResult,
286    SecureMultipartyConfig, SecureMultipartyEngine, SecurityLevel, ZKProof, ZKProofConfig,
287    ZKProofSystem, ZKVerificationConfig, ZeroKnowledgeProofEngine,
288};
289
290// Re-export mobile performance profiler types
291pub use mobile_performance_profiler::{
292    AlertSeverity, AlertThresholds, AlertType, BatteryMetrics, ChartType, CpuProfilingConfig,
293    EventData, EventMetrics, EventType, ExportConfig, ExportFormat, GpuProfilingConfig,
294    HealthStatus, ImpactLevel, MemoryProfilingConfig, MetricTrend, MobileMetricsSnapshot,
295    MobilePerformanceProfiler, MobileProfilerConfig, NetworkProfilingConfig, PerformanceAlert,
296    PlatformMetrics, ProfilingData, ProfilingEvent, ProfilingMode, ProfilingSummary,
297    RealTimeConfig, RealTimeState, SamplingConfig, SessionInfo, SessionMetadata, SystemHealth,
298    TemperatureTrend, ThermalMetrics, TrendDirection, TrendingMetrics,
299};
300
301// Re-export crash reporter types
302pub use crash_reporter::{
303    AppCrashInfo, AuthType, BatteryCrashInfo, CpuCrashInfo, CrashAnalysis, CrashAnalysisConfig,
304    CrashContext, CrashInfo, CrashPattern, CrashPrivacyConfig, CrashRecoveryConfig, CrashReport,
305    CrashReporterConfig, CrashReportingConfig, CrashSeverity, CrashStatistics, CrashStorageConfig,
306    CrashType, EncryptionKeySource, ErrorLogEntry, ExceptionInfo, ForegroundStatus, GpuCrashInfo,
307    HeapInfo, ImpactAssessment, LogLevel, MemoryDump, MemoryPermissions, MemoryRegion,
308    MemoryRegionType, MemoryUsageInfo, MobileCrashReporter, ModelCrashInfo,
309    ModelPerformanceMetrics, NetworkConnectionType, NetworkCrashInfo, PatternType,
310    PlatformCrashConfig, RecentOperation, RecoveryImpact, RecoveryStrategy, RecoverySuggestion,
311    ReportingAuthConfig, ResolutionStatus, RiskAssessment, RiskLevel, SafeModeConfig, SignalInfo,
312    SimilarCrash, StackFrame, StackInfo, StackTrace, SystemCrashInfo, SystemEvent, ThreadInfo,
313    ThreadState, UrgencyLevel, UserAction,
314};
315
316// Re-export key lifecycle management types
317pub use lifecycle::{
318    AppCheckpoint, AppLifecycleManager, AppState, BackgroundTask, BackgroundTaskConfig,
319    LifecycleConfig, LifecycleStats, LifecycleUtils, MemoryPressureLevel, MemoryWarningConfig,
320    ModelState, NetworkInterruptionConfig, NotificationConfig, NotificationType,
321    ResourceManagementConfig, ResourceRequirements, SchedulingStrategy, StateTransition, TaskType,
322    ThermalLevel, ThermalWarningConfig, TransitionReason, UserSessionState,
323};
324
325// Re-export integration testing types
326pub use integration_testing::{
327    BackendTestResults, BackendTestingConfig, CompatibilityTestResults, CompatibilityTestingConfig,
328    CrossPlatformComparison, ErrorAnalysis, IntegrationTestConfig, IntegrationTestResults,
329    MobileIntegrationTestFramework, PerformanceBenchmarkResults, PerformanceTestingConfig,
330    PlatformTestResults, PlatformTestingConfig, RecommendationPriority, RecommendationType,
331    ReportFormat, TestCategory, TestConfiguration, TestEnvironmentInfo, TestMetrics,
332    TestRecommendation, TestReportingConfig, TestResult, TestStatus, TestSummary,
333};
334
335// Re-export WebAssembly SIMD optimization types
336pub use wasm_simd::{
337    SimdInstructionSet, SimdLaneWidth, SimdOperationType, SimdPerformanceMetrics, WasmSimdConfig,
338    WasmSimdEngine,
339};
340
341// Re-export Apple Neural Engine V3 optimization types
342#[cfg(target_os = "ios")]
343pub use neural_engine_v3::{
344    AdvancedFeatures, AppleDeviceType, BatchOptimizationConfig, CachingStrategy, CalibrationStats,
345    DataType, HardwareCapabilities, NeuralEngineOperation, NeuralEngineV3Config,
346    NeuralEngineV3Engine, NeuralEngineV3Metrics, NeuralEngineVersion, PerformanceProfile,
347    PowerEfficiencyMode, QuantizationProfile,
348};
349
350// Re-export MLX framework integration types
351#[cfg(any(target_os = "ios", target_os = "macos"))]
352pub use mlx_integration::{
353    AppleSiliconDevice, CompilationStrategy, CompiledMlxModel, ComputeUnitConfig,
354    DeviceCapabilities, GraphOptimizationConfig, MemoryRequirements, MlxConfig, MlxEngine,
355    MlxOperation, MlxPerformanceMetrics, MlxPrecision, ModelPerformanceProfile, OptimizedGraph,
356    PrecisionConfig, ProfilingConfig, UnifiedMemoryConfig,
357};
358
359// Re-export Federated Learning types
360#[cfg(feature = "on-device-training")]
361pub use federated::{
362    AggregationMetadata, AggregationStrategy, ClientMetrics, ClientSelectionStrategy,
363    ComputationCapability, DifferentialPrivacyConfig, FederatedLearningClient,
364    FederatedLearningConfig, FederatedLearningStats, FederatedLearningUtils, GlobalModelUpdate,
365    LocalTrainingResult, NetworkQuality, NoiseMechanism, SecureAggregator,
366};
367
368// Re-export Flutter integration types
369#[cfg(feature = "flutter")]
370pub use flutter::{
371    FlutterChannelManager, FlutterDeviceInfo, FlutterInferenceRequest, FlutterInferenceResponse,
372    FlutterMethodCall, FlutterMethodResult, FlutterPerformanceMetrics, FlutterQuantizationConfig,
373    FlutterTrustformersConfig,
374};
375
376// Re-export Expo plugin types
377#[cfg(feature = "expo")]
378pub use expo_plugin::{
379    AssetBundlingStrategy, AssetConfig, AssetLoadingStrategy, BuildConfig,
380    CacheEvictionStrategy as ExpoCacheEvictionStrategy,
381    CompressionAlgorithm as ExpoCompressionAlgorithm, DevelopmentConfig, ExpoConfig, ExpoContext,
382    ExpoInferenceRequest, ExpoInferenceResponse, ExpoMetrics, ExpoModule, ExpoPlugin, ExpoValue,
383    LogFormat, LogLevel as ExpoLogLevel, ModuleRegistryConfig, PerformanceConfig, TargetPlatform,
384};
385
386// Re-export iOS background processing types
387#[cfg(target_os = "ios")]
388pub use ios_background::{
389    iOSBackgroundConfig, iOSBackgroundManager, BackgroundInferenceResult, BackgroundPriority,
390    BackgroundState, BackgroundStats, BackgroundTask, BackgroundTaskState, BackgroundTaskType,
391};
392
393// Re-export iOS App Extension types
394#[cfg(target_os = "ios")]
395pub use ios_app_extensions::{
396    iOSAppExtensionManager, iOSExtensionConfig, iOSExtensionType, ExtensionBatchConfig,
397    ExtensionBatteryConfig, ExtensionCacheLocation, ExtensionContext, ExtensionDataRetentionConfig,
398    ExtensionError, ExtensionErrorCategory, ExtensionInferenceRequest, ExtensionInferenceResponse,
399    ExtensionMetrics, ExtensionModelCacheConfig, ExtensionPerformanceConfig, ExtensionPriority,
400    ExtensionPrivacyConfig, ExtensionResourceConfig,
401};
402
403// Re-export iOS iCloud sync types
404#[cfg(target_os = "ios")]
405pub use ios_icloud::{
406    iCloudModelSync, iCloudSyncConfig, ConflictResolution, DatabaseScope, ModelMetadata,
407    ModelSyncResult, SyncOperation, SyncResult, SyncStatistics, SyncStatus,
408};
409
410// Re-export Android RenderScript types
411#[cfg(target_os = "android")]
412pub use android_renderscript::{
413    AndroidRenderScriptEngine, RSAllocationStrategy, RSKernelParams, RSKernelType,
414    RenderScriptConfig, RenderScriptStats,
415};
416
417// Re-export Android Work Manager types
418#[cfg(target_os = "android")]
419pub use android_work_manager::{
420    AndroidWorkManager, AndroidWorkManagerConfig, BackgroundExecutionConfig, CompressionAlgorithm,
421    ConflictResolutionStrategy, DataCompressionConfig, DataSyncConfig, ExistingWorkPolicy,
422    ForegroundNotificationConfig, StorageConstraints, TaskExecutionOrder, TaskPrioritizationConfig,
423    WorkConstraintsConfig, WorkError, WorkErrorCategory, WorkExecutionMetrics, WorkFrequency,
424    WorkInputData, WorkNetworkType, WorkPriority, WorkRequest, WorkRequestType, WorkResult,
425    WorkResultData, WorkRetryInfo, WorkRetryPolicy, WorkRetryPolicyConfig, WorkStatus,
426    WorkTaskType,
427};
428
429// Re-export Android Content Provider types
430#[cfg(target_os = "android")]
431pub use android_content_provider::{
432    AccessLevel, AccessResult, AndroidModelContentProvider,
433    CompressionAlgorithm as ContentCompressionAlgorithm, ContentProviderConfig,
434    EncryptionAlgorithm, EncryptionConfig, KeyManagementConfig, ModelInfo, ModelMetadata,
435    ModelPermissions, ModelStream, ModelType, Operation, PerformanceConfig, QueryParams,
436    QueryResult, SecurityConfig, SortOrder,
437};
438
439// Re-export Android Doze compatibility types
440#[cfg(target_os = "android")]
441pub use android_doze_compatibility::{
442    AndroidDozeCompatibilityManager, DeferredTask, DeviceState, DozeCompatibilityConfig,
443    DozeCompatibilityStats, DozeCompatibilityUtils, DozeDetectionConfig, DozeMemoryStrategy,
444    DozeNetworkConfig, DozePerformanceConfig, DozePerformanceImpact, DozeState, DozeSyncStrategy,
445    DozeTaskConfig, DozeTaskType, InferenceResult, MaintenanceWindow, MaintenanceWindowType,
446    NetworkRequest, NetworkResponse, TaskPriority, UserImpactLevel, WhitelistReason,
447};
448
449// Re-export ARKit integration types
450#[cfg(target_os = "ios")]
451pub use arkit_integration::{
452    ARFrame, ARKitConfig, ARKitInferenceEngine, ARPerformanceConfig, ARProcessingResult,
453    ARRenderingConfig, ARSessionConfig, ARSessionState, ARSessionStats, ARWorldMap, BoundingBox2D,
454    BoundingBox3D, CameraImage, DepthBuffer, DepthFormat, DetectedPlane, Detection,
455    DirectionalLight, FacePose, HandChirality, HandPose, ImageFormat, Joint, JointType,
456    LightEstimate, LightEstimationConfig, LightEstimationMode, Matrix4x4, ObjectDetectionConfig,
457    ObjectDetectionModel, PlaneClassification, PlaneDetectionConfig, PlaneOrientation, Pose,
458    PoseEstimationConfig, PoseEstimationModel, QualityThresholds, Quaternion, RenderTarget,
459    RenderingBackend, SceneAnalysis, SphericalHarmonics, Surface, SurfaceMaterial, TextureFormat,
460    TrackingQuality, TrackingState, Vec2, Vec3,
461};
462
463// Re-export Edge TPU support types
464#[cfg(target_os = "android")]
465pub use edge_tpu_support::{
466    BatchOptimizationConfig, CacheSettings, CompilationConfig, CompiledTPUModel, ComputeCapability,
467    ConcurrencyConfig, CoolingSettings, DataType, DebugLevel, DefragmentationConfig,
468    DeviceSelectionStrategy, DeviceStatus, EdgeTPUConfig, EdgeTPUEngine, InferenceResult,
469    InferenceTask, LatencyOptimizationConfig, MemoryAllocationStrategy, MemoryLayout,
470    MemoryRequirements, ModelMetadata, OptimizationLevel, PerformanceMode, PerformanceProfile,
471    PipelineConfig, PowerBudgetConfig, PowerMode, SchedulingStrategy, TPUDebugConfig, TPUDevice,
472    TPUDeviceConfig, TPUDeviceType, TPUMemoryConfig, TPUPerformanceConfig, TPUPowerConfig,
473    TPUPrecision, TPUStats, TPUThermalConfig, TaskPriority, TemperatureThresholds, Tensor,
474    TensorResult, TensorSpec, ThermalManagementStrategy,
475};
476
477/// Mobile deployment configuration
478#[derive(Debug, Clone, Serialize, Deserialize)]
479pub struct MobileConfig {
480    /// Target platform
481    pub platform: MobilePlatform,
482    /// Inference backend
483    pub backend: MobileBackend,
484    /// Memory optimization level
485    pub memory_optimization: MemoryOptimization,
486    /// Maximum memory usage in MB
487    pub max_memory_mb: usize,
488    /// Use fp16 precision for inference
489    pub use_fp16: bool,
490    /// Enable quantization
491    pub quantization: Option<MobileQuantizationConfig>,
492    /// Thread pool size (0 = auto-detect)
493    pub num_threads: usize,
494    /// Enable batch processing
495    pub enable_batching: bool,
496    /// Maximum batch size
497    pub max_batch_size: usize,
498}
499
500/// Target mobile platform
501#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
502pub enum MobilePlatform {
503    /// iOS platform
504    Ios,
505    /// Android platform
506    Android,
507    /// Generic mobile (cross-platform)
508    Generic,
509}
510
511/// Mobile inference backend
512#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
513pub enum MobileBackend {
514    /// CPU-only inference
515    CPU,
516    /// Core ML (iOS)
517    CoreML,
518    /// Neural Networks API (Android)
519    NNAPI,
520    /// GPU acceleration (Metal/Vulkan)
521    GPU,
522    /// Metal acceleration (iOS specific)
523    Metal,
524    /// Vulkan acceleration (cross-platform)
525    Vulkan,
526    /// OpenCL acceleration (cross-platform)
527    OpenCL,
528    /// Custom optimized backend
529    Custom,
530}
531
532/// Memory optimization levels
533#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
534pub enum MemoryOptimization {
535    /// Minimal optimization (fastest inference)
536    Minimal,
537    /// Balanced optimization
538    Balanced,
539    /// Maximum optimization (lowest memory usage)
540    Maximum,
541}
542
543/// Mobile quantization configuration
544#[derive(Debug, Clone, Serialize, Deserialize)]
545pub struct MobileQuantizationConfig {
546    /// Quantization scheme optimized for mobile
547    pub scheme: MobileQuantizationScheme,
548    /// Use dynamic quantization
549    pub dynamic: bool,
550    /// Per-channel quantization
551    pub per_channel: bool,
552}
553
554/// Mobile-optimized quantization schemes
555#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
556pub enum MobileQuantizationScheme {
557    /// 8-bit integer quantization
558    Int8,
559    /// 4-bit integer quantization (ultra-low memory)
560    Int4,
561    /// Float16 quantization
562    FP16,
563    /// Dynamic quantization
564    Dynamic,
565}
566
567impl Default for MobileConfig {
568    fn default() -> Self {
569        Self {
570            platform: MobilePlatform::Generic,
571            backend: MobileBackend::CPU,
572            memory_optimization: MemoryOptimization::Balanced,
573            max_memory_mb: 512, // Conservative default for mobile
574            use_fp16: true,
575            quantization: Some(MobileQuantizationConfig {
576                scheme: MobileQuantizationScheme::Int8,
577                dynamic: true,
578                per_channel: false,
579            }),
580            num_threads: 0,         // Auto-detect
581            enable_batching: false, // Conservative for mobile
582            max_batch_size: 1,
583        }
584    }
585}
586
587impl MobileConfig {
588    /// Create optimized configuration based on device detection
589    pub fn auto_detect_optimized() -> Result<Self> {
590        let device_info = crate::device_info::MobileDeviceDetector::detect()?;
591        Ok(crate::device_info::MobileDeviceDetector::generate_optimized_config(&device_info))
592    }
593
594    /// Create optimized configuration for iOS
595    pub fn ios_optimized() -> Self {
596        Self {
597            platform: MobilePlatform::Ios,
598            backend: MobileBackend::CoreML,
599            memory_optimization: MemoryOptimization::Balanced,
600            max_memory_mb: 1024, // iOS devices typically have more RAM
601            use_fp16: true,
602            quantization: Some(MobileQuantizationConfig {
603                scheme: MobileQuantizationScheme::FP16,
604                dynamic: false,
605                per_channel: true,
606            }),
607            num_threads: 0,
608            enable_batching: true,
609            max_batch_size: 4,
610        }
611    }
612
613    /// Create optimized configuration for Android
614    pub fn android_optimized() -> Self {
615        Self {
616            platform: MobilePlatform::Android,
617            backend: MobileBackend::NNAPI,
618            memory_optimization: MemoryOptimization::Balanced,
619            max_memory_mb: 768, // Conservative for Android diversity
620            use_fp16: true,
621            quantization: Some(MobileQuantizationConfig {
622                scheme: MobileQuantizationScheme::Int8,
623                dynamic: true,
624                per_channel: false,
625            }),
626            num_threads: 0,
627            enable_batching: false,
628            max_batch_size: 1,
629        }
630    }
631
632    /// Create ultra-low memory configuration
633    pub fn ultra_low_memory() -> Self {
634        Self {
635            platform: MobilePlatform::Generic,
636            backend: MobileBackend::CPU,
637            memory_optimization: MemoryOptimization::Maximum,
638            max_memory_mb: 256,
639            use_fp16: true,
640            quantization: Some(MobileQuantizationConfig {
641                scheme: MobileQuantizationScheme::Int4,
642                dynamic: true,
643                per_channel: true,
644            }),
645            num_threads: 1, // Single-threaded for minimum memory
646            enable_batching: false,
647            max_batch_size: 1,
648        }
649    }
650
651    /// Validate configuration for mobile deployment
652    pub fn validate(&self) -> Result<()> {
653        // Check memory constraints
654        if self.max_memory_mb < 64 {
655            return Err(TrustformersError::config_error(
656                "Mobile deployment requires at least 64MB memory",
657                "validate",
658            ));
659        }
660
661        if self.max_memory_mb > 4096 {
662            return Err(TrustformersError::config_error(
663                "Mobile deployment should not exceed 4GB memory",
664                "validate",
665            ));
666        }
667
668        // Validate platform-backend compatibility
669        match (self.platform, self.backend) {
670            (MobilePlatform::Ios, MobileBackend::NNAPI) => {
671                return Err(TrustformersError::config_error(
672                    "NNAPI backend is not available on iOS",
673                    "validate",
674                ));
675            },
676            (MobilePlatform::Android, MobileBackend::CoreML) => {
677                return Err(TrustformersError::config_error(
678                    "Core ML backend is not available on Android",
679                    "validate",
680                ));
681            },
682            _ => {},
683        }
684
685        // Validate batch size
686        if self.enable_batching && self.max_batch_size == 0 {
687            return Err(TrustformersError::config_error(
688                "Batch size must be > 0 when batching is enabled",
689                "validate",
690            ));
691        }
692
693        // Validate thread count
694        if self.num_threads > 16 {
695            return Err(TrustformersError::config_error(
696                "Mobile deployment should not use more than 16 threads",
697                "validate",
698            ));
699        }
700
701        Ok(())
702    }
703
704    /// Estimate memory usage for the configuration
705    pub fn estimate_memory_usage(&self, model_size_mb: usize) -> usize {
706        let mut total_memory = model_size_mb;
707
708        // Add quantization overhead
709        if let Some(ref quant_config) = self.quantization {
710            let reduction_factor = match quant_config.scheme {
711                MobileQuantizationScheme::Int4 => 8,    // 4-bit = 1/8 of FP32
712                MobileQuantizationScheme::Int8 => 4,    // 8-bit = 1/4 of FP32
713                MobileQuantizationScheme::FP16 => 2,    // 16-bit = 1/2 of FP32
714                MobileQuantizationScheme::Dynamic => 3, // Approximate
715            };
716            total_memory = model_size_mb / reduction_factor;
717        } else if self.use_fp16 {
718            total_memory = model_size_mb / 2; // FP16 is half of FP32
719        }
720
721        // Add runtime overhead
722        let runtime_overhead = match self.memory_optimization {
723            MemoryOptimization::Minimal => total_memory / 2, // 50% overhead
724            MemoryOptimization::Balanced => total_memory / 4, // 25% overhead
725            MemoryOptimization::Maximum => total_memory / 8, // 12.5% overhead
726        };
727
728        total_memory + runtime_overhead
729    }
730
731    /// Get recommended thread count for the platform
732    pub fn get_thread_count(&self) -> usize {
733        if self.num_threads > 0 {
734            return self.num_threads;
735        }
736
737        // Auto-detect based on platform and optimization level
738        let base_threads = match self.platform {
739            MobilePlatform::Ios => 4,     // iOS devices typically have 4-8 cores
740            MobilePlatform::Android => 2, // Conservative for Android diversity
741            MobilePlatform::Generic => 2, // Conservative default
742        };
743
744        match self.memory_optimization {
745            MemoryOptimization::Maximum => 1, // Single-threaded for minimum memory
746            MemoryOptimization::Balanced => base_threads, // Balanced approach
747            MemoryOptimization::Minimal => base_threads * 2, // More threads for speed
748        }
749    }
750}
751
752/// Mobile deployment statistics
753#[derive(Debug, Clone, Serialize, Deserialize)]
754pub struct MobileStats {
755    /// Platform information
756    pub platform: MobilePlatform,
757    /// Backend in use
758    pub backend: MobileBackend,
759    /// Current memory usage (MB)
760    pub memory_usage_mb: usize,
761    /// Peak memory usage (MB)
762    pub peak_memory_mb: usize,
763    /// Average inference time (ms)
764    pub avg_inference_time_ms: f32,
765    /// Total inferences performed
766    pub total_inferences: usize,
767    /// Current thread count
768    pub thread_count: usize,
769    /// Quantization status
770    pub quantization_enabled: bool,
771    /// FP16 status
772    pub fp16_enabled: bool,
773}
774
775impl MobileStats {
776    /// Create new statistics tracker
777    pub fn new(config: &MobileConfig) -> Self {
778        Self {
779            platform: config.platform,
780            backend: config.backend,
781            memory_usage_mb: 0,
782            peak_memory_mb: 0,
783            avg_inference_time_ms: 0.0,
784            total_inferences: 0,
785            thread_count: config.get_thread_count(),
786            quantization_enabled: config.quantization.is_some(),
787            fp16_enabled: config.use_fp16,
788        }
789    }
790
791    /// Update inference statistics
792    pub fn update_inference(&mut self, inference_time_ms: f32) {
793        self.total_inferences += 1;
794
795        // Update running average
796        let alpha = 0.1; // Smoothing factor
797        if self.total_inferences == 1 {
798            self.avg_inference_time_ms = inference_time_ms;
799        } else {
800            self.avg_inference_time_ms =
801                alpha * inference_time_ms + (1.0 - alpha) * self.avg_inference_time_ms;
802        }
803    }
804
805    /// Update memory statistics
806    pub fn update_memory(&mut self, current_memory_mb: usize) {
807        self.memory_usage_mb = current_memory_mb;
808        self.peak_memory_mb = self.peak_memory_mb.max(current_memory_mb);
809    }
810
811    /// Get performance summary
812    pub fn get_performance_summary(&self) -> String {
813        format!(
814            "Mobile Performance Summary:\n\
815             Platform: {:?}\n\
816             Backend: {:?}\n\
817             Memory: {} MB (peak: {} MB)\n\
818             Avg Inference: {:.2} ms\n\
819             Total Inferences: {}\n\
820             Threads: {}\n\
821             Optimizations: FP16={}, Quantization={}",
822            self.platform,
823            self.backend,
824            self.memory_usage_mb,
825            self.peak_memory_mb,
826            self.avg_inference_time_ms,
827            self.total_inferences,
828            self.thread_count,
829            self.fp16_enabled,
830            self.quantization_enabled
831        )
832    }
833}
834
835#[cfg(test)]
836mod tests {
837    use super::*;
838
839    #[test]
840    fn test_mobile_config_defaults() {
841        let config = MobileConfig::default();
842        assert_eq!(config.platform, MobilePlatform::Generic);
843        assert_eq!(config.backend, MobileBackend::CPU);
844        assert!(config.use_fp16);
845        assert!(config.quantization.is_some());
846    }
847
848    #[test]
849    fn test_ios_optimized_config() {
850        let config = MobileConfig::ios_optimized();
851        assert_eq!(config.platform, MobilePlatform::Ios);
852        assert_eq!(config.backend, MobileBackend::CoreML);
853        assert!(config.enable_batching);
854        assert_eq!(config.max_batch_size, 4);
855    }
856
857    #[test]
858    fn test_android_optimized_config() {
859        let config = MobileConfig::android_optimized();
860        assert_eq!(config.platform, MobilePlatform::Android);
861        assert_eq!(config.backend, MobileBackend::NNAPI);
862        assert!(!config.enable_batching);
863    }
864
865    #[test]
866    fn test_ultra_low_memory_config() {
867        let config = MobileConfig::ultra_low_memory();
868        assert_eq!(config.memory_optimization, MemoryOptimization::Maximum);
869        assert_eq!(config.max_memory_mb, 256);
870        assert_eq!(config.num_threads, 1);
871
872        if let Some(ref quant) = config.quantization {
873            assert_eq!(quant.scheme, MobileQuantizationScheme::Int4);
874        }
875    }
876
877    #[test]
878    fn test_config_validation() {
879        let mut config = MobileConfig::default();
880        assert!(config.validate().is_ok());
881
882        // Test invalid memory
883        config.max_memory_mb = 32;
884        assert!(config.validate().is_err());
885
886        config.max_memory_mb = 512;
887        assert!(config.validate().is_ok());
888
889        // Test platform-backend mismatch
890        config.platform = MobilePlatform::Ios;
891        config.backend = MobileBackend::NNAPI;
892        assert!(config.validate().is_err());
893
894        config.backend = MobileBackend::CoreML;
895        assert!(config.validate().is_ok());
896    }
897
898    #[test]
899    fn test_memory_estimation() {
900        let config = MobileConfig::default();
901        let model_size = 100; // 100MB
902
903        let estimated = config.estimate_memory_usage(model_size);
904        assert!(estimated < model_size); // Should be smaller due to quantization
905        assert!(estimated > 0);
906    }
907
908    #[test]
909    fn test_thread_count_detection() {
910        let mut config = MobileConfig::default();
911
912        // Auto-detect
913        config.num_threads = 0;
914        assert!(config.get_thread_count() > 0);
915
916        // Manual setting
917        config.num_threads = 4;
918        assert_eq!(config.get_thread_count(), 4);
919    }
920
921    #[test]
922    fn test_mobile_stats() {
923        let config = MobileConfig::default();
924        let mut stats = MobileStats::new(&config);
925
926        assert_eq!(stats.total_inferences, 0);
927        assert_eq!(stats.memory_usage_mb, 0);
928
929        // Update with inference
930        stats.update_inference(50.0);
931        assert_eq!(stats.total_inferences, 1);
932        assert_eq!(stats.avg_inference_time_ms, 50.0);
933
934        // Update with memory
935        stats.update_memory(128);
936        assert_eq!(stats.memory_usage_mb, 128);
937        assert_eq!(stats.peak_memory_mb, 128);
938
939        // Check summary
940        let summary = stats.get_performance_summary();
941        assert!(summary.contains("Platform"));
942        assert!(summary.contains("Memory"));
943    }
944}