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
//! T-COV-95 Interleaved Chaos: GPU Batch Processor Saturation Tests (PMAT-802)
//!
//! Dr. Popper's directive: "The batch processor only behaves complexly when it is
//! 'Overwhelmed' and 'Interrupted' concurrently. Overwhelm gpu_handlers.rs with
//! 'Interleaved Chaos'—hundreds of overlapping requests from multiple threads to
//! force the batch processor into its deepest queue-reclamation and timeout branches."
//!
//! This module tests:
//! 1. Queue saturation with hundreds of concurrent requests
//! 2. Timeout branch triggering via slow requests
//! 3. Queue reclamation under memory pressure
//! 4. Interleaved request/cancel patterns
//! 5. Channel overflow scenarios
//!
//! Target: 517 missed lines in api/gpu_handlers.rs

use crate::api::gpu_handlers::{
    BatchConfig, GpuBatchRequest, GpuBatchResponse, GpuBatchResult, GpuBatchStats,
    GpuStatusResponse,
};

#[cfg(feature = "gpu")]
use crate::api::gpu_handlers::ContinuousBatchResponse;

// ============================================================================
// BatchConfig Chaos Tests
// ============================================================================

#[test]
fn test_batch_config_extreme_values() {
    // Test with minimum values
    let config = BatchConfig {
        window_ms: 1,     // 1ms window
        min_batch: 1,     // Minimum batch of 1
        optimal_batch: 1, // Optimal of 1
        max_batch: 1,     // Max of 1
        queue_size: 1,    // Tiny queue
        gpu_threshold: 1, // GPU at 1
    };

    assert!(config.should_process(1));
    assert!(config.meets_minimum(1));
    assert!(!config.should_process(0));
    assert!(!config.meets_minimum(0));
}

#[test]
fn test_batch_config_huge_values() {
    let config = BatchConfig {
        window_ms: 60_000,     // 60 second window
        min_batch: 1000,       // Huge minimum
        optimal_batch: 10_000, // Huge optimal
        max_batch: 100_000,    // Huge max
        queue_size: 1_000_000, // Million entry queue
        gpu_threshold: 50_000, // Huge GPU threshold
    };

    assert!(!config.should_process(9999));
    assert!(config.should_process(10_000));
    assert!(!config.meets_minimum(999));
    assert!(config.meets_minimum(1000));
}

#[test]
fn test_batch_config_inverted_thresholds() {
    // min > optimal > max (pathological but should not panic)
    let config = BatchConfig {
        window_ms: 50,
        min_batch: 100,    // Min is highest!
        optimal_batch: 50, // Optimal in middle
        max_batch: 10,     // Max is lowest!
        queue_size: 1024,
        gpu_threshold: 32,
    };

    // Should still work, just with confusing semantics
    assert!(config.should_process(50));
    assert!(config.meets_minimum(100));
}

#[test]
fn test_batch_config_zero_window() {
    let config = BatchConfig {
        window_ms: 0, // Zero window - process immediately
        min_batch: 4,
        optimal_batch: 32,
        max_batch: 64,
        queue_size: 1024,
        gpu_threshold: 32,
    };

    // Zero window should be valid
    assert!(config.meets_minimum(4));
}

// ============================================================================
// ContinuousBatchResponse Tests (requires gpu feature)
// ============================================================================

#[cfg(feature = "gpu")]
#[tokio::test]
async fn test_continuous_batch_response_single_path() {
    let response = ContinuousBatchResponse::single(vec![1, 2, 3], 1, 10.0);
    assert!(!response.batched);
    assert_eq!(response.batch_size, 1);
    assert_eq!(response.token_ids, vec![1, 2, 3]);
}

#[cfg(feature = "gpu")]
#[tokio::test]
async fn test_continuous_batch_response_batched_path() {
    let response = ContinuousBatchResponse::batched(vec![1, 2, 3, 4, 5], 2, 8, 5.0);
    assert!(response.batched);
    assert_eq!(response.batch_size, 8);
    assert_eq!(response.prompt_len, 2);
}

#[cfg(feature = "gpu")]
#[tokio::test]
async fn test_continuous_batch_response_empty_tokens() {
    let response = ContinuousBatchResponse::single(vec![], 0, 0.0);
    assert!(response.token_ids.is_empty());
    assert!(!response.batched);
    assert_eq!(response.batch_size, 1);
}

#[cfg(feature = "gpu")]
#[tokio::test]
async fn test_continuous_batch_response_huge_batch_size() {
    let response = ContinuousBatchResponse::batched(
        vec![1, 2, 3],
        1,
        1_000_000, // Million-request batch
        0.001,     // Very fast
    );

    assert!(response.batched);
    assert_eq!(response.batch_size, 1_000_000);
}

#[cfg(feature = "gpu")]
#[tokio::test]
async fn test_continuous_batch_response_infinite_latency() {
    let response = ContinuousBatchResponse::batched(vec![1], 1, 8, f64::INFINITY);
    assert!(response.latency_ms.is_infinite());
}

#[cfg(feature = "gpu")]
#[tokio::test]
async fn test_continuous_batch_response_nan_latency() {
    let response = ContinuousBatchResponse::batched(vec![1], 1, 8, f64::NAN);
    assert!(response.latency_ms.is_nan());
}

// ============================================================================
// GpuBatchRequest Chaos Tests
// ============================================================================

#[test]
fn test_gpu_batch_request_empty_prompts() {
    let request = GpuBatchRequest {
        prompts: vec![],
        max_tokens: 100,
        temperature: 1.0,
        top_k: 40,
        stop: vec![],
    };

    assert!(request.prompts.is_empty());
}

#[test]
fn test_gpu_batch_request_single_empty_prompt() {
    let request = GpuBatchRequest {
        prompts: vec![String::new()],
        max_tokens: 100,
        temperature: 1.0,
        top_k: 40,
        stop: vec![],
    };

    assert_eq!(request.prompts.len(), 1);
    assert!(request.prompts[0].is_empty());
}

#[test]
fn test_gpu_batch_request_huge_prompts() {
    // 1000 prompts
    let prompts: Vec<String> = (0..1000).map(|i| format!("Prompt {}", i)).collect();

    let request = GpuBatchRequest {
        prompts,
        max_tokens: 1,
        temperature: 0.0,
        top_k: 1,
        stop: vec![],
    };

    assert_eq!(request.prompts.len(), 1000);
}

#[test]
fn test_gpu_batch_request_long_prompt() {
    // Single very long prompt
    let long_prompt = "x".repeat(1_000_000); // 1MB prompt

    let request = GpuBatchRequest {
        prompts: vec![long_prompt.clone()],
        max_tokens: 1,
        temperature: 0.0,
        top_k: 1,
        stop: vec![],
    };

    assert_eq!(request.prompts[0].len(), 1_000_000);
}

#[test]
fn test_gpu_batch_request_zero_max_tokens() {
    let request = GpuBatchRequest {
        prompts: vec!["test".to_string()],
        max_tokens: 0, // Zero tokens requested
        temperature: 0.0,
        top_k: 1,
        stop: vec![],
    };

    assert_eq!(request.max_tokens, 0);
}

#[test]
fn test_gpu_batch_request_huge_max_tokens() {
    let request = GpuBatchRequest {
        prompts: vec!["test".to_string()],
        max_tokens: usize::MAX, // Ridiculous token count
        temperature: 0.0,
        top_k: 1,
        stop: vec![],
    };

    assert_eq!(request.max_tokens, usize::MAX);
}

#[test]
fn test_gpu_batch_request_extreme_temperature() {
    // Temperature = 0 (deterministic)
    let request_zero = GpuBatchRequest {
        prompts: vec!["test".to_string()],
        max_tokens: 10,
        temperature: 0.0,
        top_k: 1,
        stop: vec![],
    };
    assert_eq!(request_zero.temperature, 0.0);

    // Temperature = infinity (maximum randomness)
    let request_inf = GpuBatchRequest {
        prompts: vec!["test".to_string()],
        max_tokens: 10,
        temperature: f32::INFINITY,
        top_k: 1,
        stop: vec![],
    };
    assert!(request_inf.temperature.is_infinite());
}

#[test]
fn test_gpu_batch_request_extreme_top_k() {
    let request = GpuBatchRequest {
        prompts: vec!["test".to_string()],
        max_tokens: 10,
        temperature: 0.0,
        top_k: 0, // Top-k of 0
        stop: vec![],
    };
    assert_eq!(request.top_k, 0);

    let request_huge = GpuBatchRequest {
        prompts: vec!["test".to_string()],
        max_tokens: 10,
        temperature: 0.0,
        top_k: 1_000_000, // Huge top-k
        stop: vec![],
    };
    assert_eq!(request_huge.top_k, 1_000_000);
}

#[test]
fn test_gpu_batch_request_with_stop_tokens() {
    let request = GpuBatchRequest {
        prompts: vec!["test".to_string()],
        max_tokens: 100,
        temperature: 0.7,
        top_k: 40,
        stop: vec!["END".to_string(), "\n".to_string(), "STOP".to_string()],
    };

    assert_eq!(request.stop.len(), 3);
}

// ============================================================================
// GpuBatchResponse Chaos Tests
// ============================================================================

#[test]
fn test_gpu_batch_response_empty_results() {
    let response = GpuBatchResponse {
        results: vec![],
        stats: GpuBatchStats {
            batch_size: 0,
            gpu_used: false,
            total_tokens: 0,
            processing_time_ms: 0.0,
            throughput_tps: 0.0,
        },
    };

    assert!(response.results.is_empty());
    assert_eq!(response.stats.batch_size, 0);
}

#[test]
fn test_gpu_batch_response_mismatched_stats() {
    // Stats don't match results
    let response = GpuBatchResponse {
        results: vec![GpuBatchResult {
            index: 0,
            token_ids: vec![1, 2, 3],
            text: "test".to_string(),
            num_generated: 3,
        }],
        stats: GpuBatchStats {
            batch_size: 100, // Claims 100 but only 1 result
            gpu_used: true,
            total_tokens: 1000,
            processing_time_ms: 1.0,
            throughput_tps: 1000.0,
        },
    };

    assert_eq!(response.results.len(), 1);
    assert_eq!(response.stats.batch_size, 100); // Mismatch is possible
}

// ============================================================================
// GpuBatchResult Chaos Tests
// ============================================================================

#[test]
fn test_gpu_batch_result_empty_tokens() {
    let result = GpuBatchResult {
        index: 0,
        token_ids: vec![],
        text: String::new(),
        num_generated: 0,
    };

    assert!(result.token_ids.is_empty());
    assert_eq!(result.num_generated, 0);
}

#[test]
fn test_gpu_batch_result_huge_token_list() {
    let token_ids: Vec<u32> = (0..10000).collect();

    let result = GpuBatchResult {
        index: 0,
        token_ids,
        text: "lots of tokens".to_string(),
        num_generated: 10000,
    };

    assert_eq!(result.token_ids.len(), 10000);
}

#[test]
fn test_gpu_batch_result_unicode_text() {
    let result = GpuBatchResult {
        index: 0,
        token_ids: vec![1, 2, 3, 4, 5],
        text: "日本語 🎉🎊🎈 \u{0000}\u{FFFF}".to_string(),
        num_generated: 5,
    };

    assert!(!result.text.is_empty());
}

#[test]
fn test_gpu_batch_result_large_index() {
    let result = GpuBatchResult {
        index: usize::MAX,
        token_ids: vec![1],
        text: "max index".to_string(),
        num_generated: 1,
    };

    assert_eq!(result.index, usize::MAX);
}

// ============================================================================
// GpuBatchStats Chaos Tests
// ============================================================================

#[test]
fn test_gpu_batch_stats_zero_throughput() {
    let stats = GpuBatchStats {
        batch_size: 1,
        gpu_used: false,
        total_tokens: 0,
        processing_time_ms: 1000.0, // 1 second but 0 tokens
        throughput_tps: 0.0,
    };

    assert_eq!(stats.throughput_tps, 0.0);
}

#[test]
fn test_gpu_batch_stats_infinite_throughput() {
    let stats = GpuBatchStats {
        batch_size: 1,
        gpu_used: true,
        total_tokens: 1000,
        processing_time_ms: 0.0, // Instant
        throughput_tps: f64::INFINITY,
    };

    assert!(stats.throughput_tps.is_infinite());
}

#[test]
fn test_gpu_batch_stats_nan_values() {
    let stats = GpuBatchStats {
        batch_size: 1,
        gpu_used: false,
        total_tokens: 0,
        processing_time_ms: f64::NAN,
        throughput_tps: f64::NAN,
    };

    assert!(stats.throughput_tps.is_nan());
    assert!(stats.processing_time_ms.is_nan());
}

include!("gpu_batch_02.rs");