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
438
439

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

    // Test softmax with small vector
    let mut data = vec![1.0f32, 2.0, 3.0, 4.0];
    let result = executor.softmax(&mut data);
    assert!(result.is_ok(), "softmax failed: {:?}", result.err());

    // Verify softmax properties
    let sum: f32 = data.iter().sum();
    assert!(
        (sum - 1.0).abs() < 0.01,
        "Softmax should sum to 1, got {}",
        sum
    );

    // Verify monotonicity (higher input -> higher output)
    for i in 1..data.len() {
        assert!(data[i] > data[i - 1], "Softmax should preserve ordering");
    }
}

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

    // Test with 32-element vector (warp-aligned)
    let mut data: Vec<f32> = (0..32).map(|i| (i as f32) / 10.0).collect();
    let result = executor.softmax(&mut data);
    assert!(result.is_ok(), "softmax larger failed: {:?}", result.err());

    // Softmax should produce valid probabilities (all positive)
    assert!(
        data.iter().all(|&x| x > 0.0),
        "Softmax outputs should be positive"
    );
    // Last element should be largest (highest input)
    assert!(
        data[31] > data[0],
        "Highest input should have highest probability"
    );
}

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

    // Uniform input should give uniform output
    let n = 8;
    let mut data = vec![0.0f32; n];
    let result = executor.softmax(&mut data);
    assert!(result.is_ok(), "softmax uniform failed: {:?}", result.err());

    // All should be 1/n
    let expected = 1.0 / n as f32;
    for (i, &val) in data.iter().enumerate() {
        assert!(
            (val - expected).abs() < 0.01,
            "Uniform softmax[{}] should be {}, got {}",
            i,
            expected,
            val
        );
    }
}

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

    // Small matrix multiplication: C = A * B
    // A is 4x4, B is 4x4, C is 4x4
    let m = 4u32;
    let n = 4u32;
    let k = 4u32;

    // Identity-like matrix A (ones on diagonal)
    let a = vec![
        1.0f32, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
    ];

    // B = some values
    let b = vec![
        1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
    ];

    let mut c = vec![0.0f32; (m * n) as usize];

    let result = executor.gemm(&a, &b, &mut c, m, n, k);
    assert!(result.is_ok(), "gemm failed: {:?}", result.err());

    // For identity * B, result should be B
    for (idx, &val) in c.iter().enumerate() {
        assert!(
            (val - b[idx]).abs() < 1e-3,
            "gemm identity mismatch at {}: {} vs {}",
            idx,
            val,
            b[idx]
        );
    }
}

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

    // Larger matrix: 32x32 * 32x32
    let m = 32u32;
    let n = 32u32;
    let k = 32u32;

    let a = vec![1.0f32; (m * k) as usize];
    let b = vec![1.0f32; (k * n) as usize];
    let mut c = vec![0.0f32; (m * n) as usize];

    let result = executor.gemm(&a, &b, &mut c, m, n, k);
    assert!(result.is_ok(), "gemm larger failed: {:?}", result.err());

    // Each element should be k (sum of k ones)
    for (idx, &val) in c.iter().enumerate() {
        assert!(
            (val - k as f32).abs() < 1.0,
            "gemm[{}] should be {}, got {}",
            idx,
            k,
            val
        );
    }
}

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

    let input_buf = GpuBuffer::from_host(&executor.context, &[1.0f32; 32]).expect("input");
    let output_buf = GpuBuffer::<f32>::new(&executor.context, 32).expect("output");

    // Try to use non-existent cached weight
    let result =
        executor.gemm_cached_async("nonexistent_weight", &input_buf, &output_buf, 32, 1, 32);
    assert!(
        result.is_err(),
        "gemm_cached_async should fail for non-existent weight"
    );
}

// ============================================================================
// COV-010: core.rs coverage tests
// Target: Increase coverage from 62.68% to 80%+
// Focus: profiler API, graph tracking, tile profiling, device info, pool stats
// ============================================================================

#[test]
#[serial]
fn test_cov010_num_devices() {
    if !CudaExecutor::is_available() {
        return;
    }
    let count = CudaExecutor::num_devices();
    assert!(count >= 1, "Should have at least 1 CUDA device");
}

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

    let result = executor.make_current();
    assert!(result.is_ok(), "make_current failed: {:?}", result.err());
}

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

    // Initially disabled
    assert!(
        !executor.is_profiling_enabled(),
        "Profiling should be disabled initially"
    );

    // Enable
    executor.enable_profiling();
    assert!(
        executor.is_profiling_enabled(),
        "Profiling should be enabled"
    );

    // Disable
    executor.disable_profiling();
    assert!(
        !executor.is_profiling_enabled(),
        "Profiling should be disabled again"
    );
}

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

    // Get profiler (immutable)
    let _profiler = executor.profiler();

    // Get profiler (mutable)
    let _profiler_mut = executor.profiler_mut();

    // Reset profiler
    executor.reset_profiler();
}

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

    let summary = executor.profiler_summary();
    // Summary should be a string (might be empty if no profiling data)
    assert!(summary.is_empty() || !summary.is_empty());
}

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

    // Get default sync mode
    let _mode = executor.profiler_sync_mode();

    // Set sync mode to deferred
    executor.set_profiler_sync_mode(trueno::SyncMode::Deferred);
    assert_eq!(executor.profiler_sync_mode(), trueno::SyncMode::Deferred);
}

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

    // Get category stats
    let stats = executor.profiler_category_stats();
    assert_eq!(stats.len(), trueno::BrickCategory::COUNT);
}

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

    // This prints to stdout, just verify it doesn't panic
    executor.print_profiler_categories();
}

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

    // Initially disabled
    assert!(
        !executor.is_graph_tracking_enabled(),
        "Graph tracking should be disabled initially"
    );

    // Enable
    executor.enable_graph_tracking();
    assert!(
        executor.is_graph_tracking_enabled(),
        "Graph tracking should be enabled"
    );

    // Disable
    executor.disable_graph_tracking();
    assert!(
        !executor.is_graph_tracking_enabled(),
        "Graph tracking should be disabled again"
    );
}

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

    // Get execution graph
    let _graph = executor.execution_graph();

    // Get ASCII tree
    let _ascii = executor.execution_graph_ascii();
}

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

    // Clear graph (should not panic even when empty)
    executor.clear_execution_graph();
}

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

    // Initially disabled
    assert!(
        !executor.is_tile_profiling_enabled(),
        "Tile profiling should be disabled initially"
    );

    // Enable
    executor.enable_tile_profiling();
    assert!(
        executor.is_tile_profiling_enabled(),
        "Tile profiling should be enabled"
    );

    // Disable
    executor.disable_tile_profiling();
    assert!(
        !executor.is_tile_profiling_enabled(),
        "Tile profiling should be disabled again"
    );
}

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

    let summary = executor.tile_summary();
    // Summary should be a string
    assert!(summary.is_empty() || !summary.is_empty());
}

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

    let json = executor.tile_stats_json();
    // JSON should be a valid string
    assert!(json.starts_with('{') || json.starts_with('[') || json.is_empty() || !json.is_empty());
}

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

    // Reset tile stats (should not panic)
    executor.reset_tile_stats();
}

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

    let result = executor.device_name();
    assert!(result.is_ok(), "device_name failed: {:?}", result.err());

    let name = result.unwrap();
    assert!(!name.is_empty(), "Device name should not be empty");
}