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

// ============================================================================
// Tests for dequantize_q8_0_simd
// ============================================================================

#[test]
fn test_dequantize_q8_0_simd_single_block_p19() {
    let data = generate_q8_0_block_data(1);
    let result = dequantize_q8_0_simd(&data);

    assert!(result.is_ok());
    let output = result.expect("test value should be present");
    assert_eq!(output.len(), 32);
}

#[test]
fn test_dequantize_q8_0_simd_multiple_blocks_p19() {
    let num_blocks = 16;
    let data = generate_q8_0_block_data(num_blocks);
    let result = dequantize_q8_0_simd(&data);

    assert!(result.is_ok());
    let output = result.expect("test value should be present");
    assert_eq!(output.len(), num_blocks * 32);
}

#[test]
fn test_dequantize_q8_0_simd_parity_with_parallel_p19() {
    let data = generate_q8_0_block_data(8);

    let simd_result = dequantize_q8_0_simd(&data).expect("test value should be present");
    let parallel_result = dequantize_q8_0_parallel(&data).expect("test value should be present");

    assert_eq!(simd_result.len(), parallel_result.len());

    for i in 0..simd_result.len() {
        let diff = (simd_result[i] - parallel_result[i]).abs();
        assert!(
            diff < 1e-6,
            "SIMD/parallel mismatch at {}: simd={}, parallel={}",
            i,
            simd_result[i],
            parallel_result[i]
        );
    }
}

#[test]
fn test_dequantize_q8_0_simd_invalid_size_p19() {
    let data = vec![0u8; 50]; // Not 34 * n
    let result = dequantize_q8_0_simd(&data);
    assert!(result.is_err());
}

// ============================================================================
// Tests for dequantize_q8_0_block (internal helper)
// ============================================================================

#[test]
fn test_dequantize_q8_0_block_basic_p19() {
    let data = generate_q8_0_block_data(1);
    let block_data = &data[0..34];

    let output = dequantize_q8_0_block(block_data);
    assert_eq!(output.len(), 32);

    for val in &output {
        assert!(val.is_finite());
    }
}

#[test]
fn test_dequantize_q8_0_block_zero_scale_p19() {
    // Block with zero scale
    let mut block_data = vec![0u8; 34];
    // Scale = 0 (f16)
    block_data[0] = 0x00;
    block_data[1] = 0x00;

    // Set some non-zero quants
    for i in 0..32 {
        block_data[2 + i] = i as u8;
    }

    let output = dequantize_q8_0_block(&block_data);
    assert_eq!(output.len(), 32);

    // With zero scale, all outputs should be 0
    for val in &output {
        assert_eq!(*val, 0.0);
    }
}

#[test]
fn test_dequantize_q8_0_block_max_values_p19() {
    let mut block_data = vec![0u8; 34];

    // Scale = 1.0 (f16 = 0x3C00)
    block_data[0] = 0x00;
    block_data[1] = 0x3C;

    // Set all quants to 127 (max positive i8)
    for i in 0..32 {
        block_data[2 + i] = 127;
    }

    let output = dequantize_q8_0_block(&block_data);

    // All values should be 127 * 1.0 = 127.0
    for val in &output {
        assert!((val - 127.0).abs() < 0.01, "Expected 127.0, got {}", val);
    }
}

#[test]
fn test_dequantize_q8_0_block_min_values_p19() {
    let mut block_data = vec![0u8; 34];

    // Scale = 1.0 (f16 = 0x3C00)
    block_data[0] = 0x00;
    block_data[1] = 0x3C;

    // Set all quants to -128 (min i8 as u8 = 0x80 = 128)
    for i in 0..32 {
        block_data[2 + i] = 0x80;
    }

    let output = dequantize_q8_0_block(&block_data);

    // All values should be -128 * 1.0 = -128.0
    for val in &output {
        assert!(
            (val - (-128.0)).abs() < 0.01,
            "Expected -128.0, got {}",
            val
        );
    }
}

#[test]
fn test_dequantize_q8_0_block_alternating_signs_p19() {
    let mut block_data = vec![0u8; 34];

    // Scale = 0.5 (f16 = 0x3800)
    block_data[0] = 0x00;
    block_data[1] = 0x38;

    // Alternating positive (64) and negative (-64 = 0xC0 = 192)
    for i in 0..32 {
        block_data[2 + i] = if i % 2 == 0 { 64 } else { 192 };
    }

    let output = dequantize_q8_0_block(&block_data);

    for (i, &val) in output.iter().enumerate() {
        if i % 2 == 0 {
            assert!((val - 32.0).abs() < 0.01, "Expected 32.0, got {}", val);
        } else {
            assert!((val - (-32.0)).abs() < 0.01, "Expected -32.0, got {}", val);
        }
    }
}

// ============================================================================
// Tests for apply_rope_rotation_simd
// ============================================================================

#[test]
fn test_apply_rope_rotation_simd_basic_p19() {
    let half_dim = 4;
    let mut x1 = vec![1.0, 2.0, 3.0, 4.0];
    let mut x2 = vec![5.0, 6.0, 7.0, 8.0];
    let cos_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1).cos()).collect();
    let sin_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1).sin()).collect();

    apply_rope_rotation_simd(&mut x1, &mut x2, &cos_vals, &sin_vals);

    // Verify finite results
    for val in x1.iter().chain(x2.iter()) {
        assert!(val.is_finite());
    }
}

#[test]
fn test_apply_rope_rotation_simd_size_8_p19() {
    // Exactly 8 elements - minimum for AVX2 SIMD path
    let half_dim = 8;
    let mut x1: Vec<f32> = (0..half_dim).map(|i| i as f32).collect();
    let mut x2: Vec<f32> = (0..half_dim).map(|i| (i + half_dim) as f32).collect();
    let cos_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.05).cos()).collect();
    let sin_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.05).sin()).collect();

    apply_rope_rotation_simd(&mut x1, &mut x2, &cos_vals, &sin_vals);

    for val in x1.iter().chain(x2.iter()) {
        assert!(val.is_finite());
    }
}

#[test]
fn test_apply_rope_rotation_simd_size_16_p19() {
    // 16 elements - 2 SIMD iterations
    let half_dim = 16;
    let mut x1: Vec<f32> = (0..half_dim).map(|i| i as f32 * 0.1).collect();
    let mut x2: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1) + 1.0).collect();
    let cos_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1).cos()).collect();
    let sin_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1).sin()).collect();

    apply_rope_rotation_simd(&mut x1, &mut x2, &cos_vals, &sin_vals);

    for val in x1.iter().chain(x2.iter()) {
        assert!(val.is_finite());
    }
}

#[test]
fn test_apply_rope_rotation_simd_size_17_remainder_p19() {
    // 17 elements - tests remainder handling (2*8 + 1)
    let half_dim = 17;
    let mut x1: Vec<f32> = (0..half_dim).map(|i| i as f32).collect();
    let mut x2: Vec<f32> = (0..half_dim).map(|i| (i + half_dim) as f32).collect();
    let cos_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.05).cos()).collect();
    let sin_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.05).sin()).collect();

    apply_rope_rotation_simd(&mut x1, &mut x2, &cos_vals, &sin_vals);

    for val in x1.iter().chain(x2.iter()) {
        assert!(val.is_finite());
    }
}

#[test]
fn test_apply_rope_rotation_simd_size_7_scalar_fallback_p19() {
    // 7 elements - should use scalar fallback (< 8)
    let half_dim = 7;
    let mut x1: Vec<f32> = (0..half_dim).map(|i| i as f32).collect();
    let mut x2: Vec<f32> = (0..half_dim).map(|i| (i + half_dim) as f32).collect();
    let cos_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1).cos()).collect();
    let sin_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1).sin()).collect();

    apply_rope_rotation_simd(&mut x1, &mut x2, &cos_vals, &sin_vals);

    for val in x1.iter().chain(x2.iter()) {
        assert!(val.is_finite());
    }
}

#[test]
fn test_apply_rope_rotation_simd_identity_p19() {
    // Identity rotation: cos=1, sin=0
    let half_dim = 8;
    let mut x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
    let mut x2 = vec![9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0];
    let original_x1 = x1.clone();
    let original_x2 = x2.clone();

    let cos_vals = vec![1.0; half_dim];
    let sin_vals = vec![0.0; half_dim];

    apply_rope_rotation_simd(&mut x1, &mut x2, &cos_vals, &sin_vals);

    // With cos=1, sin=0: x1' = x1*1 - x2*0 = x1, x2' = x1*0 + x2*1 = x2
    for i in 0..half_dim {
        assert!((x1[i] - original_x1[i]).abs() < 1e-6, "x1 changed at {}", i);
        assert!((x2[i] - original_x2[i]).abs() < 1e-6, "x2 changed at {}", i);
    }
}

#[test]
fn test_apply_rope_rotation_simd_90_degrees_p19() {
    // 90 degree rotation: cos=0, sin=1
    let half_dim = 8;
    let mut x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
    let mut x2 = vec![9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0];
    let original_x1 = x1.clone();
    let original_x2 = x2.clone();

    let cos_vals = vec![0.0; half_dim];
    let sin_vals = vec![1.0; half_dim];

    apply_rope_rotation_simd(&mut x1, &mut x2, &cos_vals, &sin_vals);

    // With cos=0, sin=1: x1' = x1*0 - x2*1 = -x2, x2' = x1*1 + x2*0 = x1
    for i in 0..half_dim {
        assert!(
            (x1[i] - (-original_x2[i])).abs() < 1e-6,
            "x1[{}]: expected {}, got {}",
            i,
            -original_x2[i],
            x1[i]
        );
        assert!(
            (x2[i] - original_x1[i]).abs() < 1e-6,
            "x2[{}]: expected {}, got {}",
            i,
            original_x1[i],
            x2[i]
        );
    }
}

#[test]
fn test_apply_rope_rotation_simd_180_degrees_p19() {
    // 180 degree rotation: cos=-1, sin=0
    let half_dim = 8;
    let mut x1 = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
    let mut x2 = vec![9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0];
    let original_x1 = x1.clone();
    let original_x2 = x2.clone();

    let cos_vals = vec![-1.0; half_dim];
    let sin_vals = vec![0.0; half_dim];

    apply_rope_rotation_simd(&mut x1, &mut x2, &cos_vals, &sin_vals);

    // With cos=-1, sin=0: x1' = x1*(-1) - x2*0 = -x1, x2' = x1*0 + x2*(-1) = -x2
    for i in 0..half_dim {
        assert!((x1[i] - (-original_x1[i])).abs() < 1e-6);
        assert!((x2[i] - (-original_x2[i])).abs() < 1e-6);
    }
}

#[test]
fn test_apply_rope_rotation_simd_parity_with_scalar_p19() {
    let half_dim = 32;
    let mut simd_x1: Vec<f32> = (0..half_dim).map(|i| (i as f32) * 0.1).collect();
    let mut simd_x2: Vec<f32> = (0..half_dim).map(|i| (i as f32) * 0.1 + 1.0).collect();
    let mut scalar_x1 = simd_x1.clone();
    let mut scalar_x2 = simd_x2.clone();

    let cos_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1).cos()).collect();
    let sin_vals: Vec<f32> = (0..half_dim).map(|i| (i as f32 * 0.1).sin()).collect();

    apply_rope_rotation_simd(&mut simd_x1, &mut simd_x2, &cos_vals, &sin_vals);
    apply_rope_rotation_scalar(&mut scalar_x1, &mut scalar_x2, &cos_vals, &sin_vals);

    for i in 0..half_dim {
        let diff_x1 = (simd_x1[i] - scalar_x1[i]).abs();
        let diff_x2 = (simd_x2[i] - scalar_x2[i]).abs();
        assert!(
            diff_x1 < 1e-5,
            "x1 SIMD/scalar mismatch at {}: simd={}, scalar={}",
            i,
            simd_x1[i],
            scalar_x1[i]
        );
        assert!(
            diff_x2 < 1e-5,
            "x2 SIMD/scalar mismatch at {}: simd={}, scalar={}",
            i,
            simd_x2[i],
            scalar_x2[i]
        );
    }
}

// ============================================================================
// Tests for apply_rope_rotation_scalar (internal helper)
// ============================================================================

#[test]
fn test_apply_rope_rotation_scalar_basic_p19() {
    let _half_dim = 4;
    let mut x1 = vec![1.0, 2.0, 3.0, 4.0];
    let mut x2 = vec![5.0, 6.0, 7.0, 8.0];
    let cos_vals = vec![1.0, 0.866, 0.5, 0.0]; // cos(0), cos(30), cos(60), cos(90)
    let sin_vals = vec![0.0, 0.5, 0.866, 1.0]; // sin(0), sin(30), sin(60), sin(90)

    apply_rope_rotation_scalar(&mut x1, &mut x2, &cos_vals, &sin_vals);

    for val in x1.iter().chain(x2.iter()) {
        assert!(val.is_finite());
    }
}

#[test]
fn test_apply_rope_rotation_scalar_empty_p19() {
    let mut x1: Vec<f32> = vec![];
    let mut x2: Vec<f32> = vec![];
    let cos_vals: Vec<f32> = vec![];
    let sin_vals: Vec<f32> = vec![];

    // Should not panic on empty input
    apply_rope_rotation_scalar(&mut x1, &mut x2, &cos_vals, &sin_vals);
    assert!(x1.is_empty());
    assert!(x2.is_empty());
}

#[test]
fn test_apply_rope_rotation_scalar_single_element_p19() {
    let mut x1 = vec![1.0];
    let mut x2 = vec![2.0];
    let cos_vals = vec![0.5];
    let sin_vals = vec![0.866];

    apply_rope_rotation_scalar(&mut x1, &mut x2, &cos_vals, &sin_vals);

    // x1' = 1*0.5 - 2*0.866 = 0.5 - 1.732 = -1.232
    // x2' = 1*0.866 + 2*0.5 = 0.866 + 1.0 = 1.866
    assert!((x1[0] - (-1.232)).abs() < 0.01);
    assert!((x2[0] - 1.866).abs() < 0.01);
}

#[test]
fn test_apply_rope_rotation_scalar_negative_values_p19() {
    let mut x1 = vec![-1.0, -2.0, -3.0, -4.0];
    let mut x2 = vec![-5.0, -6.0, -7.0, -8.0];
    let cos_vals = vec![1.0; 4];
    let sin_vals = vec![0.0; 4];

    apply_rope_rotation_scalar(&mut x1, &mut x2, &cos_vals, &sin_vals);

    // Identity rotation preserves values
    assert!((x1[0] - (-1.0)).abs() < 1e-6);
    assert!((x2[0] - (-5.0)).abs() < 1e-6);
}

// ============================================================================
// Tests for thread pool and chunking behavior
// ============================================================================

#[test]
fn test_q4k_parallel_thread_consistency_p19() {
    // Run many times to catch potential race conditions
    let data = generate_q4k_superblock_data(16);
    let reference = dequantize_q4_k_parallel(&data).expect("test value should be present");

    for run in 0..10 {
        let result = dequantize_q4_k_parallel(&data).expect("test value should be present");
        for i in 0..result.len() {
            assert_eq!(
                result[i], reference[i],
                "Thread inconsistency on run {} at index {}",
                run, i
            );
        }
    }
}