trustformers-models 0.1.1

Model implementations 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
//! Validation Tools
//!
//! Tools for validating model implementations, configurations, and outputs.

use anyhow::Result;
use scirs2_core::ndarray::ArrayD; // SciRS2 Integration Policy
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use trustformers_core::tensor::Tensor;

/// Validation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationConfig {
    /// Numerical tolerance for comparisons
    pub numerical_tolerance: f64,
    /// Whether to check for NaN values
    pub check_nan: bool,
    /// Whether to check for infinite values
    pub check_infinite: bool,
    /// Expected output shapes
    pub expected_shapes: HashMap<String, Vec<usize>>,
    /// Value range constraints
    pub value_ranges: HashMap<String, (f64, f64)>,
    /// Performance thresholds
    pub performance_thresholds: PerformanceThresholds,
}

/// Performance validation thresholds
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceThresholds {
    /// Maximum forward pass time in milliseconds
    pub max_forward_time_ms: f64,
    /// Maximum memory usage in MB
    pub max_memory_mb: f64,
    /// Minimum throughput (samples/second)
    pub min_throughput: f64,
}

/// Validation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationResult {
    /// Whether validation passed
    pub passed: bool,
    /// Validation errors
    pub errors: Vec<String>,
    /// Validation warnings
    pub warnings: Vec<String>,
    /// Performance metrics
    pub performance_metrics: Option<PerformanceMetrics>,
}

/// Performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Forward pass time in milliseconds
    pub forward_time_ms: f64,
    /// Memory usage in MB
    pub memory_usage_mb: f64,
    /// Throughput in samples/second
    pub throughput: f64,
    /// Parameter count
    pub parameter_count: usize,
}

/// Model validator
pub struct ModelValidator {
    config: ValidationConfig,
}

impl ModelValidator {
    /// Create a new model validator
    pub fn new(config: ValidationConfig) -> Self {
        Self { config }
    }

    /// Validate tensor output
    pub fn validate_tensor(&self, name: &str, tensor: &Tensor) -> ValidationResult {
        let mut result = ValidationResult {
            passed: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            performance_metrics: None,
        };

        // Validate tensor shape
        if let Some(expected_shape) = self.config.expected_shapes.get(name) {
            let actual_shape = match tensor {
                Tensor::F32(arr) => arr.shape().to_vec(),
                Tensor::F64(arr) => arr.shape().to_vec(),
                Tensor::I64(arr) => arr.shape().to_vec(),
                _ => {
                    result.errors.push("Unsupported tensor type for shape validation".to_string());
                    result.passed = false;
                    return result;
                },
            };

            if actual_shape != *expected_shape {
                result.errors.push(format!(
                    "Shape mismatch for '{}': expected {:?}, got {:?}",
                    name, expected_shape, actual_shape
                ));
                result.passed = false;
            }
        }

        // Validate numerical values
        match tensor {
            Tensor::F32(arr) => {
                self.validate_f32_values(name, arr, &mut result);
            },
            Tensor::F64(arr) => {
                self.validate_f64_values(name, arr, &mut result);
            },
            Tensor::I64(_) => {
                // Integer tensors are generally safe from NaN/Inf issues
            },
            _ => {
                // Other tensor types (F16, BF16, Complex) not supported for validation yet
            },
        }

        result
    }

    /// Validate F32 tensor values
    fn validate_f32_values(&self, name: &str, arr: &ArrayD<f32>, result: &mut ValidationResult) {
        // Check for NaN values
        if self.config.check_nan {
            let nan_count = arr.iter().filter(|&&x| x.is_nan()).count();
            if nan_count > 0 {
                result.errors.push(format!(
                    "Found {} NaN values in tensor '{}'",
                    nan_count, name
                ));
                result.passed = false;
            }
        }

        // Check for infinite values
        if self.config.check_infinite {
            let inf_count = arr.iter().filter(|&&x| x.is_infinite()).count();
            if inf_count > 0 {
                result.errors.push(format!(
                    "Found {} infinite values in tensor '{}'",
                    inf_count, name
                ));
                result.passed = false;
            }
        }

        // Check value ranges
        if let Some((min_val, max_val)) = self.config.value_ranges.get(name) {
            let actual_min = arr.iter().cloned().fold(f32::INFINITY, f32::min) as f64;
            let actual_max = arr.iter().cloned().fold(f32::NEG_INFINITY, f32::max) as f64;

            if actual_min < *min_val {
                result.warnings.push(format!(
                    "Values in '{}' below expected minimum: {} < {}",
                    name, actual_min, min_val
                ));
            }

            if actual_max > *max_val {
                result.warnings.push(format!(
                    "Values in '{}' above expected maximum: {} > {}",
                    name, actual_max, max_val
                ));
            }
        }
    }

    /// Validate F64 tensor values
    fn validate_f64_values(&self, name: &str, arr: &ArrayD<f64>, result: &mut ValidationResult) {
        // Check for NaN values
        if self.config.check_nan {
            let nan_count = arr.iter().filter(|&&x| x.is_nan()).count();
            if nan_count > 0 {
                result.errors.push(format!(
                    "Found {} NaN values in tensor '{}'",
                    nan_count, name
                ));
                result.passed = false;
            }
        }

        // Check for infinite values
        if self.config.check_infinite {
            let inf_count = arr.iter().filter(|&&x| x.is_infinite()).count();
            if inf_count > 0 {
                result.errors.push(format!(
                    "Found {} infinite values in tensor '{}'",
                    inf_count, name
                ));
                result.passed = false;
            }
        }

        // Check value ranges
        if let Some((min_val, max_val)) = self.config.value_ranges.get(name) {
            let actual_min = arr.iter().cloned().fold(f64::INFINITY, f64::min);
            let actual_max = arr.iter().cloned().fold(f64::NEG_INFINITY, f64::max);

            if actual_min < *min_val {
                result.warnings.push(format!(
                    "Values in '{}' below expected minimum: {} < {}",
                    name, actual_min, min_val
                ));
            }

            if actual_max > *max_val {
                result.warnings.push(format!(
                    "Values in '{}' above expected maximum: {} > {}",
                    name, actual_max, max_val
                ));
            }
        }
    }

    /// Validate model configuration
    pub fn validate_config<T>(&self, _config: &T) -> ValidationResult
    where
        T: std::fmt::Debug,
    {
        // This would typically use reflection or custom traits to validate config
        // For now, we provide a basic framework

        ValidationResult {
            passed: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            performance_metrics: None,
        }
    }

    /// Validate model performance
    pub fn validate_performance<F>(&self, mut model_fn: F) -> ValidationResult
    where
        F: FnMut() -> Result<Tensor>,
    {
        let mut result = ValidationResult {
            passed: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            performance_metrics: None,
        };

        // Warmup runs
        for _ in 0..3 {
            if let Err(e) = model_fn() {
                result.errors.push(format!("Warmup run failed: {}", e));
                result.passed = false;
                return result;
            }
        }

        // Timed runs
        let mut durations = Vec::new();
        let num_runs = 10;

        for _ in 0..num_runs {
            let start = std::time::Instant::now();
            if let Err(e) = model_fn() {
                result.errors.push(format!("Timed run failed: {}", e));
                result.passed = false;
                return result;
            }
            durations.push(start.elapsed().as_millis() as f64);
        }

        // Calculate performance metrics
        let avg_time_ms = durations.iter().sum::<f64>() / durations.len() as f64;
        let throughput = 1000.0 / avg_time_ms; // samples per second

        let performance_metrics = PerformanceMetrics {
            forward_time_ms: avg_time_ms,
            memory_usage_mb: 0.0, // Would need platform-specific memory measurement
            throughput,
            parameter_count: 0, // Would need model introspection
        };

        // Check against thresholds
        if avg_time_ms > self.config.performance_thresholds.max_forward_time_ms {
            result.errors.push(format!(
                "Forward pass too slow: {:.2}ms > {:.2}ms",
                avg_time_ms, self.config.performance_thresholds.max_forward_time_ms
            ));
            result.passed = false;
        }

        if throughput < self.config.performance_thresholds.min_throughput {
            result.warnings.push(format!(
                "Throughput below threshold: {:.2} < {:.2} samples/sec",
                throughput, self.config.performance_thresholds.min_throughput
            ));
        }

        result.performance_metrics = Some(performance_metrics);
        result
    }

    /// Compare two tensors for numerical equality
    pub fn compare_tensors(
        &self,
        name: &str,
        expected: &Tensor,
        actual: &Tensor,
    ) -> ValidationResult {
        let mut result = ValidationResult {
            passed: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            performance_metrics: None,
        };

        match (expected, actual) {
            (Tensor::F32(exp_arr), Tensor::F32(act_arr)) => {
                if exp_arr.shape() != act_arr.shape() {
                    result.errors.push(format!(
                        "Shape mismatch for '{}': expected {:?}, got {:?}",
                        name,
                        exp_arr.shape(),
                        act_arr.shape()
                    ));
                    result.passed = false;
                    return result;
                }

                let mut max_diff = 0.0f64;
                let mut total_diff = 0.0f64;
                let mut count = 0;

                for (exp_val, act_val) in exp_arr.iter().zip(act_arr.iter()) {
                    let diff = (*exp_val - *act_val).abs() as f64;
                    max_diff = max_diff.max(diff);
                    total_diff += diff;
                    count += 1;

                    if diff > self.config.numerical_tolerance {
                        result.errors.push(format!(
                            "Value difference exceeds tolerance for '{}': {} vs {} (diff: {})",
                            name, exp_val, act_val, diff
                        ));
                        result.passed = false;
                    }
                }

                let avg_diff = total_diff / count as f64;
                if avg_diff > self.config.numerical_tolerance * 0.1 {
                    result.warnings.push(format!(
                        "Average difference for '{}' is high: {:.6}",
                        name, avg_diff
                    ));
                }
            },
            (Tensor::I64(exp_arr), Tensor::I64(act_arr)) => {
                if exp_arr.shape() != act_arr.shape() {
                    result.errors.push(format!(
                        "Shape mismatch for '{}': expected {:?}, got {:?}",
                        name,
                        exp_arr.shape(),
                        act_arr.shape()
                    ));
                    result.passed = false;
                    return result;
                }

                for (exp_val, act_val) in exp_arr.iter().zip(act_arr.iter()) {
                    if exp_val != act_val {
                        result.errors.push(format!(
                            "Integer value mismatch for '{}': {} vs {}",
                            name, exp_val, act_val
                        ));
                        result.passed = false;
                    }
                }
            },
            _ => {
                result.errors.push(format!(
                    "Tensor type mismatch for '{}': different tensor types",
                    name
                ));
                result.passed = false;
            },
        }

        result
    }

    /// Validate model against reference implementation
    pub fn validate_against_reference<M, R>(
        &self,
        model: M,
        reference: R,
        test_inputs: Vec<Tensor>,
    ) -> ValidationResult
    where
        M: Fn(&Tensor) -> Result<Tensor>,
        R: Fn(&Tensor) -> Result<Tensor>,
    {
        let mut result = ValidationResult {
            passed: true,
            errors: Vec::new(),
            warnings: Vec::new(),
            performance_metrics: None,
        };

        for (i, input) in test_inputs.iter().enumerate() {
            let model_output = match model(input) {
                Ok(output) => output,
                Err(e) => {
                    result.errors.push(format!("Model failed on input {}: {}", i, e));
                    result.passed = false;
                    continue;
                },
            };

            let reference_output = match reference(input) {
                Ok(output) => output,
                Err(e) => {
                    result.errors.push(format!("Reference failed on input {}: {}", i, e));
                    result.passed = false;
                    continue;
                },
            };

            let comparison_result =
                self.compare_tensors(&format!("output_{}", i), &reference_output, &model_output);

            if !comparison_result.passed {
                result.errors.extend(comparison_result.errors);
                result.passed = false;
            }
            result.warnings.extend(comparison_result.warnings);
        }

        result
    }
}

impl Default for ValidationConfig {
    fn default() -> Self {
        Self {
            numerical_tolerance: 1e-5,
            check_nan: true,
            check_infinite: true,
            expected_shapes: HashMap::new(),
            value_ranges: HashMap::new(),
            performance_thresholds: PerformanceThresholds {
                max_forward_time_ms: 1000.0,
                max_memory_mb: 1024.0,
                min_throughput: 10.0,
            },
        }
    }
}

/// Validation utilities
pub struct ValidationUtils;

impl ValidationUtils {
    /// Create a strict validation config
    pub fn strict_config() -> ValidationConfig {
        ValidationConfig {
            numerical_tolerance: 1e-6,
            check_nan: true,
            check_infinite: true,
            expected_shapes: HashMap::new(),
            value_ranges: HashMap::new(),
            performance_thresholds: PerformanceThresholds {
                max_forward_time_ms: 500.0,
                max_memory_mb: 512.0,
                min_throughput: 50.0,
            },
        }
    }

    /// Create a lenient validation config
    pub fn lenient_config() -> ValidationConfig {
        ValidationConfig {
            numerical_tolerance: 1e-3,
            check_nan: true,
            check_infinite: true,
            expected_shapes: HashMap::new(),
            value_ranges: HashMap::new(),
            performance_thresholds: PerformanceThresholds {
                max_forward_time_ms: 5000.0,
                max_memory_mb: 4096.0,
                min_throughput: 1.0,
            },
        }
    }

    /// Create test tensors for validation
    pub fn create_test_tensors() -> Result<Vec<Tensor>> {
        Ok(vec![
            Tensor::zeros(&[1, 10])?,
            Tensor::ones(&[2, 20])?,
            Tensor::randn(&[4, 32])?,
            Tensor::randn(&[8, 64])?, // Using randn instead of rand
        ])
    }

    /// Generate validation report
    pub fn generate_report(results: &[ValidationResult]) -> String {
        let mut report = String::new();
        report.push_str("# Validation Report\n\n");

        let total_tests = results.len();
        let passed_tests = results.iter().filter(|r| r.passed).count();
        let failed_tests = total_tests - passed_tests;

        report.push_str("## Summary\n\n");
        report.push_str(&format!("- Total tests: {}\n", total_tests));
        report.push_str(&format!("- Passed: {}\n", passed_tests));
        report.push_str(&format!("- Failed: {}\n\n", failed_tests));

        if failed_tests > 0 {
            report.push_str("## Errors\n\n");
            for (i, result) in results.iter().enumerate() {
                if !result.passed {
                    report.push_str(&format!("### Test {}\n\n", i + 1));
                    for error in &result.errors {
                        report.push_str(&format!("- ❌ {}\n", error));
                    }
                    report.push('\n');
                }
            }
        }

        let total_warnings: usize = results.iter().map(|r| r.warnings.len()).sum();
        if total_warnings > 0 {
            report.push_str("## Warnings\n\n");
            for (i, result) in results.iter().enumerate() {
                if !result.warnings.is_empty() {
                    report.push_str(&format!("### Test {}\n\n", i + 1));
                    for warning in &result.warnings {
                        report.push_str(&format!("- ⚠️ {}\n", warning));
                    }
                    report.push('\n');
                }
            }
        }

        // Performance metrics
        let performance_results: Vec<_> =
            results.iter().filter_map(|r| r.performance_metrics.as_ref()).collect();

        if !performance_results.is_empty() {
            report.push_str("## Performance Metrics\n\n");
            let avg_time: f64 = performance_results.iter().map(|p| p.forward_time_ms).sum::<f64>()
                / performance_results.len() as f64;
            let avg_throughput: f64 = performance_results.iter().map(|p| p.throughput).sum::<f64>()
                / performance_results.len() as f64;

            report.push_str(&format!("- Average forward time: {:.2} ms\n", avg_time));
            report.push_str(&format!(
                "- Average throughput: {:.2} samples/sec\n",
                avg_throughput
            ));
        }

        report
    }
}

/// Batch validator for running multiple validations
pub struct BatchValidator {
    validators: Vec<ModelValidator>,
}

impl BatchValidator {
    /// Create a new batch validator
    pub fn new() -> Self {
        Self {
            validators: Vec::new(),
        }
    }

    /// Add a validator to the batch
    pub fn add_validator(&mut self, validator: ModelValidator) {
        self.validators.push(validator);
    }

    /// Run all validators on a tensor
    pub fn validate_tensor_batch(&self, name: &str, tensor: &Tensor) -> Vec<ValidationResult> {
        self.validators
            .iter()
            .map(|validator| validator.validate_tensor(name, tensor))
            .collect()
    }

    /// Generate combined report
    pub fn generate_batch_report(&self, results: &[Vec<ValidationResult>]) -> String {
        let mut report = String::new();
        report.push_str("# Batch Validation Report\n\n");

        for (validator_idx, validator_results) in results.iter().enumerate() {
            report.push_str(&format!("## Validator {}\n\n", validator_idx + 1));
            report.push_str(&ValidationUtils::generate_report(validator_results));
            report.push('\n');
        }

        report
    }
}

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