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
use crate::http_client::*;
// ================================================================================

/// Thread safety verification result
#[derive(Debug)]
pub struct ThreadSafetyResult {
    pub num_threads: usize,
    pub num_requests: usize,
    pub successful_requests: usize,
    pub failed_requests: usize,
    pub data_races_detected: bool,
    pub meets_qa028: bool,
}

impl ThreadSafetyResult {
    pub fn success(threads: usize, requests: usize) -> Self {
        Self {
            num_threads: threads,
            num_requests: requests,
            successful_requests: requests,
            failed_requests: 0,
            data_races_detected: false,
            meets_qa028: true,
        }
    }

    pub fn with_failures(threads: usize, total: usize, failed: usize) -> Self {
        Self {
            num_threads: threads,
            num_requests: total,
            successful_requests: total - failed,
            failed_requests: failed,
            data_races_detected: false,
            meets_qa028: failed == 0,
        }
    }

    pub fn data_race_detected(threads: usize, requests: usize) -> Self {
        Self {
            num_threads: threads,
            num_requests: requests,
            successful_requests: 0,
            failed_requests: requests,
            data_races_detected: true,
            meets_qa028: false,
        }
    }
}

/// Thread-safe request counter for testing
pub struct AtomicRequestCounter {
    pub successful: std::sync::atomic::AtomicUsize,
    pub failed: std::sync::atomic::AtomicUsize,
}

impl AtomicRequestCounter {
    pub fn new() -> Self {
        Self {
            successful: std::sync::atomic::AtomicUsize::new(0),
            failed: std::sync::atomic::AtomicUsize::new(0),
        }
    }

    pub fn record_success(&self) {
        self.successful
            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
    }

    pub fn record_failure(&self) {
        self.failed
            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
    }

    pub fn get_result(&self, threads: usize) -> ThreadSafetyResult {
        let successful = self.successful.load(std::sync::atomic::Ordering::SeqCst);
        let failed = self.failed.load(std::sync::atomic::Ordering::SeqCst);
        ThreadSafetyResult::with_failures(threads, successful + failed, failed)
    }
}

impl Default for AtomicRequestCounter {
    fn default() -> Self {
        Self::new()
    }
}

/// IMP-181a: Test thread safety result structure
#[test]
fn test_imp_181a_thread_safety_result() {
    let success = ThreadSafetyResult::success(4, 100);
    assert!(
        success.meets_qa028,
        "IMP-181a: Successful result should meet QA-028"
    );
    assert_eq!(
        success.successful_requests, 100,
        "IMP-181a: All requests should succeed"
    );

    let with_failures = ThreadSafetyResult::with_failures(4, 100, 5);
    assert!(
        !with_failures.meets_qa028,
        "IMP-181a: Failures should not meet QA-028"
    );
    assert_eq!(
        with_failures.failed_requests, 5,
        "IMP-181a: Should track 5 failures"
    );

    let data_race = ThreadSafetyResult::data_race_detected(4, 100);
    assert!(
        !data_race.meets_qa028,
        "IMP-181a: Data race should not meet QA-028"
    );
    assert!(
        data_race.data_races_detected,
        "IMP-181a: Should detect data race"
    );

    println!("\nIMP-181a: Thread Safety Results:");
    println!(
        "  Success: {}/{} -> meets_qa028={}",
        success.successful_requests, success.num_requests, success.meets_qa028
    );
    println!(
        "  Failures: {}/{} -> meets_qa028={}",
        with_failures.successful_requests, with_failures.num_requests, with_failures.meets_qa028
    );
    println!(
        "  Data race: detected={} -> meets_qa028={}",
        data_race.data_races_detected, data_race.meets_qa028
    );
}

/// IMP-181b: Test atomic request counter
#[test]
fn test_imp_181b_atomic_counter() {
    let counter = AtomicRequestCounter::new();

    // Simulate concurrent access
    for _ in 0..10 {
        counter.record_success();
    }
    for _ in 0..2 {
        counter.record_failure();
    }

    let result = counter.get_result(4);
    assert_eq!(
        result.successful_requests, 10,
        "IMP-181b: Should count 10 successes"
    );
    assert_eq!(
        result.failed_requests, 2,
        "IMP-181b: Should count 2 failures"
    );
    assert!(
        !result.meets_qa028,
        "IMP-181b: Failures should not meet QA-028"
    );

    println!("\nIMP-181b: Atomic Counter:");
    println!("  Successful: {}", result.successful_requests);
    println!("  Failed: {}", result.failed_requests);
    println!(
        "  QA-028: {}",
        if result.meets_qa028 { "PASS" } else { "FAIL" }
    );
}

/// IMP-181c: Test concurrent counter updates
#[test]
fn test_imp_181c_concurrent_updates() {
    use std::sync::Arc;
    use std::thread;

    let counter = Arc::new(AtomicRequestCounter::new());
    let num_threads = 4;
    let ops_per_thread = 100;

    let handles: Vec<_> = (0..num_threads)
        .map(|_| {
            let c = Arc::clone(&counter);
            thread::spawn(move || {
                for _ in 0..ops_per_thread {
                    c.record_success();
                }
            })
        })
        .collect();

    for h in handles {
        h.join().expect("Thread panicked");
    }

    let result = counter.get_result(num_threads);
    assert_eq!(
        result.successful_requests,
        num_threads * ops_per_thread,
        "IMP-181c: Should count all {} operations",
        num_threads * ops_per_thread
    );
    assert!(result.meets_qa028, "IMP-181c: No failures = QA-028 pass");

    println!("\nIMP-181c: Concurrent Updates:");
    println!(
        "  {} threads x {} ops = {} total",
        num_threads, ops_per_thread, result.successful_requests
    );
    println!("  Data races: {}", result.data_races_detected);
    println!(
        "  QA-028: {}",
        if result.meets_qa028 { "PASS" } else { "FAIL" }
    );
}

/// IMP-181d: Real-world concurrent requests
#[test]
#[ignore = "Requires running llama.cpp server on port 8082"]
fn test_imp_181d_realworld_concurrent() {
    use std::sync::Arc;
    use std::thread;

    let counter = Arc::new(AtomicRequestCounter::new());
    let num_threads = 4;

    let handles: Vec<_> = (0..num_threads)
        .map(|i| {
            let c = Arc::clone(&counter);
            thread::spawn(move || {
                let client = ModelHttpClient::with_timeout(30);
                let request = CompletionRequest {
                    model: "default".to_string(),
                    prompt: format!("Thread {}: Say hello", i),
                    max_tokens: 5,
                    temperature: Some(0.0),
                    stream: false,
                };

                match client.llamacpp_completion("http://127.0.0.1:8082", &request) {
                    Ok(_) => c.record_success(),
                    Err(_) => c.record_failure(),
                }
            })
        })
        .collect();

    for h in handles {
        h.join().expect("Thread panicked");
    }

    let result = counter.get_result(num_threads);

    println!("\nIMP-181d: Real-World Concurrent:");
    println!("  Threads: {}", num_threads);
    println!("  Successful: {}", result.successful_requests);
    println!("  Failed: {}", result.failed_requests);
    println!(
        "  QA-028: {}",
        if result.meets_qa028 { "PASS" } else { "FAIL" }
    );
}

// ================================================================================
// IMP-182: Deterministic Output (QA-029)
// Verify deterministic output with fixed seed
// ================================================================================

/// Determinism verification result
#[derive(Debug)]
pub struct DeterminismResult {
    pub seed: u64,
    pub num_runs: usize,
    pub outputs_identical: bool,
    pub hash_matches: bool,
    pub meets_qa029: bool,
}

impl DeterminismResult {
    pub fn deterministic(seed: u64, runs: usize) -> Self {
        Self {
            seed,
            num_runs: runs,
            outputs_identical: true,
            hash_matches: true,
            meets_qa029: true,
        }
    }

    pub fn non_deterministic(seed: u64, runs: usize, reason: &str) -> Self {
        let _ = reason;
        Self {
            seed,
            num_runs: runs,
            outputs_identical: false,
            hash_matches: false,
            meets_qa029: false,
        }
    }
}

/// Simple hash for determinism checking
pub fn simple_hash(s: &str) -> u64 {
    let mut hash: u64 = 5381;
    for b in s.bytes() {
        hash = hash.wrapping_mul(33).wrapping_add(u64::from(b));
    }
    hash
}

/// IMP-182a: Test determinism result structure
#[test]
fn test_imp_182a_determinism_result() {
    let det = DeterminismResult::deterministic(42, 5);
    assert!(
        det.outputs_identical,
        "IMP-182a: Deterministic should have identical outputs"
    );
    assert!(
        det.meets_qa029,
        "IMP-182a: Deterministic should meet QA-029"
    );

    let non_det = DeterminismResult::non_deterministic(42, 5, "Outputs differ");
    assert!(
        !non_det.outputs_identical,
        "IMP-182a: Non-deterministic should have different outputs"
    );
    assert!(
        !non_det.meets_qa029,
        "IMP-182a: Non-deterministic should not meet QA-029"
    );

    println!("\nIMP-182a: Determinism Results:");
    println!(
        "  Deterministic: seed={}, runs={}, meets_qa029={}",
        det.seed, det.num_runs, det.meets_qa029
    );
    println!(
        "  Non-deterministic: seed={}, runs={}, meets_qa029={}",
        non_det.seed, non_det.num_runs, non_det.meets_qa029
    );
}

/// IMP-182b: Test simple hash function
#[test]
fn test_imp_182b_simple_hash() {
    let s1 = "Hello, World!";
    let s2 = "Hello, World!";
    let s3 = "Hello, World?";

    let h1 = simple_hash(s1);
    let h2 = simple_hash(s2);
    let h3 = simple_hash(s3);

    assert_eq!(h1, h2, "IMP-182b: Identical strings should have same hash");
    assert_ne!(
        h1, h3,
        "IMP-182b: Different strings should have different hash"
    );

    println!("\nIMP-182b: Simple Hash:");
    println!("  '{}' -> {}", s1, h1);
    println!("  '{}' -> {}", s2, h2);
    println!("  '{}' -> {}", s3, h3);
}

/// IMP-182c: Test determinism verification
#[test]
fn test_imp_182c_determinism_check() {
    let outputs = vec![
        "The answer is 42".to_string(),
        "The answer is 42".to_string(),
        "The answer is 42".to_string(),
    ];

    let hashes: Vec<u64> = outputs.iter().map(|s| simple_hash(s)).collect();
    let all_same = hashes.windows(2).all(|w| w[0] == w[1]);

    let result = if all_same {
        DeterminismResult::deterministic(42, outputs.len())
    } else {
        DeterminismResult::non_deterministic(42, outputs.len(), "Hashes differ")
    };

    assert!(
        result.meets_qa029,
        "IMP-182c: Identical outputs should be deterministic"
    );

    // Test with different outputs
    let varied_outputs = vec![
        "The answer is 42".to_string(),
        "The answer is 43".to_string(),
    ];
    let varied_hashes: Vec<u64> = varied_outputs.iter().map(|s| simple_hash(s)).collect();
    let varied_same = varied_hashes.windows(2).all(|w| w[0] == w[1]);

    let varied_result = if varied_same {
        DeterminismResult::deterministic(42, varied_outputs.len())
    } else {
        DeterminismResult::non_deterministic(42, varied_outputs.len(), "Hashes differ")
    };

    assert!(
        !varied_result.meets_qa029,
        "IMP-182c: Different outputs should not be deterministic"
    );

    println!("\nIMP-182c: Determinism Check:");
    println!("  Same outputs: meets_qa029={}", result.meets_qa029);
    println!(
        "  Different outputs: meets_qa029={}",
        varied_result.meets_qa029
    );
}

include!("imp_182d.rs");
include!("imp_184d.rs");
include!("outliers_outlier.rs");
include!("imp_189d.rs");