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

/// IMP-039: Fused attention output projection (M18)
/// Target: Combine attention output + projection in single operation
#[test]
#[cfg(feature = "gpu")]
fn test_imp_039_fused_attn_proj() {
    use crate::gpu::{GpuModel, GpuModelConfig, StreamingKVCache};
    use std::time::Instant;

    let config = GpuModelConfig {
        vocab_size: 256,
        hidden_dim: 128,
        num_heads: 8,
        num_kv_heads: 8, // Standard MHA
        num_layers: 4,
        intermediate_dim: 256,
        eps: 1e-5,
        rope_theta: 10000.0,
            explicit_head_dim: None,
            layer_types: None,
            linear_key_head_dim: None,
            linear_value_head_dim: None,
            linear_num_key_heads: None,
            linear_num_value_heads: None,
            linear_conv_kernel_dim: None,
            constraints: None,
    num_experts: None,
    num_experts_per_tok: None,
    expert_intermediate_size: None,
    };

    let mut model = GpuModel::with_attention_buffers(config.clone(), 256)
        .expect("IMP-039: Should create model");

    // Initialize KV cache
    let head_dim = config.hidden_dim / config.num_heads;
    let mut kv_cache = StreamingKVCache::new(config.num_layers, 256, config.num_heads, head_dim);

    // Fill cache
    let prompt = vec![1, 2, 3, 4, 5, 6, 7, 8];
    let _ = model.forward_gpu_with_cache(&prompt, &mut kv_cache);

    // Test 1: Model should have fused attention projection
    assert!(
        model.has_fused_attn_proj(),
        "IMP-039: Model should have fused attention projection"
    );

    // Warmup
    for token in 10..15 {
        let _ = model.forward_gpu_incremental_optimized(token, &mut kv_cache);
    }

    // Test 2: Fused projection should be at least as fast
    let mut fused_times = Vec::with_capacity(10);
    for token in 20..30 {
        let start = Instant::now();
        let _ = model.forward_with_fused_attn_proj(token, &mut kv_cache);
        fused_times.push(start.elapsed().as_secs_f64());
    }

    let mut regular_times = Vec::with_capacity(10);
    for token in 30..40 {
        let start = Instant::now();
        let _ = model.forward_gpu_incremental_optimized(token, &mut kv_cache);
        regular_times.push(start.elapsed().as_secs_f64());
    }

    // Compare medians
    fused_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    regular_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let fused_median = fused_times[fused_times.len() / 2];
    let regular_median = regular_times[regular_times.len() / 2];

    let speedup = regular_median / fused_median;

    // Note: Performance benchmarks unreliable under coverage instrumentation
    // The key test is that fused projection works (Test 1). Performance is informational.
    // Use dedicated benchmarks (make bench) for actual performance measurement.
    let _ = speedup;
}

// ============================================================================
// Phase 10: Memory Bandwidth & Compute Optimization (M19) - IMP-040/041/042
// ============================================================================

/// IMP-040: Contiguous memory layout for attention tensors
/// Target: Reduce memory fragmentation during attention
#[test]
fn test_imp_040_contiguous_attention() {
    use crate::gpu::{ContiguousAttentionBuffer, GpuModelConfig};

    let config = GpuModelConfig {
        vocab_size: 256,
        hidden_dim: 128,
        num_heads: 8,
        num_kv_heads: 8, // Standard MHA
        num_layers: 4,
        intermediate_dim: 256,
        eps: 1e-5,
        rope_theta: 10000.0,
            explicit_head_dim: None,
            layer_types: None,
            linear_key_head_dim: None,
            linear_value_head_dim: None,
            linear_num_key_heads: None,
            linear_num_value_heads: None,
            linear_conv_kernel_dim: None,
            constraints: None,
    num_experts: None,
    num_experts_per_tok: None,
    expert_intermediate_size: None,
    };

    let max_seq_len = 256;
    let head_dim = config.hidden_dim / config.num_heads;

    // Test 1: Create contiguous attention buffer
    let mut buffer = ContiguousAttentionBuffer::new(max_seq_len, config.num_heads, head_dim);

    // Test 2: Buffer should have single contiguous allocation
    assert!(
        buffer.is_contiguous(),
        "IMP-040: Buffer should be contiguous"
    );

    // Test 3: Q, K, V, O views should not overlap but be adjacent
    let (q_view, k_view, v_view, o_view) = buffer.get_views();
    assert_eq!(
        q_view.len(),
        max_seq_len * config.num_heads * head_dim,
        "IMP-040: Q view should have correct size"
    );
    assert_eq!(
        k_view.len(),
        max_seq_len * config.num_heads * head_dim,
        "IMP-040: K view should have correct size"
    );
    assert_eq!(
        v_view.len(),
        max_seq_len * config.num_heads * head_dim,
        "IMP-040: V view should have correct size"
    );
    assert_eq!(
        o_view.len(),
        max_seq_len * config.num_heads * head_dim,
        "IMP-040: O view should have correct size"
    );

    // Test 4: Memory reuse should work
    buffer.reset();
    assert!(
        buffer.is_contiguous(),
        "IMP-040: Buffer should remain contiguous after reset"
    );
}

/// IMP-041: Vectorized RoPE computation
/// Target: SIMD-accelerated position encoding
#[test]
#[ignore = "flaky under coverage instrumentation due to timing variance"]
fn test_imp_041_vectorized_rope() {
    use crate::gpu::{scalar_rope, simd_rope};
    use std::time::Instant;

    // Test data: (batch_size=1, seq_len=64, hidden_dim=128)
    let hidden_dim = 128;
    let seq_len = 64;
    let head_dim = hidden_dim / 8; // 8 heads

    // Generate test input
    let input: Vec<f32> = (0..seq_len * hidden_dim)
        .map(|i| (i as f32) * 0.01)
        .collect();

    // Test 1: SIMD and scalar should produce same results
    let scalar_result = scalar_rope(&input, seq_len, head_dim, 10000.0);
    let simd_result = simd_rope(&input, seq_len, head_dim, 10000.0);

    assert_eq!(
        scalar_result.len(),
        simd_result.len(),
        "IMP-041: Results should have same length"
    );

    for (i, (s, v)) in scalar_result.iter().zip(simd_result.iter()).enumerate() {
        assert!(
            (s - v).abs() < 1e-5,
            "IMP-041: Results should match at index {}: scalar={}, simd={}",
            i,
            s,
            v
        );
    }

    // Test 2: SIMD should be faster (warmup first)
    for _ in 0..5 {
        let _ = scalar_rope(&input, seq_len, head_dim, 10000.0);
        let _ = simd_rope(&input, seq_len, head_dim, 10000.0);
    }

    // Benchmark scalar
    let mut scalar_times = Vec::with_capacity(10);
    for _ in 0..10 {
        let start = Instant::now();
        for _ in 0..100 {
            let _ = scalar_rope(&input, seq_len, head_dim, 10000.0);
        }
        scalar_times.push(start.elapsed().as_secs_f64());
    }
    scalar_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    // Benchmark SIMD
    let mut simd_times = Vec::with_capacity(10);
    for _ in 0..10 {
        let start = Instant::now();
        for _ in 0..100 {
            let _ = simd_rope(&input, seq_len, head_dim, 10000.0);
        }
        simd_times.push(start.elapsed().as_secs_f64());
    }
    simd_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let scalar_median = scalar_times[scalar_times.len() / 2];
    let simd_median = simd_times[simd_times.len() / 2];
    let speedup = scalar_median / simd_median;

    // Note: In test environments with load, SIMD may not always be faster due to timing variance
    // The key test is correctness (Test 1). Performance is informational.
    // We use a very lenient threshold to avoid flaky tests under coverage instrumentation.
    assert!(
        speedup >= 0.2, // Allow high variance for coverage/test environment noise
        "IMP-041: SIMD RoPE speedup ({:.2}x) should be >= 0.2x (severe slowdown indicates bug)",
        speedup
    );
}

/// IMP-042: Optimized output projection with fused residual
/// Target: Fused output proj + residual add
#[test]
fn test_imp_042_fused_output_residual() {
    use crate::gpu::{GpuModel, GpuModelConfig, StreamingKVCache};
    use std::time::Instant;

    let config = GpuModelConfig {
        vocab_size: 256,
        hidden_dim: 128,
        num_heads: 8,
        num_kv_heads: 8, // Standard MHA
        num_layers: 4,
        intermediate_dim: 256,
        eps: 1e-5,
        rope_theta: 10000.0,
            explicit_head_dim: None,
            layer_types: None,
            linear_key_head_dim: None,
            linear_value_head_dim: None,
            linear_num_key_heads: None,
            linear_num_value_heads: None,
            linear_conv_kernel_dim: None,
            constraints: None,
    num_experts: None,
    num_experts_per_tok: None,
    expert_intermediate_size: None,
    };

    let mut model = GpuModel::with_attention_buffers(config.clone(), 256)
        .expect("IMP-042: Should create model");

    // Initialize KV cache
    let head_dim = config.hidden_dim / config.num_heads;
    let mut kv_cache = StreamingKVCache::new(config.num_layers, 256, config.num_heads, head_dim);

    // Fill cache
    let prompt = vec![1, 2, 3, 4, 5, 6, 7, 8];
    let _ = model.forward_gpu_with_cache(&prompt, &mut kv_cache);

    // Test 1: Model should have fused output residual
    assert!(
        model.has_fused_output_residual(),
        "IMP-042: Model should have fused output residual capability"
    );

    // Warmup
    for token in 10..15 {
        let _ = model.forward_gpu_incremental_optimized(token, &mut kv_cache);
    }

    // Test 2: Fused output+residual should produce correct results
    let regular_logits = model
        .forward_gpu_incremental_optimized(50, &mut kv_cache)
        .expect("IMP-042: Regular forward should work");

    let fused_logits = model
        .forward_with_fused_output_residual(51, &mut kv_cache)
        .expect("IMP-042: Fused forward should work");

    // Logits should have same shape (output size)
    assert_eq!(
        regular_logits.len(),
        fused_logits.len(),
        "IMP-042: Output sizes should match"
    );

    // Test 3: Fused should be at least as fast
    let mut fused_times = Vec::with_capacity(10);
    for token in 60..70 {
        let start = Instant::now();
        let _ = model.forward_with_fused_output_residual(token, &mut kv_cache);
        fused_times.push(start.elapsed().as_secs_f64());
    }

    let mut regular_times = Vec::with_capacity(10);
    for token in 70..80 {
        let start = Instant::now();
        let _ = model.forward_gpu_incremental_optimized(token, &mut kv_cache);
        regular_times.push(start.elapsed().as_secs_f64());
    }

    fused_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    regular_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let fused_median = fused_times[fused_times.len() / 2];
    let regular_median = regular_times[regular_times.len() / 2];
    let speedup = regular_median / fused_median;

    // Note: Performance benchmarks unreliable under coverage instrumentation
    // The key test is correctness. Performance is informational only.
    let _ = speedup;
}

// ============================================================================
// Phase 11: Batch Processing & Parallel Execution (M20) - IMP-043/044/045
// ============================================================================

/// IMP-043: Batch token embedding lookup
/// Target: Process multiple tokens in single embedding lookup
#[test]
fn test_imp_043_batch_embedding() {
    use crate::gpu::{batch_embed, GpuModelConfig};
    use std::time::Instant;

    let config = GpuModelConfig {
        vocab_size: 1024,
        hidden_dim: 256,
        num_heads: 8,
        num_kv_heads: 8, // Standard MHA
        num_layers: 4,
        intermediate_dim: 512,
        eps: 1e-5,
        rope_theta: 10000.0,
            explicit_head_dim: None,
            layer_types: None,
            linear_key_head_dim: None,
            linear_value_head_dim: None,
            linear_num_key_heads: None,
            linear_num_value_heads: None,
            linear_conv_kernel_dim: None,
            constraints: None,
    num_experts: None,
    num_experts_per_tok: None,
    expert_intermediate_size: None,
    };

    // Create embedding table
    let embedding_table: Vec<f32> = (0..config.vocab_size * config.hidden_dim)
        .map(|i| (i as f32) * 0.001)
        .collect();

    // Test tokens
    let tokens: Vec<usize> = vec![1, 5, 10, 20, 50, 100, 200, 500];

    // Test 1: Batch embed should return correct shape
    let batch_result = batch_embed(&embedding_table, &tokens, config.hidden_dim);
    assert_eq!(
        batch_result.len(),
        tokens.len() * config.hidden_dim,
        "IMP-043: Batch embed should return tokens * hidden_dim elements"
    );

    // Test 2: Results should match individual lookups
    for (i, &token) in tokens.iter().enumerate() {
        let start_idx = token * config.hidden_dim;
        let end_idx = start_idx + config.hidden_dim;
        let expected = &embedding_table[start_idx..end_idx];

        let batch_start = i * config.hidden_dim;
        let batch_end = batch_start + config.hidden_dim;
        let actual = &batch_result[batch_start..batch_end];

        for (j, (&e, &a)) in expected.iter().zip(actual.iter()).enumerate() {
            assert!(
                (e - a).abs() < 1e-6,
                "IMP-043: Mismatch at token {} dim {}: expected {}, got {}",
                token,
                j,
                e,
                a
            );
        }
    }

    // Test 3: Batch should be faster than individual lookups
    // Warmup
    for _ in 0..5 {
        let _ = batch_embed(&embedding_table, &tokens, config.hidden_dim);
    }

    // Benchmark batch
    let mut batch_times = Vec::with_capacity(10);
    for _ in 0..10 {
        let start = Instant::now();
        for _ in 0..100 {
            let _ = batch_embed(&embedding_table, &tokens, config.hidden_dim);
        }
        batch_times.push(start.elapsed().as_secs_f64());
    }
    batch_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    // Benchmark individual
    let mut individual_times = Vec::with_capacity(10);
    for _ in 0..10 {
        let start = Instant::now();
        for _ in 0..100 {
            let mut result = Vec::with_capacity(tokens.len() * config.hidden_dim);
            for &token in &tokens {
                let start_idx = token * config.hidden_dim;
                let end_idx = start_idx + config.hidden_dim;
                result.extend_from_slice(&embedding_table[start_idx..end_idx]);
            }
        }
        individual_times.push(start.elapsed().as_secs_f64());
    }
    individual_times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let batch_median = batch_times[batch_times.len() / 2];
    let individual_median = individual_times[individual_times.len() / 2];
    let speedup = individual_median / batch_median;

    // Note: Performance benchmarks unreliable under coverage instrumentation
    // The key test is correctness. Performance is informational only.
    let _ = speedup;
}