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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#![allow(dead_code, unused_imports, unused_variables)]
//! Distributed Command Module
//!
//! This module provides distributed inference and worker pool management.
//! Features include starting distributed servers, benchmarking performance,
//! viewing worker statistics, and testing inference requests.

use crate::{
    backends::InferenceParams, config::Config, distributed::DistributedInference,
    metrics::MetricsCollector, models::ModelManager,
};
use anyhow::{Result, bail};
use clap::{Args, Subcommand};
use futures::StreamExt;
use std::{sync::Arc, time::Instant};
use tracing::{info, warn};

#[derive(Args)]
pub struct DistributedArgs {
    #[command(subcommand)]
    pub command: DistributedCommand,
}

#[derive(Subcommand)]
pub enum DistributedCommand {
    #[command(about = "Start distributed inference server")]
    Start {
        #[arg(short, long, help = "Number of worker processes", default_value = "0")]
        workers: usize,

        #[arg(short, long, help = "Model to preload")]
        preload_model: Option<String>,

        #[arg(long, help = "Enable load balancing", default_value = "true")]
        load_balancing: bool,

        #[arg(
            long,
            help = "Maximum concurrent requests per worker",
            default_value = "4"
        )]
        max_concurrent: usize,
    },

    #[command(about = "Test distributed inference performance")]
    Benchmark {
        #[arg(short, long, help = "Model name to benchmark")]
        model: String,

        #[arg(
            short,
            long,
            help = "Number of concurrent requests",
            default_value = "10"
        )]
        concurrent: usize,

        #[arg(
            short,
            long,
            help = "Number of requests per client",
            default_value = "5"
        )]
        requests: usize,

        #[arg(short, long, help = "Test prompt", default_value = "Hello, world!")]
        prompt: String,
    },

    #[command(about = "Show worker statistics")]
    Stats,

    #[command(about = "Test single inference request")]
    Test {
        #[arg(short, long, help = "Model name")]
        model: String,

        #[arg(short, long, help = "Input text", default_value = "Hello, world!")]
        input: String,

        #[arg(long, help = "Enable streaming output")]
        stream: bool,

        #[arg(long, help = "Maximum tokens", default_value = "100")]
        max_tokens: u32,

        #[arg(long, help = "Temperature (0.0-2.0)", default_value = "0.7")]
        temperature: f32,

        #[arg(long, help = "Top-K sampling", default_value = "40")]
        top_k: u32,
    },
}

pub async fn execute(args: DistributedArgs, config: &Config) -> Result<()> {
    match args.command {
        DistributedCommand::Start {
            workers,
            preload_model,
            load_balancing,
            max_concurrent,
        } => {
            start_distributed_server(
                config,
                workers,
                preload_model,
                load_balancing,
                max_concurrent,
            )
            .await
        }
        DistributedCommand::Benchmark {
            model,
            concurrent,
            requests,
            prompt,
        } => benchmark_distributed_inference(config, &model, concurrent, requests, &prompt).await,
        DistributedCommand::Stats => show_worker_stats(config).await,
        DistributedCommand::Test {
            model,
            input,
            stream,
            max_tokens,
            temperature,
            top_k,
        } => {
            test_inference(
                config,
                &model,
                &input,
                stream,
                max_tokens,
                temperature,
                top_k,
            )
            .await
        }
    }
}

// ============================================================================
// Validation Functions
// ============================================================================

/// Validate distributed start command arguments
fn validate_start_args(workers: usize, max_concurrent: usize) -> Result<()> {
    if workers > 32 {
        bail!("Worker count cannot exceed 32");
    }

    if max_concurrent == 0 {
        bail!("Max concurrent must be greater than 0");
    }

    if max_concurrent > 100 {
        bail!("Max concurrent cannot exceed 100");
    }

    Ok(())
}

/// Validate benchmark command arguments
fn validate_benchmark_args(
    model: &str,
    concurrent: usize,
    requests: usize,
    prompt: &str,
) -> Result<()> {
    if model.is_empty() {
        bail!("Model name cannot be empty");
    }

    if concurrent == 0 {
        bail!("Concurrent must be greater than 0");
    }

    if concurrent > 100 {
        bail!("Concurrent cannot exceed 100");
    }

    if requests == 0 {
        bail!("Requests must be greater than 0");
    }

    if prompt.is_empty() {
        bail!("Prompt cannot be empty");
    }

    Ok(())
}

/// Validate test inference command arguments
fn validate_test_args(model: &str, input: &str, max_tokens: u32, temperature: f32) -> Result<()> {
    if model.is_empty() {
        bail!("Model name cannot be empty");
    }

    if input.is_empty() {
        bail!("Input cannot be empty");
    }

    if max_tokens == 0 {
        bail!("Max tokens must be greater than 0");
    }

    if !(0.0..=2.0).contains(&temperature) {
        bail!("Temperature must be between 0.0 and 2.0");
    }

    Ok(())
}

// ============================================================================
// Command Implementations
// ============================================================================

async fn start_distributed_server(
    config: &Config,
    workers: usize,
    preload_model: Option<String>,
    load_balancing: bool,
    max_concurrent: usize,
) -> Result<()> {
    // Validate arguments
    validate_start_args(workers, max_concurrent)?;

    info!("Starting distributed inference server");

    let model_manager = Arc::new(ModelManager::new(&config.models_dir));
    let metrics = Some(Arc::new({
        let (collector, processor) = MetricsCollector::new();
        processor.start();
        collector
    }));

    // Override config with command-line arguments
    let mut distributed_config = config.distributed.clone();
    if workers > 0 {
        distributed_config.worker_count = workers;
    }
    distributed_config.load_balancing = load_balancing;
    distributed_config.max_concurrent_per_worker = max_concurrent;
    if preload_model.is_some() {
        distributed_config.preload_models = true;
    }

    info!(
        "Initializing {} workers with max {} concurrent requests each",
        distributed_config.worker_count, distributed_config.max_concurrent_per_worker
    );

    let mut distributed = DistributedInference::new(
        distributed_config,
        config.backend_config.clone(),
        model_manager,
        metrics,
    )
    .await?;

    info!("Distributed inference system started successfully");

    // If a specific model was requested for preloading, wait a moment for it to load
    if let Some(model_name) = preload_model {
        info!("Preloading model: {}", model_name);
        tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
    }

    // Keep the server running
    info!("Server is running. Press Ctrl+C to stop.");
    tokio::signal::ctrl_c().await?;

    info!("Shutting down distributed inference system");
    distributed.shutdown().await?;

    Ok(())
}

async fn benchmark_distributed_inference(
    config: &Config,
    model_name: &str,
    concurrent: usize,
    requests_per_client: usize,
    prompt: &str,
) -> Result<()> {
    // Validate arguments
    validate_benchmark_args(model_name, concurrent, requests_per_client, prompt)?;

    info!("Starting distributed inference benchmark");
    info!("Model: {}", model_name);
    info!("Concurrent clients: {}", concurrent);
    info!("Requests per client: {}", requests_per_client);
    info!("Prompt: \"{}\"", prompt);

    let model_manager = Arc::new(ModelManager::new(&config.models_dir));
    let metrics = Some(Arc::new({
        let (collector, processor) = MetricsCollector::new();
        processor.start();
        collector
    }));

    let distributed = Arc::new(
        DistributedInference::new(
            config.distributed.clone(),
            config.backend_config.clone(),
            model_manager,
            metrics,
        )
        .await?,
    );

    info!("Warming up workers...");
    tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;

    let total_requests = concurrent * requests_per_client;
    let start_time = Instant::now();

    info!("Starting benchmark with {} total requests", total_requests);

    let mut handles = Vec::new();

    for client_id in 0..concurrent {
        let distributed_clone = distributed.clone();
        let model_name = model_name.to_string();
        let prompt = prompt.to_string();

        let handle = tokio::spawn(async move {
            let mut client_stats = ClientStats::new(client_id);

            for request_id in 0..requests_per_client {
                let request_start = Instant::now();

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

                match distributed_clone.infer(&model_name, &prompt, &params).await {
                    Ok(response) => {
                        let duration = request_start.elapsed();
                        client_stats.record_success(duration, response.tokens_generated);

                        if request_id % 10 == 0 {
                            println!(
                                "Client {}: Request {}/{} completed in {:?}",
                                client_id,
                                request_id + 1,
                                requests_per_client,
                                duration
                            );
                        }
                    }
                    Err(e) => {
                        client_stats.record_failure();
                        warn!("Request failed for client {}: {}", client_id, e);
                    }
                }
            }

            client_stats
        });

        handles.push(handle);
    }

    // Wait for all clients to complete
    let mut all_stats = Vec::new();
    for handle in handles {
        match handle.await {
            Ok(stats) => all_stats.push(stats),
            Err(e) => warn!("Client task failed: {}", e),
        }
    }

    let total_duration = start_time.elapsed();

    // Aggregate statistics
    let total_successful = all_stats.iter().map(|s| s.successful_requests).sum::<u64>();
    let total_failed = all_stats.iter().map(|s| s.failed_requests).sum::<u64>();
    let total_tokens = all_stats.iter().map(|s| s.total_tokens).sum::<u32>();

    let avg_response_time = if total_successful > 0 {
        all_stats
            .iter()
            .map(|s| s.total_response_time.as_millis() as u64)
            .sum::<u64>()
            / total_successful
    } else {
        0
    };

    println!("\n=== Benchmark Results ===");
    println!("Total Duration: {:?}", total_duration);
    println!("Total Requests: {}", total_requests);
    println!("Successful Requests: {}", total_successful);
    println!("Failed Requests: {}", total_failed);
    println!(
        "Success Rate: {:.2}%",
        (total_successful as f64 / total_requests as f64) * 100.0
    );
    println!("Average Response Time: {}ms", avg_response_time);
    println!(
        "Requests per Second: {:.2}",
        total_successful as f64 / total_duration.as_secs_f64()
    );
    println!("Total Tokens Generated: {}", total_tokens);
    println!(
        "Tokens per Second: {:.2}",
        total_tokens as f64 / total_duration.as_secs_f64()
    );

    // Get worker statistics
    let worker_stats = distributed.get_detailed_stats().await?;
    println!("\n=== Worker Statistics ===");
    for (worker_id, stats) in worker_stats {
        println!(
            "Worker {}: {} requests, {} successful, {} failed, avg: {:?}",
            worker_id,
            stats.total_requests,
            stats.successful_requests,
            stats.failed_requests,
            stats.average_response_time
        );
    }

    Ok(())
}

async fn show_worker_stats(config: &Config) -> Result<()> {
    info!("Showing worker statistics (this would connect to a running distributed server)");

    // In a real implementation, this would connect to a running distributed server
    // For now, just show the configuration
    println!("Distributed Configuration:");
    println!("{}", serde_json::to_string_pretty(&config.distributed)?);

    Ok(())
}

async fn test_inference(
    config: &Config,
    model_name: &str,
    input: &str,
    stream: bool,
    max_tokens: u32,
    temperature: f32,
    top_k: u32,
) -> Result<()> {
    // Validate arguments
    validate_test_args(model_name, input, max_tokens, temperature)?;

    info!("Testing distributed inference");
    info!("Model: {}", model_name);
    info!("Input: \"{}\"", input);
    info!("Streaming: {}", stream);
    info!("Max tokens: {}", max_tokens);
    info!("Temperature: {}", temperature);
    info!("Top-K: {}", top_k);

    let model_manager = Arc::new(ModelManager::new(&config.models_dir));
    let metrics = Some(Arc::new({
        let (collector, processor) = MetricsCollector::new();
        processor.start();
        collector
    }));

    let distributed = DistributedInference::new(
        config.distributed.clone(),
        config.backend_config.clone(),
        model_manager,
        metrics,
    )
    .await?;

    let params = InferenceParams {
        max_tokens,
        temperature,
        top_k,
        top_p: 0.9,
        stream,
        stop_sequences: vec![],
        seed: None,
    };

    let start_time = Instant::now();

    if stream {
        info!("Starting streaming inference...");
        let mut stream = distributed.infer_stream(model_name, input, &params).await?;

        print!("Response: ");
        while let Some(token_result) = stream.next().await {
            match token_result {
                Ok(token) => print!("{}", token),
                Err(e) => {
                    eprintln!("\nStreaming error: {}", e);
                    break;
                }
            }
        }
        println!();
    } else {
        info!("Starting non-streaming inference...");
        match distributed.infer(model_name, input, &params).await {
            Ok(response) => {
                println!("Response: {}", response.output);
                println!("Tokens generated: {}", response.tokens_generated);
                println!("Worker ID: {}", response.worker_id);
                println!("Duration: {:?}", response.duration);
            }
            Err(e) => {
                eprintln!("Inference failed: {}", e);
                return Err(e);
            }
        }
    }

    let total_time = start_time.elapsed();
    println!("Total time: {:?}", total_time);

    Ok(())
}

#[derive(Debug, Clone)]
struct ClientStats {
    client_id: usize,
    successful_requests: u64,
    failed_requests: u64,
    total_response_time: std::time::Duration,
    total_tokens: u32,
}

impl ClientStats {
    fn new(client_id: usize) -> Self {
        Self {
            client_id,
            successful_requests: 0,
            failed_requests: 0,
            total_response_time: std::time::Duration::ZERO,
            total_tokens: 0,
        }
    }

    fn record_success(&mut self, duration: std::time::Duration, tokens: u32) {
        self.successful_requests += 1;
        self.total_response_time += duration;
        self.total_tokens += tokens;
    }

    fn record_failure(&mut self) {
        self.failed_requests += 1;
    }
}

// ============================================================================
// Unit Tests
// ============================================================================

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

    #[test]
    fn test_validate_start_args_valid() {
        let result = validate_start_args(4, 8);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_start_args_zero_workers() {
        // Zero workers is valid (uses default)
        let result = validate_start_args(0, 8);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_start_args_too_many_workers() {
        let result = validate_start_args(100, 8);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Worker count cannot exceed 32")
        );
    }

    #[test]
    fn test_validate_start_args_zero_max_concurrent() {
        let result = validate_start_args(4, 0);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Max concurrent must be greater than 0")
        );
    }

    #[test]
    fn test_validate_start_args_max_concurrent_exceeds_limit() {
        let result = validate_start_args(4, 200);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Max concurrent cannot exceed 100")
        );
    }

    #[test]
    fn test_validate_benchmark_args_valid() {
        let result = validate_benchmark_args("test-model", 10, 5, "Hello");
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_benchmark_args_empty_model() {
        let result = validate_benchmark_args("", 10, 5, "Hello");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Model name cannot be empty")
        );
    }

    #[test]
    fn test_validate_benchmark_args_zero_concurrent() {
        let result = validate_benchmark_args("test-model", 0, 5, "Hello");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Concurrent must be greater than 0")
        );
    }

    #[test]
    fn test_validate_benchmark_args_concurrent_exceeds_limit() {
        let result = validate_benchmark_args("test-model", 200, 5, "Hello");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Concurrent cannot exceed 100")
        );
    }

    #[test]
    fn test_validate_benchmark_args_zero_requests() {
        let result = validate_benchmark_args("test-model", 10, 0, "Hello");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Requests must be greater than 0")
        );
    }

    #[test]
    fn test_validate_benchmark_args_empty_prompt() {
        let result = validate_benchmark_args("test-model", 10, 5, "");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Prompt cannot be empty")
        );
    }

    #[test]
    fn test_validate_test_args_valid() {
        let result = validate_test_args("test-model", "Hello", 100, 0.7);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_test_args_empty_model() {
        let result = validate_test_args("", "Hello", 100, 0.7);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Model name cannot be empty")
        );
    }

    #[test]
    fn test_validate_test_args_empty_input() {
        let result = validate_test_args("test-model", "", 100, 0.7);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Input cannot be empty")
        );
    }

    #[test]
    fn test_validate_test_args_zero_max_tokens() {
        let result = validate_test_args("test-model", "Hello", 0, 0.7);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Max tokens must be greater than 0")
        );
    }

    #[test]
    fn test_validate_test_args_temperature_too_low() {
        let result = validate_test_args("test-model", "Hello", 100, -0.5);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Temperature must be between 0.0 and 2.0")
        );
    }

    #[test]
    fn test_validate_test_args_temperature_too_high() {
        let result = validate_test_args("test-model", "Hello", 100, 3.0);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Temperature must be between 0.0 and 2.0")
        );
    }

    #[test]
    fn test_validate_test_args_temperature_boundary_low() {
        let result = validate_test_args("test-model", "Hello", 100, 0.0);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_test_args_temperature_boundary_high() {
        let result = validate_test_args("test-model", "Hello", 100, 2.0);
        assert!(result.is_ok());
    }

    #[test]
    fn test_client_stats_new() {
        let stats = ClientStats::new(5);
        assert_eq!(stats.client_id, 5);
        assert_eq!(stats.successful_requests, 0);
        assert_eq!(stats.failed_requests, 0);
        assert_eq!(stats.total_tokens, 0);
    }

    #[test]
    fn test_client_stats_record_success() {
        let mut stats = ClientStats::new(1);
        stats.record_success(std::time::Duration::from_millis(100), 50);
        stats.record_success(std::time::Duration::from_millis(200), 30);

        assert_eq!(stats.successful_requests, 2);
        assert_eq!(stats.total_tokens, 80);
        assert_eq!(
            stats.total_response_time,
            std::time::Duration::from_millis(300)
        );
    }

    #[test]
    fn test_client_stats_record_failure() {
        let mut stats = ClientStats::new(1);
        stats.record_failure();
        stats.record_failure();

        assert_eq!(stats.failed_requests, 2);
        assert_eq!(stats.successful_requests, 0);
    }
}