STRA-338 params e03: forward step takes StepRequest/StepCaches/StepPurpose; layer phases take LayerSite/LayerEntry; window proof takes LoadedOwners
Files: crates/engine-qwen35-cuda/src/qwen_forward/executor_prefill_chunk/check_chunk_on_loaded_fn.rs crates/engine-qwen35-cuda/src/qwen_forward/executor_prefill_chunk/impl_qwenforward/impl_qwenforward_3/window_attention.rs
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
//! The single-token generation step loop.
|
||||
use super::*;
|
||||
use crate::qwen_forward::executor::{StepCaches, StepRequest};
|
||||
|
||||
pub(super) struct Generation<'a, 'context, 'driver, C: Clock = SystemClock> {
|
||||
pub(super) execution: EvaluationExecution,
|
||||
@@ -68,14 +69,10 @@ impl<C: Clock> Generation<'_, '_, '_, C> {
|
||||
let token = TokenId::new(u64::from(token), vocabulary).map_err(|_| invalid("native token invalid"))?;
|
||||
let position = native_position(self.binding.input_constants(), self.context_tokens, position)?;
|
||||
let forwarded = QwenForward::step_cached(
|
||||
self.binding,
|
||||
token,
|
||||
position,
|
||||
StepRequest { binding: self.binding, token, position },
|
||||
self.session,
|
||||
self.output,
|
||||
self.kar_cache.as_deref_mut(),
|
||||
self.moe_cache,
|
||||
self.dense_cache,
|
||||
StepCaches { kar: self.kar_cache.as_deref_mut(), moe: Some(self.moe_cache), dense: Some(self.dense_cache) },
|
||||
);
|
||||
let token = forwarded.map_err(|failure| self.forward_failure(failure, position))?;
|
||||
if self.cancelled() {
|
||||
@@ -104,14 +101,10 @@ impl<C: Clock> Generation<'_, '_, '_, C> {
|
||||
.map_err(|e| self.mtp_error(e))?;
|
||||
let position = native_position(self.binding.input_constants(), self.context_tokens, position)?;
|
||||
let result = QwenForward::step_for_mtp_cached(
|
||||
self.binding,
|
||||
token,
|
||||
position,
|
||||
StepRequest { binding: self.binding, token, position },
|
||||
self.session,
|
||||
self.output,
|
||||
self.kar_cache.as_deref_mut(),
|
||||
Some(self.moe_cache),
|
||||
Some(self.dense_cache),
|
||||
StepCaches { kar: self.kar_cache.as_deref_mut(), moe: Some(self.moe_cache), dense: Some(self.dense_cache) },
|
||||
);
|
||||
let product = result.map_err(|e| self.mtp_error(e))?;
|
||||
if self.cancelled() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Private sole new changed-path model gate; actual source identity/load preserved.
|
||||
use super::*;
|
||||
use crate::qwen_forward::executor::{LoadedCaches, LoadedOwners};
|
||||
use std::env;
|
||||
#[path = "combined_changed_boundary_proof/rig.rs"]
|
||||
pub(super) mod rig;
|
||||
@@ -32,16 +33,18 @@ fn pinned_single_load_layer0_and_window_boundaries() {
|
||||
let device = driver.device(0).expect("prefix CUDA device0 unavailable");
|
||||
let context = Rc::new(device.create_context(DEVICE_BUDGET).expect("bounded prefix Context unavailable"));
|
||||
let mut owners = Owners::load(&binding, context.as_ref(), (native_resident_mmq, NATIVE_MOE_REFUSAL));
|
||||
// Both helpers use genuine fresh target owners on the one admitted
|
||||
// binding/context/cache. No second source load or CUDA context is created.
|
||||
// Both helpers use genuine fresh target owners on the one admitted binding/context/cache: no second load.
|
||||
let layer0 = owners.run(&binding, &context, QwenForward::test_layer0_pair_on_loaded);
|
||||
let window = matches!(&layer0, Ok(Ok(_)))
|
||||
.then(|| owners.run(&binding, &context, QwenForward::test_window_boundaries_on_loaded));
|
||||
let window = matches!(&layer0, Ok(Ok(_))).then(|| {
|
||||
owners.run(&binding, &context, |binding, session, output, kar, moe, dense| {
|
||||
let caches = LoadedCaches { kar, moe, dense };
|
||||
QwenForward::test_window_boundaries_on_loaded(binding, session, LoadedOwners { output, caches })
|
||||
})
|
||||
});
|
||||
let mut teardown = owners.close(true);
|
||||
teardown.close_context(context);
|
||||
drop((binding, inherited));
|
||||
// Preserve the phase failure after every teardown attempt; a later
|
||||
// cleanup assertion must not obscure its original diagnostic cause.
|
||||
// Preserve the phase failure after every teardown attempt; a later cleanup must not obscure its cause.
|
||||
let report = unwrap_phase(layer0, "same-source layer0 diagnostic refused");
|
||||
let window = unwrap_phase(window.expect("window proof was not admitted"), "changed window proof refused");
|
||||
teardown.assert_clean();
|
||||
@@ -49,6 +52,11 @@ fn pinned_single_load_layer0_and_window_boundaries() {
|
||||
.write_after_cleanup(&required_path("LELEKA_STRA169_LAYER0_DIR"))
|
||||
.expect("exclusive bounded layer0 diagnostic write failed");
|
||||
window.assert_success_and_print();
|
||||
print_layer0_diagnostic_complete();
|
||||
}
|
||||
|
||||
/// The closing line of the combined proof: what the layer0 diagnostic did and did not claim.
|
||||
fn print_layer0_diagnostic_complete() {
|
||||
eprintln!(
|
||||
"layer0 diagnostic COMPLETE native-first same-four-rows actual-origins checked packed_down shared_scaled \
|
||||
final_products raw_words=256080 allocated_bytes=0 unpublished=true mtp=false head=false quality_pass=false \
|
||||
|
||||
@@ -82,6 +82,10 @@ use impl_prefillstepproduct::diagnose;
|
||||
mod impl_qwenforward;
|
||||
mod read_layer_attention_fn;
|
||||
mod step_layer_fns;
|
||||
mod step_request;
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
#[allow(unused_imports)]
|
||||
use read_layer_attention_fn::LayerEntry;
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
#[allow(unused_imports)]
|
||||
use read_layer_attention_fn::prepare_layer_attention_micro;
|
||||
@@ -91,3 +95,6 @@ use read_layer_attention_fn::read_layer_attention;
|
||||
use read_layer_attention_fn::validate_descriptors;
|
||||
#[allow(unused_imports)]
|
||||
use read_layer_attention_fn::validate_layer_plan;
|
||||
#[cfg(test)]
|
||||
pub(crate) use step_request::{LoadedCaches, LoadedOwners};
|
||||
pub(crate) use step_request::{StepCaches, StepPurpose, StepRequest};
|
||||
|
||||
+46
-97
@@ -10,7 +10,7 @@ impl QwenForward {
|
||||
pos: Position,
|
||||
session: &mut QwenSession<'_>,
|
||||
) -> Result<TokenId> {
|
||||
Self::step_inner(binding, token, pos, session, None, None)
|
||||
Self::step_inner(StepRequest { binding, token, position: pos }, session, None, None)
|
||||
}
|
||||
|
||||
/// Complete-forward entry retains the loaded output head across tokens.
|
||||
@@ -22,139 +22,90 @@ impl QwenForward {
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
) -> Result<TokenId> {
|
||||
Self::step_inner(binding, token, pos, session, Some(output), None)
|
||||
Self::step_inner(StepRequest { binding, token, position: pos }, session, Some(output), None)
|
||||
}
|
||||
|
||||
/// Explicit loaded KAR provider. Cache lifetime is borrowed from the
|
||||
/// caller's stable owner-thread Context; it is never self-referential.
|
||||
pub(crate) fn step_with_kar_ple_output(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
request: StepRequest<'_>,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
cache: &mut KarPleCache<'_>,
|
||||
) -> Result<TokenId> {
|
||||
Self::step_inner(binding, token, pos, session, Some(output), Some(cache))
|
||||
Self::step_inner(request, session, Some(output), Some(cache))
|
||||
}
|
||||
|
||||
pub(in super::super) fn step_inner(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
request: StepRequest<'_>,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: Option<&OutputStage<'_, '_>>,
|
||||
kar_cache: Option<&mut KarPleCache<'_>>,
|
||||
) -> Result<TokenId> {
|
||||
Self::step_core(binding, token, pos, session, output, kar_cache, None, None, false, false)
|
||||
let caches = StepCaches { kar: kar_cache, moe: None, dense: None };
|
||||
Self::step_core(request, session, output, caches, StepPurpose::Greedy)
|
||||
.and_then(|(predicted, _)| predicted.ok_or(Error::IdentityMismatch))
|
||||
}
|
||||
|
||||
/// Retain the pre-output four-stream residual only for explicit MTP use.
|
||||
/// Native/KAR callers above keep their original allocation and math path.
|
||||
pub(crate) fn step_for_mtp(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
request: StepRequest<'_>,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
kar_cache: Option<&mut KarPleCache<'_>>,
|
||||
) -> Result<TargetStepProduct> {
|
||||
Self::step_for_mtp_cached(binding, token, pos, session, output, kar_cache, None, None)
|
||||
}
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn step_cached(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
kar_cache: Option<&mut KarPleCache<'_>>,
|
||||
moe_cache: &mut MoeWeightCache<'_>,
|
||||
dense_cache: &DenseWeightCache<'_>,
|
||||
) -> Result<TokenId> {
|
||||
Self::step_core(
|
||||
binding,
|
||||
token,
|
||||
pos,
|
||||
session,
|
||||
Some(output),
|
||||
kar_cache,
|
||||
Some(moe_cache),
|
||||
Some(dense_cache),
|
||||
false,
|
||||
false,
|
||||
)
|
||||
.and_then(|(token, _)| token.ok_or(Error::IdentityMismatch))
|
||||
}
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn step_for_mtp_cached(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
kar_cache: Option<&mut KarPleCache<'_>>,
|
||||
moe_cache: Option<&mut MoeWeightCache<'_>>,
|
||||
dense_cache: Option<&DenseWeightCache<'_>>,
|
||||
) -> Result<TargetStepProduct> {
|
||||
let (predicted, streams) = Self::step_core(
|
||||
binding,
|
||||
token,
|
||||
pos,
|
||||
session,
|
||||
Some(output),
|
||||
kar_cache,
|
||||
moe_cache,
|
||||
dense_cache,
|
||||
true,
|
||||
false,
|
||||
)?;
|
||||
target_product(binding, token, pos, session, predicted, streams)
|
||||
let caches = StepCaches { kar: kar_cache, moe: None, dense: None };
|
||||
Self::step_for_mtp_cached(request, session, output, caches)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn prefill_for_mtp_cached(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
/// A complete greedy step through the supplied weight caches.
|
||||
pub(crate) fn step_cached(
|
||||
request: StepRequest<'_>,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
kar_cache: Option<&mut KarPleCache<'_>>,
|
||||
moe_cache: &mut MoeWeightCache<'_>,
|
||||
dense_cache: &DenseWeightCache<'_>,
|
||||
caches: StepCaches<'_, '_, '_, '_>,
|
||||
) -> Result<TokenId> {
|
||||
Self::step_core(request, session, Some(output), caches, StepPurpose::Greedy)
|
||||
.and_then(|(token, _)| token.ok_or(Error::IdentityMismatch))
|
||||
}
|
||||
|
||||
/// A complete step through the supplied weight caches that retains the MTP target product.
|
||||
pub(crate) fn step_for_mtp_cached(
|
||||
request: StepRequest<'_>,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
caches: StepCaches<'_, '_, '_, '_>,
|
||||
) -> Result<TargetStepProduct> {
|
||||
let (predicted, streams) = Self::step_core(request, session, Some(output), caches, StepPurpose::MtpTarget)?;
|
||||
target_product(request, session, predicted, streams)
|
||||
}
|
||||
|
||||
/// A known-next prefill step through the supplied weight caches that retains the MTP prefill product.
|
||||
pub(crate) fn prefill_for_mtp_cached(
|
||||
request: StepRequest<'_>,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
caches: StepCaches<'_, '_, '_, '_>,
|
||||
) -> Result<PrefillStepProduct> {
|
||||
let (predicted, streams) = Self::step_core(
|
||||
binding,
|
||||
token,
|
||||
pos,
|
||||
session,
|
||||
Some(output),
|
||||
kar_cache,
|
||||
Some(moe_cache),
|
||||
Some(dense_cache),
|
||||
true,
|
||||
true,
|
||||
)?;
|
||||
let (predicted, streams) = Self::step_core(request, session, Some(output), caches, StepPurpose::MtpPrefill)?;
|
||||
if predicted.is_some() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
prefill_product(binding, token, pos, session, streams)
|
||||
prefill_product(request, session, streams)
|
||||
}
|
||||
}
|
||||
|
||||
fn target_product(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
request: StepRequest<'_>,
|
||||
session: &QwenSession<'_>,
|
||||
predicted: Option<TokenId>,
|
||||
streams: Option<Box<[f32]>>,
|
||||
) -> Result<TargetStepProduct> {
|
||||
Ok(TargetStepProduct {
|
||||
binding: binding.identity(),
|
||||
token,
|
||||
position: pos,
|
||||
binding: request.binding.identity(),
|
||||
token: request.token,
|
||||
position: request.position,
|
||||
predicted: predicted.ok_or(Error::IdentityMismatch)?,
|
||||
streams: streams.ok_or(Error::IdentityMismatch)?,
|
||||
origin: session.target_origin()?,
|
||||
@@ -162,16 +113,14 @@ fn target_product(
|
||||
}
|
||||
|
||||
fn prefill_product(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
request: StepRequest<'_>,
|
||||
session: &QwenSession<'_>,
|
||||
streams: Option<Box<[f32]>>,
|
||||
) -> Result<PrefillStepProduct> {
|
||||
Ok(PrefillStepProduct {
|
||||
binding: binding.identity(),
|
||||
token,
|
||||
position: pos,
|
||||
binding: request.binding.identity(),
|
||||
token: request.token,
|
||||
position: request.position,
|
||||
streams: streams.ok_or(Error::IdentityMismatch)?,
|
||||
origin: session.target_origin()?,
|
||||
})
|
||||
|
||||
+7
-11
@@ -9,19 +9,15 @@ use super::*;
|
||||
impl QwenForward {
|
||||
/// A forward without its output head can never publish, so it is refused before reading any row or running any
|
||||
/// layer. The cache preparation above keeps its precedence; nothing has been staged yet.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(in super::super) fn step_core(
|
||||
binding: &ModelBinding,
|
||||
token: TokenId,
|
||||
pos: Position,
|
||||
request: StepRequest<'_>,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: Option<&OutputStage<'_, '_>>,
|
||||
kar_cache: Option<&mut KarPleCache<'_>>,
|
||||
mut moe_cache: Option<&mut MoeWeightCache<'_>>,
|
||||
dense_cache: Option<&DenseWeightCache<'_>>,
|
||||
capture_mtp: bool,
|
||||
prefill_only: bool,
|
||||
caches: StepCaches<'_, '_, '_, '_>,
|
||||
purpose: StepPurpose,
|
||||
) -> Result<StepCoreResult> {
|
||||
let StepRequest { binding, token, position: pos } = request;
|
||||
let StepCaches { kar: kar_cache, moe: mut moe_cache, dense: dense_cache } = caches;
|
||||
let mut streams = None;
|
||||
let ctx = LayerCtx { binding, token, pos, dense: dense_cache };
|
||||
let predicted = session.transact_completion(token, pos, &binding.identity(), |txn| {
|
||||
@@ -29,8 +25,8 @@ impl QwenForward {
|
||||
let output = output.ok_or(Error::Unsupported(Stage::Output))?;
|
||||
stage_input_row(binding, token, pos, kar_cache, txn)?;
|
||||
let final_residual = run_layers(&ctx, txn, layer_bound, moe_cache.as_deref_mut())?;
|
||||
let predicted = finish_step(&ctx, txn, output, &final_residual, prefill_only)?;
|
||||
if capture_mtp {
|
||||
let predicted = finish_step(&ctx, txn, output, &final_residual, purpose.is_prefill_only())?;
|
||||
if purpose.retains_streams() {
|
||||
streams = Some(capture_streams(&final_residual)?);
|
||||
}
|
||||
Ok(predicted)
|
||||
|
||||
@@ -53,18 +53,26 @@ fn read_layer_one<'cache, 'ctx>(
|
||||
GrStage::read_attention_after_ple(binding, layer, &ple, txn, cache)
|
||||
}
|
||||
|
||||
/// A layer about to be read together with the residual left by the layer before it (none for the first layer).
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) struct LayerEntry<'a> {
|
||||
pub(super) layer: LayerId,
|
||||
pub(super) previous: Option<&'a InjectedResidual>,
|
||||
}
|
||||
|
||||
/// Mint the actual row input without performing a scalar GR read. The real
|
||||
/// layer-one PLE history still advances in chronological transaction order;
|
||||
/// the caller completes GR only after collecting the genuine micro-window.
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
pub(super) fn prepare_layer_attention_micro(
|
||||
binding: &ModelBinding,
|
||||
layer: LayerId,
|
||||
previous: Option<&InjectedResidual>,
|
||||
entry: LayerEntry<'_>,
|
||||
txn: &mut QwenStepTxn,
|
||||
cache: &DenseWeightCache<'_>,
|
||||
ple_micro: Option<&mut super::super::input::PleMicroBatchReady<'_>>,
|
||||
) -> Result<super::super::gr::GrMicroInput> {
|
||||
let LayerEntry { layer, previous } = entry;
|
||||
match layer.get() {
|
||||
0 => {
|
||||
if previous.is_some() {
|
||||
@@ -72,7 +80,7 @@ pub(super) fn prepare_layer_attention_micro(
|
||||
}
|
||||
GrStage::prepare_micro_initial(binding, layer, txn)
|
||||
}
|
||||
1 => prepare_layer_one_micro(binding, layer, previous, txn, cache, ple_micro),
|
||||
1 => prepare_layer_one_micro(binding, entry, txn, cache, ple_micro),
|
||||
_ => GrStage::prepare_micro_after_ffn(binding, layer, previous.ok_or(Error::IdentityMismatch)?, txn),
|
||||
}
|
||||
}
|
||||
@@ -80,12 +88,12 @@ pub(super) fn prepare_layer_attention_micro(
|
||||
#[cfg(all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
fn prepare_layer_one_micro(
|
||||
binding: &ModelBinding,
|
||||
layer: LayerId,
|
||||
previous: Option<&InjectedResidual>,
|
||||
entry: LayerEntry<'_>,
|
||||
txn: &mut QwenStepTxn,
|
||||
cache: &DenseWeightCache<'_>,
|
||||
ple_micro: Option<&mut super::super::input::PleMicroBatchReady<'_>>,
|
||||
) -> Result<super::super::gr::GrMicroInput> {
|
||||
let LayerEntry { layer, previous } = entry;
|
||||
let previous = previous.ok_or(Error::IdentityMismatch)?;
|
||||
let prior = txn.ple_prior()?;
|
||||
let mut ple = diagnose(
|
||||
|
||||
@@ -108,17 +108,14 @@ impl Run<'_, '_, '_> {
|
||||
baseline.try_reserve_exact(TOKENS.len()).expect("baseline retention allocation failed");
|
||||
self.output.test_reset_replay_record(true).expect("logit observer refused");
|
||||
for (index, token) in TOKENS.into_iter().enumerate() {
|
||||
let greedy = QwenForward::step_cached(
|
||||
self.binding,
|
||||
TokenId::new(token, self.fixed.vocabulary).expect("fixed token refused"),
|
||||
Position::new(index as u64, self.fixed.context_length).expect("fixed position refused"),
|
||||
session,
|
||||
self.output,
|
||||
Some(&mut *cache),
|
||||
moe_cache,
|
||||
dense_cache,
|
||||
)
|
||||
.expect("cached baseline full forward refused");
|
||||
let request = StepRequest {
|
||||
binding: self.binding,
|
||||
token: TokenId::new(token, self.fixed.vocabulary).expect("fixed token refused"),
|
||||
position: Position::new(index as u64, self.fixed.context_length).expect("fixed position refused"),
|
||||
};
|
||||
let caches = StepCaches { kar: Some(&mut *cache), moe: Some(&mut *moe_cache), dense: Some(dense_cache) };
|
||||
let greedy = QwenForward::step_cached(request, session, self.output, caches)
|
||||
.expect("cached baseline full forward refused");
|
||||
baseline.push(capture_committed(self.binding, session, self.output, greedy, index));
|
||||
}
|
||||
baseline
|
||||
@@ -131,15 +128,13 @@ impl Run<'_, '_, '_> {
|
||||
cache: &mut KarPleCache<'_>,
|
||||
expected: &CapturedStep,
|
||||
) {
|
||||
let greedy = QwenForward::step_with_kar_ple_output(
|
||||
self.binding,
|
||||
TokenId::new(TOKENS[index], self.fixed.vocabulary).expect("fixed replay token refused"),
|
||||
Position::new(index as u64, self.fixed.context_length).expect("fixed replay position refused"),
|
||||
session,
|
||||
self.output,
|
||||
cache,
|
||||
)
|
||||
.expect("legacy uncached full forward refused");
|
||||
let request = StepRequest {
|
||||
binding: self.binding,
|
||||
token: TokenId::new(TOKENS[index], self.fixed.vocabulary).expect("fixed replay token refused"),
|
||||
position: Position::new(index as u64, self.fixed.context_length).expect("fixed replay position refused"),
|
||||
};
|
||||
let greedy = QwenForward::step_with_kar_ple_output(request, session, self.output, cache)
|
||||
.expect("legacy uncached full forward refused");
|
||||
let actual = capture_committed(self.binding, session, self.output, greedy, index);
|
||||
assert!(actual.greedy == expected.greedy, "replay greedy mismatch at step {index}");
|
||||
assert!(actual.logits.as_ref() == expected.logits.as_ref(), "full logit word mismatch at step {index}");
|
||||
@@ -204,10 +199,9 @@ pub(in crate::qwen_forward) fn check_on_loaded(
|
||||
binding: &ModelBinding,
|
||||
session: &mut QwenSession<'_>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
cache: &mut KarPleCache<'_>,
|
||||
moe_cache: &mut MoeWeightCache<'_>,
|
||||
dense_cache: &DenseWeightCache<'_>,
|
||||
caches: LoadedCaches<'_, '_, '_, '_>,
|
||||
) {
|
||||
let LoadedCaches { kar: cache, moe: moe_cache, dense: dense_cache } = caches;
|
||||
let charged = assert_topology(binding);
|
||||
let run = Run { binding, output, fixed: fixed_dimensions(binding) };
|
||||
let fresh = session.test_reset_replay_snapshot(binding).expect("fresh session snapshot refused");
|
||||
@@ -305,7 +299,8 @@ fn pinned_kar_cached_vs_legacy_same_session_reset_replays_all_logits_and_numeric
|
||||
let dense_cache =
|
||||
DenseWeightCache::new(&binding, context.as_ref()).expect("actor-owned dense weight cache load failed");
|
||||
let mut session = QwenSession::for_binding_on_shared_device(&binding, Rc::clone(&context));
|
||||
check_on_loaded(&binding, &mut session, &output, &mut cache, &mut moe_cache, &dense_cache);
|
||||
let caches = LoadedCaches { kar: &mut cache, moe: &mut moe_cache, dense: &dense_cache };
|
||||
check_on_loaded(&binding, &mut session, &output, caches);
|
||||
drop(session);
|
||||
let (moe_closed, dense_closed) = close_owners(moe_cache, dense_cache, cache, output);
|
||||
assert_teardown_clean(&close_context(moe_closed.is_ok(), dense_closed.is_ok(), context));
|
||||
|
||||
@@ -16,6 +16,13 @@ pub(super) struct LayerCtx<'a, 'ctx> {
|
||||
pub(super) dense: Option<&'a DenseWeightCache<'ctx>>,
|
||||
}
|
||||
|
||||
/// The layer one phase runs: its checked id and its kind (GDN or QSA).
|
||||
#[derive(Clone, Copy)]
|
||||
struct LayerSite {
|
||||
layer: LayerId,
|
||||
kind: BoundLayerKind,
|
||||
}
|
||||
|
||||
/// Validate the complete producer-bound descriptor sequence before touching any model rows or invoking a stage.
|
||||
pub(super) fn prepare_step_caches(
|
||||
binding: &ModelBinding,
|
||||
@@ -100,7 +107,8 @@ pub(super) fn run_layers(
|
||||
eprintln!("integration: starting layer {}", descriptor.index());
|
||||
let kind = descriptor.kind();
|
||||
let index = descriptor.index();
|
||||
let completed = run_layer(ctx, txn, index, kind, layer_bound, previous.as_ref(), moe_cache.as_deref_mut())?;
|
||||
let site = LayerSite { layer: LayerId::new(u64::from(index), layer_bound)?, kind };
|
||||
let completed = run_layer(ctx, txn, site, previous.as_ref(), moe_cache.as_deref_mut())?;
|
||||
txn.mark_layer_complete(&completed)?;
|
||||
previous = Some(completed);
|
||||
}
|
||||
@@ -111,13 +119,11 @@ pub(super) fn run_layers(
|
||||
fn run_layer(
|
||||
ctx: &LayerCtx<'_, '_>,
|
||||
txn: &mut QwenStepTxn,
|
||||
descriptor_index: u32,
|
||||
kind: BoundLayerKind,
|
||||
layer_bound: Dimension,
|
||||
site: LayerSite,
|
||||
previous: Option<&InjectedResidual>,
|
||||
moe_cache: Option<&mut MoeWeightCache<'_>>,
|
||||
) -> Result<InjectedResidual> {
|
||||
let layer = LayerId::new(u64::from(descriptor_index), layer_bound)?;
|
||||
let LayerSite { layer, kind } = site;
|
||||
#[cfg(all(test, any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))]
|
||||
if oracle_capture::enabled() {
|
||||
oracle_capture::begin(ctx.pos.get(), layer.get() as i32)?;
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
//! The inputs of one forward step, grouped by meaning: what is forwarded, which caches serve it, what it is for.
|
||||
|
||||
#[cfg(test)]
|
||||
use super::OutputStage;
|
||||
use super::{DenseWeightCache, KarPleCache, ModelBinding, MoeWeightCache, Position, TokenId};
|
||||
|
||||
/// What one forward step runs: the model binding and the token at its position.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct StepRequest<'a> {
|
||||
pub(crate) binding: &'a ModelBinding,
|
||||
pub(crate) token: TokenId,
|
||||
pub(crate) position: Position,
|
||||
}
|
||||
|
||||
/// The weight caches one forward step reads and fills. An absent cache selects the uncached path of that provider.
|
||||
pub(crate) struct StepCaches<'a, 'kar, 'moe, 'dense> {
|
||||
pub(crate) kar: Option<&'a mut KarPleCache<'kar>>,
|
||||
pub(crate) moe: Option<&'a mut MoeWeightCache<'moe>>,
|
||||
pub(crate) dense: Option<&'a DenseWeightCache<'dense>>,
|
||||
}
|
||||
|
||||
/// What a forward step is for: it decides whether the pre-output streams are retained and the output head is read.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum StepPurpose {
|
||||
/// A complete greedy step: the predicted token is published, nothing is retained for MTP.
|
||||
Greedy,
|
||||
/// A complete step whose pre-output four-stream residual is retained as the MTP target product.
|
||||
MtpTarget,
|
||||
/// A known-next prefill step: the streams are retained and no token is predicted.
|
||||
MtpPrefill,
|
||||
}
|
||||
|
||||
impl StepPurpose {
|
||||
/// Whether the four-stream residual is copied out for the MTP draft.
|
||||
pub(crate) fn retains_streams(self) -> bool {
|
||||
self != Self::Greedy
|
||||
}
|
||||
|
||||
/// Whether the step stops before the output head.
|
||||
pub(crate) fn is_prefill_only(self) -> bool {
|
||||
self == Self::MtpPrefill
|
||||
}
|
||||
}
|
||||
|
||||
/// The three weight caches a test borrows from its loaded owners; unlike `StepCaches` every cache is present.
|
||||
#[cfg(test)]
|
||||
pub(crate) struct LoadedCaches<'a, 'kar, 'moe, 'dense> {
|
||||
pub(crate) kar: &'a mut KarPleCache<'kar>,
|
||||
pub(crate) moe: &'a mut MoeWeightCache<'moe>,
|
||||
pub(crate) dense: &'a DenseWeightCache<'dense>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl<'kar, 'moe, 'dense> LoadedCaches<'_, 'kar, 'moe, 'dense> {
|
||||
/// The same caches as the optional set one forward step takes, reborrowed for one step.
|
||||
pub(crate) fn step_caches(&mut self) -> StepCaches<'_, 'kar, 'moe, 'dense> {
|
||||
StepCaches { kar: Some(&mut *self.kar), moe: Some(&mut *self.moe), dense: Some(self.dense) }
|
||||
}
|
||||
}
|
||||
|
||||
/// The output head and the weight caches a loaded proof borrows from the owners that outlive it.
|
||||
#[cfg(test)]
|
||||
pub(crate) struct LoadedOwners<'a, 'out, 'driver, 'kar, 'moe, 'dense> {
|
||||
pub(crate) output: &'a OutputStage<'out, 'driver>,
|
||||
pub(crate) caches: LoadedCaches<'a, 'kar, 'moe, 'dense>,
|
||||
}
|
||||
@@ -115,33 +115,19 @@ impl MmqRun<'_, '_, '_, '_, '_, '_> {
|
||||
pub(super) fn warm_scalar_prefix(&mut self, scalar_draft: &mut MtpDecoder) {
|
||||
self.output.test_reset_replay_record(true).expect("head observer refused");
|
||||
for index in 0..3 {
|
||||
let source = QwenForward::prefill_for_mtp_cached(
|
||||
self.binding,
|
||||
self.ids[index],
|
||||
pos(index as u64),
|
||||
self.session,
|
||||
self.output,
|
||||
Some(&mut *self.kar),
|
||||
self.moe,
|
||||
self.dense,
|
||||
)
|
||||
.expect("scalar known-next target refused");
|
||||
let request = StepRequest { binding: self.binding, token: self.ids[index], position: pos(index as u64) };
|
||||
let caches = StepCaches { kar: Some(&mut *self.kar), moe: Some(&mut *self.moe), dense: Some(self.dense) };
|
||||
let source = QwenForward::prefill_for_mtp_cached(request, self.session, self.output, caches)
|
||||
.expect("scalar known-next target refused");
|
||||
scalar_draft
|
||||
.warm_prefill(self.binding, self.session, &source, self.ids[index + 1], self.moe)
|
||||
.expect("scalar known-next warm refused");
|
||||
}
|
||||
}
|
||||
pub(super) fn mtp_step(&mut self, token: TokenId, position: u64) -> Result<TargetStepProduct> {
|
||||
QwenForward::step_for_mtp_cached(
|
||||
self.binding,
|
||||
token,
|
||||
pos(position),
|
||||
self.session,
|
||||
self.output,
|
||||
Some(&mut *self.kar),
|
||||
Some(&mut *self.moe),
|
||||
Some(self.dense),
|
||||
)
|
||||
let request = StepRequest { binding: self.binding, token, position: pos(position) };
|
||||
let caches = StepCaches { kar: Some(&mut *self.kar), moe: Some(&mut *self.moe), dense: Some(self.dense) };
|
||||
QwenForward::step_for_mtp_cached(request, self.session, self.output, caches)
|
||||
}
|
||||
pub(super) fn scalar_final_step(
|
||||
&mut self,
|
||||
|
||||
+7
-17
@@ -5,7 +5,7 @@
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use crate::qwen_forward::executor::{PrefillStepProduct, TargetStepProduct};
|
||||
use crate::qwen_forward::executor::{PrefillStepProduct, StepCaches, StepRequest, TargetStepProduct};
|
||||
use crate::qwen_forward::mtp::MtpChunkNumericSnapshot;
|
||||
use crate::qwen_forward::mtp_decode::MtpDecoder;
|
||||
use crate::qwen_forward::session::ResetReplaySnapshot;
|
||||
@@ -65,25 +65,15 @@ impl Rig<'_, '_, '_, '_, '_, '_> {
|
||||
|
||||
fn prefill(&mut self, ids: &Ids, (token, n): (TokenId, u64), what: &str) -> PrefillStepProduct {
|
||||
let position = ids.position(n);
|
||||
let kar = Some(&mut *self.kar);
|
||||
QwenForward::prefill_for_mtp_cached(
|
||||
self.binding,
|
||||
token,
|
||||
position,
|
||||
self.session,
|
||||
self.output,
|
||||
kar,
|
||||
self.moe,
|
||||
self.dense,
|
||||
)
|
||||
.expect(what)
|
||||
let request = StepRequest { binding: self.binding, token, position };
|
||||
let caches = StepCaches { kar: Some(&mut *self.kar), moe: Some(&mut *self.moe), dense: Some(self.dense) };
|
||||
QwenForward::prefill_for_mtp_cached(request, self.session, self.output, caches).expect(what)
|
||||
}
|
||||
|
||||
fn step_final(&mut self, ids: &Ids) -> TargetStepProduct {
|
||||
let (kar, moe) = (Some(&mut *self.kar), Some(&mut *self.moe));
|
||||
let position = ids.position(3);
|
||||
let (binding, output, dense) = (self.binding, self.output, Some(self.dense));
|
||||
QwenForward::step_for_mtp_cached(binding, ids.ids[3], position, self.session, output, kar, moe, dense)
|
||||
let caches = StepCaches { kar: Some(&mut *self.kar), moe: Some(&mut *self.moe), dense: Some(self.dense) };
|
||||
let request = StepRequest { binding: self.binding, token: ids.ids[3], position: ids.position(3) };
|
||||
QwenForward::step_for_mtp_cached(request, self.session, self.output, caches)
|
||||
.expect("scalar final target refused")
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -91,7 +91,8 @@ impl QwenForward {
|
||||
return Err(Error::Unsupported(Stage::Executor));
|
||||
}
|
||||
let prior = previous[index].as_ref();
|
||||
prepare_layer_attention_micro(env.binding, layer, prior, txn, env.dense, ple_micro.as_deref_mut())
|
||||
let entry = LayerEntry { layer, previous: prior };
|
||||
prepare_layer_attention_micro(env.binding, entry, txn, env.dense, ple_micro.as_deref_mut())
|
||||
},
|
||||
|inputs| {
|
||||
let (bank, rows) = Self::read_micro_bank(env, inputs)?;
|
||||
|
||||
@@ -97,13 +97,11 @@ impl QwenForward {
|
||||
pub(crate) fn test_window_boundaries_on_loaded<'driver>(
|
||||
binding: &ModelBinding,
|
||||
session: &mut QwenSession<'driver>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
kar: &mut KarPleCache<'_>,
|
||||
moe: &mut MoeWeightCache<'driver>,
|
||||
dense: &DenseWeightCache<'_>,
|
||||
owners: LoadedOwners<'_, '_, '_, '_, 'driver, '_>,
|
||||
) -> Result<WindowProofReport> {
|
||||
let output = owners.output;
|
||||
// Early errors still return to the root-owned cleanup envelope.
|
||||
let result = run(binding, session, output, kar, moe, dense);
|
||||
let result = run(binding, session, owners);
|
||||
if result.is_ok() {
|
||||
output.test_reset_replay_record(false)?;
|
||||
}
|
||||
@@ -133,12 +131,9 @@ use proof::{CancelReport, CandidateReport, Proof};
|
||||
fn run<'driver>(
|
||||
binding: &ModelBinding,
|
||||
session: &mut QwenSession<'driver>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
kar: &mut KarPleCache<'_>,
|
||||
moe: &mut MoeWeightCache<'driver>,
|
||||
dense: &DenseWeightCache<'_>,
|
||||
owners: LoadedOwners<'_, '_, '_, '_, 'driver, '_>,
|
||||
) -> Result<WindowProofReport> {
|
||||
let mut proof = Proof::open(binding, session, output, kar, moe, dense)?;
|
||||
let mut proof = Proof::open(binding, session, owners)?;
|
||||
// A: unchanged actual Native4 target/draft path FIRST. The new candidate
|
||||
// changes only the sealed window size; no retrospective baseline policy.
|
||||
let baseline = proof.baseline()?;
|
||||
|
||||
@@ -56,31 +56,33 @@ fn count_differences<A, B>(a: &[A], b: &[B], same: impl Fn(&A, &B) -> bool) -> u
|
||||
if a.len() == b.len() { a.iter().zip(b).filter(|(x, y)| !same(x, y)).count() } else { usize::MAX }
|
||||
}
|
||||
|
||||
/// The rows of one windowed prefill chunk: the model they run on, their tokens and the known next token.
|
||||
#[derive(Clone, Copy)]
|
||||
struct ChunkRequest<'a> {
|
||||
binding: &'a ModelBinding,
|
||||
ids: &'a [TokenId],
|
||||
known_next: Option<TokenId>,
|
||||
}
|
||||
|
||||
/// The windowed prefill transaction under the experimental retained-GR policy. The transaction borrows only the
|
||||
/// session; every other owner is reborrowed for this call alone.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn prepare_txn<'s, 'driver>(
|
||||
binding: &ModelBinding,
|
||||
request: ChunkRequest<'_>,
|
||||
session: &'s mut QwenSession<'driver>,
|
||||
output: &OutputStage<'_, '_>,
|
||||
kar: &mut KarPleCache<'_>,
|
||||
moe: &mut MoeWeightCache<'driver>,
|
||||
dense: &DenseWeightCache<'_>,
|
||||
ids: &[TokenId],
|
||||
known_next: Option<TokenId>,
|
||||
owners: &mut LoadedOwners<'_, '_, '_, '_, 'driver, '_>,
|
||||
boundary: BoundaryPlan,
|
||||
cancelled: impl FnMut() -> bool,
|
||||
) -> Result<QwenChunkTxn<'s, 'driver>> {
|
||||
let policy = ChunkMoePolicy::ExperimentalRetainedGr;
|
||||
QwenForward::prepare_prefill_chunk_with_policy_windowed(
|
||||
binding,
|
||||
ids,
|
||||
known_next,
|
||||
request.binding,
|
||||
request.ids,
|
||||
request.known_next,
|
||||
session,
|
||||
output,
|
||||
Some(kar),
|
||||
moe,
|
||||
dense,
|
||||
owners.output,
|
||||
Some(&mut *owners.caches.kar),
|
||||
&mut *owners.caches.moe,
|
||||
owners.caches.dense,
|
||||
HOST_BUDGET,
|
||||
cancelled,
|
||||
policy,
|
||||
@@ -93,10 +95,7 @@ fn prepare_txn<'s, 'driver>(
|
||||
pub(super) struct Proof<'a, 'driver, 'k, 'o, 'p, 'd> {
|
||||
pub(super) binding: &'a ModelBinding,
|
||||
pub(super) session: &'a mut QwenSession<'driver>,
|
||||
pub(super) output: &'a OutputStage<'o, 'p>,
|
||||
pub(super) kar: &'a mut KarPleCache<'k>,
|
||||
pub(super) moe: &'a mut MoeWeightCache<'driver>,
|
||||
pub(super) dense: &'a DenseWeightCache<'d>,
|
||||
pub(super) owners: LoadedOwners<'a, 'o, 'p, 'k, 'driver, 'd>,
|
||||
pub(super) vocab: Dimension,
|
||||
pub(super) bounds: Dimension,
|
||||
pub(super) ids: Vec<TokenId>,
|
||||
@@ -109,10 +108,7 @@ impl<'a, 'driver, 'k, 'o, 'p, 'd> Proof<'a, 'driver, 'k, 'o, 'p, 'd> {
|
||||
pub(super) fn open(
|
||||
binding: &'a ModelBinding,
|
||||
session: &'a mut QwenSession<'driver>,
|
||||
output: &'a OutputStage<'o, 'p>,
|
||||
kar: &'a mut KarPleCache<'k>,
|
||||
moe: &'a mut MoeWeightCache<'driver>,
|
||||
dense: &'a DenseWeightCache<'d>,
|
||||
owners: LoadedOwners<'a, 'o, 'p, 'k, 'driver, 'd>,
|
||||
) -> Result<Self> {
|
||||
let vocab = Dimension::new(248320)?;
|
||||
let bounds = crate::qwen_forward::contract::position_dimension();
|
||||
@@ -121,8 +117,8 @@ impl<'a, 'driver, 'k, 'o, 'p, 'd> Proof<'a, 'driver, 'k, 'o, 'p, 'd> {
|
||||
if !session.test_reset_replay_snapshot(binding)?.has_prefix(0, [-1, -1]) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
output.test_reset_replay_record(true)?;
|
||||
Ok(Self { binding, session, output, kar, moe, dense, vocab, bounds, ids, next, follow })
|
||||
owners.output.test_reset_replay_record(true)?;
|
||||
Ok(Self { binding, session, owners, vocab, bounds, ids, next, follow })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +138,7 @@ impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> {
|
||||
let product = self.baseline_prefill(&mut draft)?;
|
||||
let (logits, greedy, bits) = self.baseline_outputs(&product)?;
|
||||
let (target, mtp) = self.snapshots(&draft)?;
|
||||
draft.warm_retained(self.binding, self.session, &product, self.next, self.moe)?;
|
||||
draft.warm_retained(self.binding, self.session, &product, self.next, self.owners.caches.moe)?;
|
||||
self.continuation(&mut draft)?;
|
||||
let (continued_target, continued_mtp) = self.snapshots(&draft)?;
|
||||
drop(product);
|
||||
@@ -158,10 +154,10 @@ impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> {
|
||||
None,
|
||||
self.session,
|
||||
draft,
|
||||
self.output,
|
||||
Some(&mut *self.kar),
|
||||
self.moe,
|
||||
self.dense,
|
||||
self.owners.output,
|
||||
Some(&mut *self.owners.caches.kar),
|
||||
self.owners.caches.moe,
|
||||
self.owners.caches.dense,
|
||||
HOST_BUDGET,
|
||||
|| false,
|
||||
)?
|
||||
@@ -169,7 +165,7 @@ impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> {
|
||||
}
|
||||
|
||||
fn baseline_outputs(&self, product: &TargetStepProduct) -> Result<(Box<[u32]>, TokenId, Vec<u32>)> {
|
||||
let logits = self.output.test_reset_replay_take_logits()?;
|
||||
let logits = self.owners.output.test_reset_replay_take_logits()?;
|
||||
let greedy = product.predicted(self.session)?;
|
||||
let stream = product.streams(self.binding, self.ids[3], Position::new(3, self.bounds)?, self.session)?;
|
||||
let mut bits = Vec::new();
|
||||
@@ -181,24 +177,17 @@ impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> {
|
||||
/// The continuation row after the window: one more actual prefill step, then the draft warm.
|
||||
fn continuation(&mut self, draft: &mut MtpDecoder) -> Result<()> {
|
||||
let position = Position::new(4, self.bounds)?;
|
||||
let step = QwenForward::prefill_for_mtp_cached(
|
||||
self.binding,
|
||||
self.next,
|
||||
position,
|
||||
self.session,
|
||||
self.output,
|
||||
Some(&mut *self.kar),
|
||||
self.moe,
|
||||
self.dense,
|
||||
)?;
|
||||
draft.warm_prefill(self.binding, self.session, &step, self.follow, self.moe)?;
|
||||
let request = StepRequest { binding: self.binding, token: self.next, position };
|
||||
let caches = self.owners.caches.step_caches();
|
||||
let step = QwenForward::prefill_for_mtp_cached(request, self.session, self.owners.output, caches)?;
|
||||
draft.warm_prefill(self.binding, self.session, &step, self.follow, self.owners.caches.moe)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn candidate(&mut self, base: &Baseline) -> Result<CandidateReport> {
|
||||
let mut draft = MtpDecoder::new(self.binding)?;
|
||||
let (product, full_journal) = self.stage_full_micro(&mut draft)?;
|
||||
let logits = self.output.test_reset_replay_take_logits()?;
|
||||
let logits = self.owners.output.test_reset_replay_take_logits()?;
|
||||
let position = Position::new(3, self.bounds)?;
|
||||
let stream = product.streams(self.binding, self.ids[3], position, self.session)?;
|
||||
let logit_differences = count_differences(&base.logits, &logits, |a, b| a == b);
|
||||
@@ -229,19 +218,19 @@ impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> {
|
||||
product: &TargetStepProduct,
|
||||
) -> Result<(bool, bool)> {
|
||||
draft.validate_resume(self.binding, self.session, product, &[0, 1, 0, 1])?;
|
||||
draft.warm_retained(self.binding, self.session, product, self.next, self.moe)?;
|
||||
draft.warm_retained(self.binding, self.session, product, self.next, self.owners.caches.moe)?;
|
||||
self.continuation(draft)?;
|
||||
let target_equal = base.continued_target.numeric_eq(&self.snapshot()?);
|
||||
Ok((target_equal, draft.test_chunk_eq(&base.continued_mtp)))
|
||||
}
|
||||
|
||||
fn stage_full_micro(&mut self, draft: &mut MtpDecoder) -> Result<(TargetStepProduct, Journal)> {
|
||||
let (binding, output, dense) = (self.binding, self.output, self.dense);
|
||||
let binding = self.binding;
|
||||
let plan = BoundaryPlan::FullMicro2;
|
||||
let mut txn =
|
||||
prepare_txn(binding, self.session, output, self.kar, self.moe, dense, &self.ids, None, plan, || false)?;
|
||||
let request = ChunkRequest { binding, ids: &self.ids, known_next: None };
|
||||
let mut txn = prepare_txn(request, self.session, &mut self.owners, plan, || false)?;
|
||||
let journal = owned_journal(&txn)?;
|
||||
let ready = draft.stage_prefill_chunk(binding, &mut txn, self.moe, || false)?;
|
||||
let ready = draft.stage_prefill_chunk(binding, &mut txn, self.owners.caches.moe, || false)?;
|
||||
let source = txn.publish_joint(ready)?.into_target_product().ok_or(Error::IdentityMismatch)?;
|
||||
Ok((source, journal))
|
||||
}
|
||||
@@ -277,13 +266,14 @@ impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> {
|
||||
}
|
||||
|
||||
fn stage_first_layer(&mut self, ids: &[TokenId], draft: &mut MtpDecoder) -> Result<FirstLayer> {
|
||||
let (binding, output, dense, next) = (self.binding, self.output, self.dense, Some(self.next));
|
||||
let binding = self.binding;
|
||||
let plan = BoundaryPlan::FirstLayer;
|
||||
let mut txn = prepare_txn(binding, self.session, output, self.kar, self.moe, dense, ids, next, plan, || false)?;
|
||||
let request = ChunkRequest { binding, ids, known_next: Some(self.next) };
|
||||
let mut txn = prepare_txn(request, self.session, &mut self.owners, plan, || false)?;
|
||||
let finite_words = txn.test_partial_words();
|
||||
let journal = owned_journal(&txn)?;
|
||||
let publication_refused = txn.test_publication_preflight_refused();
|
||||
let staged = draft.stage_prefill_chunk(binding, &mut txn, self.moe, || false);
|
||||
let staged = draft.stage_prefill_chunk(binding, &mut txn, self.owners.caches.moe, || false);
|
||||
let mtp_refused = matches!(staged, Err(Error::IdentityMismatch));
|
||||
Ok(FirstLayer { finite_words, journal, publication_refused, mtp_refused })
|
||||
}
|
||||
@@ -311,8 +301,8 @@ impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> {
|
||||
/// The caches still validate against the MTP context, which must not be poisoned.
|
||||
fn healthy(&mut self) -> Result<bool> {
|
||||
let context = self.session.mtp_context()?;
|
||||
self.moe.validate(self.binding, context)?;
|
||||
self.dense.validate(self.binding)?;
|
||||
self.owners.caches.moe.validate(self.binding, context)?;
|
||||
self.owners.caches.dense.validate(self.binding)?;
|
||||
Ok(!context.is_poisoned())
|
||||
}
|
||||
|
||||
@@ -323,10 +313,8 @@ impl<'driver> Proof<'_, 'driver, '_, '_, '_, '_> {
|
||||
observed: Rc::clone(observation),
|
||||
cancelled: Rc::clone(&cancellation),
|
||||
};
|
||||
let (binding, output, dense, next) = (self.binding, self.output, self.dense, Some(self.next));
|
||||
let (session, kar, moe) = (&mut *self.session, &mut *self.kar, &mut *self.moe);
|
||||
let prepared =
|
||||
prepare_txn(binding, session, output, kar, moe, dense, &actual_ids, next, plan, || cancellation.get());
|
||||
let request = ChunkRequest { binding: self.binding, ids: &actual_ids, known_next: Some(self.next) };
|
||||
let prepared = prepare_txn(request, self.session, &mut self.owners, plan, || cancellation.get());
|
||||
Ok(matches!(prepared, Err(Error::Unsupported(Stage::Executor))))
|
||||
}
|
||||
}
|
||||
|
||||
+4
-10
@@ -3,6 +3,7 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
use crate::qwen_forward::executor::{StepCaches, StepRequest};
|
||||
use crate::qwen_forward::mtp::MtpCheckpoint;
|
||||
|
||||
type WindowResult<T> = std::result::Result<T, MtpWindowError>;
|
||||
@@ -240,16 +241,9 @@ impl MtpDecoder {
|
||||
let position = Position::new(position, crate::qwen_forward::contract::position_dimension())?;
|
||||
let (output, dense) = (cx.output, cx.dense_cache);
|
||||
let kar = kar_cache.as_deref_mut();
|
||||
let product = QwenForward::step_for_mtp_cached(
|
||||
binding,
|
||||
expected,
|
||||
position,
|
||||
target,
|
||||
output,
|
||||
kar,
|
||||
Some(cache),
|
||||
Some(dense),
|
||||
)?;
|
||||
let request = StepRequest { binding, token: expected, position };
|
||||
let caches = StepCaches { kar, moe: Some(cache), dense: Some(dense) };
|
||||
let product = QwenForward::step_for_mtp_cached(request, target, output, caches)?;
|
||||
let next = product.predicted(target)?;
|
||||
st.latest = Some(product);
|
||||
Ok(next)
|
||||
|
||||
+4
-10
@@ -2,6 +2,7 @@
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use crate::qwen_forward::executor::{StepCaches, StepRequest};
|
||||
|
||||
pub(super) fn position_at(position: u64) -> Position {
|
||||
Position::new(position, crate::qwen_forward::contract::position_dimension()).unwrap()
|
||||
@@ -25,16 +26,9 @@ impl Shared<'_, '_, '_, '_, '_> {
|
||||
native: &mut MoeWeightCache<'ctx>,
|
||||
(token, position): (TokenId, u64),
|
||||
) -> Result<TargetStepProduct> {
|
||||
QwenForward::step_for_mtp_cached(
|
||||
self.binding,
|
||||
token,
|
||||
position_at(position),
|
||||
target,
|
||||
self.output,
|
||||
Some(&mut *self.kar),
|
||||
Some(native),
|
||||
Some(self.dense),
|
||||
)
|
||||
let request = StepRequest { binding: self.binding, token, position: position_at(position) };
|
||||
let caches = StepCaches { kar: Some(&mut *self.kar), moe: Some(native), dense: Some(self.dense) };
|
||||
QwenForward::step_for_mtp_cached(request, target, self.output, caches)
|
||||
}
|
||||
pub(super) fn window<'ctx>(
|
||||
&mut self,
|
||||
|
||||
@@ -3,7 +3,7 @@ use super::contract::{Dimension, Position, TokenId};
|
||||
use super::{
|
||||
contract::{Error, Result},
|
||||
dense_weight_cache::DenseWeightCache,
|
||||
executor::QwenForward,
|
||||
executor::{QwenForward, StepCaches, StepRequest},
|
||||
kar_ple_cache::KarPleCache,
|
||||
moe_weight_cache::MoeWeightCache,
|
||||
output::OutputStage,
|
||||
@@ -230,8 +230,10 @@ fn capture_position(
|
||||
}
|
||||
begin(position, -1)?;
|
||||
let (token, position_id) = (TokenId::new(token, vocabulary)?, Position::new(position, context)?);
|
||||
let (cache, moe, dense) = (Some(&mut *caches.cache), &mut *caches.moe, caches.dense);
|
||||
let greedy = QwenForward::step_cached(binding, token, position_id, session, output, cache, moe, dense)?;
|
||||
let request = StepRequest { binding, token, position: position_id };
|
||||
let step_caches =
|
||||
StepCaches { kar: Some(&mut *caches.cache), moe: Some(&mut *caches.moe), dense: Some(caches.dense) };
|
||||
let greedy = QwenForward::step_cached(request, session, output, step_caches)?;
|
||||
emit_logits(output, greedy.get())?;
|
||||
session.oracle_capture_committed(binding, position)?;
|
||||
finish_position()
|
||||
|
||||
Reference in New Issue
Block a user