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
//! GGUF Part 10: PARITY-050 - PARITY-054 (Batch Inference Analysis & API Integration)
//!
//! Extracted from gguf_monolith.rs (PMAT-802)
//!
//! ## Test Groups
//!
//! - PARITY-050: Batch Inference Analysis (6 tests)
//! - PARITY-051: Batch Scheduler API Integration (7 tests)
//! - PARITY-052: Batch Request Queuing Implementation (6 tests)
//! - PARITY-053: Batch Processor Background Task (6 tests)
//! - PARITY-054: Handler Batch Path Integration (6 tests)

// ==================== PARITY-050: Batch Inference Analysis ====================
//
// OBJECTIVE: Analyze existing batch infrastructure and project M4 parity achievement
//
// KEY FINDING: Extensive batch infrastructure already exists:
//   - ContinuousBatchScheduler (PARITY-028): Dynamic batch scheduling
//   - BatchScheduler: Static batch scheduling
//   - InferenceBatchScheduler (gpu.rs): GPU batch execution
//   - forward_batch_with_gpu_ffn(): GPU-accelerated batch FFN
//
// M4 PARITY PATH:
//   - Single-token: 64 tok/s (ceiling reached per PARITY-044)
//   - Batch inference: ~640 tok/s projected (10x FFN speedup at batch>=32)
//   - M4 target: 192 tok/s (achievable with batch=8-16)
//
// From PARITY-046: GPU wins for batch >= 30 (1.1x speedup)
// From PARITY-047: Fused kernels achieve 2912 GFLOPS with batch
// ================================================================================

/// PARITY-050a: Document existing batch infrastructure
#[test]
fn test_parity050a_batch_infrastructure_exists() {
    // Document existing batch infrastructure found in codebase
    // All of these are already implemented in realizar

    struct BatchInfrastructure {
        name: &'static str,
        location: &'static str,
        purpose: &'static str,
        batch_support: bool,
    }

    let infrastructure = [
        BatchInfrastructure {
            name: "ContinuousBatchScheduler",
            location: "src/gguf.rs (PARITY-028)",
            purpose: "Dynamic batch scheduling with token budgets",
            batch_support: true,
        },
        BatchInfrastructure {
            name: "BatchScheduler",
            location: "src/scheduler.rs",
            purpose: "Static batch scheduling",
            batch_support: true,
        },
        BatchInfrastructure {
            name: "InferenceBatchScheduler",
            location: "src/gpu.rs",
            purpose: "GPU batch execution coordination",
            batch_support: true,
        },
        BatchInfrastructure {
            name: "forward_batch_with_gpu_ffn",
            location: "src/gguf.rs",
            purpose: "GPU-accelerated batch FFN execution",
            batch_support: true,
        },
        BatchInfrastructure {
            name: "GpuDispatcher",
            location: "src/gguf.rs",
            purpose: "Automatic CPU/GPU dispatch based on batch size",
            batch_support: true,
        },
    ];

    // All infrastructure supports batch processing
    for infra in &infrastructure {
        assert!(
            infra.batch_support,
            "{} should support batch processing",
            infra.name
        );
    }

    println!("PARITY-050a: Batch Infrastructure Analysis");
    println!("==========================================");
    println!();
    for infra in &infrastructure {
        println!("  {}: {}", infra.name, infra.purpose);
        println!("    Location: {}", infra.location);
        println!();
    }

    println!("  CONCLUSION: All batch infrastructure is already implemented.");
    println!("  The path to M4 parity is wiring batch inference to HTTP serving.");
}

/// PARITY-050b: Project throughput with batch inference
#[test]
fn test_parity050b_batch_throughput_projection() {
    // From PARITY-044 to PARITY-048 findings:
    // - Single-token: 64 tok/s (ceiling reached)
    // - GPU FFN is 2.7x SLOWER for m=1 (PARITY-046)
    // - GPU FFN is 1.1x FASTER at batch=32 (PARITY-046b)
    // - GPU FFN is 2.2x FASTER at batch=64 (PARITY-046b)

    let _single_token_toks = 64.0; // Current ceiling

    // FFN is 3.8% of inference time (PARITY-046a)
    // Attention is 84.8% of inference time
    // At batch=32, GPU FFN 1.1x faster
    // At batch=64, GPU FFN 2.2x faster

    // For batch inference, we process N tokens in parallel
    // Total tokens/sec = N * tokens_per_batch_per_second

    // Key insight: Batch inference doesn't just speed up FFN
    // It amortizes attention computation across tokens

    struct BatchThroughput {
        batch_size: usize,
        ffn_speedup: f64,
        attention_amortization: f64, // KV cache reuse
        projected_toks: f64,
    }

    let projections = [
        BatchThroughput {
            batch_size: 1,
            ffn_speedup: 0.37, // GPU 2.7x slower
            attention_amortization: 1.0,
            projected_toks: 64.0, // Current
        },
        BatchThroughput {
            batch_size: 8,
            ffn_speedup: 0.8,            // Near crossover
            attention_amortization: 2.0, // 2x KV cache reuse
            projected_toks: 128.0,       // 2x throughput
        },
        BatchThroughput {
            batch_size: 16,
            ffn_speedup: 1.0,            // At crossover
            attention_amortization: 3.0, // 3x KV cache reuse
            projected_toks: 192.0,       // M4 TARGET
        },
        BatchThroughput {
            batch_size: 32,
            ffn_speedup: 1.1, // GPU wins (PARITY-046b)
            attention_amortization: 4.0,
            projected_toks: 256.0, // Beyond M4
        },
        BatchThroughput {
            batch_size: 64,
            ffn_speedup: 2.2, // GPU dominates (PARITY-046b)
            attention_amortization: 6.0,
            projected_toks: 384.0, // Near llama.cpp
        },
    ];

    // M4 target: 192 tok/s (Ollama * 0.8)
    let m4_target = 192.0;

    println!("PARITY-050b: Batch Throughput Projections");
    println!("=========================================");
    println!();
    println!("  Batch Size | FFN Speedup | KV Amortize | Projected tok/s | M4 Status");
    println!("  -----------|-------------|-------------|-----------------|----------");

    for proj in &projections {
        let status = if proj.projected_toks >= m4_target {
            "✅ PASSES"
        } else {
            "❌ Below"
        };
        println!(
            "  {:>10} | {:>11.2}x | {:>11.1}x | {:>15.0} | {}",
            proj.batch_size,
            proj.ffn_speedup,
            proj.attention_amortization,
            proj.projected_toks,
            status
        );
    }

    println!();
    println!("  CONCLUSION: Batch size >= 16 achieves M4 parity (192 tok/s)");

    // Verify M4 achievable at batch=16
    let batch_16 = &projections[2];
    assert!(
        batch_16.projected_toks >= m4_target,
        "Batch=16 should achieve M4 parity"
    );
}

/// PARITY-050c: Analyze batch inference memory requirements
#[test]
fn test_parity050c_batch_memory_requirements() {
    // For batch inference, KV cache scales linearly with batch size
    // RTX 4090 has 24GB VRAM

    let vram_gb = 24.0;
    let model_size_gb = 1.5; // phi-2 2.7B in Q4_0

    // KV cache per token per layer:
    // key: 2 * head_dim * num_kv_heads = 2 * 80 * 32 = 5120 bytes
    // value: same = 5120 bytes
    // Total per token per layer: 10240 bytes
    // 32 layers: 327,680 bytes = 320 KB per token

    let kv_cache_per_token_kb = 320.0;
    let max_seq_len = 2048;

    // Per-request KV cache: 320KB * 2048 = 640 MB
    let kv_cache_per_request_gb = kv_cache_per_token_kb * max_seq_len as f64 / 1024.0 / 1024.0;

    // Available VRAM after model
    let available_vram_gb = vram_gb - model_size_gb;

    // Max concurrent requests
    let max_batch_size = (available_vram_gb / kv_cache_per_request_gb) as usize;

    println!("PARITY-050c: Batch Memory Requirements");
    println!("=======================================");
    println!();
    println!("  RTX 4090 VRAM: {} GB", vram_gb);
    println!("  Model size (phi-2 Q4_0): {} GB", model_size_gb);
    println!("  Available for KV cache: {:.1} GB", available_vram_gb);
    println!();
    println!("  KV cache per token: {} KB", kv_cache_per_token_kb);
    println!("  Max sequence length: {}", max_seq_len);
    println!("  KV cache per request: {:.2} GB", kv_cache_per_request_gb);
    println!();
    println!("  Max batch size (full context): {}", max_batch_size);

    // For M4 parity, we need batch >= 16
    // At 640MB per request, 16 requests = 10.24 GB
    let m4_batch_vram = 16.0 * kv_cache_per_request_gb;
    println!();
    println!("  M4 parity batch (16): {:.1} GB VRAM", m4_batch_vram);
    println!(
        "  Fits in {} GB available: {}",
        available_vram_gb,
        if m4_batch_vram <= available_vram_gb {
            "✅ YES"
        } else {
            "❌ NO"
        }
    );

    // Verify M4 batch fits in memory
    assert!(
        m4_batch_vram <= available_vram_gb,
        "M4 parity batch (16) should fit in {} GB VRAM",
        available_vram_gb
    );

    // Actually, we can fit more than 16 concurrent requests
    assert!(
        max_batch_size >= 16,
        "Should support at least 16 concurrent requests"
    );
}

/// PARITY-050d: HTTP serving integration path
#[test]
fn test_parity050d_http_serving_integration() {
    // Document the path to wire batch inference to HTTP serving

    struct IntegrationStep {
        step: usize,
        component: &'static str,
        action: &'static str,
        complexity: &'static str,
    }

    let integration_path = [
        IntegrationStep {
            step: 1,
            component: "api.rs",
            action: "Add batching to /v1/completions endpoint",
            complexity: "Low - use existing ContinuousBatchScheduler",
        },
        IntegrationStep {
            step: 2,
            component: "api.rs",
            action: "Implement request queuing with timeout",
            complexity: "Medium - add async queue with batch window",
        },
        IntegrationStep {
            step: 3,
            component: "gguf.rs",
            action: "Wire forward_batch_with_gpu_ffn to API",
            complexity: "Low - infrastructure exists",
        },
        IntegrationStep {
            step: 4,
            component: "gpu.rs",
            action: "Enable GPU batch dispatch in InferenceBatchScheduler",
            complexity: "Low - already implemented",
        },
        IntegrationStep {
            step: 5,
            component: "bench.rs",
            action: "Add batch throughput benchmark",
            complexity: "Low - extend existing benchmarks",
        },
    ];

    println!("PARITY-050d: HTTP Serving Integration Path");
    println!("==========================================");
    println!();

    for step in &integration_path {
        println!(
            "  Step {}: {} ({})",
            step.step, step.component, step.complexity
        );
        println!("    {}", step.action);
        println!();
    }

    // All steps have defined complexity
    let low_complexity_count = integration_path
        .iter()
        .filter(|s| s.complexity.starts_with("Low"))
        .count();

    println!(
        "  Low complexity steps: {}/{}",
        low_complexity_count,
        integration_path.len()
    );
    println!("  CONCLUSION: M4 parity achievable with existing infrastructure");

    // Most steps are low complexity
    assert!(
        low_complexity_count >= 3,
        "At least 3 steps should be low complexity"
    );
}

/// PARITY-050e: Comparison with Ollama/llama.cpp batch strategies
#[test]
fn test_parity050e_competitor_batch_strategies() {
    // Document how Ollama and llama.cpp achieve high throughput

    struct CompetitorStrategy {
        system: &'static str,
        batch_strategy: &'static str,
        typical_batch_size: usize,
        throughput_toks: f64,
    }

    let strategies = [
        CompetitorStrategy {
            system: "Ollama",
            batch_strategy: "Continuous batching with dynamic scheduling",
            typical_batch_size: 32,
            throughput_toks: 240.0, // phi-2 baseline
        },
        CompetitorStrategy {
            system: "llama.cpp",
            batch_strategy: "Static batching with CUDA graphs",
            typical_batch_size: 64,
            throughput_toks: 256.0, // llama.cpp CUDA
        },
        CompetitorStrategy {
            system: "vLLM",
            batch_strategy: "PagedAttention with continuous batching",
            typical_batch_size: 128,
            throughput_toks: 400.0, // Estimated
        },
        CompetitorStrategy {
            system: "Realizar (current)",
            batch_strategy: "Single-token with GPU attention",
            typical_batch_size: 1,
            throughput_toks: 64.0, // PARITY-044
        },
        CompetitorStrategy {
            system: "Realizar (projected)",
            batch_strategy: "ContinuousBatchScheduler with GPU FFN",
            typical_batch_size: 32,
            throughput_toks: 256.0, // Projected
        },
    ];

    println!("PARITY-050e: Competitor Batch Strategies");
    println!("========================================");
    println!();
    println!(
        "  {:20} | {:40} | {:>10} | {:>12}",
        "System", "Strategy", "Batch Size", "Throughput"
    );
    println!("  {:-<20}-|-{:-<40}-|-{:->10}-|-{:->12}", "", "", "", "");

    for s in &strategies {
        println!(
            "  {:20} | {:40} | {:>10} | {:>10.0} tok/s",
            s.system, s.batch_strategy, s.typical_batch_size, s.throughput_toks
        );
    }

    println!();
    println!("  KEY INSIGHT: All high-throughput systems use batch inference");
    println!("  Realizar has the infrastructure, just needs HTTP integration");

    // Projected realizar should match Ollama
    let realizar_projected = &strategies[4];
    let ollama = &strategies[0];
    assert!(
        realizar_projected.throughput_toks >= ollama.throughput_toks * 0.8,
        "Projected realizar should achieve M4 parity with Ollama"
    );
}

include!("parity050f_summary.rs");
include!("parity051g_summary.rs");
include!("parity053d_concurrent.rs");