STRA-338 size2 en3: state mirror, fused mirror candidate and verification workspace split into phase helpers
This commit is contained in:
@@ -157,19 +157,7 @@ impl<'a, 'ctx> RowEnv<'a, 'ctx> {
|
||||
let (state, next) = owner.native_fused_operands(row)?;
|
||||
let mut program = program.try_borrow_mut().map_err(|_| Error::IdentityMismatch)?;
|
||||
program.validate_context(self.context).map_err(driver_error)?;
|
||||
let inputs = FusedInputs {
|
||||
qkv: &operands.qkv,
|
||||
z: &operands.z,
|
||||
prior_history: owner.history()?,
|
||||
mixed: operands.mixed,
|
||||
w_alpha: operands.w_alpha,
|
||||
w_beta: operands.w_beta,
|
||||
conv_weights: self.aux[0],
|
||||
dt: self.aux[1],
|
||||
a: self.aux[2],
|
||||
gamma: self.aux[3],
|
||||
epsilon: self.weights.epsilon(),
|
||||
};
|
||||
let inputs = self.fused_inputs(owner, operands)?;
|
||||
if owner.is_verification() {
|
||||
let produced = chain.run_fused_verification_row(&mut program, Some(state), next, inputs)?;
|
||||
return Ok((produced.next_history, produced.normalized));
|
||||
@@ -181,4 +169,23 @@ impl<'a, 'ctx> RowEnv<'a, 'ctx> {
|
||||
drop(state_values);
|
||||
Ok((produced.next_history, produced.normalized))
|
||||
}
|
||||
fn fused_inputs<'o>(
|
||||
&'o self,
|
||||
owner: &'o chunk_device::GdnChunkDevice<'ctx>,
|
||||
operands: &'o FusedOperands<'o, 'ctx>,
|
||||
) -> Result<FusedInputs<'o, 'ctx>> {
|
||||
Ok(FusedInputs {
|
||||
qkv: &operands.qkv,
|
||||
z: &operands.z,
|
||||
prior_history: owner.history()?,
|
||||
mixed: operands.mixed,
|
||||
w_alpha: operands.w_alpha,
|
||||
w_beta: operands.w_beta,
|
||||
conv_weights: self.aux[0],
|
||||
dt: self.aux[1],
|
||||
a: self.aux[2],
|
||||
gamma: self.aux[3],
|
||||
epsilon: self.weights.epsilon(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,6 +338,17 @@ enum QueuedQkv<'ctx> {
|
||||
Host(chain::Product),
|
||||
Retained(RetainedRow<'ctx>),
|
||||
}
|
||||
impl<'ctx> QueuedQkv<'ctx> {
|
||||
fn as_input(&self) -> Result<chain::QkvInput<'_, 'ctx>> {
|
||||
Ok(match self {
|
||||
QueuedQkv::Host(product) => chain::QkvInput::Host(product),
|
||||
QueuedQkv::Retained(row) => {
|
||||
let (buffer, row) = row.parts()?;
|
||||
chain::QkvInput::Columns { buffer, row }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
// The row's operands in the order the queued drain consumes them.
|
||||
struct QueuedOperands<'ctx> {
|
||||
qkv: QueuedQkv<'ctx>,
|
||||
@@ -413,17 +424,12 @@ impl<'a, 'ctx> RowEnv<'a, 'ctx> {
|
||||
operands: &QueuedOperands<'ctx>,
|
||||
) -> Result<chain::QueuedRow> {
|
||||
let (state, next_state) = owner.queue_operands(row)?;
|
||||
let qkv = operands.qkv.as_input()?;
|
||||
chain.run_row_queue(
|
||||
queue,
|
||||
chain::QueueInputs {
|
||||
history: &operands.history,
|
||||
qkv: match &operands.qkv {
|
||||
QueuedQkv::Host(product) => chain::QkvInput::Host(product),
|
||||
QueuedQkv::Retained(row) => {
|
||||
let (buffer, row) = row.parts()?;
|
||||
chain::QkvInput::Columns { buffer, row }
|
||||
}
|
||||
},
|
||||
qkv,
|
||||
alpha: &operands.alpha,
|
||||
beta_raw: &operands.beta_raw,
|
||||
z: &operands.z,
|
||||
|
||||
+148
-79
@@ -16,13 +16,7 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
if layer >= 48 {
|
||||
return Err(Error::InvalidLayer);
|
||||
}
|
||||
// Charge all fixed mirrors while the actor loads, before lazy MoE can
|
||||
// consume the scratch reserve. Uninitialized placeholders cannot hit.
|
||||
preflight_budget(
|
||||
context.budget_bytes(),
|
||||
context.allocated_bytes().checked_add(MIRROR_BYTES).ok_or(Error::Overflow)?,
|
||||
)?;
|
||||
let buffer = context.allocate(MIRROR_BYTES).map_err(driver_error)?;
|
||||
let buffer = Self::allocate_mirror(context)?;
|
||||
Ok(Self {
|
||||
context,
|
||||
binding,
|
||||
@@ -37,6 +31,16 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
})
|
||||
}
|
||||
|
||||
// Charge all fixed mirrors while the actor loads, before lazy MoE can
|
||||
// consume the scratch reserve. Uninitialized placeholders cannot hit.
|
||||
fn allocate_mirror(context: &'ctx Context<'ctx>) -> Result<DeviceBuffer<'ctx>> {
|
||||
preflight_budget(
|
||||
context.budget_bytes(),
|
||||
context.allocated_bytes().checked_add(MIRROR_BYTES).ok_or(Error::Overflow)?,
|
||||
)?;
|
||||
context.allocate(MIRROR_BYTES).map_err(driver_error)
|
||||
}
|
||||
|
||||
pub(in super::super::super) fn compute(
|
||||
&mut self,
|
||||
prior: &GdnPrior,
|
||||
@@ -81,17 +85,32 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
gate: &[f32],
|
||||
beta: &[f32],
|
||||
) -> Result<(MirrorReadout<'ctx>, MirrorNext<'ctx>)> {
|
||||
self.admit_prior(prior)?;
|
||||
for (values, count) in
|
||||
[(prior.values(), STATE_ELEMENTS), (q, 2048), (k, 2048), (v, VALUES), (gate, 48), (beta, 48)]
|
||||
{
|
||||
validate_values(values, count)?;
|
||||
}
|
||||
let hit = self.admit_resident(prior)?;
|
||||
let mut temporary = Vec::new();
|
||||
temporary.try_reserve_exact(7).map_err(|_| Error::InputAllocation)?;
|
||||
let mut next = None;
|
||||
let mut output = None;
|
||||
let operation = self.step_uploaded(prior, hit, [q, k, v, gate, beta], &mut temporary, (&mut next, &mut output));
|
||||
self.settle_step(prior, (None, None), operation, temporary, next, output)
|
||||
}
|
||||
// The failed flag and the prior's mirror admission, checked before anything else.
|
||||
pub(in super::super) fn admit_prior(&self, prior: &GdnPrior) -> Result<()> {
|
||||
if self.failed.get() {
|
||||
return Err(Error::CudaExecution);
|
||||
}
|
||||
if !prior.mirror_admits(&self.binding, self.layer) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
for (values, count) in
|
||||
[(prior.values(), STATE_ELEMENTS), (q, 2048), (k, 2048), (v, VALUES), (gate, 48), (beta, 48)]
|
||||
{
|
||||
validate_values(values, count)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
// The resident buffer's shape and the budget; whether `prior` is exactly the state the mirror holds.
|
||||
pub(in super::super) fn admit_resident(&mut self, prior: &GdnPrior) -> Result<bool> {
|
||||
let resident = self.buffer.as_ref().ok_or(Error::CudaExecution)?;
|
||||
if resident.len() != MIRROR_BYTES {
|
||||
return Err(Error::InvalidDimension);
|
||||
@@ -104,62 +123,77 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
} else {
|
||||
self.misses += 1;
|
||||
}
|
||||
let mut temporary = Vec::new();
|
||||
temporary.try_reserve_exact(7).map_err(|_| Error::InputAllocation)?;
|
||||
let mut next = None;
|
||||
let mut output = None;
|
||||
let operation = (|| -> Result<super::super::super::CandidatePair> {
|
||||
// A rollback/reset miss uploads canonical host bytes into a
|
||||
// transient prior; it never changes the identity of the mirror.
|
||||
if !hit {
|
||||
temporary.push(upload_checked(self.context, prior.values(), &self.failed)?);
|
||||
}
|
||||
let first = temporary.len();
|
||||
for values in [q, k, v, gate, beta] {
|
||||
temporary.push(upload_checked(self.context, values, &self.failed)?);
|
||||
}
|
||||
next = Some(allocate(self.context, STATE_ELEMENTS)?);
|
||||
output = Some(allocate(self.context, VALUES)?);
|
||||
let state = if hit { resident } else { &temporary[0] };
|
||||
self.context
|
||||
.gdn_step_f32(
|
||||
state,
|
||||
&temporary[first],
|
||||
&temporary[first + 1],
|
||||
&temporary[first + 2],
|
||||
&temporary[first + 3],
|
||||
&temporary[first + 4],
|
||||
next.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
output.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
)
|
||||
.map_err(driver_error)?;
|
||||
Ok((
|
||||
download(next.as_ref().ok_or(Error::IdentityMismatch)?, STATE_ELEMENTS)?,
|
||||
download(output.as_ref().ok_or(Error::IdentityMismatch)?, VALUES)?,
|
||||
))
|
||||
})();
|
||||
let mut cleanup = None;
|
||||
for buffer in temporary {
|
||||
if let Err(error) = buffer.release() {
|
||||
self.failed.set(true);
|
||||
cleanup.get_or_insert(driver_error(error));
|
||||
}
|
||||
Ok(hit)
|
||||
}
|
||||
pub(in super::super) fn resident_buffer(&self) -> Result<&DeviceBuffer<'ctx>> {
|
||||
self.buffer.as_ref().ok_or(Error::CudaExecution)
|
||||
}
|
||||
// A rollback/reset miss uploads canonical host bytes into a
|
||||
// transient prior; it never changes the identity of the mirror.
|
||||
pub(in super::super) fn upload_prior_on_miss(
|
||||
&self,
|
||||
prior: &GdnPrior,
|
||||
hit: bool,
|
||||
temporary: &mut Vec<DeviceBuffer<'ctx>>,
|
||||
) -> Result<()> {
|
||||
if !hit {
|
||||
temporary.push(upload_checked(self.context, prior.values(), &self.failed)?);
|
||||
}
|
||||
let result = match operation {
|
||||
Err(primary) => Err(primary),
|
||||
Ok(values) => cleanup.map_or(Ok(values), Err),
|
||||
};
|
||||
match result {
|
||||
Ok(())
|
||||
}
|
||||
fn step_uploaded(
|
||||
&self,
|
||||
prior: &GdnPrior,
|
||||
hit: bool,
|
||||
inputs: [&[f32]; 5],
|
||||
temporary: &mut Vec<DeviceBuffer<'ctx>>,
|
||||
(next, output): (&mut Option<DeviceBuffer<'ctx>>, &mut Option<DeviceBuffer<'ctx>>),
|
||||
) -> Result<super::super::super::CandidatePair> {
|
||||
self.upload_prior_on_miss(prior, hit, temporary)?;
|
||||
let first = temporary.len();
|
||||
for values in inputs {
|
||||
temporary.push(upload_checked(self.context, values, &self.failed)?);
|
||||
}
|
||||
*next = Some(allocate(self.context, STATE_ELEMENTS)?);
|
||||
*output = Some(allocate(self.context, VALUES)?);
|
||||
let state = if hit { self.resident_buffer()? } else { &temporary[0] };
|
||||
self.context
|
||||
.gdn_step_f32(
|
||||
state,
|
||||
&temporary[first],
|
||||
&temporary[first + 1],
|
||||
&temporary[first + 2],
|
||||
&temporary[first + 3],
|
||||
&temporary[first + 4],
|
||||
next.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
output.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
)
|
||||
.map_err(driver_error)?;
|
||||
Self::read_step(next, output)
|
||||
}
|
||||
pub(in super::super) fn read_step(
|
||||
next: &Option<DeviceBuffer<'ctx>>,
|
||||
output: &Option<DeviceBuffer<'ctx>>,
|
||||
) -> Result<super::super::super::CandidatePair> {
|
||||
Ok((
|
||||
download(next.as_ref().ok_or(Error::IdentityMismatch)?, STATE_ELEMENTS)?,
|
||||
download(output.as_ref().ok_or(Error::IdentityMismatch)?, VALUES)?,
|
||||
))
|
||||
}
|
||||
// Release the transient uploads, then mint the candidate pair or free what the refused step still holds.
|
||||
pub(in super::super) fn settle_step(
|
||||
&self,
|
||||
prior: &GdnPrior,
|
||||
(identity, scope): (Option<TokenLayer>, Option<Rc<()>>),
|
||||
operation: Result<super::super::super::CandidatePair>,
|
||||
temporary: Vec<DeviceBuffer<'ctx>>,
|
||||
mut next: Option<DeviceBuffer<'ctx>>,
|
||||
mut output: Option<DeviceBuffer<'ctx>>,
|
||||
) -> Result<(MirrorReadout<'ctx>, MirrorNext<'ctx>)> {
|
||||
let cleanup = self.release_temporaries(temporary);
|
||||
match operation.and_then(|values| cleanup.map_or(Ok(values), Err)) {
|
||||
Ok((state, readout)) => Ok((
|
||||
MirrorReadout {
|
||||
context: self.context,
|
||||
source: prior.clone(),
|
||||
identity: None,
|
||||
scope: None,
|
||||
buffer: output.take(),
|
||||
values: Some(readout),
|
||||
failed: self.failed.clone(),
|
||||
},
|
||||
self.minted_readout(prior, (identity, scope), output.take(), readout),
|
||||
MirrorNext {
|
||||
context: self.context,
|
||||
source: prior.clone(),
|
||||
@@ -168,23 +202,58 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
},
|
||||
)),
|
||||
Err(primary) => {
|
||||
if primary == Error::CudaExecution {
|
||||
self.failed.set(true);
|
||||
}
|
||||
if let Some(buffer) = next
|
||||
&& buffer.release().is_err()
|
||||
{
|
||||
self.failed.set(true);
|
||||
}
|
||||
if let Some(buffer) = output
|
||||
&& buffer.release().is_err()
|
||||
{
|
||||
self.failed.set(true);
|
||||
}
|
||||
self.discard_step_buffers(primary, next, output);
|
||||
Err(primary)
|
||||
}
|
||||
}
|
||||
}
|
||||
fn release_temporaries(&self, temporary: Vec<DeviceBuffer<'ctx>>) -> Option<Error> {
|
||||
let mut cleanup = None;
|
||||
for buffer in temporary {
|
||||
if let Err(error) = buffer.release() {
|
||||
self.failed.set(true);
|
||||
cleanup.get_or_insert(driver_error(error));
|
||||
}
|
||||
}
|
||||
cleanup
|
||||
}
|
||||
fn minted_readout(
|
||||
&self,
|
||||
prior: &GdnPrior,
|
||||
(identity, scope): (Option<TokenLayer>, Option<Rc<()>>),
|
||||
buffer: Option<DeviceBuffer<'ctx>>,
|
||||
readout: Box<[f32]>,
|
||||
) -> MirrorReadout<'ctx> {
|
||||
MirrorReadout {
|
||||
context: self.context,
|
||||
source: prior.clone(),
|
||||
identity,
|
||||
scope,
|
||||
buffer,
|
||||
values: Some(readout),
|
||||
failed: self.failed.clone(),
|
||||
}
|
||||
}
|
||||
fn discard_step_buffers(
|
||||
&self,
|
||||
primary: Error,
|
||||
next: Option<DeviceBuffer<'ctx>>,
|
||||
output: Option<DeviceBuffer<'ctx>>,
|
||||
) {
|
||||
if primary == Error::CudaExecution {
|
||||
self.failed.set(true);
|
||||
}
|
||||
if let Some(buffer) = next
|
||||
&& buffer.release().is_err()
|
||||
{
|
||||
self.failed.set(true);
|
||||
}
|
||||
if let Some(buffer) = output
|
||||
&& buffer.release().is_err()
|
||||
{
|
||||
self.failed.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
pub(in super::super::super) fn discard(&mut self, next: MirrorNext<'ctx>) -> Result<()> {
|
||||
next.buffer.release().map_err(|error| {
|
||||
|
||||
+62
-108
@@ -11,102 +11,42 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
txn: &super::super::super::QwenStepTxn<'_, '_>,
|
||||
inputs: super::super::super::chain::ScalarRecurrenceBorrow<'_, 'ctx>,
|
||||
) -> Result<(MirrorReadout<'ctx>, MirrorNext<'ctx>)> {
|
||||
let (scope, [qk, h, gate, beta]) =
|
||||
inputs.into_parts(self.context, &self.binding, &self.failed, prior, identity, txn)?;
|
||||
if self.failed.get() {
|
||||
return Err(Error::CudaExecution);
|
||||
}
|
||||
if !prior.mirror_admits(&self.binding, self.layer) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
let (scope, products) = inputs.into_parts(self.context, &self.binding, &self.failed, prior, identity, txn)?;
|
||||
self.admit_prior(prior)?;
|
||||
validate_values(prior.values(), STATE_ELEMENTS)?;
|
||||
let resident = self.buffer.as_ref().ok_or(Error::CudaExecution)?;
|
||||
if resident.len() != MIRROR_BYTES {
|
||||
return Err(Error::InvalidDimension);
|
||||
}
|
||||
preflight_budget(self.context.budget_bytes(), self.context.allocated_bytes())?;
|
||||
let hit = self.key.as_ref().is_some_and(|key| key.matches(prior));
|
||||
#[cfg(test)]
|
||||
if hit {
|
||||
self.hits += 1;
|
||||
} else {
|
||||
self.misses += 1;
|
||||
}
|
||||
let hit = self.admit_resident(prior)?;
|
||||
let mut temporary = Vec::new();
|
||||
temporary.try_reserve_exact(1).map_err(|_| Error::InputAllocation)?;
|
||||
let mut next = None;
|
||||
let mut output = None;
|
||||
let operation = (|| -> Result<super::super::super::CandidatePair> {
|
||||
// A rollback/reset miss uploads canonical host bytes into a
|
||||
// transient prior; it never changes the identity of the mirror.
|
||||
if !hit {
|
||||
temporary.push(upload_checked(self.context, prior.values(), &self.failed)?);
|
||||
}
|
||||
next = Some(allocate(self.context, STATE_ELEMENTS)?);
|
||||
output = Some(allocate(self.context, VALUES)?);
|
||||
let state = if hit { resident } else { &temporary[0] };
|
||||
self.context
|
||||
.gdn_step_from_products_f32(
|
||||
state,
|
||||
qk,
|
||||
h,
|
||||
gate,
|
||||
beta,
|
||||
next.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
output.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
)
|
||||
.map_err(driver_error)?;
|
||||
Ok((
|
||||
download(next.as_ref().ok_or(Error::IdentityMismatch)?, STATE_ELEMENTS)?,
|
||||
download(output.as_ref().ok_or(Error::IdentityMismatch)?, VALUES)?,
|
||||
))
|
||||
})();
|
||||
let mut cleanup = None;
|
||||
for buffer in temporary {
|
||||
if let Err(error) = buffer.release() {
|
||||
self.failed.set(true);
|
||||
cleanup.get_or_insert(driver_error(error));
|
||||
}
|
||||
}
|
||||
let result = match operation {
|
||||
Err(primary) => Err(primary),
|
||||
Ok(values) => cleanup.map_or(Ok(values), Err),
|
||||
};
|
||||
match result {
|
||||
Ok((state, readout)) => Ok((
|
||||
MirrorReadout {
|
||||
context: self.context,
|
||||
source: prior.clone(),
|
||||
identity: Some(identity),
|
||||
scope: Some(scope),
|
||||
buffer: output.take(),
|
||||
values: Some(readout),
|
||||
failed: self.failed.clone(),
|
||||
},
|
||||
MirrorNext {
|
||||
context: self.context,
|
||||
source: prior.clone(),
|
||||
buffer: next.take().ok_or(Error::IdentityMismatch)?,
|
||||
values: state,
|
||||
},
|
||||
)),
|
||||
Err(primary) => {
|
||||
if primary == Error::CudaExecution {
|
||||
self.failed.set(true);
|
||||
}
|
||||
if let Some(buffer) = next
|
||||
&& buffer.release().is_err()
|
||||
{
|
||||
self.failed.set(true);
|
||||
}
|
||||
if let Some(buffer) = output
|
||||
&& buffer.release().is_err()
|
||||
{
|
||||
self.failed.set(true);
|
||||
}
|
||||
Err(primary)
|
||||
}
|
||||
}
|
||||
let operation = self.step_products(prior, hit, products, &mut temporary, (&mut next, &mut output));
|
||||
self.settle_step(prior, (Some(identity), Some(scope)), operation, temporary, next, output)
|
||||
}
|
||||
|
||||
fn step_products(
|
||||
&self,
|
||||
prior: &GdnPrior,
|
||||
hit: bool,
|
||||
[qk, h, gate, beta]: [&DeviceBuffer<'ctx>; 4],
|
||||
temporary: &mut Vec<DeviceBuffer<'ctx>>,
|
||||
(next, output): (&mut Option<DeviceBuffer<'ctx>>, &mut Option<DeviceBuffer<'ctx>>),
|
||||
) -> Result<super::super::super::CandidatePair> {
|
||||
self.upload_prior_on_miss(prior, hit, temporary)?;
|
||||
*next = Some(allocate(self.context, STATE_ELEMENTS)?);
|
||||
*output = Some(allocate(self.context, VALUES)?);
|
||||
let state = if hit { self.resident_buffer()? } else { &temporary[0] };
|
||||
self.context
|
||||
.gdn_step_from_products_f32(
|
||||
state,
|
||||
qk,
|
||||
h,
|
||||
gate,
|
||||
beta,
|
||||
next.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
output.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
)
|
||||
.map_err(driver_error)?;
|
||||
Self::read_step(next, output)
|
||||
}
|
||||
|
||||
pub(in super::super::super) fn publish(
|
||||
@@ -116,21 +56,7 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
history: Box<[f32]>,
|
||||
) -> Result<GdnPending> {
|
||||
let MirrorNext { context, source, buffer, values } = next;
|
||||
let admitted = (|| -> Result<(GdnStateKey, GdnPending)> {
|
||||
if self.failed.get() {
|
||||
return Err(Error::CudaExecution);
|
||||
}
|
||||
if !std::ptr::eq(self.context, context)
|
||||
|| buffer.len() != MIRROR_BYTES
|
||||
|| !source.mirror_admits(&self.binding, self.layer)
|
||||
{
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
// Only this producer owns the values downloaded from this exact
|
||||
// candidate buffer. A caller cannot pair it with another payload.
|
||||
let pending = GdnPending::new(&source, identity, values, history)?;
|
||||
Ok((pending.mirror_next(&source)?.mirror_key(), pending))
|
||||
})();
|
||||
let admitted = self.admit_publish(context, &buffer, &source, identity, (values, history));
|
||||
let (key, pending) = match admitted {
|
||||
Ok(value) => value,
|
||||
Err(primary) => {
|
||||
@@ -138,7 +64,35 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
return Err(primary);
|
||||
}
|
||||
};
|
||||
// Never publish a hit key until replacement cleanup succeeded.
|
||||
self.replace_buffer(buffer, key)?;
|
||||
Ok(pending)
|
||||
}
|
||||
|
||||
fn admit_publish(
|
||||
&self,
|
||||
context: &Context<'_>,
|
||||
buffer: &DeviceBuffer<'ctx>,
|
||||
source: &GdnPrior,
|
||||
identity: TokenLayer,
|
||||
(values, history): (Box<[f32]>, Box<[f32]>),
|
||||
) -> Result<(GdnStateKey, GdnPending)> {
|
||||
if self.failed.get() {
|
||||
return Err(Error::CudaExecution);
|
||||
}
|
||||
if !std::ptr::eq(self.context, context)
|
||||
|| buffer.len() != MIRROR_BYTES
|
||||
|| !source.mirror_admits(&self.binding, self.layer)
|
||||
{
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
// Only this producer owns the values downloaded from this exact
|
||||
// candidate buffer. A caller cannot pair it with another payload.
|
||||
let pending = GdnPending::new(source, identity, values, history)?;
|
||||
Ok((pending.mirror_next(source)?.mirror_key(), pending))
|
||||
}
|
||||
|
||||
// Never publish a hit key until replacement cleanup succeeded.
|
||||
fn replace_buffer(&mut self, buffer: DeviceBuffer<'ctx>, key: GdnStateKey) -> Result<()> {
|
||||
self.key = None;
|
||||
let old = match self.buffer.take() {
|
||||
Some(old) => old,
|
||||
@@ -155,7 +109,7 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
}
|
||||
self.buffer = Some(buffer);
|
||||
self.key = Some(key);
|
||||
Ok(pending)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The device image of `prior` when this mirror holds exactly that state (identity of the producing state, never a
|
||||
|
||||
@@ -16,43 +16,47 @@ impl<'ctx> GdnStateMirror<'ctx> {
|
||||
prior: &GdnPrior,
|
||||
run: impl FnOnce(Option<&DeviceBuffer<'ctx>>, &DeviceBuffer<'ctx>) -> Result<(R, Box<[f32]>)>,
|
||||
) -> Result<(R, MirrorNext<'ctx>)> {
|
||||
if self.failed.get() {
|
||||
return Err(Error::CudaExecution);
|
||||
}
|
||||
if !prior.mirror_admits(&self.binding, self.layer) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
self.admit_prior(prior)?;
|
||||
validate_values(prior.values(), STATE_ELEMENTS)?;
|
||||
let resident = self.buffer.as_ref().ok_or(Error::CudaExecution)?;
|
||||
if resident.len() != MIRROR_BYTES {
|
||||
return Err(Error::InvalidDimension);
|
||||
}
|
||||
preflight_budget(self.context.budget_bytes(), self.context.allocated_bytes())?;
|
||||
let hit = self.key.as_ref().is_some_and(|key| key.matches(prior));
|
||||
#[cfg(test)]
|
||||
if hit {
|
||||
self.hits += 1;
|
||||
} else {
|
||||
self.misses += 1;
|
||||
}
|
||||
let hit = self.admit_resident(prior)?;
|
||||
let mut next = allocate(self.context, STATE_ELEMENTS)?;
|
||||
let operation = (|| -> Result<(R, Box<[f32]>)> {
|
||||
if !hit {
|
||||
let mut bytes = Vec::new();
|
||||
bytes.try_reserve_exact(MIRROR_BYTES).map_err(|_| Error::InputAllocation)?;
|
||||
for value in prior.values() {
|
||||
bytes.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
if let Err(error) = next.copy_from(&bytes) {
|
||||
let primary = driver_error(error);
|
||||
if primary == Error::CudaExecution {
|
||||
self.failed.set(true);
|
||||
}
|
||||
return Err(primary);
|
||||
}
|
||||
let operation = self.run_candidate(prior, hit, &mut next, run);
|
||||
self.wrap_candidate(prior, next, operation)
|
||||
}
|
||||
fn run_candidate<R>(
|
||||
&self,
|
||||
prior: &GdnPrior,
|
||||
hit: bool,
|
||||
next: &mut DeviceBuffer<'ctx>,
|
||||
run: impl FnOnce(Option<&DeviceBuffer<'ctx>>, &DeviceBuffer<'ctx>) -> Result<(R, Box<[f32]>)>,
|
||||
) -> Result<(R, Box<[f32]>)> {
|
||||
if !hit {
|
||||
self.seed_candidate(prior, next)?;
|
||||
}
|
||||
run(if hit { Some(self.resident_buffer()?) } else { None }, next)
|
||||
}
|
||||
// A miss/rollback/reset: the canonical host prior bytes go into the candidate.
|
||||
fn seed_candidate(&self, prior: &GdnPrior, next: &mut DeviceBuffer<'ctx>) -> Result<()> {
|
||||
let mut bytes = Vec::new();
|
||||
bytes.try_reserve_exact(MIRROR_BYTES).map_err(|_| Error::InputAllocation)?;
|
||||
for value in prior.values() {
|
||||
bytes.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
if let Err(error) = next.copy_from(&bytes) {
|
||||
let primary = driver_error(error);
|
||||
if primary == Error::CudaExecution {
|
||||
self.failed.set(true);
|
||||
}
|
||||
run(if hit { Some(resident) } else { None }, &next)
|
||||
})();
|
||||
return Err(primary);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn wrap_candidate<R>(
|
||||
&self,
|
||||
prior: &GdnPrior,
|
||||
next: DeviceBuffer<'ctx>,
|
||||
operation: Result<(R, Box<[f32]>)>,
|
||||
) -> Result<(R, MirrorNext<'ctx>)> {
|
||||
match operation {
|
||||
Ok((value, state)) => {
|
||||
if state.len() != STATE_ELEMENTS {
|
||||
|
||||
+73
-58
@@ -15,7 +15,18 @@ impl<'ctx> Workspace<'ctx> {
|
||||
{
|
||||
return Err(Error::InputAllocation);
|
||||
}
|
||||
let mut owner = Self {
|
||||
let mut owner = Self::empty(binding, context, failed);
|
||||
if let Err(primary) = owner.allocate_owners() {
|
||||
if primary == Error::CudaExecution {
|
||||
owner.failed.set(true);
|
||||
}
|
||||
let _ = owner.cleanup();
|
||||
return Err(primary);
|
||||
}
|
||||
Ok(owner)
|
||||
}
|
||||
fn empty(binding: BindingIdentity, context: &'ctx Context<'ctx>, failed: Rc<Cell<bool>>) -> Self {
|
||||
Self {
|
||||
context,
|
||||
binding,
|
||||
failed,
|
||||
@@ -28,40 +39,36 @@ impl<'ctx> Workspace<'ctx> {
|
||||
generation: 0,
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
native_fused: None,
|
||||
};
|
||||
let allocation = (|| -> Result<()> {
|
||||
owner.operands.try_reserve_exact(SLOTS).map_err(|_| Error::InputAllocation)?;
|
||||
owner.ready.try_reserve_exact(SLOTS).map_err(|_| Error::InputAllocation)?;
|
||||
owner.operands.resize_with(SLOTS, || [None, None, None, None]);
|
||||
owner.ready.resize_with(SLOTS, || None);
|
||||
// Actual capacities are bounded metadata charged separately to host admission.
|
||||
let metadata = owner.metadata_bytes()?;
|
||||
if metadata > 1024 * 1024 {
|
||||
return Err(Error::InputAllocation);
|
||||
}
|
||||
for slot in &mut owner.operands {
|
||||
for (buffer, count) in slot.iter_mut().zip(COUNTS) {
|
||||
*buffer = Some(allocate(context, count)?);
|
||||
}
|
||||
}
|
||||
for state in &mut owner.states {
|
||||
*state = Some(allocate(context, STATE_ELEMENTS)?);
|
||||
}
|
||||
owner.readout = Some(allocate(context, VALUES)?);
|
||||
owner.guard = Some(context.gdn_state_finite_guard().map_err(driver_error)?);
|
||||
if context.memory_info().map_err(driver_error)?.0 < RESERVE {
|
||||
return Err(Error::InputAllocation);
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
if let Err(primary) = allocation {
|
||||
if primary == Error::CudaExecution {
|
||||
owner.failed.set(true);
|
||||
}
|
||||
let _ = owner.cleanup();
|
||||
return Err(primary);
|
||||
}
|
||||
Ok(owner)
|
||||
}
|
||||
fn allocate_owners(&mut self) -> Result<()> {
|
||||
self.operands.try_reserve_exact(SLOTS).map_err(|_| Error::InputAllocation)?;
|
||||
self.ready.try_reserve_exact(SLOTS).map_err(|_| Error::InputAllocation)?;
|
||||
self.operands.resize_with(SLOTS, || [None, None, None, None]);
|
||||
self.ready.resize_with(SLOTS, || None);
|
||||
// Actual capacities are bounded metadata charged separately to host admission.
|
||||
let metadata = self.metadata_bytes()?;
|
||||
if metadata > 1024 * 1024 {
|
||||
return Err(Error::InputAllocation);
|
||||
}
|
||||
self.allocate_operands()?;
|
||||
for state in &mut self.states {
|
||||
*state = Some(allocate(self.context, STATE_ELEMENTS)?);
|
||||
}
|
||||
self.readout = Some(allocate(self.context, VALUES)?);
|
||||
self.guard = Some(self.context.gdn_state_finite_guard().map_err(driver_error)?);
|
||||
if self.context.memory_info().map_err(driver_error)?.0 < RESERVE {
|
||||
return Err(Error::InputAllocation);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn allocate_operands(&mut self) -> Result<()> {
|
||||
for slot in &mut self.operands {
|
||||
for (buffer, count) in slot.iter_mut().zip(COUNTS) {
|
||||
*buffer = Some(allocate(self.context, count)?);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub(in crate::qwen_forward) fn begin(
|
||||
&mut self,
|
||||
@@ -73,22 +80,11 @@ impl<'ctx> Workspace<'ctx> {
|
||||
if self.failed.get() {
|
||||
return Err(Error::CudaExecution);
|
||||
}
|
||||
if !self.binding.matches(binding)
|
||||
|| !(1..=3).contains(&count)
|
||||
|| self.active.upgrade().is_some()
|
||||
|| self.operands.iter().any(|row| row.iter().any(Option::is_none))
|
||||
|| self.states.iter().any(Option::is_none)
|
||||
|| self.readout.is_none()
|
||||
|| self.guard.is_none()
|
||||
{
|
||||
if !self.begin_admissible(binding, count) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
let generation = self.generation.checked_add(1).ok_or(Error::Overflow)?;
|
||||
self.ready.iter_mut().for_each(|entry| *entry = None);
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
{
|
||||
self.native_fused = None;
|
||||
}
|
||||
self.reset_ready();
|
||||
let lease = Rc::new(ActiveLease {
|
||||
binding: binding.clone(),
|
||||
origin: origin.clone(),
|
||||
@@ -101,6 +97,22 @@ impl<'ctx> Workspace<'ctx> {
|
||||
self.active = Rc::downgrade(&lease);
|
||||
Ok(lease)
|
||||
}
|
||||
fn reset_ready(&mut self) {
|
||||
self.ready.iter_mut().for_each(|entry| *entry = None);
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
{
|
||||
self.native_fused = None;
|
||||
}
|
||||
}
|
||||
fn begin_admissible(&self, binding: &BindingIdentity, count: usize) -> bool {
|
||||
self.binding.matches(binding)
|
||||
&& (1..=3).contains(&count)
|
||||
&& self.active.upgrade().is_none()
|
||||
&& !self.operands.iter().any(|row| row.iter().any(Option::is_none))
|
||||
&& !self.states.iter().any(Option::is_none)
|
||||
&& self.readout.is_some()
|
||||
&& self.guard.is_some()
|
||||
}
|
||||
pub(in crate::qwen_forward) fn metadata_bytes(&self) -> Result<usize> {
|
||||
self.operands
|
||||
.capacity()
|
||||
@@ -154,17 +166,7 @@ impl<'ctx> Workspace<'ctx> {
|
||||
if self.states.iter().any(Option::is_none) || self.guard.is_none() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
// Immutable device prior occupies state2; state0/1 alone roll.
|
||||
let mut bytes = Vec::new();
|
||||
bytes.try_reserve_exact(STATE_ELEMENTS * 4).map_err(|_| Error::InputAllocation)?;
|
||||
for value in start.prior().values() {
|
||||
bytes.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
self.states[2].as_mut().ok_or(Error::IdentityMismatch)?.copy_from_owned(bytes).map_err(|error| {
|
||||
self.failed.set(true);
|
||||
lease.refuse();
|
||||
driver_error(error)
|
||||
})?;
|
||||
self.reupload_prior(lease, start.prior())?;
|
||||
Ok((
|
||||
[
|
||||
self.states[0].take().ok_or(Error::IdentityMismatch)?,
|
||||
@@ -174,6 +176,19 @@ impl<'ctx> Workspace<'ctx> {
|
||||
self.guard.take().ok_or(Error::IdentityMismatch)?,
|
||||
))
|
||||
}
|
||||
// Immutable device prior occupies state2; state0/1 alone roll.
|
||||
pub(in super::super) fn reupload_prior(&mut self, lease: &Rc<ActiveLease>, prior: &GdnPrior) -> Result<()> {
|
||||
let mut bytes = Vec::new();
|
||||
bytes.try_reserve_exact(STATE_ELEMENTS * 4).map_err(|_| Error::InputAllocation)?;
|
||||
for value in prior.values() {
|
||||
bytes.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
self.states[2].as_mut().ok_or(Error::IdentityMismatch)?.copy_from_owned(bytes).map_err(|error| {
|
||||
self.failed.set(true);
|
||||
lease.refuse();
|
||||
driver_error(error)
|
||||
})
|
||||
}
|
||||
pub(in super::super::super) fn return_layer(
|
||||
&mut self,
|
||||
states: [DeviceBuffer<'ctx>; 3],
|
||||
|
||||
+137
-91
@@ -22,111 +22,157 @@ impl<'ctx> Workspace<'ctx> {
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
row.validate(lease, prior.layer(), index, row.row.identity())?;
|
||||
let slot = Self::slot(prior.layer(), index)?;
|
||||
if self.ready[slot].as_ref().and_then(Weak::upgrade).is_none_or(|saved| !Rc::ptr_eq(&saved, row)) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
let input = if index == 0 { 2 } else { state };
|
||||
let output = if index == 0 { 0 } else { 1 - state };
|
||||
// Re-upload untouched actual prior once per replayed layer. The same
|
||||
// third whole owner is immutable throughout its accepted prefix.
|
||||
if index == 0 {
|
||||
let mut bytes = Vec::new();
|
||||
bytes.try_reserve_exact(STATE_ELEMENTS * 4).map_err(|_| Error::InputAllocation)?;
|
||||
for value in prior.values() {
|
||||
bytes.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
self.states[2].as_mut().ok_or(Error::IdentityMismatch)?.copy_from_owned(bytes).map_err(|error| {
|
||||
self.failed.set(true);
|
||||
lease.refuse();
|
||||
driver_error(error)
|
||||
})?;
|
||||
}
|
||||
let operands = &self.operands[slot];
|
||||
let mut out = self.states[output].take().ok_or(Error::IdentityMismatch)?;
|
||||
let operation = (|| -> Result<()> {
|
||||
// Preserve the completed full readout finite boundary even on replay.
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
let native_completed = if let Some((generation, program)) = &self.native_fused {
|
||||
if *generation != lease.generation {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
let mut program = program.try_borrow_mut().map_err(|_| Error::IdentityMismatch)?;
|
||||
program.validate_context(self.context).map_err(driver_error)?;
|
||||
if !program
|
||||
.replay_state_from_products(
|
||||
self.states[input].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[0].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[1].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[2].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[3].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
&out,
|
||||
self.readout.as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
)
|
||||
.map_err(driver_error)?
|
||||
{
|
||||
return Err(Error::NonFinite);
|
||||
}
|
||||
// Preserve the actual complete state boundary and whole
|
||||
// readout domain on accepted-prefix replay.
|
||||
if !self
|
||||
.guard
|
||||
.as_mut()
|
||||
.ok_or(Error::IdentityMismatch)?
|
||||
.check_next_state(&out)
|
||||
.map_err(driver_error)?
|
||||
{
|
||||
return Err(Error::NonFinite);
|
||||
}
|
||||
let _ = download(self.readout.as_ref().ok_or(Error::IdentityMismatch)?, VALUES)?;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
#[cfg(not(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64")))]
|
||||
let native_completed = false;
|
||||
if !native_completed {
|
||||
let _ = compute_row(
|
||||
self.context,
|
||||
self.states[input].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
[
|
||||
operands[0].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[1].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[2].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[3].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
],
|
||||
&mut out,
|
||||
self.readout.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
self.guard.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
self.states[output] = Some(out);
|
||||
if let Err(primary) = operation {
|
||||
lease.refuse();
|
||||
if primary == Error::CudaExecution {
|
||||
self.failed.set(true);
|
||||
}
|
||||
return Err(primary);
|
||||
}
|
||||
state = output;
|
||||
state = self.replay_row(lease, prior, (index, row), state)?;
|
||||
}
|
||||
if cancelled() {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
self.replayed_prior(lease, prior, (last, rows.len()), state)
|
||||
}
|
||||
fn replayed_prior(
|
||||
&self,
|
||||
lease: &Rc<ActiveLease>,
|
||||
prior: &GdnPrior,
|
||||
(last, count): (&Rc<VerifiedRow>, usize),
|
||||
state: usize,
|
||||
) -> Result<GdnPrior> {
|
||||
let values = download(self.states[state].as_ref().ok_or(Error::IdentityMismatch)?, STATE_ELEMENTS)?;
|
||||
let mut history = Vec::new();
|
||||
history.try_reserve_exact(HISTORY_ELEMENTS).map_err(|_| Error::InputAllocation)?;
|
||||
history.extend_from_slice(&last.history);
|
||||
GdnPrior::from_verified_prefix(
|
||||
prior,
|
||||
ReplaySeal { lease: lease.clone(), last: last.clone(), count: rows.len() },
|
||||
ReplaySeal { lease: lease.clone(), last: last.clone(), count },
|
||||
values,
|
||||
history.into_boxed_slice(),
|
||||
)
|
||||
}
|
||||
// Replay one accepted row; returns the state owner that now holds its result.
|
||||
fn replay_row(
|
||||
&mut self,
|
||||
lease: &Rc<ActiveLease>,
|
||||
prior: &GdnPrior,
|
||||
(index, row): (usize, &Rc<VerifiedRow>),
|
||||
state: usize,
|
||||
) -> Result<usize> {
|
||||
row.validate(lease, prior.layer(), index, row.row.identity())?;
|
||||
let slot = Self::slot(prior.layer(), index)?;
|
||||
self.check_saved_row(slot, row)?;
|
||||
let input = if index == 0 { 2 } else { state };
|
||||
let output = if index == 0 { 0 } else { 1 - state };
|
||||
// Re-upload untouched actual prior once per replayed layer. The same
|
||||
// third whole owner is immutable throughout its accepted prefix.
|
||||
if index == 0 {
|
||||
self.reupload_prior(lease, prior)?;
|
||||
}
|
||||
let mut out = self.states[output].take().ok_or(Error::IdentityMismatch)?;
|
||||
let operation = self.replay_step(lease, slot, input, &mut out);
|
||||
self.states[output] = Some(out);
|
||||
if let Err(primary) = operation {
|
||||
lease.refuse();
|
||||
if primary == Error::CudaExecution {
|
||||
self.failed.set(true);
|
||||
}
|
||||
return Err(primary);
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
fn check_saved_row(&self, slot: usize, row: &Rc<VerifiedRow>) -> Result<()> {
|
||||
if self.ready[slot].as_ref().and_then(Weak::upgrade).is_none_or(|saved| !Rc::ptr_eq(&saved, row)) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn replay_step(
|
||||
&mut self,
|
||||
lease: &Rc<ActiveLease>,
|
||||
slot: usize,
|
||||
input: usize,
|
||||
out: &mut DeviceBuffer<'ctx>,
|
||||
) -> Result<()> {
|
||||
// Preserve the completed full readout finite boundary even on replay.
|
||||
if !self.replay_native(lease, slot, input, out)? {
|
||||
self.replay_composed(slot, input, out)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn replay_composed(&mut self, slot: usize, input: usize, out: &mut DeviceBuffer<'ctx>) -> Result<()> {
|
||||
let operands = &self.operands[slot];
|
||||
let _ = compute_row(
|
||||
self.context,
|
||||
self.states[input].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
[
|
||||
operands[0].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[1].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[2].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[3].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
],
|
||||
out,
|
||||
self.readout.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
self.guard.as_mut().ok_or(Error::IdentityMismatch)?,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
fn replay_native(
|
||||
&mut self,
|
||||
lease: &Rc<ActiveLease>,
|
||||
slot: usize,
|
||||
input: usize,
|
||||
out: &DeviceBuffer<'ctx>,
|
||||
) -> Result<bool> {
|
||||
let Some((generation, program)) = &self.native_fused else {
|
||||
return Ok(false);
|
||||
};
|
||||
if *generation != lease.generation {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
let mut program = program.try_borrow_mut().map_err(|_| Error::IdentityMismatch)?;
|
||||
program.validate_context(self.context).map_err(driver_error)?;
|
||||
let operands = &self.operands[slot];
|
||||
if !Self::native_replay_state(&mut program, &self.states[input], operands, (out, &self.readout))? {
|
||||
return Err(Error::NonFinite);
|
||||
}
|
||||
// Preserve the actual complete state boundary and whole
|
||||
// readout domain on accepted-prefix replay.
|
||||
Self::native_boundary(&mut self.guard, out, &self.readout)?;
|
||||
Ok(true)
|
||||
}
|
||||
#[cfg(not(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64")))]
|
||||
fn replay_native(&mut self, _: &Rc<ActiveLease>, _: usize, _: usize, _: &DeviceBuffer<'ctx>) -> Result<bool> {
|
||||
Ok(false)
|
||||
}
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
fn native_replay_state(
|
||||
program: &mut leleka_cuda_driver::GdnFusedT1<'ctx>,
|
||||
state: &Option<DeviceBuffer<'ctx>>,
|
||||
operands: &[Option<DeviceBuffer<'ctx>>; 4],
|
||||
(out, readout): (&DeviceBuffer<'ctx>, &Option<DeviceBuffer<'ctx>>),
|
||||
) -> Result<bool> {
|
||||
program
|
||||
.replay_state_from_products(
|
||||
state.as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[0].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[1].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[2].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
operands[3].as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
out,
|
||||
readout.as_ref().ok_or(Error::IdentityMismatch)?,
|
||||
)
|
||||
.map_err(driver_error)
|
||||
}
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
fn native_boundary(
|
||||
guard: &mut Option<GdnStateFiniteGuard<'ctx>>,
|
||||
out: &DeviceBuffer<'ctx>,
|
||||
readout: &Option<DeviceBuffer<'ctx>>,
|
||||
) -> Result<()> {
|
||||
if !guard.as_mut().ok_or(Error::IdentityMismatch)?.check_next_state(out).map_err(driver_error)? {
|
||||
return Err(Error::NonFinite);
|
||||
}
|
||||
let _ = download(readout.as_ref().ok_or(Error::IdentityMismatch)?, VALUES)?;
|
||||
Ok(())
|
||||
}
|
||||
pub(in crate::qwen_forward) fn close(mut self) -> Result<()> {
|
||||
self.cleanup()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user