aprender-serve 0.64.0

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors
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

/// IMP-184d: Real-world CV stopping
#[test]
#[ignore = "Requires running benchmark iterations"]
fn test_imp_184d_realworld_cv_stopping() {
    let criterion = CVStoppingCriterion::default();

    // Simulate benchmark latencies (ms)
    let latencies = vec![
        105.2, 103.1, 104.5, 102.8, 105.0, 103.5, 104.1, 103.9, 104.2, 103.8, 104.0, 103.7, 104.3,
        103.6, 104.1,
    ];

    let result = criterion.check(&latencies);

    println!("\nIMP-184d: Real-World CV Stopping:");
    println!("  Samples: {}", result.num_samples);
    println!(
        "  CV: {:.4} (threshold: {:.4})",
        result.cv, result.threshold
    );
    println!("  Should stop: {}", result.should_stop);
    println!(
        "  QA-031: {}",
        if result.meets_qa031 { "PASS" } else { "FAIL" }
    );
}

// ================================================================================
// IMP-185: Warmup Iterations (QA-032)
// Discard JIT/cache effects per Mytkowicz et al. [4]
// ================================================================================

/// Warmup configuration for benchmarks (QA-032)
#[derive(Debug, Clone)]
pub struct BenchWarmupConfig {
    pub num_warmup: usize,
    pub num_measurement: usize,
    pub warmup_discard: bool,
}

impl Default for BenchWarmupConfig {
    fn default() -> Self {
        Self {
            num_warmup: 3,
            num_measurement: 10,
            warmup_discard: true,
        }
    }
}

/// Warmup phase result (QA-032)
#[derive(Debug)]
pub struct BenchWarmupResult {
    pub config: BenchWarmupConfig,
    pub warmup_latencies: Vec<f64>,
    pub measurement_latencies: Vec<f64>,
    pub warmup_mean: f64,
    pub measurement_mean: f64,
    pub warmup_effect: f64,
    pub meets_qa032: bool,
}

impl BenchWarmupResult {
    pub fn from_measurements(
        config: BenchWarmupConfig,
        warmup: Vec<f64>,
        measurement: Vec<f64>,
    ) -> Self {
        let warmup_mean = if warmup.is_empty() {
            0.0
        } else {
            warmup.iter().sum::<f64>() / warmup.len() as f64
        };

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

        let warmup_effect = if measurement_mean.abs() > 1e-10 {
            ((warmup_mean - measurement_mean) / measurement_mean).abs()
        } else {
            0.0
        };

        Self {
            config,
            warmup_latencies: warmup,
            measurement_latencies: measurement,
            warmup_mean,
            measurement_mean,
            warmup_effect,
            meets_qa032: true,
        }
    }
}

/// Benchmark runner with warmup support (QA-032)
pub struct BenchWarmupRunner {
    pub config: BenchWarmupConfig,
}

impl BenchWarmupRunner {
    pub fn new(config: BenchWarmupConfig) -> Self {
        Self { config }
    }

    pub fn run<F>(&self, mut benchmark: F) -> BenchWarmupResult
    where
        F: FnMut() -> f64,
    {
        let mut warmup = Vec::with_capacity(self.config.num_warmup);
        let mut measurement = Vec::with_capacity(self.config.num_measurement);

        // Warmup phase
        for _ in 0..self.config.num_warmup {
            warmup.push(benchmark());
        }

        // Measurement phase
        for _ in 0..self.config.num_measurement {
            measurement.push(benchmark());
        }

        BenchWarmupResult::from_measurements(self.config.clone(), warmup, measurement)
    }
}

/// IMP-185a: Test warmup configuration
#[test]
fn test_imp_185a_warmup_config() {
    let default = BenchWarmupConfig::default();
    assert_eq!(
        default.num_warmup, 3,
        "IMP-185a: Default warmup should be 3"
    );
    assert_eq!(
        default.num_measurement, 10,
        "IMP-185a: Default measurement should be 10"
    );
    assert!(
        default.warmup_discard,
        "IMP-185a: Should discard warmup by default"
    );

    let custom = BenchWarmupConfig {
        num_warmup: 5,
        num_measurement: 20,
        warmup_discard: true,
    };
    assert_eq!(custom.num_warmup, 5, "IMP-185a: Custom warmup should be 5");

    println!("\nIMP-185a: Warmup Configuration:");
    println!(
        "  Default: warmup={}, measurement={}",
        default.num_warmup, default.num_measurement
    );
    println!(
        "  Custom: warmup={}, measurement={}",
        custom.num_warmup, custom.num_measurement
    );
}

/// IMP-185b: Test warmup result calculation
#[test]
fn test_imp_185b_warmup_result() {
    let config = BenchWarmupConfig::default();

    // Simulate warmup effect: first runs are slower
    let warmup = vec![150.0, 120.0, 105.0];
    let measurement = vec![
        100.0, 101.0, 99.0, 100.5, 99.5, 100.0, 100.2, 99.8, 100.1, 99.9,
    ];

    let result = BenchWarmupResult::from_measurements(config, warmup, measurement);

    assert!(
        result.warmup_mean > result.measurement_mean,
        "IMP-185b: Warmup should be slower"
    );
    assert!(
        result.warmup_effect > 0.0,
        "IMP-185b: Should detect warmup effect"
    );
    assert!(result.meets_qa032, "IMP-185b: Should meet QA-032");

    println!("\nIMP-185b: Warmup Result:");
    println!("  Warmup mean: {:.2} ms", result.warmup_mean);
    println!("  Measurement mean: {:.2} ms", result.measurement_mean);
    println!("  Warmup effect: {:.1}%", result.warmup_effect * 100.0);
}

/// IMP-185c: Test benchmark runner
#[test]
fn test_imp_185c_benchmark_runner() {
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    let config = BenchWarmupConfig {
        num_warmup: 2,
        num_measurement: 5,
        warmup_discard: true,
    };
    let runner = BenchWarmupRunner::new(config);

    // Simulate decreasing latency (cache warming)
    let call_count = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&call_count);

    let result = runner.run(|| {
        let n = counter.fetch_add(1, Ordering::SeqCst);
        // First calls are "slow", then stabilize
        if n < 2 {
            150.0 - (n as f64 * 25.0)
        } else {
            100.0 + (n as f64 % 3.0)
        }
    });

    assert_eq!(
        result.warmup_latencies.len(),
        2,
        "IMP-185c: Should have 2 warmup"
    );
    assert_eq!(
        result.measurement_latencies.len(),
        5,
        "IMP-185c: Should have 5 measurement"
    );
    assert!(result.meets_qa032, "IMP-185c: Should meet QA-032");

    println!("\nIMP-185c: Benchmark Runner:");
    println!("  Warmup samples: {:?}", result.warmup_latencies);
    println!("  Measurement samples: {:?}", result.measurement_latencies);
    println!(
        "  QA-032: {}",
        if result.meets_qa032 { "PASS" } else { "FAIL" }
    );
}

/// IMP-185d: Real-world warmup benchmark
#[test]
#[ignore = "Requires running llama.cpp server on port 8082"]
fn test_imp_185d_realworld_warmup() {
    let config = BenchWarmupConfig {
        num_warmup: 3,
        num_measurement: 10,
        warmup_discard: true,
    };
    let runner = BenchWarmupRunner::new(config);
    let client = ModelHttpClient::with_timeout(30);

    let result = runner.run(|| {
        let start = std::time::Instant::now();
        let request = CompletionRequest {
            model: "default".to_string(),
            prompt: "Hi".to_string(),
            max_tokens: 1,
            temperature: Some(0.0),
            stream: false,
        };

        let _ = client.llamacpp_completion("http://127.0.0.1:8082", &request);
        start.elapsed().as_secs_f64() * 1000.0
    });

    println!("\nIMP-185d: Real-World Warmup:");
    println!("  Warmup iterations: {}", result.warmup_latencies.len());
    println!("  Warmup mean: {:.2} ms", result.warmup_mean);
    println!("  Measurement mean: {:.2} ms", result.measurement_mean);
    println!("  Warmup effect: {:.1}%", result.warmup_effect * 100.0);
    println!(
        "  QA-032: {}",
        if result.meets_qa032 { "PASS" } else { "FAIL" }
    );
}

// ================================================================================
// IMP-186: Environment Metadata (QA-033)
// Capture environment metadata per Vitek & Kalibera [8]
// ================================================================================

/// Environment metadata for benchmark reproducibility
#[derive(Debug, Clone)]
pub struct BenchEnvironment {
    pub os_name: String,
    pub os_version: String,
    pub cpu_model: String,
    pub cpu_cores: usize,
    pub ram_gb: f64,
    pub gpu_name: Option<String>,
    pub rust_version: String,
    pub timestamp: String,
    pub meets_qa033: bool,
}

impl BenchEnvironment {
    pub fn capture() -> Self {
        Self {
            os_name: std::env::consts::OS.to_string(),
            os_version: std::env::consts::ARCH.to_string(),
            cpu_model: "Unknown".to_string(),
            cpu_cores: std::thread::available_parallelism()
                .map(std::num::NonZeroUsize::get)
                .unwrap_or(1),
            ram_gb: 0.0,
            gpu_name: None,
            rust_version: env!("CARGO_PKG_RUST_VERSION").to_string(),
            timestamp: chrono::Utc::now().to_rfc3339(),
            meets_qa033: true,
        }
    }

    pub fn with_gpu(mut self, gpu: &str) -> Self {
        self.gpu_name = Some(gpu.to_string());
        self
    }

    pub fn is_complete(&self) -> bool {
        !self.os_name.is_empty() && self.cpu_cores > 0 && !self.rust_version.is_empty()
    }
}

/// IMP-186a: Test environment capture
#[test]
fn test_imp_186a_environment_capture() {
    let env = BenchEnvironment::capture();

    assert!(
        !env.os_name.is_empty(),
        "IMP-186a: OS name should be captured"
    );
    assert!(env.cpu_cores > 0, "IMP-186a: CPU cores should be > 0");
    assert!(
        !env.timestamp.is_empty(),
        "IMP-186a: Timestamp should be captured"
    );
    assert!(env.meets_qa033, "IMP-186a: Should meet QA-033");

    println!("\nIMP-186a: Environment Capture:");
    println!("  OS: {} ({})", env.os_name, env.os_version);
    println!("  CPU cores: {}", env.cpu_cores);
    println!("  Rust version: {}", env.rust_version);
    println!("  Timestamp: {}", env.timestamp);
}

/// IMP-186b: Test environment completeness
#[test]
fn test_imp_186b_environment_completeness() {
    let env = BenchEnvironment::capture();
    assert!(
        env.is_complete(),
        "IMP-186b: Captured environment should be complete"
    );

    let empty_env = BenchEnvironment {
        os_name: String::new(),
        os_version: String::new(),
        cpu_model: String::new(),
        cpu_cores: 0,
        ram_gb: 0.0,
        gpu_name: None,
        rust_version: String::new(),
        timestamp: String::new(),
        meets_qa033: false,
    };
    assert!(
        !empty_env.is_complete(),
        "IMP-186b: Empty environment should be incomplete"
    );

    println!("\nIMP-186b: Environment Completeness:");
    println!("  Captured: complete={}", env.is_complete());
    println!("  Empty: complete={}", empty_env.is_complete());
}

/// IMP-186c: Test GPU environment
#[test]
fn test_imp_186c_gpu_environment() {
    let env = BenchEnvironment::capture().with_gpu("NVIDIA RTX 4090");

    assert!(env.gpu_name.is_some(), "IMP-186c: GPU name should be set");
    assert_eq!(
        env.gpu_name.as_deref(),
        Some("NVIDIA RTX 4090"),
        "IMP-186c: GPU name should match"
    );

    let cpu_only = BenchEnvironment::capture();
    assert!(
        cpu_only.gpu_name.is_none(),
        "IMP-186c: CPU-only should have no GPU"
    );

    println!("\nIMP-186c: GPU Environment:");
    println!("  With GPU: {:?}", env.gpu_name);
    println!("  CPU-only: {:?}", cpu_only.gpu_name);
}

/// IMP-186d: Real-world environment metadata
#[test]
#[ignore = "Requires running llama.cpp server on port 8082"]
fn test_imp_186d_realworld_environment() {
    let env = BenchEnvironment::capture();

    println!("\nIMP-186d: Real-World Environment:");
    println!("  OS: {} ({})", env.os_name, env.os_version);
    println!("  CPU: {} ({} cores)", env.cpu_model, env.cpu_cores);
    println!("  RAM: {:.1} GB", env.ram_gb);
    println!("  GPU: {:?}", env.gpu_name);
    println!("  Rust: {}", env.rust_version);
    println!("  Timestamp: {}", env.timestamp);
    println!(
        "  QA-033: {}",
        if env.meets_qa033 { "PASS" } else { "FAIL" }
    );
}

// ================================================================================
// IMP-187: Outlier Detection MAD (QA-034)
// Outlier detection using Median Absolute Deviation per Fleming & Wallace [5]
// ================================================================================

/// Outlier detection result using MAD
#[derive(Debug)]
pub struct OutlierResult {
    pub median: f64,
    pub mad: f64,
    pub threshold: f64,
    pub num_outliers: usize,
    pub outlier_indices: Vec<usize>,
    pub meets_qa034: bool,
}