vyre-driver-cuda 0.6.1

CUDA/PTX backend for vyre through the CUDA driver API.
Documentation
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Generated live CUDA/reference differential matrix for f32 IR semantics.

mod common;

use common::{
    assert_f32_output_lanes, assert_u32_output_lanes, cuda_reference_outputs, eq_word, f32_bytes,
    ge_word, gt_word, le_word, live_backend, lt_word, ne_word, GENERATED_LANE_COUNT as LANE_COUNT,
    GENERATED_WORKGROUP_SIZE_X as WORKGROUP_SIZE_X,
};
use vyre_foundation::ir::{BufferDecl, DataType, Expr, Node, Program};

const MAX_ARITH_ULP: u32 = 1;

const F32_ARITH_BITS: &[u32] = &[
    0x0000_0000, // +0
    0x8000_0000, // -0
    0x3f80_0000, // +1
    0xbf80_0000, // -1
    0x4000_0000, // +2
    0xc000_0000, // -2
    0x3f00_0000, // +0.5
    0xbf00_0000, // -0.5
    0x0080_0000, // smallest positive normal
    0x8080_0000, // largest-magnitude negative just-normal boundary
    0x7f7f_ffff, // max finite
    0xff7f_ffff, // min finite
    0x7f80_0000, // +inf
    0xff80_0000, // -inf
    0x7fc0_0000, // canonical quiet NaN
    0xffc0_0000, // negative quiet NaN
    0x7fa0_0001, // payload NaN
    0x3eaa_aaab, // 1/3 rounded
    0xbeaa_aaab, // -1/3 rounded
    0x4120_0000, // 10
    0xc120_0000, // -10
    0x447a_0000, // 1000
    0xc47a_0000, // -1000
];

const F32_CLASSIFY_BITS: &[u32] = &[
    0x0000_0000,
    0x8000_0000,
    0x0000_0001, // positive subnormal
    0x8000_0001, // negative subnormal
    0x007f_ffff, // largest positive subnormal
    0x807f_ffff, // largest negative subnormal
    0x0080_0000,
    0x8080_0000,
    0x3f80_0000,
    0xbf80_0000,
    0x7f7f_ffff,
    0xff7f_ffff,
    0x7f80_0000,
    0xff80_0000,
    0x7fc0_0000,
    0xffc0_0000,
    0x7fa0_0001,
    0x7fff_ffff,
    0xffff_ffff,
];

#[derive(Clone, Copy)]
struct F32BinaryCase {
    name: &'static str,
    rhs: F32RhsKind,
    build: fn(Expr, Expr) -> Expr,
}

#[derive(Clone, Copy)]
struct F32UnaryCase {
    name: &'static str,
    inputs: F32InputKind,
    build: fn(Expr) -> Expr,
}

#[derive(Clone, Copy)]
struct F32ClassifyCase {
    name: &'static str,
    build: fn(Expr) -> Expr,
}

#[derive(Clone, Copy)]
struct F32CompareCase {
    name: &'static str,
    build: fn(Expr, Expr) -> Expr,
}

#[derive(Clone, Copy)]
enum F32RhsKind {
    Mixed,
    NonZero,
}

#[derive(Clone, Copy)]
enum F32InputKind {
    Mixed,
    SqrtDomain,
}

fn isnan_word(value: Expr) -> Expr {
    Expr::select(Expr::is_nan(value), Expr::u32(1), Expr::u32(0))
}

fn isinf_word(value: Expr) -> Expr {
    Expr::select(Expr::is_inf(value), Expr::u32(1), Expr::u32(0))
}

fn isfinite_word(value: Expr) -> Expr {
    Expr::select(Expr::is_finite(value), Expr::u32(1), Expr::u32(0))
}

const F32_BINARY_CASES: &[F32BinaryCase] = &[
    F32BinaryCase {
        name: "f32_add",
        rhs: F32RhsKind::Mixed,
        build: Expr::add,
    },
    F32BinaryCase {
        name: "f32_sub",
        rhs: F32RhsKind::Mixed,
        build: Expr::sub,
    },
    F32BinaryCase {
        name: "f32_mul",
        rhs: F32RhsKind::Mixed,
        build: Expr::mul,
    },
    F32BinaryCase {
        name: "f32_div_nonzero",
        rhs: F32RhsKind::NonZero,
        build: Expr::div,
    },
    F32BinaryCase {
        name: "f32_min",
        rhs: F32RhsKind::Mixed,
        build: Expr::min,
    },
    F32BinaryCase {
        name: "f32_max",
        rhs: F32RhsKind::Mixed,
        build: Expr::max,
    },
];

const F32_COMPARE_CASES: &[F32CompareCase] = &[
    F32CompareCase {
        name: "f32_eq",
        build: eq_word,
    },
    F32CompareCase {
        name: "f32_ne",
        build: ne_word,
    },
    F32CompareCase {
        name: "f32_lt",
        build: lt_word,
    },
    F32CompareCase {
        name: "f32_le",
        build: le_word,
    },
    F32CompareCase {
        name: "f32_gt",
        build: gt_word,
    },
    F32CompareCase {
        name: "f32_ge",
        build: ge_word,
    },
];

const F32_UNARY_CASES: &[F32UnaryCase] = &[
    F32UnaryCase {
        name: "f32_negate",
        inputs: F32InputKind::Mixed,
        build: Expr::negate,
    },
    F32UnaryCase {
        name: "f32_abs",
        inputs: F32InputKind::Mixed,
        build: Expr::abs,
    },
    F32UnaryCase {
        name: "f32_sqrt",
        inputs: F32InputKind::SqrtDomain,
        build: Expr::sqrt,
    },
    F32UnaryCase {
        name: "f32_reciprocal",
        inputs: F32InputKind::Mixed,
        build: Expr::reciprocal,
    },
    F32UnaryCase {
        name: "f32_floor",
        inputs: F32InputKind::Mixed,
        build: Expr::floor,
    },
    F32UnaryCase {
        name: "f32_ceil",
        inputs: F32InputKind::Mixed,
        build: Expr::ceil,
    },
    F32UnaryCase {
        name: "f32_trunc",
        inputs: F32InputKind::Mixed,
        build: Expr::trunc,
    },
];

const F32_CLASSIFY_CASES: &[F32ClassifyCase] = &[
    F32ClassifyCase {
        name: "f32_is_nan",
        build: isnan_word,
    },
    F32ClassifyCase {
        name: "f32_is_inf",
        build: isinf_word,
    },
    F32ClassifyCase {
        name: "f32_is_finite",
        build: isfinite_word,
    },
];

#[test]
fn generated_f32_binary_matrix_matches_reference_on_live_cuda() {
    let backend = live_backend();
    let lhs = generated_f32_values(F32InputKind::Mixed, 0x1357_9bdf);
    let mut checked_lanes = 0usize;

    for case in F32_BINARY_CASES {
        let rhs = generated_f32_rhs(case.rhs, 0xf00d_cafe);
        let program = f32_binary_program(case);
        let inputs = vec![f32_bytes(&lhs), f32_bytes(&rhs)];
        let outputs = cuda_reference_outputs(&backend, &program, &inputs, case.name);
        checked_lanes += assert_f32_output_lanes(
            case.name,
            LANE_COUNT,
            MAX_ARITH_ULP,
            &outputs.direct_cuda,
            &outputs.reference,
        );
        checked_lanes += assert_f32_output_lanes(
            case.name,
            LANE_COUNT,
            MAX_ARITH_ULP,
            &outputs.compiled_cuda,
            &outputs.reference,
        );
    }

    assert_eq!(
        checked_lanes,
        F32_BINARY_CASES.len() * LANE_COUNT * 2,
        "Fix: generated CUDA f32 binary matrix must keep every adversarial lane active across direct and compiled paths."
    );
}

#[test]
fn generated_f32_unary_matrix_matches_reference_on_live_cuda() {
    let backend = live_backend();
    let mut checked_lanes = 0usize;

    for case in F32_UNARY_CASES {
        let input = generated_f32_values(case.inputs, 0x2468_ace0);
        let program = f32_unary_program(case);
        let inputs = vec![f32_bytes(&input)];
        let outputs = cuda_reference_outputs(&backend, &program, &inputs, case.name);
        checked_lanes += assert_f32_output_lanes(
            case.name,
            LANE_COUNT,
            MAX_ARITH_ULP,
            &outputs.direct_cuda,
            &outputs.reference,
        );
        checked_lanes += assert_f32_output_lanes(
            case.name,
            LANE_COUNT,
            MAX_ARITH_ULP,
            &outputs.compiled_cuda,
            &outputs.reference,
        );
    }

    assert_eq!(
        checked_lanes,
        F32_UNARY_CASES.len() * LANE_COUNT * 2,
        "Fix: generated CUDA f32 unary matrix must keep every adversarial lane active across direct and compiled paths."
    );
}

#[test]
fn generated_f32_classification_matrix_matches_reference_on_live_cuda() {
    let backend = live_backend();
    let input = generated_f32_classification_values();
    let mut checked_lanes = 0usize;

    for case in F32_CLASSIFY_CASES {
        let program = f32_classify_program(case);
        let inputs = vec![f32_bytes(&input)];
        let outputs = cuda_reference_outputs(&backend, &program, &inputs, case.name);
        checked_lanes += assert_u32_output_lanes(
            case.name,
            LANE_COUNT,
            &outputs.direct_cuda,
            &outputs.reference,
        );
        checked_lanes += assert_u32_output_lanes(
            case.name,
            LANE_COUNT,
            &outputs.compiled_cuda,
            &outputs.reference,
        );
    }

    assert_eq!(
        checked_lanes,
        F32_CLASSIFY_CASES.len() * LANE_COUNT * 2,
        "Fix: generated CUDA f32 classification matrix must keep every adversarial lane active across direct and compiled paths."
    );
}

#[test]
fn generated_f32_comparison_matrix_matches_reference_on_live_cuda() {
    let backend = live_backend();
    let lhs = generated_f32_values(F32InputKind::Mixed, 0x55aa_1234);
    let rhs = generated_f32_rhs(F32RhsKind::Mixed, 0xaa55_4321);
    let mut checked_lanes = 0usize;

    for case in F32_COMPARE_CASES {
        let program = f32_compare_program(case);
        let inputs = vec![f32_bytes(&lhs), f32_bytes(&rhs)];
        let outputs = cuda_reference_outputs(&backend, &program, &inputs, case.name);
        checked_lanes += assert_u32_output_lanes(
            case.name,
            LANE_COUNT,
            &outputs.direct_cuda,
            &outputs.reference,
        );
        checked_lanes += assert_u32_output_lanes(
            case.name,
            LANE_COUNT,
            &outputs.compiled_cuda,
            &outputs.reference,
        );
    }

    assert_eq!(
        checked_lanes,
        F32_COMPARE_CASES.len() * LANE_COUNT * 2,
        "Fix: generated CUDA f32 comparison matrix must keep NaN/Inf edge lanes active across direct and compiled paths."
    );
}

fn f32_binary_program(case: &F32BinaryCase) -> Program {
    let idx = Expr::var("idx");
    let value = (case.build)(
        Expr::load("lhs", idx.clone()),
        Expr::load("rhs", idx.clone()),
    );
    Program::wrapped(
        vec![
            BufferDecl::read("lhs", 0, DataType::F32).with_count(LANE_COUNT as u32),
            BufferDecl::read("rhs", 1, DataType::F32).with_count(LANE_COUNT as u32),
            BufferDecl::output("out", 2, DataType::F32).with_count(LANE_COUNT as u32),
        ],
        [WORKGROUP_SIZE_X, 1, 1],
        guarded_store(value),
    )
}

fn f32_compare_program(case: &F32CompareCase) -> Program {
    let idx = Expr::var("idx");
    let value = (case.build)(
        Expr::load("lhs", idx.clone()),
        Expr::load("rhs", idx.clone()),
    );
    Program::wrapped(
        vec![
            BufferDecl::read("lhs", 0, DataType::F32).with_count(LANE_COUNT as u32),
            BufferDecl::read("rhs", 1, DataType::F32).with_count(LANE_COUNT as u32),
            BufferDecl::output("out", 2, DataType::U32).with_count(LANE_COUNT as u32),
        ],
        [WORKGROUP_SIZE_X, 1, 1],
        guarded_store(value),
    )
}

fn f32_unary_program(case: &F32UnaryCase) -> Program {
    let idx = Expr::var("idx");
    let value = (case.build)(Expr::load("input", idx));
    Program::wrapped(
        vec![
            BufferDecl::read("input", 0, DataType::F32).with_count(LANE_COUNT as u32),
            BufferDecl::output("out", 1, DataType::F32).with_count(LANE_COUNT as u32),
        ],
        [WORKGROUP_SIZE_X, 1, 1],
        guarded_store(value),
    )
}

fn f32_classify_program(case: &F32ClassifyCase) -> Program {
    let idx = Expr::var("idx");
    let value = (case.build)(Expr::load("input", idx));
    Program::wrapped(
        vec![
            BufferDecl::read("input", 0, DataType::F32).with_count(LANE_COUNT as u32),
            BufferDecl::output("out", 1, DataType::U32).with_count(LANE_COUNT as u32),
        ],
        [WORKGROUP_SIZE_X, 1, 1],
        guarded_store(value),
    )
}

fn guarded_store(value: Expr) -> Vec<Node> {
    vec![
        Node::let_bind("idx", Expr::gid_x()),
        Node::if_then(
            Expr::lt(Expr::var("idx"), Expr::u32(LANE_COUNT as u32)),
            vec![Node::store("out", Expr::var("idx"), value)],
        ),
    ]
}

fn generated_f32_values(kind: F32InputKind, salt: u32) -> Vec<f32> {
    (0..LANE_COUNT)
        .map(|lane| {
            let seed = F32_ARITH_BITS[lane % F32_ARITH_BITS.len()];
            let lane_word = lane as u32;
            let mixed = lane_word
                .wrapping_mul(0x45d9_f3b)
                .rotate_left((lane_word & 15) + 1)
                ^ salt.rotate_right(lane_word & 31);
            let bits = match kind {
                F32InputKind::Mixed => seed,
                F32InputKind::SqrtDomain => seed & 0x7fff_ffff,
            };
            f32::from_bits(if lane % 5 == 0 {
                bits
            } else {
                bits ^ (mixed & 0x007f_ffff)
            })
        })
        .collect()
}

fn generated_f32_rhs(kind: F32RhsKind, salt: u32) -> Vec<f32> {
    generated_f32_values(F32InputKind::Mixed, salt)
        .into_iter()
        .enumerate()
        .map(|(lane, value)| match kind {
            F32RhsKind::Mixed => value,
            F32RhsKind::NonZero if value == 0.0 || lane % 17 == 0 => {
                f32::from_bits(0x3f80_0000 ^ ((lane as u32) << 12))
            }
            F32RhsKind::NonZero => value,
        })
        .collect()
}

fn generated_f32_classification_values() -> Vec<f32> {
    (0..LANE_COUNT)
        .map(|lane| f32::from_bits(F32_CLASSIFY_BITS[lane % F32_CLASSIFY_BITS.len()]))
        .collect()
}