realizar 0.8.4

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

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

    // Initially should be 0
    assert_eq!(
        executor.workspace_batch_size(),
        0,
        "Initial batch size should be 0"
    );

    // After init_workspace
    executor.init_workspace(512, 256).expect("init workspace");
    assert_eq!(
        executor.workspace_batch_size(),
        1,
        "Batch size should be 1 after init"
    );
}

/// Test workspace_batch_size after batched init
#[test]
#[serial]
fn test_cov029_workspace_batch_size_batched() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    executor.init_workspace(512, 256).expect("init workspace");
    executor
        .init_batched_workspace(512, 256, 4)
        .expect("init batched");

    assert_eq!(executor.workspace_batch_size(), 4, "Batch size should be 4");
}

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

    // Get mutable profiler access
    let profiler = executor.profiler_mut();
    // Just verify we can access it
    assert!(
        std::ptr::from_mut(profiler) as usize != 0,
        "profiler should be valid"
    );
}

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

    let ascii = executor.execution_graph_ascii();
    // Just verify the function returns without panic - the actual format varies
    // Empty graph or a tree structure are both valid
    let _ = ascii.len();
}

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

    // Access tile stats for different levels
    let _macro_stats = executor.tile_stats(trueno::TileLevel::Macro);
    let _midi_stats = executor.tile_stats(trueno::TileLevel::Midi);
    // Just verify we can access without panic
}

// =============================================================================
// COV-030: CudaExecutor Layer API Coverage Tests
// Target: Increase layer.rs coverage from 20% to higher
// =============================================================================

/// Test workspace_output returns None before init
#[test]
#[serial]
fn test_cov030_workspace_output_before_init() {
    if !CudaExecutor::is_available() {
        return;
    }
    let executor = CudaExecutor::new(0).expect("CUDA executor");

    // Before init, workspace_output should return None
    assert!(
        executor.workspace_output().is_none(),
        "workspace_output should be None before init"
    );
}

/// Test workspace_output returns Some after init
#[test]
#[serial]
fn test_cov030_workspace_output_after_init() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Initialize workspace
    executor.init_workspace(512, 256).expect("init workspace");

    // After init, workspace_output should return Some
    assert!(
        executor.workspace_output().is_some(),
        "workspace_output should be Some after init"
    );
}

/// Test has_rmsnorm_weights before and after preload
#[test]
#[serial]
fn test_cov030_has_rmsnorm_weights() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Before preload
    assert!(
        !executor.has_rmsnorm_weights(0),
        "Should not have weights for layer 0 initially"
    );
    assert!(
        !executor.has_rmsnorm_weights(1),
        "Should not have weights for layer 1 initially"
    );

    // Preload for 2 layers - need &[&[f32]] type
    let attn_norm_0 = vec![1.0f32; 512];
    let attn_norm_1 = vec![1.0f32; 512];
    let ffn_norm_0 = vec![1.0f32; 512];
    let ffn_norm_1 = vec![1.0f32; 512];
    let attn_norms: &[&[f32]] = &[&attn_norm_0, &attn_norm_1];
    let ffn_norms: &[&[f32]] = &[&ffn_norm_0, &ffn_norm_1];
    executor
        .preload_rmsnorm_weights(2, attn_norms, ffn_norms)
        .expect("preload");

    // After preload
    assert!(
        executor.has_rmsnorm_weights(0),
        "Should have weights for layer 0 after preload"
    );
    assert!(
        executor.has_rmsnorm_weights(1),
        "Should have weights for layer 1 after preload"
    );
    assert!(
        !executor.has_rmsnorm_weights(2),
        "Should not have weights for layer 2 (not preloaded)"
    );
}

/// Test has_output_norm before and after preload
#[test]
#[serial]
fn test_cov030_has_output_norm() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Before preload
    assert!(
        !executor.has_output_norm(),
        "Should not have output norm initially"
    );

    // Preload output norm
    let gamma = vec![1.0f32; 512];
    executor
        .preload_output_norm(&gamma)
        .expect("preload output norm");

    // After preload
    assert!(
        executor.has_output_norm(),
        "Should have output norm after preload"
    );
}

/// Test has_qkv_bias before and after preload
#[test]
#[serial]
fn test_cov030_has_qkv_bias() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Before preload
    assert!(
        !executor.has_qkv_bias(0),
        "Should not have QKV bias for layer 0 initially"
    );

    // Preload for 1 layer - need &[Option<&[f32]>] type
    let q_bias_0 = vec![0.0f32; 512];
    let k_bias_0 = vec![0.0f32; 64];
    let v_bias_0 = vec![0.0f32; 64];
    let q_biases: &[Option<&[f32]>] = &[Some(&q_bias_0)];
    let k_biases: &[Option<&[f32]>] = &[Some(&k_bias_0)];
    let v_biases: &[Option<&[f32]>] = &[Some(&v_bias_0)];
    executor
        .preload_qkv_bias(1, q_biases, k_biases, v_biases)
        .expect("preload");

    // After preload
    assert!(
        executor.has_qkv_bias(0),
        "Should have QKV bias for layer 0 after preload"
    );
    assert!(
        !executor.has_qkv_bias(1),
        "Should not have QKV bias for layer 1 (not preloaded)"
    );
}

/// Test has_lm_head_bias before and after preload
#[test]
#[serial]
fn test_cov030_has_lm_head_bias() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Before preload
    assert!(
        !executor.has_lm_head_bias(),
        "Should not have LM head bias initially"
    );

    // Preload LM head bias
    let bias = vec![0.0f32; 32000];
    executor
        .preload_lm_head_bias(Some(&bias))
        .expect("preload lm head bias");

    // After preload
    assert!(
        executor.has_lm_head_bias(),
        "Should have LM head bias after preload"
    );
}

/// Test output_rmsnorm_gpu basic operation
#[test]
#[serial]
fn test_cov030_output_rmsnorm_gpu() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    let hidden_dim = 512u32;
    let epsilon = 1e-5f32;

    // Create input, output, and gamma buffers
    let input = vec![1.0f32; hidden_dim as usize];
    let mut output = vec![0.0f32; hidden_dim as usize];
    let gamma = vec![1.0f32; hidden_dim as usize];

    let result = executor.output_rmsnorm_gpu(&input, &mut output, &gamma, hidden_dim, epsilon);
    assert!(
        result.is_ok(),
        "output_rmsnorm_gpu should succeed: {:?}",
        result.err()
    );

    // Output should be normalized (not all zeros)
    let sum: f32 = output.iter().sum();
    assert!(sum.abs() > 0.0, "Output should be non-zero after RMSNorm");
}

/// Test gpu_argmax basic operation
#[test]
#[serial]
fn test_cov030_gpu_argmax_basic() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Create logits buffer with a clear maximum
    let vocab_size = 1024u32;
    let mut logits = vec![0.0f32; vocab_size as usize];
    logits[42] = 100.0; // Make index 42 the maximum

    // Upload to GPU
    let logits_gpu = GpuBuffer::from_host(executor.context(), &logits).expect("upload logits");
    let logits_ptr = logits_gpu.as_ptr();

    let result = executor.gpu_argmax(logits_ptr, vocab_size);
    assert!(
        result.is_ok(),
        "gpu_argmax should succeed: {:?}",
        result.err()
    );
    assert_eq!(result.unwrap(), 42, "gpu_argmax should return index 42");
}

/// Test gpu_argmax with large vocab
#[test]
#[serial]
fn test_cov030_gpu_argmax_large_vocab() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Typical LLM vocab size
    let vocab_size = 32000u32;
    let mut logits = vec![-10.0f32; vocab_size as usize];
    logits[31999] = 50.0; // Last index is maximum

    let logits_gpu = GpuBuffer::from_host(executor.context(), &logits).expect("upload logits");
    let logits_ptr = logits_gpu.as_ptr();

    let result = executor.gpu_argmax(logits_ptr, vocab_size);
    assert!(result.is_ok(), "gpu_argmax with large vocab should succeed");
    assert_eq!(result.unwrap(), 31999, "Should find max at last index");
}

/// Test read_hidden_state_to_cpu error before workspace init
#[test]
#[serial]
fn test_cov030_read_hidden_state_error_before_init() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Before workspace init, should return error
    let result = executor.read_hidden_state_to_cpu();
    assert!(
        result.is_err(),
        "read_hidden_state_to_cpu should error before workspace init"
    );
}

/// Test transformer_layer_batched error validation
#[test]
#[serial]
fn test_cov030_transformer_layer_batched_validation() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Create a dummy input buffer
    let input_data = vec![0.1f32; 512 * 4];
    let input = GpuBuffer::from_host(executor.context(), &input_data).expect("input");

    // Create dummy IndexedLayerWeights
    let layer_weights = test_zeroed_layer_weights();

    // Without workspace init, should return error
    let result = executor.transformer_layer_batched(
        &input,
        0,
        &layer_weights,
        4,
        &[0, 1, 2, 3],
        512,
        256,
        1e-5,
    );
    assert!(
        result.is_err(),
        "transformer_layer_batched should error without workspace"
    );
}

/// Test transformer_layer_batched batch size mismatch
#[test]
#[serial]
fn test_cov030_transformer_layer_batched_mismatch() {
    if !CudaExecutor::is_available() {
        return;
    }
    let mut executor = CudaExecutor::new(0).expect("CUDA executor");

    // Init workspace for batch_size=2
    executor.init_workspace(512, 256).expect("init workspace");
    executor
        .init_batched_workspace(512, 256, 2)
        .expect("init batched");

    let input_data = vec![0.1f32; 512 * 4];
    let input = GpuBuffer::from_host(executor.context(), &input_data).expect("input");
    let layer_weights = test_zeroed_layer_weights();

    // Request batch_size=4 but workspace is for 2
    let result = executor.transformer_layer_batched(
        &input,
        0,
        &layer_weights,
        4, // mismatch!
        &[0, 1, 2, 3],
        512,
        256,
        1e-5,
    );
    assert!(result.is_err(), "Should error on batch size mismatch");
}