STRA-338 params h02: MTP actor adopt and admit take OriginalMtpLoadedParts; draft head takes OriginalMainHead

This commit is contained in:
Pavlo Ratushnyi
2026-11-08 01:20:52 +01:00
parent 8223ee2628
commit cffd79cbc7
9 changed files with 85 additions and 46 deletions
+1 -1
View File
@@ -25,7 +25,7 @@
#[path = "original_mtp_actor_loaded.rs"]
mod loaded;
#[cfg(all(feature = "cuda-driver", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
pub use loaded::{OriginalMtpActor, OriginalMtpLoadEvidence, OriginalMtpLoadMeter};
pub use loaded::{OriginalMtpActor, OriginalMtpLoadEvidence, OriginalMtpLoadMeter, OriginalMtpLoadedParts};
/// Original `load` default. It becomes an effective window only after actual
/// geometry/KV admission; storing this number does not select a ring.
@@ -44,6 +44,21 @@ pub struct OriginalMtpLoadEvidence {
draft_vocabulary: Option<usize>,
}
/// What `OriginalMtpActor::adopt` takes over from load and bind: the assembled drafter, the draft ring it was bound to
/// (only inspected) and the retained weights it reads.
pub struct OriginalMtpLoadedParts<'a, 'ctx> {
pub drafter: OriginalMtpDrafter<'ctx>,
pub ring: &'a MtpOriginalInt8<'ctx>,
pub weights: Rc<OriginalMtpRetainedFullWeights<'ctx>>,
}
/// The loaded owners `admit` checks against each other: the drafter (checked through `&mut`), the ring and weights.
struct LoadedOwners<'a, 'ctx> {
drafter: &'a mut OriginalMtpDrafter<'ctx>,
ring: &'a MtpOriginalInt8<'ctx>,
weights: &'a OriginalMtpRetainedFullWeights<'ctx>,
}
pub struct OriginalMtpActor<'ctx> {
// Field order is destructor order: the drafter (stream drain, graphs,
// scratch and mapped owners) goes before the weights it reads.
@@ -11,13 +11,13 @@ impl<'ctx> OriginalMtpActor<'ctx> {
pub fn adopt(
context: &'ctx Context<'ctx>,
plan: OriginalMtpLoadPlan,
drafter: OriginalMtpDrafter<'ctx>,
ring: &MtpOriginalInt8<'ctx>,
weights: Rc<OriginalMtpRetainedFullWeights<'ctx>>,
parts: OriginalMtpLoadedParts<'_, 'ctx>,
evidence: OriginalMtpLoadEvidence,
) -> std::result::Result<Self, OriginalMtpLoadFailure> {
let OriginalMtpLoadedParts { drafter, ring, weights } = parts;
let mut drafter = drafter;
match Self::admit(context, &plan, &mut drafter, ring, &weights, evidence) {
let mut owners = LoadedOwners { drafter: &mut drafter, ring, weights: &weights };
match Self::admit(context, &plan, &mut owners, evidence) {
Ok((kv, disposition, coupled)) => Ok(Self {
drafter: Some(drafter),
weights,
@@ -41,13 +41,12 @@ impl<'ctx> OriginalMtpActor<'ctx> {
pub(in super::super) fn admit(
context: &'ctx Context<'ctx>,
plan: &OriginalMtpLoadPlan,
drafter: &mut OriginalMtpDrafter<'ctx>,
ring: &MtpOriginalInt8<'ctx>,
weights: &OriginalMtpRetainedFullWeights<'ctx>,
owners: &mut LoadedOwners<'_, 'ctx>,
evidence: OriginalMtpLoadEvidence,
) -> std::result::Result<(KvPlan, SetupDisposition, CoupledDraftHost), OriginalMtpLoadFailure> {
let capture = *plan.capture();
Self::check_loaded(context, &capture, drafter, ring, weights, evidence)?;
Self::check_loaded(context, &capture, owners, evidence)?;
let LoadedOwners { drafter, ring, weights } = owners;
// The retained state's ACTUAL mode, not a configured one: window ring
// as planned, or the Original fully-resident retry.
let kv = plan
@@ -61,11 +60,10 @@ impl<'ctx> OriginalMtpActor<'ctx> {
fn check_loaded(
context: &'ctx Context<'ctx>,
capture: &crate::original_mtp_loader::OriginalMtpCapture,
drafter: &mut OriginalMtpDrafter<'ctx>,
ring: &MtpOriginalInt8<'ctx>,
weights: &OriginalMtpRetainedFullWeights<'ctx>,
owners: &mut LoadedOwners<'_, 'ctx>,
evidence: OriginalMtpLoadEvidence,
) -> std::result::Result<(), OriginalMtpLoadFailure> {
let LoadedOwners { drafter, ring, weights } = owners;
drafter.validate_context(context).map_err(inconsistent)?;
ring.validate_context(context).map_err(inconsistent)?;
weights.validate_context(context).map_err(inconsistent)?;
+21 -12
View File
@@ -4,6 +4,7 @@
//! loader's placement/ordering/refusal behaviour, not model quality.
use super::*;
use crate::Driver;
use crate::original_mtp_draft_head::OriginalMainHead;
use crate::original_mtp_rt::fixtures::{self as fx, TempDir};
const ENABLE: &str = "LELEKA_MTP_RT_BANK_GPU";
@@ -30,21 +31,27 @@ fn rt_bank_places_every_tensor_and_the_expert_chunks_exactly() {
fx::write(&dir, "experts.bin", &experts);
let baseline = context.allocated_bytes();
bank_places_tensors_and_chunks(&context, &dir, &text, &blob, &experts, baseline);
bank_places_tensors_and_chunks(
&context,
BankFixture { dir: &dir, text: &text, blob: &blob, experts: &experts },
baseline,
);
bank_refusals_leave_no_allocation(&context, &dir, &text, &experts, baseline);
// the budget refusal plays cudaMalloc's: Original text plus the sizes
let tight = device.create_context(blob.len() + 16).expect("tight context");
bank_budget_refusal(&tight, &dir, &experts);
}
fn bank_places_tensors_and_chunks(
context: &Context<'_>,
dir: &TempDir,
text: &str,
blob: &[u8],
experts: &[u8],
baseline: usize,
) {
/// The files of the fixture bank: its directory, the dense index text, the dense blob and the expert bytes.
struct BankFixture<'a> {
dir: &'a TempDir,
text: &'a str,
blob: &'a [u8],
experts: &'a [u8],
}
fn bank_places_tensors_and_chunks(context: &Context<'_>, fixture: BankFixture<'_>, baseline: usize) {
let BankFixture { dir, text, blob, experts } = fixture;
let bank = context.original_mtp_rt_bank(&dir.0, 3).expect("bank");
assert_eq!(bank.device(), 0);
assert_eq!(bank.vram_bytes(), (blob.len() + experts.len()) as u64);
@@ -141,9 +148,10 @@ fn gather_width_matches<'c>(
let mut head = context.allocate(host.len()).unwrap();
head.copy_from(&host).unwrap();
// absent file: Original ignores it
assert!(context.original_mtp_draft_head(&dir.0, &head, 8, columns, vocabulary).unwrap().is_none());
let main = OriginalMainHead { weights: &head, kind: 8, columns, vocabulary };
assert!(context.original_mtp_draft_head(&dir.0, main).unwrap().is_none());
fx::write(dir, "draft_vocab.bin", &ids.iter().flat_map(|i| i.to_le_bytes()).collect::<Vec<_>>());
let draft = context.original_mtp_draft_head(&dir.0, &head, 8, columns, vocabulary).unwrap().expect("subset head");
let draft = context.original_mtp_draft_head(&dir.0, main).unwrap().expect("subset head");
assert_eq!((draft.count(), draft.row_bytes()), (ids.len(), row_bytes));
let expected: Vec<u8> =
ids.iter().flat_map(|&id| host[id as usize * row_bytes..(id as usize + 1) * row_bytes].to_vec()).collect();
@@ -168,7 +176,8 @@ fn gather_rejects_ids_outside_the_vocabulary(
) {
// an id outside the vocabulary never reaches the kernel
fx::write(dir, "draft_vocab.bin", &50i32.to_le_bytes());
let error = context.original_mtp_draft_head(&dir.0, head, 8, columns, vocabulary).err().expect("range");
let main = OriginalMainHead { weights: head, kind: 8, columns, vocabulary };
let error = context.original_mtp_draft_head(&dir.0, main).err().expect("range");
assert!(error.message().contains("outside the vocabulary"));
std::fs::remove_file(dir.0.join("draft_vocab.bin")).unwrap();
}
@@ -65,18 +65,32 @@ impl<'ctx> OriginalMtpDraftHead<'ctx> {
}
}
/// The main native head the draft head is gathered from: its `NativeHead::weights()` allocation, the quantisation
/// kind, the column count and the vocabulary rows.
#[derive(Clone, Copy)]
pub struct OriginalMainHead<'a, 'ctx> {
pub weights: &'a DeviceBuffer<'ctx>,
pub kind: u32,
pub columns: usize,
pub vocabulary: usize,
}
/// The three device buffers of one row gather: the source rows, the row ids and the destination rows.
struct GatherBuffers<'a> {
source: &'a DeviceBuffer<'a>,
ids: &'a DeviceBuffer<'a>,
destination: &'a DeviceBuffer<'a>,
}
impl<'ctx> Context<'ctx> {
/// Returns `Ok(None)` exactly where Original ignores the file. `head` is the
/// main `NativeHead::weights()` allocation (`vocabulary` rows of
/// `columns` elements in `head_kind`).
/// Returns `Ok(None)` exactly where Original ignores the file. `main.weights` is the main
/// `NativeHead::weights()` allocation (`vocabulary` rows of `columns` elements in `kind`).
pub fn original_mtp_draft_head(
&'ctx self,
directory: &Path,
head: &DeviceBuffer<'ctx>,
head_kind: u32,
columns: usize,
vocabulary: usize,
main: OriginalMainHead<'_, 'ctx>,
) -> std::result::Result<Option<OriginalMtpDraftHead<'ctx>>, OriginalMtpLoadFailure> {
let OriginalMainHead { weights: head, kind: head_kind, columns, vocabulary } = main;
let ids = match rt::read_draft_vocab(directory)? {
DraftVocab::Absent => return Ok(None),
DraftVocab::Subset(ids) => ids,
@@ -130,7 +144,7 @@ impl<'ctx> Context<'ctx> {
.allocate(count.checked_mul(row_bytes).ok_or_else(|| head_refusal(Error::InvalidSize))?)
.map_err(head_refusal)?;
forward.copy_from(&id_bytes).map_err(head_refusal)?;
self.gather_rows(head, &forward, &rows, row_bytes, count)
self.gather_rows(GatherBuffers { source: head, ids: &forward, destination: &rows }, row_bytes, count)
.map_err(|cause| OriginalMtpLoadFailure::new("mtp: the draft head gather failed", Some(cause)))?;
Ok((forward, rows))
}
@@ -153,14 +167,8 @@ impl<'ctx> Context<'ctx> {
/// `strata::kernels::gather_rows(src, row_bytes, ids, n, dst, nullptr)` then
/// `cudaDeviceSynchronize`: one launch on the legacy stream, completed
/// before return. A launch or drain failure poisons the context.
fn gather_rows(
&self,
source: &DeviceBuffer<'_>,
ids: &DeviceBuffer<'_>,
destination: &DeviceBuffer<'_>,
row_bytes: usize,
rows: usize,
) -> Result<()> {
fn gather_rows(&self, buffers: GatherBuffers<'_>, row_bytes: usize, rows: usize) -> Result<()> {
let GatherBuffers { source, ids, destination } = buffers;
let (name, unit) = gather_kernel(row_bytes);
let mut command = Command::new(
name,
+2 -2
View File
@@ -28,13 +28,13 @@ pub use original_moe_prompt_device::{
};
pub use original_mtp_actor::{ActorRefusal as OriginalMtpActorRefusal, DraftSettings as OriginalMtpDraftSettings};
#[cfg(all(feature = "cuda-driver", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
pub use original_mtp_actor::{OriginalMtpActor, OriginalMtpLoadEvidence, OriginalMtpLoadMeter};
pub use original_mtp_actor::{OriginalMtpActor, OriginalMtpLoadEvidence, OriginalMtpLoadMeter, OriginalMtpLoadedParts};
pub use original_mtp_bank::{
OriginalMtpLoadFailure, OriginalMtpRtBank, OriginalMtpRtTensor, OriginalMtpTensorKind,
original_mtp_draft_vocab_bytes,
};
#[cfg(all(feature = "cuda-driver", any(target_os = "linux", target_os = "windows"), target_pointer_width = "64"))]
pub use original_mtp_draft_head::OriginalMtpDraftHead;
pub use original_mtp_draft_head::{OriginalMainHead, OriginalMtpDraftHead};
pub use original_mtp_loader::{
CoupledPlan as OriginalMtpCoupledPlan, KvFormat as OriginalMtpKvFormat, KvMode as OriginalMtpKvMode,
KvPlan as OriginalMtpKvPlan, LoaderError as OriginalMtpPlanError, OriginalMtpCapture, OriginalMtpKvStartup,
@@ -290,10 +290,10 @@ fn adopt_actor<'ctx>(
let _ = ring.close();
return None;
};
let parts = OriginalMtpLoadedParts { drafter, ring: &ring, weights: std::rc::Rc::clone(&weights) };
let actor = step!(
"OriginalMtpActor::adopt",
OriginalMtpActor::adopt(context, plan, drafter, &ring, std::rc::Rc::clone(&weights), evidence)
.map_err(|e| e.message().to_owned())
OriginalMtpActor::adopt(context, plan, parts, evidence).map_err(|e| e.message().to_owned())
);
let Some(actor) = actor else {
let _ = ring.close();
@@ -126,7 +126,7 @@ impl<'ctx> OriginalMtpFullOwner<'ctx> {
let draft_head: Option<OriginalMtpDraftHead<'ctx>> = match rt_directory {
Some(directory) => {
let weights = head.weights();
context.original_mtp_draft_head(directory, &weights, head.kind(), head.columns(), head.vocabulary())?
context.original_mtp_draft_head(directory, head.main_head(&weights))?
}
None => None,
};
@@ -183,7 +183,8 @@ impl<'ctx> OriginalMtpFullOwner<'ctx> {
return Err(primary.into());
}
};
match OriginalMtpActor::adopt(context, plan, drafter, &ring, weights, evidence) {
let parts = leleka_cuda_driver::OriginalMtpLoadedParts { drafter, ring: &ring, weights };
match OriginalMtpActor::adopt(context, plan, parts, evidence) {
Ok(actor) => Ok((actor, ring)),
Err(primary) => {
let _ = ring.close();
@@ -37,6 +37,14 @@ impl<'ctx> OriginalMtpNativeHead<'ctx> {
pub(in crate::qwen_forward) fn dispatch(&self) -> NativeHeadDispatch {
self.dispatch
}
/// This head as the main head the draft head is gathered from, over its own `weights` allocation.
pub(in crate::qwen_forward) fn main_head<'a>(
&self,
weights: &'a DeviceBuffer<'ctx>,
) -> leleka_cuda_driver::OriginalMainHead<'a, 'ctx> {
let (kind, columns, vocabulary) = (self.kind, self.columns, self.vocabulary);
leleka_cuda_driver::OriginalMainHead { weights, kind, columns, vocabulary }
}
}
pub(in crate::qwen_forward) struct OriginalNativeOutput<'ctx> {
binding: BindingIdentity,