torsh-cli 0.1.2

Command-line tools for the ToRSh deep learning framework
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
//! Real model benchmarking with actual tensor operations
//!
//! This module provides comprehensive benchmarking capabilities using real
//! torsh-tensor operations to measure actual model performance.

// Infrastructure module - functions designed for CLI command integration
#![allow(dead_code)]

use anyhow::Result;
use std::time::Instant;
use tracing::{debug, info};

// ✅ SciRS2 POLICY COMPLIANT: Use scirs2-core unified access patterns
use scirs2_core::random::{thread_rng, Distribution, Normal};

// ToRSh integration
use torsh::core::device::DeviceType;
use torsh::tensor::Tensor;

use super::tensor_integration::forward_pass;
use super::types::{TimingResult, TorshModel};

/// Benchmark configuration
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Number of warmup iterations
    pub warmup_iterations: usize,
    /// Number of measurement iterations
    pub measurement_iterations: usize,
    /// Batch size for inference
    pub batch_size: usize,
    /// Device to run benchmarks on
    pub device: DeviceType,
    /// Whether to measure memory usage
    pub measure_memory: bool,
    /// Whether to collect detailed timing statistics
    pub collect_detailed_stats: bool,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            warmup_iterations: 10,
            measurement_iterations: 100,
            batch_size: 1,
            device: DeviceType::Cpu,
            measure_memory: true,
            collect_detailed_stats: true,
        }
    }
}

/// Detailed benchmark results
#[derive(Debug, Clone)]
pub struct DetailedBenchmarkResults {
    /// Individual iteration timings (ms)
    pub iteration_timings: Vec<f64>,
    /// Warmup phase duration (ms)
    pub warmup_duration: f64,
    /// Total measurement duration (ms)
    pub measurement_duration: f64,
    /// Memory usage samples (MB)
    pub memory_samples: Vec<f64>,
    /// Peak memory usage (MB)
    pub peak_memory: f64,
    /// Average memory usage (MB)
    pub avg_memory: f64,
    /// Throughput (samples/sec)
    pub throughput: f64,
    /// Latency statistics
    pub latency_stats: LatencyStatistics,
}

/// Latency statistics
#[derive(Debug, Clone)]
pub struct LatencyStatistics {
    /// Mean latency (ms)
    pub mean: f64,
    /// Median latency (ms)
    pub median: f64,
    /// Standard deviation (ms)
    pub std_dev: f64,
    /// Minimum latency (ms)
    pub min: f64,
    /// Maximum latency (ms)
    pub max: f64,
    /// 95th percentile (ms)
    pub p95: f64,
    /// 99th percentile (ms)
    pub p99: f64,
}

/// Run comprehensive model benchmark with real tensor operations
pub fn benchmark_model_real(
    model: &TorshModel,
    config: &BenchmarkConfig,
) -> Result<DetailedBenchmarkResults> {
    info!(
        "Starting real model benchmark with {} warmup and {} measurement iterations",
        config.warmup_iterations, config.measurement_iterations
    );

    // Create input tensor for the model
    let input_shape = model
        .layers
        .first()
        .map(|l| l.input_shape.clone())
        .unwrap_or_else(|| vec![784]);

    let input = create_input_tensor(&input_shape, config.batch_size, config.device)?;

    // Warmup phase
    debug!("Running warmup phase...");
    let warmup_start = Instant::now();
    for i in 0..config.warmup_iterations {
        let _ = forward_pass(model, &input)?;

        if i % 10 == 0 {
            debug!("Warmup iteration {}/{}", i, config.warmup_iterations);
        }
    }
    let warmup_duration = warmup_start.elapsed().as_secs_f64() * 1000.0;

    debug!("Warmup completed in {:.2} ms", warmup_duration);

    // Measurement phase
    debug!("Running measurement phase...");
    let mut iteration_timings = Vec::with_capacity(config.measurement_iterations);
    let mut memory_samples = Vec::new();

    let measurement_start = Instant::now();

    for i in 0..config.measurement_iterations {
        // Measure single iteration
        let iter_start = Instant::now();
        let _ = forward_pass(model, &input)?;
        let iter_duration = iter_start.elapsed().as_secs_f64() * 1000.0;

        iteration_timings.push(iter_duration);

        // Measure memory if requested (every 10 iterations)
        if config.measure_memory && i % 10 == 0 {
            let memory_mb = estimate_memory_usage(model);
            memory_samples.push(memory_mb);
        }

        if i % 20 == 0 {
            debug!(
                "Measurement iteration {}/{} - {:.2} ms",
                i, config.measurement_iterations, iter_duration
            );
        }
    }

    let measurement_duration = measurement_start.elapsed().as_secs_f64() * 1000.0;

    debug!("Measurement completed in {:.2} ms", measurement_duration);

    // Calculate statistics
    let latency_stats = calculate_latency_statistics(&iteration_timings)?;

    let peak_memory = memory_samples
        .iter()
        .copied()
        .max_by(|a, b| {
            a.partial_cmp(b)
                .expect("memory sample values should be comparable")
        })
        .unwrap_or(0.0);

    let avg_memory = if !memory_samples.is_empty() {
        memory_samples.iter().sum::<f64>() / memory_samples.len() as f64
    } else {
        0.0
    };

    let throughput = if latency_stats.mean > 0.0 {
        (1000.0 / latency_stats.mean) * config.batch_size as f64
    } else {
        0.0
    };

    Ok(DetailedBenchmarkResults {
        iteration_timings,
        warmup_duration,
        measurement_duration,
        memory_samples,
        peak_memory,
        avg_memory,
        throughput,
        latency_stats,
    })
}

/// Convert detailed results to timing result format
pub fn to_timing_result(results: &DetailedBenchmarkResults) -> TimingResult {
    TimingResult {
        throughput_fps: results.throughput,
        latency_ms: results.latency_stats.mean,
        memory_mb: results.peak_memory,
        warmup_time_ms: results.warmup_duration,
        avg_inference_time_ms: results.latency_stats.mean,
        min_inference_time_ms: results.latency_stats.min,
        max_inference_time_ms: results.latency_stats.max,
        std_dev_ms: results.latency_stats.std_dev,
        device_utilization: None, // Would need device-specific profiling
    }
}

/// Calculate latency statistics from timing samples
fn calculate_latency_statistics(timings: &[f64]) -> Result<LatencyStatistics> {
    if timings.is_empty() {
        anyhow::bail!("No timing samples available for statistics");
    }

    let mut sorted_timings = timings.to_vec();
    sorted_timings.sort_by(|a, b| {
        a.partial_cmp(b)
            .expect("timing values should be comparable")
    });

    let mean = sorted_timings.iter().sum::<f64>() / sorted_timings.len() as f64;

    let variance = sorted_timings
        .iter()
        .map(|&t| (t - mean).powi(2))
        .sum::<f64>()
        / sorted_timings.len() as f64;

    let std_dev = variance.sqrt();

    let median = if sorted_timings.len() % 2 == 0 {
        let mid = sorted_timings.len() / 2;
        (sorted_timings[mid - 1] + sorted_timings[mid]) / 2.0
    } else {
        sorted_timings[sorted_timings.len() / 2]
    };

    let min = sorted_timings[0];
    let max = sorted_timings[sorted_timings.len() - 1];

    let p95_idx = ((sorted_timings.len() as f64 * 0.95) as usize).min(sorted_timings.len() - 1);
    let p95 = sorted_timings[p95_idx];

    let p99_idx = ((sorted_timings.len() as f64 * 0.99) as usize).min(sorted_timings.len() - 1);
    let p99 = sorted_timings[p99_idx];

    Ok(LatencyStatistics {
        mean,
        median,
        std_dev,
        min,
        max,
        p95,
        p99,
    })
}

/// Create input tensor for benchmarking
fn create_input_tensor(
    shape: &[usize],
    batch_size: usize,
    device: DeviceType,
) -> Result<Tensor<f32>> {
    let mut full_shape = vec![batch_size];
    full_shape.extend_from_slice(shape);

    let mut rng = thread_rng();
    let normal = Normal::new(0.0, 1.0)?;

    let num_elements: usize = full_shape.iter().product();
    let data: Vec<f32> = (0..num_elements)
        .map(|_| normal.sample(&mut rng) as f32)
        .collect();

    Ok(Tensor::from_data(data, full_shape, device)?)
}

/// Estimate memory usage for a model
fn estimate_memory_usage(model: &TorshModel) -> f64 {
    let param_count: u64 = model.layers.iter().map(|l| l.parameters).sum();

    // Estimate total memory (parameters + activations + gradients)
    // Assuming f32 (4 bytes) and some overhead
    let memory_bytes = param_count * 4 * 2; // params + activations

    memory_bytes as f64 / (1024.0 * 1024.0)
}

/// Compare performance across different batch sizes
pub fn benchmark_batch_sizes(
    model: &TorshModel,
    batch_sizes: &[usize],
    device: DeviceType,
) -> Result<Vec<(usize, DetailedBenchmarkResults)>> {
    let mut results = Vec::new();

    for &batch_size in batch_sizes {
        info!("Benchmarking with batch size: {}", batch_size);

        let config = BenchmarkConfig {
            batch_size,
            device,
            ..Default::default()
        };

        let bench_results = benchmark_model_real(model, &config)?;
        results.push((batch_size, bench_results));
    }

    Ok(results)
}

/// Compare performance across different devices
pub fn benchmark_devices(
    model: &TorshModel,
    devices: &[DeviceType],
) -> Result<Vec<(DeviceType, DetailedBenchmarkResults)>> {
    let mut results = Vec::new();

    for &device in devices {
        info!("Benchmarking on device: {:?}", device);

        let config = BenchmarkConfig {
            device,
            ..Default::default()
        };

        let bench_results = benchmark_model_real(model, &config)?;
        results.push((device, bench_results));
    }

    Ok(results)
}

/// Format benchmark results for display
pub fn format_benchmark_results(results: &DetailedBenchmarkResults) -> String {
    format!(
        r#"
Benchmark Results:
==================
Throughput: {:.2} samples/sec
Latency (avg): {:.2} ms
Latency (median): {:.2} ms
Latency (min): {:.2} ms
Latency (max): {:.2} ms
Latency (p95): {:.2} ms
Latency (p99): {:.2} ms
Latency (std dev): {:.2} ms

Memory:
-------
Peak: {:.2} MB
Average: {:.2} MB

Timing:
-------
Warmup: {:.2} ms
Total Measurement: {:.2} ms
Iterations: {}
"#,
        results.throughput,
        results.latency_stats.mean,
        results.latency_stats.median,
        results.latency_stats.min,
        results.latency_stats.max,
        results.latency_stats.p95,
        results.latency_stats.p99,
        results.latency_stats.std_dev,
        results.peak_memory,
        results.avg_memory,
        results.warmup_duration,
        results.measurement_duration,
        results.iteration_timings.len(),
    )
}

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

    #[test]
    fn test_latency_statistics() {
        let timings = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
        let stats = calculate_latency_statistics(&timings)
            .expect("calculate latency statistics should succeed");

        assert!((stats.mean - 5.5).abs() < 0.1);
        assert!((stats.median - 5.5).abs() < 0.1);
        assert_eq!(stats.min, 1.0);
        assert_eq!(stats.max, 10.0);
    }

    #[test]
    fn test_benchmark_config_default() {
        let config = BenchmarkConfig::default();
        assert_eq!(config.warmup_iterations, 10);
        assert_eq!(config.measurement_iterations, 100);
        assert_eq!(config.batch_size, 1);
    }

    #[test]
    fn test_create_input_tensor() {
        let tensor = create_input_tensor(&[3, 224, 224], 2, DeviceType::Cpu)
            .expect("create input tensor should succeed");
        assert_eq!(tensor.shape().dims(), &[2, 3, 224, 224]);
    }

    #[test]
    #[ignore = "Flaky test - passes individually but may fail in full suite"]
    fn test_benchmark_model_real() {
        let model = create_real_model("test", 2, DeviceType::Cpu)
            .expect("create real model should succeed");
        let config = BenchmarkConfig {
            warmup_iterations: 2,
            measurement_iterations: 5,
            batch_size: 1,
            device: DeviceType::Cpu,
            measure_memory: true,
            collect_detailed_stats: true,
        };

        let results =
            benchmark_model_real(&model, &config).expect("benchmark model real should succeed");
        assert_eq!(results.iteration_timings.len(), 5);
        assert!(results.throughput > 0.0);
        assert!(results.peak_memory > 0.0);
    }
}