trustformers-core 0.1.1

Core traits and utilities for TrustformeRS
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
// ONNX Runtime inference backend for optimized model execution

use crate::errors::TrustformersError;
use crate::tensor::Tensor;
use anyhow::{anyhow, Result};
use std::collections::HashMap;
use std::path::Path;

/// ONNX Runtime session for model inference
#[derive(Debug)]
pub struct ONNXRuntimeSession {
    #[allow(dead_code)]
    session_config: ONNXRuntimeConfig,
    input_names: Vec<String>,
    output_names: Vec<String>,
    providers: Vec<ExecutionProvider>,
    model_path: String,
}

/// Configuration for ONNX Runtime
#[derive(Debug, Clone)]
pub struct ONNXRuntimeConfig {
    pub inter_op_num_threads: Option<usize>,
    pub intra_op_num_threads: Option<usize>,
    pub enable_cpu_mem_arena: bool,
    pub enable_mem_pattern: bool,
    pub execution_mode: ExecutionMode,
    pub graph_optimization_level: GraphOptimizationLevel,
    pub log_severity_level: LogLevel,
}

impl Default for ONNXRuntimeConfig {
    fn default() -> Self {
        Self {
            inter_op_num_threads: None,
            intra_op_num_threads: None,
            enable_cpu_mem_arena: true,
            enable_mem_pattern: true,
            execution_mode: ExecutionMode::Sequential,
            graph_optimization_level: GraphOptimizationLevel::All,
            log_severity_level: LogLevel::Warning,
        }
    }
}

/// Execution providers for different hardware backends
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum ExecutionProvider {
    CPU,
    CUDA { device_id: Option<i32> },
    TensorRT { device_id: Option<i32> },
    OpenVINO,
    DirectML,
    CoreML,
}

/// Execution modes
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum ExecutionMode {
    Sequential,
    Parallel,
}

/// Graph optimization levels
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum GraphOptimizationLevel {
    None,
    Basic,
    Extended,
    All,
}

/// Logging levels
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum LogLevel {
    Verbose,
    Info,
    Warning,
    Error,
    Fatal,
}

/// ONNX Runtime inference backend
pub struct ONNXRuntimeBackend {
    config: ONNXRuntimeConfig,
}

impl Default for ONNXRuntimeBackend {
    fn default() -> Self {
        Self::new()
    }
}

impl ONNXRuntimeBackend {
    /// Create a new ONNX Runtime backend
    pub fn new() -> Self {
        Self {
            config: ONNXRuntimeConfig::default(),
        }
    }

    /// Create with custom configuration
    pub fn with_config(config: ONNXRuntimeConfig) -> Self {
        Self { config }
    }

    /// Load a model from ONNX file
    pub fn load_model<P: AsRef<Path>>(&self, model_path: P) -> Result<ONNXRuntimeSession> {
        let model_path = model_path.as_ref();

        if !model_path.exists() {
            return Err(anyhow!("ONNX model file not found: {:?}", model_path));
        }

        // Validate file format
        if model_path.extension().and_then(|s| s.to_str()) != Some("onnx") {
            return Err(anyhow!("Invalid file format. Expected .onnx file"));
        }

        // Extract model metadata (in a real implementation, this would parse the ONNX protobuf)
        let (input_names, output_names) = self.extract_model_metadata(model_path)?;

        // Determine available execution providers
        let providers = self.get_available_providers();

        Ok(ONNXRuntimeSession {
            session_config: self.config.clone(),
            input_names,
            output_names,
            providers,
            model_path: model_path.to_string_lossy().to_string(),
        })
    }

    /// Extract input/output names from ONNX model with enhanced metadata parsing
    fn extract_model_metadata<P: AsRef<Path>>(
        &self,
        model_path: P,
    ) -> Result<(Vec<String>, Vec<String>)> {
        let model_path = model_path.as_ref();

        // Try to parse ONNX model file for actual metadata
        if model_path.exists() && model_path.extension().is_some_and(|ext| ext == "onnx") {
            // Read model file and attempt basic metadata extraction
            match std::fs::read(model_path) {
                Ok(model_bytes) => {
                    // Look for common ONNX metadata patterns in the binary data
                    let model_str = String::from_utf8_lossy(&model_bytes);
                    let mut input_names = Vec::new();
                    let mut output_names = Vec::new();

                    // Enhanced pattern matching for ONNX model inputs/outputs
                    let common_input_patterns = [
                        "input_ids",
                        "attention_mask",
                        "token_type_ids",
                        "position_ids",
                        "inputs",
                        "input",
                        "x",
                        "data",
                        "image",
                        "pixel_values",
                        "input_features",
                        "encoder_input",
                        "decoder_input",
                    ];

                    let common_output_patterns = [
                        "logits",
                        "output",
                        "outputs",
                        "predictions",
                        "scores",
                        "last_hidden_state",
                        "hidden_states",
                        "pooler_output",
                        "encoder_output",
                        "decoder_output",
                        "classification_head",
                    ];

                    // Search for input patterns
                    for pattern in &common_input_patterns {
                        if model_str.contains(pattern) {
                            input_names.push(pattern.to_string());
                        }
                    }

                    // Search for output patterns
                    for pattern in &common_output_patterns {
                        if model_str.contains(pattern) {
                            output_names.push(pattern.to_string());
                        }
                    }

                    // If we found patterns, return them
                    if !input_names.is_empty() && !output_names.is_empty() {
                        // Remove duplicates and limit to most common ones
                        input_names.truncate(4);
                        output_names.truncate(3);
                        return Ok((input_names, output_names));
                    }
                },
                Err(_) => {
                    log::warn!("Failed to read ONNX model file for metadata extraction");
                },
            }
        }

        // Enhanced fallback based on model filename heuristics
        let model_name =
            model_path.file_stem().and_then(|s| s.to_str()).unwrap_or("").to_lowercase();

        let (input_names, output_names) =
            if model_name.contains("bert") || model_name.contains("transformer") {
                // BERT-like models
                (
                    vec![
                        "input_ids".to_string(),
                        "attention_mask".to_string(),
                        "token_type_ids".to_string(),
                    ],
                    vec!["last_hidden_state".to_string(), "pooler_output".to_string()],
                )
            } else if model_name.contains("gpt") || model_name.contains("llama") {
                // Generative models
                (
                    vec!["input_ids".to_string(), "attention_mask".to_string()],
                    vec!["logits".to_string()],
                )
            } else if model_name.contains("vision") || model_name.contains("vit") {
                // Vision models
                (
                    vec!["pixel_values".to_string()],
                    vec!["logits".to_string(), "features".to_string()],
                )
            } else if model_name.contains("audio") || model_name.contains("wav2vec") {
                // Audio models
                (
                    vec!["input_features".to_string(), "attention_mask".to_string()],
                    vec!["logits".to_string(), "hidden_states".to_string()],
                )
            } else {
                // Default transformer-like model
                (
                    vec!["input_ids".to_string(), "attention_mask".to_string()],
                    vec!["logits".to_string()],
                )
            };

        Ok((input_names, output_names))
    }

    /// Get available execution providers on this system
    fn get_available_providers(&self) -> Vec<ExecutionProvider> {
        let mut providers = vec![ExecutionProvider::CPU];

        // Check for CUDA availability (simplified check)
        if std::env::var("CUDA_VISIBLE_DEVICES").is_ok()
            || std::path::Path::new("/usr/local/cuda").exists()
        {
            providers.push(ExecutionProvider::CUDA { device_id: Some(0) });
        }

        // Check for other providers based on system capabilities
        if cfg!(target_os = "windows") {
            providers.push(ExecutionProvider::DirectML);
        }

        if cfg!(target_os = "macos") {
            providers.push(ExecutionProvider::CoreML);
        }

        providers
    }

    /// Create optimized session configuration
    pub fn create_session_options(&self) -> ONNXSessionOptions {
        ONNXSessionOptions {
            execution_providers: self.get_available_providers(),
            inter_op_num_threads: self.config.inter_op_num_threads,
            intra_op_num_threads: self.config.intra_op_num_threads,
            enable_cpu_mem_arena: self.config.enable_cpu_mem_arena,
            enable_mem_pattern: self.config.enable_mem_pattern,
            execution_mode: self.config.execution_mode.clone(),
            graph_optimization_level: self.config.graph_optimization_level.clone(),
            log_severity_level: self.config.log_severity_level.clone(),
        }
    }
}

/// Session options for ONNX Runtime
#[derive(Debug, Clone)]
pub struct ONNXSessionOptions {
    pub execution_providers: Vec<ExecutionProvider>,
    pub inter_op_num_threads: Option<usize>,
    pub intra_op_num_threads: Option<usize>,
    pub enable_cpu_mem_arena: bool,
    pub enable_mem_pattern: bool,
    pub execution_mode: ExecutionMode,
    pub graph_optimization_level: GraphOptimizationLevel,
    pub log_severity_level: LogLevel,
}

impl ONNXRuntimeSession {
    /// Run inference on the loaded model
    pub fn run(&self, inputs: HashMap<String, Tensor>) -> Result<HashMap<String, Tensor>> {
        // Validate inputs
        for input_name in &self.input_names {
            if !inputs.contains_key(input_name) {
                return Err(anyhow!("Missing required input: {}", input_name));
            }
        }

        // Check for extra inputs
        for input_name in inputs.keys() {
            if !self.input_names.contains(input_name) {
                return Err(anyhow!("Unknown input: {}", input_name));
            }
        }

        // In a real implementation, this would call into ONNX Runtime C++ API
        // For now, simulate inference with placeholder results
        self.simulate_inference(inputs)
    }

    /// Simulate inference with realistic outputs based on model type and input characteristics
    fn simulate_inference(
        &self,
        inputs: HashMap<String, Tensor>,
    ) -> Result<HashMap<String, Tensor>> {
        let mut outputs = HashMap::new();
        use scirs2_core::random::*;
        let mut rng = thread_rng();

        // Determine model type from output names and generate appropriate simulated outputs
        if self.output_names.contains(&"logits".to_string()) {
            // Language model or classification model
            if let Some(input_ids) = inputs.get("input_ids") {
                let input_shape = input_ids.shape();
                let batch_size = input_shape[0];
                let seq_length = if input_shape.len() > 1 { input_shape[1] } else { 1 };

                // Determine vocab size based on model type heuristics
                let vocab_size = if self.model_path.to_lowercase().contains("gpt2") {
                    50257 // GPT-2 vocab size
                } else if self.model_path.to_lowercase().contains("bert") {
                    30522 // BERT vocab size
                } else if self.model_path.to_lowercase().contains("llama") {
                    32000 // LLaMA vocab size (approximate)
                } else {
                    50000 // Default vocab size
                };

                // Generate realistic logits with some variation
                let logits_shape = vec![batch_size, seq_length, vocab_size];
                let mut logits_data = Vec::with_capacity(batch_size * seq_length * vocab_size);

                for _ in 0..(batch_size * seq_length * vocab_size) {
                    // Generate logits with realistic distribution (mean around 0, std around 2)
                    let logit: f32 = rng.random_range(-6.0..6.0) * (rng.random::<f32>().powf(0.5));
                    logits_data.push(logit);
                }

                let logits = Tensor::from_vec(logits_data, &logits_shape)?;
                outputs.insert("logits".to_string(), logits);
            }
        }

        if self.output_names.contains(&"last_hidden_state".to_string()) {
            // BERT-like encoder model
            if let Some(input_ids) = inputs.get("input_ids") {
                let input_shape = input_ids.shape();
                let batch_size = input_shape[0];
                let seq_length = if input_shape.len() > 1 { input_shape[1] } else { 1 };
                let hidden_size = 768; // Common BERT hidden size

                let hidden_shape = vec![batch_size, seq_length, hidden_size];
                let mut hidden_data = Vec::with_capacity(batch_size * seq_length * hidden_size);

                for _ in 0..(batch_size * seq_length * hidden_size) {
                    // Generate hidden states with realistic activation patterns
                    let activation: f32 = rng.random_range(-2.0..2.0) * rng.random::<f32>().sqrt();
                    hidden_data.push(activation);
                }

                let hidden_states = Tensor::from_vec(hidden_data, &hidden_shape)?;
                outputs.insert("last_hidden_state".to_string(), hidden_states);
            }
        }

        if self.output_names.contains(&"pooler_output".to_string()) {
            // BERT pooler output for classification
            if let Some(input_ids) = inputs.get("input_ids") {
                let input_shape = input_ids.shape();
                let batch_size = input_shape[0];
                let hidden_size = 768;

                let pooler_shape = vec![batch_size, hidden_size];
                let mut pooler_data = Vec::with_capacity(batch_size * hidden_size);

                for _ in 0..(batch_size * hidden_size) {
                    // Pooler outputs are typically post-tanh, so range [-1, 1]
                    let pooled: f32 = (rng.random_range(-3.0f32..3.0f32)).tanh();
                    pooler_data.push(pooled);
                }

                let pooler_output = Tensor::from_vec(pooler_data, &pooler_shape)?;
                outputs.insert("pooler_output".to_string(), pooler_output);
            }
        }

        if self.output_names.contains(&"features".to_string())
            || inputs.contains_key("pixel_values")
        {
            // Vision model
            if let Some(pixel_values) = inputs.get("pixel_values") {
                let input_shape = pixel_values.shape();
                let batch_size = input_shape[0];
                let feature_dim = 2048; // Common vision model feature dimension

                let features_shape = vec![batch_size, feature_dim];
                let mut features_data = Vec::with_capacity(batch_size * feature_dim);

                for _ in 0..(batch_size * feature_dim) {
                    // Vision features are often ReLU-activated, so mostly positive
                    let feature: f32 = rng.random_range(0.0..5.0) * rng.random::<f32>().sqrt();
                    features_data.push(feature.max(0.0));
                }

                let features = Tensor::from_vec(features_data, &features_shape)?;
                outputs.insert("features".to_string(), features);
            }
        }

        // If no specific outputs were generated, create a generic output
        if outputs.is_empty() {
            let first_input = inputs.values().next().ok_or_else(|| {
                TrustformersError::other("Model must have at least one input".to_string())
            })?;
            let input_shape = first_input.shape();
            let batch_size = input_shape[0];

            // Create a generic classification output
            let num_classes = 1000; // ImageNet-style classification
            let output_shape = vec![batch_size, num_classes];
            let mut output_data = Vec::with_capacity(batch_size * num_classes);

            for _ in 0..(batch_size * num_classes) {
                let score: f32 = rng.random_range(-10.0..10.0) * rng.random::<f32>().powf(2.0);
                output_data.push(score);
            }

            let generic_output = Tensor::from_vec(output_data, &output_shape)?;
            outputs.insert("output".to_string(), generic_output);
        }

        Ok(outputs)
    }

    /// Get input names
    pub fn input_names(&self) -> &[String] {
        &self.input_names
    }

    /// Get output names
    pub fn output_names(&self) -> &[String] {
        &self.output_names
    }

    /// Get model path
    pub fn model_path(&self) -> &str {
        &self.model_path
    }

    /// Get available execution providers
    pub fn execution_providers(&self) -> &[ExecutionProvider] {
        &self.providers
    }

    /// Run inference with specific execution provider
    pub fn run_with_provider(
        &self,
        inputs: HashMap<String, Tensor>,
        provider: ExecutionProvider,
    ) -> Result<HashMap<String, Tensor>> {
        // Check if the requested provider is available
        if !self
            .providers
            .iter()
            .any(|p| std::mem::discriminant(p) == std::mem::discriminant(&provider))
        {
            return Err(anyhow!("Execution provider not available: {:?}", provider));
        }

        // For now, delegate to regular run method
        // In a real implementation, this would set the execution provider specifically
        self.run(inputs)
    }

    /// Benchmark inference performance
    pub fn benchmark(
        &self,
        inputs: HashMap<String, Tensor>,
        num_runs: usize,
    ) -> Result<BenchmarkResults> {
        let mut latencies = Vec::with_capacity(num_runs);

        for _ in 0..num_runs {
            let start = std::time::Instant::now();
            let _outputs = self.run(inputs.clone())?;
            let duration = start.elapsed();
            latencies.push(duration.as_secs_f64() * 1000.0); // Convert to milliseconds
        }

        // Calculate statistics
        latencies.sort_by(|a, b| a.partial_cmp(b).expect("Partial comparison failed"));
        let mean = latencies.iter().sum::<f64>() / latencies.len() as f64;
        let median = latencies[latencies.len() / 2];
        let p90 = latencies[(latencies.len() as f64 * 0.9) as usize];
        let p95 = latencies[(latencies.len() as f64 * 0.95) as usize];
        let p99 = latencies[(latencies.len() as f64 * 0.99) as usize];

        Ok(BenchmarkResults {
            num_runs,
            mean_latency_ms: mean,
            median_latency_ms: median,
            p90_latency_ms: p90,
            p95_latency_ms: p95,
            p99_latency_ms: p99,
            min_latency_ms: latencies[0],
            max_latency_ms: latencies[latencies.len() - 1],
        })
    }

    /// Get memory usage information
    pub fn get_memory_info(&self) -> Result<MemoryInfo> {
        // In a real implementation, this would query ONNX Runtime for memory usage
        Ok(MemoryInfo {
            total_memory_bytes: 0,
            available_memory_bytes: 0,
            model_memory_bytes: 0,
        })
    }
}

/// Benchmark results for inference performance
#[derive(Debug, Clone)]
pub struct BenchmarkResults {
    pub num_runs: usize,
    pub mean_latency_ms: f64,
    pub median_latency_ms: f64,
    pub p90_latency_ms: f64,
    pub p95_latency_ms: f64,
    pub p99_latency_ms: f64,
    pub min_latency_ms: f64,
    pub max_latency_ms: f64,
}

impl BenchmarkResults {
    /// Print benchmark summary
    pub fn print_summary(&self) {
        println!("ONNX Runtime Benchmark Results");
        println!("==============================");
        println!("Number of runs: {}", self.num_runs);
        println!("Mean latency: {:.2} ms", self.mean_latency_ms);
        println!("Median latency: {:.2} ms", self.median_latency_ms);
        println!("P90 latency: {:.2} ms", self.p90_latency_ms);
        println!("P95 latency: {:.2} ms", self.p95_latency_ms);
        println!("P99 latency: {:.2} ms", self.p99_latency_ms);
        println!("Min latency: {:.2} ms", self.min_latency_ms);
        println!("Max latency: {:.2} ms", self.max_latency_ms);
    }
}

/// Memory usage information
#[derive(Debug, Clone)]
pub struct MemoryInfo {
    pub total_memory_bytes: usize,
    pub available_memory_bytes: usize,
    pub model_memory_bytes: usize,
}

/// ONNX model optimization utilities
pub struct ONNXOptimizer;

impl ONNXOptimizer {
    /// Optimize ONNX model for inference
    pub fn optimize_model<P: AsRef<Path>>(
        input_path: P,
        output_path: P,
        optimization_level: GraphOptimizationLevel,
    ) -> Result<()> {
        let input_path = input_path.as_ref();
        let output_path = output_path.as_ref();

        if !input_path.exists() {
            return Err(anyhow!("Input ONNX model not found: {:?}", input_path));
        }

        // In a real implementation, this would:
        // 1. Load the ONNX model
        // 2. Apply graph optimizations based on optimization_level
        // 3. Save the optimized model

        println!("Optimizing ONNX model with level: {:?}", optimization_level);
        println!("Input: {:?}", input_path);
        println!("Output: {:?}", output_path);

        // Copy file for now (placeholder)
        std::fs::copy(input_path, output_path)?;

        Ok(())
    }

    /// Quantize ONNX model for smaller size and faster inference
    pub fn quantize_model<P: AsRef<Path>>(
        input_path: P,
        output_path: P,
        quantization_mode: QuantizationMode,
    ) -> Result<()> {
        let input_path = input_path.as_ref();
        let output_path = output_path.as_ref();

        if !input_path.exists() {
            return Err(anyhow!("Input ONNX model not found: {:?}", input_path));
        }

        println!("Quantizing ONNX model with mode: {:?}", quantization_mode);
        println!("Input: {:?}", input_path);
        println!("Output: {:?}", output_path);

        // Placeholder implementation
        std::fs::copy(input_path, output_path)?;

        Ok(())
    }
}

/// Quantization modes for ONNX models
#[derive(Debug, Clone)]
pub enum QuantizationMode {
    Static,
    Dynamic,
}

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

    #[test]
    fn test_onnx_runtime_backend_creation() {
        let backend = ONNXRuntimeBackend::new();
        assert!(backend.config.enable_cpu_mem_arena);
    }

    #[test]
    fn test_onnx_runtime_config() {
        let config = ONNXRuntimeConfig {
            inter_op_num_threads: Some(4),
            intra_op_num_threads: Some(2),
            enable_cpu_mem_arena: false,
            enable_mem_pattern: false,
            execution_mode: ExecutionMode::Parallel,
            graph_optimization_level: GraphOptimizationLevel::Basic,
            log_severity_level: LogLevel::Error,
        };

        let backend = ONNXRuntimeBackend::with_config(config.clone());
        assert_eq!(backend.config.inter_op_num_threads, Some(4));
        assert_eq!(backend.config.intra_op_num_threads, Some(2));
        assert!(!backend.config.enable_cpu_mem_arena);
    }

    #[test]
    fn test_execution_providers() {
        let backend = ONNXRuntimeBackend::new();
        let providers = backend.get_available_providers();

        // CPU provider should always be available
        assert!(providers.iter().any(|p| matches!(p, ExecutionProvider::CPU)));
    }

    #[test]
    fn test_session_options() {
        let backend = ONNXRuntimeBackend::new();
        let options = backend.create_session_options();

        assert!(!options.execution_providers.is_empty());
        assert!(options.enable_cpu_mem_arena);
    }

    #[test]
    fn test_load_nonexistent_model() {
        let backend = ONNXRuntimeBackend::new();
        let result = backend.load_model("nonexistent.onnx");

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[test]
    fn test_benchmark_results() {
        let results = BenchmarkResults {
            num_runs: 100,
            mean_latency_ms: 15.5,
            median_latency_ms: 14.2,
            p90_latency_ms: 18.7,
            p95_latency_ms: 20.1,
            p99_latency_ms: 25.3,
            min_latency_ms: 12.1,
            max_latency_ms: 28.9,
        };

        assert_eq!(results.num_runs, 100);
        assert!((results.mean_latency_ms - 15.5).abs() < 1e-6);
    }

    #[test]
    fn test_memory_info() {
        let info = MemoryInfo {
            total_memory_bytes: 1024 * 1024 * 1024,    // 1GB
            available_memory_bytes: 512 * 1024 * 1024, // 512MB
            model_memory_bytes: 100 * 1024 * 1024,     // 100MB
        };

        assert_eq!(info.total_memory_bytes, 1024 * 1024 * 1024);
        assert_eq!(info.available_memory_bytes, 512 * 1024 * 1024);
        assert_eq!(info.model_memory_bytes, 100 * 1024 * 1024);
    }

    #[test]
    fn test_quantization_modes() {
        // Test quantization mode creation
        let static_mode = QuantizationMode::Static;
        let dynamic_mode = QuantizationMode::Dynamic;

        match static_mode {
            QuantizationMode::Static => {},
            _ => panic!("Expected Static mode"),
        }

        match dynamic_mode {
            QuantizationMode::Dynamic => {},
            _ => panic!("Expected Dynamic mode"),
        }
    }

    #[test]
    fn test_optimizer_operations() -> Result<()> {
        let temp_dir = tempdir()?;
        let input_path = temp_dir.path().join("input.onnx");
        let output_path = temp_dir.path().join("output.onnx");

        // Create a dummy input file
        std::fs::write(&input_path, "dummy onnx content")?;

        // Test optimization
        ONNXOptimizer::optimize_model(&input_path, &output_path, GraphOptimizationLevel::All)?;
        assert!(output_path.exists());

        // Test quantization
        let quantized_path = temp_dir.path().join("quantized.onnx");
        ONNXOptimizer::quantize_model(&output_path, &quantized_path, QuantizationMode::Dynamic)?;
        assert!(quantized_path.exists());

        Ok(())
    }
}