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

/// IMP-168d: Real-world memory leak test
#[test]
#[ignore = "Requires running llama.cpp server on port 8082"]
fn test_imp_168d_realworld_memory_leak() {
    // This test requires: llama-server -m model.gguf --host 127.0.0.1 --port 8082 -ngl 99
    // Would need to monitor /proc/[pid]/status or similar for memory tracking
    let client = ModelHttpClient::with_timeout(30);
    let request = CompletionRequest {
        model: "default".to_string(),
        prompt: "Hi".to_string(),
        max_tokens: 5,
        temperature: Some(0.0),
        stream: false,
    };

    // Run 100 cycles (abbreviated test)
    let mut success_count = 0;
    for _ in 0..100 {
        if client
            .llamacpp_completion("http://127.0.0.1:8082", &request)
            .is_ok()
        {
            success_count += 1;
        }
    }

    // test memory tracking (would need actual /proc monitoring)
    let test_cycles = vec![0, 25, 50, 75, 100];
    let test_memory = vec![4000.0, 4005.0, 4002.0, 4008.0, 4003.0]; // Stable

    let detector = MemoryLeakDetector::analyze(&test_cycles, &test_memory);

    println!("\nIMP-168d: Real-World Memory Leak Test:");
    println!("  Inference cycles completed: {}", success_count);
    println!(
        "  Leak rate: {:.2} MB/1000 cycles",
        detector.leak_rate_per_1000
    );
    println!("  Leak detected: {}", detector.leak_detected);
    println!(
        "  QA-015: {}",
        if !detector.leak_detected {
            "PASS"
        } else {
            "FAIL"
        }
    );
}

// =========================================================================
// IMP-169: Warm Inference Latency Stability (QA-017, EXTREME TDD)
// =========================================================================
// Per spec QA-017: Warm inference latency within 10% of steady state
// Run with: cargo test test_imp_169 --lib --features bench-http

/// IMP-169a: Warm latency stability measurement
#[derive(Debug, Clone)]
pub struct WarmLatencyStability {
    /// Steady state latency (average after warmup)
    pub steady_state_ms: f64,
    /// Individual warm latencies
    pub warm_latencies: Vec<f64>,
    /// Max deviation from steady state (%)
    pub max_deviation_percent: f64,
    /// Whether all samples are within 10% (QA-017)
    pub meets_qa017: bool,
    /// Number of samples exceeding 10%
    pub outlier_count: usize,
}

impl WarmLatencyStability {
    pub fn analyze(latencies: &[f64], warmup_count: usize) -> Self {
        if latencies.len() <= warmup_count {
            return Self {
                steady_state_ms: 0.0,
                warm_latencies: Vec::new(),
                max_deviation_percent: 0.0,
                meets_qa017: true,
                outlier_count: 0,
            };
        }

        let warm = &latencies[warmup_count..];
        let steady_state = warm.iter().sum::<f64>() / warm.len() as f64;

        let mut max_deviation = 0.0_f64;
        let mut outliers = 0;

        for &lat in warm {
            let deviation = ((lat - steady_state) / steady_state).abs() * 100.0;
            max_deviation = max_deviation.max(deviation);
            if deviation > 10.0 {
                outliers += 1;
            }
        }

        Self {
            steady_state_ms: steady_state,
            warm_latencies: warm.to_vec(),
            max_deviation_percent: max_deviation,
            meets_qa017: outliers == 0,
            outlier_count: outliers,
        }
    }
}

/// IMP-169a: Test warm latency stability
#[test]
fn test_imp_169a_warm_latency_stability() {
    // Stable latencies (within 10%)
    let stable = vec![
        500.0, 300.0, 200.0, // Warmup
        100.0, 102.0, 98.0, 101.0, 99.0, 103.0, 97.0, 100.0, 102.0, 98.0, // Warm
    ];
    let stable_analysis = WarmLatencyStability::analyze(&stable, 3);

    assert!(
        stable_analysis.meets_qa017,
        "IMP-169a: Stable latencies should meet QA-017"
    );
    assert!(
        stable_analysis.max_deviation_percent < 10.0,
        "IMP-169a: Max deviation should be <10%, got {:.2}%",
        stable_analysis.max_deviation_percent
    );

    // Unstable latencies (spikes beyond 10%)
    let unstable = vec![
        500.0, 300.0, 200.0, // Warmup
        100.0, 102.0, 150.0, 101.0, 99.0, 100.0, 97.0, 100.0, 102.0, 98.0, // One spike
    ];
    let unstable_analysis = WarmLatencyStability::analyze(&unstable, 3);

    assert!(
        !unstable_analysis.meets_qa017,
        "IMP-169a: Spike should fail QA-017"
    );

    println!("\nIMP-169a: Warm Latency Stability:");
    println!(
        "  Stable: steady={:.1}ms, max_dev={:.2}%, outliers={}, QA-017={}",
        stable_analysis.steady_state_ms,
        stable_analysis.max_deviation_percent,
        stable_analysis.outlier_count,
        stable_analysis.meets_qa017
    );
    println!(
        "  Unstable: steady={:.1}ms, max_dev={:.2}%, outliers={}, QA-017={}",
        unstable_analysis.steady_state_ms,
        unstable_analysis.max_deviation_percent,
        unstable_analysis.outlier_count,
        unstable_analysis.meets_qa017
    );
}

/// IMP-169b: Latency stability over time
#[derive(Debug, Clone)]
pub struct LatencyTrendAnalysis {
    /// Latency samples
    pub latencies: Vec<f64>,
    /// Trend direction ("stable", "degrading", "improving")
    pub trend: String,
    /// Slope of trend line (ms per sample)
    pub trend_slope: f64,
    /// Predicted latency after 100 more samples
    pub predicted_100: f64,
}

impl LatencyTrendAnalysis {
    pub fn analyze(latencies: &[f64]) -> Self {
        if latencies.len() < 2 {
            return Self {
                latencies: latencies.to_vec(),
                trend: "unknown".to_string(),
                trend_slope: 0.0,
                predicted_100: 0.0,
            };
        }

        // Simple linear regression
        let n = latencies.len() as f64;
        let indices: Vec<f64> = (0..latencies.len()).map(|i| i as f64).collect();
        let sum_x: f64 = indices.iter().sum();
        let sum_y: f64 = latencies.iter().sum();
        let sum_xy: f64 = indices
            .iter()
            .zip(latencies.iter())
            .map(|(x, y)| x * y)
            .sum();
        let sum_xx: f64 = indices.iter().map(|x| x.powi(2)).sum();

        let slope = (n * sum_xy - sum_x * sum_y) / (n * sum_xx - sum_x.powi(2));
        let intercept = (sum_y - slope * sum_x) / n;

        let trend = if slope.abs() < 0.1 {
            "stable"
        } else if slope > 0.0 {
            "degrading"
        } else {
            "improving"
        };

        let predicted = intercept + slope * (latencies.len() as f64 + 100.0);

        Self {
            latencies: latencies.to_vec(),
            trend: trend.to_string(),
            trend_slope: slope,
            predicted_100: predicted,
        }
    }
}

/// IMP-169b: Test latency trend analysis
#[test]
fn test_imp_169b_latency_trend() {
    // Stable trend
    let stable = vec![
        100.0, 102.0, 98.0, 101.0, 99.0, 100.0, 103.0, 97.0, 100.0, 101.0,
    ];
    let stable_trend = LatencyTrendAnalysis::analyze(&stable);

    assert_eq!(
        stable_trend.trend, "stable",
        "IMP-169b: Should detect stable trend"
    );

    // Degrading trend
    let degrading = vec![
        100.0, 105.0, 110.0, 115.0, 120.0, 125.0, 130.0, 135.0, 140.0, 145.0,
    ];
    let degrading_trend = LatencyTrendAnalysis::analyze(&degrading);

    assert_eq!(
        degrading_trend.trend, "degrading",
        "IMP-169b: Should detect degrading trend"
    );

    println!("\nIMP-169b: Latency Trend Analysis:");
    println!(
        "  Stable: trend={}, slope={:.3}ms/sample",
        stable_trend.trend, stable_trend.trend_slope
    );
    println!(
        "  Degrading: trend={}, slope={:.3}ms/sample, predicted@+100={:.1}ms",
        degrading_trend.trend, degrading_trend.trend_slope, degrading_trend.predicted_100
    );
}

/// IMP-169c: P99/P50 ratio tracking
#[derive(Debug, Clone)]
pub struct TailLatencyTracking {
    /// P50 latency
    pub p50_ms: f64,
    /// P99 latency
    pub p99_ms: f64,
    /// P99/P50 ratio
    pub tail_ratio: f64,
    /// Whether ratio is acceptable (< 2.0 per QA-012)
    pub acceptable: bool,
    /// Trend of tail ratio over time
    pub ratio_trend: String,
}

impl TailLatencyTracking {
    pub fn analyze(latencies: &[f64]) -> Self {
        let percentiles = LatencyPercentiles::from_samples(latencies);
        let tail_ratio = percentiles.tail_latency_ratio();

        Self {
            p50_ms: percentiles.p50_ms,
            p99_ms: percentiles.p99_ms,
            tail_ratio,
            acceptable: tail_ratio < 2.0,
            ratio_trend: "unknown".to_string(), // Would need multiple snapshots
        }
    }
}

/// IMP-169c: Test tail latency tracking
#[test]
fn test_imp_169c_tail_latency_tracking() {
    // Good tail latency
    let good = vec![
        100.0, 102.0, 98.0, 101.0, 99.0, 100.0, 103.0, 97.0, 100.0, 105.0,
    ];
    let good_tail = TailLatencyTracking::analyze(&good);

    assert!(
        good_tail.acceptable,
        "IMP-169c: Low variance should have acceptable tail"
    );

    // Bad tail latency (outliers)
    let bad = vec![
        100.0, 102.0, 98.0, 101.0, 99.0, 100.0, 103.0, 97.0, 300.0, 400.0,
    ];
    let bad_tail = TailLatencyTracking::analyze(&bad);

    assert!(
        !bad_tail.acceptable,
        "IMP-169c: Outliers should have unacceptable tail"
    );

    println!("\nIMP-169c: Tail Latency Tracking:");
    println!(
        "  Good: p50={:.1}ms, p99={:.1}ms, ratio={:.2}x, acceptable={}",
        good_tail.p50_ms, good_tail.p99_ms, good_tail.tail_ratio, good_tail.acceptable
    );
    println!(
        "  Bad: p50={:.1}ms, p99={:.1}ms, ratio={:.2}x, acceptable={}",
        bad_tail.p50_ms, bad_tail.p99_ms, bad_tail.tail_ratio, bad_tail.acceptable
    );
}

/// IMP-169d: Real-world warm latency stability
#[test]
#[ignore = "Requires running llama.cpp server on port 8082"]
fn test_imp_169d_realworld_warm_latency() {
    let client = ModelHttpClient::with_timeout(30);
    let request = CompletionRequest {
        model: "default".to_string(),
        prompt: "Hello".to_string(),
        max_tokens: 5,
        temperature: Some(0.0),
        stream: false,
    };

    // Collect latencies (3 warmup + 10 warm)
    let mut latencies = Vec::new();
    for _ in 0..13 {
        let start = std::time::Instant::now();
        if client
            .llamacpp_completion("http://127.0.0.1:8082", &request)
            .is_ok()
        {
            latencies.push(start.elapsed().as_secs_f64() * 1000.0);
        }
    }

    if latencies.len() < 5 {
        println!("IMP-169d: Not enough samples");
        return;
    }

    let stability = WarmLatencyStability::analyze(&latencies, 3);
    let trend = LatencyTrendAnalysis::analyze(&latencies[3..]);

    println!("\nIMP-169d: Real-World Warm Latency Stability:");
    println!("  Samples: {}", latencies.len());
    println!("  Steady state: {:.1}ms", stability.steady_state_ms);
    println!("  Max deviation: {:.2}%", stability.max_deviation_percent);
    println!(
        "  QA-017: {}",
        if stability.meets_qa017 {
            "PASS"
        } else {
            "FAIL"
        }
    );
    println!("  Trend: {}", trend.trend);
}

// =========================================================================
// IMP-170: Token Generation Rate Stability (QA-019, EXTREME TDD)
// =========================================================================
// Per spec QA-019: Token generation rate stable (CV < 10%)
// Run with: cargo test test_imp_170 --lib --features bench-http

/// IMP-170a: Token rate stability measurement
#[derive(Debug, Clone)]
pub struct TokenRateStability {
    /// Token rates for each generation (tok/s)
    pub rates: Vec<f64>,
    /// Mean rate
    pub mean_rate: f64,
    /// Standard deviation
    pub stddev_rate: f64,
    /// Coefficient of variation
    pub cv: f64,
    /// Whether CV < 10% (QA-019)
    pub meets_qa019: bool,
}

impl TokenRateStability {
    pub fn analyze(rates: &[f64]) -> Self {
        if rates.is_empty() {
            return Self {
                rates: Vec::new(),
                mean_rate: 0.0,
                stddev_rate: 0.0,
                cv: 0.0,
                meets_qa019: true,
            };
        }

        let n = rates.len();
        let mean = rates.iter().sum::<f64>() / n as f64;
        let variance = rates.iter().map(|r| (r - mean).powi(2)).sum::<f64>() / n as f64;
        let stddev = variance.sqrt();
        let cv = if mean > 0.0 { stddev / mean } else { 0.0 };

        Self {
            rates: rates.to_vec(),
            mean_rate: mean,
            stddev_rate: stddev,
            cv,
            meets_qa019: cv < 0.10,
        }
    }
}