diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache.rs index 2a25b9a8..ffefd98a 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache.rs @@ -12,7 +12,7 @@ use leleka_cuda_driver::{ use std::cell::{Cell, RefCell}; #[path = "mtp_kv_resident.rs"] mod mtp_kv_resident; -use mtp_kv_resident::MtpKvResident; +use mtp_kv_resident::{MtpKvClaim, MtpKvResident}; #[path = "cache_lru_index.rs"] mod cache_lru_index; use cache_lru_index::{LruIndex, PreparedUpdates}; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache/impl_moeweightcache/impl_moeweightcache_4.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache/impl_moeweightcache/impl_moeweightcache_4.rs index 57c21d7f..06e453d7 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache/impl_moeweightcache/impl_moeweightcache_4.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache/impl_moeweightcache/impl_moeweightcache_4.rs @@ -145,8 +145,11 @@ impl<'ctx> MoeWeightCache<'ctx> { ) -> Result<()> { self.validate(binding, context)?; let mut slot = self.mtp_kv.try_borrow_mut().map_err(|_| Error::IdentityMismatch)?; - let result = - slot.as_mut().ok_or(Error::IdentityMismatch)?.prepare(binding, context, &self.mmq_owner, next, frame); + let result = slot.as_mut().ok_or(Error::IdentityMismatch)?.prepare( + MtpKvClaim { binding, context, owner: &self.mmq_owner }, + next, + frame, + ); if context.is_poisoned() || matches!(result, Err(Error::CudaExecution)) { self.failed.set(true); } diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/mtp_experts/impl_mtpblock.rs b/crates/engine-qwen35-cuda/src/qwen_forward/mtp_experts/impl_mtpblock.rs index 30b4f77a..d36ea296 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/mtp_experts/impl_mtpblock.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/mtp_experts/impl_mtpblock.rs @@ -45,7 +45,8 @@ impl MtpExpertStage { let context = session.mtp_context()?; preflight(context, DEVICE_PEAK)?; let (ids, weights) = route_mixed(context, binding, input, cache)?; - let output = compose_bound(context, binding, input, session, ids, weights, cache, Rc::clone(failed))?; + let call = Bound { context, binding, session, cache }; + let output = compose_bound(call, input, ids, weights, Rc::clone(failed))?; validate_values(&output, N)?; input.frame().validate(binding, session)?; Ok(MtpBlock { frame: input.frame().clone(), output }) @@ -109,12 +110,12 @@ impl<'a, 'k> Bound<'a, '_, '_, 'k> { } pub(super) fn compose( &self, - frame: &MtpFrame, input: &MtpMlpReady, weights: BoundWeights<'_, '_>, picks: ([u32; K], [f32; K]), failed: Rc>, ) -> Result> { + let frame = input.frame(); let (ids, route_weights) = picks; let BoundWeights { bank, shared } = weights; if self.cache.requires_iq3_mmq_admission() { @@ -138,23 +139,19 @@ impl<'a, 'k> Bound<'a, '_, '_, 'k> { } } } -#[allow(clippy::too_many_arguments)] pub(super) fn compose_bound( - context: &Context<'_>, - binding: &ModelBinding, + call: Bound<'_, '_, '_, '_>, input: &MtpMlpReady, - session: &QwenSession<'_>, ids: [u32; K], weights: [f32; K], - cache: &MoeWeightCache<'_>, failed: Rc>, ) -> Result> { // Only this current frame-bound invocation can supply the numeric helper. // No device product escapes to a later draft position or rollback epoch. - let call = Bound { context, binding, session, cache }; + let (binding, session) = (call.binding, call.session); let frame = input.frame().clone(); call.admit(&frame)?; - let values = call.compose(&frame, input, call.weights()?, (ids, weights), failed)?; + let values = call.compose(input, call.weights()?, (ids, weights), failed)?; frame.validate(binding, session)?; if !frame.same(input.frame()) { return Err(Error::IdentityMismatch); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/mtp_experts/route_fn.rs b/crates/engine-qwen35-cuda/src/qwen_forward/mtp_experts/route_fn.rs index f848b535..979f68e3 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/mtp_experts/route_fn.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/mtp_experts/route_fn.rs @@ -24,15 +24,21 @@ fn route_on_device( let weights: [f32; K] = download(weights, K)?.as_ref().try_into().map_err(|_| Error::InvalidDimension)?; Ok((validate_route(ids, &weights)?, weights)) } +/// The device context, model binding and expert cache the MTP routed and shared expert math runs against. +#[derive(Clone, Copy)] +pub(super) struct MtpRoutingScope<'a, 'c, 'k> { + pub(super) context: &'a Context<'c>, + pub(super) binding: &'a ModelBinding, + pub(super) cache: &'a MoeWeightCache<'k>, +} pub(super) fn compose( - context: &Context<'_>, - binding: &ModelBinding, + scope: MtpRoutingScope<'_, '_, '_>, mixed: &[f32], x: &DeviceBuffer<'_>, ids: [u32; K], weights: [f32; K], - cache: &MoeWeightCache<'_>, ) -> Result> { + let MtpRoutingScope { context, binding, cache } = scope; let parts = routed_parts(context, binding, x, ids, cache)?; let shared = shared(context, binding, mixed, x, cache)?; combine_uploaded(context, &parts, &weights, &shared) @@ -49,8 +55,9 @@ pub(super) fn routed_parts( context.mtp_quantize_scaled_2560(x, &mut q, &mut scales).map_err(moe_driver_error)?; download(&scales, 80)?; // Must inspect FP32 scales before any dot. let mut parts = reserve(K * N)?; + let scope = MtpRoutingScope { context, binding, cache }; for expert in ids { - let values = routed(context, binding, expert, &q, &scales, cache)?; + let values = routed(scope, expert, &q, &scales)?; validate_values(&values, N)?; parts.extend_from_slice(&values); } @@ -78,13 +85,12 @@ pub(super) fn combine_uploaded( Ok(result) } pub(super) fn routed( - context: &Context<'_>, - binding: &ModelBinding, + scope: MtpRoutingScope<'_, '_, '_>, expert: u32, q: &DeviceBuffer<'_>, scales: &DeviceBuffer<'_>, - cache: &MoeWeightCache<'_>, ) -> Result> { + let MtpRoutingScope { context, binding, cache } = scope; let bank = cache.mtp_bank(binding, context)?; let hidden = routed_hidden(context, bank, expert, q, scales)?; routed_down(context, bank, expert, hidden) diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/mtp_kv_resident.rs b/crates/engine-qwen35-cuda/src/qwen_forward/mtp_kv_resident.rs index 6d897e46..671b3cbb 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/mtp_kv_resident.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/mtp_kv_resident.rs @@ -8,6 +8,13 @@ use crate::model_binding::qwen4exp_input::{BindingIdentity, ModelBinding}; use leleka_cuda_driver::{Context, DeviceBuffer, MtpDenseKvRing}; use std::rc::Rc; const RING_BYTES: usize = 128 * 1024 * 1024; +/// The binding, device context and cache owner token a caller claims the mirror for; the mirror refuses any other. +#[derive(Clone, Copy)] +pub(super) struct MtpKvClaim<'a, 'c> { + pub(super) binding: &'a ModelBinding, + pub(super) context: &'a Context<'c>, + pub(super) owner: &'a Rc<()>, +} /// A cache entry is a numerical mirror, never a checkpoint or host publication. pub(super) struct MtpKvResident<'ctx> { binding: BindingIdentity, @@ -29,15 +36,8 @@ impl<'ctx> MtpKvResident<'ctx> { } Ok(Self { binding, context, owner, ring, prior: None }) } - pub(super) fn prepare( - &mut self, - binding: &ModelBinding, - context: &Context<'_>, - owner: &Rc<()>, - next: &DenseKv, - frame: &MtpFrame, - ) -> Result<()> { - self.admit(binding, context, owner)?; + pub(super) fn prepare(&mut self, claim: MtpKvClaim<'_, '_>, next: &DenseKv, frame: &MtpFrame) -> Result<()> { + self.admit(claim.binding, claim.context, claim.owner)?; next.ring_validate(frame)?; let position = u32::try_from(frame.position().get()).map_err(|_| Error::Overflow)?; if self.prior.as_ref().map(|old| next.ring_same(old, frame)).transpose()?.unwrap_or(false) {