diff --git a/architecture.json b/architecture.json index f9364c07..97034ee9 100644 --- a/architecture.json +++ b/architecture.json @@ -180,9 +180,6 @@ {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/contract.rs", "ratchet": "process-global", "reason": "the engine's one reader of the driver's serving context (cells): contract::serving_cells() is called where a model is bound (InputConstants::from_artifact) and by sizing code that has no binding; every validator takes the bound from the binding"}, {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/original_serve_residency/split_group_pcie.rs", "ratchet": "process-global", "reason": "the adaptive-tier tuning constants, derived once from the settings capture (immutable after the first read)"}, {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/original_serve_stack.rs", "ratchet": "process-global", "reason": "the epoch of the bring-up trace timestamps (DBG_T0), set once; diagnostics only"}, - {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/original_spec_controller/trace_enabled.rs", "ratchet": "process-global", "reason": "reads the serving context (cells) that the engine configures once at start-up (leleka-cuda-model::serving_context, an immutable bound afterwards); threading it through every position check is the S13/S14 injection"}, - {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/original_spec_draft.rs", "ratchet": "process-global", "reason": "reads the serving context (cells) that the engine configures once at start-up (leleka-cuda-model::serving_context, an immutable bound afterwards); threading it through every position check is the S13/S14 injection"}, - {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/original_spec_draft/prefill_records.rs", "ratchet": "process-global", "reason": "reads the serving context (cells) that the engine configures once at start-up (leleka-cuda-model::serving_context, an immutable bound afterwards); threading it through every position check is the S13/S14 injection"}, {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/qsa_index/context.rs", "ratchet": "process-global", "reason": "reads the serving context (cells) that the engine configures once at start-up (leleka-cuda-model::serving_context, an immutable bound afterwards); threading it through every position check is the S13/S14 injection"}, {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/qsa_native_resources.rs", "ratchet": "process-global", "reason": "reads the serving context (cells) that the engine configures once at start-up (leleka-cuda-model::serving_context, an immutable bound afterwards); threading it through every position check is the S13/S14 injection"}, {"crate": "leleka-engine-qwen35-cuda", "file": "src/qwen_forward/session.rs", "ratchet": "process-global", "reason": "instance-local lazy cell (std::cell::OnceCell), not a process global: the gate pattern matches the type name"}, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine.rs index a421a8ab..c096a558 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine.rs @@ -10,7 +10,7 @@ use super::contract::{Error, Result}; use super::original_spec_adaptive::AdaptiveConfig; use super::original_spec_controller::{ - NanGuard, OriginalSpecController, OriginalSpecEngine, SpecConfig, SpecError, SpecWindow, + NanGuard, OriginalSpecController, OriginalSpecEngine, SpecConfig, SpecContext, SpecError, SpecWindow, }; use super::original_spec_draft::{DraftPick, OriginalDraftEngine, OriginalFirstDraftEngine, TargetSeedRow}; use super::original_spec_policy::MAX_T; @@ -164,6 +164,8 @@ pub(crate) struct OriginalServeSettings { pub(crate) max_t: usize, pub(crate) mtp_max_t: usize, pub(crate) max_context: i64, + /// The serving cells of the model binding the settings are made for (the bound of every cell position). + pub(crate) serving_cells: i64, pub(crate) vocabulary: u32, pub(crate) suffix_min_match: usize, pub(crate) coupled: bool, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/env_tests.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/env_tests.rs index 2eed6bf0..92891e56 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/env_tests.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/env_tests.rs @@ -4,12 +4,18 @@ use super::super::original_spec_adaptive::{AdaptiveConfig, FLOOR_CEILING_LEGACY}; use super::super::original_spec_controller::NanGuard; use super::{MAX_T, OriginalServeSettings}; +use crate::qwen_forward::original_spec_controller::SpecContext; use crate::settings::MapEnv; const VOCAB: u32 = 1000; +/// The context of an unconfigured process, with the usual 4096-token ceiling. +fn ctx() -> SpecContext { + SpecContext::trained(4096) +} + fn engine(pairs: &[(&str, &str)]) -> OriginalServeSettings { - OriginalServeSettings::from_environment(4096, VOCAB, false, &MapEnv::of(pairs)) + OriginalServeSettings::from_environment(ctx(), VOCAB, false, &MapEnv::of(pairs)) } #[test] @@ -114,7 +120,7 @@ fn the_legacy_policy_lowers_the_ceiling_before_an_explicit_ceiling_overrides_it( fn arms(list: &str) -> Vec<(String, OriginalServeSettings)> { let env = MapEnv::of(&[("LELEKA_SPEC_ARMS", list)]); - OriginalServeSettings::arms_from_environment(4096, VOCAB, false, &env) + OriginalServeSettings::arms_from_environment(ctx(), VOCAB, false, &env) } fn arm_labels(list: &str) -> Vec { @@ -124,7 +130,7 @@ fn arm_labels(list: &str) -> Vec { #[test] #[rustfmt::skip] fn the_arm_list_builds_one_setting_per_known_arm() { - assert!(OriginalServeSettings::arms_from_environment(4096, VOCAB, false, &MapEnv::new()).is_empty()); + assert!(OriginalServeSettings::arms_from_environment(ctx(), VOCAB, false, &MapEnv::new()).is_empty()); assert_eq!(arm_labels("fixed3, fixed6 ,adaptive@0.5,bogus,"), ["fixed3", "fixed6", "adaptive@0.5"]); let fixed = arms("fixed3").remove(0).1; assert_eq!((fixed.max_t, fixed.mtp_max_t, fixed.suffix_min_match, fixed.adaptive), (5, 3, 3, None)); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/round_tally.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/round_tally.rs index 2e576a2b..82013f81 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/round_tally.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/round_tally.rs @@ -5,7 +5,7 @@ use super::{ ADAPTIVE_DEFAULT, AdaptiveConfig, CommittedWindow, DraftCompletion, DraftLimits, DraftPick, DraftStage, EnvSource, Error, FirstDraftSeed, FirstStage, MAX_T, NanGuard, OriginalFirstDraftEngine, OriginalServeEngine, OriginalServePort, OriginalServeSettings, OriginalSpecController, OriginalSpecEngine, Result, ServeOutcome, - ServeStop, SpecConfig, SpecError, SpecWindow, TracePort, + ServeStop, SpecConfig, SpecContext, SpecError, SpecWindow, TracePort, }; /// The produced-token and round counters of one served request, and why it stopped if a round decided that. diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/round_tally/original_serve_settings.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/round_tally/original_serve_settings.rs index a9325d3c..8bfed56c 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/round_tally/original_serve_settings.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/round_tally/original_serve_settings.rs @@ -4,17 +4,19 @@ use super::{ ADAPTIVE_DEFAULT, AdaptiveConfig, EnvSource, MAX_T, NanGuard, OriginalServeSettings, OriginalSpecController, - OriginalSpecEngine, Result, RoundTally, ServeOutcome, ServeStop, SpecConfig, SpecError, SpecWindow, serve_rounds, + OriginalSpecEngine, Result, RoundTally, ServeOutcome, ServeStop, SpecConfig, SpecContext, SpecError, SpecWindow, + serve_rounds, }; impl OriginalServeSettings { - pub(crate) fn mode17(max_context: i64, vocabulary: u32, coupled: bool) -> Self { + pub(crate) fn mode17(context: SpecContext, vocabulary: u32, coupled: bool) -> Self { const SPEC: usize = 4; const SUFFIX: usize = 3; Self { max_t: (SPEC + 2).min(MAX_T), mtp_max_t: SPEC, - max_context, + max_context: context.max_context, + serving_cells: context.serving_cells, vocabulary, suffix_min_match: SUFFIX, coupled, @@ -29,9 +31,9 @@ impl OriginalServeSettings { /// `LELEKA_SPEC_ADAPTIVE=on` (adaptive window; widest window `LELEKA_SPEC_ADAPTIVE_MAX_T`, default 8 rows, MTP /// chain up /// to `max_t - 1` drafts). Unset = mode17 exactly. - pub(crate) fn from_environment(max_context: i64, vocabulary: u32, coupled: bool, env: &impl EnvSource) -> Self { + pub(crate) fn from_environment(context: SpecContext, vocabulary: u32, coupled: bool, env: &impl EnvSource) -> Self { let num = |name: &str| env.get(name).and_then(|v| v.trim().parse::().ok()); - let mut s = Self::mode17(max_context, vocabulary, coupled); + let mut s = Self::mode17(context, vocabulary, coupled); s.nan_guard = NanGuard::from_environment(env); if let Some(spec) = num("LELEKA_SPEC") { let spec = spec.clamp(2, MAX_T); @@ -72,13 +74,13 @@ impl OriginalServeSettings { /// the /// adaptive window; the verifier is built for the widest arm. Empty when unset. pub(crate) fn arms_from_environment( - max_context: i64, + context: SpecContext, vocabulary: u32, coupled: bool, env: &impl EnvSource, ) -> Vec<(String, Self)> { let Some(list) = env.get("LELEKA_SPEC_ARMS") else { return Vec::new() }; - let base = Self::from_environment(max_context, vocabulary, coupled, env); + let base = Self::from_environment(context, vocabulary, coupled, env); let mut arms = Vec::new(); for entry in list.split(',').map(str::trim).filter(|l| !l.is_empty()) { if let Some(arm) = base.arm(entry, env) { @@ -166,7 +168,7 @@ impl OriginalServeSettings { OriginalSpecController::new( SpecConfig::new( SpecWindow { max_t: self.max_t, mtp_max_t: self.mtp_max_t }, - self.max_context, + SpecContext { max_context: self.max_context, serving_cells: self.serving_cells }, self.vocabulary, self.suffix_min_match, self.coupled, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/tests.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/tests.rs index 9b31660b..e3c3af89 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/tests.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/tests.rs @@ -263,7 +263,7 @@ fn run_request(skew: i32, max_t: usize, max_new: usize) -> (Vec, Vec OriginalServeSettings { max_t: 4, mtp_max_t: 0, max_context, + serving_cells: 262_144, vocabulary: VOCAB, suffix_min_match: 0, coupled: false, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/tests/restore_attempts_do_not_depend.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/tests/restore_attempts_do_not_depend.rs index 098dc35b..59417272 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/tests/restore_attempts_do_not_depend.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_engine/tests/restore_attempts_do_not_depend.rs @@ -7,6 +7,7 @@ use super::{ ServeStop, SpecConfig, SpecError, SpecWindow, SpeculativeRequest, VOCAB, VerifiedWindow, engine, events, idx, model, prompt, prompt_long, run_request, seed_row, serve, serve_in, }; +use crate::qwen_forward::original_spec_controller::SpecContext; #[test] fn restore_attempts_do_not_depend_on_engine_health_and_reset_draft_state() { @@ -116,7 +117,7 @@ fn controller_both_restores_run_when_the_verifier_fails() { let (f, w) = Fake::new(&p); let mut e = OriginalServeEngine::new(f, VOCAB, 4, 3).unwrap(); let mut c = OriginalSpecController::new( - SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 0 }, 4096, VOCAB, 0, false).unwrap(), + SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 0 }, 4096, VOCAB, 0, false).unwrap(), ) .unwrap(); c.begin_request(&p, 8, 0.0, &[]).unwrap(); @@ -136,7 +137,7 @@ fn preparation_drives_the_engine_through_after_verified() { let mut v = e.verify(2, &[a], &p[..2]).unwrap(); let outs = e.outputs(&v).to_vec(); e.commit_verified(&mut v, 1).unwrap(); - let prep = DraftPreparation::new(4, 3, VOCAB).unwrap(); + let prep = DraftPreparation::new(4, 3, VOCAB, 262_144).unwrap(); let batch = prep .after_verified( &mut e, @@ -167,7 +168,7 @@ fn the_adaptive_window_is_selected_by_the_explicit_switch_then_by_a_fixed_spec_t #[test] fn a_window_cap_narrows_every_bound_and_leaves_the_rest() { - let mut wide = OriginalServeSettings::mode17(4096, VOCAB, true); + let mut wide = OriginalServeSettings::mode17(SpecContext::trained(4096), VOCAB, true); wide.max_t = 8; wide.mtp_max_t = 8; wide.adaptive = Some(AdaptiveConfig::new(8, 7)); @@ -187,7 +188,7 @@ fn a_window_cap_narrows_every_bound_and_leaves_the_rest() { } // a cap above the settings changes nothing; a fixed window narrower than the cap keeps its own width assert_eq!(wide.with_window_cap(8), wide); - let fixed = OriginalServeSettings::mode17(4096, VOCAB, false); + let fixed = OriginalServeSettings::mode17(SpecContext::trained(4096), VOCAB, false); assert_eq!(fixed.with_window_cap(8), fixed); let narrow = fixed.with_window_cap(3); assert_eq!((narrow.max_t, narrow.mtp_max_t, narrow.max_drafts()), (3, 3, 2)); @@ -204,7 +205,7 @@ fn assert_controller_and_engine_accept(s: &OriginalServeSettings, cap: usize) { #[test] fn mode17_settings_follow_generate_defaults() { - let s = OriginalServeSettings::mode17(4096, VOCAB, false); + let s = OriginalServeSettings::mode17(SpecContext::trained(4096), VOCAB, false); assert_eq!((s.max_t, s.mtp_max_t, s.suffix_min_match), (6, 4, 3)); assert_eq!(s.max_drafts(), 3); // the drafter is loaded at the verify width (6), not at its own window width (4): suffix windows are 6 rows wide diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/lifecycle_fake/stack.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/lifecycle_fake/stack.rs index c57ae8e1..bf2422bd 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/lifecycle_fake/stack.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/lifecycle_fake/stack.rs @@ -11,6 +11,7 @@ use crate::qwen_forward::contract::Error; use crate::qwen_forward::original_serve_engine::{ OriginalServeEngine, OriginalServeSettings, ServeCallbacks, ServeOutcome, SpeculativeRequest, serve_request, }; +use crate::qwen_forward::original_spec_controller::SpecContext; use crate::qwen_forward::original_spec_controller::{OriginalSpecController, SpecError}; use leleka_obs::time::{Clock, ManualClock}; @@ -49,8 +50,10 @@ pub(crate) struct FakeStack { impl FakeStack { /// A stack over `dev`, with the cross-session prefix cache on a `GRID` grid when `prefix` is set. pub(crate) fn new(dev: FakeDevice, clock: ManualClock, prefix: bool) -> Self { - let settings = - OriginalServeSettings { suffix_min_match: 0, ..OriginalServeSettings::mode17(4096, VOCAB, false) }; + let settings = OriginalServeSettings { + suffix_min_match: 0, + ..OriginalServeSettings::mode17(SpecContext::trained(4096), VOCAB, false) + }; let cache = prefix.then(|| { let cfg = PrefixConfig { block_tokens: GRID, ..PrefixConfig::auto() }; PrefixServe::new(cfg, 1024, 1 << 30, "fake").with_clock(clock.clone().into()) diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/stack_phases/open_phase_steps.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/stack_phases/open_phase_steps.rs index 1dad386f..42bd0e7f 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/stack_phases/open_phase_steps.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/stack_phases/open_phase_steps.rs @@ -77,17 +77,17 @@ fn check_model_shape(binding: &ModelBinding) -> std::result::Result<(), ServeFai } /// The window policies the environment names: the first arm is live, the rest are parked with their controllers. -pub(super) fn serve_policies(config: &StackConfig) -> std::result::Result { +pub(super) fn serve_policies(config: &StackConfig, serving_cells: u64) -> std::result::Result { let env = crate::settings::process().env(); - let mut settings = OriginalServeSettings::from_environment(config.max_context, VOCABULARY, config.coupled, env); + let context = crate::qwen_forward::original_spec_controller::SpecContext::new(config.max_context, serving_cells); + let mut settings = OriginalServeSettings::from_environment(context, VOCABULARY, config.coupled, env); let mut arm_label = String::from("env"); - let arm_order: Vec = - OriginalServeSettings::arms_from_environment(config.max_context, VOCABULARY, config.coupled, env) - .into_iter() - .map(|(l, _)| l) - .collect(); + let arm_order: Vec = OriginalServeSettings::arms_from_environment(context, VOCABULARY, config.coupled, env) + .into_iter() + .map(|(l, _)| l) + .collect(); let mut parked = Vec::new(); - let arms = OriginalServeSettings::arms_from_environment(config.max_context, VOCABULARY, config.coupled, env); + let arms = OriginalServeSettings::arms_from_environment(context, VOCABULARY, config.coupled, env); let verifier_max_t = arms.iter().map(|(_, s)| s.max_t).chain([settings.max_t]).max().unwrap_or(settings.max_t); if let Some(((label, first), rest)) = arms.split_first() { settings = *first; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/stack_phases/original_serve_stack_open/assemble.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/stack_phases/original_serve_stack_open/assemble.rs index cee01510..89ac2ba9 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/stack_phases/original_serve_stack_open/assemble.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_serve_stack/stack_phases/original_serve_stack_open/assemble.rs @@ -34,7 +34,7 @@ pub(super) struct Assembly<'a, 'ctx> { /// in the order `open` computed them. pub(super) fn prepare(binding: &ModelBinding, config: &StackConfig) -> std::result::Result { let max_cells = usize::try_from(binding.input_constants().bound_cells()).map_err(|_| failure(Error::Overflow))?; - let policies = serve_policies(config)?; + let policies = serve_policies(config, binding.input_constants().bound_cells())?; let rope = RopeConstants::from_serving_context(); let blobs = ExpertBlobSizes::from_binding(binding).map_err(at("ExpertBlobSizes::from_binding"))?; let formats = expert_formats(binding).map_err(at("expert_formats"))?; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller.rs index 8d83ed9f..519f4a88 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller.rs @@ -75,10 +75,32 @@ struct WindowFailure<'a> { error: Error, attempt: &'a mut u32, } +/// The context a speculative run is bound by: the request ceiling (`--max-context`) and the serving cells of the +/// model binding it runs over (the bound of every cell position). +#[derive(Clone, Copy, Debug)] +pub(crate) struct SpecContext { + pub(crate) max_context: i64, + pub(crate) serving_cells: i64, +} +impl SpecContext { + pub(crate) const fn new(max_context: i64, serving_cells: u64) -> Self { + Self { max_context, serving_cells: serving_cells as i64 } + } + /// The request ceiling is at least one cell and no larger than the serving cells. + pub(crate) const fn is_servable(&self) -> bool { + self.max_context >= 1 && self.max_context <= self.serving_cells + } + /// The context of an unconfigured process: the 262144 trained cells. + #[cfg(test)] + pub(crate) const fn trained(max_context: i64) -> Self { + Self::new(max_context, 262_144) + } +} pub(crate) struct SpecConfig { max_t: usize, mtp_t: usize, max_context: i64, + serving_cells: i64, vocabulary: u32, suffix_min_match: usize, coupled: bool, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/finish_round_steps.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/finish_round_steps.rs index 1885f338..0f230a6d 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/finish_round_steps.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/finish_round_steps.rs @@ -173,7 +173,8 @@ impl OriginalSpecController { Ok(()) }; self.chain_clamped = clamped; - let preparation = DraftPreparation::new(self.config.max_t, chain, self.config.vocabulary); + let preparation = + DraftPreparation::new(self.config.max_t, chain, self.config.vocabulary, self.config.serving_cells); match (planned, preparation) { (Err(e), _) | (_, Err(e)) => Err(e.into()), (Ok(()), Ok(preparation)) => draft_batch(engine, round, r, (preparation, floor, self.config.coupled)), diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests.rs index c447f0a2..370828f0 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests.rs @@ -190,7 +190,7 @@ impl Fake { } fn config(max_t: usize, mtp: usize, suffix: usize) -> SpecConfig { - SpecConfig::new(SpecWindow { max_t: max_t, mtp_max_t: mtp }, 4096, VOCAB, suffix, false).unwrap() + SpecConfig::new_trained(SpecWindow { max_t: max_t, mtp_max_t: mtp }, 4096, VOCAB, suffix, false).unwrap() } fn prompt(n: usize) -> Vec { let mut v = vec![11]; @@ -247,7 +247,8 @@ fn run_to_the_end( let EndRunSetup { max_t, mtp, suffix, adaptive } = setup; let p = prompt(prompt_len); let mut e = Fake::new(&p[..prompt_len - 1]); - let mut cfg = SpecConfig::new(SpecWindow { max_t: max_t, mtp_max_t: mtp }, 64, VOCAB, suffix, false).unwrap(); + let mut cfg = + SpecConfig::new_trained(SpecWindow { max_t: max_t, mtp_max_t: mtp }, 64, VOCAB, suffix, false).unwrap(); cfg.max_context = max_context; if adaptive { cfg = cfg.with_adaptive(Some(AdaptiveConfig::new(max_t, max_t - 1))); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/adaptation_join_failure_is_reported.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/adaptation_join_failure_is_reported.rs index dd7a8672..bf02e9d1 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/adaptation_join_failure_is_reported.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/adaptation_join_failure_is_reported.rs @@ -135,7 +135,7 @@ fn check_shape(g: &Greedy, shape: Shape) { e.bad_draft = bad; e.probs = pr.to_vec(); let mut c = OriginalSpecController::new( - SpecConfig::new(SpecWindow { max_t: max_t, mtp_max_t: mtp }, 4096, VOCAB, suffix, false).unwrap(), + SpecConfig::new_trained(SpecWindow { max_t: max_t, mtp_max_t: mtp }, 4096, VOCAB, suffix, false).unwrap(), ) .unwrap(); c.begin_request(&g.p, max_new, min_p, &eos).unwrap(); @@ -232,7 +232,7 @@ impl OriginalFirstDraftEngine for First { #[test] fn draft_first_entry_is_a_separate_literal_path_and_drains() { - let prep = DraftPreparation::new(4, 3, VOCAB).unwrap(); + let prep = DraftPreparation::new(4, 3, VOCAB, 262_144).unwrap(); let (point, sampling) = (FirstSeedPoint { cell: 9, token: 5 }, DraftSampling { min_p: 0.0, coupled: false }); let mut f = First { log: Vec::new(), fail: false }; let b = prep.from_committed_seed(&mut f, &0u8, point, sampling).unwrap(); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/config_rejects_out_of_range.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/config_rejects_out_of_range.rs index f9ee0902..ba5daa09 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/config_rejects_out_of_range.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/config_rejects_out_of_range.rs @@ -9,14 +9,14 @@ use super::{ #[test] fn config_rejects_out_of_range_and_mtp_zero_means_max_t() { - assert!(SpecConfig::new(SpecWindow { max_t: 0, mtp_max_t: 0 }, 4096, VOCAB, 0, false).is_err()); - assert!(SpecConfig::new(SpecWindow { max_t: 9, mtp_max_t: 0 }, 4096, VOCAB, 0, false).is_err()); - assert!(SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 0 }, 0, VOCAB, 0, false).is_err()); - assert!(SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 0 }, 262145, VOCAB, 0, false).is_err()); - assert!(SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 0 }, 4096, 0, 0, false).is_err()); - assert_eq!(SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 0 }, 4096, VOCAB, 0, false).unwrap().mtp_t, 4); - assert_eq!(SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 9 }, 4096, VOCAB, 0, false).unwrap().mtp_t, 4); - assert_eq!(SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 2 }, 4096, VOCAB, 0, false).unwrap().mtp_t, 2); + assert!(SpecConfig::new_trained(SpecWindow { max_t: 0, mtp_max_t: 0 }, 4096, VOCAB, 0, false).is_err()); + assert!(SpecConfig::new_trained(SpecWindow { max_t: 9, mtp_max_t: 0 }, 4096, VOCAB, 0, false).is_err()); + assert!(SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 0 }, 0, VOCAB, 0, false).is_err()); + assert!(SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 0 }, 262145, VOCAB, 0, false).is_err()); + assert!(SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 0 }, 4096, 0, 0, false).is_err()); + assert_eq!(SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 0 }, 4096, VOCAB, 0, false).unwrap().mtp_t, 4); + assert_eq!(SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 9 }, 4096, VOCAB, 0, false).unwrap().mtp_t, 4); + assert_eq!(SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 2 }, 4096, VOCAB, 0, false).unwrap().mtp_t, 2); } #[test] @@ -180,7 +180,7 @@ fn max_new_ends_inside_a_window_without_overshooting_output() { fn a_window_near_the_context_end_takes_only_the_rows_that_fit() { let p = prompt(4); let mut e = Fake::new(&p[..3]); - let mut cfg = SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 4 }, 6, VOCAB, 0, false).unwrap(); + let mut cfg = SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 4 }, 6, VOCAB, 0, false).unwrap(); cfg.max_context = 6; let mut c = OriginalSpecController::new(cfg).unwrap(); begin(&mut c, &p, 100, &[]); @@ -236,7 +236,7 @@ fn the_chain_is_shortened_near_the_end_and_restored_after_it() { fn a_chain_clamped_by_one_request_is_restored_for_the_next() { let p = prompt(10); let mut e = Fake::new(&p[..9]); - let mut cfg = SpecConfig::new(SpecWindow { max_t: 4, mtp_max_t: 4 }, 64, VOCAB, 0, false).unwrap(); + let mut cfg = SpecConfig::new_trained(SpecWindow { max_t: 4, mtp_max_t: 4 }, 64, VOCAB, 0, false).unwrap(); cfg.max_context = 20; let mut c = OriginalSpecController::new(cfg).unwrap(); c.begin_request(&p, 1000, 0.0, &[]).unwrap(); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/golden.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/golden.rs index 0310962d..dd56f8a7 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/golden.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/tests/golden.rs @@ -9,6 +9,7 @@ use super::super::super::original_spec_adaptive::AdaptiveConfig; use super::{Fake, OriginalSpecController, SpecConfig, SpecWindow, VOCAB, drive, next, prompt}; +use crate::qwen_forward::original_spec_controller::SpecContext; use std::path::PathBuf; fn golden_path(name: &str) -> PathBuf { @@ -73,8 +74,14 @@ impl Scenario { let p = prompt(self.prompt_len); let mut e = Fake::new(&p[..self.prompt_len - 1]); e.bad_draft = self.bad_draft; - let mut cfg = - SpecConfig::new(SpecWindow { max_t: self.max_t, mtp_max_t: self.mtp }, 64, VOCAB, 0, false).unwrap(); + let mut cfg = SpecConfig::new( + SpecWindow { max_t: self.max_t, mtp_max_t: self.mtp }, + SpecContext::trained(64), + VOCAB, + 0, + false, + ) + .unwrap(); cfg.max_context = self.max_context; if self.adaptive { cfg = cfg.with_adaptive(Some(AdaptiveConfig::new(self.max_t, self.max_t - 1))); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/trace_enabled.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/trace_enabled.rs index 524ec46d..832f8176 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/trace_enabled.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_controller/trace_enabled.rs @@ -1,7 +1,9 @@ //! Settings of the speculative controller: NanGuard (non-finite window retry policy and its injection), SpecConfig //! (window, context, suffix and adaptive options), the round accessors, and the `LELEKA_SPEC_TRACE/STATS` switches. -use super::{AdaptiveConfig, CommittedRound, Error, MAX_T, NanGuard, Result, SpecConfig, SpecError, SpecWindow}; +use super::{ + AdaptiveConfig, CommittedRound, Error, MAX_T, NanGuard, Result, SpecConfig, SpecContext, SpecError, SpecWindow, +}; /// `LELEKA_SPEC_TRACE`: one `DBG spec-trace` line per timed round (width, source, accepted, round ms, the policy's /// expectation). @@ -100,18 +102,14 @@ impl NanGuard { impl SpecConfig { pub(crate) fn new( window: SpecWindow, - max_context: i64, + context: SpecContext, vocabulary: u32, suffix_min_match: usize, coupled: bool, ) -> Result { let SpecWindow { max_t, mtp_max_t } = window; - if !(1..=MAX_T).contains(&max_t) - || max_context < 1 - || max_context > crate::qwen_forward::contract::serving_cells() as i64 - || vocabulary == 0 - || vocabulary > 262144 - { + let SpecContext { max_context, serving_cells } = context; + if !(1..=MAX_T).contains(&max_t) || !context.is_servable() || vocabulary == 0 || vocabulary > 262144 { return Err(Error::InvalidDimension); } // Original --mtp-max-t<=0 means S; public usize zero carries that case. @@ -120,6 +118,7 @@ impl SpecConfig { max_t, mtp_t, max_context, + serving_cells, vocabulary, suffix_min_match, coupled, @@ -127,6 +126,17 @@ impl SpecConfig { nan_guard: NanGuard::off(), }) } + /// `new` for the context of an unconfigured process (262144 serving cells). + #[cfg(test)] + pub(crate) fn new_trained( + window: SpecWindow, + max_context: i64, + vocabulary: u32, + suffix_min_match: usize, + coupled: bool, + ) -> Result { + Self::new(window, SpecContext::trained(max_context), vocabulary, suffix_min_match, coupled) + } pub(crate) fn with_nan_guard(mut self, guard: NanGuard) -> Self { self.nan_guard = guard; self diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_draft.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_draft.rs index ac65cb46..e0a1b0e0 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_draft.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_draft.rs @@ -118,13 +118,15 @@ pub(crate) struct DraftPreparation { max_t: usize, max_drafts: usize, vocabulary: u32, + /// The serving cells of the binding: no draft cell lies at or past it. + serving_cells: i64, } impl DraftPreparation { - pub(crate) fn new(max_t: usize, max_drafts: usize, vocabulary: u32) -> Result { + pub(crate) fn new(max_t: usize, max_drafts: usize, vocabulary: u32, serving_cells: i64) -> Result { if !(1..=MAX_T).contains(&max_t) || max_drafts > max_t - 1 || vocabulary == 0 || vocabulary > 262144 { return Err(Error::InvalidDimension); } - Ok(Self { max_t, max_drafts, vocabulary }) + Ok(Self { max_t, max_drafts, vocabulary, serving_cells }) } /// Target receipts stay borrowed until catch-up, selection, chain and drain /// complete. Ordinary confidence truncation leaves the rest of IDs/probs 0. @@ -150,8 +152,7 @@ impl DraftPreparation { || outputs.len() > self.max_t || a >= outputs.len() || p < 0 - || p.checked_add(outputs.len() as i64) - .is_none_or(|end| end > (crate::qwen_forward::contract::serving_cells() as i64)) + || p.checked_add(outputs.len() as i64).is_none_or(|end| end > self.serving_cells) || !min_p.is_finite() { return Err(Error::InvalidDimension); @@ -239,9 +240,7 @@ impl DraftPreparation { ) -> Result { let FirstSeedPoint { cell, token } = point; if cell < 0 - || cell - .checked_add(self.max_t as i64) - .is_none_or(|end| end > (crate::qwen_forward::contract::serving_cells() as i64)) + || cell.checked_add(self.max_t as i64).is_none_or(|end| end > self.serving_cells) || token < 0 || token as u32 >= self.vocabulary || !sampling.min_p.is_finite() diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_draft/prefill_records.rs b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_draft/prefill_records.rs index bfda9ca1..5ed7fa67 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_draft/prefill_records.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/original_spec_draft/prefill_records.rs @@ -8,24 +8,24 @@ pub(crate) struct PrefillRecords { records: Box<[i32]>, rows: usize, cell0: i64, + /// The serving cells of the binding: no record cell lies past it. + serving_cells: i64, } impl PrefillRecords { - pub(crate) fn new(next_tokens: &[i32], cell0: i64, vocabulary: u32) -> Result { + pub(crate) fn new(next_tokens: &[i32], cell0: i64, vocabulary: u32, serving_cells: i64) -> Result { let n = next_tokens.len(); - Self::check_geometry(n, cell0, vocabulary)?; + Self::check_geometry(n, cell0, (vocabulary, serving_cells))?; if next_tokens.iter().any(|&v| v < 0 || v as u32 >= vocabulary) { return Err(Error::InvalidToken); } let rec = Self::build_records(next_tokens, cell0)?; - Ok(Self { records: rec.into_boxed_slice(), rows: n, cell0 }) + Ok(Self { records: rec.into_boxed_slice(), rows: n, cell0, serving_cells }) } - fn check_geometry(n: usize, cell0: i64, vocabulary: u32) -> Result<()> { + fn check_geometry(n: usize, cell0: i64, (vocabulary, serving_cells): (u32, i64)) -> Result<()> { if n == 0 - || (n as i64) > (crate::qwen_forward::contract::serving_cells() as i64) + || (n as i64) > serving_cells || cell0 < 0 - || cell0 - .checked_add(n as i64) - .is_none_or(|end| end > (crate::qwen_forward::contract::serving_cells() as i64)) + || cell0.checked_add(n as i64).is_none_or(|end| end > serving_cells) || vocabulary == 0 || vocabulary > 262144 { @@ -69,8 +69,8 @@ impl PrefillRecords { let PrefillSchedule { max_t, cell0, window, prompt_len } = schedule; if !(1..=MAX_T).contains(&max_t) || cell0 != self.cell0 - || !(0..=(crate::qwen_forward::contract::serving_cells() as i64)).contains(&window) - || !(0..=(crate::qwen_forward::contract::serving_cells() as i64)).contains(&prompt_len) + || !(0..=self.serving_cells).contains(&window) + || !(0..=self.serving_cells).contains(&prompt_len) { return Err(Error::InvalidDimension); }