inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
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
use crate::backends::{Backend, BackendType, InferenceParams};
use crate::config::Config;
use crate::models::ModelManager;
use anyhow::Result;
use clap::Args;
use std::time::{Duration, Instant};
use tracing::info;

#[derive(Args)]
pub struct BenchArgs {
    #[arg(short, long, help = "Model file path or name")]
    pub model: String,

    #[arg(short, long, help = "Number of iterations", default_value = "10")]
    pub iterations: u32,

    #[arg(long, help = "Prompt for text generation benchmarks")]
    pub prompt: Option<String>,

    #[arg(long, help = "Number of tokens to generate", default_value = "100")]
    pub tokens: u32,

    #[arg(long, help = "Warmup iterations", default_value = "3")]
    pub warmup: u32,

    #[arg(long, help = "Backend to use", value_enum)]
    pub backend: Option<BackendType>,

    #[arg(long, help = "Enable detailed per-iteration output")]
    pub verbose: bool,
}

pub async fn execute(args: BenchArgs, config: &Config) -> Result<()> {
    // Pre-execution validation
    validate_args(&args)?;

    info!("Starting benchmark for model: {}", args.model);

    let model_manager = ModelManager::new(&config.models_dir);
    let model_info = model_manager.resolve_model(&args.model).await?;

    let backend_type = args
        .backend
        .or_else(|| BackendType::from_model_path(&model_info.path))
        .ok_or_else(|| {
            anyhow::anyhow!(
                "No suitable backend found for model: {}",
                model_info.path.display()
            )
        })?;

    let mut backend = Backend::new(backend_type, &config.backend_config)?;

    println!("Loading model: {}", model_info.name);
    let load_start = Instant::now();
    backend.load_model(&model_info).await?;
    let load_time = load_start.elapsed();

    println!("Model loaded in: {:?}", load_time);
    println!();

    let prompt = args
        .prompt
        .unwrap_or_else(|| "The quick brown fox jumps over the lazy dog.".to_string());

    let inference_params = InferenceParams {
        max_tokens: args.tokens,
        temperature: 0.7,
        top_k: 40,
        top_p: 0.9,
        stream: false,
        stop_sequences: vec![],
        seed: None,
    };

    println!("Benchmark Configuration:");
    println!("  Model: {}", model_info.name);
    println!("  Backend: {}", backend_type);
    println!("  Iterations: {}", args.iterations);
    println!("  Warmup: {}", args.warmup);
    println!("  Max tokens: {}", args.tokens);
    println!(
        "  Prompt: {}",
        if prompt.len() > 50 {
            format!("{}...", &prompt[..50])
        } else {
            prompt.clone()
        }
    );
    println!();

    // Warmup
    if args.warmup > 0 {
        println!("Warming up ({} iterations)...", args.warmup);
        for i in 1..=args.warmup {
            let start = Instant::now();
            let _ = backend.infer(&prompt, &inference_params).await?;
            let duration = start.elapsed();
            if args.verbose {
                println!("  Warmup {}: {:?}", i, duration);
            }
        }
        println!("Warmup completed.\n");
    }

    // Benchmark
    println!("Running benchmark...");
    let mut durations = Vec::new();
    let mut total_tokens = 0u32;

    let bench_start = Instant::now();

    for i in 1..=args.iterations {
        let start = Instant::now();
        let result = backend.infer(&prompt, &inference_params).await?;
        let duration = start.elapsed();

        let token_count = estimate_token_count(&result);
        total_tokens += token_count;
        durations.push(duration);

        if args.verbose {
            println!(
                "  Iteration {}: {:?} ({} tokens, {:.1} tok/s)",
                i,
                duration,
                token_count,
                token_count as f64 / duration.as_secs_f64()
            );
        }
    }

    let total_time = bench_start.elapsed();

    // Statistics
    durations.sort();
    let min = durations[0];
    let max = durations[durations.len() - 1];
    let median = durations[durations.len() / 2];
    let mean = calculate_mean(&durations);

    let total_tokens_per_sec = total_tokens as f64 / total_time.as_secs_f64();
    let mean_tokens_per_sec = args.tokens as f64 / mean.as_secs_f64();

    println!("\nBenchmark Results:");
    println!("==================");
    println!("Total time: {:?}", total_time);
    println!("Total tokens: {}", total_tokens);
    println!("Throughput: {:.1} tokens/sec", total_tokens_per_sec);
    println!();
    println!("Per-iteration statistics:");
    println!(
        "  Min:    {:?} ({:.1} tok/s)",
        min,
        args.tokens as f64 / min.as_secs_f64()
    );
    println!(
        "  Max:    {:?} ({:.1} tok/s)",
        max,
        args.tokens as f64 / max.as_secs_f64()
    );
    println!("  Mean:   {:?} ({:.1} tok/s)", mean, mean_tokens_per_sec);
    println!(
        "  Median: {:?} ({:.1} tok/s)",
        median,
        args.tokens as f64 / median.as_secs_f64()
    );
    println!();

    // Performance classification
    let performance_rating = classify_performance(mean_tokens_per_sec);
    println!("Performance: {}", performance_rating);

    // Memory usage estimation
    if let Ok(memory_info) = get_memory_info() {
        println!("Estimated memory usage: {:.1} GB", memory_info.used_gb);
    }

    Ok(())
}

/// Validate benchmark arguments before execution
fn validate_args(args: &BenchArgs) -> Result<()> {
    // Validate model name
    if args.model.is_empty() {
        anyhow::bail!("Model name cannot be empty");
    }

    // Validate iterations
    if args.iterations == 0 {
        anyhow::bail!("Iterations must be greater than 0");
    }

    if args.iterations > 1000 {
        anyhow::bail!("Iterations must be 1000 or less to ensure reasonable benchmark times");
    }

    // Validate tokens
    if args.tokens == 0 {
        anyhow::bail!("Tokens must be greater than 0");
    }

    if args.tokens > 10000 {
        anyhow::bail!("Tokens must be 10000 or less to ensure reasonable benchmark times");
    }

    // Warmup is optional (can be 0), but cap at reasonable limit
    if args.warmup > 100 {
        anyhow::bail!("Warmup iterations must be 100 or less");
    }

    Ok(())
}

fn estimate_token_count(text: &str) -> u32 {
    // Rough estimation: ~4 characters per token for English text
    (text.len() as f32 / 4.0).ceil() as u32
}

fn calculate_mean(durations: &[Duration]) -> Duration {
    let total_nanos: u128 = durations.iter().map(|d| d.as_nanos()).sum();
    Duration::from_nanos((total_nanos / durations.len() as u128) as u64)
}

fn classify_performance(tokens_per_sec: f64) -> String {
    if tokens_per_sec > 100.0 {
        "Excellent (>100 tok/s)".to_string()
    } else if tokens_per_sec > 50.0 {
        "Good (50-100 tok/s)".to_string()
    } else if tokens_per_sec > 20.0 {
        "Fair (20-50 tok/s)".to_string()
    } else {
        "Needs improvement (<20 tok/s)".to_string()
    }
}

fn get_memory_info() -> Result<MemoryInfo> {
    use sysinfo::{System, SystemExt};
    let mut sys = System::new_all();
    sys.refresh_memory();

    Ok(MemoryInfo {
        used_gb: sys.used_memory() as f64 / 1024.0 / 1024.0 / 1024.0,
        total_gb: sys.total_memory() as f64 / 1024.0 / 1024.0 / 1024.0,
    })
}

struct MemoryInfo {
    used_gb: f64,
    #[allow(dead_code)]
    total_gb: f64,
}

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

    #[test]
    fn test_calculate_mean() {
        let durations = vec![
            Duration::from_millis(100),
            Duration::from_millis(200),
            Duration::from_millis(300),
        ];
        let mean = calculate_mean(&durations);
        assert_eq!(mean.as_millis(), 200);
    }

    #[test]
    fn test_estimate_token_count() {
        let text = "Hello, world!"; // 13 characters
        let count = estimate_token_count(text);
        assert_eq!(count, 4); // 13 / 4 = 3.25, ceil = 4
    }

    #[test]
    fn test_classify_performance() {
        assert_eq!(classify_performance(150.0), "Excellent (>100 tok/s)");
        assert_eq!(classify_performance(75.0), "Good (50-100 tok/s)");
        assert_eq!(classify_performance(35.0), "Fair (20-50 tok/s)");
        assert_eq!(classify_performance(10.0), "Needs improvement (<20 tok/s)");
    }

    #[test]
    fn test_validate_args_empty_model() {
        let args = BenchArgs {
            model: String::new(),
            iterations: 10,
            prompt: None,
            tokens: 100,
            warmup: 3,
            backend: None,
            verbose: false,
        };
        let result = validate_args(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Model name cannot be empty")
        );
    }

    #[test]
    fn test_validate_args_zero_iterations() {
        let args = BenchArgs {
            model: "test-model".to_string(),
            iterations: 0,
            prompt: None,
            tokens: 100,
            warmup: 3,
            backend: None,
            verbose: false,
        };
        let result = validate_args(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Iterations must be greater than 0")
        );
    }

    #[test]
    fn test_validate_args_too_many_iterations() {
        let args = BenchArgs {
            model: "test-model".to_string(),
            iterations: 1001,
            prompt: None,
            tokens: 100,
            warmup: 3,
            backend: None,
            verbose: false,
        };
        let result = validate_args(&args);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("1000 or less"));
    }

    #[test]
    fn test_validate_args_zero_tokens() {
        let args = BenchArgs {
            model: "test-model".to_string(),
            iterations: 10,
            prompt: None,
            tokens: 0,
            warmup: 3,
            backend: None,
            verbose: false,
        };
        let result = validate_args(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Tokens must be greater than 0")
        );
    }

    #[test]
    fn test_validate_args_too_many_tokens() {
        let args = BenchArgs {
            model: "test-model".to_string(),
            iterations: 10,
            prompt: None,
            tokens: 10001,
            warmup: 3,
            backend: None,
            verbose: false,
        };
        let result = validate_args(&args);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("10000 or less"));
    }

    #[test]
    fn test_validate_args_too_many_warmup() {
        let args = BenchArgs {
            model: "test-model".to_string(),
            iterations: 10,
            prompt: None,
            tokens: 100,
            warmup: 101,
            backend: None,
            verbose: false,
        };
        let result = validate_args(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Warmup iterations must be 100 or less")
        );
    }

    #[test]
    fn test_validate_args_valid() {
        let args = BenchArgs {
            model: "test-model".to_string(),
            iterations: 10,
            prompt: Some("test prompt".to_string()),
            tokens: 100,
            warmup: 3,
            backend: None,
            verbose: true,
        };
        let result = validate_args(&args);
        assert!(result.is_ok());
    }
}