STRA-338 size2 cd7: qsa_verification_commit functions cut into helpers
This commit is contained in:
@@ -3,6 +3,87 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
type Indices<'ctx> = [OriginalQsaResidentIndex<'ctx>; 12];
|
||||
/// The window the bank staged (and its keep count) must be the window armed in `live`.
|
||||
pub(super) fn check_armed_window(
|
||||
live: &Live,
|
||||
(generation, position, rows): (u64, usize, usize),
|
||||
keep: usize,
|
||||
) -> Result<()> {
|
||||
if !live.armed
|
||||
|| live.generation != generation
|
||||
|| live.position != position
|
||||
|| live.rows != rows
|
||||
|| keep == 0
|
||||
|| keep > rows
|
||||
{
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// The tail, `dead`, pooled-key and block-position addresses of an index image.
|
||||
pub(super) fn image_addresses(i: &OriginalQsaResidentIndex<'_>) -> Result<[u64; 4]> {
|
||||
let tail = i.h(1, 0, 1536)?.qwen_address();
|
||||
let dead = i.h(2, 0, 512)?.qwen_address();
|
||||
let pooled = i.h(0, 0, i.image[0].as_ref().ok_or(Error::ContextPoisoned)?.len())?.qwen_address();
|
||||
let block_pos = i.h(3, 0, 4)?.qwen_address();
|
||||
Ok([tail, dead, pooled, block_pos])
|
||||
}
|
||||
/// A buffer must not overlap any buffer of an index image.
|
||||
fn image_clear(buffer: &DeviceBuffer<'_>, index: &OriginalQsaResidentIndex<'_>) -> Result<()> {
|
||||
for b in index.image.iter().flatten() {
|
||||
if overlaps(buffer, b)? {
|
||||
return Err(Error::OutputAliasesInput);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Every norm weight is a whole buffer that overlaps no index image.
|
||||
fn check_gamma<'ctx>(
|
||||
context: &'ctx Context<'ctx>,
|
||||
gamma: &[&DeviceBuffer<'ctx>; 12],
|
||||
indices: &Indices<'ctx>,
|
||||
) -> Result<()> {
|
||||
for g in gamma.iter().copied() {
|
||||
whole(context, g, 512)?;
|
||||
for i in indices {
|
||||
image_clear(g, i)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// The retained RoPE tables must not overlap any index image.
|
||||
fn check_rope_clear(rope: &OriginalVerifyRope<'_, '_>, indices: &Indices<'_>) -> Result<()> {
|
||||
for b in indices.iter().flat_map(|i| i.image.iter().flatten()) {
|
||||
if let Some(p) = rope.positions {
|
||||
if overlaps(p, b)? {
|
||||
return Err(Error::OutputAliasesInput);
|
||||
}
|
||||
}
|
||||
if let Some((cos, sin, _)) = rope.angles {
|
||||
if overlaps(cos, b)? || overlaps(sin, b)? {
|
||||
return Err(Error::OutputAliasesInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Every index and journal belongs to `context`, is idle and stands at `end`.
|
||||
fn check_aligned<'ctx>(
|
||||
context: &'ctx Context<'ctx>,
|
||||
indices: &Indices<'ctx>,
|
||||
mapped: &[OriginalQsaMappedJournal<'ctx>; 12],
|
||||
end: usize,
|
||||
) -> Result<()> {
|
||||
for (i, m) in indices.iter().zip(mapped.iter()) {
|
||||
i.validate_context(context)?;
|
||||
m.validate_context(context)?;
|
||||
if i.in_flight || i.unpublished || i.end != end || m.next_position() != end {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
pub fn new(
|
||||
context: &'ctx Context<'ctx>,
|
||||
@@ -12,38 +93,10 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
rope: OriginalVerifyRope<'a, 'ctx>,
|
||||
) -> Result<Rc<Self>> {
|
||||
let end = indices[0].next_position();
|
||||
for (i, m) in indices.iter().zip(mapped.iter()) {
|
||||
i.validate_context(context)?;
|
||||
m.validate_context(context)?;
|
||||
if i.in_flight || i.unpublished || i.end != end || m.next_position() != end {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
for g in gamma {
|
||||
whole(context, g, 512)?;
|
||||
for i in &indices {
|
||||
for b in i.image.iter().flatten() {
|
||||
if overlaps(g, b)? {
|
||||
return Err(Error::OutputAliasesInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
check_aligned(context, &indices, &mapped, end)?;
|
||||
check_gamma(context, &gamma, &indices)?;
|
||||
rope.validate(context, crate::serving_context::geometry().cells())?;
|
||||
for i in &indices {
|
||||
for b in i.image.iter().flatten() {
|
||||
if let Some(p) = rope.positions {
|
||||
if overlaps(p, b)? {
|
||||
return Err(Error::OutputAliasesInput);
|
||||
}
|
||||
}
|
||||
if let Some((cos, sin, _)) = rope.angles {
|
||||
if overlaps(cos, b)? || overlaps(sin, b)? {
|
||||
return Err(Error::OutputAliasesInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
check_rope_clear(&rope, &indices)?;
|
||||
Ok(Rc::new(Self {
|
||||
context,
|
||||
indices: indices.map(RefCell::new),
|
||||
@@ -183,6 +236,34 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
self.indices.iter().map(|i| i.borrow().allocated_bytes()).sum::<usize>()
|
||||
+ self.mapped.iter().map(|m| m.borrow().allocated_bytes()).sum::<usize>()
|
||||
}
|
||||
/// Every index and journal is idle and stands at `position`.
|
||||
fn check_idle_at(&self, position: usize) -> Result<()> {
|
||||
for (i, m) in self.indices.iter().zip(self.mapped.iter()) {
|
||||
let i = i.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let m = m.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if i.in_flight || i.unpublished || i.end != position || m.next_position() != position {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// The NaN guard's undo (`discard_window`): the state a window disturbs, as it stands before this one. The device
|
||||
/// is idle here (the previous commit was waited), so these are plain synchronous reads; off, nothing is read.
|
||||
fn snapshot_window_state(&self) -> Result<()> {
|
||||
if !self.snapshots.get() {
|
||||
return Ok(());
|
||||
}
|
||||
let n = OriginalQsaResidentIndex::WINDOW_STATE_BYTES;
|
||||
let mut snapshot = self.snapshot.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?;
|
||||
snapshot.clear();
|
||||
snapshot.try_reserve_exact(12 * n).map_err(|_| Error::HostAllocation)?;
|
||||
snapshot.resize(12 * n, 0);
|
||||
for (layer, i) in self.indices.iter().enumerate() {
|
||||
let i = i.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
i.read_window_state(&mut snapshot[layer * n..(layer + 1) * n])?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub(crate) fn begin_window(&self, bank: &OriginalVerifyBank<'ctx>) -> Result<()> {
|
||||
self.validate_context(self.context)?;
|
||||
bank.validate_context(self.context)?;
|
||||
@@ -191,26 +272,8 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
let (generation, position, rows) = bank.staged_window()?;
|
||||
for (i, m) in self.indices.iter().zip(self.mapped.iter()) {
|
||||
let i = i.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let m = m.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if i.in_flight || i.unpublished || i.end != position || m.next_position() != position {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
// The NaN guard's undo (`discard_window`): the state a window disturbs, as it stands before this one. The device is idle here (the
|
||||
// previous commit was waited), so these are plain synchronous reads; off, nothing is read.
|
||||
if self.snapshots.get() {
|
||||
let n = OriginalQsaResidentIndex::WINDOW_STATE_BYTES;
|
||||
let mut snapshot = self.snapshot.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?;
|
||||
snapshot.clear();
|
||||
snapshot.try_reserve_exact(12 * n).map_err(|_| Error::HostAllocation)?;
|
||||
snapshot.resize(12 * n, 0);
|
||||
for (layer, i) in self.indices.iter().enumerate() {
|
||||
let i = i.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
i.read_window_state(&mut snapshot[layer * n..(layer + 1) * n])?;
|
||||
}
|
||||
}
|
||||
self.check_idle_at(position)?;
|
||||
self.snapshot_window_state()?;
|
||||
// All immutable borrows above are gone. This single-thread-affine owner
|
||||
// has no escaping RefMut; the fixed pass performs no fallible allocation.
|
||||
for i in &self.indices {
|
||||
|
||||
+152
-104
@@ -1,5 +1,6 @@
|
||||
//! Moved out of the parent module by STRA-338: impl OriginalVerifyQsaJournal (2).
|
||||
|
||||
use super::impl_originalverifyqsajournal::{check_armed_window, image_addresses};
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
@@ -43,54 +44,16 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
capture: &mut OriginalCapture<'_, 'ctx>,
|
||||
) -> Result<bool> {
|
||||
self.validate_context(journal.context())?;
|
||||
let r = self.rope;
|
||||
if r.angles.is_some() {
|
||||
if self.rope.angles.is_some() {
|
||||
return Ok(false);
|
||||
}
|
||||
let t = journal.geometry().max_t;
|
||||
if t == 0 || t > 8 {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
let mut tail = [0u64; 12];
|
||||
let mut dead = [0u64; 12];
|
||||
let mut pooled = [0u64; 12];
|
||||
let mut block_pos = [0u64; 12];
|
||||
let mut gamma = [0u64; 12];
|
||||
for layer in 0..12 {
|
||||
let i = self.indices[layer].try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
tail[layer] = i.h(1, 0, 1536)?.qwen_address();
|
||||
dead[layer] = i.h(2, 0, 512)?.qwen_address();
|
||||
pooled[layer] = i.h(0, 0, i.image[0].as_ref().ok_or(Error::ContextPoisoned)?.len())?.qwen_address();
|
||||
block_pos[layer] = i.h(3, 0, 4)?.qwen_address();
|
||||
gamma[layer] = whole(self.context, self.gamma[layer], 512)?;
|
||||
}
|
||||
let raw = journal.address("idx_raw_L", 0, 512)?;
|
||||
let snap = journal.address("tail_snap", 0, 1536)?;
|
||||
let commit = journal.address("commit", 0, (t + 2) * 4)?;
|
||||
// affine layout: the row append of (layer, row) binds idx_raw_L + (layer * t + row) * 512 and the tail snapshot of a layer is at + layer * 1536 (checked at the far end)
|
||||
if journal.address("idx_raw_L", (11 * t + t - 1) * 512, 512)? != raw + ((11 * t + t - 1) * 512) as u64
|
||||
|| journal.address("tail_snap", 11 * 1536, 1536)? != snap + (11 * 1536) as u64
|
||||
|| journal.address("commit", (2 + t - 1) * 4, 4)? != commit + ((2 + t - 1) * 4) as u64
|
||||
{
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
let positions = r
|
||||
.positions
|
||||
.map(|b| whole(self.context, b, crate::serving_context::geometry().cells() * 12))
|
||||
.transpose()?
|
||||
.unwrap_or(0);
|
||||
let pack = |a: f32, b: f32| (a.to_bits() as u64) | ((b.to_bits() as u64) << 32);
|
||||
let mut words: Vec<u64> = Vec::with_capacity(5 * 12 + 7 + 4);
|
||||
for v in [&tail, &dead, &pooled, &block_pos, &gamma] {
|
||||
words.extend_from_slice(v)
|
||||
}
|
||||
words.extend_from_slice(&[raw, snap, commit, positions, t as u64, journal.geometry().max_cells as u64, 0]);
|
||||
words.extend_from_slice(&[
|
||||
pack(1e-6, r.theta_scale),
|
||||
pack(r.freq_scale, r.corr_low),
|
||||
pack(r.corr_high, r.ext_factor),
|
||||
pack(r.magnitude, 0.0),
|
||||
]);
|
||||
let layers = self.fused_layers()?;
|
||||
let io = fused_io(journal, t)?;
|
||||
let words = self.fused_words(&layers, io, (t, journal.geometry().max_cells))?;
|
||||
journal.submit_launches(
|
||||
capture,
|
||||
vec![crate::original_verify::kernels::Launch::new(
|
||||
@@ -104,6 +67,47 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
)?;
|
||||
Ok(true)
|
||||
}
|
||||
/// Per layer: the tail, `dead`, pooled-key and block-position addresses of its index image, and its norm weight.
|
||||
fn fused_layers(&self) -> Result<[[u64; 12]; 5]> {
|
||||
let mut layers = [[0u64; 12]; 5];
|
||||
for layer in 0..12 {
|
||||
let i = self.indices[layer].try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let [tail, dead, pooled, block_pos] = image_addresses(&i)?;
|
||||
layers[0][layer] = tail;
|
||||
layers[1][layer] = dead;
|
||||
layers[2][layer] = pooled;
|
||||
layers[3][layer] = block_pos;
|
||||
layers[4][layer] = whole(self.context, self.gamma[layer], 512)?;
|
||||
}
|
||||
Ok(layers)
|
||||
}
|
||||
/// The launch words: the layer tables, the journal addresses, the positions, the sizes and the packed RoPE constants.
|
||||
fn fused_words(&self, layers: &[[u64; 12]; 5], io: [u64; 3], (t, max_cells): (usize, usize)) -> Result<Vec<u64>> {
|
||||
let r = self.rope;
|
||||
let positions = self.positions_address()?;
|
||||
let pack = |a: f32, b: f32| (a.to_bits() as u64) | ((b.to_bits() as u64) << 32);
|
||||
let mut words: Vec<u64> = Vec::with_capacity(5 * 12 + 7 + 4);
|
||||
for v in layers {
|
||||
words.extend_from_slice(v)
|
||||
}
|
||||
words.extend_from_slice(&[io[0], io[1], io[2], positions, t as u64, max_cells as u64, 0]);
|
||||
words.extend_from_slice(&[
|
||||
pack(1e-6, r.theta_scale),
|
||||
pack(r.freq_scale, r.corr_low),
|
||||
pack(r.corr_high, r.ext_factor),
|
||||
pack(r.magnitude, 0.0),
|
||||
]);
|
||||
Ok(words)
|
||||
}
|
||||
/// The device address of the retained positions table, or 0 without one.
|
||||
fn positions_address(&self) -> Result<u64> {
|
||||
Ok(self
|
||||
.rope
|
||||
.positions
|
||||
.map(|b| whole(self.context, b, crate::serving_context::geometry().cells() * 12))
|
||||
.transpose()?
|
||||
.unwrap_or(0))
|
||||
}
|
||||
/// Original provisional index append, row by row on the SAME captured stream.
|
||||
/// Caller records the tail snapshot before the first group's append.
|
||||
pub fn capture_append_rows(
|
||||
@@ -134,8 +138,15 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
let gamma = whole(self.context, self.gamma[layer], 512)?;
|
||||
let mut commands = Vec::new();
|
||||
commands.try_reserve_exact(end - begin).map_err(|_| Error::HostAllocation)?;
|
||||
let r = self.rope;
|
||||
let (name, cos, sin, max_pos) = match r.angles {
|
||||
let plan = self.append_plan(gamma)?;
|
||||
for row in begin..end {
|
||||
commands.push(self.append_command(&plan, journal, &i, (layer * t + row, row), commit)?);
|
||||
}
|
||||
journal.submit_qsa(capture, commands)
|
||||
}
|
||||
/// The kernel variant (analytic or table RoPE) and the fixed addresses of every row append of a layer.
|
||||
fn append_plan(&self, gamma: u64) -> Result<AppendPlan> {
|
||||
let (name, cos, sin, max_pos) = match self.rope.angles {
|
||||
Some((c, s, n)) => (
|
||||
"verify_qsa_append_table",
|
||||
whole(self.context, c, n * 128)?,
|
||||
@@ -144,46 +155,49 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
),
|
||||
None => ("verify_qsa_append_analytic", 0, 0, 0),
|
||||
};
|
||||
let positions = r
|
||||
.positions
|
||||
.map(|b| whole(self.context, b, crate::serving_context::geometry().cells() * 12))
|
||||
.transpose()?
|
||||
.unwrap_or(0);
|
||||
for row in begin..end {
|
||||
commands.push(Command::new(
|
||||
name,
|
||||
[1, 1, 1],
|
||||
[256, 1, 1],
|
||||
0,
|
||||
vec![
|
||||
Arg::Address(journal.address("idx_raw_L", (layer * t + row) * 512, 512)?),
|
||||
Arg::Address(if commit {
|
||||
journal.address("commit", (2 + row) * 4, 4)?
|
||||
} else {
|
||||
journal.address("step", row * 16, 4)?
|
||||
}),
|
||||
Arg::I32(0),
|
||||
Arg::Address(gamma),
|
||||
Arg::F32(1e-6),
|
||||
Arg::Address(i.h(1, 0, 1536)?.qwen_address()),
|
||||
Arg::Address(i.h(2, 0, 512)?.qwen_address()),
|
||||
Arg::Address(i.h(0, 0, i.image[0].as_ref().ok_or(Error::ContextPoisoned)?.len())?.qwen_address()),
|
||||
Arg::Address(i.h(3, 0, 4)?.qwen_address()),
|
||||
Arg::I32(journal.geometry().max_cells as i32),
|
||||
Arg::F32(r.theta_scale),
|
||||
Arg::F32(r.freq_scale),
|
||||
Arg::F32(r.corr_low),
|
||||
Arg::F32(r.corr_high),
|
||||
Arg::F32(r.ext_factor),
|
||||
Arg::F32(r.magnitude),
|
||||
Arg::Address(positions),
|
||||
Arg::Address(cos),
|
||||
Arg::Address(sin),
|
||||
Arg::I32(max_pos),
|
||||
],
|
||||
)?);
|
||||
}
|
||||
journal.submit_qsa(capture, commands)
|
||||
let positions = self.positions_address()?;
|
||||
Ok(AppendPlan { name, gamma, positions, cos, sin, max_pos })
|
||||
}
|
||||
fn append_command(
|
||||
&self,
|
||||
plan: &AppendPlan,
|
||||
journal: &OriginalVerifyGdnJournal<'_, 'ctx>,
|
||||
i: &OriginalQsaResidentIndex<'ctx>,
|
||||
(raw_row, row): (usize, usize),
|
||||
commit: bool,
|
||||
) -> Result<Command> {
|
||||
let raw = journal.address("idx_raw_L", raw_row * 512, 512)?;
|
||||
let step =
|
||||
if commit { journal.address("commit", (2 + row) * 4, 4)? } else { journal.address("step", row * 16, 4)? };
|
||||
let [tail, dead, pooled, block_pos] = image_addresses(i)?;
|
||||
let addresses = AppendAddresses { raw, step, tail, dead, pooled, block_pos };
|
||||
let max_cells = journal.geometry().max_cells as i32;
|
||||
Command::new(plan.name, [1, 1, 1], [256, 1, 1], 0, self.append_args(plan, &addresses, max_cells))
|
||||
}
|
||||
fn append_args(&self, plan: &AppendPlan, a: &AppendAddresses, max_cells: i32) -> Vec<Arg> {
|
||||
let r = self.rope;
|
||||
vec![
|
||||
Arg::Address(a.raw),
|
||||
Arg::Address(a.step),
|
||||
Arg::I32(0),
|
||||
Arg::Address(plan.gamma),
|
||||
Arg::F32(1e-6),
|
||||
Arg::Address(a.tail),
|
||||
Arg::Address(a.dead),
|
||||
Arg::Address(a.pooled),
|
||||
Arg::Address(a.block_pos),
|
||||
Arg::I32(max_cells),
|
||||
Arg::F32(r.theta_scale),
|
||||
Arg::F32(r.freq_scale),
|
||||
Arg::F32(r.corr_low),
|
||||
Arg::F32(r.corr_high),
|
||||
Arg::F32(r.ext_factor),
|
||||
Arg::F32(r.magnitude),
|
||||
Arg::Address(plan.positions),
|
||||
Arg::Address(plan.cos),
|
||||
Arg::Address(plan.sin),
|
||||
Arg::I32(plan.max_pos),
|
||||
]
|
||||
}
|
||||
/// Publish the K/V half of a verifier window only from the append receipts
|
||||
/// produced by the full resident QSA sequence. This is deliberately a
|
||||
@@ -196,18 +210,28 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
) -> Result<()> {
|
||||
self.validate_context(self.context)?;
|
||||
workspace.validate_context(self.context)?;
|
||||
let live = self.live.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let (generation, position, rows) = bank.staged_window()?;
|
||||
let keep = bank.staged_keep()?;
|
||||
if !live.armed
|
||||
|| live.generation != generation
|
||||
|| live.position != position
|
||||
|| live.rows != rows
|
||||
|| keep == 0
|
||||
|| keep > rows
|
||||
{
|
||||
return Err(Error::InvalidQwenShape);
|
||||
let keep = self.check_appended(bank, &appended)?;
|
||||
for (m, a) in self.mapped.iter().zip(appended) {
|
||||
let result = m.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?.accept_prefix(workspace, &a, keep);
|
||||
if let Err(e) = result {
|
||||
self.failed.set(true);
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// The append receipts must be those of the armed window, and each journal must accept its final prefix. Returns the
|
||||
/// number of rows to keep.
|
||||
fn check_appended(
|
||||
&self,
|
||||
bank: &OriginalVerifyBank<'ctx>,
|
||||
appended: &[OriginalQsaMappedAppend; 12],
|
||||
) -> Result<usize> {
|
||||
let live = self.live.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let staged = bank.staged_window()?;
|
||||
let keep = bank.staged_keep()?;
|
||||
check_armed_window(&live, staged, keep)?;
|
||||
let (_, position, rows) = staged;
|
||||
for (m, a) in self.mapped.iter().zip(appended.iter()) {
|
||||
let m = m.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if a.first_position() != position
|
||||
@@ -218,14 +242,38 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
}
|
||||
m.validate_prefix(a.final_prefix())?;
|
||||
}
|
||||
drop(live);
|
||||
for (m, a) in self.mapped.iter().zip(appended) {
|
||||
let result = m.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?.accept_prefix(workspace, &a, keep);
|
||||
if let Err(e) = result {
|
||||
self.failed.set(true);
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
Ok(keep)
|
||||
}
|
||||
}
|
||||
/// The journal addresses of the fused commit: the raw index rows, the tail snapshots and the commit table. Their
|
||||
/// layer and row strides are affine, so the far ends are checked against the first.
|
||||
fn fused_io(journal: &OriginalVerifyGdnJournal<'_, '_>, t: usize) -> Result<[u64; 3]> {
|
||||
let raw = journal.address("idx_raw_L", 0, 512)?;
|
||||
let snap = journal.address("tail_snap", 0, 1536)?;
|
||||
let commit = journal.address("commit", 0, (t + 2) * 4)?;
|
||||
if journal.address("idx_raw_L", (11 * t + t - 1) * 512, 512)? != raw + ((11 * t + t - 1) * 512) as u64
|
||||
|| journal.address("tail_snap", 11 * 1536, 1536)? != snap + (11 * 1536) as u64
|
||||
|| journal.address("commit", (2 + t - 1) * 4, 4)? != commit + ((2 + t - 1) * 4) as u64
|
||||
{
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
Ok([raw, snap, commit])
|
||||
}
|
||||
/// The kernel and the addresses shared by every row append of one layer.
|
||||
struct AppendPlan {
|
||||
name: &'static str,
|
||||
gamma: u64,
|
||||
positions: u64,
|
||||
cos: u64,
|
||||
sin: u64,
|
||||
max_pos: i32,
|
||||
}
|
||||
/// The addresses one row append binds.
|
||||
struct AppendAddresses {
|
||||
raw: u64,
|
||||
step: u64,
|
||||
tail: u64,
|
||||
dead: u64,
|
||||
pooled: u64,
|
||||
block_pos: u64,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Moved out of the parent module by STRA-338: impl OriginalVerifyQsaJournal (3).
|
||||
|
||||
use super::impl_originalverifyqsajournal::check_armed_window;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
@@ -8,17 +9,10 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
pub(crate) fn complete_commit(&self, bank: &OriginalVerifyBank<'ctx>) -> Result<()> {
|
||||
self.validate_context(self.context)?;
|
||||
let mut live = self.live.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let (generation, position, rows) = bank.staged_window()?;
|
||||
let staged = bank.staged_window()?;
|
||||
let keep = bank.staged_keep()?;
|
||||
if !live.armed
|
||||
|| live.generation != generation
|
||||
|| live.position != position
|
||||
|| live.rows != rows
|
||||
|| keep == 0
|
||||
|| keep > rows
|
||||
{
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
check_armed_window(&live, staged, keep)?;
|
||||
let position = staged.1;
|
||||
let end = position.checked_add(keep).ok_or(Error::InvalidSize)?;
|
||||
for i in &self.indices {
|
||||
let i = i.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
@@ -26,17 +20,7 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
// The window graph appended the actual K/V (resident pool and host copy) before the index images are made visible; publish
|
||||
// that K/V half on the host here, after the commit drain, and only then the index half. Do not publish an index-only
|
||||
// verifier state: every journal must stand at `position` and every one is advanced together.
|
||||
for m in &self.mapped {
|
||||
if m.try_borrow().map_err(|_| Error::InvalidQwenShape)?.next_position() != position {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
for m in &self.mapped {
|
||||
m.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?.publish_window_rows(position, keep)?;
|
||||
}
|
||||
self.publish_window_kv(position, keep)?;
|
||||
for i in &self.indices {
|
||||
let mut i = i.borrow_mut();
|
||||
i.coverage.end.set(i.coverage.end.get().min(position));
|
||||
@@ -48,6 +32,20 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
live.armed = false;
|
||||
Ok(())
|
||||
}
|
||||
/// The window graph appended the actual K/V (resident pool and host copy) before the index images are made
|
||||
/// visible; publish that K/V half on the host here, after the commit drain, and only then the index half. Do not
|
||||
/// publish an index-only verifier state: every journal must stand at `position` and every one is advanced together.
|
||||
fn publish_window_kv(&self, position: usize, keep: usize) -> Result<()> {
|
||||
for m in &self.mapped {
|
||||
if m.try_borrow().map_err(|_| Error::InvalidQwenShape)?.next_position() != position {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
for m in &self.mapped {
|
||||
m.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?.publish_window_rows(position, keep)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Switch on the per-window snapshots `discard_window` needs (the NaN guard, `LELEKA_ORIGINAL_SERVE_NAN_GUARD=on`). They cost twelve small
|
||||
/// device-to-host reads and one of the 360 KB PLE history at the start of every window, so they are off unless asked for.
|
||||
pub fn set_window_snapshots(&self, on: bool) {
|
||||
@@ -81,32 +79,17 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
) -> Result<()> {
|
||||
self.validate_context(self.context)?;
|
||||
bank.validate_context(self.context)?;
|
||||
let (generation, position, rows) = bank.staged_window()?;
|
||||
let staged = bank.staged_window()?;
|
||||
let n = OriginalQsaResidentIndex::WINDOW_STATE_BYTES;
|
||||
let snapshot = self.snapshot.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if !self.snapshots.get() || snapshot.len() != 12 * n {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
{
|
||||
let live = self.live.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if !live.armed || live.generation != generation || live.position != position || live.rows != rows {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
for (i, m) in self.indices.iter().zip(self.mapped.iter()) {
|
||||
let i = i.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let m = m.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if !i.in_flight || !i.unpublished || i.end != position || m.next_position() != position {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
}
|
||||
// the PLE history: the window advanced it row by row, and only the commit graph sets it (from the kept row's snapshot)
|
||||
self.check_discardable(staged)?;
|
||||
// the PLE history: the window advanced it row by row, and only the commit graph sets it (from the kept
|
||||
// row's snapshot)
|
||||
let ple_snapshot = self.ple_snapshot.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if let Some(ple) = ple {
|
||||
if ple_snapshot.len() != ple.len() {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
Self::check_ple_snapshot(&ple_snapshot, ple)?;
|
||||
for (layer, i) in self.indices.iter().enumerate() {
|
||||
i.try_borrow_mut()
|
||||
.map_err(|_| Error::InvalidQwenShape)?
|
||||
@@ -115,6 +98,32 @@ impl<'a, 'ctx> OriginalVerifyQsaJournal<'a, 'ctx> {
|
||||
if let Some(ple) = ple {
|
||||
ple.copy_from_range_shared(0, &ple_snapshot)?;
|
||||
}
|
||||
self.release_window()
|
||||
}
|
||||
/// The PLE history snapshot has the size of the image it restores.
|
||||
fn check_ple_snapshot(snapshot: &[u8], ple: Option<&DeviceBuffer<'ctx>>) -> Result<()> {
|
||||
match ple {
|
||||
Some(ple) if snapshot.len() != ple.len() => Err(Error::InvalidQwenShape),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
/// The staged window is the armed one, and every index is still in flight and unpublished at its position.
|
||||
fn check_discardable(&self, (generation, position, rows): (u64, usize, usize)) -> Result<()> {
|
||||
let live = self.live.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if !live.armed || live.generation != generation || live.position != position || live.rows != rows {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
for (i, m) in self.indices.iter().zip(self.mapped.iter()) {
|
||||
let i = i.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let m = m.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
if !i.in_flight || !i.unpublished || i.end != position || m.next_position() != position {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// Publish every index at its restored position and disarm the journal.
|
||||
fn release_window(&self) -> Result<()> {
|
||||
let mut live = self.live.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?;
|
||||
for i in &self.indices {
|
||||
let mut i = i.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?;
|
||||
|
||||
Reference in New Issue
Block a user