Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
301 lines
10 KiB
Rust
301 lines
10 KiB
Rust
#![cfg(all(
|
|
feature = "cuda-driver",
|
|
any(target_os = "linux", target_os = "windows"),
|
|
target_pointer_width = "64"
|
|
))]
|
|
|
|
use leleka_cuda_driver::{Context, Device, DeviceBuffer, Driver, Error};
|
|
|
|
// Recorded binary32 expectations, independent of wrapper code and PTX output.
|
|
const ARITHMETIC: &[(&[u32], &[u32], u32)] = &[
|
|
(&[0x4b800000, 0x3f800000, 0xcb800000], &[0x3f800000; 3], 0),
|
|
(&[0xbf800000, 0x3f800001], &[0x3f800000, 0x3f7ffffe], 0),
|
|
(&[0x80000000], &[0x3f800000], 0),
|
|
(&[0], &[0xbf800000], 0),
|
|
(&[1], &[0x3f800000], 1),
|
|
(&[0x00800000], &[0x3f000000], 0x00400000),
|
|
(&[1], &[0x3f000000], 0),
|
|
(&[3], &[0x3f000000], 2),
|
|
(&[0x7f7fffff], &[0x40000000], 0x7f800000),
|
|
(&[0xff800000], &[0x3f800000], 0xff800000),
|
|
(&[0x7f800000, 0xff800000], &[0x3f800000; 2], 0x7fc00000),
|
|
(&[0x7fc00000], &[0x3f800000], 0x7fc00000),
|
|
(&[0x7f800000], &[0], 0x7fc00000),
|
|
];
|
|
const ADD_INPUTS: &[(u32, u32)] = &[
|
|
(0x80000000, 0x80000000),
|
|
(0, 0x80000000),
|
|
(1, 1),
|
|
(0x00800000, 0x80800000),
|
|
(0x3f800000, 0x33800000),
|
|
(0x7f7fffff, 0x7f7fffff),
|
|
(0xff800000, 0x3f800000),
|
|
(0x7f800000, 0xff800000),
|
|
(0x7fc00000, 0x3f800000),
|
|
];
|
|
|
|
struct MatvecCase {
|
|
matrix: Vec<f32>,
|
|
vector: Vec<f32>,
|
|
}
|
|
|
|
#[inline(never)]
|
|
fn rounded_product(a: f32, b: f32) -> f32 {
|
|
std::hint::black_box(a * b)
|
|
}
|
|
|
|
#[inline(never)]
|
|
fn rounded_add(a: f32, b: f32) -> f32 {
|
|
std::hint::black_box(a + b)
|
|
}
|
|
|
|
// Separate non-inlined operations publish a binary32 result at each step.
|
|
// No f64 accumulation, fused multiply/add, tolerance or PTX-derived reference.
|
|
fn oracle(case: &MatvecCase) -> Vec<f32> {
|
|
case.matrix
|
|
.chunks_exact(case.vector.len())
|
|
.map(|row| {
|
|
row.iter()
|
|
.zip(&case.vector)
|
|
.fold(0.0f32, |s, (&a, &b)| rounded_add(s, rounded_product(a, b)))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn bytes(values: &[f32]) -> Vec<u8> {
|
|
values.iter().flat_map(|v| v.to_ne_bytes()).collect()
|
|
}
|
|
|
|
fn upload<'a>(context: &'a Context<'_>, values: &[f32]) -> DeviceBuffer<'a> {
|
|
let mut buffer = context.allocate(values.len() * 4).expect("allocation");
|
|
buffer.copy_from(&bytes(values)).expect("upload");
|
|
buffer
|
|
}
|
|
|
|
fn read(buffer: &DeviceBuffer<'_>) -> Vec<u8> {
|
|
let mut output = vec![0; buffer.len()];
|
|
buffer.copy_to(&mut output).expect("readback");
|
|
output
|
|
}
|
|
|
|
fn assert_value(actual: f32, expected: f32) {
|
|
if expected.is_nan() {
|
|
assert!(actual.is_nan(), "expected NaN classification");
|
|
} else {
|
|
assert_eq!(actual.to_bits(), expected.to_bits(), "zero bit tolerance");
|
|
}
|
|
}
|
|
|
|
fn assert_output(buffer: &DeviceBuffer<'_>, expected: &[f32]) {
|
|
let actual = read(buffer);
|
|
assert_eq!(actual.len(), expected.len() * 4);
|
|
for (word, &expected) in actual.as_chunks::<4>().0.iter().zip(expected) {
|
|
assert_value(f32::from_ne_bytes(*word), expected);
|
|
}
|
|
}
|
|
|
|
fn finish(context: Context<'_>) {
|
|
assert_eq!(context.allocated_bytes(), 0, "all explicit frees succeeded");
|
|
context.close().expect("module unload and context destroy");
|
|
println!("CONTEXT_CLEANUP PASS allocated=0 close=ok");
|
|
}
|
|
|
|
fn double_output(context: &Context<'_>, input: &DeviceBuffer<'_>, values: &[f32]) {
|
|
let expected: Vec<_> = values.iter().map(|&v| rounded_add(v, v)).collect();
|
|
let mut output = upload(context, &vec![f32::NAN; values.len()]);
|
|
context
|
|
.vector_add_f32(input, input, &mut output)
|
|
.expect("interleaved add");
|
|
assert_output(&output, &expected);
|
|
output.release().expect("interleaved output free");
|
|
println!("VECTOR PASS count={} interleaved=true", values.len());
|
|
}
|
|
|
|
fn execute_matvec(context: &Context<'_>, case: &MatvecCase, interleave: bool) {
|
|
let expected = oracle(case);
|
|
let matrix = upload(context, &case.matrix);
|
|
let vector = upload(context, &case.vector);
|
|
let mut output = upload(context, &vec![f32::NAN; expected.len()]);
|
|
launch_matvec(context, case, [&matrix, &vector], &mut output);
|
|
assert_output(&output, &expected);
|
|
assert_eq!(read(&matrix), bytes(&case.matrix), "matrix unchanged");
|
|
assert_eq!(read(&vector), bytes(&case.vector), "vector unchanged");
|
|
if interleave {
|
|
double_output(context, &output, &expected);
|
|
assert_output(&output, &expected);
|
|
}
|
|
matrix.release().expect("matrix free");
|
|
vector.release().expect("vector free");
|
|
output.release().expect("output free");
|
|
assert_eq!(context.allocated_bytes(), 0);
|
|
println!(
|
|
"MATVEC PASS rows={} columns={}",
|
|
expected.len(),
|
|
case.vector.len()
|
|
);
|
|
}
|
|
|
|
fn launch_matvec(
|
|
context: &Context<'_>,
|
|
case: &MatvecCase,
|
|
inputs: [&DeviceBuffer<'_>; 2],
|
|
output: &mut DeviceBuffer<'_>,
|
|
) {
|
|
context
|
|
.matvec_f32(
|
|
inputs[0],
|
|
inputs[1],
|
|
output,
|
|
case.matrix.len() / case.vector.len(),
|
|
case.vector.len(),
|
|
)
|
|
.expect("installed-driver matvec launch, sync and restoration");
|
|
}
|
|
|
|
fn arithmetic(device: &Device<'_>) {
|
|
assert_eq!(rounded_add(rounded_add(16777216.0, -16777216.0), 1.0), 1.0);
|
|
assert_eq!(
|
|
f32::from_bits(0x3f800001)
|
|
.mul_add(f32::from_bits(0x3f7ffffe), -1.0)
|
|
.to_bits(),
|
|
0xa8800000
|
|
);
|
|
for (index, &(a, b, expected)) in ARITHMETIC.iter().enumerate() {
|
|
let case = MatvecCase {
|
|
matrix: a.iter().map(|&bits| f32::from_bits(bits)).collect(),
|
|
vector: b.iter().map(|&bits| f32::from_bits(bits)).collect(),
|
|
};
|
|
assert_value(oracle(&case)[0], f32::from_bits(expected));
|
|
let context = device.create_context(65536).expect("arithmetic context");
|
|
execute_matvec(&context, &case, false);
|
|
finish(context);
|
|
println!("ARITHMETIC PASS index={index} expected_bits={expected:08x}");
|
|
}
|
|
}
|
|
|
|
fn rectangular_case(rows: usize, columns: usize, offset: f32) -> MatvecCase {
|
|
MatvecCase {
|
|
matrix: (0..rows)
|
|
.flat_map(|r| (0..columns).map(move |c| offset + r as f32 * 0.25 + c as f32 * 0.125))
|
|
.collect(),
|
|
vector: (0..columns)
|
|
.map(|c| (c as f32 + 1.0) * if c.is_multiple_of(2) { 0.5 } else { -0.5 })
|
|
.collect(),
|
|
}
|
|
}
|
|
|
|
fn shapes(device: &Device<'_>) {
|
|
for rows in [1, 127, 128, 129] {
|
|
for columns in [1, 2, 3] {
|
|
let context = device.create_context(65536).expect("shape context");
|
|
execute_matvec(&context, &rectangular_case(rows, columns, 1.0), false);
|
|
finish(context);
|
|
}
|
|
}
|
|
let context = device
|
|
.create_context(65536)
|
|
.expect("larger partial block context");
|
|
execute_matvec(&context, &rectangular_case(1025, 7, 1.0), false);
|
|
finish(context);
|
|
}
|
|
|
|
fn shared_input(device: &Device<'_>) {
|
|
let context = device.create_context(16).expect("shared-input context");
|
|
let input = upload(&context, &[1.0, -2.0, 3.0]);
|
|
let mut output = upload(&context, &[f32::NAN]);
|
|
assert_eq!(context.allocated_bytes(), 16, "shared input charged once");
|
|
context
|
|
.matvec_f32(&input, &input, &mut output, 1, 3)
|
|
.expect("shared immutable input");
|
|
assert_output(&output, &[14.0]);
|
|
assert_eq!(read(&input), bytes(&[1.0, -2.0, 3.0]));
|
|
input.release().expect("shared input free");
|
|
output.release().expect("shared output free");
|
|
finish(context);
|
|
println!("MATVEC PASS rows=1 columns=3 shared_input=true");
|
|
}
|
|
|
|
fn foreign_refusal(first: &Context<'_>, second: &Context<'_>) {
|
|
let matrix = upload(first, &[2.0]);
|
|
let vector = upload(second, &[3.0]);
|
|
let mut output = upload(second, &[-0.0]);
|
|
assert_eq!(
|
|
second.matvec_f32(&matrix, &vector, &mut output, 1, 1),
|
|
Err(Error::ContextMismatch)
|
|
);
|
|
assert_eq!(
|
|
second.matvec_f32(&vector, &vector, &mut output, 0, 1),
|
|
Err(Error::InvalidMatvecShape)
|
|
);
|
|
assert_eq!(read(&output), bytes(&[-0.0]), "refusals preserve output");
|
|
assert_eq!(first.allocated_bytes(), 4);
|
|
assert_eq!(second.allocated_bytes(), 8);
|
|
matrix.release().expect("foreign matrix free");
|
|
vector.release().expect("foreign vector free");
|
|
output.release().expect("foreign output free");
|
|
println!("ADMISSION PASS foreign_context and zero_rows");
|
|
}
|
|
|
|
fn two_contexts(device: &Device<'_>) {
|
|
let first = device
|
|
.create_context(65536)
|
|
.expect("first concurrent context");
|
|
let second = device
|
|
.create_context(65536)
|
|
.expect("second concurrent context");
|
|
foreign_refusal(&first, &second);
|
|
for _ in 0..2 {
|
|
execute_matvec(&first, &rectangular_case(129, 3, 1.0), true);
|
|
execute_matvec(&second, &rectangular_case(127, 2, -2.0), true);
|
|
}
|
|
finish(first);
|
|
finish(second);
|
|
}
|
|
|
|
fn vector_case(device: &Device<'_>, count: usize) {
|
|
let pairs: Vec<_> = (0..count)
|
|
.map(|i| ADD_INPUTS[i % ADD_INPUTS.len()])
|
|
.collect();
|
|
let a: Vec<_> = pairs.iter().map(|&(a, _)| f32::from_bits(a)).collect();
|
|
let b: Vec<_> = pairs.iter().map(|&(_, b)| f32::from_bits(b)).collect();
|
|
let expected: Vec<_> = a.iter().zip(&b).map(|(&a, &b)| rounded_add(a, b)).collect();
|
|
assert_eq!(
|
|
expected[0].to_bits(),
|
|
0x80000000,
|
|
"negative-zero add reference"
|
|
);
|
|
let context = device.create_context(count * 12).expect("vector context");
|
|
let input_a = upload(&context, &a);
|
|
let input_b = upload(&context, &b);
|
|
let mut output = upload(&context, &vec![f32::NAN; count]);
|
|
context
|
|
.vector_add_f32(&input_a, &input_b, &mut output)
|
|
.expect("installed-driver vector add");
|
|
assert_output(&output, &expected);
|
|
assert_eq!(read(&input_a), bytes(&a));
|
|
assert_eq!(read(&input_b), bytes(&b));
|
|
input_a.release().expect("vector a free");
|
|
input_b.release().expect("vector b free");
|
|
output.release().expect("vector output free");
|
|
finish(context);
|
|
println!("VECTOR PASS count={count} interleaved=false");
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "requires explicit installed-driver GPU experiment authorization"]
|
|
fn installed_driver_fixed_kernels() {
|
|
assert_eq!(std::env::var("LELEKA_CUDA_REAL_GPU").as_deref(), Ok("1"));
|
|
println!("TOLERANCE finite/zero/infinity=exact_bits NaN=classification");
|
|
let driver = Driver::load().expect("trusted installed libcuda.so.1");
|
|
println!("DRIVER API version={}", driver.version());
|
|
let device = driver.device(0).expect("authorized GPU ordinal zero");
|
|
arithmetic(&device);
|
|
shapes(&device);
|
|
shared_input(&device);
|
|
two_contexts(&device);
|
|
for count in [1, 127, 128, 129, 1025] {
|
|
vector_case(&device, count);
|
|
}
|
|
println!("GPU_NUMERICAL PASS matvec=31 vector=9 arithmetic=13 contexts_closed=34 refusals=2");
|
|
}
|