tenflowers-core 0.1.1

Core tensor operations and execution engine for TenfloweRS
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
/// Performance Regression Gates
///
/// This module provides infrastructure for performance regression testing using
/// criterion-based thresholds. It ensures that performance-critical operations
/// maintain their speed characteristics across code changes.
///
/// # Architecture
///
/// The performance gate system consists of:
/// - Baseline measurements for critical operations
/// - Configurable regression thresholds
/// - Automatic validation against baselines
/// - CI-friendly pass/fail reporting
///
/// # Usage
///
/// ```rust,no_run
/// use tenflowers_core::performance_gates::{PerformanceGate, OperationBaseline};
/// use tenflowers_core::{Tensor, ops::matmul};
///
/// // Define a baseline
/// let baseline = OperationBaseline::new(
///     "matmul_64x64",
///     100_000, // 100 microseconds baseline
///     0.10,    // Allow 10% regression
/// );
///
/// // Create test data
/// let size = 64;
/// let a = Tensor::<f32>::zeros(&[size, size]);
/// let b = Tensor::<f32>::zeros(&[size, size]);
///
/// // Validate performance
/// let gate = PerformanceGate::new(baseline);
/// let passed = gate.validate(|| {
///     matmul(&a, &b).expect("matmul should succeed");
/// });
///
/// assert!(passed, "Performance regression detected!");
/// ```
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

lazy_static::lazy_static! {
    /// Global registry of performance baselines
    static ref PERFORMANCE_BASELINES: Arc<Mutex<HashMap<String, OperationBaseline>>> = {
        Arc::new(Mutex::new(initialize_baselines()))
    };
}

/// Performance baseline for a specific operation
#[derive(Debug, Clone)]
pub struct OperationBaseline {
    /// Name of the operation
    pub name: String,
    /// Baseline time in nanoseconds
    pub baseline_ns: u64,
    /// Maximum allowed regression (0.10 = 10%)
    pub max_regression: f64,
    /// Minimum sample size for measurements
    pub min_samples: usize,
    /// Warmup iterations before measurement
    pub warmup_iters: usize,
}

impl OperationBaseline {
    /// Create a new operation baseline
    pub fn new(name: &str, baseline_ns: u64, max_regression: f64) -> Self {
        Self {
            name: name.to_string(),
            baseline_ns,
            max_regression,
            min_samples: 10,
            warmup_iters: 3,
        }
    }

    /// Create a baseline with custom sampling configuration
    pub fn with_sampling(
        name: &str,
        baseline_ns: u64,
        max_regression: f64,
        min_samples: usize,
        warmup_iters: usize,
    ) -> Self {
        Self {
            name: name.to_string(),
            baseline_ns,
            max_regression,
            min_samples,
            warmup_iters,
        }
    }

    /// Check if a measured time passes the regression threshold
    pub fn check_regression(&self, measured_ns: u64) -> bool {
        let threshold_ns = (self.baseline_ns as f64 * (1.0 + self.max_regression)) as u64;
        measured_ns <= threshold_ns
    }

    /// Calculate regression percentage
    pub fn regression_percentage(&self, measured_ns: u64) -> f64 {
        ((measured_ns as f64 - self.baseline_ns as f64) / self.baseline_ns as f64) * 100.0
    }
}

/// Performance gate validator
pub struct PerformanceGate {
    baseline: OperationBaseline,
}

impl PerformanceGate {
    /// Create a new performance gate
    pub fn new(baseline: OperationBaseline) -> Self {
        Self { baseline }
    }

    /// Validate that operation meets performance baseline
    ///
    /// Returns true if performance is within acceptable regression threshold
    pub fn validate<F>(&self, mut op: F) -> bool
    where
        F: FnMut(),
    {
        // Warmup iterations
        for _ in 0..self.baseline.warmup_iters {
            op();
        }

        // Measurement iterations
        let mut times = Vec::with_capacity(self.baseline.min_samples);
        for _ in 0..self.baseline.min_samples {
            let start = Instant::now();
            op();
            let elapsed = start.elapsed();
            times.push(elapsed.as_nanos() as u64);
        }

        // Calculate median time (more robust than mean for performance)
        times.sort_unstable();
        let median_ns = times[times.len() / 2];

        self.baseline.check_regression(median_ns)
    }

    /// Validate and return detailed measurement
    pub fn validate_detailed<F>(&self, mut op: F) -> PerformanceMeasurement
    where
        F: FnMut(),
    {
        // Warmup iterations
        for _ in 0..self.baseline.warmup_iters {
            op();
        }

        // Measurement iterations
        let mut times = Vec::with_capacity(self.baseline.min_samples);
        for _ in 0..self.baseline.min_samples {
            let start = Instant::now();
            op();
            let elapsed = start.elapsed();
            times.push(elapsed.as_nanos() as u64);
        }

        // Calculate statistics
        times.sort_unstable();
        let median_ns = times[times.len() / 2];
        let min_ns = *times.first().expect("collection should not be empty");
        let max_ns = *times.last().expect("collection should not be empty");
        let mean_ns = times.iter().sum::<u64>() / times.len() as u64;

        let passed = self.baseline.check_regression(median_ns);
        let regression_pct = self.baseline.regression_percentage(median_ns);

        PerformanceMeasurement {
            operation: self.baseline.name.clone(),
            baseline_ns: self.baseline.baseline_ns,
            measured_ns: median_ns,
            min_ns,
            max_ns,
            mean_ns,
            regression_pct,
            passed,
            samples: times.len(),
        }
    }
}

/// Detailed performance measurement result
#[derive(Debug, Clone)]
pub struct PerformanceMeasurement {
    pub operation: String,
    pub baseline_ns: u64,
    pub measured_ns: u64,
    pub min_ns: u64,
    pub max_ns: u64,
    pub mean_ns: u64,
    pub regression_pct: f64,
    pub passed: bool,
    pub samples: usize,
}

impl PerformanceMeasurement {
    /// Format as human-readable report
    pub fn report(&self) -> String {
        let status = if self.passed { "✓ PASS" } else { "✗ FAIL" };
        let regression_sign = if self.regression_pct >= 0.0 { "+" } else { "" };

        format!(
            "{} | {} | baseline: {:>8}ns | measured: {:>8}ns | regression: {}{:>6.2}% | min: {:>8}ns | max: {:>8}ns | samples: {}",
            status,
            self.operation,
            self.baseline_ns,
            self.measured_ns,
            regression_sign,
            self.regression_pct,
            self.min_ns,
            self.max_ns,
            self.samples
        )
    }

    /// Get duration from nanoseconds
    pub fn baseline_duration(&self) -> Duration {
        Duration::from_nanos(self.baseline_ns)
    }

    /// Get measured duration
    pub fn measured_duration(&self) -> Duration {
        Duration::from_nanos(self.measured_ns)
    }
}

/// Suite of performance gates for comprehensive validation
pub struct PerformanceGateSuite {
    gates: Vec<(String, PerformanceGate)>,
}

impl PerformanceGateSuite {
    /// Create a new empty suite
    pub fn new() -> Self {
        Self { gates: Vec::new() }
    }

    /// Add a gate to the suite
    pub fn add_gate(&mut self, name: String, gate: PerformanceGate) -> &mut Self {
        self.gates.push((name, gate));
        self
    }

    /// Run all gates and collect results
    pub fn run_all<F>(&self, op_factory: F) -> Vec<PerformanceMeasurement>
    where
        F: Fn(&str) -> Box<dyn FnMut()>,
    {
        let mut results = Vec::new();
        for (name, gate) in &self.gates {
            let mut op = op_factory(name);
            let measurement = gate.validate_detailed(&mut *op);
            results.push(measurement);
        }
        results
    }

    /// Check if all gates pass
    pub fn all_passed(&self, results: &[PerformanceMeasurement]) -> bool {
        results.iter().all(|r| r.passed)
    }

    /// Print comprehensive report
    pub fn print_report(&self, results: &[PerformanceMeasurement]) {
        println!(
            "\n╔════════════════════════════════════════════════════════════════════════════╗"
        );
        println!("║                    PERFORMANCE REGRESSION GATE REPORT                      ║");
        println!(
            "╚════════════════════════════════════════════════════════════════════════════╝\n"
        );

        for result in results {
            println!("{}", result.report());
        }

        let total = results.len();
        let passed = results.iter().filter(|r| r.passed).count();
        let failed = total - passed;

        println!("\n{}", "".repeat(80));
        println!(
            "Summary: {} total | {} passed | {} failed",
            total, passed, failed
        );

        if failed > 0 {
            println!("\n⚠ WARNING: Performance regressions detected!");
        } else {
            println!("\n✓ All performance gates passed!");
        }
    }
}

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

/// Initialize default performance baselines for critical operations
fn initialize_baselines() -> HashMap<String, OperationBaseline> {
    let mut baselines = HashMap::new();

    // Matrix multiplication baselines (in nanoseconds)
    // These are conservative estimates - adjust based on your hardware
    baselines.insert(
        "matmul_64x64_f32".to_string(),
        OperationBaseline::new("matmul_64x64_f32", 50_000, 0.15),
    );
    baselines.insert(
        "matmul_128x128_f32".to_string(),
        OperationBaseline::new("matmul_128x128_f32", 400_000, 0.15),
    );
    baselines.insert(
        "matmul_256x256_f32".to_string(),
        OperationBaseline::new("matmul_256x256_f32", 3_000_000, 0.15),
    );

    // Binary operations baselines
    baselines.insert(
        "add_10k_f32".to_string(),
        OperationBaseline::new("add_10k_f32", 5_000, 0.20),
    );
    baselines.insert(
        "mul_10k_f32".to_string(),
        OperationBaseline::new("mul_10k_f32", 5_000, 0.20),
    );

    // Reduction operations baselines
    baselines.insert(
        "sum_100k_f32".to_string(),
        OperationBaseline::new("sum_100k_f32", 20_000, 0.20),
    );
    baselines.insert(
        "mean_100k_f32".to_string(),
        OperationBaseline::new("mean_100k_f32", 25_000, 0.20),
    );

    // Convolution baselines (conservative for CPU)
    baselines.insert(
        "conv2d_3x3_32ch".to_string(),
        OperationBaseline::new("conv2d_3x3_32ch", 1_000_000, 0.15),
    );

    baselines
}

/// Register a custom baseline
pub fn register_baseline(baseline: OperationBaseline) {
    if let Ok(mut baselines) = PERFORMANCE_BASELINES.lock() {
        baselines.insert(baseline.name.clone(), baseline);
    }
}

/// Get a baseline by name
pub fn get_baseline(name: &str) -> Option<OperationBaseline> {
    PERFORMANCE_BASELINES
        .lock()
        .ok()
        .and_then(|baselines| baselines.get(name).cloned())
}

/// List all registered baselines
pub fn list_baselines() -> Vec<String> {
    PERFORMANCE_BASELINES
        .lock()
        .ok()
        .map(|baselines| baselines.keys().cloned().collect())
        .unwrap_or_default()
}

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

    #[test]
    fn test_baseline_creation() {
        let baseline = OperationBaseline::new("test_op", 1000, 0.10);
        assert_eq!(baseline.name, "test_op");
        assert_eq!(baseline.baseline_ns, 1000);
        assert_eq!(baseline.max_regression, 0.10);
    }

    #[test]
    fn test_regression_check() {
        let baseline = OperationBaseline::new("test_op", 1000, 0.10);

        // Within threshold
        assert!(baseline.check_regression(1000));
        assert!(baseline.check_regression(1050));
        assert!(baseline.check_regression(1100));

        // Exceeds threshold
        assert!(!baseline.check_regression(1150));
        assert!(!baseline.check_regression(2000));
    }

    #[test]
    fn test_regression_percentage() {
        let baseline = OperationBaseline::new("test_op", 1000, 0.10);

        assert_eq!(baseline.regression_percentage(1000), 0.0);
        assert_eq!(baseline.regression_percentage(1100), 10.0);
        assert_eq!(baseline.regression_percentage(1200), 20.0);
        assert_eq!(baseline.regression_percentage(900), -10.0);
    }

    #[test]
    fn test_performance_gate_validation() {
        // Use more realistic baseline accounting for closure call and measurement overhead
        let baseline = OperationBaseline::new("fast_op", 10_000, 0.50);
        let gate = PerformanceGate::new(baseline);

        // Fast operation should pass
        let passed = gate.validate(|| {
            // Simulate fast operation (essentially instant)
            let _ = 1 + 1;
        });

        assert!(passed, "Fast operation should pass performance gate");
    }

    #[test]
    fn test_performance_measurement_report() {
        let measurement = PerformanceMeasurement {
            operation: "test_op".to_string(),
            baseline_ns: 1000,
            measured_ns: 1050,
            min_ns: 1000,
            max_ns: 1100,
            mean_ns: 1050,
            regression_pct: 5.0,
            passed: true,
            samples: 10,
        };

        let report = measurement.report();
        assert!(report.contains("✓ PASS"));
        assert!(report.contains("test_op"));
        assert!(report.contains("1000"));
        assert!(report.contains("1050"));
    }

    #[test]
    fn test_baseline_registry() {
        let baseline = OperationBaseline::new("custom_op", 5000, 0.15);
        register_baseline(baseline.clone());

        let retrieved = get_baseline("custom_op");
        assert!(retrieved.is_some());
        let retrieved = retrieved.expect("test: operation should succeed");
        assert_eq!(retrieved.name, "custom_op");
        assert_eq!(retrieved.baseline_ns, 5000);
    }

    #[test]
    fn test_gate_suite() {
        let mut suite = PerformanceGateSuite::new();

        // Use more realistic baselines accounting for closure call and measurement overhead
        let baseline1 = OperationBaseline::new("op1", 10_000, 0.50);
        let baseline2 = OperationBaseline::new("op2", 10_000, 0.50);

        suite.add_gate("op1".to_string(), PerformanceGate::new(baseline1));
        suite.add_gate("op2".to_string(), PerformanceGate::new(baseline2));

        let results = suite.run_all(|_name| {
            Box::new(|| {
                let _ = 1 + 1;
            })
        });

        assert_eq!(results.len(), 2);
        assert!(suite.all_passed(&results));
    }
}