cuda-driver: remember that an immutable MTP ring snapshot was validated (validate_snapshot scanned every page, several times per round)

validate_snapshot walked all end/4 pages (65,536 at 256k) and checked four run lengths each, and the speculation round called it about seven
times (checkpoint_draft, launch ring, launch_inner, begin/finish mutation, transition ring). The snapshot is immutable, so the answer is
memoized in a Cell; a snapshot built from a validated prior plus freshly sized pages (finish_original_graph_mutation) is born validated.
No numeric change. Not yet compiled on Linux (the driver is cfg'd out on Windows) and not measured.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-29 01:03:28 +01:00
co-authored by Claude Sonnet 5.5
parent 406b40a882
commit 286c2538ef
4 changed files with 4865 additions and 4849 deletions
File diff suppressed because it is too large Load Diff
@@ -1,201 +1,203 @@
//! Child of mtp_original_int8. A skipped Original prompt group advances only
//! the logical conditioning cursor; it NEVER manufactures materialized KV pages.
use super::*;
use crate::{OriginalDeviceResidual, OriginalPromptLeaseIdentity};
/// Genuine ring- and prompt-lease-bound no-work result. The retained physical
/// snapshot remains unchanged and may be empty even when `logical_end` is large.
pub struct OriginalMtpSkippedPrefix {
physical: MtpInt8Snapshot,
generation: u64,
start: usize,
logical_end: usize,
full_prompt_length: usize,
first_needed: usize,
lease: OriginalPromptLeaseIdentity,
}
impl OriginalMtpSkippedPrefix {
pub fn logical_end(&self) -> usize {
self.logical_end
}
pub fn first_needed(&self) -> usize {
self.first_needed
}
pub fn physical_snapshot(&self) -> &MtpInt8Snapshot {
&self.physical
}
pub fn materialized_end(&self) -> usize {
self.physical.0.end
}
pub fn validate_current(&self, ring: &MtpOriginalInt8<'_>) -> Result<()> {
ring.validate_snapshot(&self.physical)?;
if ring.generation != self.generation
|| !ring
.current
.as_ref()
.is_some_and(|s| Rc::ptr_eq(&s.0, &self.physical.0))
{
return Err(Error::InvalidQwenShape);
}
Ok(())
}
pub fn validate_source(
&self,
residual: &OriginalDeviceResidual<'_, '_, '_>,
cell0: usize,
full_prompt_length: usize,
) -> Result<()> {
if self.start != cell0
|| self.logical_end.checked_sub(cell0) != Some(residual.rows())
|| full_prompt_length != self.full_prompt_length
|| !self.lease.same_lease(&residual.lease_identity())
{
return Err(Error::InvalidQwenShape);
}
Ok(())
}
pub fn metadata_bytes(&self) -> usize {
std::mem::size_of::<Self>()
}
}
impl MtpOriginalInt8<'_> {
/// Literal mtp.cpp690 cutoff, checked against actual whole prompt rows. Root
/// supplies full_prompt_length from retained OriginalPromptTxn source, not
/// from this chunk's end. No stream, kernel, page allocation or numerical
/// completion occurs in this outcome.
pub fn skip_original_prompt_prefix(
&mut self,
residual: &OriginalDeviceResidual<'_, '_, '_>,
physical_prior: &MtpInt8Snapshot,
logical_prior: Option<&OriginalMtpSkippedPrefix>,
cell0: usize,
full_prompt_length: usize,
) -> Result<Option<OriginalMtpSkippedPrefix>> {
self.validate_snapshot(physical_prior)?;
if !residual.context_matches(self.context)
|| residual.rows() == 0
|| full_prompt_length > MTP_ORIGINAL_MAX_CELLS
{
return Err(Error::InvalidProjectionShape);
}
let end = cell0
.checked_add(residual.rows())
.ok_or(Error::InvalidSize)?;
if end > full_prompt_length || end > MTP_ORIGINAL_MAX_CELLS {
return Err(Error::InvalidProjectionShape);
}
let first_needed = full_prompt_length.saturating_sub(MTP_ORIGINAL_WINDOW + 64);
let logical_start = if let Some(old) = logical_prior {
old.validate_current(self)?;
if old.full_prompt_length != full_prompt_length
|| old.first_needed != first_needed
|| !Rc::ptr_eq(&old.physical.0, &physical_prior.0)
{
return Err(Error::InvalidQwenShape);
}
old.logical_end
} else {
if self
.current
.as_ref()
.is_some_and(|s| !Rc::ptr_eq(&s.0, &physical_prior.0))
{
return Err(Error::InvalidQwenShape);
}
if self.current.is_none() && physical_prior.0.end != 0 {
return Err(Error::InvalidQwenShape);
}
physical_prior.0.end
};
if logical_start != cell0 {
return Err(Error::InvalidQwenShape);
}
// All complete groups are skipped iff the last group's end<=cutoff. This
// includes Original short remainder group; no individual-cell rounding.
if end > first_needed {
return Ok(None);
}
let generation = self
.generation
.checked_add(1)
.ok_or(Error::InvalidQwenShape)?;
let result = OriginalMtpSkippedPrefix {
physical: physical_prior.clone(),
generation,
start: cell0,
logical_end: end,
full_prompt_length,
first_needed,
lease: residual.lease_identity(),
};
self.current = Some(physical_prior.clone());
self.generation = generation;
Ok(Some(result))
}
}
impl MtpOriginalInt8<'_> {
/// Private affine transition used only by the literal captured drafter. Actual
/// graph/window guards and successful drain, not these integers, authorize it.
pub(crate) fn begin_original_graph_mutation(&mut self, prior: &MtpInt8Snapshot) -> Result<u64> {
self.validate_snapshot(prior)?;
if self
.current
.as_ref()
.is_some_and(|s| !Rc::ptr_eq(&s.0, &prior.0))
|| self.current.is_none() && prior.0.end != 0
{
return Err(Error::InvalidQwenShape);
}
let generation = self
.generation
.checked_add(1)
.ok_or(Error::InvalidQwenShape)?;
self.current = None;
self.generation = generation;
Ok(generation)
}
pub(crate) fn finish_original_graph_mutation(
&mut self,
prior: &MtpInt8Snapshot,
first: usize,
end: usize,
generation: u64,
) -> Result<MtpInt8Snapshot> {
self.validate_snapshot(prior)?;
if self.current.is_some()
|| self.generation != generation
|| first > prior.0.end
|| end <= first
|| end > MTP_ORIGINAL_MAX_CELLS
{
return Err(Error::InvalidQwenShape);
}
let page0 = first / 4;
let page1 = end.div_ceil(4);
let mut pages = Vec::new();
pages
.try_reserve_exact(page1)
.map_err(|_| Error::HostAllocation)?;
for p in 0..page1 {
if p < page0 {
pages.push(prior.0.pages.get(p).ok_or(Error::InvalidQwenShape)?.clone());
continue;
}
let mut runs: [Vec<u8>; 4] = std::array::from_fn(|_| Vec::new());
for (i, n) in PAGE_BYTES.into_iter().enumerate() {
runs[i] = zero_bytes(n)?;
}
self.read_encoded_page(p, &mut runs)?;
pages.push(Rc::new(Page {
runs: runs.map(Vec::into_boxed_slice),
}));
}
let snapshot = MtpInt8Snapshot(Rc::new(Snapshot {
origin: self.origin.clone(),
pages: Rc::new(pages.into_boxed_slice()),
end,
}));
self.current = Some(snapshot.clone());
Ok(snapshot)
}
}
//! Child of mtp_original_int8. A skipped Original prompt group advances only
//! the logical conditioning cursor; it NEVER manufactures materialized KV pages.
use super::*;
use crate::{OriginalDeviceResidual, OriginalPromptLeaseIdentity};
/// Genuine ring- and prompt-lease-bound no-work result. The retained physical
/// snapshot remains unchanged and may be empty even when `logical_end` is large.
pub struct OriginalMtpSkippedPrefix {
physical: MtpInt8Snapshot,
generation: u64,
start: usize,
logical_end: usize,
full_prompt_length: usize,
first_needed: usize,
lease: OriginalPromptLeaseIdentity,
}
impl OriginalMtpSkippedPrefix {
pub fn logical_end(&self) -> usize {
self.logical_end
}
pub fn first_needed(&self) -> usize {
self.first_needed
}
pub fn physical_snapshot(&self) -> &MtpInt8Snapshot {
&self.physical
}
pub fn materialized_end(&self) -> usize {
self.physical.0.end
}
pub fn validate_current(&self, ring: &MtpOriginalInt8<'_>) -> Result<()> {
ring.validate_snapshot(&self.physical)?;
if ring.generation != self.generation
|| !ring
.current
.as_ref()
.is_some_and(|s| Rc::ptr_eq(&s.0, &self.physical.0))
{
return Err(Error::InvalidQwenShape);
}
Ok(())
}
pub fn validate_source(
&self,
residual: &OriginalDeviceResidual<'_, '_, '_>,
cell0: usize,
full_prompt_length: usize,
) -> Result<()> {
if self.start != cell0
|| self.logical_end.checked_sub(cell0) != Some(residual.rows())
|| full_prompt_length != self.full_prompt_length
|| !self.lease.same_lease(&residual.lease_identity())
{
return Err(Error::InvalidQwenShape);
}
Ok(())
}
pub fn metadata_bytes(&self) -> usize {
std::mem::size_of::<Self>()
}
}
impl MtpOriginalInt8<'_> {
/// Literal mtp.cpp690 cutoff, checked against actual whole prompt rows. Root
/// supplies full_prompt_length from retained OriginalPromptTxn source, not
/// from this chunk's end. No stream, kernel, page allocation or numerical
/// completion occurs in this outcome.
pub fn skip_original_prompt_prefix(
&mut self,
residual: &OriginalDeviceResidual<'_, '_, '_>,
physical_prior: &MtpInt8Snapshot,
logical_prior: Option<&OriginalMtpSkippedPrefix>,
cell0: usize,
full_prompt_length: usize,
) -> Result<Option<OriginalMtpSkippedPrefix>> {
self.validate_snapshot(physical_prior)?;
if !residual.context_matches(self.context)
|| residual.rows() == 0
|| full_prompt_length > MTP_ORIGINAL_MAX_CELLS
{
return Err(Error::InvalidProjectionShape);
}
let end = cell0
.checked_add(residual.rows())
.ok_or(Error::InvalidSize)?;
if end > full_prompt_length || end > MTP_ORIGINAL_MAX_CELLS {
return Err(Error::InvalidProjectionShape);
}
let first_needed = full_prompt_length.saturating_sub(MTP_ORIGINAL_WINDOW + 64);
let logical_start = if let Some(old) = logical_prior {
old.validate_current(self)?;
if old.full_prompt_length != full_prompt_length
|| old.first_needed != first_needed
|| !Rc::ptr_eq(&old.physical.0, &physical_prior.0)
{
return Err(Error::InvalidQwenShape);
}
old.logical_end
} else {
if self
.current
.as_ref()
.is_some_and(|s| !Rc::ptr_eq(&s.0, &physical_prior.0))
{
return Err(Error::InvalidQwenShape);
}
if self.current.is_none() && physical_prior.0.end != 0 {
return Err(Error::InvalidQwenShape);
}
physical_prior.0.end
};
if logical_start != cell0 {
return Err(Error::InvalidQwenShape);
}
// All complete groups are skipped iff the last group's end<=cutoff. This
// includes Original short remainder group; no individual-cell rounding.
if end > first_needed {
return Ok(None);
}
let generation = self
.generation
.checked_add(1)
.ok_or(Error::InvalidQwenShape)?;
let result = OriginalMtpSkippedPrefix {
physical: physical_prior.clone(),
generation,
start: cell0,
logical_end: end,
full_prompt_length,
first_needed,
lease: residual.lease_identity(),
};
self.current = Some(physical_prior.clone());
self.generation = generation;
Ok(Some(result))
}
}
impl MtpOriginalInt8<'_> {
/// Private affine transition used only by the literal captured drafter. Actual
/// graph/window guards and successful drain, not these integers, authorize it.
pub(crate) fn begin_original_graph_mutation(&mut self, prior: &MtpInt8Snapshot) -> Result<u64> {
self.validate_snapshot(prior)?;
if self
.current
.as_ref()
.is_some_and(|s| !Rc::ptr_eq(&s.0, &prior.0))
|| self.current.is_none() && prior.0.end != 0
{
return Err(Error::InvalidQwenShape);
}
let generation = self
.generation
.checked_add(1)
.ok_or(Error::InvalidQwenShape)?;
self.current = None;
self.generation = generation;
Ok(generation)
}
pub(crate) fn finish_original_graph_mutation(
&mut self,
prior: &MtpInt8Snapshot,
first: usize,
end: usize,
generation: u64,
) -> Result<MtpInt8Snapshot> {
self.validate_snapshot(prior)?;
if self.current.is_some()
|| self.generation != generation
|| first > prior.0.end
|| end <= first
|| end > MTP_ORIGINAL_MAX_CELLS
{
return Err(Error::InvalidQwenShape);
}
let page0 = first / 4;
let page1 = end.div_ceil(4);
let mut pages = Vec::new();
pages
.try_reserve_exact(page1)
.map_err(|_| Error::HostAllocation)?;
for p in 0..page1 {
if p < page0 {
pages.push(prior.0.pages.get(p).ok_or(Error::InvalidQwenShape)?.clone());
continue;
}
let mut runs: [Vec<u8>; 4] = std::array::from_fn(|_| Vec::new());
for (i, n) in PAGE_BYTES.into_iter().enumerate() {
runs[i] = zero_bytes(n)?;
}
self.read_encoded_page(p, &mut runs)?;
pages.push(Rc::new(Page {
runs: runs.map(Vec::into_boxed_slice),
}));
}
let snapshot = MtpInt8Snapshot(Rc::new(Snapshot {
origin: self.origin.clone(),
pages: Rc::new(pages.into_boxed_slice()),
end,
// the leading pages are the checked prior's (`validate_snapshot` above), the others were just sized by `zero_bytes`
checked: std::cell::Cell::new(true),
}));
self.current = Some(snapshot.clone());
Ok(snapshot)
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff