realizar 0.8.5

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

// =============================================================================
// COV-022: layer.rs utility functions (Refs PMAT-802)
// =============================================================================

#[test]
#[serial]
fn test_cov022_has_rmsnorm_weights_false_when_not_cached() {
    if !CudaExecutor::is_available() {
        return;
    }
    let executor = CudaExecutor::new(0).expect("CUDA executor");

    // Layer 0 RMSNorm weights not cached - should return false
    assert!(
        !executor.has_rmsnorm_weights(0),
        "has_rmsnorm_weights should return false when not cached"
    );
    assert!(
        !executor.has_rmsnorm_weights(5),
        "has_rmsnorm_weights should return false for any layer"
    );
}

#[test]
#[serial]
fn test_cov022_has_output_norm_false_when_not_cached() {
    if !CudaExecutor::is_available() {
        return;
    }
    let executor = CudaExecutor::new(0).expect("CUDA executor");

    // Output norm not cached - should return false
    assert!(
        !executor.has_output_norm(),
        "has_output_norm should return false when not cached"
    );
}

#[test]
#[serial]
fn test_cov022_has_qkv_bias_false_when_not_cached() {
    if !CudaExecutor::is_available() {
        return;
    }
    let executor = CudaExecutor::new(0).expect("CUDA executor");

    // QKV bias not cached - should return false
    assert!(
        !executor.has_qkv_bias(0),
        "has_qkv_bias should return false when not cached"
    );
    assert!(
        !executor.has_qkv_bias(10),
        "has_qkv_bias should return false for any layer"
    );
}

#[test]
#[serial]
fn test_cov022_has_lm_head_bias_false_when_not_cached() {
    if !CudaExecutor::is_available() {
        return;
    }
    let executor = CudaExecutor::new(0).expect("CUDA executor");

    // LM head bias not cached - should return false
    assert!(
        !executor.has_lm_head_bias(),
        "has_lm_head_bias should return false when not cached"
    );
}

#[test]
#[serial]
fn test_cov022_preload_output_norm_basic() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Preload output norm gamma
    let gamma = vec![1.0f32; 256]; // hidden_dim = 256
    let result = executor.preload_output_norm(&gamma);

    assert!(
        result.is_ok(),
        "preload_output_norm should succeed: {:?}",
        result.err()
    );
    let bytes = result.unwrap();
    assert!(bytes > 0, "Should have uploaded some bytes");
    assert!(
        executor.has_output_norm(),
        "Output norm should be cached now"
    );

    // Preloading again should return 0 (already cached)
    let result2 = executor.preload_output_norm(&gamma);
    assert!(result2.is_ok(), "Second preload should succeed");
    assert_eq!(result2.unwrap(), 0, "Second preload should return 0 bytes");
}

#[test]
#[serial]
fn test_cov022_preload_lm_head_bias_none() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Preload with None bias - should return 0
    let result = executor.preload_lm_head_bias(None);

    assert!(result.is_ok(), "preload_lm_head_bias(None) should succeed");
    assert_eq!(result.unwrap(), 0, "None bias should return 0 bytes");
    assert!(
        !executor.has_lm_head_bias(),
        "LM head bias should not be cached"
    );
}

#[test]
#[serial]
fn test_cov022_preload_lm_head_bias_empty() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Preload with empty bias slice - should return 0
    let empty_bias: [f32; 0] = [];
    let result = executor.preload_lm_head_bias(Some(&empty_bias));

    assert!(result.is_ok(), "preload_lm_head_bias(empty) should succeed");
    assert_eq!(result.unwrap(), 0, "Empty bias should return 0 bytes");
    assert!(
        !executor.has_lm_head_bias(),
        "LM head bias should not be cached"
    );
}

#[test]
#[serial]
fn test_cov022_preload_lm_head_bias_valid() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Preload with valid bias
    let bias = vec![0.1f32; 32000]; // vocab_size = 32000
    let result = executor.preload_lm_head_bias(Some(&bias));

    assert!(
        result.is_ok(),
        "preload_lm_head_bias should succeed: {:?}",
        result.err()
    );
    let bytes = result.unwrap();
    assert!(bytes > 0, "Should have uploaded some bytes");
    assert!(
        executor.has_lm_head_bias(),
        "LM head bias should be cached now"
    );

    // Preloading again should return 0 (already cached)
    let result2 = executor.preload_lm_head_bias(Some(&bias));
    assert!(result2.is_ok(), "Second preload should succeed");
    assert_eq!(result2.unwrap(), 0, "Second preload should return 0 bytes");
}

#[test]
#[serial]
fn test_cov022_cache_rmsnorm_gamma_basic() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Cache a gamma weight
    let gamma = vec![1.0f32; 256];
    let result = executor.cache_rmsnorm_gamma("test_layer.gamma", &gamma);

    assert!(
        result.is_ok(),
        "cache_rmsnorm_gamma should succeed: {:?}",
        result.err()
    );
    let bytes = result.unwrap();
    assert!(bytes > 0, "Should have uploaded some bytes");

    // Caching again should return 0 (already cached)
    let result2 = executor.cache_rmsnorm_gamma("test_layer.gamma", &gamma);
    assert!(result2.is_ok(), "Second cache should succeed");
    assert_eq!(result2.unwrap(), 0, "Second cache should return 0 bytes");
}

#[test]
#[serial]
fn test_cov022_workspace_output_none_initially() {
    if !CudaExecutor::is_available() {
        return;
    }
    let executor = CudaExecutor::new(0).expect("CUDA executor");

    // Workspace output should be None before initialization
    assert!(
        executor.workspace_output().is_none(),
        "workspace_output should be None initially"
    );
}

#[test]
#[serial]
fn test_cov022_read_hidden_state_workspace_not_initialized() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Workspace not initialized - should fail
    let result = executor.read_hidden_state_to_cpu();

    assert!(
        result.is_err(),
        "Should fail when workspace not initialized"
    );
    let err_msg = format!("{:?}", result.err().unwrap());
    assert!(
        err_msg.contains("workspace not initialized") || err_msg.contains("APR-TRACE-001"),
        "Error should mention workspace not initialized: {}",
        err_msg
    );
}

#[test]
#[serial]
fn test_cov022_preload_qkv_bias_empty() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Empty biases - should succeed with 0 bytes
    let q_biases: Vec<Option<&[f32]>> = vec![None, None];
    let k_biases: Vec<Option<&[f32]>> = vec![None, None];
    let v_biases: Vec<Option<&[f32]>> = vec![None, None];

    let result = executor.preload_qkv_bias(2, &q_biases, &k_biases, &v_biases);

    assert!(result.is_ok(), "preload_qkv_bias with None should succeed");
    assert_eq!(result.unwrap(), 0, "Should return 0 bytes for None biases");
    assert!(!executor.has_qkv_bias(0), "Should not have QKV bias");
}

#[test]
#[serial]
fn test_cov022_preload_rmsnorm_weights_basic() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Create norm weights for 2 layers
    let attn_norm_0 = vec![1.0f32; 256];
    let attn_norm_1 = vec![1.0f32; 256];
    let ffn_norm_0 = vec![1.0f32; 256];
    let ffn_norm_1 = vec![1.0f32; 256];

    let attn_norms: Vec<&[f32]> = vec![&attn_norm_0, &attn_norm_1];
    let ffn_norms: Vec<&[f32]> = vec![&ffn_norm_0, &ffn_norm_1];

    let result = executor.preload_rmsnorm_weights(2, &attn_norms, &ffn_norms);

    assert!(
        result.is_ok(),
        "preload_rmsnorm_weights should succeed: {:?}",
        result.err()
    );
    let bytes = result.unwrap();
    assert!(bytes > 0, "Should have uploaded some bytes");

    // Check that layers have weights cached
    assert!(
        executor.has_rmsnorm_weights(0),
        "Layer 0 should have RMSNorm weights"
    );
    assert!(
        executor.has_rmsnorm_weights(1),
        "Layer 1 should have RMSNorm weights"
    );
    assert!(
        !executor.has_rmsnorm_weights(2),
        "Layer 2 should not have RMSNorm weights"
    );
}

// =============================================================================
// COV-023: quantized.rs functions (Refs PMAT-802)
// =============================================================================

#[test]
#[serial]
fn test_cov023_q4k_gemv_cached_weight_not_cached() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    let n = 32u32;
    let k = 256u32;

    let input = vec![0.1f32; k as usize];
    let mut output = vec![0.0f32; n as usize];

    // Weight not cached - should fail
    let result = executor.q4k_gemv_cached("nonexistent_weight", &input, &mut output, n, k);

    assert!(result.is_err(), "Should fail when weight not cached");
    let err_msg = format!("{:?}", result.err().unwrap());
    assert!(
        err_msg.contains("not cached"),
        "Error should mention not cached: {}",
        err_msg
    );
}

#[test]
#[serial]
fn test_cov023_q5k_gemv_cached_weight_not_cached() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    let n = 32u32;
    let k = 256u32;

    let input = vec![0.1f32; k as usize];
    let mut output = vec![0.0f32; n as usize];

    // Weight not cached - should fail
    let result = executor.q5k_gemv_cached("nonexistent_weight", &input, &mut output, n, k);

    assert!(result.is_err(), "Should fail when weight not cached");
    let err_msg = format!("{:?}", result.err().unwrap());
    assert!(
        err_msg.contains("not cached"),
        "Error should mention not cached: {}",
        err_msg
    );
}

#[test]
#[serial]
fn test_cov023_q6k_gemv_cached_weight_not_cached() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    let n = 32u32;
    let k = 256u32;

    let input = vec![0.1f32; k as usize];
    let mut output = vec![0.0f32; n as usize];

    // Weight not cached - should fail
    let result = executor.q6k_gemv_cached("nonexistent_weight", &input, &mut output, n, k);

    assert!(result.is_err(), "Should fail when weight not cached");
    let err_msg = format!("{:?}", result.err().unwrap());
    assert!(
        err_msg.contains("not cached"),
        "Error should mention not cached: {}",
        err_msg
    );
}

#[test]
#[serial]
fn test_cov023_gelu_gpu_basic() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    let n = 256u32;

    // Create GPU buffer with test data
    let data = vec![0.5f32; n as usize];
    let buffer = GpuBuffer::from_host(executor.context(), &data).expect("buffer");

    // Apply GELU
    let result = executor.gelu_gpu(&buffer, n);

    assert!(
        result.is_ok(),
        "gelu_gpu should succeed: {:?}",
        result.err()
    );
}

#[test]
#[serial]
fn test_cov023_rmsnorm_gpu_basic() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    let hidden_size = 256u32;
    let epsilon = 1e-5f32;

    // Create GPU buffers
    let input_data = vec![0.5f32; hidden_size as usize];
    let gamma_data = vec![1.0f32; hidden_size as usize];

    let input = GpuBuffer::from_host(executor.context(), &input_data).expect("input");
    let gamma = GpuBuffer::from_host(executor.context(), &gamma_data).expect("gamma");

    // Apply RMSNorm
    let result = executor.rmsnorm_gpu(&input, &gamma, hidden_size, epsilon);

    assert!(
        result.is_ok(),
        "rmsnorm_gpu should succeed: {:?}",
        result.err()
    );
    let output = result.unwrap();
    assert_eq!(
        output.len(),
        hidden_size as usize,
        "Output should have hidden_size elements"
    );
}