fix(runtime): scalar draft-KV fallback checks its weights against the retained MTP owner, not the MoE cache

With --mtp-rt-dir the retained MTP weights come from the RT bank, whose buffers are not the pinned pack's MoE-cache matrices, so the pointer
identity check of record_original_scalar_draft_kv always failed (InvalidProjectionShape -> CudaExecution -> response.failed) on every
prompt that takes the short-tail scalar fallback (first request of a Codex session; a resumed prefix with a tiny tail). Found by the reference-config
Codex reruns 943c/945c, which died on it; the first 23 requests of 945c decoded at 138.8 tok/s weighted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-24 05:08:48 +02:00
co-authored by Claude Sonnet 5.5
parent 4cbda5834c
commit e5924c1099
4 changed files with 51 additions and 5 deletions
@@ -445,6 +445,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx>{
let (context,max_t)=(self.context,self.max_t);
let scalar=self.mtp_full.as_mut().ok_or(Error::IdentityMismatch)?.actor_mut(binding,context,max_t)?;
let scalar_weights=std::rc::Rc::clone(self.mtp_scalar_weights.as_ref().ok_or(Error::IdentityMismatch)?);
let retained_mtp=self.mtp_weights.clone().ok_or(Error::IdentityMismatch)?;
let draft_kv=draft.with_pending_ring(binding,&txn,|ring,prior,first_needed|{
crate::bringup_trace!("DBG[{:.0}s] draft ring open first_needed={first_needed:?}",crate::qwen_forward::original_serve_stack::dbg_el());
let mut workspace=dense.native_batch_workspace(binding,self.context)?.ok_or(Error::IdentityMismatch)?;
@@ -453,7 +454,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx>{
crate::bringup_trace!("DBG[{:.0}s] record_original_draft_kv -> {}",crate::qwen_forward::original_serve_stack::dbg_el(),match &outcome{OriginalPromptDraftOutcome::Completed(_)=>"Completed",OriginalPromptDraftOutcome::Fallback(reason)=>{crate::bringup_trace!("DBG fallback reason: {reason:?}");"Fallback"},OriginalPromptDraftOutcome::Skipped=>"Skipped",OriginalPromptDraftOutcome::Cancelled=>"Cancelled"});
let outcome=match outcome{
OriginalPromptDraftOutcome::Fallback(_)=>rows.record_original_scalar_draft_kv(
binding,dense,moe,&txn,&final_row,sample.next(),scalar,&mut workspace,ring,prior,
binding,dense,&retained_mtp,&txn,&final_row,sample.next(),scalar,&mut workspace,ring,prior,
scalar_weights,first_needed,policy,&mut cancelled)?,
other=>other,
};
@@ -2,12 +2,13 @@
//! phase, shifted actual IDs and current ring receipt, literal scalar producer.
use super::*;
use leleka_cuda_driver::{OriginalMtpActor,OriginalMtpRetainedScalarWeights,OriginalMtpScalarPolicy};
use crate::qwen_forward::moe_weight_cache::RetainedOriginalMtpWeights;
impl<'source,'arena,'loan,'ctx> PromptDeviceRows<'source,'arena,'loan,'ctx> {
/// Original Prefill uses only known-next conditioning with no head/D2H.
/// The genuine all-row R and immutable input owners survive through drain.
#[allow(clippy::too_many_arguments)]
pub(in crate::qwen_forward) fn record_original_scalar_draft_kv(
&mut self,binding:&ModelBinding,dense:&DenseWeightCache<'ctx>,mtp:&MoeWeightCache<'ctx>,
&mut self,binding:&ModelBinding,dense:&DenseWeightCache<'ctx>,mtp:&RetainedOriginalMtpWeights<'ctx>,
txn:&OriginalPromptTxn<'_, '_>,preview:&PromptDeviceFinal,last_next:TokenId,
producer:&mut OriginalMtpActor<'ctx>,workspace:&mut NativeBatchWorkspace<'ctx>,
ring:&mut MtpOriginalInt8<'ctx>,prior:&MtpInt8Snapshot,
@@ -34,8 +35,10 @@ impl<'source,'arena,'loan,'ctx> PromptDeviceRows<'source,'arena,'loan,'ctx> {
next.push(last_next.get() as i32);
let context=self.sources.context;
// Exact Original acquisition order, authentic pack descriptor checks.
let matrix=|role,expected|->Result<&leleka_cuda_driver::DeviceBuffer<'ctx>>{
let(kind,rows,columns,buffer)=mtp.mtp_matrix(binding,context,role)?;
// The retained scalar weights come from the same owner the drafter was built with: the pinned pack's cache, or with an RT
// directory (--mtp-rt-dir) its own bank, whose buffers are not the MoE cache's. Check against that owner.
let matrix=|role,expected|->Result<Rc<leleka_cuda_driver::DeviceBuffer<'ctx>>>{
let(kind,rows,columns,buffer)=mtp.matrix(binding,context,role)?;
if (kind,rows,columns)!=expected{crate::bringup_trace!("DBG scalar draft-kv matrix {role:?}: pack has {:?} {rows}x{columns}, expected {:?}",kind,expected);return Err(Error::InvalidDimension)}Ok(buffer)
};
retained.with_whole_loans(context,|weights,_|{
@@ -51,7 +54,7 @@ impl<'source,'arena,'loan,'ctx> PromptDeviceRows<'source,'arena,'loan,'ctx> {
(weights.input.value,MtpRole::ValueProjection,(MtpKind::Q8_0,512,2560)),
(weights.input.key_norm,MtpRole::KeyNorm,(MtpKind::F32,1,256)),
(weights.gr_inject,MtpRole::AttentionInject,(MtpKind::Bf16,4,10240)),
]{match matrix(role,shape){Ok(b) if std::ptr::eq(buffer,b)=>{},Ok(_)=>{crate::bringup_trace!("DBG scalar draft-kv matrix {role:?}: buffer is not the retained one");return Err(leleka_cuda_driver::Error::InvalidProjectionShape)},Err(e)=>{crate::bringup_trace!("DBG scalar draft-kv matrix {role:?} err {e:?}");return Err(leleka_cuda_driver::Error::InvalidProjectionShape)}}}
]{match matrix(role,shape){Ok(b) if std::ptr::eq(buffer,&*b)=>{},Ok(_)=>{crate::bringup_trace!("DBG scalar draft-kv matrix {role:?}: buffer is not the retained one");return Err(leleka_cuda_driver::Error::InvalidProjectionShape)},Err(e)=>{crate::bringup_trace!("DBG scalar draft-kv matrix {role:?} err {e:?}");return Err(leleka_cuda_driver::Error::InvalidProjectionShape)}}}
Ok(())
}).map_err(micro_driver_error)?;
// Input/draft host policy and all eleven immutable device owners are live
@@ -0,0 +1,21 @@
set -uo pipefail
# claude/e2e (second rerun, scalar draft-KV identity fix with --mtp-rt-dir, auto expert cache + spec-min-p 0.5): the real Codex CLI (0.163.0) writes a Three.js tic-tac-toe game (rerun after the 32768 output cap) against the production stack
# (leleka-inference Original serve, default conversation cache, behind strata-next). Profiling on, engine log kept.
export E2E_BIN=$HOME/work/leleka-e2e/bin-sm120
export E2E_OUT=$HOME/work/leleka-e2e/runs/943d-codex-ttt
export CODEX_TIMEOUT_S=5400
export LELEKA_BRINGUP_TRACE=1 # PREFILL-PROFILE / round lines in the engine log
# reference-equivalent engine configuration (the vanilla Strata Codex runs): automatic per-layer expert cache with the 98x/990 profile,
# --spec-min-p 0.5 (MTP depth 4 is the Original's default), INT8 K+V 32768 resident cells (default), adaptive tier, seed default; the gpu budget
# the way the 98x/99x auto-cache jobs did (30 GiB), so the auto-sized cache has the free VRAM the Original had
export LELEKA_ORIGINAL_SERVE_EXPERT_SLOTS=auto E2E_GPU_BUDGET=32212254720
export E2E_ENGINE_ARGS="--spec-min-p 0.5"
export E2E_SAMPLE_S=10
export LELEKA_CAPTURE_DIR=$HOME/work/codex-e2e/capture/943d-codex-ttt # PRIVATE: prompt + generated token ids of every request, for offline replay (never in Git) # RSS / VRAM / MemAvailable every 10 s
export E2E_STATUS_FILE=$HOME/work/codex-e2e/STATUS.txt
export E2E_RUN_CMD="bash $HOME/work/leleka-e2e/scripts/e2e/codex/run-codex.sh ttt"
rm -rf $E2E_OUT $LELEKA_CAPTURE_DIR
date -u +%FT%TZ; sha256sum $E2E_BIN/* $HOME/work/codex-e2e/bin/codex $HOME/work/codex-e2e/vendor/three.module.min.js
bash $HOME/work/leleka-e2e/scripts/e2e/run-e2e.sh
echo "run-e2e rc=$?"
date -u +%FT%TZ
@@ -0,0 +1,21 @@
set -uo pipefail
# claude/e2e (second rerun, scalar draft-KV identity fix with --mtp-rt-dir, auto expert cache + spec-min-p 0.5): the real Codex CLI (0.163.0) builds a CRM (Python stdlib + SQLite) with the PREFIX CACHE on (--prefix-cache-mib auto,
# replaces the conversation cache). Long-horizon session; profiling on, engine log kept.
export E2E_BIN=$HOME/work/leleka-e2e/bin-sm120
export E2E_OUT=$HOME/work/leleka-e2e/runs/945d-codex-crm
export CODEX_TIMEOUT_S=10200
export LELEKA_BRINGUP_TRACE=1 LELEKA_PREFIX_TRACE=1
# reference-equivalent engine configuration (the vanilla Strata Codex runs): automatic per-layer expert cache with the 98x/990 profile,
# --spec-min-p 0.5 (MTP depth 4 is the Original's default), INT8 K+V 32768 resident cells (default), adaptive tier, seed default; the gpu budget
# the way the 98x/99x auto-cache jobs did (30 GiB), so the auto-sized cache has the free VRAM the Original had
export LELEKA_ORIGINAL_SERVE_EXPERT_SLOTS=auto E2E_GPU_BUDGET=32212254720
export E2E_ENGINE_ARGS="--prefix-cache-mib auto --spec-min-p 0.5"
export E2E_SAMPLE_S=10
export LELEKA_CAPTURE_DIR=$HOME/work/codex-e2e/capture/945d-codex-crm # PRIVATE: prompt + generated token ids of every request, for offline replay (never in Git)
export E2E_STATUS_FILE=$HOME/work/codex-e2e/STATUS.txt
export E2E_RUN_CMD="bash $HOME/work/leleka-e2e/scripts/e2e/codex/run-codex.sh crm"
rm -rf $E2E_OUT $LELEKA_CAPTURE_DIR
date -u +%FT%TZ; sha256sum $E2E_BIN/* $HOME/work/codex-e2e/bin/codex
bash $HOME/work/leleka-e2e/scripts/e2e/run-e2e.sh
echo "run-e2e rc=$?"
date -u +%FT%TZ