oxigdal-ml 0.1.6

Machine learning capabilities for OxiGDAL - ONNX Runtime integration for geospatial ML workflows
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! TensorFlow Lite model support
//!
//! This module provides TensorFlow Lite model loading and inference integration
//! for geospatial ML workflows.
//!
//! # System Requirements
//!
//! To use TFLite support, enable the `tflite` feature flag:
//!
//! ```toml
//! [dependencies]
//! oxigdal-ml = { version = "0.1.0", features = ["tflite"] }
//! ```
//!
//! **Note:** The `tflitec` crate requires either:
//! - Pre-built TensorFlow Lite C library (set `TFLITEC_PREBUILT_PATH` env var)
//! - Bazel 6.5.0+ for building from source (may take 10-30 minutes on first build)
//!
//! To use a pre-built TensorFlow Lite library:
//! ```bash
//! export TFLITEC_PREBUILT_PATH=/path/to/libtensorflowlite_c.{so,dylib,dll}
//! cargo build --features tflite
//! ```
//!
//! # Features
//!
//! - TFLite model loading and initialization
//! - Hardware acceleration delegates (GPU, NNAPI, CoreML, XNNPACK)
//! - Quantized model support (INT8, FP16)
//! - Dynamic shape tensors
//! - Multi-threading support
//!
//! # Example
//!
//! ```ignore
//! use oxigdal_ml::models::TfLiteModel;
//! use oxigdal_ml::models::tflite::{TfLiteConfig, Delegate};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = TfLiteConfig::builder()
//!     .threads(4)
//!     .delegate(Delegate::Gpu)
//!     .build();
//!
//! let model = TfLiteModel::from_file("model.tflite", config)?;
//! # Ok(())
//! # }
//! ```

use crate::error::{InferenceError, MlError, ModelError, Result};
use crate::models::{Model, ModelMetadata};
use oxigdal_core::buffer::RasterBuffer;
use oxigdal_core::types::RasterDataType;
use std::path::Path;
use std::sync::Mutex;
use tracing::{debug, info, warn};

#[cfg(feature = "tflite")]
use tflitec::{interpreter::Interpreter, model::Model as TfLiteModelInner, options::Options};

/// TensorFlow Lite execution delegate
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Delegate {
    /// CPU execution (default)
    Cpu,
    /// GPU delegate (OpenGL/Metal/Vulkan)
    Gpu,
    /// NNAPI delegate (Android)
    Nnapi,
    /// CoreML delegate (iOS/macOS)
    CoreMl,
    /// XNNPACK delegate (optimized CPU)
    Xnnpack,
}

/// TensorFlow Lite configuration
#[derive(Debug, Clone)]
pub struct TfLiteConfig {
    /// Number of threads for CPU execution
    pub threads: usize,
    /// Execution delegate
    pub delegate: Delegate,
    /// Enable profiling
    pub profiling: bool,
    /// Allow FP16 precision reduction
    pub allow_fp16: bool,
    /// Allocate tensors on demand
    pub lazy_allocation: bool,
}

impl Default for TfLiteConfig {
    fn default() -> Self {
        Self {
            threads: num_cpus::get(),
            delegate: Delegate::Cpu,
            profiling: false,
            allow_fp16: true,
            lazy_allocation: false,
        }
    }
}

impl TfLiteConfig {
    /// Creates a new configuration builder
    #[must_use]
    pub fn builder() -> TfLiteConfigBuilder {
        TfLiteConfigBuilder::default()
    }
}

/// Builder for TensorFlow Lite configuration
#[derive(Debug, Default)]
pub struct TfLiteConfigBuilder {
    threads: Option<usize>,
    delegate: Option<Delegate>,
    profiling: bool,
    allow_fp16: bool,
    lazy_allocation: bool,
}

impl TfLiteConfigBuilder {
    /// Sets the number of threads
    #[must_use]
    pub fn threads(mut self, threads: usize) -> Self {
        self.threads = Some(threads);
        self
    }

    /// Sets the execution delegate
    #[must_use]
    pub fn delegate(mut self, delegate: Delegate) -> Self {
        self.delegate = Some(delegate);
        self
    }

    /// Enables profiling
    #[must_use]
    pub fn profiling(mut self, enable: bool) -> Self {
        self.profiling = enable;
        self
    }

    /// Allows FP16 precision reduction
    #[must_use]
    pub fn allow_fp16(mut self, allow: bool) -> Self {
        self.allow_fp16 = allow;
        self
    }

    /// Enables lazy tensor allocation
    #[must_use]
    pub fn lazy_allocation(mut self, enable: bool) -> Self {
        self.lazy_allocation = enable;
        self
    }

    /// Builds the configuration
    #[must_use]
    pub fn build(self) -> TfLiteConfig {
        TfLiteConfig {
            threads: self.threads.unwrap_or_else(num_cpus::get),
            delegate: self.delegate.unwrap_or(Delegate::Cpu),
            profiling: self.profiling,
            allow_fp16: self.allow_fp16,
            lazy_allocation: self.lazy_allocation,
        }
    }
}

/// TensorFlow Lite tensor information
#[derive(Debug, Clone)]
pub struct TensorInfo {
    /// Tensor name
    pub name: String,
    /// Tensor shape (NHWC format)
    pub shape: Vec<usize>,
    /// Data type
    pub dtype: TensorDataType,
    /// Quantization parameters (if quantized)
    pub quantization: Option<QuantizationParams>,
}

/// Tensor data type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TensorDataType {
    /// 32-bit floating point
    Float32,
    /// 16-bit floating point
    Float16,
    /// 8-bit signed integer (quantized)
    Int8,
    /// 8-bit unsigned integer (quantized)
    UInt8,
    /// 16-bit signed integer
    Int16,
    /// 32-bit signed integer
    Int32,
    /// 64-bit signed integer
    Int64,
}

impl TensorDataType {
    /// Converts to OxiGDAL raster data type
    #[must_use]
    pub fn to_raster_type(self) -> RasterDataType {
        match self {
            Self::Float32 => RasterDataType::Float32,
            Self::Float16 => RasterDataType::Float32, // No FP16 in OxiGDAL yet
            Self::Int8 | Self::UInt8 => RasterDataType::UInt8,
            Self::Int16 => RasterDataType::Int16,
            Self::Int32 => RasterDataType::Int32,
            Self::Int64 => RasterDataType::Float64, // Closest match
        }
    }
}

/// Quantization parameters for quantized tensors
#[derive(Debug, Clone, Copy)]
pub struct QuantizationParams {
    /// Scale factor
    pub scale: f32,
    /// Zero point
    pub zero_point: i32,
}

impl QuantizationParams {
    /// Dequantizes a quantized value
    #[must_use]
    pub fn dequantize(&self, quantized: i32) -> f32 {
        (quantized - self.zero_point) as f32 * self.scale
    }

    /// Quantizes a floating-point value
    #[must_use]
    pub fn quantize(&self, value: f32) -> i32 {
        (value / self.scale).round() as i32 + self.zero_point
    }
}

/// TensorFlow Lite model wrapper
#[cfg(feature = "tflite")]
pub struct TfLiteModel {
    /// Model file path
    path: String,
    /// Model configuration
    config: TfLiteConfig,
    /// Model metadata
    metadata: ModelMetadata,
    /// Input tensor information
    input_info: Vec<TensorInfo>,
    /// Output tensor information
    output_info: Vec<TensorInfo>,
    /// TFLite model (kept alive for interpreter)
    #[allow(dead_code)]
    tflite_model: TfLiteModelInner,
    /// TFLite interpreter for inference (wrapped in Mutex for interior mutability)
    interpreter: Mutex<Interpreter>,
}

#[cfg(feature = "tflite")]
impl TfLiteModel {
    /// Loads a TensorFlow Lite model from file
    ///
    /// # Errors
    /// Returns an error if the model cannot be loaded or initialized
    pub fn from_file<P: AsRef<Path>>(path: P, config: TfLiteConfig) -> Result<Self> {
        let path = path.as_ref();
        info!("Loading TFLite model from {:?}", path);

        if !path.exists() {
            return Err(ModelError::NotFound {
                path: path.display().to_string(),
            }
            .into());
        }

        debug!(
            "TFLite config: threads={}, delegate={:?}",
            config.threads, config.delegate
        );

        // Load TFLite model
        let tflite_model = TfLiteModelInner::new(path).map_err(|e| ModelError::LoadFailed {
            path: path.display().to_string(),
            reason: format!("TFLite model loading failed: {}", e),
        })?;

        // Create interpreter options
        let mut options = Options::default();
        options.thread_count = config.threads as i32;

        // Create interpreter
        let mut interpreter =
            Interpreter::new(&tflite_model, Some(options)).map_err(|e| ModelError::LoadFailed {
                path: path.display().to_string(),
                reason: format!("TFLite interpreter creation failed: {}", e),
            })?;

        // Allocate tensors
        interpreter
            .allocate_tensors()
            .map_err(|e| ModelError::LoadFailed {
                path: path.display().to_string(),
                reason: format!("TFLite tensor allocation failed: {}", e),
            })?;

        // Extract input tensor information
        let input_count = interpreter.input_tensor_count();
        let mut input_info = Vec::with_capacity(input_count);
        let mut input_names = Vec::with_capacity(input_count);

        for i in 0..input_count {
            let tensor = interpreter.input(i).ok_or_else(|| ModelError::LoadFailed {
                path: path.display().to_string(),
                reason: format!("Failed to get input tensor {}", i),
            })?;

            let name = tensor
                .name()
                .unwrap_or_else(|| format!("input_{}", i))
                .to_string();
            let shape = tensor.shape().iter().map(|&s| s as usize).collect();
            let dtype = Self::convert_tensor_type(tensor.data_type());
            let quantization = Self::get_quantization_params(&tensor);

            input_names.push(name.clone());
            input_info.push(TensorInfo {
                name,
                shape,
                dtype,
                quantization,
            });
        }

        // Extract output tensor information
        let output_count = interpreter.output_tensor_count();
        let mut output_info = Vec::with_capacity(output_count);
        let mut output_names = Vec::with_capacity(output_count);

        for i in 0..output_count {
            let tensor = interpreter
                .output(i)
                .ok_or_else(|| ModelError::LoadFailed {
                    path: path.display().to_string(),
                    reason: format!("Failed to get output tensor {}", i),
                })?;

            let name = tensor
                .name()
                .unwrap_or_else(|| format!("output_{}", i))
                .to_string();
            let shape = tensor.shape().iter().map(|&s| s as usize).collect();
            let dtype = Self::convert_tensor_type(tensor.data_type());
            let quantization = Self::get_quantization_params(&tensor);

            output_names.push(name.clone());
            output_info.push(TensorInfo {
                name,
                shape,
                dtype,
                quantization,
            });
        }

        // Create metadata
        let (input_shape, output_shape) =
            if let (Some(input), Some(output)) = (input_info.first(), output_info.first()) {
                let in_shape = &input.shape;
                let out_shape = &output.shape;

                // Extract CHW from NHWC
                let input_shape = if in_shape.len() == 4 {
                    (in_shape[3], in_shape[1], in_shape[2])
                } else {
                    (3, 256, 256)
                };

                let output_shape = if out_shape.len() == 4 {
                    (out_shape[3], out_shape[1], out_shape[2])
                } else {
                    (1, 256, 256)
                };

                (input_shape, output_shape)
            } else {
                ((3, 256, 256), (1, 256, 256))
            };

        let metadata = ModelMetadata {
            name: path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("unknown")
                .to_string(),
            version: "1.0".to_string(),
            description: "TensorFlow Lite model".to_string(),
            input_names,
            output_names,
            input_shape,
            output_shape,
            class_labels: None,
        };

        info!(
            "Successfully loaded TFLite model: {} inputs, {} outputs",
            input_count, output_count
        );

        Ok(Self {
            path: path.display().to_string(),
            config,
            metadata,
            input_info,
            output_info,
            tflite_model,
            interpreter: Mutex::new(interpreter),
        })
    }

    /// Loads a quantized TensorFlow Lite model
    ///
    /// # Errors
    /// Returns an error if the model cannot be loaded
    pub fn from_file_quantized<P: AsRef<Path>>(path: P, config: TfLiteConfig) -> Result<Self> {
        let mut model = Self::from_file(path, config)?;

        // Mark tensors as quantized (placeholder)
        for tensor in &mut model.input_info {
            tensor.quantization = Some(QuantizationParams {
                scale: 0.003921569, // 1/255
                zero_point: 0,
            });
        }

        Ok(model)
    }

    /// Returns the TFLite configuration
    #[must_use]
    pub fn config(&self) -> &TfLiteConfig {
        &self.config
    }

    /// Returns input tensor information
    #[must_use]
    pub fn input_info(&self) -> &[TensorInfo] {
        &self.input_info
    }

    /// Returns output tensor information
    #[must_use]
    pub fn output_info(&self) -> &[TensorInfo] {
        &self.output_info
    }

    /// Runs inference with raw tensor data
    ///
    /// # Errors
    /// Returns an error if inference fails
    pub fn invoke(&self, input_tensor: &[f32]) -> Result<Vec<f32>> {
        debug!(
            "Running TFLite inference on {} input values",
            input_tensor.len()
        );

        let mut interpreter = self
            .interpreter
            .lock()
            .map_err(|_| InferenceError::Failed {
                reason: "Failed to lock interpreter mutex".to_string(),
            })?;

        // Get input tensor and validate size
        let input = interpreter.input(0).ok_or_else(|| InferenceError::Failed {
            reason: "Failed to get input tensor".to_string(),
        })?;

        let expected_size: usize = input.shape().iter().map(|&s| s as usize).product();
        if input_tensor.len() != expected_size {
            return Err(InferenceError::Failed {
                reason: format!(
                    "Input size mismatch: expected {}, got {}",
                    expected_size,
                    input_tensor.len()
                ),
            }
            .into());
        }

        // Copy input data to tensor
        interpreter
            .copy(0, input_tensor)
            .map_err(|e| InferenceError::Failed {
                reason: format!("Failed to copy input data: {}", e),
            })?;

        // Run inference
        interpreter.invoke().map_err(|e| InferenceError::Failed {
            reason: format!("TFLite inference failed: {}", e),
        })?;

        // Extract output data
        let output = interpreter
            .output(0)
            .ok_or_else(|| InferenceError::Failed {
                reason: "Failed to get output tensor".to_string(),
            })?;

        let output_data = output.data::<f32>().ok_or_else(|| InferenceError::Failed {
            reason: "Failed to extract output data as f32".to_string(),
        })?;

        Ok(output_data.to_vec())
    }

    /// Checks if the model uses quantization
    #[must_use]
    pub fn is_quantized(&self) -> bool {
        self.input_info.iter().any(|t| t.quantization.is_some())
            || self.output_info.iter().any(|t| t.quantization.is_some())
    }

    /// Gets the delegate in use
    #[must_use]
    pub fn delegate(&self) -> Delegate {
        self.config.delegate
    }

    /// Helper: Converts TFLite tensor type to our enum
    fn convert_tensor_type(tflite_type: tflitec::tensor::DataType) -> TensorDataType {
        use tflitec::tensor::DataType;
        match tflite_type {
            DataType::Float32 => TensorDataType::Float32,
            DataType::Float16 => TensorDataType::Float16,
            DataType::Int8 => TensorDataType::Int8,
            DataType::UInt8 => TensorDataType::UInt8,
            DataType::Int16 => TensorDataType::Int16,
            DataType::Int32 => TensorDataType::Int32,
            DataType::Int64 => TensorDataType::Int64,
            _ => TensorDataType::Float32, // Default fallback
        }
    }

    /// Helper: Extracts quantization parameters from tensor
    fn get_quantization_params(tensor: &tflitec::tensor::Tensor) -> Option<QuantizationParams> {
        let params = tensor.quantization_params()?;
        if params.scale.is_empty() || params.zero_point.is_empty() {
            return None;
        }

        Some(QuantizationParams {
            scale: params.scale[0],
            zero_point: params.zero_point[0],
        })
    }
}

#[cfg(feature = "tflite")]
impl Model for TfLiteModel {
    fn metadata(&self) -> &ModelMetadata {
        &self.metadata
    }

    fn predict(&self, input: &RasterBuffer) -> Result<RasterBuffer> {
        debug!(
            "TFLite prediction on {}x{} raster",
            input.width(),
            input.height()
        );

        let (channels, height, width) = self.input_shape();

        // Validate input dimensions
        if input.width() as usize != width || input.height() as usize != height {
            warn!(
                "Input size mismatch: expected {}x{}, got {}x{}",
                width,
                height,
                input.width(),
                input.height()
            );
        }

        // Convert raster to tensor format (NHWC)
        let mut tensor_data = Vec::with_capacity(height * width * channels);
        for _c in 0..channels {
            for y in 0..height {
                for x in 0..width {
                    let value = input.get_pixel(x as u64, y as u64).unwrap_or(0.0);
                    tensor_data.push(value as f32);
                }
            }
        }

        // Run inference
        let output_tensor = self.invoke(&tensor_data)?;

        // Convert output tensor back to raster
        let (_out_channels, out_height, out_width) = self.output_shape();
        let output_type = self.output_info[0].dtype.to_raster_type();
        let mut output = RasterBuffer::zeros(out_width as u64, out_height as u64, output_type);

        // Copy first channel (assuming single-channel output for segmentation)
        for y in 0..out_height {
            for x in 0..out_width {
                let idx = y * out_width + x;
                if let Some(&value) = output_tensor.get(idx) {
                    let _ = output.set_pixel(x as u64, y as u64, value as f64);
                }
            }
        }

        Ok(output)
    }

    fn predict_batch(&self, inputs: &[RasterBuffer]) -> Result<Vec<RasterBuffer>> {
        debug!("TFLite batch prediction on {} inputs", inputs.len());

        // Process each input sequentially (true batching requires model support)
        inputs.iter().map(|input| self.predict(input)).collect()
    }

    fn input_shape(&self) -> (usize, usize, usize) {
        if let Some(info) = self.input_info.first() {
            // NHWC format: [batch, height, width, channels]
            let shape = &info.shape;
            if shape.len() == 4 {
                return (shape[3], shape[1], shape[2]); // (C, H, W)
            }
        }
        (3, 256, 256) // Default
    }

    fn output_shape(&self) -> (usize, usize, usize) {
        if let Some(info) = self.output_info.first() {
            let shape = &info.shape;
            if shape.len() == 4 {
                return (shape[3], shape[1], shape[2]); // (C, H, W)
            }
        }
        (2, 256, 256) // Default
    }
}

/// Placeholder module when tflite feature is not enabled
#[cfg(not(feature = "tflite"))]
pub struct TfLiteModel;

#[cfg(not(feature = "tflite"))]
impl TfLiteModel {
    /// Returns an error indicating TFLite support is not enabled
    ///
    /// # Errors
    /// Always returns an error when the feature is not enabled
    pub fn from_file<P: AsRef<Path>>(_path: P, _config: TfLiteConfig) -> Result<Self> {
        Err(MlError::FeatureNotAvailable {
            feature: "TensorFlow Lite support".to_string(),
            flag: "tflite".to_string(),
        })
    }

    /// Returns an error indicating TFLite support is not enabled
    ///
    /// # Errors
    /// Always returns an error when the feature is not enabled
    pub fn from_file_quantized<P: AsRef<Path>>(_path: P, _config: TfLiteConfig) -> Result<Self> {
        Err(MlError::FeatureNotAvailable {
            feature: "TensorFlow Lite support".to_string(),
            flag: "tflite".to_string(),
        })
    }
}

// Helper function to get number of CPUs
#[cfg(not(feature = "tflite"))]
mod num_cpus {
    pub fn get() -> usize {
        std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(4)
    }
}

#[cfg(feature = "tflite")]
mod num_cpus {
    pub fn get() -> usize {
        std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(4)
    }
}

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

    #[test]
    fn test_tflite_config_builder() {
        let config = TfLiteConfig::builder()
            .threads(8)
            .delegate(Delegate::Gpu)
            .profiling(true)
            .allow_fp16(false)
            .build();

        assert_eq!(config.threads, 8);
        assert_eq!(config.delegate, Delegate::Gpu);
        assert!(config.profiling);
        assert!(!config.allow_fp16);
    }

    #[test]
    fn test_quantization_params() {
        let params = QuantizationParams {
            scale: 0.1,
            zero_point: 128,
        };

        // Test quantization
        let value = 5.0;
        let quantized = params.quantize(value);
        assert_eq!(quantized, 178); // round(5.0 / 0.1) + 128

        // Test dequantization
        let dequantized = params.dequantize(quantized);
        assert!((dequantized - value).abs() < 0.2);
    }

    #[test]
    fn test_tensor_dtype_conversion() {
        assert_eq!(
            TensorDataType::Float32.to_raster_type(),
            RasterDataType::Float32
        );
        assert_eq!(
            TensorDataType::UInt8.to_raster_type(),
            RasterDataType::UInt8
        );
    }

    #[test]
    fn test_delegate_variants() {
        let delegates = vec![
            Delegate::Cpu,
            Delegate::Gpu,
            Delegate::Nnapi,
            Delegate::CoreMl,
            Delegate::Xnnpack,
        ];

        for delegate in delegates {
            let config = TfLiteConfig::builder().delegate(delegate).build();
            assert_eq!(config.delegate, delegate);
        }
    }

    #[cfg(not(feature = "tflite"))]
    #[test]
    fn test_tflite_not_available() {
        let config = TfLiteConfig::default();
        let result = TfLiteModel::from_file("model.tflite", config);
        assert!(result.is_err());

        if let Err(MlError::FeatureNotAvailable { feature, flag }) = result {
            assert!(feature.contains("TensorFlow Lite"));
            assert_eq!(flag, "tflite");
        }
    }

    #[cfg(feature = "tflite")]
    #[test]
    fn test_model_not_found() {
        use std::env;
        let temp_dir = env::temp_dir();
        let nonexistent_path = temp_dir.join("nonexistent_model.tflite");

        let config = TfLiteConfig::default();
        let result = TfLiteModel::from_file(&nonexistent_path, config);

        assert!(result.is_err());
        if let Err(MlError::Model(ModelError::NotFound { path })) = result {
            assert!(path.contains("nonexistent_model.tflite"));
        } else {
            panic!("Expected NotFound error");
        }
    }

    #[cfg(feature = "tflite")]
    #[test]
    fn test_tensor_info_structure() {
        let info = TensorInfo {
            name: "test_tensor".to_string(),
            shape: vec![1, 224, 224, 3],
            dtype: TensorDataType::Float32,
            quantization: None,
        };

        assert_eq!(info.name, "test_tensor");
        assert_eq!(info.shape.len(), 4);
        assert_eq!(info.shape[0], 1); // batch
        assert_eq!(info.shape[1], 224); // height
        assert_eq!(info.shape[2], 224); // width
        assert_eq!(info.shape[3], 3); // channels
        assert!(info.quantization.is_none());
    }

    #[cfg(feature = "tflite")]
    #[test]
    fn test_quantized_tensor_info() {
        let quant_params = QuantizationParams {
            scale: 0.003921569,
            zero_point: 0,
        };

        let info = TensorInfo {
            name: "quantized_input".to_string(),
            shape: vec![1, 128, 128, 3],
            dtype: TensorDataType::UInt8,
            quantization: Some(quant_params),
        };

        assert!(info.quantization.is_some());
        if let Some(params) = info.quantization {
            assert!((params.scale - 0.003921569).abs() < 1e-7);
            assert_eq!(params.zero_point, 0);
        }
    }

    #[cfg(feature = "tflite")]
    #[test]
    fn test_config_default_values() {
        let config = TfLiteConfig::default();

        assert!(config.threads > 0);
        assert_eq!(config.delegate, Delegate::Cpu);
        assert!(!config.profiling);
        assert!(config.allow_fp16);
        assert!(!config.lazy_allocation);
    }

    #[cfg(feature = "tflite")]
    #[test]
    fn test_config_custom_values() {
        let config = TfLiteConfig::builder()
            .threads(16)
            .delegate(Delegate::Xnnpack)
            .profiling(true)
            .allow_fp16(false)
            .lazy_allocation(true)
            .build();

        assert_eq!(config.threads, 16);
        assert_eq!(config.delegate, Delegate::Xnnpack);
        assert!(config.profiling);
        assert!(!config.allow_fp16);
        assert!(config.lazy_allocation);
    }

    #[test]
    fn test_all_delegates() {
        // Test all delegate variants are valid
        let all_delegates = [
            Delegate::Cpu,
            Delegate::Gpu,
            Delegate::Nnapi,
            Delegate::CoreMl,
            Delegate::Xnnpack,
        ];

        for delegate in &all_delegates {
            let config = TfLiteConfig::builder().delegate(*delegate).build();
            assert_eq!(config.delegate, *delegate);
        }
    }

    #[test]
    fn test_tensor_dtype_conversions() {
        let types = [
            (TensorDataType::Float32, RasterDataType::Float32),
            (TensorDataType::Float16, RasterDataType::Float32),
            (TensorDataType::Int8, RasterDataType::UInt8),
            (TensorDataType::UInt8, RasterDataType::UInt8),
            (TensorDataType::Int16, RasterDataType::Int16),
            (TensorDataType::Int32, RasterDataType::Int32),
            (TensorDataType::Int64, RasterDataType::Float64),
        ];

        for (tensor_type, expected_raster_type) in types {
            assert_eq!(tensor_type.to_raster_type(), expected_raster_type);
        }
    }

    #[test]
    fn test_quantization_round_trip() {
        let params = QuantizationParams {
            scale: 0.1,
            zero_point: 128,
        };

        let test_values = [-10.0, 0.0, 5.0, 10.0, 25.5];

        for &value in &test_values {
            let quantized = params.quantize(value);
            let dequantized = params.dequantize(quantized);
            // Allow small error due to rounding
            assert!(
                (dequantized - value).abs() < 0.2,
                "Round-trip failed for value {}: got {}",
                value,
                dequantized
            );
        }
    }

    #[test]
    fn test_quantization_edge_cases() {
        let params = QuantizationParams {
            scale: 1.0,
            zero_point: 0,
        };

        // Test identity transform when scale=1, zero_point=0
        assert_eq!(params.quantize(5.0), 5);
        assert_eq!(params.quantize(-5.0), -5);
        assert!((params.dequantize(10) - 10.0).abs() < 1e-6);
    }
}