Files
leleka/crates/cuda-driver/src/gdn_native_batch_gpu.rs
T

175 lines
5.8 KiB
Rust

//! Root-only ignored smoke of the Strata-prefill GDN program (option B). It
//! proves transport, order, geometry, immutability, refusal and cleanup of the
//! new program with synthetic data and a host-side stand-in for the closed
//! cuBLAS adapter. It is NOT the Original17 prefill numerical gate: tensors
//! produced by the real adapter and the captured prompt-path references are
//! compared by that separate gate, never by this test.
use super::*;
use crate::Driver;
use std::cell::{Cell, RefCell};
const ENABLE: &str = "LELEKA_STRA169_RUN_GDN_BATCH";
const EPS: f32 = GDN_BATCH_EPSILON;
const Q8_0_BLOCK: usize = 34;
// ---------------------------------------------------------------- host reference of the original kernels
struct Reference {
h: Vec<f32>,
next_history: Vec<f32>,
next_state: Vec<f32>,
y: Vec<f32>,
y16: Vec<u16>,
}
// ---------------------------------------------------------------- host stand-in for the cuBLAS adapter
#[derive(Default, Clone, Copy)]
struct Poison {
z: bool,
ab: bool,
dt: bool,
state: bool,
}
#[derive(Default)]
struct Captured {
scratch: Vec<(usize, Vec<u16>)>, // (rows, FP16 matrix prefix)
x: Vec<(usize, Vec<u8>)>, // (depth, activation bytes)
y: Vec<(usize, Vec<f32>)>, // (rows, result words written by call)
}
struct HostGemm {
poison: Poison,
captured: RefCell<Captured>,
calls: Cell<usize>,
}
/// Device copies of the immutable inputs and the weights.
struct FixtureInputs<'a> {
mixed: DeviceBuffer<'a>,
mixed_h: DeviceBuffer<'a>,
mixed_bf: DeviceBuffer<'a>,
mixed_bf_lo: DeviceBuffer<'a>,
qkv_w: DeviceBuffer<'a>,
z_w: DeviceBuffer<'a>,
out_w: DeviceBuffer<'a>,
alpha_w: DeviceBuffer<'a>,
beta_w: DeviceBuffer<'a>,
scratch: DeviceBuffer<'a>,
dt: DeviceBuffer<'a>,
a: DeviceBuffer<'a>,
history: DeviceBuffer<'a>,
conv_w: DeviceBuffer<'a>,
state: DeviceBuffer<'a>,
gamma: DeviceBuffer<'a>,
}
/// The buffers the program writes.
struct FixtureOutputs<'a> {
qkv: DeviceBuffer<'a>,
z: DeviceBuffer<'a>,
ab: DeviceBuffer<'a>,
gate: DeviceBuffer<'a>,
beta: DeviceBuffer<'a>,
next_history: DeviceBuffer<'a>,
h: DeviceBuffer<'a>,
next_state: DeviceBuffer<'a>,
y: DeviceBuffer<'a>,
y16: DeviceBuffer<'a>,
bo: DeviceBuffer<'a>,
}
/// Host copies of the immutable inputs.
struct HostInputs {
mixed: Vec<f32>,
history: Vec<f32>,
conv_w: Vec<f32>,
dt: Vec<f32>,
a: Vec<f32>,
gamma: Vec<f32>,
state: Vec<f32>,
}
struct Fixture<'a> {
inp: FixtureInputs<'a>,
out: FixtureOutputs<'a>,
host: HostInputs,
}
#[test]
#[ignore = "Root-only GPU qualification; never run by the source owner"]
fn strata_prefill_gdn_program_runs_in_order_refuses_earliest_and_cleans_up() {
assert_eq!(std::env::var(ENABLE).as_deref(), Ok("yes"));
let driver = Driver::load().unwrap();
let device = driver.device(0).unwrap();
let context = device.create_context(768 * 1024 * 1024).unwrap();
let result = (|| -> Result<()> {
let mut batch = context.gdn_prefill_batch()?;
if batch.allocated_bytes() != STATUS_BYTES {
return mismatch("unexpected owner charge");
}
run_cases(&context, &mut batch)?;
// Admission refusals leave nothing behind.
let idle =
HostGemm { poison: Poison::default(), captured: RefCell::new(Captured::default()), calls: Cell::new(0) };
if batch.begin(0, &idle).is_ok() || batch.begin(GDN_BATCH_MAX_TOKENS + 1, &idle).is_ok() {
return mismatch("token bounds not enforced");
}
batch.close()
})();
let leaked = context.allocated_bytes();
assert!(result.is_ok(), "{result:?}");
assert_eq!(leaked, 0, "bytes still allocated after close");
}
/// The clean chunks, the earliest-refusal cases and the clean chunk after them, all on one owner.
fn run_cases(context: &Context<'_>, batch: &mut GdnPrefillBatch<'_>) -> Result<()> {
// Serial-conv chunk, tiled-conv chunk, then the same owner again.
for tokens in [2, 70, 5] {
run_case(context, batch, tokens, Poison::default(), None)?;
}
// Earliest stage in program order wins and the owner stays usable.
run_case(context, batch, 4, Poison { z: true, ab: true, ..Poison::default() }, Some(GdnBatchStage::Z))?;
run_case(context, batch, 4, Poison { ab: true, ..Poison::default() }, Some(GdnBatchStage::Ab))?;
run_case(context, batch, 4, Poison { dt: true, ..Poison::default() }, Some(GdnBatchStage::Gate))?;
// A non-finite prior state is caught by the recurrence kernel's own witness
// before the final-state scan could see it.
run_case(context, batch, 4, Poison { state: true, ..Poison::default() }, Some(GdnBatchStage::StateSteps))?;
run_case(context, batch, 3, Poison::default(), None)
}
#[path = "gdn_native_batch_gpu/bytes_of_fn.rs"]
mod bytes_of_fn;
#[allow(unused_imports)]
use bytes_of_fn::bf_bits;
#[allow(unused_imports)]
use bytes_of_fn::bf_value;
#[allow(unused_imports)]
use bytes_of_fn::bytes_of;
#[allow(unused_imports)]
use bytes_of_fn::close;
#[allow(unused_imports)]
use bytes_of_fn::f16_bits;
#[allow(unused_imports)]
use bytes_of_fn::f16_value;
#[allow(unused_imports)]
use bytes_of_fn::halves_of;
#[allow(unused_imports)]
use bytes_of_fn::pattern;
#[allow(unused_imports)]
use bytes_of_fn::q8_0;
#[allow(unused_imports)]
use bytes_of_fn::reference;
#[allow(unused_imports)]
use bytes_of_fn::sigm;
#[allow(unused_imports)]
use bytes_of_fn::words_of;
#[path = "gdn_native_batch_gpu/impl_hostgemm.rs"]
mod impl_hostgemm;
#[allow(unused_imports)]
use impl_hostgemm::mismatch;
#[allow(unused_imports)]
use impl_hostgemm::read;
#[allow(unused_imports)]
use impl_hostgemm::upload;
#[path = "gdn_native_batch_gpu/run_case_fn.rs"]
mod run_case_fn;
#[allow(unused_imports)]
use run_case_fn::check_clean;
#[allow(unused_imports)]
use run_case_fn::run_case;