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

/// PARITY-051g: Summary and implementation checklist
#[test]
fn test_parity051g_summary() {
    println!("PARITY-051g: Batch Scheduler API Integration Summary");
    println!("====================================================");
    println!();
    println!("  IMPLEMENTATION CHECKLIST:");
    println!("  -------------------------");
    println!("  [ ] 1. Add BatchRequest/BatchResponse structs to api.rs");
    println!("  [ ] 2. Add BatchConfig struct with window/batch settings");
    println!("  [ ] 3. Add batch_scheduler field to AppState");
    println!("  [ ] 4. Add batch_request_tx channel to AppState");
    println!("  [ ] 5. Implement batch_processor background task");
    println!("  [ ] 6. Modify openai_completions_handler for batch path");
    println!("  [ ] 7. Add AppState::with_batch_scheduler() builder method");
    println!("  [ ] 8. Add integration tests for batch completions");
    println!();
    println!("  DEPENDENCIES:");
    println!("  -------------");
    println!("  - ContinuousBatchScheduler: ✅ Exists (PARITY-028)");
    println!("  - forward_batch_with_gpu_ffn: ✅ Exists");
    println!("  - OwnedQuantizedModelCachedSync: ✅ Exists");
    println!("  - tokio channels: ✅ Already in dependencies");
    println!();
    println!("  ESTIMATED CHANGES:");
    println!("  ------------------");
    println!("  - api.rs: ~150 lines (structs, handler mod, task)");
    println!("  - New tests: ~100 lines");
    println!("  - Total: ~250 lines of new code");
    println!();
    println!("  NEXT STEPS:");
    println!("  -----------");
    println!("  PARITY-052: Implement BatchRequest queuing");
    println!("  PARITY-053: Benchmark batch throughput");
    println!("  PARITY-054: Validate M4 parity achievement");

    // Test passes as documentation
    assert!(true, "PARITY-051g: Summary documented");
}

// ==================== PARITY-052: Batch Request Queuing Implementation ====================
//
// OBJECTIVE: Implement the batch request queuing infrastructure in api.rs
//
// IMPLEMENTATION COMPLETE:
//   - BatchConfig: Configuration for window timing and size thresholds
//   - ContinuousBatchRequest: Internal request with oneshot response channel
//   - ContinuousBatchResponse: Result returned via oneshot channel
//   - BatchQueueStats: Statistics for monitoring batch performance
//   - AppState extensions: batch_request_tx, batch_config, accessor methods
//
// This provides the foundation for continuous batch inference in HTTP serving.
// ================================================================================

/// PARITY-052a: Test BatchConfig default values
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity052a_batch_config_defaults() {
    use crate::api::BatchConfig;

    let config = BatchConfig::default();

    println!("PARITY-052a: BatchConfig Default Values");
    println!("=======================================");
    println!();
    println!(
        "  window_ms: {} (batch accumulation window)",
        config.window_ms
    );
    println!(
        "  min_batch: {} (minimum for GPU benefit)",
        config.min_batch
    );
    println!(
        "  optimal_batch: {} (M4 parity threshold)",
        config.optimal_batch
    );
    println!(
        "  max_batch: {} (GPU optimal from PARITY-046)",
        config.max_batch
    );
    println!("  queue_size: {} (request buffer)", config.queue_size);

    // Verify defaults match PARITY-095 aligned thresholds
    assert_eq!(
        config.window_ms, 50,
        "Default window should be 50ms (PARITY-095)"
    );
    assert_eq!(config.min_batch, 4, "Min batch should be 4");
    assert_eq!(
        config.optimal_batch, 32,
        "Optimal batch should be 32 (PARITY-095: aligned with GPU threshold)"
    );
    assert_eq!(config.max_batch, 64, "Max batch should be 64 (PARITY-095)");
    assert_eq!(config.queue_size, 1024, "Queue size should be 1024");
}

/// PARITY-052b: Test BatchConfig presets
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity052b_batch_config_presets() {
    use crate::api::BatchConfig;

    let low_latency = BatchConfig::low_latency();
    let high_throughput = BatchConfig::high_throughput();

    println!("PARITY-052b: BatchConfig Presets");
    println!("================================");
    println!();
    println!("  LOW LATENCY preset:");
    println!("    window_ms: {} (shorter wait)", low_latency.window_ms);
    println!("    min_batch: {} (smaller batches)", low_latency.min_batch);
    println!("    optimal_batch: {}", low_latency.optimal_batch);
    println!("    max_batch: {}", low_latency.max_batch);
    println!();
    println!("  HIGH THROUGHPUT preset:");
    println!("    window_ms: {} (longer wait)", high_throughput.window_ms);
    println!(
        "    min_batch: {} (larger batches)",
        high_throughput.min_batch
    );
    println!("    optimal_batch: {}", high_throughput.optimal_batch);
    println!("    max_batch: {}", high_throughput.max_batch);

    // Low latency: smaller batches, shorter window
    assert!(
        low_latency.window_ms < 10,
        "Low latency should have shorter window"
    );
    assert!(
        low_latency.optimal_batch < 16,
        "Low latency should have smaller optimal batch"
    );

    // High throughput: larger batches, longer window
    assert!(
        high_throughput.window_ms > 10,
        "High throughput should have longer window"
    );
    assert!(
        high_throughput.optimal_batch > 16,
        "High throughput should have larger optimal batch"
    );
}

/// PARITY-052c: Test BatchConfig decision methods
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity052c_batch_config_decisions() {
    use crate::api::BatchConfig;

    let config = BatchConfig::default();

    println!("PARITY-052c: BatchConfig Decision Methods");
    println!("=========================================");
    println!();

    // Test should_process threshold
    let test_sizes = [1, 4, 8, 16, 32, 64];
    println!("  should_process (batch >= {}):", config.optimal_batch);
    for size in test_sizes {
        let should = config.should_process(size);
        println!(
            "    batch={}: {}",
            size,
            if should { "✅ PROCESS" } else { "⏳ wait" }
        );
    }

    println!();
    println!("  meets_minimum (batch >= {}):", config.min_batch);
    for size in test_sizes {
        let meets = config.meets_minimum(size);
        println!(
            "    batch={}: {}",
            size,
            if meets { "✅ YES" } else { "❌ NO" }
        );
    }

    // Verify decision logic (PARITY-095: threshold aligned at 32)
    assert!(
        !config.should_process(31),
        "batch=31 should not trigger process"
    );
    assert!(
        config.should_process(32),
        "batch=32 should trigger process (GPU threshold)"
    );
    assert!(config.should_process(64), "batch=64 should trigger process");

    assert!(!config.meets_minimum(3), "batch=3 should not meet minimum");
    assert!(config.meets_minimum(4), "batch=4 should meet minimum");
}

/// PARITY-052d: Test ContinuousBatchResponse creation
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity052d_batch_response_creation() {
    use crate::api::ContinuousBatchResponse;

    println!("PARITY-052d: ContinuousBatchResponse Creation");
    println!("=============================================");
    println!();

    // Test single-request response
    let single = ContinuousBatchResponse::single(
        vec![1, 2, 3, 4, 5], // token_ids (prompt + generated)
        2,                   // prompt_len
        15.6,                // latency_ms
    );

    println!("  Single-request response:");
    println!("    batched: {}", single.batched);
    println!("    batch_size: {}", single.batch_size);
    println!("    prompt_len: {}", single.prompt_len);
    println!("    generated_tokens: {:?}", single.generated_tokens());
    println!("    latency_ms: {:.1}", single.latency_ms);

    assert!(!single.batched, "Single response should not be batched");
    assert_eq!(
        single.batch_size, 1,
        "Single response batch_size should be 1"
    );
    assert_eq!(
        single.generated_tokens(),
        &[3, 4, 5],
        "Should skip prompt tokens"
    );

    // Test batched response
    let batched = ContinuousBatchResponse::batched(
        vec![10, 20, 30, 40, 50, 60], // token_ids
        3,                            // prompt_len
        16,                           // batch_size
        83.0,                         // latency_ms
    );

    println!();
    println!("  Batched response:");
    println!("    batched: {}", batched.batched);
    println!("    batch_size: {}", batched.batch_size);
    println!("    prompt_len: {}", batched.prompt_len);
    println!("    generated_tokens: {:?}", batched.generated_tokens());
    println!("    latency_ms: {:.1}", batched.latency_ms);

    assert!(batched.batched, "Batched response should be batched");
    assert_eq!(batched.batch_size, 16, "Batch size should be 16");
    assert_eq!(
        batched.generated_tokens(),
        &[40, 50, 60],
        "Should skip prompt tokens"
    );
}

/// PARITY-052e: Test AppState batch configuration
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity052e_appstate_batch_config() {
    println!("PARITY-052e: AppState Batch Configuration");
    println!("=========================================");
    println!();
    println!("  NEW APPSTATE FIELDS:");
    println!("    - batch_request_tx: Option<mpsc::Sender<ContinuousBatchRequest>>");
    println!("    - batch_config: Option<BatchConfig>");
    println!();
    println!("  NEW ACCESSOR METHODS:");
    println!("    - batch_request_tx() -> Option<&Sender>");
    println!("    - batch_config() -> Option<&BatchConfig>");
    println!("    - batch_enabled() -> bool");
    println!("    - with_batch_config() -> Self (builder)");
    println!();
    println!("  USAGE PATTERN:");
    println!("    let (tx, rx) = tokio::sync::mpsc::channel(config.queue_size);");
    println!("    let state = AppState::with_cached_model(model)?");
    println!("        .with_batch_config(tx, BatchConfig::default());");
    println!();
    println!("  BACKWARD COMPATIBLE:");
    println!("    - batch_enabled() returns false by default");
    println!("    - Handlers check batch_enabled() before using batch path");
    println!("    - Existing single-request path unchanged");

    // Test passes as documentation
    assert!(true, "PARITY-052e: AppState batch config documented");
}

/// PARITY-052f: Summary and integration status
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity052f_summary() {
    println!("PARITY-052f: Batch Request Queuing Summary");
    println!("==========================================");
    println!();
    println!("  IMPLEMENTATION STATUS: ✅ COMPLETE");
    println!();
    println!("  STRUCTS ADDED (api.rs):");
    println!("    ✅ BatchConfig - window timing and size thresholds");
    println!("    ✅ ContinuousBatchRequest - internal request with oneshot channel");
    println!("    ✅ ContinuousBatchResponse - result with batching metadata");
    println!("    ✅ BatchQueueStats - monitoring statistics");
    println!();
    println!("  APPSTATE EXTENSIONS:");
    println!("    ✅ batch_request_tx field");
    println!("    ✅ batch_config field");
    println!("    ✅ batch_request_tx() accessor");
    println!("    ✅ batch_config() accessor");
    println!("    ✅ batch_enabled() check");
    println!("    ✅ with_batch_config() builder");
    println!();
    println!("  CONFIG PRESETS:");
    println!("    ✅ BatchConfig::default() - balanced");
    println!("    ✅ BatchConfig::low_latency() - smaller batches, shorter window");
    println!("    ✅ BatchConfig::high_throughput() - larger batches, longer window");
    println!();
    println!("  NEXT STEPS:");
    println!("    PARITY-053: Implement batch processor background task");
    println!("    PARITY-054: Benchmark batch throughput");
    println!("    PARITY-055: Validate M4 parity achievement");

    // Test passes as documentation
    assert!(true, "PARITY-052f: Summary documented");
}

// ==================== PARITY-053: Batch Processor Background Task ====================
//
// OBJECTIVE: Implement the background task that processes batched inference requests
//
// IMPLEMENTATION COMPLETE:
//   - spawn_batch_processor(): Creates channel and spawns background task
//   - batch_processor_task(): Main loop with timeout-based batching
//   - process_batch(): Processes requests concurrently within batch
//   - BatchProcessResult: Result type for batch processing
//
// BATCHING STRATEGY:
//   - Collect requests until batch_size >= optimal_batch (process immediately)
//   - Process on timeout (window_ms) if batch has requests
//   - Concurrent processing within batch using tokio::spawn
// ================================================================================

/// PARITY-053a: Document batch processor architecture
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity053a_batch_processor_architecture() {
    println!("PARITY-053a: Batch Processor Architecture");
    println!("=========================================");
    println!();
    println!("  COMPONENTS:");
    println!("    1. spawn_batch_processor(model, config) -> Sender");
    println!("       - Creates mpsc channel with config.queue_size buffer");
    println!("       - Spawns batch_processor_task as background tokio task");
    println!("       - Returns Sender for submitting requests");
    println!();
    println!("    2. batch_processor_task(rx, model, config)");
    println!("       - Main event loop running continuously");
    println!("       - Collects requests with timeout-based batching");
    println!("       - Processes when batch ready or window expires");
    println!();
    println!("    3. process_batch(model, config, batch)");
    println!("       - Spawns concurrent tokio tasks for each request");
    println!("       - Each task calls model.generate_with_cache()");
    println!("       - Sends results via oneshot channels");
    println!();
    println!("  BATCHING TRIGGERS:");
    println!("    - batch.len() >= config.optimal_batch (immediate)");
    println!("    - window_ms timeout reached (process current)");
    println!("    - Channel closed (process remaining, exit)");

    // Test passes as documentation
    assert!(true, "PARITY-053a: Architecture documented");
}

/// PARITY-053b: Document batch processor flow
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity053b_batch_processor_flow() {
    println!("PARITY-053b: Batch Processor Flow");
    println!("=================================");
    println!();
    println!("  REQUEST SUBMISSION FLOW:");
    println!("    1. HTTP handler receives /v1/completions request");
    println!("    2. Handler tokenizes prompt");
    println!("    3. Handler creates oneshot channel for response");
    println!("    4. Handler sends ContinuousBatchRequest via mpsc");
    println!("    5. Handler awaits oneshot receiver");
    println!();
    println!("  BATCH PROCESSOR FLOW:");
    println!("    1. Receive request from mpsc (with timeout)");
    println!("    2. Add to batch vector");
    println!("    3. Check if batch ready:");
    println!("       - batch.len() >= optimal_batch? -> process immediately");
    println!("       - timeout elapsed? -> process current batch");
    println!("    4. process_batch():");
    println!("       a. Spawn task for each request");
    println!("       b. Call model.generate_with_cache()");
    println!("       c. Send result via oneshot");
    println!("    5. Clear batch, reset window timer");
    println!();
    println!("  HANDLER RESPONSE FLOW:");
    println!("    1. Handler receives ContinuousBatchResponse via oneshot");
    println!("    2. Handler decodes token_ids to text");
    println!("    3. Handler returns HTTP response");

    // Test passes as documentation
    assert!(true, "PARITY-053b: Flow documented");
}

/// PARITY-053c: Document spawn_batch_processor usage
#[test]
#[cfg(feature = "gpu")]
#[serial_test::serial]
fn test_parity053c_spawn_batch_processor_usage() {
    println!("PARITY-053c: spawn_batch_processor Usage");
    println!("========================================");
    println!();
    println!("  FUNCTION SIGNATURE:");
    println!("    pub fn spawn_batch_processor(");
    println!("        model: Arc<OwnedQuantizedModelCachedSync>,");
    println!("        config: BatchConfig,");
    println!("    ) -> tokio::sync::mpsc::Sender<ContinuousBatchRequest>");
    println!();
    println!("  USAGE EXAMPLE:");
    println!("    // During server startup");
    println!("    let model = Arc::new(OwnedQuantizedModelCachedSync::new(...)?);");
    println!("    let config = BatchConfig::default();");
    println!("    let batch_tx = spawn_batch_processor(model.clone(), config.clone());");
    println!();
    println!("    // Create AppState with batch support");
    println!("    let state = AppState::with_cached_model(model)?");
    println!("        .with_batch_config(batch_tx, config);");
    println!();
    println!("  LIFECYCLE:");
    println!("    - Task runs until channel is closed (all senders dropped)");
    println!("    - Processes remaining batch on shutdown");
    println!("    - Graceful shutdown via dropping AppState");

    // Test passes as documentation
    assert!(true, "PARITY-053c: Usage documented");
}