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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! GGUF Part 11: PARITY-055 - PARITY-063 (Batch Throughput Benchmarking & Speculative Decoding Phase 2)
//!
//! Extracted from gguf_monolith.rs (PMAT-802)
//!
//! ## Test Groups
//!
//! - PARITY-055: Benchmark Batch Throughput (6 tests)
//! - PARITY-056: M4 Parity Validation (6 tests)
//! - PARITY-057: Live Benchmark Suite (6 tests)
//! - PARITY-058: Implementation Overview (6 tests)
//! - PARITY-059: Speculative Decoding API (6 tests)
//! - PARITY-060: Generate with Speculative (6 tests)
//! - PARITY-061: Handler Path Integration (6 tests)
//! - PARITY-062: Benchmark Comparison (6 tests)
//! - PARITY-063: Phase 2 Summary (6 tests)

// ============================================================
// PARITY-055: Benchmark Batch Throughput (M4 Parity Validation)
// ============================================================
//
// Goal: Verify batch inference achieves M4 parity target (192 tok/s)
//
// Throughput Model:
//   - Single-request: 64 tok/s (CPU KV cache ceiling)
//   - Batch=4: 64 * 4 = 256 tok/s (parallel requests)
//   - Batch=16: 64 * 16 = 1024 tok/s (theoretical max)
//   - Batch=32: 64 * 32 = 2048 tok/s (max batch size)
//
// M4 Parity (192 tok/s) achieved at batch >= 3

/// PARITY-055a: Throughput calculation methodology
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055a_throughput_methodology() {
    println!("PARITY-055a: Throughput Calculation Methodology");
    println!("===============================================");
    println!();
    println!("  BASELINE SINGLE-REQUEST:");
    println!("    - Current: 64 tok/s (CPU KV cache path)");
    println!("    - Limited by sequential token generation");
    println!("    - Each token requires full forward pass");
    println!();
    println!("  BATCH THROUGHPUT MODEL:");
    println!("    throughput = single_tok_s * batch_size");
    println!();
    println!("  CALCULATIONS:");
    let single_tok_s = 64.0_f64;
    for batch_size in [1, 2, 4, 8, 16, 32] {
        let throughput = single_tok_s * batch_size as f64;
        let parity = if throughput >= 192.0 { "✅ M4" } else { "" };
        println!(
            "    batch={:2}: {:4.0} tok/s {}",
            batch_size, throughput, parity
        );
    }
    println!();
    println!("  M4 PARITY THRESHOLD:");
    println!("    Target: 192 tok/s");
    println!("    Achieved at: batch >= 3");
    println!("    Optimal at: batch = 16 (1024 tok/s)");

    // Verify calculations
    assert_eq!(
        (single_tok_s * 3.0) as usize,
        192,
        "PARITY-055a: M4 parity at batch=3"
    );
    assert_eq!(
        (single_tok_s * 16.0) as usize,
        1024,
        "PARITY-055a: Optimal batch throughput"
    );
}

/// PARITY-055b: Benchmark configuration
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055b_benchmark_config() {
    println!("PARITY-055b: Benchmark Configuration");
    println!("====================================");
    println!();
    println!("  LOAD GENERATOR:");
    println!("    Tool: ab (Apache Bench) or wrk");
    println!("    Concurrency: -c 16 (match batch size)");
    println!("    Requests: -n 1000 (statistically significant)");
    println!("    Keep-alive: -k (reuse connections)");
    println!();
    println!("  SERVER CONFIGURATION:");
    println!("    BatchConfig::default():");
    println!("      window_ms: 10");
    println!("      min_batch: 4");
    println!("      optimal_batch: 16");
    println!("      max_batch: 32");
    println!("      queue_size: 1024");
    println!();
    println!("  BENCHMARK COMMAND:");
    println!("    # Start server with batch enabled");
    println!("    cargo run --release -- serve --batch");
    println!();
    println!("    # Run benchmark");
    println!("    ab -n 1000 -c 16 -k -p payload.json \\");
    println!("       -T application/json http://localhost:8080/v1/completions");
    println!();
    println!("  METRICS TO COLLECT:");
    println!("    - Requests/second (total throughput)");
    println!("    - Mean response time (latency)");
    println!("    - p95/p99 latency (tail latency)");
    println!("    - tokens/second = requests/sec * avg_tokens_per_request");

    // Verify benchmark makes sense
    let requests_per_sec = 64.0_f64; // If single request takes ~15ms
    let avg_tokens = 10.0_f64;
    let tokens_per_sec = requests_per_sec * avg_tokens;
    assert!(
        tokens_per_sec >= 192.0,
        "PARITY-055b: Throughput model valid"
    );
}

/// PARITY-055c: Latency vs throughput tradeoff
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055c_latency_tradeoff() {
    println!("PARITY-055c: Latency vs Throughput Tradeoff");
    println!("==========================================");
    println!();
    println!("  SINGLE-REQUEST (batch disabled):");
    println!("    Latency: ~15ms per request");
    println!("    Throughput: 64 tok/s");
    println!("    Best for: Interactive use, low-latency requirements");
    println!();
    println!("  BATCH MODE (batch enabled):");
    println!("    Latency: 15-25ms (includes batch window)");
    println!("    Throughput: 192-1024 tok/s");
    println!("    Best for: High-volume APIs, batch processing");
    println!();
    println!("  PRESETS:");
    println!("    low_latency:");
    println!("      window_ms: 5");
    println!("      optimal_batch: 8");
    println!("      Expected latency: +5ms");
    println!();
    println!("    high_throughput:");
    println!("      window_ms: 20");
    println!("      optimal_batch: 32");
    println!("      Expected latency: +20ms");
    println!();
    println!("  TRADEOFF CURVE:");
    let base_latency = 15.0_f64; // ms
    for (name, window, batch) in [
        ("single", 0, 1),
        ("low_latency", 5, 8),
        ("default", 10, 16),
        ("high_throughput", 20, 32),
    ] {
        let latency = base_latency + window as f64;
        let throughput = 64.0 * batch as f64;
        println!(
            "    {:16} latency={:2}ms  throughput={:4.0} tok/s",
            name, latency as i32, throughput
        );
    }

    // Verify default hits M4 parity
    assert!(
        64.0 * 16.0 >= 192.0,
        "PARITY-055c: Default config achieves M4 parity"
    );
}

/// PARITY-055d: Concurrent batch estimation
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055d_concurrent_estimation() {
    println!("PARITY-055d: Concurrent Batch Estimation");
    println!("========================================");
    println!();
    println!("  CURRENT IMPLEMENTATION:");
    println!("    - Concurrent: parallel tokio tasks per request");
    println!("    - Each request calls generate_with_cache() independently");
    println!("    - CPU-bound operations interleave across cores");
    println!();
    println!("  THROUGHPUT SCALING:");
    let single_tok_s = 64.0_f64;
    let cpu_cores = 16; // Typical server
    println!("    Cores available: {}", cpu_cores);
    println!("    Single-core: {:.0} tok/s", single_tok_s);
    println!();
    println!("  SCALING FACTORS:");
    println!("    batch=1:  {:.0} tok/s (no parallelism)", single_tok_s);
    println!(
        "    batch=4:  {:.0} tok/s (4x parallel)",
        single_tok_s * 4.0
    );
    println!(
        "    batch=8:  {:.0} tok/s (8x parallel)",
        single_tok_s * 8.0
    );
    println!(
        "    batch=16: {:.0} tok/s (16x parallel, CPU saturated)",
        single_tok_s * 16.0
    );
    println!();
    println!("  REALISTIC EXPECTATIONS:");
    println!("    - Perfect scaling up to core count");
    println!("    - Diminishing returns beyond CPU cores");
    println!("    - Memory bandwidth may become bottleneck");
    println!("    - Thermal throttling under sustained load");

    // Verify M4 parity achievable
    let m4_target = 192.0;
    let batch_for_parity = (m4_target / single_tok_s).ceil() as usize;
    assert_eq!(
        batch_for_parity, 3,
        "PARITY-055d: Need batch>=3 for M4 parity"
    );
}

/// PARITY-055e: Benchmark execution script
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055e_benchmark_script() {
    println!("PARITY-055e: Benchmark Execution Script");
    println!("=======================================");
    println!();
    println!("  SCRIPT: scripts/bench-batch-throughput.sh");
    println!();
    println!("  #!/bin/bash");
    println!("  set -e");
    println!();
    println!("  # Build release");
    println!("  cargo build --release --features cuda");
    println!();
    println!("  # Create payload");
    println!("  cat > /tmp/payload.json << 'EOF'");
    println!("  {{\"prompt\": \"Hello\", \"max_tokens\": 10}}");
    println!("  EOF");
    println!();
    println!("  # Start server with batch mode (background)");
    println!("  ./target/release/realizar serve --batch &");
    println!("  SERVER_PID=$!");
    println!("  sleep 2  # Wait for startup");
    println!();
    println!("  # Run benchmarks at different concurrency levels");
    println!("  for C in 1 4 8 16 32; do");
    println!("    echo \"=== Concurrency: $C ===\"");
    println!("    ab -n 100 -c $C -k -p /tmp/payload.json \\");
    println!("       -T application/json http://localhost:8080/v1/completions");
    println!("  done");
    println!();
    println!("  # Cleanup");
    println!("  kill $SERVER_PID");
    println!();
    println!("  OUTPUT METRICS:");
    println!("    - Requests per second");
    println!("    - Time per request");
    println!("    - Transfer rate");

    // Document script location
    assert!(true, "PARITY-055e: Benchmark script documented");
}

/// PARITY-055f: Summary and M4 parity validation
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity055f_summary() {
    println!("PARITY-055f: Batch Throughput Benchmark Summary");
    println!("===============================================");
    println!();
    println!("  M4 PARITY TARGET: 192 tok/s");
    println!();
    println!("  THEORETICAL ANALYSIS:");
    println!("    ✅ Single-request baseline: 64 tok/s");
    println!("    ✅ Batch=3 achieves: 192 tok/s (M4 parity)");
    println!("    ✅ Batch=16 achieves: 1024 tok/s (5.3x M4)");
    println!("    ✅ Batch=32 achieves: 2048 tok/s (10.7x M4)");
    println!();
    println!("  IMPLEMENTATION STATUS:");
    println!("    ✅ PARITY-052: Batch queue structs");
    println!("    ✅ PARITY-053: Background processor");
    println!("    ✅ PARITY-054: Handler integration");
    println!("    ✅ PARITY-055: Benchmark methodology");
    println!();
    println!("  VALIDATION:");
    println!("    Run: scripts/bench-batch-throughput.sh");
    println!("    Expected: >192 tok/s at concurrency >= 4");
    println!();
    println!("  NEXT STEPS:");
    println!("    PARITY-056: Execute benchmark, record results");
    println!("    PARITY-057: Optimize based on results");

    // Verify M4 parity achievable
    let single_tok_s = 64.0_f64;
    let m4_target = 192.0_f64;
    let min_batch = (m4_target / single_tok_s).ceil() as usize;
    assert_eq!(min_batch, 3, "PARITY-055f: M4 parity requires batch >= 3");
    assert!(
        single_tok_s * 16.0 > m4_target * 5.0,
        "PARITY-055f: Optimal batch exceeds 5x M4"
    );
}

// ============================================================
// PARITY-056: Execute Benchmark, Record Results
// ============================================================
//
// Execute batch throughput benchmark and validate M4 parity
//
// Expected results (based on theoretical model):
//   - Concurrency=1:  ~64 tok/s (single-request baseline)
//   - Concurrency=4:  ~256 tok/s (4x parallel)
//   - Concurrency=16: ~1024 tok/s (16x parallel)
//
// M4 Parity (192 tok/s) expected at concurrency >= 4

/// PARITY-056a: Benchmark execution prerequisites
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity056a_benchmark_prerequisites() {
    println!("PARITY-056a: Benchmark Execution Prerequisites");
    println!("==============================================");
    println!();
    println!("  REQUIRED SOFTWARE:");
    println!("    - ab (Apache Bench): apt install apache2-utils");
    println!("    - OR wrk: apt install wrk");
    println!("    - curl: for health checks");
    println!();
    println!("  SERVER BUILD:");
    println!("    cargo build --release --features cuda");
    println!();
    println!("  MODEL REQUIREMENTS:");
    println!("    - GGUF model file (any Q4_K quantized)");
    println!("    - Sufficient RAM for model loading");
    println!("    - RTX 4090 available for CUDA operations");
    println!();
    println!("  ENVIRONMENT:");
    println!("    - CPU: 16+ cores for optimal batch scaling");
    println!("    - RAM: 16GB+ for model + concurrent requests");
    println!("    - No other heavy processes running");
    println!();
    println!("  VERIFICATION:");
    println!("    # Check tools installed");
    println!("    which ab wrk curl");
    println!();
    println!("    # Check GPU available");
    println!("    nvidia-smi");

    // Document prerequisites
    assert!(true, "PARITY-056a: Prerequisites documented");
}

/// PARITY-056b: Expected benchmark results
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity056b_expected_results() {
    println!("PARITY-056b: Expected Benchmark Results");
    println!("=======================================");
    println!();
    println!("  BASELINE MEASUREMENTS (from PARITY-044 to PARITY-050):");
    println!("    Single-request: 64 tok/s");
    println!("    With KV cache: O(n) per token");
    println!("    CPU SIMD optimized: AVX2 4-accumulator");
    println!();
    println!("  EXPECTED BATCH THROUGHPUT:");
    let baseline = 64.0_f64;
    let m4_target = 192.0_f64;
    println!("    | Concurrency | Expected tok/s | M4 Status |");
    println!("    |-------------|----------------|-----------|");
    for c in [1, 2, 4, 8, 16, 32] {
        let expected = baseline * c as f64;
        let status = if expected >= m4_target {
            "✅ PARITY"
        } else {
            ""
        };
        println!("    | {:>11} | {:>14.0} | {:>9} |", c, expected, status);
    }
    println!();
    println!("  EXPECTED REQUEST LATENCY:");
    println!("    | Concurrency | Expected ms |");
    println!("    |-------------|-------------|");
    for c in [1, 4, 16, 32] {
        // Latency = base + batch_window + (batch_overhead * batch_size)
        let base_ms = 15.0_f64;
        let window_ms = 10.0_f64;
        let overhead_per_req = 0.5_f64;
        let expected_ms = base_ms + window_ms + (overhead_per_req * c as f64);
        println!("    | {:>11} | {:>11.1} |", c, expected_ms);
    }
    println!();
    println!("  M4 PARITY THRESHOLD:");
    println!("    Target: {} tok/s", m4_target as i64);
    println!("    Expected at: concurrency >= 3");
    println!("    Comfortable at: concurrency >= 4");

    // Verify expected results
    assert!(baseline * 4.0 >= m4_target, "PARITY-056b: M4 parity at c=4");
}

/// PARITY-056c: Benchmark execution steps
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity056c_execution_steps() {
    println!("PARITY-056c: Benchmark Execution Steps");
    println!("======================================");
    println!();
    println!("  STEP 1: Start Server with Batch Mode");
    println!("    # Terminal 1");
    println!("    cargo run --release --features cuda -- serve --demo --batch");
    println!();
    println!("  STEP 2: Wait for Server Ready");
    println!("    # Wait for 'Listening on' message");
    println!("    curl -s http://localhost:8080/health | jq .");
    println!();
    println!("  STEP 3: Create Request Payload");
    println!("    cat > /tmp/bench_payload.json << 'EOF'");
    println!("    {{");
    println!("      \"prompt\": \"Hello, world\",");
    println!("      \"max_tokens\": 10,");
    println!("      \"temperature\": 0.7");
    println!("    }}");
    println!("    EOF");
    println!();
    println!("  STEP 4: Run Benchmark at Each Concurrency Level");
    println!("    for C in 1 4 8 16 32; do");
    println!("      echo \"=== Concurrency: $C ===\"");
    println!("      ab -n 100 -c $C -k \\");
    println!("         -p /tmp/bench_payload.json \\");
    println!("         -T application/json \\");
    println!("         http://localhost:8080/v1/completions 2>&1 | \\");
    println!("         grep -E '(Requests per second|Time per request|Transfer rate)'");
    println!("    done");
    println!();
    println!("  STEP 5: Calculate Token Throughput");
    println!("    # tokens/sec = requests/sec * avg_tokens_per_response");
    println!("    # Example: 10 req/s * 10 tokens = 100 tok/s");

    // Document execution steps
    assert!(true, "PARITY-056c: Execution steps documented");
}

include!("parity056d_interpret.rs");
include!("parity058c_performance.rs");
include!("parity060b_draft_generate.rs");
include!("parity062a_benchmark.rs");
include!("parity063e_checklist.rs");