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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Phase 50 - KV Cache Scheduler Coverage Tests (gpu/scheduler/kv.rs)
//!
//! Comprehensive tests for KV cache operations:
//! - forward_gpu_with_cache: Forward pass with KV cache population
//! - forward_gpu_incremental: Incremental forward using cached KV
//! - generate_with_cache: Full generation loop with KV caching
//! - apply_rope: Rotary position embedding (internal)
//! - gqa_attention_with_kv / gqa_incremental_attention: GQA attention patterns
//!
//! Strategy: Use MockExecutor for CPU logic paths without GPU hardware.

use crate::gpu::executor::MockExecutor;
use crate::gpu::scheduler::{
    AttentionBuffers, BlockWeights, GpuGenerateConfig, GpuModel, GpuModelConfig, WeightType,
};
use crate::gpu::StreamingKVCache;

// ============================================================================
// Test Helpers
// ============================================================================

/// Create a minimal test model configuration (MHA - Multi-Head Attention)
fn create_kv_test_config() -> GpuModelConfig {
    GpuModelConfig {
        hidden_dim: 64,
        intermediate_dim: 128,
        num_layers: 2,
        num_heads: 4,
        num_kv_heads: 4, // MHA (not GQA)
        vocab_size: 100,
        eps: 1e-5,
        rope_theta: 10000.0,
            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 a GQA test configuration (num_heads > num_kv_heads)
fn create_kv_gqa_config() -> GpuModelConfig {
    GpuModelConfig {
        hidden_dim: 64,
        intermediate_dim: 128,
        num_layers: 2,
        num_heads: 8,
        num_kv_heads: 2, // GQA: 4 Q heads per KV head
        vocab_size: 100,
        eps: 1e-5,
        rope_theta: 10000.0,
            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 a single-layer config for fast iteration tests
fn create_kv_single_layer_config() -> GpuModelConfig {
    GpuModelConfig {
        hidden_dim: 32,
        intermediate_dim: 64,
        num_layers: 1,
        num_heads: 2,
        num_kv_heads: 2,
        vocab_size: 50,
        eps: 1e-5,
        rope_theta: 10000.0,
            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 a deeper model for stress testing
fn create_kv_deep_config() -> GpuModelConfig {
    GpuModelConfig {
        hidden_dim: 64,
        intermediate_dim: 128,
        num_layers: 4,
        num_heads: 4,
        num_kv_heads: 4,
        vocab_size: 100,
        eps: 1e-5,
        rope_theta: 10000.0,
            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,
    }
}

// ============================================================================
// StreamingKVCache Construction Tests
// ============================================================================

#[test]
fn test_kv_cache_new_basic() {
    let config = create_kv_test_config();
    let cache = StreamingKVCache::new(
        config.num_layers,
        256,
        config.num_kv_heads,
        config.head_dim(),
    );

    assert_eq!(cache.len(), 0);
    assert!(cache.is_empty());
    assert_eq!(cache.max_positions(), 256);
}

#[test]
fn test_kv_cache_new_gqa() {
    let config = create_kv_gqa_config();
    let cache = StreamingKVCache::new(
        config.num_layers,
        512,
        config.num_kv_heads,
        config.head_dim(),
    );

    assert!(cache.is_empty());
    assert_eq!(cache.max_positions(), 512);
}

#[test]
fn test_kv_cache_memory_calculation() {
    let config = create_kv_test_config();
    let max_positions = 128;
    let cache = StreamingKVCache::new(
        config.num_layers,
        max_positions,
        config.num_kv_heads,
        config.head_dim(),
    );

    // Memory = num_layers * max_positions * num_kv_heads * head_dim * 2 (K+V) * 4 bytes
    let expected = config.num_layers * max_positions * config.num_kv_heads * config.head_dim() * 2 * 4;
    assert_eq!(cache.memory_bytes(), expected);
}

#[test]
fn test_kv_cache_memory_mb_calculation() {
    let config = create_kv_test_config();
    let cache = StreamingKVCache::new(
        config.num_layers,
        1024,
        config.num_kv_heads,
        config.head_dim(),
    );

    let bytes = cache.memory_bytes();
    let expected_mb = bytes as f64 / (1024.0 * 1024.0);
    assert!((cache.memory_mb() - expected_mb).abs() < 0.001);
}

// ============================================================================
// StreamingKVCache Append and Get Tests
// ============================================================================

#[test]
fn test_kv_cache_append_single_position() {
    let config = create_kv_test_config();
    let mut cache = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    let kv_dim = config.num_kv_heads * config.head_dim();
    let key = vec![1.0f32; kv_dim];
    let value = vec![2.0f32; kv_dim];

    // Append to all layers for one position
    for layer in 0..config.num_layers {
        cache.append(layer, &key, &value);
    }

    assert_eq!(cache.len(), 1);
    assert!(!cache.is_empty());
}

#[test]
fn test_kv_cache_append_multiple_positions() {
    let config = create_kv_test_config();
    let mut cache = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    let kv_dim = config.num_kv_heads * config.head_dim();

    // Append 5 positions
    for pos in 0..5 {
        let key = vec![pos as f32; kv_dim];
        let value = vec![(pos as f32) * 2.0; kv_dim];

        for layer in 0..config.num_layers {
            cache.append(layer, &key, &value);
        }
    }

    assert_eq!(cache.len(), 5);
}

#[test]
fn test_kv_cache_get_valid_single_layer() {
    let config = create_kv_single_layer_config();
    let mut cache = StreamingKVCache::new(
        config.num_layers,
        32,
        config.num_kv_heads,
        config.head_dim(),
    );

    let kv_dim = config.num_kv_heads * config.head_dim();
    let key = vec![1.0f32; kv_dim];
    let value = vec![2.0f32; kv_dim];

    cache.append(0, &key, &value);

    let (keys, values) = cache.get_valid(0);
    assert_eq!(keys.len(), kv_dim);
    assert_eq!(values.len(), kv_dim);

    // Verify values
    assert!(keys.iter().all(|&k| (k - 1.0).abs() < 1e-6));
    assert!(values.iter().all(|&v| (v - 2.0).abs() < 1e-6));
}

#[test]
fn test_kv_cache_get_range() {
    let config = create_kv_test_config();
    let mut cache = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    let kv_dim = config.num_kv_heads * config.head_dim();

    // Append 10 positions
    for pos in 0..10 {
        let key = vec![pos as f32; kv_dim];
        let value = vec![(pos as f32) * 10.0; kv_dim];

        for layer in 0..config.num_layers {
            cache.append(layer, &key, &value);
        }
    }

    // Get range 2..5
    let (keys, values) = cache.get_range(0, 2, 5);
    assert_eq!(keys.len(), 3 * kv_dim);
    assert_eq!(values.len(), 3 * kv_dim);

    // First element should be from position 2
    assert!((keys[0] - 2.0).abs() < 1e-6);
    // Last element should be from position 4
    assert!((keys[(2 * kv_dim)] - 4.0).abs() < 1e-6);
}

#[test]
fn test_kv_cache_clear() {
    let config = create_kv_test_config();
    let mut cache = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    let kv_dim = config.num_kv_heads * config.head_dim();
    let key = vec![1.0f32; kv_dim];
    let value = vec![2.0f32; kv_dim];

    // Add some positions
    for _ in 0..5 {
        for layer in 0..config.num_layers {
            cache.append(layer, &key, &value);
        }
    }
    assert_eq!(cache.len(), 5);

    // Clear
    cache.clear();
    assert_eq!(cache.len(), 0);
    assert!(cache.is_empty());
}

#[test]
fn test_kv_cache_circular_buffer_wraparound() {
    let config = create_kv_single_layer_config();
    let max_positions = 4;
    let mut cache = StreamingKVCache::new(
        config.num_layers,
        max_positions,
        config.num_kv_heads,
        config.head_dim(),
    );

    let kv_dim = config.num_kv_heads * config.head_dim();

    // Append more positions than max_positions (should wrap around)
    for pos in 0..10 {
        let key = vec![pos as f32; kv_dim];
        let value = vec![(pos as f32) * 10.0; kv_dim];

        for layer in 0..config.num_layers {
            cache.append(layer, &key, &value);
        }
    }

    // Cache should be capped at max_positions
    assert_eq!(cache.len(), max_positions);
}

// ============================================================================
// forward_gpu_with_cache Tests
// ============================================================================

#[test]
fn test_forward_gpu_with_cache_single_token() {
    let config = create_kv_single_layer_config();
    let mut model = GpuModel::new(config.clone()).expect("test value should be present");

    let mock = MockExecutor::new("forward_with_cache_single");
    model.with_test_executor(Box::new(mock));

    let mut kv_cache = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    let token_ids = vec![1];
    let result = model.forward_gpu_with_cache(&token_ids, &mut kv_cache);

    assert!(result.is_ok());
    let logits = result.expect("test value should be present");
    assert_eq!(logits.len(), config.vocab_size);

    // Cache should have 1 position after forward
    assert_eq!(kv_cache.len(), 1);
}

#[test]
fn test_forward_gpu_with_cache_multiple_tokens() {
    let config = create_kv_test_config();
    let mut model = GpuModel::new(config.clone()).expect("test value should be present");

    let mock = MockExecutor::new("forward_with_cache_multi");
    model.with_test_executor(Box::new(mock));

    let mut kv_cache = StreamingKVCache::new(
        config.num_layers,
        128,
        config.num_kv_heads,
        config.head_dim(),
    );

    let token_ids = vec![1, 2, 3, 4, 5];
    let result = model.forward_gpu_with_cache(&token_ids, &mut kv_cache);

    assert!(result.is_ok());
    let logits = result.expect("test value should be present");
    // Output is only for final position
    assert_eq!(logits.len(), config.vocab_size);

    // Cache should have all positions
    assert_eq!(kv_cache.len(), token_ids.len());
}

#[test]
fn test_forward_gpu_with_cache_empty_tokens_error() {
    let config = create_kv_single_layer_config();
    let mut model = GpuModel::new(config.clone()).expect("test value should be present");

    let mut kv_cache = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    let token_ids: Vec<usize> = vec![];
    let result = model.forward_gpu_with_cache(&token_ids, &mut kv_cache);

    assert!(result.is_err());
}

#[test]
fn test_forward_gpu_with_cache_out_of_bounds_token_error() {
    let config = create_kv_single_layer_config();
    let mut model = GpuModel::new(config.clone()).expect("test value should be present");

    let mut kv_cache = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    // vocab_size is 50, so token 999 is out of bounds
    let token_ids = vec![1, 999];
    let result = model.forward_gpu_with_cache(&token_ids, &mut kv_cache);

    assert!(result.is_err());
}

#[test]
fn test_forward_gpu_with_cache_gqa_model() {
    let config = create_kv_gqa_config();
    let mut model = GpuModel::new(config.clone()).expect("test value should be present");

    let mock = MockExecutor::new("forward_gqa_cache");
    model.with_test_executor(Box::new(mock));

    let mut kv_cache = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    let token_ids = vec![1, 2, 3];
    let result = model.forward_gpu_with_cache(&token_ids, &mut kv_cache);

    assert!(result.is_ok());
    assert_eq!(kv_cache.len(), 3);
}

include!("forward_gpu.rs");
include!("gqa_cache.rs");