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

// ============================================================================
// fused_swiglu_simd: Size-based coverage
// ============================================================================

#[test]
fn test_fused_swiglu_simd_size_7() {
    // Not divisible by 8 - hits remainder path
    let mut gate: Vec<f32> = (0..7).map(|i| i as f32 * 0.5).collect();
    let up = vec![1.0f32; 7];

    fused_swiglu_simd(&mut gate, &up);

    // silu(0) = 0
    assert!((gate[0] - 0.0).abs() < 1e-5);
}

#[test]
fn test_fused_swiglu_simd_size_15() {
    // 8 + 7 remainder
    let mut gate: Vec<f32> = (0..15).map(|i| (i as f32 - 7.0) * 0.2).collect();
    let up = vec![1.0f32; 15];

    fused_swiglu_simd(&mut gate, &up);

    // All should be finite
    for g in &gate {
        assert!(g.is_finite());
    }
}

#[test]
fn test_fused_swiglu_simd_size_64() {
    // Perfectly divisible by 8
    let mut gate: Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 0.05).collect();
    let up = vec![1.0f32; 64];

    fused_swiglu_simd(&mut gate, &up);

    // Should have proper sigmoid-like distribution
    assert!(gate.iter().all(|g| g.is_finite()));
}

// ============================================================================
// softmax_simd: Size-based coverage
// ============================================================================

#[test]
fn test_softmax_simd_size_7() {
    // Not divisible by 8
    let mut x: Vec<f32> = (0..7).map(|i| i as f32).collect();
    softmax_simd(&mut x);

    let sum: f32 = x.iter().sum();
    assert!((sum - 1.0).abs() < 1e-5);
}

#[test]
fn test_softmax_simd_size_15() {
    // 8 + 7 remainder
    let mut x: Vec<f32> = (0..15).map(|i| i as f32 * 0.1).collect();
    softmax_simd(&mut x);

    let sum: f32 = x.iter().sum();
    assert!((sum - 1.0).abs() < 1e-5);
}

#[test]
fn test_softmax_simd_size_64() {
    let mut x: Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 0.1).collect();
    softmax_simd(&mut x);

    let sum: f32 = x.iter().sum();
    assert!((sum - 1.0).abs() < 1e-5);
}

#[test]
fn test_softmax_simd_size_100() {
    // 12 * 8 + 4 remainder
    let mut x: Vec<f32> = (0..100).map(|i| i as f32 * 0.01).collect();
    softmax_simd(&mut x);

    let sum: f32 = x.iter().sum();
    assert!((sum - 1.0).abs() < 1e-5);
}

// ============================================================================
// quantize_activations_q8_0: Edge cases
// ============================================================================

#[test]
fn test_quantize_activations_q8_0_size_1() {
    let activations = vec![42.0f32];
    let (scales, quants) = quantize_activations_q8_0(&activations);

    // 1 block
    assert_eq!(scales.len(), 1);
    assert_eq!(quants.len(), 32); // Padded

    // First element should be 127 (max value maps to max quant)
    assert_eq!(quants[0], 127);
    // Padding should be zeros
    for q in &quants[1..32] {
        assert_eq!(*q, 0i8);
    }
}

#[test]
fn test_quantize_activations_q8_0_symmetric() {
    let activations = vec![-10.0, 0.0, 10.0];
    let (_scales, quants) = quantize_activations_q8_0(&activations);

    // Scale = 10.0 / 127.0
    // quants: -127, 0, 127
    assert_eq!(quants[0], -127);
    assert_eq!(quants[1], 0);
    assert_eq!(quants[2], 127);
}

#[test]
fn test_quantize_activations_q8_0_near_zero_max() {
    let activations = vec![1e-12f32; 10];
    let (scales, _quants) = quantize_activations_q8_0(&activations);

    // Fallback scale
    assert!((scales[0] - 1.0 / 127.0).abs() < 1e-10);
}

#[test]
fn test_quantize_activations_q8_0_exact_block() {
    let activations: Vec<f32> = (0..32).map(|i| i as f32).collect();
    let (scales, quants) = quantize_activations_q8_0(&activations);

    assert_eq!(scales.len(), 1);
    assert_eq!(quants.len(), 32);
    // Max value is 31, so quants[31] = 127
    assert_eq!(quants[31], 127);
}

#[test]
fn test_quantize_activations_q8_0_multi_block() {
    let activations: Vec<f32> = (0..100).map(|i| (i as f32 - 50.0) * 0.5).collect();
    let (scales, quants) = quantize_activations_q8_0(&activations);

    // 4 blocks (ceil(100/32) = 4)
    assert_eq!(scales.len(), 4);
    assert_eq!(quants.len(), 128);
}

// ============================================================================
// fused_rmsnorm_q4_0_matmul: Error paths and edge cases
// ============================================================================

#[test]
fn test_fused_rmsnorm_q4_0_matmul_input_dim_mismatch() {
    let input = vec![1.0f32; 16]; // Wrong size
    let norm_weight = vec![1.0f32; 32];
    let weight_data = vec![0u8; 18]; // 1 block

    let result = fused_rmsnorm_q4_0_matmul(&input, &norm_weight, 1e-5, &weight_data, 32, 1);
    assert!(result.is_err());
}

#[test]
fn test_fused_rmsnorm_q4_0_matmul_weight_too_small() {
    let input = vec![1.0f32; 32];
    let norm_weight = vec![1.0f32; 32];
    let weight_data = vec![0u8; 10]; // Too small (need 18 bytes for 1 block)

    let result = fused_rmsnorm_q4_0_matmul(&input, &norm_weight, 1e-5, &weight_data, 32, 1);
    assert!(result.is_err());
}

#[test]
fn test_fused_rmsnorm_q4_0_matmul_zero_out_dim() {
    let input = vec![1.0f32; 32];
    let norm_weight = vec![1.0f32; 32];
    let weight_data = vec![0u8; 18];

    let result = fused_rmsnorm_q4_0_matmul(&input, &norm_weight, 1e-5, &weight_data, 32, 0);
    assert!(result.is_ok());
    assert!(result.expect("test value should be present").is_empty());
}

// ============================================================================
// fused_rmsnorm_ffn_up_gate: Error paths and edge cases
// ============================================================================

#[test]
fn test_fused_rmsnorm_ffn_up_gate_input_dim_mismatch() {
    let input = vec![1.0f32; 16]; // Wrong size
    let norm_weight = vec![1.0f32; 32];
    let up_weight = vec![0u8; 18];
    let gate_weight = vec![0u8; 18];

    let result =
        fused_rmsnorm_ffn_up_gate(&input, &norm_weight, 1e-5, &up_weight, &gate_weight, 32, 1);
    assert!(result.is_err());
}

#[test]
fn test_fused_rmsnorm_ffn_up_gate_up_weight_too_small() {
    let input = vec![1.0f32; 32];
    let norm_weight = vec![1.0f32; 32];
    let up_weight = vec![0u8; 10]; // Too small
    let gate_weight = vec![0u8; 18];

    let result =
        fused_rmsnorm_ffn_up_gate(&input, &norm_weight, 1e-5, &up_weight, &gate_weight, 32, 1);
    assert!(result.is_err());
}

#[test]
fn test_fused_rmsnorm_ffn_up_gate_gate_weight_too_small() {
    let input = vec![1.0f32; 32];
    let norm_weight = vec![1.0f32; 32];
    let up_weight = vec![0u8; 18];
    let gate_weight = vec![0u8; 10]; // Too small

    let result =
        fused_rmsnorm_ffn_up_gate(&input, &norm_weight, 1e-5, &up_weight, &gate_weight, 32, 1);
    assert!(result.is_err());
}

#[test]
fn test_fused_rmsnorm_ffn_up_gate_zero_out_dim() {
    let input = vec![1.0f32; 32];
    let norm_weight = vec![1.0f32; 32];
    let up_weight = vec![0u8; 18];
    let gate_weight = vec![0u8; 18];

    let result =
        fused_rmsnorm_ffn_up_gate(&input, &norm_weight, 1e-5, &up_weight, &gate_weight, 32, 0);
    assert!(result.is_ok());
    let (up, gate) = result.expect("test value should be present");
    assert!(up.is_empty());
    assert!(gate.is_empty());
}

// ============================================================================
// Scalar vs SIMD parity tests
// ============================================================================

#[test]
fn test_swiglu_scalar_produces_output() {
    let values: Vec<f32> = (0..32).map(|i| (i as f32 - 16.0) * 0.2).collect();
    let up = vec![1.0f32; 32];

    // Test that scalar swiglu produces valid output
    let mut gate_scalar = values.clone();
    fused_swiglu_scalar(&mut gate_scalar, &up);

    // All values should be finite
    for v in &gate_scalar {
        assert!(v.is_finite(), "SwiGLU output should be finite");
    }

    // Output should differ from input (transformation applied)
    let different = gate_scalar
        .iter()
        .zip(values.iter())
        .any(|(a, b)| (a - b).abs() > 1e-10);
    assert!(different, "SwiGLU should transform input");
}

#[test]
fn test_softmax_scalar_simd_parity() {
    let values: Vec<f32> = (0..32).map(|i| i as f32 * 0.1).collect();

    // Scalar
    let mut x_scalar = values.clone();
    softmax_scalar(&mut x_scalar);

    // SIMD (via dispatch)
    let mut x_simd = values.clone();
    softmax_simd(&mut x_simd);

    // Should match within tolerance
    for (s, d) in x_scalar.iter().zip(x_simd.iter()) {
        assert!((s - d).abs() < 1e-5, "Mismatch: scalar={}, simd={}", s, d);
    }
}

#[test]
fn test_quantize_rmsnorm_scalar_simd_parity() {
    let input: Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 0.05).collect();
    let norm_weight = vec![1.0f32; 64];
    let eps = 1e-5;

    // Scalar
    let (scales_scalar, quants_scalar) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);

    // SIMD (via dispatch) - may use AVX2
    let (scales_simd, quants_simd) = quantize_rmsnorm_q8_0(&input, &norm_weight, eps);

    // Scales should be very close
    for (s, d) in scales_scalar.iter().zip(scales_simd.iter()) {
        assert!(
            (s - d).abs() < 1e-5,
            "Scale mismatch: scalar={}, simd={}",
            s,
            d
        );
    }

    // Quants may differ by 1 due to rounding
    for (s, d) in quants_scalar.iter().zip(quants_simd.iter()) {
        assert!(
            (*s as i32 - *d as i32).abs() <= 1,
            "Quant mismatch: scalar={}, simd={}",
            s,
            d
        );
    }
}

// ============================================================================
// Additional edge case tests for full coverage
// ============================================================================

#[test]
fn test_quantize_rmsnorm_q8_0_scalar_size_1() {
    let input = vec![1.0f32];
    let norm_weight = vec![1.0f32];
    let eps = 1e-5;

    let (scales, quants) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);

    assert_eq!(scales.len(), 1);
    assert_eq!(quants.len(), 32);
    // First element should be 127 (normalized = 1.0)
    assert_eq!(quants[0], 127);
}

#[test]
fn test_quantize_rmsnorm_q8_0_scalar_size_31() {
    // Just under block boundary
    let input: Vec<f32> = (0..31).map(|i| i as f32 * 0.1).collect();
    let norm_weight = vec![1.0f32; 31];
    let eps = 1e-5;

    let (scales, quants) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);

    assert_eq!(scales.len(), 1);
    assert_eq!(quants.len(), 32);
    // Padding at position 31
    assert_eq!(quants[31], 0i8);
}

#[test]
fn test_quantize_rmsnorm_q8_0_scalar_size_64() {
    // Exactly 2 blocks
    let input: Vec<f32> = (0..64).map(|i| (i as f32 - 32.0) * 0.1).collect();
    let norm_weight = vec![1.0f32; 64];
    let eps = 1e-5;

    let (scales, quants) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);

    assert_eq!(scales.len(), 2);
    assert_eq!(quants.len(), 64);
    // Both blocks should have valid scales
    assert!(scales[0] > 0.0);
    assert!(scales[1] > 0.0);
}

#[test]
fn test_fused_swiglu_scalar_symmetry() {
    // silu(-x) * 1 = -silu(x) for silu
    let mut gate_pos = vec![1.0, 2.0, 3.0];
    let mut gate_neg = vec![-1.0, -2.0, -3.0];
    let up = vec![1.0, 1.0, 1.0];

    fused_swiglu_scalar(&mut gate_pos, &up);
    fused_swiglu_scalar(&mut gate_neg, &up);

    // silu(-x) = -x * sigmoid(-x), silu(x) = x * sigmoid(x)
    // They are NOT equal in magnitude, but have opposite signs
    for i in 0..3 {
        assert!(gate_pos[i] > 0.0);
        assert!(gate_neg[i] < 0.0);
    }
}

#[test]
fn test_softmax_scalar_two_elements() {
    let mut x = vec![0.0, 0.0];
    softmax_scalar(&mut x);

    // Equal inputs should give equal outputs
    assert!((x[0] - 0.5).abs() < 1e-5);
    assert!((x[1] - 0.5).abs() < 1e-5);
}

#[test]
fn test_softmax_scalar_diff_10() {
    // Large difference should make smaller almost 0
    let mut x = vec![0.0, 10.0];
    softmax_scalar(&mut x);

    assert!(x[0] < 0.001);
    assert!(x[1] > 0.999);
}

#[test]
fn test_quantize_activations_q8_0_all_negative() {
    let activations = vec![-5.0f32; 16];
    let (_scales, quants) = quantize_activations_q8_0(&activations);

    // All quants should be -127 (max negative)
    for q in &quants[..16] {
        assert_eq!(*q, -127);
    }
    // Padding should be 0
    for q in &quants[16..32] {
        assert_eq!(*q, 0);
    }
}

#[test]
fn test_quantize_activations_q8_0_alternating() {
    let activations = vec![1.0, -1.0, 1.0, -1.0];
    let (_scales, quants) = quantize_activations_q8_0(&activations);

    // Scale = 1.0 / 127.0
    // quants should alternate 127, -127
    assert_eq!(quants[0], 127);
    assert_eq!(quants[1], -127);
    assert_eq!(quants[2], 127);
    assert_eq!(quants[3], -127);
}

// ============================================================================
// Test clamping behavior
// ============================================================================

#[test]
fn test_quantize_rmsnorm_q8_0_scalar_clamping() {
    // Create input that would exceed i8 range after quantization
    let input = vec![1000.0f32; 32];
    let norm_weight = vec![1.0f32; 32];
    let eps = 1e-5;

    let (scales, quants) = quantize_rmsnorm_q8_0_scalar(&input, &norm_weight, eps);

    // All quants should be 127 (clamped max)
    for q in quants {
        assert_eq!(q, 127);
    }
    assert!(scales[0] > 0.0);
}