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
//! API Tests Part 19: T-COV-95 Deep Coverage Bridge
//!
//! Covers additional uncovered paths in:
//! - BatchConfig: low_latency, high_throughput, should_process, meets_minimum
//! - ContinuousBatchResponse: single, batched, generated_tokens edge cases
//! - ChatCompletionChunk, ChatChunkChoice, ChatDelta serde
//! - ChatChoice, OpenAIModelsResponse, OpenAIModel serde
//! - TraceData/TraceOperation serde
//! - Additional HTTP endpoints: /metrics, /models, /realize/generate, /realize/batch,
//!   /stream/generate, /v1/gpu/warmup, /v1/gpu/status, /v1/predict, /v1/explain
//! - format_chat_messages: model-specific formatting (qwen, phi, tinyllama)
//! - ContextWindowManager: truncation required, large message sets
//! - build_trace_data: deeper field verification
//!
//! Refs PMAT-802: Protocol T-COV-95

use axum::{
    body::Body,
    http::{Request, StatusCode},
};
use tower::util::ServiceExt;

use crate::api::test_helpers::create_test_app_shared;

// ============================================================================
// BatchConfig methods
// ============================================================================

#[test]
fn test_batch_config_low_latency() {
    use crate::api::gpu_handlers::BatchConfig;

    let config = BatchConfig::low_latency();
    assert!(config.window_ms <= 10); // Low latency = short window
    assert!(config.min_batch > 0);
    assert!(config.optimal_batch > 0);
    assert!(config.max_batch >= config.optimal_batch);
    assert!(config.queue_size > 0);
}

#[test]
fn test_batch_config_high_throughput() {
    use crate::api::gpu_handlers::BatchConfig;

    let config = BatchConfig::high_throughput();
    assert!(config.window_ms >= 50); // High throughput = longer window
    assert!(config.min_batch >= 4);
    assert!(config.max_batch >= 64);
    assert!(config.queue_size >= 1024);
}

#[test]
fn test_batch_config_should_process_at_optimal() {
    use crate::api::gpu_handlers::BatchConfig;

    let config = BatchConfig::low_latency();
    // At optimal batch size → should process
    assert!(config.should_process(config.optimal_batch));
    // Above optimal → should process
    assert!(config.should_process(config.optimal_batch + 1));
    // Below optimal → should not process
    assert!(!config.should_process(config.optimal_batch - 1));
}

#[test]
fn test_batch_config_should_process_zero() {
    use crate::api::gpu_handlers::BatchConfig;

    let config = BatchConfig::low_latency();
    assert!(!config.should_process(0));
}

#[test]
fn test_batch_config_meets_minimum() {
    use crate::api::gpu_handlers::BatchConfig;

    let config = BatchConfig::low_latency();
    assert!(config.meets_minimum(config.min_batch));
    assert!(config.meets_minimum(config.min_batch + 1));
    assert!(!config.meets_minimum(0));
    assert!(!config.meets_minimum(config.min_batch - 1));
}

#[test]
fn test_batch_config_meets_minimum_high_throughput() {
    use crate::api::gpu_handlers::BatchConfig;

    let config = BatchConfig::high_throughput();
    assert!(config.meets_minimum(config.min_batch));
    assert!(!config.meets_minimum(1));
}

// ============================================================================
// ContinuousBatchResponse
// ============================================================================

#[test]
fn test_continuous_batch_response_single() {
    use crate::api::gpu_handlers::ContinuousBatchResponse;

    let resp = ContinuousBatchResponse::single(vec![1, 2, 3, 4, 5], 2, 5.0);
    assert!(!resp.batched);
    assert_eq!(resp.batch_size, 1);
    assert_eq!(resp.prompt_len, 2);
    assert_eq!(resp.token_ids, vec![1, 2, 3, 4, 5]);
    assert!((resp.latency_ms - 5.0).abs() < 1e-6);
}

#[test]
fn test_continuous_batch_response_batched() {
    use crate::api::gpu_handlers::ContinuousBatchResponse;

    let resp = ContinuousBatchResponse::batched(vec![1, 2, 3, 4, 5], 2, 8, 10.0);
    assert!(resp.batched);
    assert_eq!(resp.batch_size, 8);
    assert_eq!(resp.prompt_len, 2);
}

#[test]
fn test_continuous_batch_response_generated_tokens() {
    use crate::api::gpu_handlers::ContinuousBatchResponse;

    let resp = ContinuousBatchResponse::single(vec![10, 20, 30, 40, 50], 3, 1.0);
    let generated = resp.generated_tokens();
    assert_eq!(generated, &[40, 50]);
}

#[test]
fn test_continuous_batch_response_generated_tokens_empty() {
    use crate::api::gpu_handlers::ContinuousBatchResponse;

    // prompt_len equals total tokens → no generated tokens
    let resp = ContinuousBatchResponse::single(vec![1, 2, 3], 3, 1.0);
    let generated = resp.generated_tokens();
    assert!(generated.is_empty());
}

#[test]
fn test_continuous_batch_response_generated_tokens_all_generated() {
    use crate::api::gpu_handlers::ContinuousBatchResponse;

    // prompt_len = 0 → all tokens are generated
    let resp = ContinuousBatchResponse::single(vec![1, 2, 3], 0, 1.0);
    let generated = resp.generated_tokens();
    assert_eq!(generated, &[1, 2, 3]);
}

#[test]
fn test_continuous_batch_response_generated_tokens_prompt_exceeds() {
    use crate::api::gpu_handlers::ContinuousBatchResponse;

    // prompt_len > total tokens → return empty (edge case)
    let resp = ContinuousBatchResponse::single(vec![1, 2], 10, 1.0);
    let generated = resp.generated_tokens();
    assert!(generated.is_empty());
}

// ============================================================================
// ChatCompletionChunk, ChatChunkChoice, ChatDelta serde
// ============================================================================

#[test]
fn test_chat_completion_chunk_serde() {
    let chunk = crate::api::ChatCompletionChunk {
        id: "chatcmpl-123".to_string(),
        object: "chat.completion.chunk".to_string(),
        created: 1700000000,
        model: "test-model".to_string(),
        choices: vec![crate::api::ChatChunkChoice {
            index: 0,
            delta: crate::api::ChatDelta {
                role: Some("assistant".to_string()),
                content: None,
            },
            finish_reason: None,
        }],
    };
    let json = serde_json::to_string(&chunk).expect("JSON serialization failed");
    let deserialized: crate::api::ChatCompletionChunk = serde_json::from_str(&json).expect("JSON deserialization failed");
    assert_eq!(deserialized.id, "chatcmpl-123");
    assert_eq!(deserialized.object, "chat.completion.chunk");
    assert_eq!(deserialized.choices.len(), 1);
    assert_eq!(
        deserialized.choices[0].delta.role,
        Some("assistant".to_string())
    );
    assert!(deserialized.choices[0].delta.content.is_none());
    assert!(deserialized.choices[0].finish_reason.is_none());
}

#[test]
fn test_chat_delta_with_content() {
    let delta = crate::api::ChatDelta {
        role: None,
        content: Some("Hello ".to_string()),
    };
    let json = serde_json::to_string(&delta).expect("JSON serialization failed");
    // role is None → should be skipped in serialization
    assert!(!json.contains("role"));
    let deserialized: crate::api::ChatDelta = serde_json::from_str(&json).expect("JSON deserialization failed");
    assert!(deserialized.role.is_none());
    assert_eq!(deserialized.content, Some("Hello ".to_string()));
}

#[test]
fn test_chat_chunk_choice_with_finish_reason() {
    let choice = crate::api::ChatChunkChoice {
        index: 0,
        delta: crate::api::ChatDelta {
            role: None,
            content: None,
        },
        finish_reason: Some("stop".to_string()),
    };
    let json = serde_json::to_string(&choice).expect("JSON serialization failed");
    let deserialized: crate::api::ChatChunkChoice = serde_json::from_str(&json).expect("JSON deserialization failed");
    assert_eq!(deserialized.finish_reason, Some("stop".to_string()));
}

// ============================================================================
// ChatChoice, OpenAIModelsResponse, OpenAIModel serde
// ============================================================================

#[test]
fn test_chat_choice_serde() {
    let choice = crate::api::ChatChoice {
        index: 0,
        message: crate::api::ChatMessage {
            role: "assistant".to_string(),
            content: "Hello!".to_string(),
            name: None,
        
            ..Default::default()
        },
        finish_reason: "stop".to_string(),
    };
    let json = serde_json::to_string(&choice).expect("JSON serialization failed");
    let deserialized: crate::api::ChatChoice = serde_json::from_str(&json).expect("JSON deserialization failed");
    assert_eq!(deserialized.index, 0);
    assert_eq!(deserialized.message.role, "assistant");
    assert_eq!(deserialized.finish_reason, "stop");
}

#[test]
fn test_openai_models_response_serde() {
    let resp = crate::api::OpenAIModelsResponse {
        object: "list".to_string(),
        data: vec![crate::api::OpenAIModel {
            id: "test-model".to_string(),
            object: "model".to_string(),
            created: 1700000000,
            owned_by: "realizar".to_string(),
        }],
    };
    let json = serde_json::to_string(&resp).expect("JSON serialization failed");
    let deserialized: crate::api::OpenAIModelsResponse = serde_json::from_str(&json).expect("JSON deserialization failed");
    assert_eq!(deserialized.object, "list");
    assert_eq!(deserialized.data.len(), 1);
    assert_eq!(deserialized.data[0].id, "test-model");
    assert_eq!(deserialized.data[0].owned_by, "realizar");
}

#[test]
fn test_openai_model_serde() {
    let model = crate::api::OpenAIModel {
        id: "tinyllama-1.1b".to_string(),
        object: "model".to_string(),
        created: 1700000000,
        owned_by: "realizar".to_string(),
    };
    let json = serde_json::to_string(&model).expect("JSON serialization failed");
    let deserialized: crate::api::OpenAIModel = serde_json::from_str(&json).expect("JSON deserialization failed");
    assert_eq!(deserialized.id, "tinyllama-1.1b");
}

// ============================================================================
// TraceData/TraceOperation serde
// ============================================================================

#[test]
fn test_trace_data_serde() {
    let trace = crate::api::TraceData {
        level: "brick".to_string(),
        operations: 10,
        total_time_us: 5000,
        breakdown: vec![
            crate::api::TraceOperation {
                name: "embedding_lookup".to_string(),
                time_us: 500,
                details: Some("10 tokens".to_string()),
            },
            crate::api::TraceOperation {
                name: "matmul_qkv".to_string(),
                time_us: 1667,
                details: None,
            },
        ],
        provenance: crate::api::TraceProvenance::Estimated,
    };
    let json = serde_json::to_string(&trace).expect("JSON serialization failed");
    let deserialized: crate::api::TraceData = serde_json::from_str(&json).expect("JSON deserialization failed");
    assert_eq!(deserialized.level, "brick");
    assert_eq!(deserialized.operations, 10);
    assert_eq!(deserialized.breakdown.len(), 2);
}

#[test]
fn test_trace_operation_serde() {
    let op = crate::api::TraceOperation {
        name: "softmax".to_string(),
        time_us: 100,
        details: None,
    };
    let json = serde_json::to_string(&op).expect("JSON serialization failed");
    let deserialized: crate::api::TraceOperation = serde_json::from_str(&json).expect("JSON deserialization failed");
    assert_eq!(deserialized.name, "softmax");
    assert!(deserialized.details.is_none());
}

// ============================================================================
// build_trace_data: deeper field verification
// ============================================================================

#[test]
fn test_build_trace_data_brick_breakdown_fields() {
    let (brick, _, _) = crate::api::build_trace_data(Some("brick"), 1000, 20, 10, 4);
    let b = brick.expect("test value should be present");
    assert_eq!(b.operations, 10); // completion_tokens
    assert_eq!(b.total_time_us, 1000);
    assert_eq!(b.breakdown.len(), 1);
    assert_eq!(b.breakdown[0].name, "total_inference");
    assert_eq!(b.breakdown[0].time_us, 1000);
    let details = b.breakdown[0].details.as_ref().expect("details present");
    assert!(details.contains("20 prompt"));
    assert!(details.contains("10 completion"));
    assert!(details.contains("apr profile"));
}

#[test]
fn test_build_trace_data_step_breakdown_fields() {
    let (_, step, _) = crate::api::build_trace_data(Some("step"), 2000, 15, 8, 6);
    let s = step.expect("test value should be present");
    assert_eq!(s.operations, 8); // completion_tokens
    assert_eq!(s.total_time_us, 2000);
    assert_eq!(s.breakdown.len(), 1);
    assert_eq!(s.breakdown[0].name, "total_inference");
    assert_eq!(s.breakdown[0].time_us, 2000);
    let details = s.breakdown[0].details.as_ref().expect("details present");
    assert!(details.contains("15 prompt"));
    assert!(details.contains("8 completion"));
    assert!(details.contains("apr profile"));
}

#[test]
fn test_build_trace_data_layer_breakdown_fields() {
    let (_, _, layer) = crate::api::build_trace_data(Some("layer"), 4000, 10, 5, 4);
    let l = layer.expect("test value should be present");
    assert_eq!(l.operations, 4); // num_layers
    assert_eq!(l.total_time_us, 4000);
    assert_eq!(l.breakdown.len(), 1);
    assert_eq!(l.breakdown[0].name, "total_inference");
    assert_eq!(l.breakdown[0].time_us, 4000);
    let details = l.breakdown[0].details.as_ref().expect("details present");
    assert!(details.contains("4 layers"));
    assert!(details.contains("apr profile"));
}

#[test]
fn test_build_trace_data_unknown_level() {
    let (brick, step, layer) = crate::api::build_trace_data(Some("unknown"), 100, 10, 5, 4);
    assert!(brick.is_none());
    assert!(step.is_none());
    assert!(layer.is_none());
}

// ============================================================================
// Additional HTTP endpoint integration tests
// ============================================================================

#[tokio::test]
async fn test_metrics_endpoint() {
    let app = create_test_app_shared();
    let request = Request::builder()
        .method("GET")
        .uri("/metrics")
        .body(Body::empty())
        .expect("test value should be present");

    let response = app.oneshot(request).await.expect("test value should be present");
    assert!(response.status() == StatusCode::OK || response.status() == StatusCode::NOT_FOUND,);
}

#[tokio::test]
async fn test_native_models_endpoint() {
    let app = create_test_app_shared();
    let request = Request::builder()
        .method("GET")
        .uri("/models")
        .body(Body::empty())
        .expect("test value should be present");

    let response = app.oneshot(request).await.expect("test value should be present");
    assert!(response.status() == StatusCode::OK || response.status() == StatusCode::NOT_FOUND,);
}

#[tokio::test]
async fn test_realize_generate_endpoint() {
    let app = create_test_app_shared();
    let request = Request::builder()
        .method("POST")
        .uri("/realize/generate")
        .header("content-type", "application/json")
        .body(Body::from(
            r#"{"prompt":"Hello","max_tokens":5,"temperature":0.0}"#,
        ))
        .expect("test value should be present");

    let response = app.oneshot(request).await.expect("test value should be present");
    // aprender#2376(5): the shared test state has NO model, so this condition is
    // deterministic — a server with no usable model answers 503 on every route.
    // The old assertion accepted a SET of statuses that included the defect, so it
    // could not fail and held the 404-here/500-there split in place.
    assert_eq!(
        response.status(),
        StatusCode::SERVICE_UNAVAILABLE,
        "no model is resident: expected 503"
    );
}

#[tokio::test]
async fn test_realize_batch_endpoint() {
    let app = create_test_app_shared();
    let request = Request::builder()
        .method("POST")
        .uri("/realize/batch")
        .header("content-type", "application/json")
        .body(Body::from(
            r#"{"prompts":["Hello","World"],"max_tokens":5}"#,
        ))
        .expect("test value should be present");

    let response = app.oneshot(request).await.expect("test value should be present");
    // aprender#2376(5): the shared test state has NO model, so this condition is
    // deterministic — a server with no usable model answers 503 on every route.
    // The old assertion accepted a SET of statuses that included the defect, so it
    // could not fail and held the 404-here/500-there split in place.
    assert_eq!(
        response.status(),
        StatusCode::SERVICE_UNAVAILABLE,
        "no model is resident: expected 503"
    );
}

include!("stream_generate.rs");