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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! Neural Networks API (NNAPI) Backend for Android Integration
//!
//! This module provides NNAPI integration for optimized inference on Android devices,
//! leveraging Google's Neural Networks API for hardware-accelerated inference.

#[cfg(all(target_os = "android", feature = "nnapi"))]
use crate::{MemoryOptimization, MobileConfig, MobileStats};
#[cfg(all(target_os = "android", feature = "nnapi"))]
use serde::{Deserialize, Serialize};
#[cfg(all(target_os = "android", feature = "nnapi"))]
use std::collections::HashMap;
#[cfg(all(target_os = "android", feature = "nnapi"))]
use std::time::Instant;
use trustformers_core::error::{CoreError, Result};
#[cfg(all(target_os = "android", feature = "nnapi"))]
use trustformers_core::Tensor;
use trustformers_core::TrustformersError;

#[cfg(all(target_os = "android", feature = "nnapi"))]
use jni::{
    objects::{JByteArray, JClass, JObject, JString},
    sys::{jbyteArray, jlong, jobject},
    JNIEnv, JavaVM,
};

/// NNAPI inference engine for Android
#[cfg(all(target_os = "android", feature = "nnapi"))]
pub struct NNAPIEngine {
    config: NNAPIConfig,
    model_handle: Option<usize>,
    stats: NNAPIStats,
    device_info: AndroidDeviceInfo,
    jvm: Option<JavaVM>,
    compilation_handle: Option<usize>,
}

/// NNAPI configuration
#[cfg(all(target_os = "android", feature = "nnapi"))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NNAPIConfig {
    /// Preferred device types for execution
    pub preferred_devices: Vec<NNAPIDeviceType>,
    /// Enable relaxed computation for better performance
    pub allow_relaxed_computation: bool,
    /// Cache compilation results
    pub enable_compilation_caching: bool,
    /// Execution preference
    pub execution_preference: NNAPIExecutionPreference,
    /// Maximum number of concurrent executions
    pub max_concurrent_executions: usize,
    /// Memory mapping for large models
    pub use_memory_mapping: bool,
    /// Timeout for operations (milliseconds)
    pub operation_timeout_ms: u32,
}

/// NNAPI device types
#[cfg(all(target_os = "android", feature = "nnapi"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NNAPIDeviceType {
    /// CPU implementation
    CPU,
    /// GPU implementation (Vulkan, OpenGL ES)
    GPU,
    /// Dedicated neural processing unit
    NPU,
    /// Digital Signal Processor
    DSP,
    /// Accelerator (vendor-specific)
    Accelerator,
    /// Any available device
    Any,
}

/// NNAPI execution preferences
#[cfg(all(target_os = "android", feature = "nnapi"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NNAPIExecutionPreference {
    /// Prefer fast single-threaded inference
    FastSingleAnswer,
    /// Prefer sustained throughput
    SustainedSpeed,
    /// Prefer power efficiency
    LowPower,
}

/// Android device information
#[cfg(all(target_os = "android", feature = "nnapi"))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AndroidDeviceInfo {
    /// Android version (API level)
    pub android_api_level: u32,
    /// Device manufacturer
    pub manufacturer: String,
    /// Device model
    pub device_model: String,
    /// Available NNAPI devices
    pub available_devices: Vec<NNAPIDeviceInfo>,
    /// Total system memory in MB
    pub total_memory_mb: usize,
    /// Available memory in MB
    pub available_memory_mb: usize,
    /// Vulkan support
    pub has_vulkan: bool,
    /// OpenGL ES version
    pub opengl_es_version: String,
}

/// NNAPI device information
#[cfg(all(target_os = "android", feature = "nnapi"))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NNAPIDeviceInfo {
    /// Device name
    pub name: String,
    /// Device type
    pub device_type: NNAPIDeviceType,
    /// Device version
    pub version: String,
    /// Supported operations
    pub supported_operations: Vec<String>,
    /// Performance characteristics
    pub performance_info: NNAPIPerformanceInfo,
}

/// NNAPI performance information
#[cfg(all(target_os = "android", feature = "nnapi"))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NNAPIPerformanceInfo {
    /// Execution time scaling factor
    pub exec_time: f32,
    /// Power usage scaling factor
    pub power_usage: f32,
    /// Memory bandwidth
    pub memory_bandwidth_mbps: usize,
    /// Compute throughput
    pub compute_throughput_ops: usize,
}

/// NNAPI statistics
#[cfg(all(target_os = "android", feature = "nnapi"))]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NNAPIStats {
    /// Total executions performed
    pub total_executions: usize,
    /// Average execution time (ms)
    pub avg_execution_time_ms: f32,
    /// Compilation time (ms)
    pub compilation_time_ms: f32,
    /// Memory usage in MB
    pub memory_usage_mb: usize,
    /// Device utilization percentages
    pub device_utilization: HashMap<String, f32>,
    /// Power consumption estimate
    pub estimated_power_mw: f32,
    /// Cache hit rate for compilations
    pub compilation_cache_hit_rate: f32,
}

#[cfg(all(target_os = "android", feature = "nnapi"))]
impl NNAPIEngine {
    /// Create new NNAPI engine
    pub fn new(config: NNAPIConfig) -> Result<Self> {
        config.validate()?;

        let device_info = Self::detect_device_info()?;
        let stats = NNAPIStats::new();

        Ok(Self {
            config,
            model_handle: None,
            stats,
            device_info,
            jvm: None,
            compilation_handle: None,
        })
    }

    /// Initialize with Android JVM context
    pub fn init_with_jvm(&mut self, jvm: JavaVM) -> Result<()> {
        self.jvm = Some(jvm);

        // Initialize NNAPI through JNI
        self.init_nnapi_context()?;

        Ok(())
    }

    /// Load NNAPI model from data
    pub fn load_model(&mut self, model_data: &[u8]) -> Result<()> {
        let start_time = Instant::now();

        tracing::info!("Loading NNAPI model ({} bytes)", model_data.len());

        // Create NNAPI model
        let model_handle = self.create_nnapi_model(model_data)?;

        // Compile model for target devices
        let compilation_handle = self.compile_model(model_handle)?;

        self.model_handle = Some(model_handle);
        self.compilation_handle = Some(compilation_handle);

        let compilation_time = start_time.elapsed().as_millis() as f32;
        self.stats.compilation_time_ms = compilation_time;

        tracing::info!(
            "NNAPI model compiled successfully in {:.2}ms on {} devices",
            compilation_time,
            self.device_info.available_devices.len()
        );

        Ok(())
    }

    /// Execute NNAPI inference
    pub fn execute(&mut self, input: &HashMap<String, Tensor>) -> Result<HashMap<String, Tensor>> {
        if self.compilation_handle.is_none() {
            return Err(TrustformersError::runtime_error("NNAPI model not compiled".into()).into());
        }

        let start_time = Instant::now();

        // Create execution instance
        let execution_handle = self.create_execution()?;

        // Set input tensors
        self.set_input_tensors(execution_handle, input)?;

        // Prepare output tensors
        let output_tensors = self.prepare_output_tensors()?;

        // Execute inference
        self.execute_inference(execution_handle)?;

        // Retrieve results
        let results = self.get_output_tensors(execution_handle, output_tensors)?;

        // Cleanup execution
        self.cleanup_execution(execution_handle)?;

        let execution_time = start_time.elapsed().as_millis() as f32;
        self.stats.update_execution(execution_time);

        Ok(results)
    }

    /// Execute batch inference
    pub fn batch_execute(
        &mut self,
        inputs: &[HashMap<String, Tensor>],
    ) -> Result<Vec<HashMap<String, Tensor>>> {
        let mut results = Vec::with_capacity(inputs.len());

        // NNAPI doesn't have native batch support, so we execute sequentially
        // but can optimize by reusing execution instances
        for input in inputs {
            let result = self.execute(input)?;
            results.push(result);
        }

        Ok(results)
    }

    /// Get NNAPI statistics
    pub fn get_stats(&self) -> &NNAPIStats {
        &self.stats
    }

    /// Get device information
    pub fn get_device_info(&self) -> &AndroidDeviceInfo {
        &self.device_info
    }

    /// Optimize configuration for current device
    pub fn optimize_for_device(&mut self) -> Result<()> {
        // Select best devices based on available hardware
        self.config.preferred_devices = self.select_optimal_devices();

        // Adjust execution preference based on device capabilities
        self.config.execution_preference = self.select_execution_preference();

        // Enable features based on Android API level
        if self.device_info.android_api_level >= 30 {
            // Android 11+ features
            self.config.enable_compilation_caching = true;
            self.config.max_concurrent_executions = 4;
        } else if self.device_info.android_api_level >= 29 {
            // Android 10 features
            self.config.enable_compilation_caching = true;
            self.config.max_concurrent_executions = 2;
        } else {
            // Older Android versions
            self.config.enable_compilation_caching = false;
            self.config.max_concurrent_executions = 1;
        }

        tracing::info!(
            "Optimized NNAPI configuration for {} (API {}) with {} devices",
            self.device_info.device_model,
            self.device_info.android_api_level,
            self.device_info.available_devices.len()
        );

        Ok(())
    }

    // Private implementation methods

    fn detect_device_info() -> Result<AndroidDeviceInfo> {
        // This would use Android APIs to detect device information
        // For now, return a placeholder
        Ok(AndroidDeviceInfo {
            android_api_level: 30,
            manufacturer: "Google".to_string(),
            device_model: "Pixel".to_string(),
            available_devices: vec![NNAPIDeviceInfo {
                name: "CPU".to_string(),
                device_type: NNAPIDeviceType::CPU,
                version: "1.0".to_string(),
                supported_operations: vec!["CONV_2D".to_string(), "FULLY_CONNECTED".to_string()],
                performance_info: NNAPIPerformanceInfo {
                    exec_time: 1.0,
                    power_usage: 1.0,
                    memory_bandwidth_mbps: 1000,
                    compute_throughput_ops: 1000000,
                },
            }],
            total_memory_mb: 4096,
            available_memory_mb: 2048,
            has_vulkan: true,
            opengl_es_version: "3.2".to_string(),
        })
    }

    fn init_nnapi_context(&self) -> Result<()> {
        // Initialize NNAPI context through JNI
        Ok(())
    }

    fn create_nnapi_model(&self, _model_data: &[u8]) -> Result<usize> {
        // Create NNAPI model from data
        Ok(1) // Placeholder handle
    }

    fn compile_model(&self, _model_handle: usize) -> Result<usize> {
        // Compile NNAPI model for target devices
        Ok(1) // Placeholder compilation handle
    }

    fn create_execution(&self) -> Result<usize> {
        // Create NNAPI execution instance
        Ok(1) // Placeholder execution handle
    }

    fn set_input_tensors(
        &self,
        _execution_handle: usize,
        _input: &HashMap<String, Tensor>,
    ) -> Result<()> {
        // Set input tensors for execution
        Ok(())
    }

    fn prepare_output_tensors(&self) -> Result<Vec<String>> {
        // Prepare output tensor placeholders
        Ok(vec!["output".to_string()])
    }

    fn execute_inference(&self, _execution_handle: usize) -> Result<()> {
        // Execute NNAPI inference
        Ok(())
    }

    fn get_output_tensors(
        &self,
        _execution_handle: usize,
        _output_names: Vec<String>,
    ) -> Result<HashMap<String, Tensor>> {
        // Get output tensors from execution
        Ok(HashMap::new())
    }

    fn cleanup_execution(&self, _execution_handle: usize) -> Result<()> {
        // Cleanup execution resources
        Ok(())
    }

    fn select_optimal_devices(&self) -> Vec<NNAPIDeviceType> {
        let mut devices = Vec::new();

        // Prefer specialized hardware first
        for device in &self.device_info.available_devices {
            match device.device_type {
                NNAPIDeviceType::NPU => devices.push(NNAPIDeviceType::NPU),
                NNAPIDeviceType::DSP => devices.push(NNAPIDeviceType::DSP),
                NNAPIDeviceType::Accelerator => devices.push(NNAPIDeviceType::Accelerator),
                _ => {},
            }
        }

        // Add GPU if available and Vulkan is supported
        if self.device_info.has_vulkan {
            devices.push(NNAPIDeviceType::GPU);
        }

        // Always include CPU as fallback
        devices.push(NNAPIDeviceType::CPU);

        if devices.is_empty() {
            devices.push(NNAPIDeviceType::Any);
        }

        devices
    }

    fn select_execution_preference(&self) -> NNAPIExecutionPreference {
        // Select execution preference based on device characteristics
        if self.device_info.available_memory_mb < 1024 {
            NNAPIExecutionPreference::LowPower
        } else if self.device_info.available_devices.len() > 2 {
            NNAPIExecutionPreference::SustainedSpeed
        } else {
            NNAPIExecutionPreference::FastSingleAnswer
        }
    }
}

#[cfg(all(target_os = "android", feature = "nnapi"))]
impl Default for NNAPIConfig {
    fn default() -> Self {
        Self {
            preferred_devices: vec![NNAPIDeviceType::Any],
            allow_relaxed_computation: true,
            enable_compilation_caching: true,
            execution_preference: NNAPIExecutionPreference::FastSingleAnswer,
            max_concurrent_executions: 1,
            use_memory_mapping: true,
            operation_timeout_ms: 5000,
        }
    }
}

#[cfg(all(target_os = "android", feature = "nnapi"))]
impl NNAPIConfig {
    /// Validate NNAPI configuration
    pub fn validate(&self) -> Result<()> {
        if self.preferred_devices.is_empty() {
            return Err(TrustformersError::config_error {
                message: "Must specify at least one preferred device".to_string(),
                context: trustformers_core::error::ErrorContext::new(
                    trustformers_core::error::ErrorCode::E4001,
                    "validate".to_string(),
                ),
            });
        }

        if self.max_concurrent_executions == 0 {
            return Err(TrustformersError::config_error {
                message: "Concurrent executions must be > 0".to_string(),
                context: trustformers_core::error::ErrorContext::new(
                    trustformers_core::error::ErrorCode::E4001,
                    "validate".to_string(),
                ),
            });
        }

        if self.max_concurrent_executions > 8 {
            return Err(TrustformersError::config_error {
                message: "Too many concurrent executions for Android".to_string(),
                context: trustformers_core::error::ErrorContext::new(
                    trustformers_core::error::ErrorCode::E4001,
                    "validate".to_string(),
                ),
            });
        }

        if self.operation_timeout_ms < 100 {
            return Err(TrustformersError::config_error {
                message: "Operation timeout too short".to_string(),
                context: trustformers_core::error::ErrorContext::new(
                    trustformers_core::error::ErrorCode::E4001,
                    "validate".to_string(),
                ),
            });
        }

        Ok(())
    }

    /// Create power-optimized configuration
    pub fn power_optimized() -> Self {
        Self {
            preferred_devices: vec![
                NNAPIDeviceType::NPU,
                NNAPIDeviceType::DSP,
                NNAPIDeviceType::CPU,
            ],
            allow_relaxed_computation: true,
            enable_compilation_caching: true,
            execution_preference: NNAPIExecutionPreference::LowPower,
            max_concurrent_executions: 1,
            use_memory_mapping: false,
            operation_timeout_ms: 10000,
        }
    }

    /// Create performance-optimized configuration
    pub fn performance_optimized() -> Self {
        Self {
            preferred_devices: vec![
                NNAPIDeviceType::GPU,
                NNAPIDeviceType::NPU,
                NNAPIDeviceType::CPU,
            ],
            allow_relaxed_computation: true,
            enable_compilation_caching: true,
            execution_preference: NNAPIExecutionPreference::SustainedSpeed,
            max_concurrent_executions: 4,
            use_memory_mapping: true,
            operation_timeout_ms: 2000,
        }
    }
}

#[cfg(all(target_os = "android", feature = "nnapi"))]
impl NNAPIStats {
    fn new() -> Self {
        Self {
            total_executions: 0,
            avg_execution_time_ms: 0.0,
            compilation_time_ms: 0.0,
            memory_usage_mb: 0,
            device_utilization: HashMap::new(),
            estimated_power_mw: 0.0,
            compilation_cache_hit_rate: 0.0,
        }
    }

    fn update_execution(&mut self, execution_time_ms: f32) {
        self.total_executions += 1;

        // Update running average
        let alpha = 0.1;
        if self.total_executions == 1 {
            self.avg_execution_time_ms = execution_time_ms;
        } else {
            self.avg_execution_time_ms =
                alpha * execution_time_ms + (1.0 - alpha) * self.avg_execution_time_ms;
        }
    }
}

/// Convert mobile config to NNAPI config
#[cfg(all(target_os = "android", feature = "nnapi"))]
pub fn mobile_config_to_nnapi(mobile_config: &MobileConfig) -> NNAPIConfig {
    let mut nnapi_config = NNAPIConfig::default();

    // Map memory optimization to NNAPI settings
    match mobile_config.memory_optimization {
        MemoryOptimization::Maximum => {
            nnapi_config = NNAPIConfig::power_optimized();
            nnapi_config.max_concurrent_executions = 1;
            nnapi_config.use_memory_mapping = false;
        },
        MemoryOptimization::Balanced => {
            nnapi_config.execution_preference = NNAPIExecutionPreference::FastSingleAnswer;
            nnapi_config.max_concurrent_executions = 2;
            nnapi_config.use_memory_mapping = true;
        },
        MemoryOptimization::Minimal => {
            nnapi_config = NNAPIConfig::performance_optimized();
            nnapi_config.max_concurrent_executions = mobile_config.num_threads.max(1);
        },
    }

    // Enable relaxed computation for FP16
    nnapi_config.allow_relaxed_computation = mobile_config.use_fp16;

    nnapi_config
}

/// JNI exports for Android integration
#[cfg(all(target_os = "android", feature = "nnapi"))]
#[no_mangle]
pub extern "system" fn Java_com_trustformers_NNAPIEngine_createEngine(
    env: JNIEnv,
    _class: JClass,
    config_json: JString,
) -> jlong {
    let config_str: String = match env.get_string(config_json) {
        Ok(s) => s.into(),
        Err(_) => return 0,
    };

    match serde_json::from_str::<NNAPIConfig>(&config_str) {
        Ok(config) => match NNAPIEngine::new(config) {
            Ok(engine) => Box::into_raw(Box::new(engine)) as jlong,
            Err(_) => 0,
        },
        Err(_) => 0,
    }
}

#[cfg(all(target_os = "android", feature = "nnapi"))]
#[no_mangle]
pub extern "system" fn Java_com_trustformers_NNAPIEngine_loadModel(
    _env: JNIEnv,
    _class: JClass,
    engine_ptr: jlong,
    model_data: jbyteArray,
) -> jlong {
    if engine_ptr == 0 {
        return 0;
    }

    let engine = unsafe { &mut *(engine_ptr as *mut NNAPIEngine) };

    // Convert Java byte array to Rust slice
    // This is a simplified implementation - real implementation would handle JNI properly
    let model_bytes = vec![0u8; 1024]; // Placeholder

    match engine.load_model(&model_bytes) {
        Ok(_) => 1,
        Err(_) => 0,
    }
}

#[cfg(all(target_os = "android", feature = "nnapi"))]
#[no_mangle]
pub extern "system" fn Java_com_trustformers_NNAPIEngine_execute(
    _env: JNIEnv,
    _class: JClass,
    engine_ptr: jlong,
    input_data: jobject,
) -> jobject {
    if engine_ptr == 0 {
        return std::ptr::null_mut();
    }

    let engine = unsafe { &mut *(engine_ptr as *mut NNAPIEngine) };

    // Convert Java input to HashMap<String, Tensor>
    // This is a simplified implementation
    let input = HashMap::new();

    match engine.execute(&input) {
        Ok(_output) => {
            // Convert output to Java object
            std::ptr::null_mut() // Placeholder
        },
        Err(_) => std::ptr::null_mut(),
    }
}

// Stub implementations for non-Android platforms

#[cfg(not(all(target_os = "android", feature = "nnapi")))]
pub struct NNAPIEngine;

#[cfg(not(all(target_os = "android", feature = "nnapi")))]
impl NNAPIEngine {
    pub fn new(_config: ()) -> Result<Self> {
        Err(TrustformersError::runtime_error("NNAPI only available on Android".into()).into())
    }
}

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

    #[cfg(all(target_os = "android", feature = "nnapi"))]
    #[test]
    fn test_nnapi_config_validation() {
        let config = NNAPIConfig::default();
        assert!(config.validate().is_ok());

        let mut invalid_config = config.clone();
        invalid_config.preferred_devices.clear();
        assert!(invalid_config.validate().is_err());

        invalid_config.preferred_devices.push(NNAPIDeviceType::CPU);
        invalid_config.max_concurrent_executions = 0;
        assert!(invalid_config.validate().is_err());
    }

    #[cfg(all(target_os = "android", feature = "nnapi"))]
    #[test]
    fn test_optimized_configs() {
        let power_config = NNAPIConfig::power_optimized();
        assert_eq!(
            power_config.execution_preference,
            NNAPIExecutionPreference::LowPower
        );
        assert_eq!(power_config.max_concurrent_executions, 1);
        assert!(!power_config.use_memory_mapping);

        let perf_config = NNAPIConfig::performance_optimized();
        assert_eq!(
            perf_config.execution_preference,
            NNAPIExecutionPreference::SustainedSpeed
        );
        assert_eq!(perf_config.max_concurrent_executions, 4);
        assert!(perf_config.use_memory_mapping);
    }

    #[cfg(all(target_os = "android", feature = "nnapi"))]
    #[test]
    fn test_mobile_to_nnapi_config_conversion() {
        let mobile_config = crate::MobileConfig {
            memory_optimization: MemoryOptimization::Maximum,
            num_threads: 1,
            use_fp16: true,
            ..Default::default()
        };

        let nnapi_config = mobile_config_to_nnapi(&mobile_config);
        assert_eq!(
            nnapi_config.execution_preference,
            NNAPIExecutionPreference::LowPower
        );
        assert_eq!(nnapi_config.max_concurrent_executions, 1);
        assert!(nnapi_config.allow_relaxed_computation);
        assert!(!nnapi_config.use_memory_mapping);
    }
}