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

/// PARITY-008c: Popper score calculation
#[test]
fn test_parity008c_popper_score_calculation() {
    // Popper score: How well predictions match evidence
    // Score = 1.0 - (|predicted - actual| / predicted)

    // Local struct for Popper score calculation testing
    struct PopperScore {
        prediction: String,
        predicted: f64,
        actual: f64,
        score: f64,
    }

    impl PopperScore {
        fn calculate(prediction: String, predicted: f64, actual: f64) -> Self {
            let score = 1.0 - ((predicted - actual).abs() / predicted);
            Self {
                prediction,
                predicted,
                actual,
                score,
            }
        }
    }

    let before = PopperScore::calculate(
        "GPU 10x faster".to_string(),
        10.0, // Predicted speedup
        2.5,  // Actual speedup
    );

    let after = PopperScore::calculate(
        "GPU 10x faster".to_string(),
        10.0, // Predicted speedup
        9.5,  // Actual speedup (after optimization)
    );

    assert!(
        before.score < after.score,
        "PARITY-008c: Score should improve when actual approaches predicted"
    );

    // Perfect match should give score of 1.0
    let perfect = PopperScore::calculate("Test".to_string(), 5.0, 5.0);
    assert!(
        (perfect.score - 1.0).abs() < 0.01,
        "PARITY-008c: Perfect match should give score ~1.0"
    );
}

/// PARITY-008d: Explicit thresholds for acceptance
#[test]
fn test_parity008d_explicit_thresholds() {
    struct AcceptanceThreshold {
        metric: String,
        minimum: f64,
        target: f64,
        stretch: f64,
    }

    let throughput_threshold = AcceptanceThreshold {
        metric: "tok/s".to_string(),
        minimum: 64.0,  // Current baseline
        target: 225.0,  // Ollama parity
        stretch: 300.0, // Exceeds Ollama
    };

    let latency_threshold = AcceptanceThreshold {
        metric: "ms/token".to_string(),
        minimum: 50.0, // Maximum acceptable
        target: 4.4,   // Ollama parity (1000/225)
        stretch: 3.3,  // Exceeds Ollama
    };

    assert!(
        throughput_threshold.minimum < throughput_threshold.target,
        "PARITY-008d: Minimum should be less than target"
    );
    assert!(
        throughput_threshold.target < throughput_threshold.stretch,
        "PARITY-008d: Target should be less than stretch"
    );
    assert!(
        latency_threshold.minimum > latency_threshold.target,
        "PARITY-008d: Latency minimum should be higher than target (lower is better)"
    );
}

/// PARITY-008e: Benchmark reproducibility check
#[test]
fn test_parity008e_benchmark_reproducibility() {
    use std::time::Instant;

    let config = GGUFConfig {
        architecture: "test".to_string(),
        constraints: crate::gguf::ArchConstraints::from_architecture("test"),
        hidden_dim: 64,
        intermediate_dim: 128,
        num_layers: 2,
        num_heads: 4,
        num_kv_heads: 4,
        vocab_size: 100,
        context_length: 64,
        rope_theta: 10000.0,
        eps: 1e-5,
        rope_type: 0,
            explicit_head_dim: None,
            query_pre_attn_scalar: None,
        bos_token_id: None,
            eos_token_id: None,
    };
    let model = create_test_model_with_config(&config);

    let gen_config = QuantizedGenerateConfig {
        max_tokens: 10,
        temperature: 0.0,
        top_k: 1,
        stop_tokens: vec![],
        trace: false,
            ..Default::default()
    };

    let prompt = vec![1u32, 2, 3];

    // Run twice with same seed/config
    let start1 = Instant::now();
    let result1 = model.generate_with_cache(&prompt, &gen_config).expect("test value should be present");
    let _time1 = start1.elapsed();

    let start2 = Instant::now();
    let result2 = model.generate_with_cache(&prompt, &gen_config).expect("test value should be present");
    let _time2 = start2.elapsed();

    // Outputs should be identical (deterministic sampling)
    assert_eq!(
        result1, result2,
        "PARITY-008e: Deterministic sampling should produce identical outputs"
    );
}

/// PARITY-008f: Measurement validation
#[test]
fn test_parity008f_measurement_validation() {
    // Validate that measurements are within expected ranges

    struct Measurement {
        value: f64,
        unit: String,
        min_valid: f64,
        max_valid: f64,
    }

    impl Measurement {
        fn is_valid(&self) -> bool {
            self.value >= self.min_valid && self.value <= self.max_valid
        }
    }

    let throughput = Measurement {
        value: 64.0,
        unit: "tok/s".to_string(),
        min_valid: 0.1,
        max_valid: 10000.0,
    };

    let latency = Measurement {
        value: 15.6,
        unit: "ms".to_string(),
        min_valid: 0.001,
        max_valid: 10000.0,
    };

    assert!(
        throughput.is_valid(),
        "PARITY-008f: Throughput should be valid"
    );
    assert!(latency.is_valid(), "PARITY-008f: Latency should be valid");

    // Invalid measurement should fail
    let invalid = Measurement {
        value: -5.0,
        unit: "tok/s".to_string(),
        min_valid: 0.0,
        max_valid: 10000.0,
    };
    assert!(
        !invalid.is_valid(),
        "PARITY-008f: Negative throughput should be invalid"
    );
}

// ========================================================================
// PARITY-009: Benchmark Infrastructure (QA-031 to QA-040)
// ========================================================================

/// Test PARITY-009a: QA-031 CV-based stopping criterion per Hoefler & Belli
#[test]
fn test_parity009a_cv_stopping_criterion() {
    /// Benchmark runner with CV-based stopping
    /// Per Hoefler & Belli SC'15: Stop when CV < threshold
    #[derive(Debug)]
    struct CVStoppingBenchmark {
        target_cv: f64,
        max_iterations: usize,
        min_iterations: usize,
    }

    impl CVStoppingBenchmark {
        fn new() -> Self {
            Self {
                target_cv: 0.05, // 5% CV threshold per spec
                max_iterations: 100,
                min_iterations: 5,
            }
        }

        fn calculate_cv(values: &[f64]) -> f64 {
            if values.len() < 2 {
                return 1.0; // High CV for insufficient data
            }
            let mean: f64 = values.iter().sum::<f64>() / values.len() as f64;
            if mean == 0.0 {
                return 0.0;
            }
            let variance: f64 =
                values.iter().map(|v| (v - mean).powi(2)).sum::<f64>() / values.len() as f64;
            variance.sqrt() / mean
        }

        fn should_stop(&self, values: &[f64]) -> (bool, f64) {
            if values.len() < self.min_iterations {
                return (false, 1.0);
            }
            if values.len() >= self.max_iterations {
                return (true, Self::calculate_cv(values));
            }
            let cv = Self::calculate_cv(values);
            (cv < self.target_cv, cv)
        }

        fn run<F>(&self, mut benchmark_fn: F) -> (Vec<f64>, usize, f64)
        where
            F: FnMut() -> f64,
        {
            let mut values = Vec::new();
            loop {
                values.push(benchmark_fn());
                let (stop, cv) = self.should_stop(&values);
                if stop {
                    let len = values.len();
                    return (values, len, cv);
                }
            }
        }
    }

    let runner = CVStoppingBenchmark::new();

    // Simulate stable measurements (low CV)
    let mut counter = 0;
    let (_values, iterations, cv) = runner.run(|| {
        counter += 1;
        100.0 + (counter as f64 * 0.01) // Very stable: 100.01, 100.02, ...
    });

    println!("\nPARITY-009a: CV-based stopping");
    println!("  Iterations: {}", iterations);
    println!("  Final CV: {:.4}", cv);
    println!("  Target CV: {:.4}", runner.target_cv);

    assert!(
        cv < runner.target_cv,
        "QA-031: CV should be below threshold"
    );
    assert!(
        iterations >= runner.min_iterations,
        "QA-031: Should run minimum iterations"
    );
    assert!(
        iterations <= runner.max_iterations,
        "QA-031: Should not exceed max iterations"
    );
}

/// Test PARITY-009b: QA-032 Warmup iterations discard
#[test]
fn test_parity009b_warmup_discard() {
    /// Benchmark with warmup discard per Mytkowicz et al.
    #[derive(Debug)]
    struct WarmupBenchmark {
        warmup_iterations: usize,
        measurement_iterations: usize,
    }

    impl WarmupBenchmark {
        fn new(warmup: usize, measure: usize) -> Self {
            Self {
                warmup_iterations: warmup,
                measurement_iterations: measure,
            }
        }

        fn run<F>(&self, mut benchmark_fn: F) -> (Vec<f64>, Vec<f64>)
        where
            F: FnMut(usize) -> f64,
        {
            let mut warmup_values = Vec::with_capacity(self.warmup_iterations);
            let mut measurement_values = Vec::with_capacity(self.measurement_iterations);

            // Warmup phase (JIT, cache warming)
            for i in 0..self.warmup_iterations {
                warmup_values.push(benchmark_fn(i));
            }

            // Measurement phase
            for i in 0..self.measurement_iterations {
                measurement_values.push(benchmark_fn(self.warmup_iterations + i));
            }

            (warmup_values, measurement_values)
        }
    }

    let runner = WarmupBenchmark::new(3, 5);

    // Simulate JIT warmup effect: first iterations are slower
    let (warmup, measurements) = runner.run(|i| {
        if i < 3 {
            200.0 - (i as f64 * 30.0) // Warmup: 200, 170, 140
        } else {
            100.0 + (i as f64 * 0.5) // Stable: ~101.5 - 103.5
        }
    });

    let warmup_mean: f64 = warmup.iter().sum::<f64>() / warmup.len() as f64;
    let measure_mean: f64 = measurements.iter().sum::<f64>() / measurements.len() as f64;

    println!("\nPARITY-009b: Warmup discard");
    println!(
        "  Warmup iterations: {} (mean: {:.1})",
        warmup.len(),
        warmup_mean
    );
    println!(
        "  Measurement iterations: {} (mean: {:.1})",
        measurements.len(),
        measure_mean
    );

    assert_eq!(warmup.len(), 3, "QA-032: Should have 3 warmup iterations");
    assert_eq!(
        measurements.len(),
        5,
        "QA-032: Should have 5 measurement iterations"
    );
    assert!(
        warmup_mean > measure_mean,
        "QA-032: Warmup should be slower (JIT effect)"
    );
}

/// Test PARITY-009c: QA-033 Environment metadata capture
#[test]
fn test_parity009c_environment_metadata() {
    /// Environment metadata per Vitek & Kalibera
    #[derive(Debug, Clone)]
    struct EnvironmentMetadata {
        // System info
        os: String,
        arch: String,
        cpu_model: String,
        cpu_cores: usize,
        ram_gb: usize,

        // Runtime info
        rust_version: String,
        cargo_profile: String,
        target_triple: String,

        // Benchmark config
        timestamp: String,
        git_commit: String,
        benchmark_version: String,
    }

    impl EnvironmentMetadata {
        fn capture() -> Self {
            Self {
                os: std::env::consts::OS.to_string(),
                arch: std::env::consts::ARCH.to_string(),
                cpu_model: "Unknown".to_string(), // Would read from /proc/cpuinfo
                cpu_cores: std::thread::available_parallelism()
                    .map(std::num::NonZero::get)
                    .unwrap_or(1),
                ram_gb: 16, // Would read from system
                rust_version: env!("CARGO_PKG_RUST_VERSION").to_string(),
                cargo_profile: if cfg!(debug_assertions) {
                    "debug"
                } else {
                    "release"
                }
                .to_string(),
                target_triple: std::env::consts::ARCH.to_string(),
                timestamp: "2025-12-13T22:00:00Z".to_string(),
                git_commit: "abc123".to_string(),
                benchmark_version: "1.0.0".to_string(),
            }
        }

        fn is_reproducible(&self) -> bool {
            !self.os.is_empty()
                && !self.arch.is_empty()
                && self.cpu_cores > 0
                && !self.cargo_profile.is_empty()
        }
    }

    let env = EnvironmentMetadata::capture();

    println!("\nPARITY-009c: Environment metadata");
    println!("  OS: {}", env.os);
    println!("  Arch: {}", env.arch);
    println!("  CPU cores: {}", env.cpu_cores);
    println!("  Profile: {}", env.cargo_profile);

    assert!(
        env.is_reproducible(),
        "QA-033: Environment must be reproducible"
    );
    assert!(!env.os.is_empty(), "QA-033: OS must be captured");
    assert!(!env.arch.is_empty(), "QA-033: Arch must be captured");
    assert!(env.cpu_cores > 0, "QA-033: CPU cores must be captured");
}