trustformers-mobile 0.1.1

Mobile deployment support for TrustformeRS (iOS, Android)
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
//! iOS Platform Support for TrustformeRS
//!
//! This module provides iOS-specific functionality including Core ML integration,
//! iOS framework bindings, and platform-specific optimizations.

use crate::{MobileBackend, MobileConfig, MobilePlatform, MobileStats};
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_float, c_int, c_uint, c_void};
use std::ptr;
use std::slice;
use trustformers_core::error::{CoreError, Result};
use trustformers_core::Tensor;

#[cfg(target_os = "ios")]
use core_foundation::{
    base::{CFRelease, CFTypeRef},
    string::{CFString, CFStringRef},
};
#[cfg(target_os = "ios")]
use objc::runtime::{Class, Object};

// Import submodules
pub mod engine;
pub mod metal;
pub mod mps;

// Re-export key types from submodules
pub use engine::{
    iOSInferenceEngine, LoadDistributionStrategy, MetalComputeState, MultiGPUManager,
};
pub use metal::{
    MTLBuffer, MTLCommandBuffer, MTLCommandQueue, MTLComputeCommandEncoder,
    MTLComputePipelineState, MTLDevice, MTLFunction, MTLLibrary, MTLOrigin, MTLRegion, MTLSize,
};
pub use mps::{
    MPSDataType, MPSGraph, MPSGraphConvolution2DOpDescriptor,
    MPSGraphDepthwiseConvolution2DOpDescriptor, MPSGraphDevice, MPSGraphExecutable,
    MPSGraphExecutionDescriptor, MPSGraphMatrixMultiplicationDescriptor,
    MPSGraphPooling2DOpDescriptor, MPSGraphTensor, MPSGraphTensorData, MPSShape,
};

/// iOS device information
#[derive(Debug, Clone)]
pub struct IOsDeviceInfo {
    pub device_name: String,
    pub system_name: String,
    pub system_version: String,
    pub model: String,
    pub localized_model: String,
    pub identifer_for_vendor: Option<String>,
    pub is_simulator: bool,
    pub total_memory_gb: f64,
    pub available_memory_gb: f64,
    pub battery_level: f32,
    pub thermal_state: ThermalState,
    pub processor_count: usize,
    pub cpu_type: String,
    pub gpu_family: u32,
    pub max_threads_per_group: usize,
    pub supports_neural_engine: bool,
}

/// iOS thermal states
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThermalState {
    Nominal = 0,
    Fair = 1,
    Serious = 2,
    Critical = 3,
}

impl IOsDeviceInfo {
    /// Get device information on iOS
    #[cfg(target_os = "ios")]
    pub fn detect() -> Result<Self> {
        // This is a simplified version - real implementation would use iOS APIs
        Ok(Self {
            device_name: "iPhone".to_string(),
            system_name: "iOS".to_string(),
            system_version: "17.0".to_string(),
            model: "iPhone15,2".to_string(),
            localized_model: "iPhone".to_string(),
            identifer_for_vendor: None,
            is_simulator: false,
            total_memory_gb: 6.0,
            available_memory_gb: 4.5,
            battery_level: 0.85,
            thermal_state: ThermalState::Nominal,
            processor_count: 6,
            cpu_type: "A16 Bionic".to_string(),
            gpu_family: metal::MTL_GPU_FAMILY_APPLE_7,
            max_threads_per_group: 1024,
            supports_neural_engine: true,
        })
    }

    /// Get device information on non-iOS platforms (stub)
    #[cfg(not(target_os = "ios"))]
    pub fn detect() -> Result<Self> {
        // Stub implementation for non-iOS platforms
        Ok(Self {
            device_name: "iOS Device".to_string(),
            system_name: "iOS".to_string(),
            system_version: "17.0".to_string(),
            model: "iPhone".to_string(),
            localized_model: "iPhone".to_string(),
            identifer_for_vendor: None,
            is_simulator: true,
            total_memory_gb: 4.0,
            available_memory_gb: 3.0,
            battery_level: 0.5,
            thermal_state: ThermalState::Nominal,
            processor_count: 4,
            cpu_type: "Simulated".to_string(),
            gpu_family: metal::MTL_GPU_FAMILY_APPLE_5,
            max_threads_per_group: 512,
            supports_neural_engine: false,
        })
    }

    /// Get device performance tier
    pub fn performance_tier(&self) -> crate::device_info::PerformanceTier {
        if self.gpu_family >= metal::MTL_GPU_FAMILY_APPLE_7 {
            crate::device_info::PerformanceTier::Flagship
        } else if self.gpu_family >= metal::MTL_GPU_FAMILY_APPLE_5 {
            crate::device_info::PerformanceTier::HighEnd
        } else if self.gpu_family >= metal::MTL_GPU_FAMILY_APPLE_3 {
            crate::device_info::PerformanceTier::MidRange
        } else {
            crate::device_info::PerformanceTier::Budget
        }
    }

    /// Check if device supports feature
    pub fn supports_feature(&self, feature: &iOSFeature) -> bool {
        match feature {
            iOSFeature::CoreML => true,
            iOSFeature::MetalPerformanceShaders => true,
            iOSFeature::NeuralEngine => self.supports_neural_engine,
            iOSFeature::ARKit => self.gpu_family >= metal::MTL_GPU_FAMILY_APPLE_3,
            iOSFeature::CreateML => self.gpu_family >= metal::MTL_GPU_FAMILY_APPLE_5,
            iOSFeature::VisionFramework => true,
            iOSFeature::SpeechFramework => true,
            iOSFeature::NaturalLanguage => true,
        }
    }
}

/// iOS-specific features
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum iOSFeature {
    CoreML,
    MetalPerformanceShaders,
    NeuralEngine,
    ARKit,
    CreateML,
    VisionFramework,
    SpeechFramework,
    NaturalLanguage,
}

/// C API structures for FFI
#[repr(C)]
pub struct CInferenceResult {
    pub success: bool,
    pub output_data: *mut c_float,
    pub output_size: usize,
    pub inference_time_ms: f32,
    pub error_message: *mut c_char,
}

#[repr(C)]
pub struct CTrustformersConfig {
    pub use_coreml: bool,
    pub use_nnapi: bool,
    pub use_gpu: bool,
    pub use_fp16: bool,
    pub num_threads: c_int,
    pub max_memory_mb: c_int,
}

#[repr(C)]
pub struct CTrustformersInferenceResult {
    pub data: *mut f32,
    pub size: usize,
    pub success: bool,
}

#[repr(C)]
pub struct CTrustformersTensor {
    pub data: *mut c_void,
    pub shape: *mut usize,
    pub ndim: usize,
    pub dtype: c_int,
}

/// C API functions
#[no_mangle]
pub unsafe extern "C" fn trustformers_ios_inference_engine_new(
    config: *const CTrustformersConfig,
) -> *mut iOSInferenceEngine {
    if config.is_null() {
        return ptr::null_mut();
    }

    let config_ref = &*config;
    let mobile_config = MobileConfig {
        platform: MobilePlatform::Ios,
        backend: if config_ref.use_coreml {
            MobileBackend::CoreML
        } else if config_ref.use_gpu {
            MobileBackend::Metal
        } else {
            MobileBackend::CPU
        },
        memory_optimization: crate::MemoryOptimization::Balanced,
        max_memory_mb: config_ref.max_memory_mb as usize,
        use_fp16: config_ref.use_fp16,
        quantization: None,
        num_threads: config_ref.num_threads as usize,
        enable_batching: false,
        max_batch_size: 1,
    };

    match iOSInferenceEngine::new(mobile_config) {
        Ok(engine) => Box::into_raw(Box::new(engine)),
        Err(_) => ptr::null_mut(),
    }
}

#[no_mangle]
pub unsafe extern "C" fn trustformers_ios_inference_engine_free(engine: *mut iOSInferenceEngine) {
    if !engine.is_null() {
        let _ = Box::from_raw(engine);
    }
}

#[no_mangle]
pub unsafe extern "C" fn trustformers_ios_load_model(
    engine: *mut iOSInferenceEngine,
    model_path: *const c_char,
) -> bool {
    if engine.is_null() || model_path.is_null() {
        return false;
    }

    let engine_ref = &mut *engine;
    let path_cstr = CStr::from_ptr(model_path);

    match path_cstr.to_str() {
        Ok(path_str) => engine_ref.load_model_from_path(path_str).is_ok(),
        Err(_) => false,
    }
}

#[no_mangle]
pub unsafe extern "C" fn trustformers_ios_inference(
    engine: *mut iOSInferenceEngine,
    input_data: *const f32,
    input_size: usize,
) -> CInferenceResult {
    let mut result = CInferenceResult {
        success: false,
        output_data: ptr::null_mut(),
        output_size: 0,
        inference_time_ms: 0.0,
        error_message: ptr::null_mut(),
    };

    if engine.is_null() || input_data.is_null() || input_size == 0 {
        let error_msg = CString::new("Invalid input parameters").unwrap_or_default();
        result.error_message = error_msg.into_raw();
        return result;
    }

    let engine_ref = &mut *engine;
    let input_slice = slice::from_raw_parts(input_data, input_size);

    // Convert input to tensor format - simplified for C API
    let shape = vec![1, input_size]; // Assume batch size 1
    let tensor_result = Tensor::from_vec(input_slice.to_vec(), &shape);

    match tensor_result {
        Ok(input_tensor) => {
            let start_time = std::time::Instant::now();
            match engine_ref.inference(&input_tensor) {
                Ok(output_tensor) => {
                    let inference_time = start_time.elapsed().as_secs_f32() * 1000.0;

                    match output_tensor.data() {
                        Ok(output_data_vec) => {
                            let output_size = output_data_vec.len();
                            let output_ptr = output_data_vec.as_ptr() as *mut f32;
                            std::mem::forget(output_data_vec); // Prevent deallocation

                            result.success = true;
                            result.output_data = output_ptr;
                            result.output_size = output_size;
                            result.inference_time_ms = inference_time;
                        },
                        Err(_) => {
                            let error_msg =
                                CString::new("Failed to extract output data").unwrap_or_default();
                            result.error_message = error_msg.into_raw();
                        },
                    }
                },
                Err(_) => {
                    let error_msg = CString::new("Inference failed").unwrap_or_default();
                    result.error_message = error_msg.into_raw();
                },
            }
        },
        Err(_) => {
            let error_msg = CString::new("Failed to create input tensor").unwrap_or_default();
            result.error_message = error_msg.into_raw();
        },
    }

    result
}

#[no_mangle]
pub unsafe extern "C" fn trustformers_ios_free_inference_result(result: *mut CInferenceResult) {
    if result.is_null() {
        return;
    }

    let result_ref = &*result;

    if !result_ref.output_data.is_null() {
        let _ = Vec::from_raw_parts(
            result_ref.output_data,
            result_ref.output_size,
            result_ref.output_size,
        );
    }

    if !result_ref.error_message.is_null() {
        let _ = CString::from_raw(result_ref.error_message);
    }
}

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

    #[test]
    fn test_ios_device_info_detection() {
        let device_info = IOsDeviceInfo::detect();
        assert!(device_info.is_ok());

        let info = device_info.expect("operation failed in test");
        assert!(!info.device_name.is_empty());
        assert!(!info.system_name.is_empty());
        assert!(info.processor_count > 0);
    }

    #[test]
    fn test_performance_tier_classification() {
        let mut device_info = IOsDeviceInfo::detect().expect("operation failed in test");

        // Test flagship tier
        device_info.gpu_family = metal::MTL_GPU_FAMILY_APPLE_7;
        assert_eq!(
            device_info.performance_tier(),
            crate::device_info::PerformanceTier::Flagship
        );

        // Test high-end tier
        device_info.gpu_family = metal::MTL_GPU_FAMILY_APPLE_5;
        assert_eq!(
            device_info.performance_tier(),
            crate::device_info::PerformanceTier::HighEnd
        );
    }

    #[test]
    fn test_ios_feature_support() {
        let device_info = IOsDeviceInfo::detect().expect("operation failed in test");

        // Core ML should be supported on all iOS devices
        assert!(device_info.supports_feature(&iOSFeature::CoreML));
        assert!(device_info.supports_feature(&iOSFeature::MetalPerformanceShaders));
    }

    #[test]
    fn test_thermal_state_variants() {
        // Test all thermal state variants
        let states = [
            ThermalState::Nominal,
            ThermalState::Fair,
            ThermalState::Serious,
            ThermalState::Critical,
        ];

        for (i, state) in states.iter().enumerate() {
            assert_eq!(*state as usize, i);
        }
    }

    #[test]
    fn test_c_api_structures() {
        // Test C API structure creation
        let config = CTrustformersConfig {
            use_coreml: true,
            use_nnapi: false,
            use_gpu: true,
            use_fp16: true,
            num_threads: 4,
            max_memory_mb: 512,
        };

        assert!(config.use_coreml);
        assert!(!config.use_nnapi);
        assert_eq!(config.num_threads, 4);
    }

    #[test]
    fn test_load_distribution_strategies() {
        let strategies = [
            LoadDistributionStrategy::RoundRobin,
            LoadDistributionStrategy::PerformanceBased,
            LoadDistributionStrategy::MemoryBased,
            LoadDistributionStrategy::Adaptive,
        ];

        for strategy in strategies {
            // Test that strategies can be cloned and debugged
            let cloned = strategy.clone();
            let debug_str = format!("{:?}", cloned);
            assert!(!debug_str.is_empty());
        }
    }
}