313 lines
12 KiB
Rust
313 lines
12 KiB
Rust
//! Changed queue proof only; the actual cached full Key/Value operators run first.
|
|
use super::*;
|
|
use crate::Driver;
|
|
|
|
fn upload<'ctx>(context: &'ctx Context<'ctx>, bytes: &[u8]) -> Result<DeviceBuffer<'ctx>> {
|
|
let mut owner = context.allocate(bytes.len())?;
|
|
if let Err(error) = owner.copy_from(bytes) {
|
|
return combine(Err(error), owner.release());
|
|
}
|
|
Ok(owner)
|
|
}
|
|
fn bf16(count: usize, seed: usize) -> Vec<u8> {
|
|
(0..count)
|
|
.flat_map(|i| {
|
|
let value = ((i.wrapping_mul(3).wrapping_add(seed) % 17) as f32 - 8.0) / 4096.0;
|
|
((value.to_bits() >> 16) as u16).to_le_bytes()
|
|
})
|
|
.collect()
|
|
}
|
|
fn values(count: usize, seed: usize, zeros: bool) -> Vec<f32> {
|
|
(0..count)
|
|
.map(
|
|
|i| {
|
|
if zeros { if i % 2 == 0 { -0.0 } else { 0.0 } } else { ((i + seed) % 19) as f32 / 128.0 - 9.0 / 128.0 }
|
|
},
|
|
)
|
|
.collect()
|
|
}
|
|
fn read(buffer: &DeviceBuffer<'_>) -> Result<Vec<u32>> {
|
|
let mut bytes = staging(buffer.bytes)?;
|
|
buffer.copy_to(&mut bytes)?;
|
|
Ok(bytes.as_chunks::<4>().0.iter().map(|b| u32::from_le_bytes(*b)).collect())
|
|
}
|
|
struct Reference {
|
|
products: Vec<Vec<u32>>,
|
|
refusal: Option<PleClosedRefusal>,
|
|
}
|
|
fn record(products: &mut Vec<Vec<u32>>, output: &DeviceBuffer<'_>) -> Result<bool> {
|
|
let words = read(output)?;
|
|
let valid = words.iter().all(|word| f32::from_bits(*word).is_finite());
|
|
products.push(words);
|
|
Ok(valid)
|
|
}
|
|
fn original(
|
|
context: &Context<'_>,
|
|
weights: [&DeviceBuffer<'_>; 6],
|
|
embedding: &[f32],
|
|
streams: &[f32],
|
|
history: &[f32],
|
|
) -> Result<Reference> {
|
|
let mut buffers: [Option<DeviceBuffer<'_>>; 13] = std::array::from_fn(|_| None);
|
|
let operation = (|| {
|
|
for (i, size) in SIZES[..13].iter().enumerate() {
|
|
buffers[i] = Some(context.allocate(*size)?);
|
|
}
|
|
for (slot, values) in [(0, embedding), (1, streams), (2, history)] {
|
|
buffers[slot].as_mut().ok_or(Error::ContextPoisoned)?.copy_from(&float_staging(values)?)?;
|
|
}
|
|
let mut products = Vec::new();
|
|
products.try_reserve_exact(10).map_err(|_| Error::HostAllocation)?;
|
|
for role in 0..10 {
|
|
// Disjoint true whole outputs; retain all original dependencies.
|
|
let index = OUTPUTS[role];
|
|
let (inputs, outputs) = buffers.split_at_mut(index);
|
|
let get = |i: usize| inputs[i].as_ref().ok_or(Error::ContextPoisoned);
|
|
if role == 7 {
|
|
let (conv, next) = outputs.split_at_mut(1);
|
|
context.ple_conv_history_f32(
|
|
get(2)?,
|
|
get(9)?,
|
|
weights[5],
|
|
conv[0].as_mut().ok_or(Error::ContextPoisoned)?,
|
|
next[0].as_mut().ok_or(Error::ContextPoisoned)?,
|
|
)?;
|
|
} else {
|
|
let output = outputs[0].as_mut().ok_or(Error::ContextPoisoned)?;
|
|
match role {
|
|
0 => context.bf16_f32(
|
|
weights[0],
|
|
get(0)?,
|
|
output,
|
|
crate::Bf16ProjectionShape { rows: 10240, columns: 2560 },
|
|
)?,
|
|
1 => context.bf16_f32(
|
|
weights[1],
|
|
get(0)?,
|
|
output,
|
|
crate::Bf16ProjectionShape { rows: 2560, columns: 2560 },
|
|
)?,
|
|
2 => context.ple_grouped_norm_f32(get(3)?, weights[2], output)?,
|
|
3 => context.ple_grouped_norm_f32(get(1)?, weights[3], output)?,
|
|
4 => context.ple_signed_gate_f32(get(5)?, get(6)?, output)?,
|
|
5 => context.ple_broadcast_f32(get(4)?, get(7)?, output)?,
|
|
6 => context.ple_grouped_norm_f32(get(8)?, weights[4], output)?,
|
|
8 => (),
|
|
9 => context.ple_residual_f32(get(1)?, get(8)?, get(10)?, output)?,
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
if !record(&mut products, buffers[index].as_ref().ok_or(Error::ContextPoisoned)?)? {
|
|
return Ok(Reference { products, refusal: Some(REFUSALS[role]) });
|
|
}
|
|
}
|
|
Ok(Reference { products, refusal: None })
|
|
})();
|
|
let mut cleanup = Ok(());
|
|
for slot in &mut buffers {
|
|
if let Some(buffer) = slot.take() {
|
|
cleanup = combine(cleanup, buffer.release());
|
|
}
|
|
}
|
|
drop(buffers);
|
|
combine(operation, cleanup)
|
|
}
|
|
fn refs<'a, 'b>(weights: &'a [DeviceBuffer<'b>]) -> [&'a DeviceBuffer<'b>; 6] {
|
|
[&weights[0], &weights[1], &weights[2], &weights[3], &weights[4], &weights[5]]
|
|
}
|
|
type QueuedProducts = (Option<PleClosedRefusal>, Vec<Vec<u32>>);
|
|
fn queued(
|
|
queue: &mut PleClosedQueue<'_>,
|
|
weights: [&DeviceBuffer<'_>; 6],
|
|
embedding: &[f32],
|
|
streams: &[f32],
|
|
history: &[f32],
|
|
) -> Result<QueuedProducts> {
|
|
let result = queue.compose(
|
|
PleClosedWeights { key: weights[0], value: weights[1], aux: [weights[2], weights[3], weights[4], weights[5]] },
|
|
embedding,
|
|
streams,
|
|
history,
|
|
)?;
|
|
let refusal = result.numerical_refusal();
|
|
let mut products = Vec::new();
|
|
products.try_reserve_exact(10).map_err(|_| Error::HostAllocation)?;
|
|
for index in OUTPUTS {
|
|
products.push(read(result.owner.buffer(index)?)?);
|
|
}
|
|
let flags = result.flags;
|
|
drop(result);
|
|
if flags.iter().any(|f| *f > 1) {
|
|
return Err(Error::InvalidDriverValue("PLE fixture flags"));
|
|
}
|
|
Ok((refusal, products))
|
|
}
|
|
#[test]
|
|
#[ignore]
|
|
fn original_first_whole_ple_queue_causal_history_status_reset_and_cleanup() {
|
|
if std::env::var("LELEKA_STRA169_RUN_PLE_CLOSED_QUEUE").as_deref() != Ok("yes") {
|
|
return;
|
|
}
|
|
let driver = Driver::load().expect("actual driver");
|
|
let device = driver.device(0).expect("actual device");
|
|
let context = device.create_context(512 * 1024 * 1024).expect("actual context");
|
|
let foreign = device.create_context(4 * 1024 * 1024).expect("actual foreign context");
|
|
let mut weights = Vec::new();
|
|
let mut queue = None;
|
|
let mut foreign_weight = None;
|
|
let result = (|| {
|
|
weights.try_reserve_exact(6).map_err(|_| Error::HostAllocation)?;
|
|
weights.push(upload(&context, &bf16(10240 * 2560, 2))?);
|
|
weights.push(upload(&context, &bf16(2560 * 2560, 3))?);
|
|
for _ in 0..3 {
|
|
weights.push(upload(&context, &float_staging(&vec![1.0; 10240])?)?);
|
|
}
|
|
weights.push(upload(&context, &bf16(4 * 10240, 4))?);
|
|
queue = Some(context.ple_closed_queue()?);
|
|
let q = queue.as_mut().ok_or(Error::ContextPoisoned)?;
|
|
let mut words = 0usize;
|
|
let mut differences = 0usize;
|
|
let mut guards = 0usize;
|
|
let mut history = values(92160, 5, false);
|
|
for case in 0..4 {
|
|
let embedding = values(2560, case * 3, case == 2);
|
|
let streams = values(10240, case * 5, case == 2);
|
|
let old = original(&context, refs(&weights), &embedding, &streams, &history)?;
|
|
let (refusal, products) = queued(q, refs(&weights), &embedding, &streams, &history)?;
|
|
if old.refusal.is_some() || refusal.is_some() {
|
|
differences += 1;
|
|
}
|
|
for (a, b) in old.products.iter().zip(&products) {
|
|
words += a.len();
|
|
differences += a.iter().zip(b).filter(|(a, b)| a != b).count();
|
|
if a.len() != b.len() {
|
|
differences += 1;
|
|
}
|
|
}
|
|
// The genuine previous candidate history supplies the next causal
|
|
// row; it must equal the original complete history first.
|
|
history = products[8].iter().map(|word| f32::from_bits(*word)).collect();
|
|
}
|
|
let embedding = values(2560, 3, false);
|
|
let streams = values(10240, 5, false);
|
|
for (weight, offset, bytes) in [
|
|
(0, 52_428_798, vec![0x80, 0x7f]),
|
|
(1, 13_107_198, vec![0x80, 0x7f]),
|
|
(2, 40956, f32::NAN.to_le_bytes().to_vec()),
|
|
(3, 40956, f32::INFINITY.to_le_bytes().to_vec()),
|
|
(4, 40956, f32::NAN.to_le_bytes().to_vec()),
|
|
(5, 81918, vec![0xc0, 0x7f]),
|
|
] {
|
|
let mut saved = staging(weights[weight].bytes)?;
|
|
weights[weight].copy_to(&mut saved)?;
|
|
weights[weight].copy_from_range(offset, &bytes)?;
|
|
let old = original(&context, refs(&weights), &embedding, &streams, &history)?;
|
|
let (refusal, _) = queued(q, refs(&weights), &embedding, &streams, &history)?;
|
|
if old.refusal.is_none() || old.refusal != refusal {
|
|
differences += 1;
|
|
}
|
|
guards += 1;
|
|
weights[weight].copy_from(&saved)?;
|
|
let old = original(&context, refs(&weights), &embedding, &streams, &history)?;
|
|
let (refusal, products) = queued(q, refs(&weights), &embedding, &streams, &history)?;
|
|
if refusal.is_some() || old.refusal.is_some() {
|
|
differences += 1;
|
|
}
|
|
for (a, b) in old.products.iter().zip(products) {
|
|
words += a.len();
|
|
differences += a.iter().zip(b).filter(|(a, b)| **a != *b).count();
|
|
}
|
|
guards += 1;
|
|
}
|
|
let before = read(q.buffer(12)?)?;
|
|
let mut invalid = embedding.clone();
|
|
invalid[2559] = f32::NAN;
|
|
if q.compose(
|
|
PleClosedWeights {
|
|
key: &weights[0],
|
|
value: &weights[1],
|
|
aux: [&weights[2], &weights[3], &weights[4], &weights[5]],
|
|
},
|
|
&invalid,
|
|
&streams,
|
|
&history,
|
|
)
|
|
.is_ok()
|
|
{
|
|
differences += 1;
|
|
}
|
|
guards += 1;
|
|
if q.compose(
|
|
PleClosedWeights {
|
|
key: &weights[0],
|
|
value: &weights[1],
|
|
aux: [&weights[2], &weights[3], &weights[4], &weights[5]],
|
|
},
|
|
&embedding[..2559],
|
|
&streams,
|
|
&history,
|
|
)
|
|
.is_ok()
|
|
{
|
|
differences += 1;
|
|
}
|
|
guards += 1;
|
|
foreign_weight = Some(foreign.allocate(1024)?);
|
|
if q.compose(
|
|
PleClosedWeights {
|
|
key: foreign_weight.as_ref().ok_or(Error::ContextPoisoned)?,
|
|
value: &weights[1],
|
|
aux: [&weights[2], &weights[3], &weights[4], &weights[5]],
|
|
},
|
|
&embedding,
|
|
&streams,
|
|
&history,
|
|
)
|
|
.is_ok()
|
|
{
|
|
differences += 1;
|
|
}
|
|
guards += 1;
|
|
if !matches!(q.weight(q.buffer(0)?, 10240, 4), Err(Error::OutputAliasesInput)) {
|
|
differences += 1;
|
|
}
|
|
guards += 1;
|
|
if read(q.buffer(12)?)? != before {
|
|
differences += 1;
|
|
}
|
|
Ok((words, differences, guards))
|
|
})();
|
|
let mut cleanup = Ok(());
|
|
if let Some(q) = queue.take() {
|
|
cleanup = combine(cleanup, q.close());
|
|
}
|
|
drop(queue);
|
|
if let Some(buffer) = foreign_weight.take() {
|
|
cleanup = combine(cleanup, buffer.release());
|
|
}
|
|
drop(foreign_weight);
|
|
for buffer in weights.drain(..) {
|
|
cleanup = combine(cleanup, buffer.release());
|
|
}
|
|
drop(weights);
|
|
let allocated = context.allocated_bytes();
|
|
let foreign_allocated = foreign.allocated_bytes();
|
|
cleanup = combine(cleanup, foreign.close());
|
|
cleanup = combine(cleanup, context.close());
|
|
let (words, differences, guards) = combine(result, cleanup).expect("original-first PLE and full cleanup");
|
|
assert_eq!(allocated, 0);
|
|
assert_eq!(foreign_allocated, 0);
|
|
assert_eq!(differences, 0);
|
|
assert_eq!(guards, 16);
|
|
eprintln!(
|
|
"PLE_CLOSED_QUEUE PASS words={words} differences={differences} guards={guards} original_first=true causal_history=true allocated0=true"
|
|
);
|
|
let tiny = device.create_context(CHARGE + RESERVE - 1).expect("actual budget context");
|
|
let refused = matches!(tiny.ple_closed_queue(), Err(Error::BudgetExceeded));
|
|
let allocated = tiny.allocated_bytes();
|
|
let closed = tiny.close();
|
|
assert!(refused);
|
|
assert_eq!(allocated, 0);
|
|
assert!(closed.is_ok());
|
|
}
|