aprender-serve 0.65.2

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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! T-COV-95 Phase 50: Deep coverage for apr_transformer/mod.rs
//!
//! Covers:
//! - ActivationStats::from_slice: empty, normal, NaN/Inf handling, all-zeros, single element
//! - dequant_perrow: aligned, padded, truncated data
//! - dequant_q6k_block: zero block, non-zero block
//! - dequant_q4k_block: zero block, non-zero block
//! - LayerActivation construction
//! - ForwardTrace construction
//! - AprTransformerConfig serialization roundtrip

// Import from apr_transformer module (two levels up: tests/mod.rs -> apr_transformer/mod.rs)
use crate::apr_transformer::{
    ActivationStats, AprTransformerConfig, ForwardTrace, LayerActivation,
};

// Private functions accessible via super::super (within the same crate module)
use super::super::{dequant_perrow, dequant_q4k_block, dequant_q6k_block};

// ============================================================================
// ActivationStats::from_slice
// ============================================================================

#[test]
fn test_activation_stats_empty() {
    let stats = ActivationStats::from_slice(&[]);
    assert_eq!(stats.count, 0);
    assert_eq!(stats.nan_count, 0);
    assert_eq!(stats.inf_count, 0);
    assert_eq!(stats.zero_count, 0);
    assert_eq!(stats.mean, 0.0);
    assert_eq!(stats.std_dev, 0.0);
}

#[test]
fn test_activation_stats_single_element() {
    let stats = ActivationStats::from_slice(&[42.0]);
    assert_eq!(stats.count, 1);
    assert!((stats.min - 42.0).abs() < 0.001);
    assert!((stats.max - 42.0).abs() < 0.001);
    assert!((stats.mean - 42.0).abs() < 0.001);
    assert_eq!(stats.std_dev, 0.0); // Single element -> std_dev = 0
    assert_eq!(stats.nan_count, 0);
    assert_eq!(stats.inf_count, 0);
    assert_eq!(stats.zero_count, 0);
}

#[test]
fn test_activation_stats_basic() {
    let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
    let stats = ActivationStats::from_slice(&data);
    assert_eq!(stats.count, 5);
    assert!((stats.min - 1.0).abs() < 0.001);
    assert!((stats.max - 5.0).abs() < 0.001);
    assert!((stats.mean - 3.0).abs() < 0.001);
    // std_dev of [1,2,3,4,5] = sqrt(2.5) ~= 1.5811
    assert!((stats.std_dev - 1.5811).abs() < 0.01);
    assert_eq!(stats.nan_count, 0);
    assert_eq!(stats.inf_count, 0);
    assert_eq!(stats.zero_count, 0);
}

#[test]
fn test_activation_stats_with_nan() {
    let data = vec![1.0, f32::NAN, 3.0, f32::NAN, 5.0];
    let stats = ActivationStats::from_slice(&data);
    assert_eq!(stats.count, 5);
    assert_eq!(stats.nan_count, 2);
    assert_eq!(stats.inf_count, 0);
    // Mean computed over valid values only: (1 + 3 + 5) / 3 = 3.0
    assert!((stats.mean - 3.0).abs() < 0.001);
}

#[test]
fn test_activation_stats_with_inf() {
    let data = vec![1.0, f32::INFINITY, 3.0, f32::NEG_INFINITY, 5.0];
    let stats = ActivationStats::from_slice(&data);
    assert_eq!(stats.count, 5);
    assert_eq!(stats.nan_count, 0);
    assert_eq!(stats.inf_count, 2);
    // Mean computed over valid values: (1 + 3 + 5) / 3 = 3.0
    assert!((stats.mean - 3.0).abs() < 0.001);
}

#[test]
fn test_activation_stats_all_nan() {
    let data = vec![f32::NAN, f32::NAN, f32::NAN];
    let stats = ActivationStats::from_slice(&data);
    assert_eq!(stats.count, 3);
    assert_eq!(stats.nan_count, 3);
    assert_eq!(stats.mean, 0.0); // No valid values -> mean=0
    assert_eq!(stats.std_dev, 0.0);
}

#[test]
fn test_activation_stats_all_zeros() {
    let data = vec![0.0; 10];
    let stats = ActivationStats::from_slice(&data);
    assert_eq!(stats.count, 10);
    assert_eq!(stats.zero_count, 10);
    assert!((stats.min - 0.0).abs() < 0.001);
    assert!((stats.max - 0.0).abs() < 0.001);
    assert!((stats.mean - 0.0).abs() < 0.001);
    assert!((stats.std_dev - 0.0).abs() < 0.001);
}

#[test]
fn test_activation_stats_negative_values() {
    let data = vec![-5.0, -3.0, -1.0, 0.0, 2.0, 4.0];
    let stats = ActivationStats::from_slice(&data);
    assert_eq!(stats.count, 6);
    assert!((stats.min - (-5.0)).abs() < 0.001);
    assert!((stats.max - 4.0).abs() < 0.001);
    // Mean: (-5 + -3 + -1 + 0 + 2 + 4) / 6 = -3/6 = -0.5
    assert!((stats.mean - (-0.5)).abs() < 0.001);
    assert_eq!(stats.zero_count, 1);
}

#[test]
fn test_activation_stats_large_values() {
    let data = vec![1e30, -1e30, 1e-30, -1e-30];
    let stats = ActivationStats::from_slice(&data);
    assert_eq!(stats.count, 4);
    assert_eq!(stats.nan_count, 0);
    assert_eq!(stats.inf_count, 0);
    assert!(stats.min < -1e29);
    assert!(stats.max > 1e29);
}

#[test]
fn test_activation_stats_default() {
    let stats = ActivationStats::default();
    assert_eq!(stats.count, 0);
    assert_eq!(stats.nan_count, 0);
    assert_eq!(stats.inf_count, 0);
    assert_eq!(stats.zero_count, 0);
    assert_eq!(stats.mean, 0.0);
    assert_eq!(stats.std_dev, 0.0);
    assert_eq!(stats.min, 0.0);
    assert_eq!(stats.max, 0.0);
}

#[test]
fn test_activation_stats_clone() {
    let data = vec![1.0, 2.0, 3.0];
    let stats = ActivationStats::from_slice(&data);
    let cloned = stats.clone();
    assert_eq!(cloned.count, stats.count);
    assert!((cloned.mean - stats.mean).abs() < 0.001);
    assert!((cloned.std_dev - stats.std_dev).abs() < 0.001);
}

#[test]
fn test_activation_stats_debug() {
    let stats = ActivationStats::from_slice(&[1.0, 2.0]);
    let debug_str = format!("{:?}", stats);
    assert!(debug_str.contains("ActivationStats"));
}

// ============================================================================
// dequant_q4k_block
// ============================================================================

#[test]
fn test_dequant_q4k_block_zeros() {
    let block = vec![0u8; 144]; // All zeros
    let mut out = vec![0.0f32; 256];
    dequant_q4k_block(&block, &mut out);
    // When d=0 and dmin=0, all values should be 0
    for &v in &out {
        assert!((v - 0.0).abs() < 0.001, "Expected 0.0, got {}", v);
    }
}

#[test]
fn test_dequant_q4k_block_nonzero_d() {
    let mut block = vec![0u8; 144];
    // Set d = 1.0 (f16: 0x3C00)
    block[0..2].copy_from_slice(&0x3C00u16.to_le_bytes());
    // Set dmin = 0
    block[2..4].copy_from_slice(&0x0000u16.to_le_bytes());
    // Set scale[0] = 1 (for first block of 64)
    block[4] = 0x01;
    // Set some qs to nonzero (nibbles)
    for i in 0..128 {
        block[16 + i] = 0x55; // low=5, high=5
    }
    let mut out = vec![0.0f32; 256];
    dequant_q4k_block(&block, &mut out);
    // At least some values should be nonzero
    let nonzero_count = out.iter().filter(|&&v| v.abs() > 0.001).count();
    assert!(
        nonzero_count > 0,
        "Expected some nonzero values from dequant_q4k_block"
    );
}

#[test]
fn test_dequant_q4k_block_output_length() {
    let block = vec![0u8; 144];
    let mut out = vec![-1.0f32; 256];
    dequant_q4k_block(&block, &mut out);
    // Should write exactly 256 values
    assert_eq!(out.len(), 256);
}

// ============================================================================
// dequant_q6k_block
// ============================================================================

#[test]
fn test_dequant_q6k_block_zeros() {
    let block = vec![0u8; 210]; // All zeros
    let mut out = vec![0.0f32; 256];
    dequant_q6k_block(&block, &mut out);
    // When d=0 (in f16 at bytes 208-209), all values should be 0
    for &v in &out {
        assert!((v - 0.0).abs() < 0.001, "Expected 0.0, got {}", v);
    }
}

#[test]
fn test_dequant_q6k_block_nonzero_d() {
    let mut block = vec![0u8; 210];
    // Set d = 1.0 (f16: 0x3C00) at offset 208
    block[208..210].copy_from_slice(&0x3C00u16.to_le_bytes());
    // Set some scale values
    block[192] = 1; // scale[0] = 1
    block[193] = 1;
    // Set some ql values to nonzero
    for i in 0..128 {
        block[i] = 0x33; // low nibble = 3
    }
    let mut out = vec![0.0f32; 256];
    dequant_q6k_block(&block, &mut out);
    // Some values should be nonzero (d=1.0, scale=1, some q values)
    let nonzero_count = out.iter().filter(|&&v| v.abs() > 0.001).count();
    assert!(
        nonzero_count > 0,
        "Expected some nonzero values from dequant_q6k_block"
    );
}

#[test]
fn test_dequant_q6k_block_output_length() {
    let block = vec![0u8; 210];
    let mut out = vec![-1.0f32; 256];
    dequant_q6k_block(&block, &mut out);
    assert_eq!(out.len(), 256);
}

// ============================================================================
// dequant_perrow
// ============================================================================

#[test]
fn test_dequant_perrow_aligned() {
    // 2 rows, 256 cols (1 block per row) for Q4K (144 bytes/block, 256 values/block)
    let block_bytes = 144;
    let block_elems = 256;
    let rows = 2;
    let cols = 256;
    let data = vec![0u8; rows * block_bytes]; // all zeros
    let dims = vec![rows, cols];

    let result = dequant_perrow(&data, &dims, block_elems, block_bytes, |block, out| {
        // Simple dequant: just fill with 1.0
        for v in out.iter_mut() {
            *v = if block[0] == 0 { 0.0 } else { 1.0 };
        }
    });

    assert_eq!(result.len(), rows * cols);
}

#[test]
fn test_dequant_perrow_padded() {
    // 2 rows, 128 cols but block_elems=256, so each row is padded to 256
    let block_bytes = 144;
    let block_elems = 256;
    let rows = 2;
    let cols: usize = 128; // less than block_elems
    let blocks_per_row = cols.div_ceil(block_elems); // 1
    let data = vec![0u8; rows * blocks_per_row * block_bytes];
    let dims = vec![rows, cols];

    let result = dequant_perrow(&data, &dims, block_elems, block_bytes, |_block, out| {
        for (i, v) in out.iter_mut().enumerate() {
            *v = i as f32;
        }
    });

    // Should have rows * cols values (padding stripped)
    assert_eq!(result.len(), rows * cols);
    // First row: values 0..128
    for i in 0..cols {
        assert!((result[i] - i as f32).abs() < 0.001);
    }
}

#[test]
fn test_dequant_perrow_truncated_data() {
    // Data is shorter than expected - should fill remaining with zeros
    let block_bytes = 144;
    let block_elems = 256;
    let rows = 2;
    let cols = 256;
    let data = vec![0u8; block_bytes]; // Only enough for 1 row
    let dims = vec![rows, cols];

    let result = dequant_perrow(&data, &dims, block_elems, block_bytes, |_block, out| {
        for v in out.iter_mut() {
            *v = 42.0;
        }
    });

    // Should still return rows*cols values (second row padded with zeros)
    assert_eq!(result.len(), rows * cols);
}

// ============================================================================
// LayerActivation construction
// ============================================================================

#[test]
fn test_layer_activation_construction() {
    let stats = ActivationStats::from_slice(&[1.0, 2.0, 3.0]);
    let layer_act = LayerActivation {
        layer_idx: 0,
        attn_norm_stats: stats.clone(),
        qkv_stats: stats.clone(),
        attn_out_stats: stats.clone(),
        ffn_norm_stats: stats.clone(),
        ffn_gate_stats: ActivationStats::default(),
        ffn_up_stats: ActivationStats::default(),
        ffn_silu_gate_stats: ActivationStats::default(),
        ffn_swiglu_inner_stats: ActivationStats::default(),
        ffn_out_stats: stats.clone(),
        output_stats: stats,
        last_token: None,
    };
    assert_eq!(layer_act.layer_idx, 0);
    assert_eq!(layer_act.attn_norm_stats.count, 3);
    assert_eq!(layer_act.output_stats.count, 3);
}

#[test]
fn test_layer_activation_debug() {
    let stats = ActivationStats::default();
    let layer_act = LayerActivation {
        layer_idx: 5,
        attn_norm_stats: stats.clone(),
        qkv_stats: stats.clone(),
        attn_out_stats: stats.clone(),
        ffn_norm_stats: stats.clone(),
        ffn_gate_stats: ActivationStats::default(),
        ffn_up_stats: ActivationStats::default(),
        ffn_silu_gate_stats: ActivationStats::default(),
        ffn_swiglu_inner_stats: ActivationStats::default(),
        ffn_out_stats: stats.clone(),
        output_stats: stats,
        last_token: None,
    };
    let debug_str = format!("{:?}", layer_act);
    assert!(debug_str.contains("LayerActivation"));
}

#[test]
fn test_layer_activation_clone() {
    let stats = ActivationStats::from_slice(&[1.0]);
    let layer_act = LayerActivation {
        layer_idx: 3,
        attn_norm_stats: stats.clone(),
        qkv_stats: stats.clone(),
        attn_out_stats: stats.clone(),
        ffn_norm_stats: stats.clone(),
        ffn_gate_stats: ActivationStats::default(),
        ffn_up_stats: ActivationStats::default(),
        ffn_silu_gate_stats: ActivationStats::default(),
        ffn_swiglu_inner_stats: ActivationStats::default(),
        ffn_out_stats: stats.clone(),
        output_stats: stats,
        last_token: None,
    };
    let cloned = layer_act.clone();
    assert_eq!(cloned.layer_idx, 3);
}

// ============================================================================
// ForwardTrace construction
// ============================================================================

#[test]
fn test_forward_trace_construction() {
    let stats = ActivationStats::from_slice(&[1.0, 2.0]);
    let trace = ForwardTrace {
        input_tokens: vec![1, 2, 3],
        embed_stats: stats.clone(),
        layer_activations: vec![],
        final_norm_stats: stats.clone(),
        logits_stats: stats,
        logits: vec![0.1, 0.2, 0.7],
    };
    assert_eq!(trace.input_tokens, vec![1, 2, 3]);
    assert!(trace.layer_activations.is_empty());
    assert_eq!(trace.logits.len(), 3);
}

#[test]
fn test_forward_trace_with_layers() {
    let stats = ActivationStats::default();
    let layer_act = LayerActivation {
        layer_idx: 0,
        attn_norm_stats: stats.clone(),
        qkv_stats: stats.clone(),
        attn_out_stats: stats.clone(),
        ffn_norm_stats: stats.clone(),
        ffn_gate_stats: ActivationStats::default(),
        ffn_up_stats: ActivationStats::default(),
        ffn_silu_gate_stats: ActivationStats::default(),
        ffn_swiglu_inner_stats: ActivationStats::default(),
        ffn_out_stats: stats.clone(),
        output_stats: stats.clone(),
        last_token: None,
    };
    let trace = ForwardTrace {
        input_tokens: vec![1],
        embed_stats: stats.clone(),
        layer_activations: vec![layer_act],
        final_norm_stats: stats.clone(),
        logits_stats: stats,
        logits: vec![0.5],
    };
    assert_eq!(trace.layer_activations.len(), 1);
    assert_eq!(trace.layer_activations[0].layer_idx, 0);
}

#[test]
fn test_forward_trace_debug() {
    let stats = ActivationStats::default();
    let trace = ForwardTrace {
        input_tokens: vec![1],
        embed_stats: stats.clone(),
        layer_activations: vec![],
        final_norm_stats: stats.clone(),
        logits_stats: stats,
        logits: vec![],
    };
    let debug_str = format!("{:?}", trace);
    assert!(debug_str.contains("ForwardTrace"));
}

#[test]
fn test_forward_trace_clone() {
    let stats = ActivationStats::default();
    let trace = ForwardTrace {
        input_tokens: vec![1, 2],
        embed_stats: stats.clone(),
        layer_activations: vec![],
        final_norm_stats: stats.clone(),
        logits_stats: stats,
        logits: vec![0.3, 0.7],
    };
    let cloned = trace.clone();
    assert_eq!(cloned.input_tokens, vec![1, 2]);
    assert_eq!(cloned.logits.len(), 2);
}