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
//! NNAPI Model Management and Construction
//!
//! This module handles NNAPI model creation, building, and management
//! for neural network acceleration on Android devices.

use std::os::raw::c_void;
use std::ptr;
use trustformers_core::error::{CoreError, Result};

use super::bindings::*;

/// NNAPI model builder for constructing neural network models
pub struct NNAPIModelBuilder {
    model: *mut ANeuralNetworksModel,
    operand_count: u32,
    operation_count: u32,
    finalized: bool,
}

impl NNAPIModelBuilder {
    /// Create a new NNAPI model builder
    #[cfg(target_os = "android")]
    pub fn new() -> Result<Self> {
        let mut model: *mut ANeuralNetworksModel = ptr::null_mut();

        let result = unsafe { ANeuralNetworksModel_create(&mut model) };
        if !nnapi_is_success(result) {
            return Err(TrustformersError::runtime_error(format!(
                "Failed to create NNAPI model: {}",
                nnapi_result_to_string(result)
            )).into());
        }

        Ok(Self {
            model,
            operand_count: 0,
            operation_count: 0,
            finalized: false,
        })
    }

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

    /// Add an operand to the model
    #[cfg(target_os = "android")]
    pub fn add_operand(&mut self, operand_type: &ANeuralNetworksOperandType) -> Result<u32> {
        if self.finalized {
            return Err(TrustformersError::runtime_error("Model is already finalized".into()).into());
        }

        let result = unsafe { ANeuralNetworksModel_addOperand(self.model, operand_type) };
        if !nnapi_is_success(result) {
            return Err(TrustformersError::runtime_error(format!(
                "Failed to add operand: {}",
                nnapi_result_to_string(result)
            )).into());
        }

        let operand_index = self.operand_count;
        self.operand_count += 1;
        Ok(operand_index)
    }

    #[cfg(not(target_os = "android"))]
    pub fn add_operand(&mut self, _operand_type: &ANeuralNetworksOperandType) -> Result<u32> {
        Err(TrustformersError::runtime_error(
            "NNAPI is only available on Android".into(),
        ))
    }

    /// Set operand value (for constant operands)
    #[cfg(target_os = "android")]
    pub fn set_operand_value(&mut self, index: u32, buffer: &[u8]) -> Result<()> {
        if self.finalized {
            return Err(TrustformersError::runtime_error("Model is already finalized".into()).into());
        }

        let result = unsafe {
            ANeuralNetworksModel_setOperandValue(
                self.model,
                index as i32,
                buffer.as_ptr() as *const c_void,
                buffer.len(),
            )
        };

        if !nnapi_is_success(result) {
            return Err(TrustformersError::runtime_error(format!(
                "Failed to set operand value: {}",
                nnapi_result_to_string(result)
            )).into());
        }

        Ok(())
    }

    #[cfg(not(target_os = "android"))]
    pub fn set_operand_value(&mut self, _index: u32, _buffer: &[u8]) -> Result<()> {
        Err(TrustformersError::runtime_error(
            "NNAPI is only available on Android".into(),
        ))
    }

    /// Add an operation to the model
    #[cfg(target_os = "android")]
    pub fn add_operation(
        &mut self,
        operation_type: i32,
        inputs: &[u32],
        outputs: &[u32],
    ) -> Result<()> {
        if self.finalized {
            return Err(TrustformersError::runtime_error("Model is already finalized".into()).into());
        }

        let result = unsafe {
            ANeuralNetworksModel_addOperation(
                self.model,
                operation_type,
                inputs.len() as u32,
                inputs.as_ptr(),
                outputs.len() as u32,
                outputs.as_ptr(),
            )
        };

        if !nnapi_is_success(result) {
            return Err(TrustformersError::runtime_error(format!(
                "Failed to add operation: {}",
                nnapi_result_to_string(result)
            )).into());
        }

        self.operation_count += 1;
        Ok(())
    }

    #[cfg(not(target_os = "android"))]
    pub fn add_operation(
        &mut self,
        _operation_type: i32,
        _inputs: &[u32],
        _outputs: &[u32],
    ) -> Result<()> {
        Err(TrustformersError::runtime_error(
            "NNAPI is only available on Android".into(),
        ))
    }

    /// Identify model inputs and outputs
    #[cfg(target_os = "android")]
    pub fn identify_inputs_and_outputs(&mut self, inputs: &[u32], outputs: &[u32]) -> Result<()> {
        if self.finalized {
            return Err(TrustformersError::runtime_error("Model is already finalized".into()).into());
        }

        let result = unsafe {
            ANeuralNetworksModel_identifyInputsAndOutputs(
                self.model,
                inputs.len() as u32,
                inputs.as_ptr(),
                outputs.len() as u32,
                outputs.as_ptr(),
            )
        };

        if !nnapi_is_success(result) {
            return Err(TrustformersError::runtime_error(format!(
                "Failed to identify inputs and outputs: {}",
                nnapi_result_to_string(result)
            )).into());
        }

        Ok(())
    }

    #[cfg(not(target_os = "android"))]
    pub fn identify_inputs_and_outputs(&mut self, _inputs: &[u32], _outputs: &[u32]) -> Result<()> {
        Err(TrustformersError::runtime_error(
            "NNAPI is only available on Android".into(),
        ))
    }

    /// Finalize model construction
    #[cfg(target_os = "android")]
    pub fn finalize(&mut self) -> Result<()> {
        if self.finalized {
            return Ok(().into());
        }

        let result = unsafe { ANeuralNetworksModel_finish(self.model) };
        if !nnapi_is_success(result) {
            return Err(TrustformersError::runtime_error(format!(
                "Failed to finalize model: {}",
                nnapi_result_to_string(result)
            )).into());
        }

        self.finalized = true;
        tracing::info!(
            "NNAPI model finalized with {} operands and {} operations",
            self.operand_count,
            self.operation_count
        );
        Ok(())
    }

    #[cfg(not(target_os = "android"))]
    pub fn finalize(&mut self) -> Result<()> {
        Err(TrustformersError::runtime_error(
            "NNAPI is only available on Android".into(),
        ))
    }

    /// Get the raw model pointer (for internal use)
    pub fn get_model_ptr(&self) -> *mut ANeuralNetworksModel {
        self.model
    }

    /// Check if model is finalized
    pub fn is_finalized(&self) -> bool {
        self.finalized
    }

    /// Get number of operands in the model
    pub fn get_operand_count(&self) -> u32 {
        self.operand_count
    }

    /// Get number of operations in the model
    pub fn get_operation_count(&self) -> u32 {
        self.operation_count
    }
}

impl Drop for NNAPIModelBuilder {
    fn drop(&mut self) {
        #[cfg(target_os = "android")]
        if !self.model.is_null() {
            unsafe {
                ANeuralNetworksModel_free(self.model);
            }
        }
    }
}

/// Create operand type for tensor
pub fn create_tensor_operand_type(
    data_type: i32,
    dimensions: &[u32],
    scale: f32,
    zero_point: i32,
) -> ANeuralNetworksOperandType {
    ANeuralNetworksOperandType {
        type_: data_type,
        dimensionCount: dimensions.len() as u32,
        dimensions: dimensions.as_ptr(),
        scale,
        zeroPoint: zero_point,
    }
}

/// Create operand type for scalar
pub fn create_scalar_operand_type(data_type: i32) -> ANeuralNetworksOperandType {
    ANeuralNetworksOperandType {
        type_: data_type,
        dimensionCount: 0,
        dimensions: ptr::null(),
        scale: 0.0,
        zeroPoint: 0,
    }
}

/// Example model builder: Simple Conv2D + ReLU network
pub struct ExampleConv2DModel {
    builder: NNAPIModelBuilder,
    input_index: u32,
    output_index: u32,
}

impl ExampleConv2DModel {
    /// Create a new example Conv2D model
    pub fn new() -> Result<Self> {
        let mut builder = NNAPIModelBuilder::new()?;
        let (input_index, output_index) = Self::build_model(&mut builder)?;

        Ok(Self {
            builder,
            input_index,
            output_index,
        })
    }

    /// Build the example model structure
    fn build_model(builder: &mut NNAPIModelBuilder) -> Result<(u32, u32)> {
        // Define input tensor (1x224x224x3 NHWC)
        let input_dims = [1u32, 224, 224, 3];
        let input_type = create_tensor_operand_type(
            ANEURALNETWORKS_TENSOR_FLOAT32,
            &input_dims,
            0.0,
            0,
        );
        let input_index = builder.add_operand(&input_type)?;

        // Define weight tensor (32x3x3x3 - 32 filters, 3x3 kernel, 3 input channels)
        let weight_dims = [32u32, 3, 3, 3];
        let weight_type = create_tensor_operand_type(
            ANEURALNETWORKS_TENSOR_FLOAT32,
            &weight_dims,
            0.0,
            0,
        );
        let weight_index = builder.add_operand(&weight_type)?;

        // Initialize weights (simplified - all 0.1)
        let weight_count = 32 * 3 * 3 * 3;
        let weights: Vec<f32> = vec![0.1; weight_count];
        let weight_bytes = unsafe {
            std::slice::from_raw_parts(
                weights.as_ptr() as *const u8,
                weights.len() * std::mem::size_of::<f32>(),
            )
        };
        builder.set_operand_value(weight_index, weight_bytes)?;

        // Define bias tensor (32 values)
        let bias_dims = [32u32];
        let bias_type = create_tensor_operand_type(
            ANEURALNETWORKS_TENSOR_FLOAT32,
            &bias_dims,
            0.0,
            0,
        );
        let bias_index = builder.add_operand(&bias_type)?;

        // Initialize bias (all 0.0)
        let biases: Vec<f32> = vec![0.0; 32];
        let bias_bytes = unsafe {
            std::slice::from_raw_parts(
                biases.as_ptr() as *const u8,
                biases.len() * std::mem::size_of::<f32>(),
            )
        };
        builder.set_operand_value(bias_index, bias_bytes)?;

        // Add Conv2D parameters as scalar operands
        let padding_left = 1i32;
        let padding_right = 1i32;
        let padding_top = 1i32;
        let padding_bottom = 1i32;
        let stride_width = 1i32;
        let stride_height = 1i32;
        let activation = ANEURALNETWORKS_RELU;

        let int32_type = create_scalar_operand_type(ANEURALNETWORKS_INT32);

        // Add parameter operands
        let pad_left_index = builder.add_operand(&int32_type)?;
        builder.set_operand_value(
            pad_left_index,
            unsafe { std::slice::from_raw_parts(&padding_left as *const i32 as *const u8, 4) },
        )?;

        let pad_right_index = builder.add_operand(&int32_type)?;
        builder.set_operand_value(
            pad_right_index,
            unsafe { std::slice::from_raw_parts(&padding_right as *const i32 as *const u8, 4) },
        )?;

        let pad_top_index = builder.add_operand(&int32_type)?;
        builder.set_operand_value(
            pad_top_index,
            unsafe { std::slice::from_raw_parts(&padding_top as *const i32 as *const u8, 4) },
        )?;

        let pad_bottom_index = builder.add_operand(&int32_type)?;
        builder.set_operand_value(
            pad_bottom_index,
            unsafe { std::slice::from_raw_parts(&padding_bottom as *const i32 as *const u8, 4) },
        )?;

        let stride_w_index = builder.add_operand(&int32_type)?;
        builder.set_operand_value(
            stride_w_index,
            unsafe { std::slice::from_raw_parts(&stride_width as *const i32 as *const u8, 4) },
        )?;

        let stride_h_index = builder.add_operand(&int32_type)?;
        builder.set_operand_value(
            stride_h_index,
            unsafe { std::slice::from_raw_parts(&stride_height as *const i32 as *const u8, 4) },
        )?;

        let activation_index = builder.add_operand(&int32_type)?;
        builder.set_operand_value(
            activation_index,
            unsafe { std::slice::from_raw_parts(&activation as *const i32 as *const u8, 4) },
        )?;

        // Define output tensor (1x222x222x32 NHWC after conv with padding=1, stride=1)
        let output_dims = [1u32, 222, 222, 32];
        let output_type = create_tensor_operand_type(
            ANEURALNETWORKS_TENSOR_FLOAT32,
            &output_dims,
            0.0,
            0,
        );
        let output_index = builder.add_operand(&output_type)?;

        // Add Conv2D operation
        let conv_inputs = vec![
            input_index,
            weight_index,
            bias_index,
            pad_left_index,
            pad_right_index,
            pad_top_index,
            pad_bottom_index,
            stride_w_index,
            stride_h_index,
            activation_index,
        ];
        let conv_outputs = vec![output_index];

        builder.add_operation(ANEURALNETWORKS_CONV_2D, &conv_inputs, &conv_outputs)?;

        // Identify model inputs and outputs
        builder.identify_inputs_and_outputs(&[input_index], &[output_index])?;

        // Finalize the model
        builder.finalize()?;

        tracing::info!("Example Conv2D model built successfully");
        Ok((input_index, output_index))
    }

    /// Get input operand index
    pub fn get_input_index(&self) -> u32 {
        self.input_index
    }

    /// Get output operand index
    pub fn get_output_index(&self) -> u32 {
        self.output_index
    }

    /// Get the model pointer
    pub fn get_model_ptr(&self) -> *mut ANeuralNetworksModel {
        self.builder.get_model_ptr()
    }
}

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

    #[test]
    fn test_operand_type_creation() {
        let dims = [1, 224, 224, 3];
        let tensor_type = create_tensor_operand_type(
            ANEURALNETWORKS_TENSOR_FLOAT32,
            &dims,
            0.0,
            0,
        );

        assert_eq!(tensor_type.type_, ANEURALNETWORKS_TENSOR_FLOAT32);
        assert_eq!(tensor_type.dimensionCount, 4);
        assert_eq!(tensor_type.scale, 0.0);
        assert_eq!(tensor_type.zeroPoint, 0);

        let scalar_type = create_scalar_operand_type(ANEURALNETWORKS_INT32);
        assert_eq!(scalar_type.type_, ANEURALNETWORKS_INT32);
        assert_eq!(scalar_type.dimensionCount, 0);
    }

    #[cfg(target_os = "android")]
    #[test]
    fn test_model_builder() {
        let builder = NNAPIModelBuilder::new();
        if builder.is_err() {
            // NNAPI might not be available in test environment
            return;
        }

        let mut builder = builder.expect("operation failed in test");
        assert_eq!(builder.get_operand_count(), 0);
        assert_eq!(builder.get_operation_count(), 0);
        assert!(!builder.is_finalized().into());
    }

    #[cfg(target_os = "android")]
    #[test]
    fn test_example_model() {
        let model = ExampleConv2DModel::new();
        if model.is_err() {
            // NNAPI might not be available in test environment
            return;
        }

        let model = model.expect("operation failed in test");
        assert!(!model.get_model_ptr().is_null());
        assert_eq!(model.get_input_index(), 0);
    }
}