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
//! Part 24: Comprehensive coverage for quantize/mod.rs functions
//!
//! Target: 90% coverage for quantize/mod.rs
//! Focus on:
//! - quantize_activations_q8k_into error branches
//! - quantize_to_q8_blocks error branches
//! - InterleavedQ4K all paths
//! - fused_q4_0_q8_0_parallel_matvec all paths
//! - fused_q8_0_q8_0_parallel_matvec all paths
//! - f16_to_f32_lut

use crate::quantize::{
    dequantize_q8_blocks, f16_to_f32_lut, fused_q4_0_q8_0_parallel_matvec,
    fused_q4_0_q8_0_parallel_matvec_into, fused_q8_0_q8_0_parallel_matvec,
    fused_q8_0_q8_0_parallel_matvec_into, quantize_activations_q8k_into, quantize_to_q8_blocks,
    InterleavedQ4K, Q8_0Block,
};

// Import internal functions for direct testing
use crate::quantize::{
    extract_scale_min, fused_q4_0_q8_0_dot_scalar,
    fused_q8_0_q8_0_dot_scalar,
};

// =============================================================================
// f16_to_f32_lut tests
// =============================================================================

#[test]
fn test_f16_lut_zero() {
    let result = f16_to_f32_lut(0);
    assert_eq!(result, 0.0);
}

#[test]
fn test_f16_lut_one() {
    // f16 representation of 1.0 is 0x3C00
    let result = f16_to_f32_lut(0x3C00);
    assert!((result - 1.0).abs() < 1e-6);
}

#[test]
fn test_f16_lut_negative_one() {
    // f16 representation of -1.0 is 0xBC00
    let result = f16_to_f32_lut(0xBC00);
    assert!((result - (-1.0)).abs() < 1e-6);
}

#[test]
fn test_f16_lut_half() {
    // f16 representation of 0.5 is 0x3800
    let result = f16_to_f32_lut(0x3800);
    assert!((result - 0.5).abs() < 1e-6);
}

#[test]
fn test_f16_lut_various_values() {
    // Test a range of values to ensure LUT is correctly populated
    let test_cases = [
        (0x0000u16, 0.0f32), // Zero
        (0x8000, -0.0),      // Negative zero
        (0x3C00, 1.0),       // One
        (0x4000, 2.0),       // Two
        (0x4200, 3.0),       // Three
        (0x3E00, 1.5),       // 1.5
        (0x4400, 4.0),       // Four
        (0x4500, 5.0),       // Five
    ];

    for (bits, expected) in test_cases {
        let result = f16_to_f32_lut(bits);
        assert!(
            (result - expected).abs() < 1e-3,
            "f16 bits {:#06x}: expected {}, got {}",
            bits,
            expected,
            result
        );
    }
}

#[test]
fn test_f16_lut_max_value() {
    // Max normal f16 value
    let result = f16_to_f32_lut(0x7BFF);
    assert!(result > 60000.0 && result < 70000.0);
}

#[test]
fn test_f16_lut_infinity() {
    // f16 positive infinity
    let result = f16_to_f32_lut(0x7C00);
    assert!(result.is_infinite() && result.is_sign_positive());
}

#[test]
fn test_f16_lut_nan() {
    // f16 NaN (any value with exp=0x1F and mantissa!=0)
    let result = f16_to_f32_lut(0x7C01);
    assert!(result.is_nan());
}

// =============================================================================
// quantize_activations_q8k_into tests
// =============================================================================

#[test]
fn test_q8k_into_not_multiple_of_256() {
    let activations = vec![0.0f32; 255]; // Not multiple of 256
    let mut scales = vec![0.0f32; 1];
    let mut quants = vec![0i8; 255];

    let result = quantize_activations_q8k_into(&activations, &mut scales, &mut quants);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("multiple of 256"));
}

#[test]
fn test_q8k_into_scales_too_small() {
    let activations = vec![0.0f32; 512]; // 2 superblocks
    let mut scales = vec![0.0f32; 1]; // Only 1 scale, need 2
    let mut quants = vec![0i8; 512];

    let result = quantize_activations_q8k_into(&activations, &mut scales, &mut quants);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("Scales buffer too small"));
}

#[test]
fn test_q8k_into_quants_too_small() {
    let activations = vec![0.0f32; 512]; // 2 superblocks
    let mut scales = vec![0.0f32; 2];
    let mut quants = vec![0i8; 256]; // Only 256, need 512

    let result = quantize_activations_q8k_into(&activations, &mut scales, &mut quants);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("Quants buffer too small"));
}

#[test]
fn test_q8k_into_success() {
    let activations = vec![1.0f32; 256];
    let mut scales = vec![0.0f32; 1];
    let mut quants = vec![0i8; 256];

    let result = quantize_activations_q8k_into(&activations, &mut scales, &mut quants);
    assert!(result.is_ok());
    // Scale should be non-zero for non-zero input
    assert!(scales[0].abs() > 0.0);
}

#[test]
fn test_q8k_into_two_superblocks() {
    let activations = vec![0.5f32; 512];
    let mut scales = vec![0.0f32; 2];
    let mut quants = vec![0i8; 512];

    let result = quantize_activations_q8k_into(&activations, &mut scales, &mut quants);
    assert!(result.is_ok());
    assert!(scales[0].abs() > 0.0);
    assert!(scales[1].abs() > 0.0);
}

// =============================================================================
// quantize_to_q8_blocks tests
// =============================================================================

#[test]
fn test_quantize_to_q8_blocks_not_multiple_of_32() {
    let values = vec![0.0f32; 33]; // Not multiple of 32
    let result = quantize_to_q8_blocks(&values);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("multiple of 32"));
}

#[test]
fn test_quantize_to_q8_blocks_success() {
    let values = vec![1.0f32; 64]; // 2 blocks
    let result = quantize_to_q8_blocks(&values);
    assert!(result.is_ok());
    let blocks = result.expect("test value should be present");
    assert_eq!(blocks.len(), 2);
}

#[test]
fn test_quantize_to_q8_blocks_empty() {
    let values: Vec<f32> = vec![];
    let result = quantize_to_q8_blocks(&values);
    assert!(result.is_ok());
    let blocks = result.expect("test value should be present");
    assert!(blocks.is_empty());
}

#[test]
fn test_quantize_to_q8_blocks_single_block() {
    let values = vec![0.5f32; 32];
    let result = quantize_to_q8_blocks(&values);
    assert!(result.is_ok());
    let blocks = result.expect("test value should be present");
    assert_eq!(blocks.len(), 1);
}

// =============================================================================
// dequantize_q8_blocks tests
// =============================================================================

#[test]
fn test_dequantize_q8_blocks_roundtrip() {
    let original = vec![0.5f32; 64];
    let blocks = quantize_to_q8_blocks(&original).expect("test value should be present");
    let dequantized = dequantize_q8_blocks(&blocks);

    assert_eq!(dequantized.len(), original.len());

    // Check approximate equality (quantization introduces error)
    for (o, d) in original.iter().zip(dequantized.iter()) {
        assert!((o - d).abs() < 0.1, "original={}, dequantized={}", o, d);
    }
}

#[test]
fn test_dequantize_q8_blocks_empty() {
    let blocks: Vec<Q8_0Block> = vec![];
    let result = dequantize_q8_blocks(&blocks);
    assert!(result.is_empty());
}

// =============================================================================
// InterleavedQ4K tests
// =============================================================================

#[test]
fn test_interleaved_q4k_invalid_size() {
    let data = vec![0u8; 100]; // Not multiple of 144
    let result = InterleavedQ4K::from_q4k(&data);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("not a multiple of super-block size"));
}

#[test]
fn test_interleaved_q4k_empty() {
    let data: Vec<u8> = vec![];
    let result = InterleavedQ4K::from_q4k(&data);
    assert!(result.is_ok());
    let interleaved = result.expect("test value should be present");
    assert_eq!(interleaved.num_super_blocks, 0);
    assert_eq!(interleaved.num_values(), 0);
}

#[test]
fn test_interleaved_q4k_single_superblock() {
    // Create valid Q4_K super-block data (144 bytes)
    let mut data = vec![0u8; 144];
    // Set d (f16 at offset 0-1) to some value
    data[0] = 0x00;
    data[1] = 0x3C; // f16 1.0

    // Set dmin (f16 at offset 2-3)
    data[2] = 0x00;
    data[3] = 0x00; // f16 0.0

    // Scales at offset 4-15 (12 bytes)
    // Quantized values at offset 16-143 (128 bytes)

    let result = InterleavedQ4K::from_q4k(&data);
    assert!(result.is_ok());
    let interleaved = result.expect("test value should be present");
    assert_eq!(interleaved.num_super_blocks, 1);
    assert_eq!(interleaved.num_values(), 256);
    assert_eq!(interleaved.d.len(), 1);
    assert_eq!(interleaved.dmin.len(), 1);
    assert_eq!(interleaved.scales.len(), 12);
    assert_eq!(interleaved.qs.len(), 128);
}

#[test]
fn test_interleaved_q4k_two_superblocks() {
    let data = vec![0u8; 288]; // 2 super-blocks
    let result = InterleavedQ4K::from_q4k(&data);
    assert!(result.is_ok());
    let interleaved = result.expect("test value should be present");
    assert_eq!(interleaved.num_super_blocks, 2);
    assert_eq!(interleaved.num_values(), 512);
}

#[test]
fn test_interleaved_q4k_dot_wrong_length() {
    let data = vec![0u8; 144];
    let interleaved = InterleavedQ4K::from_q4k(&data).expect("test value should be present");

    // Wrong activation length
    let activations = vec![1.0f32; 100]; // Should be 256
    let result = interleaved.dot(&activations);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("doesn't match"));
}

#[test]
fn test_interleaved_q4k_dot_success() {
    // Create Q4_K data with known values
    let mut data = vec![0u8; 144];
    // Set d to 1.0 (f16)
    data[0] = 0x00;
    data[1] = 0x3C;
    // Set some quantized values
    for i in 16..144 {
        data[i] = 0x55; // Arbitrary pattern
    }

    let interleaved = InterleavedQ4K::from_q4k(&data).expect("test value should be present");
    let activations = vec![1.0f32; 256];

    let result = interleaved.dot(&activations);
    assert!(result.is_ok());
    // Result should be finite
    assert!(result.expect("test value should be present").is_finite());
}

#[test]
fn test_interleaved_q4k_dot_zero_activations() {
    let data = vec![0u8; 144];
    let interleaved = InterleavedQ4K::from_q4k(&data).expect("test value should be present");

    let activations = vec![0.0f32; 256];
    let result = interleaved.dot(&activations).expect("test value should be present");
    assert_eq!(result, 0.0);
}

// =============================================================================
// fused_q4_0_q8_0_parallel_matvec tests
// =============================================================================

#[test]
fn test_fused_q4_0_q8_0_weight_too_small() {
    let weight_data = vec![0u8; 10]; // Too small
    let activations = vec![1.0f32; 32];

    let result = fused_q4_0_q8_0_parallel_matvec(&weight_data, &activations, 32, 1);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("weight data too small"));
}

#[test]
fn test_fused_q4_0_q8_0_activation_mismatch() {
    // Q4_0: 18 bytes per block of 32
    let weight_data = vec![0u8; 18]; // 1 row, 1 block
    let activations = vec![1.0f32; 64]; // Wrong size

    let result = fused_q4_0_q8_0_parallel_matvec(&weight_data, &activations, 32, 1);
    assert!(result.is_err());
    let err = result.unwrap_err().to_string();
    assert!(err.contains("doesn't match in_dim"));
}

#[test]
fn test_fused_q4_0_q8_0_sequential_path() {
    // Small matrix - sequential path (out_dim < 1024)
    let in_dim = 32;
    let out_dim = 4;
    let _blocks_per_row = 1;
    let bytes_per_row = 18; // 1 block

    let weight_data = vec![0u8; out_dim * bytes_per_row];
    let activations = vec![1.0f32; in_dim];

    let result = fused_q4_0_q8_0_parallel_matvec(&weight_data, &activations, in_dim, out_dim);
    assert!(result.is_ok());
    let output = result.expect("test value should be present");
    assert_eq!(output.len(), out_dim);
}

#[test]
fn test_fused_q4_0_q8_0_parallel_path() {
    // Large matrix - parallel path (out_dim >= 1024)
    let in_dim = 32;
    let out_dim = 2048;
    let bytes_per_row = 18; // 1 block

    let weight_data = vec![0u8; out_dim * bytes_per_row];
    let activations = vec![0.5f32; in_dim];

    let result = fused_q4_0_q8_0_parallel_matvec(&weight_data, &activations, in_dim, out_dim);
    assert!(result.is_ok());
    let output = result.expect("test value should be present");
    assert_eq!(output.len(), out_dim);
}

#[test]
fn test_fused_q4_0_q8_0_boundary_1024() {
    // Exactly at threshold
    let in_dim = 32;
    let out_dim = 1024;
    let bytes_per_row = 18;

    let weight_data = vec![0u8; out_dim * bytes_per_row];
    let activations = vec![1.0f32; in_dim];

    let result = fused_q4_0_q8_0_parallel_matvec(&weight_data, &activations, in_dim, out_dim);
    assert!(result.is_ok());
    let output = result.expect("test value should be present");
    assert_eq!(output.len(), out_dim);
}

// =============================================================================
// fused_q4_0_q8_0_parallel_matvec_into tests
// =============================================================================

#[test]
fn test_fused_q4_0_q8_0_into_weight_too_small() {
    let weight_data = vec![0u8; 10];
    let activations = vec![1.0f32; 32];
    let mut output = vec![0.0f32; 1];

    let result = fused_q4_0_q8_0_parallel_matvec_into(&weight_data, &activations, 32, &mut output);
    assert!(result.is_err());
}

#[test]
fn test_fused_q4_0_q8_0_into_activation_mismatch() {
    let weight_data = vec![0u8; 18];
    let activations = vec![1.0f32; 64];
    let mut output = vec![0.0f32; 1];

    let result = fused_q4_0_q8_0_parallel_matvec_into(&weight_data, &activations, 32, &mut output);
    assert!(result.is_err());
}

#[test]
fn test_fused_q4_0_q8_0_into_success() {
    let in_dim = 32;
    let out_dim = 4;
    let weight_data = vec![0u8; out_dim * 18];
    let activations = vec![1.0f32; in_dim];
    let mut output = vec![0.0f32; out_dim];

    let result =
        fused_q4_0_q8_0_parallel_matvec_into(&weight_data, &activations, in_dim, &mut output);
    assert!(result.is_ok());
}

include!("fused_03.rs");
include!("fused_02_03.rs");