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
use crate::{
    backends::{Backend, BackendType, InferenceParams},
    batch::{BatchConfig, BatchOutputFormat, BatchProcessor},
    config::Config,
    metrics::MetricsCollector,
    models::ModelManager,
};
use anyhow::Result;
use clap::{Args, ValueEnum};
use std::{path::PathBuf, sync::Arc};
use tracing::{info, warn};

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

    #[arg(short, long, help = "Input file path (JSON, JSONL, CSV, TSV, or text)")]
    pub input: PathBuf,

    #[arg(short, long, help = "Output file path")]
    pub output: Option<PathBuf>,

    #[arg(long, help = "Output format", value_enum, default_value = "json-lines")]
    pub output_format: OutputFormat,

    #[arg(long, help = "Maximum tokens to generate", default_value = "512")]
    pub max_tokens: u32,

    #[arg(long, help = "Temperature for text generation", default_value = "0.7")]
    pub temperature: f32,

    #[arg(long, help = "Top-K for text generation", default_value = "40")]
    pub top_k: u32,

    #[arg(long, help = "Top-p for text generation", default_value = "0.9")]
    pub top_p: f32,

    #[arg(long, help = "Number of concurrent requests", default_value = "4")]
    pub concurrency: usize,

    #[arg(long, help = "Timeout per request in seconds", default_value = "300")]
    pub timeout: u64,

    #[arg(long, help = "Number of retry attempts", default_value = "3")]
    pub retries: u32,

    #[arg(
        long,
        help = "Checkpoint interval (save progress every N items)",
        default_value = "100"
    )]
    pub checkpoint: u32,

    #[arg(long, help = "Continue processing on individual failures")]
    pub continue_on_error: bool,

    #[arg(long, help = "Shuffle input order for better load balancing")]
    pub shuffle: bool,

    #[arg(long, help = "Enable metrics collection")]
    pub metrics: bool,

    #[arg(long, help = "Resume from checkpoint file")]
    pub resume: Option<PathBuf>,

    #[arg(long, help = "Dry run - validate inputs without processing")]
    pub dry_run: bool,

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

    #[arg(short, long, help = "Verbose output")]
    pub verbose: bool,
}

#[derive(Clone, ValueEnum)]
pub enum OutputFormat {
    #[value(name = "json")]
    Json,
    #[value(name = "json-lines")]
    JsonLines,
    #[value(name = "csv")]
    Csv,
    #[value(name = "tsv")]
    Tsv,
}

impl From<OutputFormat> for BatchOutputFormat {
    fn from(format: OutputFormat) -> Self {
        match format {
            OutputFormat::Json => BatchOutputFormat::Json,
            OutputFormat::JsonLines => BatchOutputFormat::JsonLines,
            OutputFormat::Csv => BatchOutputFormat::Csv,
            OutputFormat::Tsv => BatchOutputFormat::Tsv,
        }
    }
}

/// Validate batch processing parameters
fn validate_parameters(args: &BatchArgs) -> Result<()> {
    // Validate model name
    if args.model.is_empty() {
        anyhow::bail!("Model name cannot be empty");
    }

    // Validate input file
    if !args.input.exists() {
        anyhow::bail!("Input file does not exist: {}", args.input.display());
    }

    // Validate output directory if specified
    if let Some(ref output) = args.output {
        if let Some(parent) = output.parent() {
            if !parent.exists() {
                anyhow::bail!("Output directory does not exist: {}", parent.display());
            }
        }
    }

    // Validate resume checkpoint if specified
    if let Some(ref resume) = args.resume {
        if !resume.exists() {
            anyhow::bail!("Checkpoint file does not exist: {}", resume.display());
        }
    }

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

    if args.max_tokens > 32768 {
        anyhow::bail!("Max tokens cannot exceed 32768");
    }

    if args.temperature < 0.0 || args.temperature > 2.0 {
        anyhow::bail!("Temperature must be between 0.0 and 2.0");
    }

    if args.top_p < 0.0 || args.top_p > 1.0 {
        anyhow::bail!("Top-p must be between 0.0 and 1.0");
    }

    if args.concurrency == 0 {
        anyhow::bail!("Concurrency must be at least 1");
    }

    if args.concurrency > 128 {
        anyhow::bail!("Concurrency cannot exceed 128");
    }

    if args.timeout == 0 {
        anyhow::bail!("Timeout must be at least 1 second");
    }

    if args.checkpoint == 0 {
        anyhow::bail!("Checkpoint interval must be at least 1");
    }

    Ok(())
}

pub async fn execute(args: BatchArgs, config: &Config) -> Result<()> {
    info!("Starting batch processing with model: {}", args.model);

    // Validate all parameters
    validate_parameters(&args)?;

    if args.dry_run {
        return validate_batch_inputs(&args).await;
    }

    // Set up metrics if requested
    let metrics = if args.metrics {
        let (collector, processor) = MetricsCollector::new();
        processor.start();
        Some(Arc::new(collector))
    } else {
        None
    };

    // Create batch configuration
    let batch_config = BatchConfig {
        concurrency: args.concurrency,
        timeout_seconds: args.timeout,
        retry_attempts: args.retries,
        checkpoint_interval: args.checkpoint,
        output_format: args.output_format.clone().into(),
        continue_on_error: args.continue_on_error,
        shuffle_inputs: args.shuffle,
    };

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

    info!("Validating model: {}", model_info.name);
    let validation_result = model_manager
        .validate_model_comprehensive(&model_info.path, Some(config))
        .await?;
    if !validation_result.is_valid {
        warn!("Model validation issues:");
        for error in &validation_result.errors {
            warn!("  - {}", error);
        }
        if !args.continue_on_error {
            return Err(anyhow::anyhow!("Model validation failed"));
        }
    }

    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)?;

    info!("Loading model...");
    let load_start = std::time::Instant::now();
    backend.load_model(&model_info).await?;
    let load_duration = load_start.elapsed();
    info!("Model loaded in {:?}", load_duration);

    // Record model load metrics
    if let Some(ref metrics) = metrics {
        metrics.record_model_loaded(
            model_info.name.clone(),
            model_info.size,
            load_duration,
            backend_type.to_string(),
        );
    }

    // Create inference parameters
    let inference_params = InferenceParams {
        max_tokens: args.max_tokens,
        temperature: args.temperature,
        top_k: args.top_k,
        top_p: args.top_p,
        stream: false, // Batch processing uses non-streaming
        stop_sequences: vec![],
        seed: None,
    };

    // Estimate total items for progress tracking
    let total_items = estimate_batch_size(&args.input).await?;
    info!("Estimated {} items to process", total_items);

    // Create batch processor
    let mut processor = BatchProcessor::new(batch_config, total_items);
    if let Some(metrics) = metrics {
        processor = processor.with_metrics(metrics);
    }

    // Determine output path
    let default_output = args.input.with_extension("batch.jsonl");
    let output_path = args.output.as_deref().unwrap_or(default_output.as_path());

    info!("Output will be saved to: {}", output_path.display());

    // Process the batch
    let progress = processor
        .process_file(
            &mut backend,
            &args.input,
            Some(output_path),
            &inference_params,
        )
        .await?;

    // Print summary
    print_batch_summary(&progress, &args);

    Ok(())
}

async fn validate_batch_inputs(args: &BatchArgs) -> Result<()> {
    info!("Validating batch inputs (dry run mode)");

    let batch_config = BatchConfig::default();
    let processor = BatchProcessor::new(batch_config, 0);

    match processor.load_inputs(&args.input).await {
        Ok(inputs) => {
            info!(
                "✓ Successfully parsed {} inputs from {}",
                inputs.len(),
                args.input.display()
            );

            if args.verbose {
                info!("Sample inputs:");
                for input in inputs.iter().take(3) {
                    info!(
                        "  {}: {} ({})",
                        input.id,
                        input.content.chars().take(50).collect::<String>(),
                        if input.content.len() > 50 { "..." } else { "" }
                    );
                }
                if inputs.len() > 3 {
                    info!("  ... and {} more", inputs.len() - 3);
                }
            }

            // Validate output path
            if let Some(output_path) = &args.output {
                if let Some(parent) = output_path.parent() {
                    if !parent.exists() {
                        warn!("Output directory does not exist: {}", parent.display());
                    }
                }
            }

            info!("✓ Batch validation complete - ready for processing");
            Ok(())
        }
        Err(e) => Err(anyhow::anyhow!("Failed to parse batch inputs: {}", e)),
    }
}

async fn estimate_batch_size(input_path: &std::path::Path) -> Result<usize> {
    let content = tokio::fs::read_to_string(input_path).await?;
    let extension = input_path
        .extension()
        .and_then(|s| s.to_str())
        .unwrap_or("");

    let count = match extension.to_lowercase().as_str() {
        "json" => {
            let value: serde_json::Value = serde_json::from_str(&content)?;
            match value {
                serde_json::Value::Array(ref items) => items.len(),
                _ => 1,
            }
        }
        "jsonl" | "ndjson" => content
            .lines()
            .filter(|line| !line.trim().is_empty())
            .count(),
        "csv" | "tsv" => {
            let delimiter = if extension == "tsv" { b'\t' } else { b',' };
            let mut rdr = csv::ReaderBuilder::new()
                .delimiter(delimiter)
                .from_reader(content.as_bytes());
            rdr.records().count()
        }
        _ => content
            .lines()
            .filter(|line| !line.trim().is_empty())
            .count(),
    };

    Ok(count)
}

fn print_batch_summary(progress: &crate::batch::BatchProgress, args: &BatchArgs) {
    println!("\n=== Batch Processing Summary ===");
    println!("Input file: {}", args.input.display());
    println!("Model: {}", args.model);
    println!("Total items: {}", progress.total_items);
    println!("Completed: {}", progress.completed_items);
    println!("Failed: {}", progress.failed_items);
    println!("Skipped: {}", progress.skipped_items);

    let success_rate = if progress.total_items > 0 {
        (progress.completed_items as f64 / progress.total_items as f64) * 100.0
    } else {
        0.0
    };
    println!("Success rate: {:.1}%", success_rate);

    if let Some(completion_time) = progress.estimated_completion {
        let duration = completion_time - progress.start_time;
        println!(
            "Processing time: {}",
            humantime::format_duration(duration.to_std().unwrap_or(std::time::Duration::ZERO))
        );
    }

    println!("Average rate: {:.2} items/second", progress.current_rate);

    if let Some(output) = &args.output {
        println!("Output saved to: {}", output.display());
    }

    if progress.failed_items > 0 {
        println!("\n⚠️  {} items failed processing", progress.failed_items);
        if args.continue_on_error {
            println!("Failed items are included in output with error details");
        }
    }

    if progress.completed_items > 0 {
        println!("\n✅ Batch processing completed successfully!");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    /// Create default BatchArgs for testing with a valid temp file
    fn create_test_args_with_file(temp_file: &NamedTempFile) -> BatchArgs {
        BatchArgs {
            model: "test-model".to_string(),
            input: temp_file.path().to_path_buf(),
            output: None,
            output_format: OutputFormat::JsonLines,
            max_tokens: 512,
            temperature: 0.7,
            top_k: 40,
            top_p: 0.9,
            concurrency: 4,
            timeout: 300,
            retries: 3,
            checkpoint: 100,
            continue_on_error: false,
            shuffle: false,
            metrics: false,
            resume: None,
            dry_run: false,
            backend: None,
            verbose: false,
        }
    }

    #[test]
    fn test_validate_empty_model() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.model = String::new();

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Model name cannot be empty")
        );
    }

    #[test]
    fn test_validate_max_tokens_zero() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.max_tokens = 0;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Max tokens must be greater than 0")
        );
    }

    #[test]
    fn test_validate_max_tokens_exceeds_limit() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.max_tokens = 32769;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Max tokens cannot exceed 32768")
        );
    }

    #[test]
    fn test_validate_max_tokens_boundary_valid() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);

        // Test lower boundary
        args.max_tokens = 1;
        assert!(validate_parameters(&args).is_ok());

        // Test upper boundary
        args.max_tokens = 32768;
        assert!(validate_parameters(&args).is_ok());
    }

    #[test]
    fn test_validate_temperature_too_low() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.temperature = -0.1;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Temperature must be between 0.0 and 2.0")
        );
    }

    #[test]
    fn test_validate_temperature_too_high() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.temperature = 2.1;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Temperature must be between 0.0 and 2.0")
        );
    }

    #[test]
    fn test_validate_temperature_boundary_valid() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);

        // Test lower boundary
        args.temperature = 0.0;
        assert!(validate_parameters(&args).is_ok());

        // Test upper boundary
        args.temperature = 2.0;
        assert!(validate_parameters(&args).is_ok());
    }

    #[test]
    fn test_validate_top_p_too_low() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.top_p = -0.1;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Top-p must be between 0.0 and 1.0")
        );
    }

    #[test]
    fn test_validate_top_p_too_high() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.top_p = 1.1;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Top-p must be between 0.0 and 1.0")
        );
    }

    #[test]
    fn test_validate_top_p_boundary_valid() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);

        // Test lower boundary
        args.top_p = 0.0;
        assert!(validate_parameters(&args).is_ok());

        // Test upper boundary
        args.top_p = 1.0;
        assert!(validate_parameters(&args).is_ok());
    }

    #[test]
    fn test_validate_concurrency_zero() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.concurrency = 0;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Concurrency must be at least 1")
        );
    }

    #[test]
    fn test_validate_concurrency_exceeds_limit() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.concurrency = 129;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Concurrency cannot exceed 128")
        );
    }

    #[test]
    fn test_validate_concurrency_boundary_valid() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);

        // Test lower boundary
        args.concurrency = 1;
        assert!(validate_parameters(&args).is_ok());

        // Test upper boundary
        args.concurrency = 128;
        assert!(validate_parameters(&args).is_ok());
    }

    #[test]
    fn test_validate_timeout_zero() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.timeout = 0;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Timeout must be at least 1 second")
        );
    }

    #[test]
    fn test_validate_timeout_boundary_valid() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);

        // Test lower boundary
        args.timeout = 1;
        assert!(validate_parameters(&args).is_ok());
    }

    #[test]
    fn test_validate_checkpoint_zero() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);
        args.checkpoint = 0;

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Checkpoint interval must be at least 1")
        );
    }

    #[test]
    fn test_validate_checkpoint_boundary_valid() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let mut args = create_test_args_with_file(&temp_file);

        // Test lower boundary
        args.checkpoint = 1;
        assert!(validate_parameters(&args).is_ok());
    }

    #[test]
    fn test_validate_input_file_not_exists() {
        let args = BatchArgs {
            model: "test-model".to_string(),
            input: PathBuf::from("/nonexistent/path/to/file.json"),
            output: None,
            output_format: OutputFormat::JsonLines,
            max_tokens: 512,
            temperature: 0.7,
            top_k: 40,
            top_p: 0.9,
            concurrency: 4,
            timeout: 300,
            retries: 3,
            checkpoint: 100,
            continue_on_error: false,
            shuffle: false,
            metrics: false,
            resume: None,
            dry_run: false,
            backend: None,
            verbose: false,
        };

        let result = validate_parameters(&args);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("Input file does not exist")
        );
    }

    #[test]
    fn test_validate_all_parameters_valid() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "test").unwrap();
        let args = create_test_args_with_file(&temp_file);

        let result = validate_parameters(&args);
        assert!(result.is_ok());
    }
}