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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! Android RenderScript Legacy Support
//!
//! This module provides RenderScript support for older Android devices that don't
//! support modern compute APIs like Vulkan. RenderScript is deprecated but still
//! widely used for compute tasks on older Android versions.
//!
//! Note: RenderScript was deprecated in Android API 31, but this implementation
//! provides backward compatibility for devices running Android 7.0+ (API 24-30).

use crate::{MobileBackend, MobileConfig, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_float, c_int, c_uint, c_void};
use std::ptr;
use std::sync::Arc;
use trustformers_core::{CoreError, Tensor};

/// RenderScript configuration for Android
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RenderScriptConfig {
    /// Enable RenderScript compute support
    pub enable_compute: bool,
    /// Use RenderScript for matrix operations
    pub matrix_operations: bool,
    /// Use RenderScript for convolutions
    pub convolution_operations: bool,
    /// Use RenderScript for pooling operations
    pub pooling_operations: bool,
    /// Use RenderScript for activation functions
    pub activation_functions: bool,
    /// Maximum work group size
    pub max_work_group_size: u32,
    /// Target API level for compatibility
    pub target_api_level: u32,
    /// Enable performance profiling
    pub enable_profiling: bool,
    /// Memory allocation strategy
    pub allocation_strategy: RSAllocationStrategy,
}

impl Default for RenderScriptConfig {
    fn default() -> Self {
        Self {
            enable_compute: true,
            matrix_operations: true,
            convolution_operations: true,
            pooling_operations: true,
            activation_functions: true,
            max_work_group_size: 1024,
            target_api_level: 24, // Android 7.0
            enable_profiling: false,
            allocation_strategy: RSAllocationStrategy::Shared,
        }
    }
}

/// RenderScript allocation strategies
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum RSAllocationStrategy {
    /// Use shared memory allocations
    Shared,
    /// Use graphics surface allocations
    Graphics,
    /// Use usage-specific allocations
    Usage,
}

/// RenderScript kernel types
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum RSKernelType {
    /// Matrix multiplication kernel
    MatMul,
    /// 2D Convolution kernel
    Conv2D,
    /// Depthwise convolution kernel
    DepthwiseConv2D,
    /// Max pooling kernel
    MaxPool2D,
    /// Average pooling kernel
    AvgPool2D,
    /// ReLU activation kernel
    ReLU,
    /// Sigmoid activation kernel
    Sigmoid,
    /// Tanh activation kernel
    Tanh,
    /// Element-wise addition kernel
    Add,
    /// Element-wise multiplication kernel
    Mul,
    /// Batch normalization kernel
    BatchNorm,
    /// Softmax kernel
    Softmax,
}

/// Android RenderScript engine
pub struct AndroidRenderScriptEngine {
    config: RenderScriptConfig,
    mobile_config: MobileConfig,
    rs_context: Option<RSContext>,
    compiled_kernels: HashMap<RSKernelType, CompiledKernel>,
    allocation_cache: HashMap<String, RSAllocation>,
    stats: RenderScriptStats,
}

impl AndroidRenderScriptEngine {
    /// Create new RenderScript engine
    pub fn new(config: RenderScriptConfig, mobile_config: MobileConfig) -> Result<Self> {
        let mut engine = Self {
            config,
            mobile_config,
            rs_context: None,
            compiled_kernels: HashMap::new(),
            allocation_cache: HashMap::new(),
            stats: RenderScriptStats::default(),
        };

        // Initialize RenderScript context
        engine.initialize_renderscript()?;

        // Compile essential kernels
        engine.compile_kernels()?;

        Ok(engine)
    }

    /// Initialize RenderScript context
    fn initialize_renderscript(&mut self) -> Result<()> {
        // Check API level compatibility
        if self.config.target_api_level > 30 {
            return Err(TrustformersError::invalid_input(
                "RenderScript is not supported on API level > 30".to_string(),
            )
            .into());
        }

        // Create RenderScript context
        let context = RSContext::create(self.config.target_api_level)?;
        self.rs_context = Some(context);

        self.log_rs_event("RenderScript context initialized");
        Ok(())
    }

    /// Compile RenderScript kernels
    fn compile_kernels(&mut self) -> Result<()> {
        let context = self.rs_context.as_ref().ok_or_else(|| {
            TrustformersError::InvalidState("RenderScript context not initialized".to_string())
        })?;

        // Compile kernels based on configuration
        if self.config.matrix_operations {
            self.compile_kernel(context, RSKernelType::MatMul)?;
        }

        if self.config.convolution_operations {
            self.compile_kernel(context, RSKernelType::Conv2D)?;
            self.compile_kernel(context, RSKernelType::DepthwiseConv2D)?;
        }

        if self.config.pooling_operations {
            self.compile_kernel(context, RSKernelType::MaxPool2D)?;
            self.compile_kernel(context, RSKernelType::AvgPool2D)?;
        }

        if self.config.activation_functions {
            self.compile_kernel(context, RSKernelType::ReLU)?;
            self.compile_kernel(context, RSKernelType::Sigmoid)?;
            self.compile_kernel(context, RSKernelType::Tanh)?;
        }

        // Always compile basic operations
        self.compile_kernel(context, RSKernelType::Add)?;
        self.compile_kernel(context, RSKernelType::Mul)?;
        self.compile_kernel(context, RSKernelType::BatchNorm)?;
        self.compile_kernel(context, RSKernelType::Softmax)?;

        self.log_rs_event(
            &format!(
                "Compiled {} RenderScript kernels",
                self.compiled_kernels.len()
            )
            .into(),
        );
        Ok(())
    }

    /// Compile a specific kernel
    fn compile_kernel(&mut self, context: &RSContext, kernel_type: RSKernelType) -> Result<()> {
        let kernel_source = self.get_kernel_source(kernel_type);
        let compiled = CompiledKernel::compile(context, kernel_type, &kernel_source)?;
        self.compiled_kernels.insert(kernel_type, compiled);
        Ok(())
    }

    /// Get RenderScript kernel source code
    fn get_kernel_source(&self, kernel_type: RSKernelType) -> String {
        match kernel_type {
            RSKernelType::MatMul => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

rs_allocation gInputA;
rs_allocation gInputB;
int gWidthA, gHeightA, gWidthB;

float __attribute__((kernel)) matmul(uint32_t x, uint32_t y) {
    float sum = 0.0f;
    for (int k = 0; k < gWidthA; k++) {
        float a = rsGetElementAt_float(gInputA, k, y);
        float b = rsGetElementAt_float(gInputB, x, k);
        sum += a * b;
    }
    return sum;
}
"#.to_string(),

            RSKernelType::Conv2D => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

rs_allocation gInput;
rs_allocation gKernel;
int gInputWidth, gInputHeight, gKernelSize, gStride, gPadding;

float __attribute__((kernel)) conv2d(uint32_t x, uint32_t y) {
    float sum = 0.0f;
    for (int ky = 0; ky < gKernelSize; ky++) {
        for (int kx = 0; kx < gKernelSize; kx++) {
            int ix = x * gStride + kx - gPadding;
            int iy = y * gStride + ky - gPadding;

            if (ix >= 0 && ix < gInputWidth && iy >= 0 && iy < gInputHeight) {
                float input_val = rsGetElementAt_float(gInput, ix, iy);
                float kernel_val = rsGetElementAt_float(gKernel, kx, ky);
                sum += input_val * kernel_val;
            }
        }
    }
    return sum;
}
"#.to_string(),

            RSKernelType::ReLU => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

float __attribute__((kernel)) relu(float in) {
    return fmax(0.0f, in);
}
"#.to_string(),

            RSKernelType::Sigmoid => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

float __attribute__((kernel)) sigmoid(float in) {
    return 1.0f / (1.0f + exp(-in));
}
"#.to_string(),

            RSKernelType::Tanh => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

float __attribute__((kernel)) tanh_activation(float in) {
    return tanh(in);
}
"#.to_string(),

            RSKernelType::MaxPool2D => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

rs_allocation gInput;
int gInputWidth, gInputHeight, gPoolSize, gStride;

float __attribute__((kernel)) maxpool2d(uint32_t x, uint32_t y) {
    float max_val = -FLT_MAX;
    for (int py = 0; py < gPoolSize; py++) {
        for (int px = 0; px < gPoolSize; px++) {
            int ix = x * gStride + px;
            int iy = y * gStride + py;

            if (ix < gInputWidth && iy < gInputHeight) {
                float val = rsGetElementAt_float(gInput, ix, iy);
                max_val = fmax(max_val, val);
            }
        }
    }
    return max_val;
}
"#.to_string(),

            RSKernelType::AvgPool2D => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

rs_allocation gInput;
int gInputWidth, gInputHeight, gPoolSize, gStride;

float __attribute__((kernel)) avgpool2d(uint32_t x, uint32_t y) {
    float sum = 0.0f;
    int count = 0;
    for (int py = 0; py < gPoolSize; py++) {
        for (int px = 0; px < gPoolSize; px++) {
            int ix = x * gStride + px;
            int iy = y * gStride + py;

            if (ix < gInputWidth && iy < gInputHeight) {
                float val = rsGetElementAt_float(gInput, ix, iy);
                sum += val;
                count++;
            }
        }
    }
    return count > 0 ? sum / count : 0.0f;
}
"#.to_string(),

            RSKernelType::Add => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

float __attribute__((kernel)) add(float a, float b) {
    return a + b;
}
"#.to_string(),

            RSKernelType::Mul => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

float __attribute__((kernel)) mul(float a, float b) {
    return a * b;
}
"#.to_string(),

            RSKernelType::BatchNorm => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

float gMean, gVariance, gGamma, gBeta, gEpsilon;

float __attribute__((kernel)) batchnorm(float in) {
    float normalized = (in - gMean) / sqrt(gVariance + gEpsilon);
    return gGamma * normalized + gBeta;
}
"#.to_string(),

            RSKernelType::Softmax => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

rs_allocation gInput;
float gMaxVal, gSum;
int gSize;

float __attribute__((kernel)) softmax(uint32_t x) {
    float val = rsGetElementAt_float(gInput, x, 0);
    return exp(val - gMaxVal) / gSum;
}
"#.to_string(),

            RSKernelType::DepthwiseConv2D => r#"
#pragma version(1)
#pragma rs java_package_name(com.trustformers.renderscript)

rs_allocation gInput;
rs_allocation gKernel;
int gInputWidth, gInputHeight, gKernelSize, gStride, gPadding, gChannel;

float __attribute__((kernel)) depthwise_conv2d(uint32_t x, uint32_t y) {
    float sum = 0.0f;
    for (int ky = 0; ky < gKernelSize; ky++) {
        for (int kx = 0; kx < gKernelSize; kx++) {
            int ix = x * gStride + kx - gPadding;
            int iy = y * gStride + ky - gPadding;

            if (ix >= 0 && ix < gInputWidth && iy >= 0 && iy < gInputHeight) {
                float input_val = rsGetElementAt_float(gInput, ix + iy * gInputWidth + gChannel * gInputWidth * gInputHeight);
                float kernel_val = rsGetElementAt_float(gKernel, kx + ky * gKernelSize + gChannel * gKernelSize * gKernelSize);
                sum += input_val * kernel_val;
            }
        }
    }
    return sum;
}
"#.to_string(),
        }
    }

    /// Execute RenderScript kernel
    pub fn execute_kernel(
        &mut self,
        kernel_type: RSKernelType,
        inputs: &[&Tensor],
        output_shape: &[usize],
        params: Option<RSKernelParams>,
    ) -> Result<Tensor> {
        let context = self.rs_context.as_ref().ok_or_else(|| {
            TrustformersError::InvalidState("RenderScript context not initialized".to_string())
        })?;

        let kernel = self.compiled_kernels.get(&kernel_type).ok_or_else(|| {
            TrustformersError::InvalidState(format!("Kernel {:?} not compiled", kernel_type))
        })?;

        // Create allocations for inputs and output
        let mut input_allocations = Vec::new();
        for input in inputs {
            let allocation = self.create_allocation(context, input)?;
            input_allocations.push(allocation);
        }

        let output_allocation = self.create_output_allocation(context, output_shape)?;

        // Set kernel parameters
        if let Some(params) = params {
            self.set_kernel_params(kernel, &params)?;
        }

        // Bind input allocations
        for (i, allocation) in input_allocations.iter().enumerate() {
            kernel.bind_allocation(i, allocation)?;
        }

        // Execute kernel
        let launch_params = RSLaunchParams::from_shape(output_shape);
        kernel.launch(&launch_params)?;

        // Copy result back to CPU
        let result = self.allocation_to_tensor(&output_allocation, output_shape)?;

        // Update statistics
        self.stats.total_kernel_executions += 1;
        self.stats
            .kernel_type_counts
            .entry(kernel_type)
            .and_modify(|count| *count += 1)
            .or_insert(1);

        Ok(result)
    }

    /// Create RenderScript allocation from tensor
    fn create_allocation(&mut self, context: &RSContext, tensor: &Tensor) -> Result<RSAllocation> {
        let allocation =
            RSAllocation::create(context, tensor.shape(), self.config.allocation_strategy)?;

        // Copy tensor data to allocation
        allocation.copy_from_tensor(tensor)?;

        Ok(allocation)
    }

    /// Create output allocation
    fn create_output_allocation(
        &self,
        context: &RSContext,
        shape: &[usize],
    ) -> Result<RSAllocation> {
        RSAllocation::create(context, shape, self.config.allocation_strategy)
    }

    /// Set kernel parameters
    fn set_kernel_params(&self, kernel: &CompiledKernel, params: &RSKernelParams) -> Result<()> {
        match params {
            RSKernelParams::Conv2D {
                stride,
                padding,
                kernel_size,
            } => {
                kernel.set_int_param("gStride", *stride as i32)?;
                kernel.set_int_param("gPadding", *padding as i32)?;
                kernel.set_int_param("gKernelSize", *kernel_size as i32)?;
            },
            RSKernelParams::Pool2D { pool_size, stride } => {
                kernel.set_int_param("gPoolSize", *pool_size as i32)?;
                kernel.set_int_param("gStride", *stride as i32)?;
            },
            RSKernelParams::BatchNorm {
                mean,
                variance,
                gamma,
                beta,
                epsilon,
            } => {
                kernel.set_float_param("gMean", *mean)?;
                kernel.set_float_param("gVariance", *variance)?;
                kernel.set_float_param("gGamma", *gamma)?;
                kernel.set_float_param("gBeta", *beta)?;
                kernel.set_float_param("gEpsilon", *epsilon)?;
            },
            RSKernelParams::MatMul {
                width_a,
                height_a,
                width_b,
            } => {
                kernel.set_int_param("gWidthA", *width_a as i32)?;
                kernel.set_int_param("gHeightA", *height_a as i32)?;
                kernel.set_int_param("gWidthB", *width_b as i32)?;
            },
            RSKernelParams::Softmax { max_val, sum } => {
                kernel.set_float_param("gMaxVal", *max_val)?;
                kernel.set_float_param("gSum", *sum)?;
            },
        }
        Ok(())
    }

    /// Convert allocation back to tensor
    fn allocation_to_tensor(&self, allocation: &RSAllocation, shape: &[usize]) -> Result<Tensor> {
        allocation.to_tensor(shape)
    }

    /// Log RenderScript events
    fn log_rs_event(&self, message: &str) {
        if self.config.enable_profiling {
            println!("[RenderScript] {}", message);
        }
    }

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

    /// Check RenderScript availability
    pub fn is_available() -> bool {
        // Check if RenderScript is available on this device
        // This would query the Android system in a real implementation
        true // Placeholder
    }

    /// Get supported API level
    pub fn get_api_level() -> u32 {
        // Get the device's API level
        28 // Placeholder - Android 9.0
    }
}

/// RenderScript context wrapper
struct RSContext {
    context: *mut c_void,
    api_level: u32,
}

impl RSContext {
    fn create(api_level: u32) -> Result<Self> {
        // Create RenderScript context
        // This would call native RenderScript APIs
        Ok(Self {
            context: ptr::null_mut(), // Placeholder
            api_level,
        })
    }
}

/// Compiled RenderScript kernel
struct CompiledKernel {
    script: *mut c_void,
    kernel_type: RSKernelType,
}

impl CompiledKernel {
    fn compile(context: &RSContext, kernel_type: RSKernelType, source: &str) -> Result<Self> {
        // Compile RenderScript kernel from source
        // This would use RenderScript compiler APIs
        Ok(Self {
            script: ptr::null_mut(), // Placeholder
            kernel_type,
        })
    }

    fn bind_allocation(&self, index: usize, allocation: &RSAllocation) -> Result<()> {
        // Bind allocation to kernel parameter
        Ok(())
    }

    fn set_int_param(&self, name: &str, value: i32) -> Result<()> {
        // Set integer parameter
        Ok(())
    }

    fn set_float_param(&self, name: &str, value: f32) -> Result<()> {
        // Set float parameter
        Ok(())
    }

    fn launch(&self, params: &RSLaunchParams) -> Result<()> {
        // Launch kernel execution
        Ok(())
    }
}

/// RenderScript allocation wrapper
struct RSAllocation {
    allocation: *mut c_void,
    shape: Vec<usize>,
}

impl RSAllocation {
    fn create(
        context: &RSContext,
        shape: &[usize],
        strategy: RSAllocationStrategy,
    ) -> Result<Self> {
        // Create RenderScript allocation
        Ok(Self {
            allocation: ptr::null_mut(), // Placeholder
            shape: shape.to_vec(),
        })
    }

    fn copy_from_tensor(&self, tensor: &Tensor) -> Result<()> {
        // Copy tensor data to allocation
        Ok(())
    }

    fn to_tensor(&self, shape: &[usize]) -> Result<Tensor> {
        // Copy allocation data back to tensor
        // Placeholder implementation
        Tensor::zeros(shape, trustformers_core::DType::F32)
    }
}

/// RenderScript launch parameters
struct RSLaunchParams {
    dimensions: Vec<u32>,
}

impl RSLaunchParams {
    fn from_shape(shape: &[usize]) -> Self {
        Self {
            dimensions: shape.iter().map(|&s| s as u32).collect(),
        }
    }
}

/// RenderScript kernel parameters
#[derive(Debug, Clone)]
pub enum RSKernelParams {
    Conv2D {
        stride: usize,
        padding: usize,
        kernel_size: usize,
    },
    Pool2D {
        pool_size: usize,
        stride: usize,
    },
    BatchNorm {
        mean: f32,
        variance: f32,
        gamma: f32,
        beta: f32,
        epsilon: f32,
    },
    MatMul {
        width_a: usize,
        height_a: usize,
        width_b: usize,
    },
    Softmax {
        max_val: f32,
        sum: f32,
    },
}

/// RenderScript statistics
#[derive(Debug, Clone, Default)]
pub struct RenderScriptStats {
    pub total_kernel_executions: usize,
    pub kernel_type_counts: HashMap<RSKernelType, usize>,
    pub total_allocation_count: usize,
    pub peak_memory_usage_mb: f32,
}

impl RenderScriptStats {
    /// Get most used kernel type
    pub fn most_used_kernel(&self) -> Option<RSKernelType> {
        self.kernel_type_counts
            .iter()
            .max_by_key(|(_, &count)| count)
            .map(|(&kernel_type, _)| kernel_type)
    }

    /// Get kernel usage distribution
    pub fn kernel_distribution(&self) -> HashMap<RSKernelType, f32> {
        let total = self.total_kernel_executions as f32;
        if total == 0.0 {
            return HashMap::new();
        }

        self.kernel_type_counts
            .iter()
            .map(|(&kernel_type, &count)| (kernel_type, count as f32 / total))
            .collect()
    }
}

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

    #[test]
    fn test_renderscript_config_default() {
        let config = RenderScriptConfig::default();
        assert!(config.enable_compute);
        assert!(config.matrix_operations);
        assert_eq!(config.target_api_level, 24);
    }

    #[test]
    fn test_renderscript_availability() {
        // Note: This would fail on non-Android platforms
        // In a real implementation, this would properly check platform
        let available = AndroidRenderScriptEngine::is_available();
        // For testing purposes, we just check that the function exists
        let _ = available;
    }

    #[test]
    fn test_renderscript_stats() {
        let mut stats = RenderScriptStats::default();
        stats.total_kernel_executions = 100;
        stats.kernel_type_counts.insert(RSKernelType::Conv2D, 60);
        stats.kernel_type_counts.insert(RSKernelType::ReLU, 40);

        assert_eq!(stats.most_used_kernel(), Some(RSKernelType::Conv2D).into());

        let distribution = stats.kernel_distribution();
        assert_eq!(distribution.get(&RSKernelType::Conv2D), Some(&0.6));
        assert_eq!(distribution.get(&RSKernelType::ReLU), Some(&0.4));
    }

    #[test]
    fn test_kernel_source_generation() {
        let config = RenderScriptConfig::default();
        let mobile_config = MobileConfig::default();

        // This would fail in a real test environment without Android
        // but tests that the engine structure is correct
        let result = AndroidRenderScriptEngine::new(config, mobile_config);
        // We expect this to fail on non-Android platforms
        // In a real implementation, this would be properly mocked
    }
}