From a70b03ce3e13386015e03208a5bfcdeef8ef0628 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sun, 8 Nov 2026 02:27:11 +0100 Subject: [PATCH] STRA-338 params e05: native penalty history takes SamplingWindow and HeadSamplerChoice Files: --- .../original_serve_host/window_parts.rs | 18 +++-- .../src/qwen_forward/output_native.rs | 2 +- .../src/qwen_forward/output_native_history.rs | 69 ++++++++++--------- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_host/window_parts.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_host/window_parts.rs index f9c5241c..a7b0972f 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_host/window_parts.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_host/window_parts.rs @@ -3,6 +3,7 @@ //! of the window in the order `run_window` ran it. use super::super::original_serve_pool::window_trace; +use super::super::output_native::{HeadSamplerChoice, SamplingWindow}; #[allow(unused_imports)] use super::*; use leleka_cuda_driver::{NativeHeadSampling, OriginalGpuStream, OriginalVerifyCpuRows, OriginalVerifyCpuSink}; @@ -92,6 +93,14 @@ impl WindowRun { fn empty() -> Self { Self { result: Err(Error::IdentityMismatch), pool_error: None, driver_failure: None, verifier_ms: 0.0 } } + /// The cells the verify step fills, borrowed from this run. + fn cells(&mut self) -> Cells<'_> { + Cells { + pool_error: &mut self.pool_error, + driver_failure: &mut self.driver_failure, + verifier_ms: &mut self.verifier_ms, + } + } } impl<'a, 'ctx> OriginalServeHost<'a, 'ctx> { @@ -111,13 +120,10 @@ impl<'a, 'ctx> OriginalServeHost<'a, 'ctx> { let (params, path, enabled) = (self.sampling.params, self.sampling.path, self.sampling.enabled()); let mut run = WindowRun::empty(); let Self { binding, session, history, pool, ledger, .. } = &mut *self; - let cells = Cells { - pool_error: &mut run.pool_error, - driver_failure: &mut run.driver_failure, - verifier_ms: &mut run.verifier_ms, - }; + let cells = run.cells(); + let window = SamplingWindow { binding, session: &**session, origin, tail: consumed, tokens: inputs }; #[rustfmt::skip] - let result = history.with_sampling(binding, &**session, origin, consumed, inputs, params, path, enabled, + let result = history.with_sampling(window, HeadSamplerChoice { params, path, enabled }, || if ledger.agrees_with(consumed) { Ok(()) } else { Err(Error::IdentityMismatch) }, |sampling| { history_span.stop(); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/output_native.rs b/crates/engine-qwen35-cuda/src/qwen_forward/output_native.rs index 987ed94a..72fde5d8 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/output_native.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/output_native.rs @@ -10,7 +10,7 @@ use leleka_cuda_driver::{ use std::rc::Rc; #[path = "output_native_history.rs"] mod history; -pub(in crate::qwen_forward) use history::OriginalNativePenaltyHistory; +pub(in crate::qwen_forward) use history::{HeadSamplerChoice, OriginalNativePenaltyHistory, SamplingWindow}; /// The exact `NativeHead::weights()` allocation borrowed by the original MTP /// drafter. This carries no host copy, row staging buffer, or replacement /// dispatch setting; `OriginalNativeOutput` remains the model-affine source. diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/output_native_history.rs b/crates/engine-qwen35-cuda/src/qwen_forward/output_native_history.rs index ccb0cdeb..ff1e520f 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/output_native_history.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/output_native_history.rs @@ -9,6 +9,24 @@ const HISTORY: usize = 4096; const ROWS: usize = 8; const BYTES: usize = HISTORY * ROWS * 4; +/// The target-step window one sampling pass authenticates: the binding and session it must belong to, the step +/// origin, the controller's consumed token tail and the tokens of the verify window. +#[derive(Clone, Copy)] +pub(in crate::qwen_forward) struct SamplingWindow<'a, 'ctx> { + pub(in crate::qwen_forward) binding: &'a ModelBinding, + pub(in crate::qwen_forward) session: &'a QwenSession<'ctx>, + pub(in crate::qwen_forward) origin: &'a TargetStepOrigin, + pub(in crate::qwen_forward) tail: &'a [i32], + pub(in crate::qwen_forward) tokens: &'a [i32], +} + +/// How the native head samples one window: the sampler parameters and path, and whether sampling is enabled. +pub(in crate::qwen_forward) struct HeadSamplerChoice { + pub(in crate::qwen_forward) params: SamplerParams, + pub(in crate::qwen_forward) path: SamplerPath, + pub(in crate::qwen_forward) enabled: bool, +} + pub(in crate::qwen_forward) struct OriginalNativePenaltyHistory<'ctx> { binding: BindingIdentity, context: &'ctx Context<'ctx>, @@ -47,22 +65,18 @@ impl<'ctx> OriginalNativePenaltyHistory<'ctx> { /// window can overwrite the borrowed history while the consumer runs. pub(in crate::qwen_forward) fn with_sampling( &mut self, - binding: &ModelBinding, - session: &QwenSession<'ctx>, - origin: &TargetStepOrigin, - tail: &[i32], - window: &[i32], - params: SamplerParams, - path: SamplerPath, - enabled: bool, + window: SamplingWindow<'_, 'ctx>, + sampler: HeadSamplerChoice, mut authenticate: impl FnMut() -> Result<()>, consume: impl FnOnce(NativeHeadSampling<'_, 'ctx>) -> Result, ) -> Result { + let SamplingWindow { session, origin, tail, tokens, .. } = window; + let HeadSamplerChoice { params, path, enabled } = sampler; origin.validate(session)?; authenticate()?; - self.admit_window(binding, session, origin, tail, window, ¶ms)?; + self.admit_window(&window, ¶ms)?; let h = if params.penalty_last_n <= 0 { 0 } else { (params.penalty_last_n as usize).min(HISTORY) }; - self.upload_penalty_rows(tail, window, h)?; + self.upload_penalty_rows(tail, tokens, h)?; let history = if h == 0 { None } else { Some(self.device.as_ref().ok_or(Error::IdentityMismatch)?) }; let position = origin.next_position(); let operation = consume(NativeHeadSampling { position, params, history, history_len: h, path, enabled }); @@ -91,39 +105,26 @@ impl<'ctx> OriginalNativePenaltyHistory<'ctx> { } } } - fn admit_window( - &mut self, - binding: &ModelBinding, - session: &QwenSession<'ctx>, - origin: &TargetStepOrigin, - tail: &[i32], - window: &[i32], - params: &SamplerParams, - ) -> Result<()> { + fn admit_window(&mut self, window: &SamplingWindow<'_, 'ctx>, params: &SamplerParams) -> Result<()> { if self.failed || self.context.is_poisoned() { return Err(Error::CudaExecution); } - self.check_identity(binding, session, origin, tail, window)?; + self.check_identity(window)?; params.validate().map_err(driver)?; let vocabulary = 248320; - if window.iter().any(|&id| id < 0 || id >= vocabulary) || !self.tail_checked.in_range(tail, vocabulary) { + if window.tokens.iter().any(|&id| id < 0 || id >= vocabulary) + || !self.tail_checked.in_range(window.tail, vocabulary) + { return Err(Error::InvalidToken); } - origin.next_position().checked_add(window.len() as u64).ok_or(Error::Overflow)?; + window.origin.next_position().checked_add(window.tokens.len() as u64).ok_or(Error::Overflow)?; Ok(()) } - fn check_identity( - &self, - binding: &ModelBinding, - session: &QwenSession<'ctx>, - origin: &TargetStepOrigin, - tail: &[i32], - window: &[i32], - ) -> Result<()> { - if !self.binding.matches(&binding.identity()) - || !std::ptr::eq(self.context, session.mtp_context()?) - || !(1..=ROWS).contains(&window.len()) - || tail.len() as u64 != origin.next_position() + fn check_identity(&self, window: &SamplingWindow<'_, 'ctx>) -> Result<()> { + if !self.binding.matches(&window.binding.identity()) + || !std::ptr::eq(self.context, window.session.mtp_context()?) + || !(1..=ROWS).contains(&window.tokens.len()) + || window.tail.len() as u64 != window.origin.next_position() { return Err(Error::IdentityMismatch); }