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

// ===== IMP-132: Wire latency recording into adaptive attention path =====

/// IMP-132a: Adaptive attention should record latency for CPU dispatches
#[cfg(feature = "gpu")]
#[test]
fn test_imp_132a_adaptive_attention_records_cpu_latency() {
    use crate::gguf::{
        DispatchMetrics, GGUFConfig, OwnedQuantizedModelCachedSync, QuantizedGenerateConfig,
    };
    use std::sync::Arc;

    let config = GGUFConfig {
        architecture: "test".to_string(),
        constraints: crate::gguf::ArchConstraints::from_architecture("test"),
        hidden_dim: 16,
        intermediate_dim: 32,
        num_layers: 1,
        num_heads: 2,
        num_kv_heads: 2,
        vocab_size: 100,
        context_length: 128,
        rope_theta: 10000.0,
        eps: 1e-5,
        rope_type: 0,
            explicit_head_dim: None,
            query_pre_attn_scalar: None,
        bos_token_id: None,
            eos_token_id: None,
    };

    let model = create_test_quantized_model(&config);
    let cached_model = OwnedQuantizedModelCachedSync::new(model);
    let metrics = Arc::new(DispatchMetrics::new());

    let gen_config = QuantizedGenerateConfig {
        max_tokens: 5,
        temperature: 0.0,
        top_k: 1,
        stop_tokens: vec![],
        trace: false,
            ..Default::default()
    };

    // Generate tokens to trigger CPU dispatches (cache < 64 tokens)
    let _ = cached_model.generate_with_cache_adaptive(&[1, 2, 3], &gen_config, &metrics);

    // IMP-132a: After CPU dispatches, latency should be recorded
    assert!(
        metrics.cpu_latency_count() > 0,
        "IMP-132a: CPU latency count should be > 0 after adaptive generation. Got: {}",
        metrics.cpu_latency_count()
    );
}

/// IMP-132b: Latency values should be reasonable (not zero for executed paths)
#[cfg(feature = "gpu")]
#[test]
#[ignore = "GPU latency test - timing-sensitive and may fail under coverage instrumentation"]
fn test_imp_132b_latency_values_are_reasonable() {
    use crate::gguf::{
        DispatchMetrics, GGUFConfig, OwnedQuantizedModelCachedSync, QuantizedGenerateConfig,
    };
    use std::sync::Arc;

    let config = GGUFConfig {
        architecture: "test".to_string(),
        constraints: crate::gguf::ArchConstraints::from_architecture("test"),
        hidden_dim: 16,
        intermediate_dim: 32,
        num_layers: 1,
        num_heads: 2,
        num_kv_heads: 2,
        vocab_size: 100,
        context_length: 128,
        rope_theta: 10000.0,
        eps: 1e-5,
        rope_type: 0,
            explicit_head_dim: None,
            query_pre_attn_scalar: None,
        bos_token_id: None,
            eos_token_id: None,
    };

    let model = create_test_quantized_model(&config);
    let cached_model = OwnedQuantizedModelCachedSync::new(model);
    let metrics = Arc::new(DispatchMetrics::new());

    let gen_config = QuantizedGenerateConfig {
        max_tokens: 5,
        temperature: 0.0,
        top_k: 1,
        stop_tokens: vec![],
        trace: false,
            ..Default::default()
    };

    // Generate tokens
    let _ = cached_model.generate_with_cache_adaptive(&[1, 2, 3], &gen_config, &metrics);

    // IMP-132b: Mean latency should be > 0 (actual time was measured)
    let mean_latency = metrics.cpu_latency_mean_us();
    assert!(
        mean_latency > 0.0,
        "IMP-132b: Mean CPU latency should be > 0µs after attention. Got: {:.1}µs",
        mean_latency
    );
}

/// IMP-132c: Latency count should match dispatch count
#[cfg(feature = "gpu")]
#[test]
fn test_imp_132c_latency_count_matches_dispatch_count() {
    use crate::gguf::{
        DispatchMetrics, GGUFConfig, OwnedQuantizedModelCachedSync, QuantizedGenerateConfig,
    };
    use std::sync::Arc;

    let config = GGUFConfig {
        architecture: "test".to_string(),
        constraints: crate::gguf::ArchConstraints::from_architecture("test"),
        hidden_dim: 16,
        intermediate_dim: 32,
        num_layers: 2, // 2 layers for more dispatches
        num_heads: 2,
        num_kv_heads: 2,
        vocab_size: 100,
        context_length: 128,
        rope_theta: 10000.0,
        eps: 1e-5,
        rope_type: 0,
            explicit_head_dim: None,
            query_pre_attn_scalar: None,
        bos_token_id: None,
            eos_token_id: None,
    };

    let model = create_test_quantized_model(&config);
    let cached_model = OwnedQuantizedModelCachedSync::new(model);
    let metrics = Arc::new(DispatchMetrics::new());

    let gen_config = QuantizedGenerateConfig {
        max_tokens: 10,
        temperature: 0.0,
        top_k: 1,
        stop_tokens: vec![],
        trace: false,
            ..Default::default()
    };

    // Generate tokens
    let _ = cached_model.generate_with_cache_adaptive(&[1, 2, 3, 4, 5], &gen_config, &metrics);

    // IMP-132c: Every CPU dispatch should record latency
    let cpu_dispatches = metrics.cpu_dispatches();
    let cpu_latency_count = metrics.cpu_latency_count();

    assert_eq!(
        cpu_dispatches, cpu_latency_count,
        "IMP-132c: CPU latency count ({}) should match dispatch count ({})",
        cpu_latency_count, cpu_dispatches
    );
}

/// IMP-132d: GPU dispatches should also record latency (when cache >= 64)
#[cfg(feature = "gpu")]
#[test]
fn test_imp_132d_gpu_dispatches_record_latency() {
    use crate::gguf::{
        DispatchMetrics, GGUFConfig, OwnedQuantizedModelCachedSync, QuantizedGenerateConfig,
    };
    use std::sync::Arc;

    let config = GGUFConfig {
        architecture: "test".to_string(),
        constraints: crate::gguf::ArchConstraints::from_architecture("test"),
        hidden_dim: 16,
        intermediate_dim: 32,
        num_layers: 1,
        num_heads: 2,
        num_kv_heads: 2,
        vocab_size: 100,
        context_length: 256,
        rope_theta: 10000.0,
        eps: 1e-5,
        rope_type: 0,
            explicit_head_dim: None,
            query_pre_attn_scalar: None,
        bos_token_id: None,
            eos_token_id: None,
    };

    let model = create_test_quantized_model(&config);
    let cached_model = OwnedQuantizedModelCachedSync::new(model);
    let metrics = Arc::new(DispatchMetrics::new());

    let gen_config = QuantizedGenerateConfig {
        max_tokens: 80, // Generate enough to trigger GPU dispatch
        temperature: 0.0,
        top_k: 1,
        stop_tokens: vec![],
        trace: false,
            ..Default::default()
    };

    // Generate enough tokens to trigger GPU dispatch (cache >= 64 tokens)
    let _ = cached_model.generate_with_cache_adaptive(&[1], &gen_config, &metrics);

    // IMP-132d: After many tokens, should have GPU dispatches with latency recorded
    let gpu_dispatches = metrics.gpu_dispatches();
    let gpu_latency_count = metrics.gpu_latency_count();

    if gpu_dispatches > 0 {
        assert_eq!(
            gpu_dispatches, gpu_latency_count,
            "IMP-132d: GPU latency count ({}) should match dispatch count ({})",
            gpu_latency_count, gpu_dispatches
        );
    }
}

// ============================================================
// IMP-133: Add latency mean to JSON response
// RED phase: Tests written first, implementation to follow
// ============================================================

// IMP-133a: DispatchMetrics should have cpu_latency_mean_us method
#[test]
fn test_imp_133a_dispatch_metrics_has_mean_methods() {
    use crate::gguf::DispatchMetrics;
    use std::time::Duration;

    let metrics = DispatchMetrics::new();

    // Record some latencies
    metrics.record_cpu_latency(Duration::from_micros(100));
    metrics.record_cpu_latency(Duration::from_micros(200));
    metrics.record_cpu_latency(Duration::from_micros(300));

    metrics.record_gpu_latency(Duration::from_micros(500));
    metrics.record_gpu_latency(Duration::from_micros(700));

    // IMP-133a: Mean methods should exist and return correct values
    let cpu_mean = metrics.cpu_latency_mean_us();
    let gpu_mean = metrics.gpu_latency_mean_us();

    assert!(
        (cpu_mean - 200.0).abs() < 1.0,
        "IMP-133a: CPU mean should be ~200µs, got {}",
        cpu_mean
    );
    assert!(
        (gpu_mean - 600.0).abs() < 1.0,
        "IMP-133a: GPU mean should be ~600µs, got {}",
        gpu_mean
    );
}

// IMP-133b: Mean should be 0 when no samples recorded
#[test]
fn test_imp_133b_mean_zero_when_empty() {
    use crate::gguf::DispatchMetrics;

    let metrics = DispatchMetrics::new();

    // IMP-133b: Mean should be 0.0 when no samples recorded
    assert_eq!(
        metrics.cpu_latency_mean_us(),
        0.0,
        "IMP-133b: CPU mean should be 0 when empty"
    );
    assert_eq!(
        metrics.gpu_latency_mean_us(),
        0.0,
        "IMP-133b: GPU mean should be 0 when empty"
    );
}

// IMP-133c: JSON response should include mean latency fields
#[test]
fn test_imp_133c_json_response_includes_mean() {
    use crate::gguf::DispatchMetrics;
    use std::sync::Arc;
    use std::time::Duration;

    let metrics = Arc::new(DispatchMetrics::new());

    // Record some latencies
    metrics.record_cpu_dispatch();
    metrics.record_cpu_latency(Duration::from_micros(100));
    metrics.record_cpu_dispatch();
    metrics.record_cpu_latency(Duration::from_micros(300));

    // Build response (would be done by handler)
    let response = DispatchMetricsResponse {
        cpu_dispatches: metrics.cpu_dispatches(),
        gpu_dispatches: metrics.gpu_dispatches(),
        total_dispatches: metrics.total_dispatches(),
        gpu_ratio: metrics.gpu_ratio(),
        cpu_latency_p50_us: metrics.cpu_latency_p50_us(),
        cpu_latency_p95_us: metrics.cpu_latency_p95_us(),
        cpu_latency_p99_us: metrics.cpu_latency_p99_us(),
        gpu_latency_p50_us: metrics.gpu_latency_p50_us(),
        gpu_latency_p95_us: metrics.gpu_latency_p95_us(),
        gpu_latency_p99_us: metrics.gpu_latency_p99_us(),
        // IMP-133: New mean fields
        cpu_latency_mean_us: metrics.cpu_latency_mean_us(),
        gpu_latency_mean_us: metrics.gpu_latency_mean_us(),
        // IMP-134: New min/max fields
        cpu_latency_min_us: metrics.cpu_latency_min_us(),
        cpu_latency_max_us: metrics.cpu_latency_max_us(),
        gpu_latency_min_us: metrics.gpu_latency_min_us(),
        gpu_latency_max_us: metrics.gpu_latency_max_us(),
        // IMP-135: Variance/stddev fields
        cpu_latency_variance_us: metrics.cpu_latency_variance_us(),
        cpu_latency_stddev_us: metrics.cpu_latency_stddev_us(),
        gpu_latency_variance_us: metrics.gpu_latency_variance_us(),
        gpu_latency_stddev_us: metrics.gpu_latency_stddev_us(),
        // IMP-136: Histogram bucket configuration
        bucket_boundaries_us: metrics.bucket_boundaries_us(),
        cpu_latency_bucket_counts: metrics.cpu_latency_buckets().to_vec(),
        gpu_latency_bucket_counts: metrics.gpu_latency_buckets().to_vec(),
        // IMP-140: Throughput metrics
        throughput_rps: 0.0,
        elapsed_seconds: 0.0,
    };

    // IMP-133c: Response should have mean fields with correct values
    assert!(
        (response.cpu_latency_mean_us - 200.0).abs() < 1.0,
        "IMP-133c: Response CPU mean should be ~200µs, got {}",
        response.cpu_latency_mean_us
    );
    assert_eq!(
        response.gpu_latency_mean_us, 0.0,
        "IMP-133c: Response GPU mean should be 0 (no GPU samples)"
    );
}

// IMP-133d: Mean should handle single sample correctly
#[test]
fn test_imp_133d_mean_single_sample() {
    use crate::gguf::DispatchMetrics;
    use std::time::Duration;

    let metrics = DispatchMetrics::new();

    // Single sample
    metrics.record_cpu_latency(Duration::from_micros(42));

    // IMP-133d: Mean of single sample should equal that sample
    assert!(
        (metrics.cpu_latency_mean_us() - 42.0).abs() < 0.1,
        "IMP-133d: Mean of single sample should be 42µs, got {}",
        metrics.cpu_latency_mean_us()
    );
}

// ============================================================
// IMP-134: Add min/max latency tracking
// RED phase: Tests written first, implementation to follow
// ============================================================

// IMP-134a: DispatchMetrics should have min/max methods
#[test]
fn test_imp_134a_dispatch_metrics_has_min_max_methods() {
    use crate::gguf::DispatchMetrics;
    use std::time::Duration;

    let metrics = DispatchMetrics::new();

    // Record some latencies with varying values
    metrics.record_cpu_latency(Duration::from_micros(100));
    metrics.record_cpu_latency(Duration::from_micros(50));
    metrics.record_cpu_latency(Duration::from_micros(300));

    metrics.record_gpu_latency(Duration::from_micros(200));
    metrics.record_gpu_latency(Duration::from_micros(800));

    // IMP-134a: Min/max methods should exist and return correct values
    assert_eq!(
        metrics.cpu_latency_min_us(),
        50,
        "IMP-134a: CPU min should be 50µs"
    );
    assert_eq!(
        metrics.cpu_latency_max_us(),
        300,
        "IMP-134a: CPU max should be 300µs"
    );
    assert_eq!(
        metrics.gpu_latency_min_us(),
        200,
        "IMP-134a: GPU min should be 200µs"
    );
    assert_eq!(
        metrics.gpu_latency_max_us(),
        800,
        "IMP-134a: GPU max should be 800µs"
    );
}

// IMP-134b: Min/max should be 0 when no samples recorded
#[test]
fn test_imp_134b_min_max_zero_when_empty() {
    use crate::gguf::DispatchMetrics;

    let metrics = DispatchMetrics::new();

    // IMP-134b: Min/max should be 0 when no samples recorded
    assert_eq!(
        metrics.cpu_latency_min_us(),
        0,
        "IMP-134b: CPU min should be 0 when empty"
    );
    assert_eq!(
        metrics.cpu_latency_max_us(),
        0,
        "IMP-134b: CPU max should be 0 when empty"
    );
    assert_eq!(
        metrics.gpu_latency_min_us(),
        0,
        "IMP-134b: GPU min should be 0 when empty"
    );
    assert_eq!(
        metrics.gpu_latency_max_us(),
        0,
        "IMP-134b: GPU max should be 0 when empty"
    );
}

// IMP-134c: JSON response should include min/max latency fields