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
//! SIMD Benchmarking Utilities
//!
//! This module provides benchmarking utilities for comparing SIMD-optimized operations
//! against standard implementations and measuring performance improvements.

use std::time::Instant;

/// Benchmark result structure
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
    pub optimized_time_ns: u64,
    pub standard_time_ns: u64,
    pub speedup: f64,
    pub size: usize,
    pub iterations: usize,
}

impl BenchmarkResult {
    /// Print a summary of the benchmark results
    pub fn print_summary(&self) {
        println!("SIMD Benchmark Results:");
        println!("  Size: {} elements", self.size);
        println!("  Iterations: {}", self.iterations);
        println!("  Optimized time: {} ns", self.optimized_time_ns);
        println!("  Standard time: {} ns", self.standard_time_ns);
        println!("  Speedup: {:.2}x", self.speedup);
    }

    /// Get the performance improvement as a percentage
    pub fn improvement_percentage(&self) -> f64 {
        (self.speedup - 1.0) * 100.0
    }

    /// Check if the optimization provides significant improvement
    pub fn is_significant_improvement(&self, threshold: f64) -> bool {
        self.speedup > threshold
    }
}

/// SIMD benchmarking utilities
pub struct Benchmarks;

impl Benchmarks {
    /// Benchmark optimized vs standard addition operations
    pub fn benchmark_add_performance(size: usize, iterations: usize) -> BenchmarkResult {
        let a = vec![1.0f32; size];
        let b = vec![2.0f32; size];
        let mut result_optimized = vec![0.0f32; size];
        let mut result_standard = vec![0.0f32; size];

        // Benchmark optimized implementation (unchecked for fair comparison)
        let start = Instant::now();
        for _ in 0..iterations {
            super::basic_ops::BasicOps::add_f32_unchecked(&a, &b, &mut result_optimized);
        }
        let optimized_time = start.elapsed();

        // Benchmark standard implementation
        let start = Instant::now();
        for _ in 0..iterations {
            for i in 0..size {
                result_standard[i] = a[i] + b[i];
            }
        }
        let standard_time = start.elapsed();

        BenchmarkResult {
            optimized_time_ns: optimized_time.as_nanos() as u64,
            standard_time_ns: standard_time.as_nanos() as u64,
            speedup: standard_time.as_secs_f64() / optimized_time.as_secs_f64(),
            size,
            iterations,
        }
    }

    /// Benchmark optimized vs standard multiplication operations
    pub fn benchmark_mul_performance(size: usize, iterations: usize) -> BenchmarkResult {
        let a = vec![2.0f32; size];
        let b = vec![3.0f32; size];
        let mut result_optimized = vec![0.0f32; size];
        let mut result_standard = vec![0.0f32; size];

        // Benchmark optimized implementation
        let start = Instant::now();
        for _ in 0..iterations {
            super::basic_ops::BasicOps::mul_f32_unchecked(&a, &b, &mut result_optimized);
        }
        let optimized_time = start.elapsed();

        // Benchmark standard implementation
        let start = Instant::now();
        for _ in 0..iterations {
            for i in 0..size {
                result_standard[i] = a[i] * b[i];
            }
        }
        let standard_time = start.elapsed();

        BenchmarkResult {
            optimized_time_ns: optimized_time.as_nanos() as u64,
            standard_time_ns: standard_time.as_nanos() as u64,
            speedup: standard_time.as_secs_f64() / optimized_time.as_secs_f64(),
            size,
            iterations,
        }
    }

    /// Benchmark optimized vs standard ReLU activation
    pub fn benchmark_relu_performance(size: usize, iterations: usize) -> BenchmarkResult {
        use scirs2_core::random::Random;
        let mut rng = Random::seed(42);
        let input: Vec<f32> = (0..size).map(|_| rng.random_range(-5.0..5.0)).collect();
        let mut result_optimized = vec![0.0f32; size];
        let mut result_standard = vec![0.0f32; size];

        // Benchmark optimized implementation
        let start = Instant::now();
        for _ in 0..iterations {
            super::activation_functions::ActivationFunctions::relu_f32_optimized(
                &input,
                &mut result_optimized,
            )
            .expect("optimized ReLU should not fail during benchmarking");
        }
        let optimized_time = start.elapsed();

        // Benchmark standard implementation
        let start = Instant::now();
        for _ in 0..iterations {
            for i in 0..size {
                result_standard[i] = input[i].max(0.0);
            }
        }
        let standard_time = start.elapsed();

        BenchmarkResult {
            optimized_time_ns: optimized_time.as_nanos() as u64,
            standard_time_ns: standard_time.as_nanos() as u64,
            speedup: standard_time.as_secs_f64() / optimized_time.as_secs_f64(),
            size,
            iterations,
        }
    }

    /// Benchmark optimized vs standard dot product
    pub fn benchmark_dot_product_performance(size: usize, iterations: usize) -> BenchmarkResult {
        let a = vec![1.5f32; size];
        let b = vec![2.5f32; size];

        // Benchmark optimized implementation
        let start = Instant::now();
        for _ in 0..iterations {
            let _ = super::matrix_ops::MatrixOps::dot_product_f32_optimized(&a, &b)
                .expect("optimized dot product should not fail during benchmarking");
        }
        let optimized_time = start.elapsed();

        // Benchmark standard implementation
        let start = Instant::now();
        for _ in 0..iterations {
            let _: f32 = a.iter().zip(b.iter()).map(|(&x, &y)| x * y).sum();
        }
        let standard_time = start.elapsed();

        BenchmarkResult {
            optimized_time_ns: optimized_time.as_nanos() as u64,
            standard_time_ns: standard_time.as_nanos() as u64,
            speedup: standard_time.as_secs_f64() / optimized_time.as_secs_f64(),
            size,
            iterations,
        }
    }

    /// Benchmark optimized vs standard sum reduction
    pub fn benchmark_sum_performance(size: usize, iterations: usize) -> BenchmarkResult {
        let input = vec![1.5f32; size];

        // Benchmark optimized implementation
        let start = Instant::now();
        for _ in 0..iterations {
            let _ = super::reduction_ops::ReductionOps::sum_f32_unchecked(&input);
        }
        let optimized_time = start.elapsed();

        // Benchmark standard implementation
        let start = Instant::now();
        for _ in 0..iterations {
            let _: f32 = input.iter().sum();
        }
        let standard_time = start.elapsed();

        BenchmarkResult {
            optimized_time_ns: optimized_time.as_nanos() as u64,
            standard_time_ns: standard_time.as_nanos() as u64,
            speedup: standard_time.as_secs_f64() / optimized_time.as_secs_f64(),
            size,
            iterations,
        }
    }

    /// Benchmark optimized vs standard exp function
    pub fn benchmark_exp_performance(size: usize, iterations: usize) -> BenchmarkResult {
        use scirs2_core::random::Random;
        let mut rng = Random::seed(42);
        let input: Vec<f32> = (0..size).map(|_| rng.random_range(-2.0..2.0)).collect();
        let mut result_optimized = vec![0.0f32; size];
        let mut result_standard = vec![0.0f32; size];

        // Benchmark optimized implementation
        let start = Instant::now();
        for _ in 0..iterations {
            super::math_functions::MathFunctions::exp_f32_optimized(&input, &mut result_optimized)
                .expect("optimized exp should not fail during benchmarking");
        }
        let optimized_time = start.elapsed();

        // Benchmark standard implementation
        let start = Instant::now();
        for _ in 0..iterations {
            for i in 0..size {
                result_standard[i] = input[i].exp();
            }
        }
        let standard_time = start.elapsed();

        BenchmarkResult {
            optimized_time_ns: optimized_time.as_nanos() as u64,
            standard_time_ns: standard_time.as_nanos() as u64,
            speedup: standard_time.as_secs_f64() / optimized_time.as_secs_f64(),
            size,
            iterations,
        }
    }

    /// Comprehensive benchmark suite testing multiple operations
    pub fn comprehensive_benchmark_suite(
        size: usize,
        iterations: usize,
    ) -> Vec<(&'static str, BenchmarkResult)> {
        let mut results = Vec::new();

        println!("Running comprehensive SIMD benchmark suite...");
        println!("Size: {} elements, Iterations: {}", size, iterations);
        println!("{}", "=".repeat(60));

        // Addition benchmark
        print!("Benchmarking addition... ");
        let add_result = Self::benchmark_add_performance(size, iterations);
        println!("Speedup: {:.2}x", add_result.speedup);
        results.push(("Addition", add_result));

        // Multiplication benchmark
        print!("Benchmarking multiplication... ");
        let mul_result = Self::benchmark_mul_performance(size, iterations);
        println!("Speedup: {:.2}x", mul_result.speedup);
        results.push(("Multiplication", mul_result));

        // ReLU benchmark
        print!("Benchmarking ReLU... ");
        let relu_result = Self::benchmark_relu_performance(size, iterations);
        println!("Speedup: {:.2}x", relu_result.speedup);
        results.push(("ReLU", relu_result));

        // Dot product benchmark
        print!("Benchmarking dot product... ");
        let dot_result = Self::benchmark_dot_product_performance(size, iterations);
        println!("Speedup: {:.2}x", dot_result.speedup);
        results.push(("Dot Product", dot_result));

        // Sum reduction benchmark
        print!("Benchmarking sum reduction... ");
        let sum_result = Self::benchmark_sum_performance(size, iterations);
        println!("Speedup: {:.2}x", sum_result.speedup);
        results.push(("Sum", sum_result));

        // Exp function benchmark
        print!("Benchmarking exp function... ");
        let exp_result = Self::benchmark_exp_performance(size, iterations);
        println!("Speedup: {:.2}x", exp_result.speedup);
        results.push(("Exp", exp_result));

        println!("{}", "=".repeat(60));

        results
    }

    /// Print detailed benchmark report
    pub fn print_benchmark_report(results: &[(&'static str, BenchmarkResult)]) {
        println!("\nDetailed Benchmark Report:");
        println!("{}", "=".repeat(80));

        for (operation, result) in results {
            println!("\n{} Performance:", operation);
            println!(
                "  Optimized: {:.2} ms",
                result.optimized_time_ns as f64 / 1_000_000.0
            );
            println!(
                "  Standard:  {:.2} ms",
                result.standard_time_ns as f64 / 1_000_000.0
            );
            println!(
                "  Speedup:   {:.2}x ({:.1}% improvement)",
                result.speedup,
                result.improvement_percentage()
            );

            if result.speedup > 1.5 {
                println!("  Status:    🚀 Excellent optimization");
            } else if result.speedup > 1.2 {
                println!("  Status:    ✅ Good optimization");
            } else if result.speedup > 1.0 {
                println!("  Status:    📈 Modest improvement");
            } else {
                println!("  Status:    ⚠️ No improvement");
            }
        }

        // Overall statistics
        let avg_speedup: f64 =
            results.iter().map(|(_, r)| r.speedup).sum::<f64>() / results.len() as f64;
        let max_speedup = results
            .iter()
            .map(|(_, r)| r.speedup)
            .fold(0.0f64, f64::max);
        let min_speedup = results
            .iter()
            .map(|(_, r)| r.speedup)
            .fold(f64::INFINITY, f64::min);

        println!("\n{}", "=".repeat(80));
        println!("Overall Performance Summary:");
        println!("  Average speedup: {:.2}x", avg_speedup);
        println!("  Best speedup:    {:.2}x", max_speedup);
        println!("  Worst speedup:   {:.2}x", min_speedup);
        println!("  Total operations: {}", results.len());

        let good_optimizations = results.iter().filter(|(_, r)| r.speedup > 1.2).count();
        println!(
            "  Good optimizations: {}/{}",
            good_optimizations,
            results.len()
        );
    }

    /// Warm-up function to stabilize CPU frequency and caches
    pub fn warmup() {
        let warmup_size = 1000;
        let warmup_iterations = 100;

        let a = vec![1.0f32; warmup_size];
        let b = vec![2.0f32; warmup_size];
        let mut result = vec![0.0f32; warmup_size];

        // Warm-up with some operations
        for _ in 0..warmup_iterations {
            for i in 0..warmup_size {
                result[i] = a[i] + b[i] * 2.0;
            }
        }

        // Force the compiler to not optimize away the warmup
        let _checksum: f32 = result.iter().sum();
    }

    /// Run scalability test across different array sizes
    pub fn scalability_test(
        operation: &str,
        base_iterations: usize,
    ) -> Vec<(usize, BenchmarkResult)> {
        let sizes = vec![32, 64, 128, 256, 512, 1024, 2048, 4096, 8192];
        let mut results = Vec::new();

        println!("Running scalability test for {}...", operation);

        for &size in &sizes {
            // Adjust iterations based on size to keep test time reasonable
            let iterations = (base_iterations * 1000) / size.max(1);
            let iterations = iterations.max(10); // Minimum 10 iterations

            let result = match operation {
                "add" => Self::benchmark_add_performance(size, iterations),
                "mul" => Self::benchmark_mul_performance(size, iterations),
                "relu" => Self::benchmark_relu_performance(size, iterations),
                "dot" => Self::benchmark_dot_product_performance(size, iterations),
                "sum" => Self::benchmark_sum_performance(size, iterations),
                "exp" => Self::benchmark_exp_performance(size, iterations),
                _ => panic!("Unknown operation: {}", operation),
            };

            println!("Size {}: {:.2}x speedup", size, result.speedup);
            results.push((size, result));
        }

        results
    }
}

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

    #[test]
    fn test_benchmark_result() {
        let result = BenchmarkResult {
            optimized_time_ns: 100,
            standard_time_ns: 200,
            speedup: 2.0,
            size: 1000,
            iterations: 100,
        };

        assert_eq!(result.improvement_percentage(), 100.0);
        assert!(result.is_significant_improvement(1.5));
        assert!(!result.is_significant_improvement(2.5));
    }

    #[test]
    fn test_benchmark_add_performance() {
        // Run a small benchmark to ensure it works
        let result = Benchmarks::benchmark_add_performance(100, 10);

        // Basic sanity checks
        // Note: optimized_time_ns and standard_time_ns are unsigned, so >= 0 is always true
        assert!(result.speedup >= 0.0 || result.speedup.is_infinite() || result.speedup.is_nan());
        assert_eq!(result.size, 100);
        assert_eq!(result.iterations, 10);
    }

    #[test]
    fn test_comprehensive_benchmark_basic() {
        // Run a minimal comprehensive benchmark
        let results = Benchmarks::comprehensive_benchmark_suite(32, 5);

        // Should have multiple benchmark results
        assert!(results.len() >= 5);

        // Each result should be valid
        for (name, result) in &results {
            assert!(!name.is_empty());
            // Note: optimized_time_ns and standard_time_ns are unsigned, so >= 0 is always true
            // Speedup can be infinite, NaN, or positive depending on timing precision
            assert!(
                result.speedup >= 0.0 || result.speedup.is_infinite() || result.speedup.is_nan()
            );
        }
    }

    #[test]
    fn test_warmup() {
        // Should not panic or error
        Benchmarks::warmup();
    }

    #[test]
    fn test_scalability_test_basic() {
        // Run a very basic scalability test
        let results = Benchmarks::scalability_test("add", 1);

        // Should have multiple size results
        assert!(!results.is_empty());

        // Check that results are ordered by size
        for i in 1..results.len() {
            assert!(
                results[i].0 > results[i - 1].0,
                "Sizes should be increasing"
            );
        }
    }
}