266 lines
12 KiB
Rust
266 lines
12 KiB
Rust
//! Changed GDN row transport only. The original scalar calls finish FIRST for
|
|
//! every comparison. Synthetic data does not certify a binding, row, layer or
|
|
//! model publication; it proves queued bytes, ordered refusal and cleanup.
|
|
use super::*;
|
|
use crate::Driver;
|
|
|
|
const ENABLE: &str = "LELEKA_STRA169_RUN_GDN_ROW_QUEUE";
|
|
const CHANNELS: usize = 10_240;
|
|
const HISTORY: usize = 30_720;
|
|
const STATE: usize = 786_432;
|
|
const VALUES: usize = 6_144;
|
|
const EPSILON: f32 = 1e-6;
|
|
|
|
#[derive(Default, Debug)]
|
|
struct Report {
|
|
words: usize,
|
|
differences: usize,
|
|
refusals: usize,
|
|
guards: usize,
|
|
rows: usize,
|
|
}
|
|
|
|
/// One complete row of inputs and outputs, all whole real owners.
|
|
struct Row<'a> {
|
|
history: DeviceBuffer<'a>,
|
|
qkv: DeviceBuffer<'a>,
|
|
weights: DeviceBuffer<'a>,
|
|
alpha: DeviceBuffer<'a>,
|
|
beta_raw: DeviceBuffer<'a>,
|
|
dt: DeviceBuffer<'a>,
|
|
a: DeviceBuffer<'a>,
|
|
gamma: DeviceBuffer<'a>,
|
|
z: DeviceBuffer<'a>,
|
|
state: DeviceBuffer<'a>,
|
|
next_history: DeviceBuffer<'a>,
|
|
raw: DeviceBuffer<'a>,
|
|
h: DeviceBuffer<'a>,
|
|
qk: DeviceBuffer<'a>,
|
|
sigmoid: DeviceBuffer<'a>,
|
|
gate: DeviceBuffer<'a>,
|
|
next_state: DeviceBuffer<'a>,
|
|
readout: DeviceBuffer<'a>,
|
|
normalized: DeviceBuffer<'a>,
|
|
}
|
|
#[derive(Clone, Copy, Default)]
|
|
struct Poison {
|
|
alpha: bool,
|
|
conv_weights: bool,
|
|
state: bool,
|
|
z: bool,
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "Root-only changed GDN row queue proof: exact scalar equivalence, ordered refusal, cleanup"]
|
|
fn queued_gdn_row_matches_scalar_calls_orders_refusals_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(256 * 1024 * 1024).unwrap();
|
|
let foreign = device.create_context(64 * 1024 * 1024).unwrap();
|
|
let mut report = Report::default();
|
|
let result = (|| -> Result<()> {
|
|
let mut queue = context.gdn_row_queue()?;
|
|
if queue.allocated_bytes() != STATUS_BYTES {
|
|
return Err(Error::InvalidDriverValue("unexpected queue charge"));
|
|
}
|
|
// Three clean rows on one queue: statuses are reset on every drain.
|
|
for salt in [0, 1, 2] {
|
|
clean_pair(&context, &mut queue, salt, &mut report)?;
|
|
}
|
|
// Earliest refusal in program order, then the queue stays usable.
|
|
let cases = [
|
|
(Poison { alpha: true, ..Poison::default() }, GdnQueueStage::Alpha),
|
|
(Poison { conv_weights: true, ..Poison::default() }, GdnQueueStage::ConvRaw),
|
|
(Poison { state: true, ..Poison::default() }, GdnQueueStage::NextState),
|
|
(Poison { z: true, ..Poison::default() }, GdnQueueStage::Z),
|
|
(Poison { alpha: true, z: true, ..Poison::default() }, GdnQueueStage::Alpha),
|
|
];
|
|
for (poison, stage) in cases {
|
|
refusal_case(&context, &mut queue, poison, stage, &mut report)?;
|
|
clean_pair(&context, &mut queue, 3, &mut report)?;
|
|
}
|
|
// Retained batched QKV rows: first, middle and last of a 3-row and a
|
|
// 64-row batch; a poisoned OTHER row is irrelevant, the USED row refuses.
|
|
for (tokens, used) in [(3usize, 0usize), (3, 1), (3, 2), (64, 63)] {
|
|
columns_pair(&context, &mut queue, 8, tokens, used, false, false, &mut report)?;
|
|
columns_pair(&context, &mut queue, 8, tokens, used, false, true, &mut report)?;
|
|
columns_pair(&context, &mut queue, 8, tokens, used, true, false, &mut report)?;
|
|
clean_pair(&context, &mut queue, 3, &mut report)?;
|
|
}
|
|
// Program order, data flow, aliasing, context and shape admission.
|
|
let row = Row::new(&context, 7, Poison::default())?;
|
|
let other = Row::new(&foreign, 7, Poison::default())?;
|
|
let admission = (|| -> Result<()> {
|
|
// Convolution before the two input scans.
|
|
let mut prefix = queue.begin()?;
|
|
refuse(
|
|
prefix.conv_silu(&row.history, &row.qkv, &row.weights, &row.next_history, &row.raw, &row.h),
|
|
Error::InvalidQwenShape,
|
|
&mut report,
|
|
)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
// Q/K over a buffer that is not the convolution H output.
|
|
let mut prefix = queue.begin()?;
|
|
prefix.scan_alpha(&row.alpha)?;
|
|
prefix.scan_beta_raw(&row.beta_raw)?;
|
|
prefix.conv_silu(&row.history, &row.qkv, &row.weights, &row.next_history, &row.raw, &row.h)?;
|
|
refuse(prefix.qk_l2(&row.raw, &row.qk, EPSILON), Error::InvalidQwenShape, &mut report)?;
|
|
// A latched admission error still drains the admitted prefix first.
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
// Recurrence whose current state and next state are one owner.
|
|
let mut prefix = queue.begin()?;
|
|
prefix.scan_alpha(&row.alpha)?;
|
|
prefix.scan_beta_raw(&row.beta_raw)?;
|
|
prefix.conv_silu(&row.history, &row.qkv, &row.weights, &row.next_history, &row.raw, &row.h)?;
|
|
prefix.qk_l2(&row.h, &row.qk, EPSILON)?;
|
|
prefix.beta_sigmoid(&row.beta_raw, &row.sigmoid)?;
|
|
prefix.gate_softplus(&row.alpha, &row.dt, &row.a, &row.gate)?;
|
|
refuse(
|
|
prefix.recurrence(&row.state, &row.qk, &row.h, &row.gate, &row.sigmoid, &row.state, &row.readout),
|
|
Error::OutputAliasesInput,
|
|
&mut report,
|
|
)?;
|
|
refuse(prefix.finish(), Error::OutputAliasesInput, &mut report)?;
|
|
// A buffer from another Context.
|
|
let mut prefix = queue.begin()?;
|
|
refuse(prefix.scan_alpha(&other.alpha), Error::ContextMismatch, &mut report)?;
|
|
refuse(prefix.finish(), Error::ContextMismatch, &mut report)?;
|
|
// A whole buffer of the wrong size.
|
|
let short = blank(&context, 47)?;
|
|
let mut prefix = queue.begin()?;
|
|
let result = prefix.scan_alpha(&short);
|
|
refuse(result, Error::InvalidQwenShape, &mut report)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
short.release()?;
|
|
// History fetch of a buffer that is not the convolution output,
|
|
// a second fetch, and a finish before the program is complete.
|
|
let mut prefix = queue.begin()?;
|
|
prefix.scan_alpha(&row.alpha)?;
|
|
prefix.scan_beta_raw(&row.beta_raw)?;
|
|
prefix.conv_silu(&row.history, &row.qkv, &row.weights, &row.next_history, &row.raw, &row.h)?;
|
|
refuse(prefix.fetch_next_history(&row.history), Error::InvalidQwenShape, &mut report)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
let mut prefix = queue.begin()?;
|
|
prefix.scan_alpha(&row.alpha)?;
|
|
let incomplete = prefix.finish();
|
|
refuse(incomplete, Error::InvalidQwenShape, &mut report)?;
|
|
// Retained-row admission: not a whole number of rows, a row beyond
|
|
// the batch, the host convolution after a column scan, a different
|
|
// row index or owner at the convolution, and a late column scan.
|
|
let ragged = blank(&context, CHANNELS + 1)?;
|
|
let image = upload(&context, &column_image(1, 3, 1))?;
|
|
let admission = (|| -> Result<()> {
|
|
let mut prefix = queue.begin()?;
|
|
refuse(prefix.scan_qkv_columns(&ragged, 0), Error::InvalidQwenShape, &mut report)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
let mut prefix = queue.begin()?;
|
|
refuse(prefix.scan_qkv_columns(&image, 3), Error::InvalidQwenShape, &mut report)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
let mut prefix = queue.begin()?;
|
|
prefix.scan_qkv_columns(&image, 1)?;
|
|
prefix.scan_alpha(&row.alpha)?;
|
|
prefix.scan_beta_raw(&row.beta_raw)?;
|
|
refuse(
|
|
prefix.conv_silu(&row.history, &row.qkv, &row.weights, &row.next_history, &row.raw, &row.h),
|
|
Error::InvalidQwenShape,
|
|
&mut report,
|
|
)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
let mut prefix = queue.begin()?;
|
|
prefix.scan_qkv_columns(&image, 1)?;
|
|
prefix.scan_alpha(&row.alpha)?;
|
|
prefix.scan_beta_raw(&row.beta_raw)?;
|
|
refuse(
|
|
prefix.conv_silu_columns(
|
|
&row.history,
|
|
&image,
|
|
2,
|
|
&row.weights,
|
|
&row.next_history,
|
|
&row.raw,
|
|
&row.h,
|
|
),
|
|
Error::InvalidQwenShape,
|
|
&mut report,
|
|
)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
let mut prefix = queue.begin()?;
|
|
prefix.scan_alpha(&row.alpha)?;
|
|
refuse(prefix.scan_qkv_columns(&image, 1), Error::InvalidQwenShape, &mut report)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)?;
|
|
// The retained-row conv over another owner than the scanned one.
|
|
let mut prefix = queue.begin()?;
|
|
prefix.scan_qkv_columns(&image, 1)?;
|
|
prefix.scan_alpha(&row.alpha)?;
|
|
prefix.scan_beta_raw(&row.beta_raw)?;
|
|
refuse(
|
|
prefix.conv_silu_columns(
|
|
&row.history,
|
|
&ragged,
|
|
1,
|
|
&row.weights,
|
|
&row.next_history,
|
|
&row.raw,
|
|
&row.h,
|
|
),
|
|
Error::InvalidQwenShape,
|
|
&mut report,
|
|
)?;
|
|
refuse(prefix.finish(), Error::InvalidQwenShape, &mut report)
|
|
})();
|
|
combine(combine(admission, ragged.release()), image.release())
|
|
})();
|
|
let cleanup = combine(row.release(), other.release());
|
|
combine(admission, cleanup)?;
|
|
// The queue survives every refusal above and still matches the scalar path.
|
|
clean_pair(&context, &mut queue, 4, &mut report)?;
|
|
queue.close()
|
|
})();
|
|
let allocated = (context.allocated_bytes(), foreign.allocated_bytes());
|
|
let closes = (context.close(), foreign.close());
|
|
result.expect("completed changed GDN row queue proof");
|
|
assert!(closes.0.is_ok() && closes.1.is_ok());
|
|
assert_eq!(allocated, (0, 0));
|
|
assert_eq!(report.differences, 0, "{report:?}");
|
|
// 3 + 5 clean rows after refusals + 1 final + per column group (12 clean_pair-or-columns).
|
|
assert_eq!(report.rows, 9 + 4 * (2 + 1));
|
|
println!(
|
|
"GDN_ROW_QUEUE PASS rows={} exact_words={} refusals={} guards={} allocated0=true",
|
|
report.rows, report.words, report.refusals, report.guards
|
|
);
|
|
}
|
|
|
|
#[path = "gdn_closed_queue_gpu/bytes_fn.rs"]
|
|
mod bytes_fn;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::blank;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::bytes;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::column_image;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::pattern;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::queued;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::queued_columns;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::read;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::scalar;
|
|
#[allow(unused_imports)]
|
|
use bytes_fn::upload;
|
|
#[path = "gdn_closed_queue_gpu/columns_pair_fn.rs"]
|
|
mod columns_pair_fn;
|
|
#[allow(unused_imports)]
|
|
use columns_pair_fn::clean_pair;
|
|
#[allow(unused_imports)]
|
|
use columns_pair_fn::columns_pair;
|
|
#[allow(unused_imports)]
|
|
use columns_pair_fn::compare;
|
|
#[allow(unused_imports)]
|
|
use columns_pair_fn::refusal_case;
|
|
#[allow(unused_imports)]
|
|
use columns_pair_fn::refuse;
|