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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

#[test]
#[cfg(feature = "cuda")]
fn test_imp_1008b_zero_clone_forward_block() {
    // IMP-1008b: forward_block_refcell should not clone weights
    use crate::cuda::CudaExecutor;
    use std::time::Instant;

    if !CudaExecutor::is_available() {
        println!("IMP-1008b: CUDA not available, skipping");
        return;
    }

    let config = GpuModelConfig {
        vocab_size: 100,
        hidden_dim: 256,
        num_heads: 4,
        num_kv_heads: 4,
        num_layers: 2,
        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,
    };

    let model = GpuModel::new_with_cuda(config.clone()).expect("Failed to create CUDA model");

    let input: Vec<f32> = vec![0.1; 256];
    let block_idx = 0;
    let num_kv_heads = model.config.num_kv_heads;
    let head_dim = model.config.head_dim();
    let max_positions = 128;

    // Warmup
    let mut cache = StreamingKVCache::new(
        model.config.num_layers,
        max_positions,
        num_kv_heads,
        head_dim,
    );
    let _ = model.forward_block_refcell(&input, block_idx, &mut cache);

    let iterations = 100;

    // Benchmark with RefCell (zero-clone)
    let mut cache2 = StreamingKVCache::new(
        model.config.num_layers,
        max_positions,
        num_kv_heads,
        head_dim,
    );
    let start = Instant::now();
    for _ in 0..iterations {
        let _ = model.forward_block_refcell(&input, block_idx, &mut cache2);
    }
    let refcell_time = start.elapsed();
    let refcell_avg_us = refcell_time.as_micros() as f64 / iterations as f64;

    println!(
        "IMP-1008b: forward_block_refcell avg={:.1}µs ({} iterations)",
        refcell_avg_us, iterations
    );

    // Should be faster than clone-based approach
    assert!(
        refcell_avg_us > 0.0,
        "IMP-1008b: forward_block_refcell should complete"
    );
}

#[test]
#[cfg(feature = "cuda")]
fn test_imp_1008c_generate_throughput_refcell() {
    // IMP-1008c: generate_refcell should have better throughput than clone-based generate
    use crate::cuda::CudaExecutor;
    use std::time::Instant;

    if !CudaExecutor::is_available() {
        println!("IMP-1008c: CUDA not available, skipping");
        return;
    }

    let config = GpuModelConfig {
        vocab_size: 100,
        hidden_dim: 256,
        num_heads: 4,
        num_kv_heads: 4,
        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,
    };

    let model = GpuModel::new_with_cuda(config).expect("Failed to create CUDA model");

    let prompt: Vec<usize> = vec![1, 2, 3, 4, 5];
    let gen_config = GpuGenerateConfig::deterministic(10);

    // Measure RefCell-based generation
    let start = Instant::now();
    let tokens = model
        .generate_refcell(&prompt, &gen_config)
        .expect("Generation failed");
    let elapsed = start.elapsed();

    let tokens_generated = tokens.len() - prompt.len();
    let tok_per_sec = tokens_generated as f64 / elapsed.as_secs_f64();

    println!(
        "IMP-1008c: Generated {} tokens in {:.3}ms ({:.1} tok/s)",
        tokens_generated,
        elapsed.as_secs_f64() * 1000.0,
        tok_per_sec
    );

    // Target: >50 tok/s (improvement over IMP-1006's 35 tok/s)
    println!(
        "IMP-1008c: Previous=35.1 tok/s, Current={:.1} tok/s, Target=228 tok/s (Ollama)",
        tok_per_sec
    );

    assert!(
        tokens_generated > 0,
        "IMP-1008c: Should generate at least some tokens"
    );
}

#[test]
#[ignore = "flaky performance test - depends on hardware state"]
#[cfg(feature = "cuda")]
fn test_imp_1008d_compare_clone_vs_refcell() {
    // IMP-1008d: Direct comparison of clone-based vs RefCell-based forward
    use crate::cuda::CudaExecutor;
    use std::time::Instant;

    if !CudaExecutor::is_available() {
        println!("IMP-1008d: CUDA not available, skipping");
        return;
    }

    let config = GpuModelConfig {
        vocab_size: 100,
        hidden_dim: 256,
        num_heads: 4,
        num_kv_heads: 4,
        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,
    };

    let mut clone_model =
        GpuModel::new_with_cuda(config.clone()).expect("Failed to create clone model");
    let refcell_model = GpuModel::new_with_cuda(config).expect("Failed to create refcell model");

    let prompt: Vec<usize> = vec![1, 2, 3, 4, 5];
    let gen_config = GpuGenerateConfig::deterministic(10);

    // Warmup
    let _ = clone_model.generate(&prompt, &gen_config);
    let _ = refcell_model.generate_refcell(&prompt, &gen_config);

    let iterations = 5;

    // Benchmark clone-based
    let start = Instant::now();
    for _ in 0..iterations {
        let _ = clone_model.generate(&prompt, &gen_config);
    }
    let clone_time = start.elapsed();

    // Benchmark RefCell-based
    let start = Instant::now();
    for _ in 0..iterations {
        let _ = refcell_model.generate_refcell(&prompt, &gen_config);
    }
    let refcell_time = start.elapsed();

    let clone_avg_ms = clone_time.as_secs_f64() * 1000.0 / iterations as f64;
    let refcell_avg_ms = refcell_time.as_secs_f64() * 1000.0 / iterations as f64;
    let speedup = clone_avg_ms / refcell_avg_ms;

    println!(
        "IMP-1008d: Clone={:.2}ms, RefCell={:.2}ms, Speedup={:.2}x",
        clone_avg_ms, refcell_avg_ms, speedup
    );

    // RefCell should be faster (no cloning overhead)
    // For small test models, improvement may be small
    // For phi-2 scale, improvement would be dramatic (8.6GB cloning eliminated)
    assert!(
        speedup > 0.9,
        "IMP-1008d: RefCell should not be slower than clone"
    );
}

#[test]
#[ignore = "flaky performance test - depends on hardware state"]
#[cfg(feature = "cuda")]
fn test_imp_1009a_main_generate_uses_refcell() {
    // IMP-1009a: Main generate() should use RefCell path when CUDA available
    use crate::cuda::CudaExecutor;
    use std::time::Instant;

    if !CudaExecutor::is_available() {
        println!("IMP-1009a: CUDA not available, skipping");
        return;
    }

    let config = GpuModelConfig {
        vocab_size: 100,
        hidden_dim: 256,
        num_heads: 4,
        num_kv_heads: 4,
        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,
    };

    let mut model = GpuModel::new_with_cuda(config).expect("Failed to create CUDA model");

    let prompt: Vec<usize> = vec![1, 2, 3, 4, 5];
    let gen_config = GpuGenerateConfig::deterministic(10);

    // Measure main generate() throughput
    let start = Instant::now();
    let tokens = model
        .generate(&prompt, &gen_config)
        .expect("Generation failed");
    let elapsed = start.elapsed();

    let tokens_generated = tokens.len() - prompt.len();
    let tok_per_sec = tokens_generated as f64 / elapsed.as_secs_f64();

    println!(
        "IMP-1009a: Main generate() - {} tokens in {:.3}ms ({:.1} tok/s)",
        tokens_generated,
        elapsed.as_secs_f64() * 1000.0,
        tok_per_sec
    );

    // After IMP-1009, main generate() should be fast (>100 tok/s target)
    // This test will FAIL before IMP-1009 wiring (expect ~35 tok/s)
    // This test will PASS after IMP-1009 wiring (expect ~200+ tok/s)
    println!(
        "IMP-1009a: Target=100+ tok/s (RefCell speed), Current={:.1} tok/s",
        tok_per_sec
    );

    // Assert that main generate() is fast (indicates RefCell is wired in)
    assert!(
        tok_per_sec > 100.0,
        "IMP-1009a: Main generate() should achieve >100 tok/s with RefCell wiring"
    );
}

#[test]
#[ignore = "flaky performance test - depends on hardware state"]
#[cfg(feature = "cuda")]
fn test_imp_1009b_generate_parity_with_refcell() {
    // IMP-1009b: Main generate() should match generate_refcell() throughput
    use crate::cuda::CudaExecutor;
    use std::time::Instant;

    if !CudaExecutor::is_available() {
        println!("IMP-1009b: CUDA not available, skipping");
        return;
    }

    let config = GpuModelConfig {
        vocab_size: 100,
        hidden_dim: 256,
        num_heads: 4,
        num_kv_heads: 4,
        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,
    };

    let mut clone_model = GpuModel::new_with_cuda(config.clone()).expect("Failed to create model");
    let refcell_model = GpuModel::new_with_cuda(config).expect("Failed to create model");

    let prompt: Vec<usize> = vec![1, 2, 3, 4, 5];
    let gen_config = GpuGenerateConfig::deterministic(10);

    // Warmup
    let _ = clone_model.generate(&prompt, &gen_config);
    let _ = refcell_model.generate_refcell(&prompt, &gen_config);

    let iterations = 5;

    // Benchmark main generate()
    let start = Instant::now();
    for _ in 0..iterations {
        let _ = clone_model.generate(&prompt, &gen_config);
    }
    let main_time = start.elapsed();

    // Benchmark generate_refcell()
    let start = Instant::now();
    for _ in 0..iterations {
        let _ = refcell_model.generate_refcell(&prompt, &gen_config);
    }
    let refcell_time = start.elapsed();

    let main_avg_ms = main_time.as_secs_f64() * 1000.0 / iterations as f64;
    let refcell_avg_ms = refcell_time.as_secs_f64() * 1000.0 / iterations as f64;
    let ratio = main_avg_ms / refcell_avg_ms;

    println!(
        "IMP-1009b: Main={:.2}ms, RefCell={:.2}ms, Ratio={:.2}x",
        main_avg_ms, refcell_avg_ms, ratio
    );

    // After IMP-1009, main should be within 1.2x of RefCell (allow some overhead)
    assert!(
        ratio < 1.5,
        "IMP-1009b: Main generate() should be within 1.5x of RefCell throughput"
    );
}

// ============================================================================
// Coverage Improvement Tests - Scalar/SIMD Operations
// ============================================================================

#[test]
fn test_exceeds_gpu_buffer_limit() {
    // Within limit
    assert!(!exceeds_gpu_buffer_limit(1000));
    // At limit
    assert!(!exceeds_gpu_buffer_limit(MAX_GPU_BUFFER_BYTES / 4));
    // Exceeds limit
    assert!(exceeds_gpu_buffer_limit(MAX_GPU_BUFFER_BYTES / 4 + 1));
}

#[test]
fn test_scalar_softmax_basic() {
    let input = vec![1.0, 2.0, 3.0];
    let output = scalar_softmax(&input);

    assert_eq!(output.len(), 3);
    // Sum should be 1
    let sum: f32 = output.iter().sum();
    assert!((sum - 1.0).abs() < 1e-5);
    // Largest input should have largest output
    assert!(output[2] > output[1] && output[1] > output[0]);
}

#[test]
fn test_scalar_softmax_empty() {
    let input: Vec<f32> = vec![];
    let output = scalar_softmax(&input);
    assert!(output.is_empty());
}

#[test]
fn test_scalar_softmax_single() {
    let input = vec![5.0];
    let output = scalar_softmax(&input);
    assert_eq!(output.len(), 1);
    assert!((output[0] - 1.0).abs() < 1e-5);
}

#[test]
fn test_simd_softmax_basic() {
    let input = vec![1.0, 2.0, 3.0, 4.0];
    let output = simd_softmax(&input);

    assert_eq!(output.len(), 4);
    let sum: f32 = output.iter().sum();
    assert!((sum - 1.0).abs() < 1e-5);
}

#[test]
fn test_simd_softmax_empty() {
    let input: Vec<f32> = vec![];
    let output = simd_softmax(&input);
    assert!(output.is_empty());
}

#[test]
fn test_simd_softmax_matches_scalar() {
    let input = vec![0.5, 1.5, 2.5, 0.0];
    let scalar_out = scalar_softmax(&input);
    let simd_out = simd_softmax(&input);

    for (s, si) in scalar_out.iter().zip(simd_out.iter()) {
        assert!((s - si).abs() < 1e-5, "SIMD softmax should match scalar");
    }
}

#[test]
fn test_scalar_rope_basic() {
    let seq_len = 2;
    let head_dim = 4;
    let hidden_dim = head_dim;
    let input = vec![1.0; seq_len * hidden_dim];
    let theta = 10000.0;

    let output = scalar_rope(&input, seq_len, head_dim, theta);

    assert_eq!(output.len(), input.len());
    // Should have rotated values
}

#[test]
fn test_scalar_rope_empty() {
    let output = scalar_rope(&[], 0, 4, 10000.0);
    assert!(output.is_empty());
}

#[test]
fn test_scalar_rope_zero_head_dim() {
    let input = vec![1.0, 2.0, 3.0, 4.0];
    let output = scalar_rope(&input, 2, 0, 10000.0);
    assert!(output.is_empty());
}

#[test]
fn test_simd_rope_basic() {
    let seq_len = 2;
    let head_dim = 4;
    let hidden_dim = head_dim;
    let input = vec![1.0; seq_len * hidden_dim];
    let theta = 10000.0;

    let output = simd_rope(&input, seq_len, head_dim, theta);

    assert_eq!(output.len(), input.len());
}

#[test]
fn test_simd_rope_empty() {
    let output = simd_rope(&[], 0, 4, 10000.0);
    assert!(output.is_empty());
}