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
//! Phase 36: SIMD Helper Functions Tests
//!
//! These tests cover the SIMD helper functions in simd.rs:
//! - f16 conversion: `f16_to_f32`, `read_f16`
//! - Scale extraction: `extract_scale_min`, `extract_scale_min_from_slice`
//! - Activation functions: `softmax_simd`, `fused_swiglu_simd`
//!
//! Strategy: Use proptest to fuzz inputs and verify against reference implementations.

use proptest::prelude::*;

use crate::quantize::simd::{extract_scale_min, read_f16};
use crate::quantize::{f16_to_f32, fused_swiglu_simd, softmax_simd};

// =============================================================================
// f16 Conversion Tests
// =============================================================================

#[test]
fn test_f16_to_f32_zero() {
    // Positive zero: 0x0000
    assert_eq!(f16_to_f32(0x0000), 0.0);

    // Negative zero: 0x8000
    let neg_zero = f16_to_f32(0x8000);
    assert!(neg_zero == 0.0 || neg_zero == -0.0);
}

#[test]
fn test_f16_to_f32_one() {
    // 1.0 in f16: sign=0, exp=15 (biased), mantissa=0 → 0x3C00
    let result = f16_to_f32(0x3C00);
    assert!(
        (result - 1.0).abs() < 1e-6,
        "1.0 conversion: got {}",
        result
    );
}

#[test]
fn test_f16_to_f32_negative_one() {
    // -1.0 in f16: sign=1, exp=15, mantissa=0 → 0xBC00
    let result = f16_to_f32(0xBC00);
    assert!(
        (result - (-1.0)).abs() < 1e-6,
        "-1.0 conversion: got {}",
        result
    );
}

#[test]
fn test_f16_to_f32_two() {
    // 2.0 in f16: sign=0, exp=16 (biased), mantissa=0 → 0x4000
    let result = f16_to_f32(0x4000);
    assert!(
        (result - 2.0).abs() < 1e-6,
        "2.0 conversion: got {}",
        result
    );
}

#[test]
fn test_f16_to_f32_half() {
    // 0.5 in f16: sign=0, exp=14 (biased), mantissa=0 → 0x3800
    let result = f16_to_f32(0x3800);
    assert!(
        (result - 0.5).abs() < 1e-6,
        "0.5 conversion: got {}",
        result
    );
}

#[test]
fn test_f16_to_f32_infinity() {
    // +Inf: sign=0, exp=31, mantissa=0 → 0x7C00
    let result = f16_to_f32(0x7C00);
    assert!(result.is_infinite() && result > 0.0, "Should be +Inf");

    // -Inf: sign=1, exp=31, mantissa=0 → 0xFC00
    let result = f16_to_f32(0xFC00);
    assert!(result.is_infinite() && result < 0.0, "Should be -Inf");
}

#[test]
fn test_f16_to_f32_nan() {
    // NaN: exp=31, mantissa!=0 → 0x7C01 (one example)
    let result = f16_to_f32(0x7C01);
    assert!(result.is_nan(), "Should be NaN");
}

#[test]
fn test_f16_to_f32_subnormal() {
    // Subnormal: exp=0, mantissa!=0
    // Smallest subnormal: 0x0001 = 2^-24 ≈ 5.96e-8
    let result = f16_to_f32(0x0001);
    assert!(
        result > 0.0 && result < 1e-6,
        "Subnormal should be tiny positive: {}",
        result
    );
}

#[test]
fn test_read_f16_basic() {
    // Test read_f16 helper
    let bytes = 0x3C00u16.to_le_bytes();
    let result = read_f16(&bytes);
    assert!((result - 1.0).abs() < 1e-6, "read_f16(1.0): got {}", result);
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    /// Test f16_to_f32 matches half crate for normal values
    #[test]
    fn prop_f16_to_f32_matches_half(bits in 0u16..0x7C00) {
        // Skip NaN/Inf range (0x7C00+)
        let our_result = f16_to_f32(bits);
        let half_result = half::f16::from_bits(bits).to_f32();

        // Allow small tolerance for subnormals
        if our_result.abs() < 1e-6 && half_result.abs() < 1e-6 {
            // Both near zero, that's fine
            return Ok(());
        }

        let tolerance = half_result.abs() * 1e-5 + 1e-10;
        prop_assert!(
            (our_result - half_result).abs() <= tolerance,
            "f16_to_f32 mismatch for bits 0x{:04X}: ours={}, half={}",
            bits, our_result, half_result
        );
    }
}

// =============================================================================
// Scale Extraction Tests
// =============================================================================

#[test]
fn test_extract_scale_min_first_four_blocks() {
    // First 4 blocks use simple layout: scale = q[j] & 63, min = q[j+4] & 63
    let scales: [u8; 12] = [
        10, 20, 30, 40, // scales for blocks 0-3
        5, 15, 25, 35, // mins for blocks 0-3
        0, 0, 0, 0, // high bits (unused for first 4)
    ];

    let (s0, m0) = extract_scale_min(&scales, 0);
    assert_eq!(s0, 10.0);
    assert_eq!(m0, 5.0);

    let (s1, m1) = extract_scale_min(&scales, 1);
    assert_eq!(s1, 20.0);
    assert_eq!(m1, 15.0);

    let (s2, m2) = extract_scale_min(&scales, 2);
    assert_eq!(s2, 30.0);
    assert_eq!(m2, 25.0);

    let (s3, m3) = extract_scale_min(&scales, 3);
    assert_eq!(s3, 40.0);
    assert_eq!(m3, 35.0);
}

#[test]
fn test_extract_scale_min_last_four_blocks() {
    // Last 4 blocks use packed layout
    // For j=4: d = (scales[8] & 0x0F) | ((scales[0] >> 6) << 4)
    //          m = (scales[8] >> 4) | ((scales[4] >> 6) << 4)
    let scales: [u8; 12] = [
        0b11_000000, // byte 0: high bits = 3
        0b10_000000, // byte 1: high bits = 2
        0b01_000000, // byte 2: high bits = 1
        0b00_000000, // byte 3: high bits = 0
        0b11_000000, // byte 4: high bits = 3 (for min)
        0b10_000000, // byte 5: high bits = 2
        0b01_000000, // byte 6: high bits = 1
        0b00_000000, // byte 7: high bits = 0
        0b0101_0010, // byte 8: low=2 (scale4), high=5 (min4)
        0b0110_0011, // byte 9: low=3 (scale5), high=6 (min5)
        0b0111_0100, // byte 10: low=4 (scale6), high=7 (min6)
        0b1000_0101, // byte 11: low=5 (scale7), high=8 (min7)
    ];

    // Block 4: d = 2 | (3 << 4) = 2 + 48 = 50, m = 5 | (3 << 4) = 5 + 48 = 53
    let (s4, m4) = extract_scale_min(&scales, 4);
    assert_eq!(s4, 50.0);
    assert_eq!(m4, 53.0);
}

#[test]
fn test_extract_scale_min_all_zeros() {
    let scales: [u8; 12] = [0; 12];

    for i in 0..8 {
        let (s, m) = extract_scale_min(&scales, i);
        assert_eq!(s, 0.0, "Block {} scale should be 0", i);
        assert_eq!(m, 0.0, "Block {} min should be 0", i);
    }
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test extract_scale_min returns values in valid 6-bit range
    #[test]
    fn prop_extract_scale_min_range(
        scales in prop::collection::vec(any::<u8>(), 12..=12)
    ) {
        let scales_arr: [u8; 12] = scales.try_into().expect("try_into conversion failed");

        for i in 0..8 {
            let (s, m) = extract_scale_min(&scales_arr, i);
            prop_assert!(s >= 0.0 && s <= 63.0, "Scale out of range: {}", s);
            prop_assert!(m >= 0.0 && m <= 63.0, "Min out of range: {}", m);
        }
    }
}

// =============================================================================
// Softmax SIMD Tests
// =============================================================================

/// Reference softmax implementation (numerically stable)
fn softmax_reference(x: &[f32]) -> Vec<f32> {
    if x.is_empty() {
        return vec![];
    }

    let max_val = x.iter().copied().fold(f32::NEG_INFINITY, f32::max);
    let exp_vals: Vec<f32> = x.iter().map(|v| (*v - max_val).exp()).collect();
    let sum: f32 = exp_vals.iter().sum();
    exp_vals.iter().map(|v| v / sum).collect()
}

#[test]
fn test_softmax_simd_empty() {
    let mut x: Vec<f32> = vec![];
    softmax_simd(&mut x);
    assert!(x.is_empty());
}

#[test]
fn test_softmax_simd_single() {
    let mut x = vec![1.0f32];
    softmax_simd(&mut x);
    assert!(
        (x[0] - 1.0).abs() < 1e-6,
        "Softmax of single element should be 1.0"
    );
}

#[test]
fn test_softmax_simd_two_equal() {
    let mut x = vec![1.0f32, 1.0];
    softmax_simd(&mut x);
    assert!(
        (x[0] - 0.5).abs() < 1e-6,
        "Equal inputs should give 0.5 each: {:?}",
        x
    );
    assert!((x[1] - 0.5).abs() < 1e-6);
}

#[test]
fn test_softmax_simd_large_difference() {
    // When one value is much larger, it should dominate
    let mut x = vec![0.0, 0.0, 0.0, 100.0];
    softmax_simd(&mut x);
    assert!(x[3] > 0.99, "Largest value should dominate: {:?}", x);
}

#[test]
fn test_softmax_simd_sum_to_one() {
    let mut x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    softmax_simd(&mut x);
    let sum: f32 = x.iter().sum();
    assert!(
        (sum - 1.0).abs() < 1e-6,
        "Softmax should sum to 1.0: got {}",
        sum
    );
}

#[test]
fn test_softmax_simd_all_negative() {
    let mut x = vec![-1.0, -2.0, -3.0, -4.0];
    softmax_simd(&mut x);
    let sum: f32 = x.iter().sum();
    assert!(
        (sum - 1.0).abs() < 1e-6,
        "Softmax with negative inputs should still sum to 1.0"
    );
    // First element should be largest (least negative)
    assert!(x[0] > x[1] && x[1] > x[2] && x[2] > x[3]);
}

#[test]
fn test_softmax_simd_matches_reference_small() {
    let mut x = vec![1.0, 2.0, 3.0, 4.0];
    let reference = softmax_reference(&x);
    softmax_simd(&mut x);

    for (i, (actual, expected)) in x.iter().zip(reference.iter()).enumerate() {
        assert!(
            (actual - expected).abs() < 1e-5,
            "Mismatch at {}: got {}, expected {}",
            i,
            actual,
            expected
        );
    }
}

#[test]
fn test_softmax_simd_matches_reference_large() {
    // Test with 32 elements (uses SIMD path)
    let mut x: Vec<f32> = (0..32).map(|i| i as f32 * 0.1).collect();
    let reference = softmax_reference(&x);
    softmax_simd(&mut x);

    for (i, (actual, expected)) in x.iter().zip(reference.iter()).enumerate() {
        assert!(
            (actual - expected).abs() < 1e-5,
            "Mismatch at {}: got {}, expected {}",
            i,
            actual,
            expected
        );
    }
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(50))]

    /// Test softmax_simd always sums to 1.0
    #[test]
    fn prop_softmax_simd_sums_to_one(
        x in prop::collection::vec(-100.0f32..100.0f32, 1..=64)
    ) {
        let mut x_copy = x.clone();
        softmax_simd(&mut x_copy);

        let sum: f32 = x_copy.iter().sum();
        prop_assert!(
            (sum - 1.0).abs() < 1e-5,
            "Softmax should sum to 1.0: got {} for input len {}",
            sum, x.len()
        );
    }

    /// Test softmax_simd values are all non-negative
    #[test]
    fn prop_softmax_simd_non_negative(
        x in prop::collection::vec(-100.0f32..100.0f32, 1..=64)
    ) {
        let mut x_copy = x.clone();
        softmax_simd(&mut x_copy);

        for (i, v) in x_copy.iter().enumerate() {
            prop_assert!(*v >= 0.0, "Softmax value at {} should be non-negative: {}", i, v);
        }
    }

    /// Test softmax_simd preserves relative ordering (larger input → larger output)
    #[test]
    fn prop_softmax_simd_preserves_order(
        x in prop::collection::vec(-10.0f32..10.0f32, 2..=32)
    ) {
        let mut x_copy = x.clone();
        softmax_simd(&mut x_copy);

        // Find max input and its softmax output
        let (max_idx, _) = x.iter().enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .expect("test value should be present");

        let (softmax_max_idx, _) = x_copy.iter().enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .expect("test value should be present");

        prop_assert_eq!(
            max_idx, softmax_max_idx,
            "Max input index should have max softmax output"
        );
    }

    /// Test softmax_simd matches reference implementation
    #[test]
    fn prop_softmax_simd_matches_reference(
        x in prop::collection::vec(-10.0f32..10.0f32, 1..=64)
    ) {
        let reference = softmax_reference(&x);
        let mut x_copy = x.clone();
        softmax_simd(&mut x_copy);

        for (i, (actual, expected)) in x_copy.iter().zip(reference.iter()).enumerate() {
            let tolerance = expected.abs() * 1e-4 + 1e-6;
            prop_assert!(
                (actual - expected).abs() <= tolerance,
                "Mismatch at {}: got {}, expected {} (input len {})",
                i, actual, expected, x.len()
            );
        }
    }
}

// =============================================================================
// Numerical Stability Tests
// =============================================================================

#[test]
fn test_softmax_simd_large_values() {
    // Large values should not overflow
    let mut x = vec![1000.0, 1001.0, 1002.0, 1003.0];
    softmax_simd(&mut x);

    // Should not be NaN or Inf
    for v in &x {
        assert!(!v.is_nan(), "Softmax should not produce NaN");
        assert!(!v.is_infinite(), "Softmax should not produce Inf");
    }

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

include!("softmax_simd.rs");