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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Framework comparison benchmarking
//!
//! This module provides utilities to benchmark TenfloweRS against other
//! machine learning frameworks like PyTorch, TensorFlow, and NumPy.

use super::performance_benchmark::BenchmarkConfig;
use crate::{Result, Tensor, TensorError};
use std::collections::HashMap;
use std::process::Command;
use std::time::{Duration, Instant};

/// Framework comparison result
#[derive(Debug, Clone)]
pub struct FrameworkComparisonResult {
    pub operation: String,
    pub size: usize,
    pub tenflowers_time: Duration,
    pub framework_times: HashMap<String, Duration>,
    pub tenflowers_throughput: f64,
    pub framework_throughputs: HashMap<String, f64>,
    pub relative_performance: HashMap<String, f64>, // TenfloweRS vs other frameworks
}

impl FrameworkComparisonResult {
    pub fn new(
        operation: String,
        size: usize,
        tenflowers_time: Duration,
        framework_times: HashMap<String, Duration>,
    ) -> Self {
        let tenflowers_throughput = size as f64 / tenflowers_time.as_secs_f64();

        let mut framework_throughputs = HashMap::new();
        let mut relative_performance = HashMap::new();

        for (framework, time) in &framework_times {
            let throughput = size as f64 / time.as_secs_f64();
            framework_throughputs.insert(framework.clone(), throughput);

            // Relative performance: TenfloweRS time / framework time
            // > 1.0 means TenfloweRS is slower, < 1.0 means TenfloweRS is faster
            let relative = tenflowers_time.as_nanos() as f64 / time.as_nanos() as f64;
            relative_performance.insert(framework.clone(), relative);
        }

        Self {
            operation,
            size,
            tenflowers_time,
            framework_times,
            tenflowers_throughput,
            framework_throughputs,
            relative_performance,
        }
    }
}

/// Framework benchmarking configuration
#[derive(Debug, Clone)]
pub struct FrameworkBenchmarkConfig {
    pub base_config: BenchmarkConfig,
    pub frameworks_to_test: Vec<String>,
    pub python_executable: String,
    pub skip_missing_frameworks: bool,
}

impl Default for FrameworkBenchmarkConfig {
    fn default() -> Self {
        Self {
            base_config: BenchmarkConfig::default(),
            frameworks_to_test: vec![
                "numpy".to_string(),
                "pytorch".to_string(),
                "tensorflow".to_string(),
            ],
            python_executable: "python3".to_string(),
            skip_missing_frameworks: true,
        }
    }
}

/// Check if a framework is available via Python
fn check_framework_availability(framework: &str, python_executable: &str) -> bool {
    let import_name = match framework {
        "numpy" => "numpy",
        "pytorch" => "torch",
        "tensorflow" => "tensorflow",
        _ => framework,
    };

    let output = Command::new(python_executable)
        .arg("-c")
        .arg(format!("import {import_name}"))
        .output();

    output.as_ref().map(|o| o.status.success()).unwrap_or(false)
}

/// Generate Python benchmark script for a specific operation
fn generate_python_benchmark_script(
    framework: &str,
    operation: &str,
    size: usize,
    iterations: usize,
) -> String {
    let setup_code = match framework {
        "numpy" => format!(
            r#"
import numpy as np
import time
a = np.random.randn({size}).astype(np.float32)
b = np.random.randn({size}).astype(np.float32)
"#
        ),
        "pytorch" => format!(
            r#"
import torch
import time
a = torch.randn({size}, dtype=torch.float32)
b = torch.randn({size}, dtype=torch.float32)
"#
        ),
        "tensorflow" => format!(
            r#"
import tensorflow as tf
import time
a = tf.random.normal([{size}], dtype=tf.float32)
b = tf.random.normal([{size}], dtype=tf.float32)
"#
        ),
        _ => return String::new(),
    };

    let operation_code = match (framework, operation) {
        ("numpy", "add") => "result = np.add(a, b)",
        ("numpy", "mul") => "result = np.multiply(a, b)",
        ("numpy", "sub") => "result = np.subtract(a, b)",
        ("numpy", "div") => "result = np.divide(a, b)",
        ("pytorch", "add") => "result = torch.add(a, b)",
        ("pytorch", "mul") => "result = torch.mul(a, b)",
        ("pytorch", "sub") => "result = torch.sub(a, b)",
        ("pytorch", "div") => "result = torch.div(a, b)",
        ("tensorflow", "add") => "result = tf.add(a, b)",
        ("tensorflow", "mul") => "result = tf.multiply(a, b)",
        ("tensorflow", "sub") => "result = tf.subtract(a, b)",
        ("tensorflow", "div") => "result = tf.divide(a, b)",
        _ => return String::new(),
    };

    format!(
        r#"
{setup_code}

# Warmup
for _ in range(5):
    {operation_code}

# Benchmark
start_time = time.perf_counter()
for _ in range({iterations}):
    {operation_code}
end_time = time.perf_counter()

elapsed_ns = (end_time - start_time) * 1e9
print(f"{{elapsed_ns:.0f}}")
"#
    )
}

/// Benchmark a specific operation against external frameworks
fn benchmark_operation_against_frameworks(
    operation: &str,
    size: usize,
    config: &FrameworkBenchmarkConfig,
) -> Result<FrameworkComparisonResult> {
    // Benchmark TenfloweRS
    let tenflowers_time = benchmark_tenflowers_operation(operation, size, &config.base_config)?;

    // Benchmark other frameworks
    let mut framework_times = HashMap::new();

    for framework in &config.frameworks_to_test {
        if !check_framework_availability(framework, &config.python_executable) {
            if config.skip_missing_frameworks {
                println!("Warning: {framework} not available, skipping");
                continue;
            } else {
                return Err(TensorError::other(format!(
                    "Framework {framework} not available"
                )));
            }
        }

        if let Ok(time) = benchmark_external_framework(
            framework,
            operation,
            size,
            &config.base_config,
            &config.python_executable,
        ) {
            framework_times.insert(framework.clone(), time);
        } else {
            println!("Warning: Failed to benchmark {framework} for {operation}");
        }
    }

    Ok(FrameworkComparisonResult::new(
        operation.to_string(),
        size,
        tenflowers_time,
        framework_times,
    ))
}

/// Benchmark TenfloweRS operation
fn benchmark_tenflowers_operation(
    operation: &str,
    size: usize,
    config: &BenchmarkConfig,
) -> Result<Duration> {
    // Create test data
    let a_data: Vec<f32> = (0..size).map(|i| i as f32).collect();
    let b_data: Vec<f32> = (0..size).map(|i| (i as f32) + 1.0).collect();

    let a = Tensor::from_vec(a_data, &[size])?;
    let b = Tensor::from_vec(b_data, &[size])?;

    // Warmup
    for _ in 0..config.warmup_iterations {
        match operation {
            "add" => {
                let _ = super::binary::add(&a, &b)?;
            }
            "mul" => {
                let _ = super::binary::mul(&a, &b)?;
            }
            "sub" => {
                let _ = super::binary::sub(&a, &b)?;
            }
            "div" => {
                let _ = super::binary::div(&a, &b)?;
            }
            _ => {
                return Err(TensorError::other(format!(
                    "Unknown operation: {operation}"
                )))
            }
        }
    }

    // Benchmark
    let start = Instant::now();
    for _ in 0..config.measurement_iterations {
        match operation {
            "add" => {
                let _ = super::binary::add(&a, &b)?;
            }
            "mul" => {
                let _ = super::binary::mul(&a, &b)?;
            }
            "sub" => {
                let _ = super::binary::sub(&a, &b)?;
            }
            "div" => {
                let _ = super::binary::div(&a, &b)?;
            }
            _ => {
                return Err(TensorError::other(format!(
                    "Unknown operation: {operation}"
                )))
            }
        }
    }
    let elapsed = start.elapsed() / config.measurement_iterations as u32;

    Ok(elapsed)
}

/// Benchmark external framework via Python
fn benchmark_external_framework(
    framework: &str,
    operation: &str,
    size: usize,
    config: &BenchmarkConfig,
    python_executable: &str,
) -> Result<Duration> {
    let script =
        generate_python_benchmark_script(framework, operation, size, config.measurement_iterations);

    if script.is_empty() {
        return Err(TensorError::other(format!(
            "Unsupported framework/operation: {framework}/{operation}"
        )));
    }

    let output = Command::new(python_executable)
        .arg("-c")
        .arg(&script)
        .output()
        .map_err(|e| TensorError::other(format!("Failed to execute Python script: {e}")))?;

    if !output.status.success() {
        return Err(TensorError::other(format!(
            "Python script failed: {}",
            String::from_utf8_lossy(&output.stderr)
        )));
    }

    let elapsed_ns_str = String::from_utf8_lossy(&output.stdout);
    let elapsed_ns: f64 = elapsed_ns_str
        .trim()
        .parse()
        .map_err(|e| TensorError::other(format!("Failed to parse timing result: {e}")))?;

    Ok(Duration::from_nanos(elapsed_ns as u64))
}

/// Run framework comparison benchmark suite
pub fn run_framework_comparison_benchmark(
    config: FrameworkBenchmarkConfig,
) -> Result<Vec<FrameworkComparisonResult>> {
    println!("Running TenfloweRS Framework Comparison Benchmark");
    println!("Testing against external frameworks...\n");

    let operations = vec!["add", "mul", "sub", "div"];
    let mut results = Vec::new();

    for &size in &config.base_config.sizes {
        println!("Benchmarking size: {size}");

        for operation in &operations {
            match benchmark_operation_against_frameworks(operation, size, &config) {
                Ok(result) => {
                    results.push(result);
                }
                Err(e) => {
                    println!("Warning: Failed to benchmark {operation} at size {size}: {e}");
                }
            }
        }
    }

    print_framework_comparison_results(&results);

    Ok(results)
}

/// Print framework comparison results in formatted table
pub fn print_framework_comparison_results(results: &[FrameworkComparisonResult]) {
    if results.is_empty() {
        println!("No benchmark results to display");
        return;
    }

    println!("\n{:-<120}", "");
    println!(
        "| {:^12} | {:^8} | {:^15} | {:^15} | {:^15} | {:^15} | {:^15} |",
        "Operation",
        "Size",
        "TenfloweRS (μs)",
        "NumPy (μs)",
        "PyTorch (μs)",
        "TensorFlow (μs)",
        "Relative Perf."
    );
    println!("{:-<120}", "");

    for result in results {
        let tf_us = result.tenflowers_time.as_micros();
        let numpy_us = result
            .framework_times
            .get("numpy")
            .map(|t| t.as_micros())
            .unwrap_or(0);
        let pytorch_us = result
            .framework_times
            .get("pytorch")
            .map(|t| t.as_micros())
            .unwrap_or(0);
        let tensorflow_us = result
            .framework_times
            .get("tensorflow")
            .map(|t| t.as_micros())
            .unwrap_or(0);

        // Calculate average relative performance (lower is better for TenfloweRS)
        let avg_relative = if !result.relative_performance.is_empty() {
            result.relative_performance.values().sum::<f64>()
                / result.relative_performance.len() as f64
        } else {
            0.0
        };

        println!(
            "| {:^12} | {:^8} | {:^15} | {:^15} | {:^15} | {:^15} | {:^15.2} |",
            result.operation,
            result.size,
            if tf_us > 0 {
                tf_us.to_string()
            } else {
                "-".to_string()
            },
            if numpy_us > 0 {
                numpy_us.to_string()
            } else {
                "-".to_string()
            },
            if pytorch_us > 0 {
                pytorch_us.to_string()
            } else {
                "-".to_string()
            },
            if tensorflow_us > 0 {
                tensorflow_us.to_string()
            } else {
                "-".to_string()
            },
            avg_relative
        );
    }
    println!("{:-<120}", "");

    // Summary statistics
    let all_relative_perfs: Vec<f64> = results
        .iter()
        .flat_map(|r| r.relative_performance.values())
        .cloned()
        .collect();

    if !all_relative_perfs.is_empty() {
        let avg_relative = all_relative_perfs.iter().sum::<f64>() / all_relative_perfs.len() as f64;
        let best_relative = all_relative_perfs
            .iter()
            .fold(f64::INFINITY, |a, &b| a.min(b));
        let worst_relative = all_relative_perfs.iter().fold(0.0f64, |a, &b| a.max(b));

        println!("Summary:");
        println!("  Average relative performance: {avg_relative:.2}x");
        println!("  Best relative performance: {best_relative:.2}x");
        println!("  Worst relative performance: {worst_relative:.2}x");

        if avg_relative < 1.0 {
            println!(
                "  🚀 TenfloweRS is on average {:.2}x faster than other frameworks",
                1.0 / avg_relative
            );
        } else {
            println!(
                "  ⚠️  TenfloweRS is on average {avg_relative:.2}x slower than other frameworks"
            );
        }
    }
}

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

    #[test]
    fn test_framework_availability_check() {
        // Test with Python (should be available in most environments)
        let has_python = check_framework_availability("sys", "python3")
            || check_framework_availability("sys", "python");

        // This test is environment-dependent, so we just ensure it doesn't crash
        println!("Python available: {}", has_python);
    }

    #[test]
    fn test_benchmark_script_generation() {
        let script = generate_python_benchmark_script("numpy", "add", 1000, 10);
        assert!(script.contains("import numpy"));
        assert!(script.contains("np.add"));

        let script = generate_python_benchmark_script("pytorch", "mul", 1000, 10);
        assert!(script.contains("import torch"));
        assert!(script.contains("torch.mul"));
    }

    #[test]
    fn test_framework_comparison_result() {
        let mut framework_times = HashMap::new();
        framework_times.insert("numpy".to_string(), Duration::from_millis(2));
        framework_times.insert("pytorch".to_string(), Duration::from_millis(3));

        let result = FrameworkComparisonResult::new(
            "add".to_string(),
            1000,
            Duration::from_millis(1),
            framework_times,
        );

        assert_eq!(result.operation, "add");
        assert_eq!(result.size, 1000);
        assert!(result.relative_performance.contains_key("numpy"));
        assert!(result.relative_performance.contains_key("pytorch"));

        // TenfloweRS is faster (1ms vs 2ms, 3ms), so relative should be < 1.0
        assert!(result.relative_performance["numpy"] < 1.0);
        assert!(result.relative_performance["pytorch"] < 1.0);
    }
}