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

// ============================================================================
// forward_gpu_incremental Tests
// ============================================================================

#[test]
fn test_forward_gpu_incremental_basic() {
    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_incremental");
    model.with_test_executor(Box::new(mock));

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

    // First populate cache
    let _ = model.forward_gpu_with_cache(&[1, 2], &mut kv_cache);
    assert_eq!(kv_cache.len(), 2);

    // Now do incremental forward
    let result = model.forward_gpu_incremental(3, &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 now have 3 positions
    assert_eq!(kv_cache.len(), 3);
}

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

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

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

    // Initial forward
    let _ = model.forward_gpu_with_cache(&[1], &mut kv_cache);

    // Multiple incremental forwards
    for token in [2, 3, 4, 5] {
        let result = model.forward_gpu_incremental(token, &mut kv_cache);
        assert!(result.is_ok());
    }

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

#[test]
fn test_forward_gpu_incremental_out_of_bounds_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 9999 is out of bounds
    let result = model.forward_gpu_incremental(9999, &mut kv_cache);

    assert!(result.is_err());
}

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

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

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

    // Initial forward
    let _ = model.forward_gpu_with_cache(&[1, 2], &mut kv_cache);

    // Incremental forward with GQA
    let result = model.forward_gpu_incremental(3, &mut kv_cache);
    assert!(result.is_ok());
    assert_eq!(kv_cache.len(), 3);
}

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

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

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

    // Initial forward
    let _ = model.forward_gpu_with_cache(&[1], &mut kv_cache);

    // Incremental forward through 4 layers
    let result = model.forward_gpu_incremental(2, &mut kv_cache);
    assert!(result.is_ok());
}

// ============================================================================
// generate_with_cache Tests
// ============================================================================

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

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

    let gen_config = GpuGenerateConfig::deterministic(3);
    let prompt = vec![1, 2];
    let result = model.generate_with_cache(&prompt, &gen_config);

    assert!(result.is_ok());
    let tokens = result.expect("test value should be present");
    // Should have at least prompt tokens
    assert!(tokens.len() >= prompt.len());
}

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

    let gen_config = GpuGenerateConfig::deterministic(5);
    let prompt: Vec<usize> = vec![];
    let result = model.generate_with_cache(&prompt, &gen_config);

    assert!(result.is_err());
}

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

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

    // Mock returns zeros, so token 0 will be generated and should stop
    let gen_config = GpuGenerateConfig::deterministic(10).with_stop_tokens(vec![0]);
    let prompt = vec![1];
    let result = model.generate_with_cache(&prompt, &gen_config);

    assert!(result.is_ok());
}

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

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

    let gen_config = GpuGenerateConfig::with_sampling(3, 0.8, 5);
    let prompt = vec![1, 2];
    let result = model.generate_with_cache(&prompt, &gen_config);

    assert!(result.is_ok());
}

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

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

    let gen_config = GpuGenerateConfig::deterministic(2);
    let prompt = vec![1];
    let result = model.generate_with_cache(&prompt, &gen_config);

    assert!(result.is_ok());
}

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

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

    // Generate exactly max_tokens
    let max_tokens = 5;
    let gen_config = GpuGenerateConfig::deterministic(max_tokens);
    let prompt = vec![1];
    let result = model.generate_with_cache(&prompt, &gen_config);

    assert!(result.is_ok());
    let tokens = result.expect("test value should be present");
    // Should have prompt + up to max_tokens (may stop earlier if stop token hit)
    assert!(tokens.len() <= prompt.len() + max_tokens);
}

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

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

    let gen_config = GpuGenerateConfig::deterministic(2);
    let prompt = vec![5];
    let result = model.generate_with_cache(&prompt, &gen_config);

    assert!(result.is_ok());
    let tokens = result.expect("test value should be present");
    assert!(!tokens.is_empty());
}

// ============================================================================
// KV Cache State Isolation Tests
// ============================================================================

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

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

    // First cache
    let mut cache1 = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    // Second cache
    let mut cache2 = StreamingKVCache::new(
        config.num_layers,
        64,
        config.num_kv_heads,
        config.head_dim(),
    );

    // Populate first cache
    let _ = model.forward_gpu_with_cache(&[1, 2, 3], &mut cache1);
    assert_eq!(cache1.len(), 3);

    // Second cache should be independent
    let _ = model.forward_gpu_with_cache(&[4, 5], &mut cache2);
    assert_eq!(cache2.len(), 2);

    // First cache should still have 3
    assert_eq!(cache1.len(), 3);
}

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

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

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

    // First generation
    let _ = model.forward_gpu_with_cache(&[1, 2, 3], &mut cache);
    assert_eq!(cache.len(), 3);

    // Clear for new generation
    cache.clear();
    assert_eq!(cache.len(), 0);

    // Second generation
    let _ = model.forward_gpu_with_cache(&[4, 5], &mut cache);
    assert_eq!(cache.len(), 2);
}

// ============================================================================
// Memory and Performance Tests
// ============================================================================

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

    // Verify memory calculation for larger context
    let expected = config.num_layers * max_positions * config.num_kv_heads * config.head_dim() * 2 * 4;
    assert_eq!(cache.memory_bytes(), expected);

    // Should be less than 1MB for this config
    assert!(cache.memory_mb() < 1.0);
}

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

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

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

    // Populate with many positions via incremental forward
    let _ = model.forward_gpu_with_cache(&[1], &mut cache);

    for token in 2..100 {
        let result = model.forward_gpu_incremental(token % config.vocab_size, &mut cache);
        assert!(result.is_ok());
    }

    // Should have 99 positions (or wrapped around to max_positions)
    assert!(cache.len() <= max_positions);
}

// ============================================================================
// RoPE Integration Tests (via forward passes)
// ============================================================================

#[test]
fn test_rope_applied_in_forward_with_cache() {
    // RoPE is applied internally during forward_gpu_with_cache
    // We verify it doesn't panic and produces consistent output
    let config = create_kv_test_config();
    let mut model = GpuModel::new(config.clone()).expect("test value should be present");

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

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

    let result = model.forward_gpu_with_cache(&[1, 2, 3], &mut cache);
    assert!(result.is_ok());
}

#[test]
fn test_rope_applied_in_incremental_forward() {
    // RoPE uses start_pos in incremental forward
    let config = create_kv_test_config();
    let mut model = GpuModel::new(config.clone()).expect("test value should be present");

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

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

    // Populate cache first
    let _ = model.forward_gpu_with_cache(&[1, 2], &mut cache);

    // Incremental forward with correct position
    let result = model.forward_gpu_incremental(3, &mut cache);
    assert!(result.is_ok());
}

#[test]
fn test_rope_theta_variations() {
    // Test with different rope_theta values
    let mut config = create_kv_test_config();

    // Higher rope_theta (e.g., for longer context)
    config.rope_theta = 100000.0;

    let mut model = GpuModel::new(config.clone()).expect("test value should be present");
    let mock = MockExecutor::new("rope_theta");
    model.with_test_executor(Box::new(mock));

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

    let result = model.forward_gpu_with_cache(&[1, 2], &mut cache);
    assert!(result.is_ok());
}

// ============================================================================
// GQA Attention Pattern Tests
// ============================================================================

#[test]
fn test_gqa_heads_per_kv_ratio() {
    let config = create_kv_gqa_config();

    // num_heads=8, num_kv_heads=2 => 4 Q heads per KV head
    let heads_per_kv = config.num_heads / config.num_kv_heads;
    assert_eq!(heads_per_kv, 4);
}