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

/// M33: llama.cpp Completion Endpoint (IMP-086)
/// Target: /completion returns generated text (llama.cpp compatible)
///
/// Tests llama.cpp-compatible completion API.
/// Run with: `cargo test test_imp_086 --ignored`
#[test]
#[ignore = "Requires running server"]
fn test_imp_086_llamacpp_endpoint() {
    // IMP-086: Integration test for /completion (llama.cpp-compatible)

    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(10))
        .build()
        .expect("Failed to create HTTP client");

    let url = "http://127.0.0.1:3000/completion";

    // llama.cpp-style request format
    let request = serde_json::json!({
        "prompt": "Hello, world!",
        "n_predict": 10,
        "temperature": 0.0
    });

    match client.post(url).json(&request).send() {
        Ok(response) => {
            if response.status().is_success() {
                let body: serde_json::Value = response.json().expect("Valid JSON");
                assert!(
                    body.get("content").is_some() || body.get("text").is_some(),
                    "IMP-086: Response should have 'content' or 'text'"
                );
                println!("IMP-086: ✅ llama.cpp completion endpoint works");
            } else if response.status().as_u16() == 404 {
                println!("IMP-086: ⚠️ /completion not implemented yet (404)");
            } else {
                panic!("IMP-086: Unexpected status: {}", response.status());
            }
        },
        Err(e) => {
            panic!(
                "IMP-086: Server not running. Start with: cargo run --example api_server. Error: {}",
                e
            );
        },
    }
}

/// M33: Benchmark Integration (IMP-087)
/// Target: realizar appears in bench-server-matrix.sh output
///
/// Verifies benchmark infrastructure is functional.
/// Run with: `cargo test test_imp_087 --ignored`
#[test]
#[ignore = "Requires benchmark infrastructure"]
fn test_imp_087_benchmark_integration() {
    // IMP-087: Benchmark integration test
    //
    // This test verifies that:
    // 1. The benchmark script exists
    // 2. The server can respond to benchmark-style requests
    // 3. Throughput can be measured

    use std::time::Instant;

    // Check if benchmark script exists
    let script_path = std::path::Path::new("scripts/bench-server-matrix.sh");
    if script_path.exists() {
        println!("IMP-087: ✅ Benchmark script exists at scripts/bench-server-matrix.sh");
    } else {
        println!("IMP-087: ⚠️ Benchmark script not found (optional)");
    }

    // Test benchmark-style request pattern
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(30))
        .build()
        .expect("Failed to create HTTP client");

    let url = "http://127.0.0.1:3000/generate";
    let request = serde_json::json!({
        "prompt": "Benchmark test",
        "max_tokens": 10,
        "temperature": 0.0
    });

    // Run 5 iterations to measure throughput
    let iterations = 5;
    let start = Instant::now();
    let mut success_count = 0;
    let mut total_tokens = 0;

    for i in 0..iterations {
        match client.post(url).json(&request).send() {
            Ok(response) if response.status().is_success() => {
                if let Ok(body) = response.json::<serde_json::Value>() {
                    if let Some(text) = body.get("text").and_then(|t| t.as_str()) {
                        total_tokens += text.split_whitespace().count();
                        success_count += 1;
                    }
                }
            },
            Ok(response) => {
                println!(
                    "IMP-087: Iteration {} failed with status {}",
                    i,
                    response.status()
                );
            },
            Err(e) => {
                assert!(
                    i != 0,
                    "IMP-087: Server not running. Start with: cargo run --example api_server. Error: {}",
                    e
                );
            },
        }
    }

    let elapsed = start.elapsed();
    let throughput = if elapsed.as_secs_f64() > 0.0 {
        total_tokens as f64 / elapsed.as_secs_f64()
    } else {
        0.0
    };

    println!(
        "IMP-087: ✅ Benchmark test: {} iterations, {} tokens, {:.2} tok/s",
        success_count, total_tokens, throughput
    );

    assert!(
        success_count > 0,
        "IMP-087: At least one benchmark iteration should succeed"
    );
}

/// M33: GQA Support - num_kv_heads in config (IMP-088)
/// Target: GpuModelConfig has num_kv_heads field for Grouped Query Attention
#[test]
#[cfg(feature = "gpu")]
fn test_imp_088_gqa_config_num_kv_heads() {
    use crate::gpu::GpuModelConfig;

    // Create config with different num_kv_heads (GQA pattern)
    // Qwen 1.5B: 12 heads, 2 kv_heads (6:1 ratio)
    let config = GpuModelConfig {
        vocab_size: 151936,
        hidden_dim: 1536,
        num_heads: 12,
        num_kv_heads: 2, // GQA: fewer KV heads than Q heads
        num_layers: 28,
        intermediate_dim: 8960,
        eps: 1e-6,
        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,
    };

    assert_eq!(config.num_heads, 12, "IMP-088: Should have 12 Q heads");
    assert_eq!(config.num_kv_heads, 2, "IMP-088: Should have 2 KV heads");

    // head_dim should be hidden_dim / num_heads
    let head_dim = config.hidden_dim / config.num_heads;
    assert_eq!(head_dim, 128, "IMP-088: Head dim should be 128");

    // KV size per layer should use num_kv_heads
    let kv_head_dim = config.hidden_dim / config.num_heads; // Same head_dim
    let kv_size = config.num_kv_heads * kv_head_dim;
    assert_eq!(kv_size, 256, "IMP-088: KV size should be 2*128=256");
}

/// M33: GQA Attention Forward (IMP-089)
/// Target: Forward pass handles K/V with fewer heads than Q
#[test]
#[cfg(feature = "gpu")]
fn test_imp_089_gqa_attention_forward() {
    use crate::gpu::{GpuModel, GpuModelConfig};

    // Create GQA config (fewer KV heads than Q heads)
    let config = GpuModelConfig {
        vocab_size: 256,
        hidden_dim: 128,
        num_heads: 4,    // 4 Q heads
        num_kv_heads: 2, // 2 KV heads (2:1 ratio, each KV serves 2 Q heads)
        num_layers: 2,
        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::new(config).expect("Failed to create GQA model");

    // Forward should work with GQA attention
    let tokens = vec![1usize, 2, 3];
    let result = model.forward_gpu(&tokens);

    assert!(
        result.is_ok(),
        "IMP-089: Forward pass should handle GQA attention. Error: {:?}",
        result.err()
    );

    let logits = result.expect("test");
    // Output should be [seq_len * vocab_size]
    assert_eq!(
        logits.len(),
        tokens.len() * 256,
        "IMP-089: Logits should be seq_len * vocab_size"
    );
}

/// M33: CPU Embedding for Large Vocab (IMP-090)
/// Target: Handle vocab sizes that exceed GPU buffer limits (>65536 tokens)
/// wgpu max buffer is 256MB, large vocab like Qwen (151936) needs CPU fallback
#[test]
#[cfg(feature = "gpu")]
fn test_imp_090_cpu_embedding_large_vocab() {
    use crate::gpu::{GpuModel, GpuModelConfig};

    // Large vocab size that would exceed GPU buffer limits if stored fully
    // Real example: Qwen 2.5 Coder 1.5B has vocab_size=151936
    // Buffer size would be: 151936 * 1536 * 4 = 933MB > 256MB wgpu limit
    // Test with smaller but still "large vocab" threshold (>65536)
    let large_vocab_config = GpuModelConfig {
        vocab_size: 100_000, // Large vocab - requires CPU embedding fallback
        hidden_dim: 256,     // Smaller hidden_dim for test speed
        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,
    };

    // This should NOT fail due to GPU buffer limits
    // Instead, it should use CPU embedding lookup
    let model_result = GpuModel::new(large_vocab_config);

    assert!(
        model_result.is_ok(),
        "IMP-090: Should create model with large vocab using CPU embedding. Error: {:?}",
        model_result.err()
    );

    let mut model = model_result.expect("test");

    // Forward pass should also work with CPU embedding lookup
    let tokens = vec![0usize, 1000, 50000, 99999]; // Include edge tokens
    let result = model.forward_gpu(&tokens);

    assert!(
        result.is_ok(),
        "IMP-090: Forward pass should work with CPU embedding for large vocab. Error: {:?}",
        result.err()
    );

    let logits = result.expect("test");
    assert_eq!(
        logits.len(),
        tokens.len() * 100_000,
        "IMP-090: Logits should be seq_len * vocab_size"
    );

    // Verify embeddings are valid (not all zeros, not NaN)
    let has_valid_values = logits.iter().any(|&v| v != 0.0 && !v.is_nan());
    assert!(
        has_valid_values,
        "IMP-090: Logits should contain valid non-zero values"
    );
}

/// IMP-093: Real GGUF GPU benchmark test
///
/// Tests the full GPU inference path with a real GGUF model.
/// This verifies IMP-092 (no weight cloning) improves performance.
///
/// Run: cargo test --features gpu test_imp_093_real_gguf_gpu_benchmark -- --nocapture --ignored
#[test]
#[cfg(feature = "gpu")]
#[ignore] // Requires real GGUF file - run manually
fn test_imp_093_real_gguf_gpu_benchmark() {
    use crate::gguf::MappedGGUFModel;
    use crate::gpu::GpuModel;
    use std::path::Path;
    use std::time::Instant;

    // Real GGUF model path (Qwen 2.5 Coder 1.5B Q4_K_M)
    let home = std::env::var("HOME").expect("HOME env var not set");
    let model_path_str = format!(
        "{}/src/single-shot-eval/models/raw/qwen2.5-coder-1.5b-instruct-q4_k_m.gguf",
        home
    );
    let model_path = &model_path_str;

    if !Path::new(model_path).exists() {
        eprintln!("IMP-093: Skipping - model not found at {}", model_path);
        return;
    }

    println!("\n=== IMP-093: Real GGUF GPU Benchmark ===\n");
    println!("Model: {}", model_path);

    // Load model
    let load_start = Instant::now();
    let mapped = MappedGGUFModel::from_path(model_path).expect("Failed to load GGUF");
    let load_mmap = load_start.elapsed();
    println!("  Mmap load: {:?}", load_mmap);

    let gpu_start = Instant::now();
    let mut gpu_model = GpuModel::from_mapped_gguf(&mapped).expect("Failed to load to GPU");
    let gpu_load = gpu_start.elapsed();
    println!("  GPU load: {:?}", gpu_load);
    println!(
        "  Config: hidden={}, layers={}, vocab={}, heads={}, kv_heads={}, intermediate={}",
        gpu_model.config().hidden_dim,
        gpu_model.config().num_layers,
        gpu_model.config().vocab_size,
        gpu_model.config().num_heads,
        gpu_model.config().num_kv_heads,
        gpu_model.config().intermediate_dim,
    );
    println!();

    // Test tokens (small prompt)
    let test_tokens = vec![0usize, 1, 2, 3];
    let max_tokens = 5;

    // Warmup
    println!("Warmup...");
    let _ = gpu_model.generate(
        &test_tokens,
        &crate::gpu::GpuGenerateConfig {
            max_tokens: 1,
            ..Default::default()
        },
    );

    // Benchmark generation
    println!("\nGenerating {} tokens...", max_tokens);
    let gen_start = Instant::now();
    let result = gpu_model.generate(
        &test_tokens,
        &crate::gpu::GpuGenerateConfig {
            max_tokens,
            ..Default::default()
        },
    );
    let gen_elapsed = gen_start.elapsed();

    assert!(
        result.is_ok(),
        "IMP-093: Generation should succeed: {:?}",
        result.err()
    );

    let generated = result.expect("test");
    let gen_secs = gen_elapsed.as_secs_f64();
    let tps = max_tokens as f64 / gen_secs;

    println!("\n=== Results ===");
    println!(
        "  Generated: {} tokens",
        generated.len() - test_tokens.len()
    );
    println!("  Time: {:.3}s", gen_secs);
    println!("  Throughput: {:.2} tok/s", tps);
    println!();

    // Performance assertions (soft targets - document actual vs target)
    // Target: ≥10 tok/s (Ollama achieves ~143 tok/s)
    // IMP-092 eliminates 3.7GB/token memory copying
    let target_tps = 10.0;
    if tps < target_tps {
        eprintln!(
            "WARNING: Below target {} tok/s (actual: {:.2} tok/s)",
            target_tps, tps
        );
        eprintln!("Parity gap with Ollama (~143 tok/s): {:.0}x", 143.0 / tps);
    } else {
        println!(
            "PASS: Achieved {:.2} tok/s (target: {} tok/s)",
            tps, target_tps
        );
    }
}