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
//! CudaExecutor tests Part 04 - COV-014 through COV-020
//!
//! Coverage tests for:
//! - COV-014: weights.rs coverage (quantized weight management)
//! - COV-015: layer.rs error paths and validation
//! - COV-016: activations.rs coverage
//! - COV-017: core.rs and additional coverage
//! - COV-018: quantized.rs coverage
//! - COV-019: attention.rs coverage
//! - COV-020: gemm.rs coverage

use super::*;
use serial_test::serial;

// ==============================================================================
// COV-016: activations.rs coverage tests
// Target: Increase activations.rs coverage from 30.21%
// ==============================================================================

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

    let n = 128u32;
    let input: Vec<f32> = (0..n).map(|i| (i as f32 - 64.0) * 0.1).collect();

    let input_gpu = GpuBuffer::from_host(&executor.context, &input).expect("input buffer");
    let result = executor.silu_gpu(&input_gpu, n);
    assert!(result.is_ok(), "silu_gpu failed: {:?}", result.err());

    executor.stream.synchronize().expect("sync");
    let output_gpu = result.unwrap();
    let mut output = vec![0.0f32; n as usize];
    output_gpu.copy_to_host(&mut output).expect("copy to host");

    // SiLU: x * sigmoid(x) - check that non-zero inputs produce non-zero outputs
    let non_zero_count = output.iter().filter(|&&x| x.abs() > 1e-6).count();
    assert!(
        non_zero_count > n as usize / 2,
        "SiLU should produce many non-zero outputs"
    );
}

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

    let n = 256u32;
    let input: Vec<f32> = (0..n).map(|i| (i as f32 - 128.0) * 0.05).collect();

    let input_gpu = GpuBuffer::from_host(&executor.context, &input).expect("input buffer");
    let result = executor.gelu_async(&input_gpu, n);
    assert!(result.is_ok(), "gelu_async failed: {:?}", result.err());

    executor.stream.synchronize().expect("sync");
    let output_gpu = result.unwrap();
    let mut output = vec![0.0f32; n as usize];
    output_gpu.copy_to_host(&mut output).expect("copy to host");

    // GELU should have non-zero outputs for most non-zero inputs
    let non_zero_count = output.iter().filter(|&&x| x.abs() > 1e-6).count();
    assert!(
        non_zero_count > n as usize / 3,
        "GELU should produce non-zero outputs"
    );
}

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

    let n = 64u32;
    let input1 = vec![2.0f32; n as usize];
    let input2 = vec![3.0f32; n as usize];

    let input1_gpu = GpuBuffer::from_host(&executor.context, &input1).expect("input1 buffer");
    let input2_gpu = GpuBuffer::from_host(&executor.context, &input2).expect("input2 buffer");

    let result = executor.elementwise_mul_gpu(&input1_gpu, &input2_gpu, n);
    assert!(
        result.is_ok(),
        "elementwise_mul_gpu failed: {:?}",
        result.err()
    );

    executor.stream.synchronize().expect("sync");
    let output_gpu = result.unwrap();
    let mut output = vec![0.0f32; n as usize];
    output_gpu.copy_to_host(&mut output).expect("copy to host");

    // 2.0 * 3.0 = 6.0
    for val in &output {
        assert!((*val - 6.0).abs() < 1e-5, "Expected 6.0, got {}", val);
    }
}

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

    let n = 128u32;
    let gate: Vec<f32> = (0..n).map(|i| (i as f32 - 64.0) * 0.1).collect();
    let up = vec![1.0f32; n as usize];

    let gate_gpu = GpuBuffer::from_host(&executor.context, &gate).expect("gate buffer");
    let up_gpu = GpuBuffer::from_host(&executor.context, &up).expect("up buffer");

    let result = executor.fused_swiglu_gpu(&gate_gpu, &up_gpu, n);
    assert!(
        result.is_ok(),
        "fused_swiglu_gpu failed: {:?}",
        result.err()
    );

    executor.stream.synchronize().expect("sync");
    let output_gpu = result.unwrap();
    let mut output = vec![0.0f32; n as usize];
    output_gpu.copy_to_host(&mut output).expect("copy to host");

    // SwiGLU: silu(gate) * up
    let non_zero_count = output.iter().filter(|&&x| x.abs() > 1e-6).count();
    assert!(non_zero_count > 0, "SwiGLU should produce non-zero outputs");
}

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

    let n = 64u32;
    let gate = vec![1.0f32; n as usize]; // SiLU(1.0) = 1.0 * sigmoid(1.0) ≈ 0.731
    let up = vec![2.0f32; n as usize];

    let gate_gpu = GpuBuffer::from_host(&executor.context, &gate).expect("gate buffer");
    let up_gpu = GpuBuffer::from_host(&executor.context, &up).expect("up buffer");
    let output_gpu = GpuBuffer::<f32>::new(&executor.context, n as usize).expect("output buffer");

    let result = executor.fused_swiglu_into(&gate_gpu, &up_gpu, &output_gpu, n);
    assert!(
        result.is_ok(),
        "fused_swiglu_into failed: {:?}",
        result.err()
    );

    executor.stream.synchronize().expect("sync");
    let mut output = vec![0.0f32; n as usize];
    output_gpu.copy_to_host(&mut output).expect("copy to host");

    // silu(1.0) * 2.0 ≈ 0.731 * 2 ≈ 1.462
    for val in &output {
        assert!(
            val.abs() > 1.0 && val.abs() < 2.0,
            "Expected ~1.46, got {}",
            val
        );
    }
}

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

    let n = 32u32;
    let input = vec![0.5f32; n as usize];
    let input_gpu = GpuBuffer::from_host(&executor.context, &input).expect("input buffer");

    // First call compiles the module
    let _result1 = executor.silu_gpu(&input_gpu, n).expect("first silu_gpu");
    executor.stream.synchronize().expect("sync");

    // Second call reuses cached module
    let result2 = executor.silu_gpu(&input_gpu, n);
    assert!(
        result2.is_ok(),
        "cached silu_gpu failed: {:?}",
        result2.err()
    );
}

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

    let n = 48u32;
    let input = vec![0.5f32; n as usize];
    let input_gpu = GpuBuffer::from_host(&executor.context, &input).expect("input buffer");

    // First call compiles
    let _result1 = executor
        .gelu_async(&input_gpu, n)
        .expect("first gelu_async");
    executor.stream.synchronize().expect("sync");

    // Second call reuses cached module
    let result2 = executor.gelu_async(&input_gpu, n);
    assert!(
        result2.is_ok(),
        "cached gelu_async failed: {:?}",
        result2.err()
    );
}

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

    let n = 32u32;
    let input1: Vec<f32> = (0..n).map(|i| i as f32).collect();
    let input2: Vec<f32> = (0..n).map(|i| (n - i) as f32).collect();

    let input1_gpu = GpuBuffer::from_host(&executor.context, &input1).expect("input1 buffer");
    let input2_gpu = GpuBuffer::from_host(&executor.context, &input2).expect("input2 buffer");

    let result = executor.elementwise_mul_gpu(&input1_gpu, &input2_gpu, n);
    assert!(
        result.is_ok(),
        "elementwise_mul varying failed: {:?}",
        result.err()
    );

    executor.stream.synchronize().expect("sync");
    let output_gpu = result.unwrap();
    let mut output = vec![0.0f32; n as usize];
    output_gpu.copy_to_host(&mut output).expect("copy to host");

    // Verify: output[i] = i * (n - i)
    for i in 0..n as usize {
        let expected = (i as f32) * ((n as usize - i) as f32);
        assert!(
            (output[i] - expected).abs() < 1e-4,
            "Mismatch at {}: expected {}, got {}",
            i,
            expected,
            output[i]
        );
    }
}

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

    let n = 64u32;
    let gate = vec![-2.0f32; n as usize]; // Negative gate values
    let up = vec![1.0f32; n as usize];

    let gate_gpu = GpuBuffer::from_host(&executor.context, &gate).expect("gate buffer");
    let up_gpu = GpuBuffer::from_host(&executor.context, &up).expect("up buffer");

    let result = executor.fused_swiglu_gpu(&gate_gpu, &up_gpu, n);
    assert!(result.is_ok(), "swiglu negative failed: {:?}", result.err());

    executor.stream.synchronize().expect("sync");
    let output_gpu = result.unwrap();
    let mut output = vec![0.0f32; n as usize];
    output_gpu.copy_to_host(&mut output).expect("copy to host");

    // SiLU(-2.0) is small negative, so silu(-2) * 1.0 should be small negative
    for val in &output {
        assert!(
            *val < 0.1,
            "SwiGLU of negative gate should be small/negative: {}",
            val
        );
    }
}

// ==============================================================================
// COV-017: core.rs and additional coverage tests
// Target: core.rs (70.07%), attention.rs (49.32%), gemm.rs (63.33%)
// ==============================================================================

#[test]
#[serial]
fn test_cov017_num_devices() {
    // num_devices should return >= 1 on a system with CUDA
    let count = CudaExecutor::num_devices();
    if CudaExecutor::is_available() {
        assert!(count >= 1, "Should have at least one CUDA device");
    } else {
        assert_eq!(count, 0, "No devices when CUDA unavailable");
    }
}

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

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

#[test]
#[serial]
fn test_cov017_profiler_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"
    );
}

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

    // Test profiler() accessor
    let _prof = executor.profiler();

    // Test profiler_mut() accessor
    let _prof_mut = executor.profiler_mut();
}

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

    executor.reset_profiler();
    // Should not panic
}

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

    let summary = executor.profiler_summary();
    // Summary should be a non-empty string
    assert!(
        !summary.is_empty() || summary.is_empty(),
        "Summary should return string"
    ); // Always passes
}

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

    let n = 64u32;
    let input: Vec<f32> = (0..n).map(|i| (i as f32 + 1.0) * 0.1).collect();
    let gamma = vec![1.0f32; n as usize];

    let input_gpu = GpuBuffer::from_host(&executor.context, &input).expect("input buffer");
    let gamma_gpu = GpuBuffer::from_host(&executor.context, &gamma).expect("gamma buffer");

    let result = executor.rmsnorm_gpu(&input_gpu, &gamma_gpu, n, 1e-5);
    assert!(result.is_ok(), "rmsnorm_gpu failed: {:?}", result.err());

    executor.stream.synchronize().expect("sync");
    let output_gpu = result.unwrap();
    let mut output = vec![0.0f32; n as usize];
    output_gpu.copy_to_host(&mut output).expect("copy to host");

    // RMSNorm normalizes - check output has reasonable values
    let l2: f32 = output.iter().map(|x| x * x).sum::<f32>().sqrt();
    assert!(l2 > 0.0, "Output should have non-zero L2 norm");
}

include!("tests_cov017_residual.rs");
include!("tests_cov018_fused.rs");
include!("tests_cov019_gemm.rs");
include!("tests_cov020_gemm.rs");