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

/// M31: Bulkhead Pattern (IMP-078)
/// Target: Separate pools, prevent starvation, configurable sizes
#[test]
#[cfg(feature = "gpu")]
fn test_imp_078_bulkhead_pattern() {
    use crate::gpu::{BulkheadConfig, BulkheadManager, RequestType};

    // Test 1: Create bulkhead manager with config
    let config = BulkheadConfig::new()
        .with_pool("inference", 10)
        .with_pool("embedding", 5)
        .with_pool("batch", 2);
    let manager = BulkheadManager::new(&config);

    // Test 2: Acquire from specific pool
    let permit = manager.acquire(RequestType::Inference);
    assert!(
        permit.is_ok(),
        "IMP-078: Should acquire from inference pool"
    );
    assert_eq!(
        manager.available(RequestType::Inference),
        9,
        "IMP-078: Should decrement available"
    );

    // Test 3: Pools are isolated
    let embed_permit = manager.acquire(RequestType::Embedding);
    assert!(
        embed_permit.is_ok(),
        "IMP-078: Should acquire from embedding pool"
    );
    assert_eq!(
        manager.available(RequestType::Inference),
        9,
        "IMP-078: Inference should be unchanged"
    );
    assert_eq!(
        manager.available(RequestType::Embedding),
        4,
        "IMP-078: Embedding should decrement"
    );

    // Test 4: Pool exhaustion doesn't affect others
    for _ in 0..2 {
        let _ = manager.acquire(RequestType::Batch);
    }
    let batch_overflow = manager.try_acquire(RequestType::Batch);
    assert!(
        batch_overflow.is_err(),
        "IMP-078: Batch pool should be exhausted"
    );
    assert_eq!(
        manager.available(RequestType::Inference),
        9,
        "IMP-078: Inference still available"
    );

    // Test 5: Release returns to correct pool
    manager.release(&permit.expect("test"));
    assert_eq!(
        manager.available(RequestType::Inference),
        10,
        "IMP-078: Should release to correct pool"
    );

    // Test 6: Get pool stats
    let stats = manager.stats();
    assert_eq!(stats.pool_count, 3, "IMP-078: Should have 3 pools");
    assert!(
        stats.total_capacity >= 17,
        "IMP-078: Total capacity should sum pools"
    );
}

// ========================================================================
// M32: Production Logging & Diagnostics (IMP-079, IMP-080, IMP-081)
// ========================================================================

/// M32: Structured Logging (IMP-079)
/// Target: JSON-formatted logs, correlation IDs, configurable levels
#[test]
#[cfg(feature = "gpu")]
fn test_imp_079_structured_logging() {
    use crate::gpu::{LogConfig, LogEntry, LogLevel, Logger};

    // Test 1: Create logger with config
    let config = LogConfig::new()
        .with_level(LogLevel::Debug)
        .with_json_format(true)
        .with_module_level("gpu", LogLevel::Trace);
    let logger = Logger::new(config);

    // Test 2: Create log entry with structured data
    let entry = LogEntry::new(LogLevel::Info, "Request started")
        .with_correlation_id("req-12345")
        .with_field("model", "llama-7b")
        .with_field("tokens", "128");
    assert_eq!(
        entry.correlation_id(),
        Some("req-12345"),
        "IMP-079: Should have correlation ID"
    );
    assert_eq!(entry.level(), LogLevel::Info, "IMP-079: Should have level");

    // Test 3: JSON formatting
    let json = entry.to_json();
    assert!(
        json.contains("\"level\":\"INFO\""),
        "IMP-079: JSON should have level"
    );
    assert!(
        json.contains("\"correlation_id\":\"req-12345\""),
        "IMP-079: JSON should have correlation ID"
    );
    assert!(
        json.contains("\"model\":\"llama-7b\""),
        "IMP-079: JSON should have custom fields"
    );

    // Test 4: Module-specific log levels
    assert!(
        logger.is_enabled(LogLevel::Trace, "gpu"),
        "IMP-079: gpu should allow Trace"
    );
    assert!(
        logger.is_enabled(LogLevel::Debug, "inference"),
        "IMP-079: Other modules use default"
    );
    assert!(
        !logger.is_enabled(LogLevel::Trace, "inference"),
        "IMP-079: Trace should be filtered for non-gpu"
    );

    // Test 5: Log with automatic timestamp
    let entry = LogEntry::new(LogLevel::Warn, "High memory usage");
    assert!(entry.timestamp() > 0, "IMP-079: Should have timestamp");
}

/// M32: Performance Diagnostics (IMP-080)
/// Target: Latency breakdown, memory tracking, GPU timing
#[test]
#[cfg(feature = "gpu")]
fn test_imp_080_performance_diagnostics() {
    use crate::gpu::{DiagnosticsCollector, MemoryTracker, PhaseTimer};

    // Test 1: Create diagnostics collector
    let collector = DiagnosticsCollector::new();

    // Test 2: Track request phases
    // Use a tight busy-wait floor so elapsed time is bounded below by a known
    // minimum even under CI scheduler jitter. `std::thread::sleep` can both
    // overshoot and undershoot under load, which made the prior
    // `inference > tokenization` ordering assertion flaky on shared runners.
    const TOKEN_FLOOR_US: u64 = 5_000;
    const INFER_FLOOR_US: u64 = 10_000;
    let timer = PhaseTimer::new();
    timer.start_phase("tokenization");
    let t0 = std::time::Instant::now();
    while t0.elapsed().as_micros() < u128::from(TOKEN_FLOOR_US) {
        std::hint::spin_loop();
    }
    timer.end_phase("tokenization");
    timer.start_phase("inference");
    let t1 = std::time::Instant::now();
    while t1.elapsed().as_micros() < u128::from(INFER_FLOOR_US) {
        std::hint::spin_loop();
    }
    timer.end_phase("inference");

    let breakdown = timer.breakdown();
    assert!(
        breakdown.contains_key("tokenization"),
        "IMP-080: Should track tokenization"
    );
    assert!(
        breakdown.contains_key("inference"),
        "IMP-080: Should track inference"
    );
    // Each phase recorded a nonzero duration that respects its busy-wait floor.
    // We deliberately do NOT assert inference > tokenization — that ordering is
    // scheduler-dependent under CI load and caused a real flake (imp_078.rs).
    let tok = *breakdown.get("tokenization").expect("test");
    let inf = *breakdown.get("inference").expect("test");
    assert!(tok >= TOKEN_FLOOR_US, "IMP-080: tokenization {tok}µs < {TOKEN_FLOOR_US}µs floor");
    assert!(inf >= INFER_FLOOR_US, "IMP-080: inference {inf}µs < {INFER_FLOOR_US}µs floor");

    // Test 3: Memory allocation tracking
    let tracker = MemoryTracker::new();
    tracker.record_allocation("model_weights", 1024 * 1024 * 1024);
    tracker.record_allocation("kv_cache", 256 * 1024 * 1024);
    tracker.record_deallocation("kv_cache", 256 * 1024 * 1024);

    let report = tracker.report();
    assert_eq!(
        report.peak_bytes,
        1024 * 1024 * 1024 + 256 * 1024 * 1024,
        "IMP-080: Should track peak"
    );
    assert_eq!(
        report.current_bytes,
        1024 * 1024 * 1024,
        "IMP-080: Should track current"
    );
    assert_eq!(
        report.allocation_count, 2,
        "IMP-080: Should count allocations"
    );

    // Test 4: Report to collector
    collector.record_request_timing("req-001", timer.breakdown());
    collector.record_memory_snapshot(report);
    let summary = collector.summary();
    assert!(summary.request_count >= 1, "IMP-080: Should count requests");
}

/// M32: Debug Mode (IMP-081)
/// Target: Verbose logging, request replay, state dump
#[test]
#[cfg(feature = "gpu")]
fn test_imp_081_debug_mode() {
    use crate::gpu::{DebugMode, RequestCapture, StateDump};

    // Test 1: Enable debug mode
    let debug = DebugMode::new();
    assert!(
        !debug.is_enabled(),
        "IMP-081: Should be disabled by default"
    );
    debug.enable();
    assert!(debug.is_enabled(), "IMP-081: Should enable");

    // Test 2: Capture request for replay
    let capture = RequestCapture::new()
        .with_input("Hello, world!")
        .with_params("temperature", "0.7")
        .with_params("max_tokens", "100");
    assert_eq!(
        capture.input(),
        "Hello, world!",
        "IMP-081: Should capture input"
    );
    assert_eq!(capture.params().len(), 2, "IMP-081: Should capture params");

    // Test 3: Serialize/deserialize for replay
    let json = capture.to_json();
    let restored = RequestCapture::from_json(&json);
    assert!(restored.is_ok(), "IMP-081: Should deserialize");
    assert_eq!(
        restored.expect("test").input(),
        "Hello, world!",
        "IMP-081: Should restore input"
    );

    // Test 4: State dump on error
    let dump = StateDump::new()
        .with_error("Out of memory")
        .with_stack_trace("at inference::generate\nat main")
        .with_state("model_loaded", "true")
        .with_state("tokens_processed", "42");
    assert_eq!(
        dump.error(),
        "Out of memory",
        "IMP-081: Should capture error"
    );
    assert!(
        dump.stack_trace().contains("inference::generate"),
        "IMP-081: Should have stack"
    );
    assert_eq!(dump.state().len(), 2, "IMP-081: Should capture state");

    // Test 5: Dump to file (mock)
    let dump_json = dump.to_json();
    assert!(
        dump_json.contains("Out of memory"),
        "IMP-081: JSON should have error"
    );
    assert!(
        dump_json.contains("tokens_processed"),
        "IMP-081: JSON should have state"
    );
}

// =========================================================================
// M33: GGUF HTTP Serving Integration Tests
// Per spec v2.15.0: Wire GpuModel to HTTP server
// =========================================================================

/// M33: GgufModelState (IMP-082)
/// Target: App state that holds a loaded GGUF model for HTTP serving
#[test]
#[cfg(feature = "gpu")]
fn test_imp_082_gguf_model_state() {
    use crate::gpu::GgufModelState;

    // Test 1: Create empty state
    let state = GgufModelState::new();
    assert!(!state.is_loaded(), "IMP-082: Should be unloaded initially");

    // Test 2: State reports model info
    assert_eq!(
        state.model_name(),
        None,
        "IMP-082: No model name when empty"
    );
    assert_eq!(state.vocab_size(), 0, "IMP-082: Zero vocab when empty");

    // Test 3: Ready check
    assert!(!state.is_ready(), "IMP-082: Not ready when empty");
}

/// M33: Load GGUF to GPU (IMP-083)
/// Target: Pipeline from GGUF file to GpuModel ready for inference
#[test]
#[cfg(feature = "gpu")]
fn test_imp_083_load_gguf_to_gpu() {
    use crate::gpu::load_gguf_to_gpu;

    // Test with test GGUF data (minimal model)
    let vocab_size = 256;
    let hidden_dim = 64;
    let num_layers = 2;

    // Create minimal test GGUF-like config
    let result = load_gguf_to_gpu(vocab_size, hidden_dim, num_layers);

    // This should work - creates a minimal GPU model
    assert!(result.is_ok(), "IMP-083: Should load test model to GPU");

    let state = result.expect("test");
    assert!(state.is_loaded(), "IMP-083: Should be loaded after load");
    assert!(state.is_ready(), "IMP-083: Should be ready for inference");
    assert_eq!(
        state.vocab_size(),
        vocab_size,
        "IMP-083: Should have correct vocab"
    );
}

/// M33: Serve GGUF Model (IMP-084)
/// Target: HTTP server with loaded GGUF model (integration test)
///
/// Verifies that a GGUF model can be served via HTTP.
/// Run with: `cargo test test_imp_084 --ignored --features gpu`
#[test]
#[ignore = "Requires integration test setup"]
fn test_imp_084_serve_gguf_model() {
    // IMP-084: Integration test for serve_gguf_model
    //
    // This test verifies the HTTP serving infrastructure is correct.
    // It uses a demo model since real GGUF files may not be available.

    // Check if realizar server is running on default port
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(5))
        .build()
        .expect("Failed to create HTTP client");

    let health_url = "http://127.0.0.1:3000/health";
    match client.get(health_url).send() {
        Ok(response) => {
            assert!(
                response.status().is_success(),
                "IMP-084: Health endpoint should return 200 OK"
            );
            println!("IMP-084: ✅ Server health check passed");

            // Test generate endpoint with demo model
            let gen_url = "http://127.0.0.1:3000/generate";
            let request = serde_json::json!({
                "prompt": "Hello",
                "max_tokens": 5,
                "temperature": 0.0
            });

            match client.post(gen_url).json(&request).send() {
                Ok(gen_response) => {
                    assert!(
                        gen_response.status().is_success(),
                        "IMP-084: Generate endpoint should return 200 OK"
                    );
                    let body: serde_json::Value = gen_response.json().expect("Valid JSON");
                    assert!(
                        body.get("text").is_some(),
                        "IMP-084: Response should have text"
                    );
                    println!("IMP-084: ✅ Generate endpoint works, got: {:?}", body);
                },
                Err(e) => {
                    println!("IMP-084: ⚠️ Generate endpoint not available: {}", e);
                },
            }
        },
        Err(e) => {
            panic!(
                "IMP-084: Server not running at {}. Start with: cargo run --example api_server. Error: {}",
                health_url, e
            );
        },
    }
}

/// M33: OpenAI Completions Endpoint (IMP-085)
/// Target: /v1/completions returns generated text
///
/// Tests OpenAI-compatible completions API.
/// Run with: `cargo test test_imp_085 --ignored`
#[test]
#[ignore = "Requires running server"]
fn test_imp_085_completions_endpoint() {
    // IMP-085: Integration test for /v1/completions (OpenAI-compatible)

    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(10))
        .build()
        .expect("Failed to create HTTP client");

    let url = "http://127.0.0.1:3000/v1/completions";

    // OpenAI-style request format
    let request = serde_json::json!({
        "model": "demo",
        "prompt": "The capital of France is",
        "max_tokens": 10,
        "temperature": 0.0
    });

    match client.post(url).json(&request).send() {
        Ok(response) => {
            if response.status().is_success() {
                let body: serde_json::Value = response.json().expect("Valid JSON");
                assert!(
                    body.get("choices").is_some(),
                    "IMP-085: Response should have 'choices'"
                );
                println!("IMP-085: ✅ OpenAI completions endpoint works");
            } else if response.status().as_u16() == 404 {
                println!("IMP-085: ⚠️ /v1/completions not implemented yet (404)");
            } else {
                panic!("IMP-085: Unexpected status: {}", response.status());
            }
        },
        Err(e) => {
            panic!(
                "IMP-085: Server not running. Start with: cargo run --example api_server. Error: {}",
                e
            );
        },
    }
}