STRA-338 size: cuda-driver OriginalMtpScalarPrefill::new_internal admit/empty/init phases extracted (132 -> 8 lines)

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 14:17:08 +01:00
parent a893014a44
commit d00723490c
@@ -60,27 +60,46 @@ impl<'ctx> OriginalMtpScalarPrefill<'ctx> {
prompt_cap: usize,
exact: Option<OriginalLayoutSetup<'ctx>>,
) -> Result<Self> {
let legacy_charge = Self::planned_device_bytes(max_t, prompt_cap)?;
let charge = if exact.is_some() {
legacy_charge.checked_sub(Scratch::new(max_t)?.bytes).ok_or(Error::InvalidSize)?
} else {
legacy_charge
};
if let Some(e) = &exact {
if e.layout.t != max_t {
return Err(Error::InvalidQwenShape);
}
checked(c, &e.bank, e.layout.bytes)?;
Self::admit_new(c, max_t, prompt_cap, exact.as_ref())?;
let mut o = Self::empty(c, max_t, prompt_cap, exact.as_ref())?;
if let Err(e) = o.init_device(c, exact.as_ref()) {
return combine(Err(e), o.close());
}
let assets = [
Ok(o)
}
/// The six sealed prefill assets, in module-slot order.
fn scalar_assets() -> [&'static [u8]; 6] {
[
include_bytes!("../../mtp_original_prefill.ptx").as_slice(),
include_bytes!("../../mtp_original_prefill_embedding.ptx").as_slice(),
include_bytes!("../../mtp_original_tail.ptx").as_slice(),
include_bytes!("../../mtp_original_scalar_norm.ptx").as_slice(),
include_bytes!("../../mtp_original_scalar_gr.ptx").as_slice(),
include_bytes!("../../mtp_original_kv.ptx").as_slice(),
];
if assets.iter().any(|p| p.windows(8).any(|w| w == b"UNSEALED")) {
]
}
/// Shape, sealed-asset, context and budget admission before any device memory exists.
fn admit_new(
c: &'ctx Context<'ctx>,
max_t: usize,
prompt_cap: usize,
exact: Option<&OriginalLayoutSetup<'ctx>>,
) -> Result<()> {
let legacy_charge = Self::planned_device_bytes(max_t, prompt_cap)?;
let charge = if exact.is_some() {
legacy_charge.checked_sub(Scratch::new(max_t)?.bytes).ok_or(Error::InvalidSize)?
} else {
legacy_charge
};
if let Some(e) = exact {
if e.layout.t != max_t {
return Err(Error::InvalidQwenShape);
}
checked(c, &e.bank, e.layout.bytes)?;
}
if Self::scalar_assets().iter().any(|p| p.windows(8).any(|w| w == b"UNSEALED")) {
return Err(Error::UnsupportedQwenOperation);
}
let required = charge.checked_add(RESERVE_BYTES).ok_or(Error::BudgetExceeded)?;
@@ -92,7 +111,17 @@ impl<'ctx> OriginalMtpScalarPrefill<'ctx> {
{
return Err(Error::BudgetExceeded);
}
let mut o = Self {
Ok(())
}
/// An owner with no device memory, modules or stream yet.
fn empty(
c: &'ctx Context<'ctx>,
max_t: usize,
prompt_cap: usize,
exact: Option<&OriginalLayoutSetup<'ctx>>,
) -> Result<Self> {
Ok(Self {
context: c,
max_t,
prompt_cap,
@@ -100,11 +129,11 @@ impl<'ctx> OriginalMtpScalarPrefill<'ctx> {
scratch: None,
records: None,
status: None,
layout: match &exact {
layout: match exact {
Some(e) => e.layout.scalar(),
None => Scratch::new(max_t)?,
},
original_layout: exact.as_ref().map(|e| e.layout.clone()),
original_layout: exact.map(|e| e.layout.clone()),
shared_limit: 0,
plain_tile: 0,
stream: None,
@@ -117,74 +146,83 @@ impl<'ctx> OriginalMtpScalarPrefill<'ctx> {
pending_host: Vec::new(),
graph_metadata_bytes: 0,
has_pending: false,
};
let operation = (|| {
let g = c.enter().inspect_err(|_| {
c.poison();
c.retain_driver();
})?;
let load = (|| {
o.modules[0] = Some(Rc::new(Module::load(assets[0], ENTRIES)?));
o.modules[1] = Some(Rc::new(Module::load(assets[1], &[c"mtp_prefill_native_embedding"])?));
o.modules[2] = Some(Rc::new(Module::load(assets[2], TAIL_ENTRIES)?));
o.modules[3] = Some(Rc::new(Module::load(
assets[3],
&[c"mtp_scalar_norm1024", c"mtp_scalar_rope", c"mtp_scalar_copy_i32", c"mtp_scalar_encoded"],
)?));
o.modules[4] = Some(Rc::new(Module::load(assets[4], GR_ENTRIES)?));
o.modules[5] = Some(Rc::new(Module::load(assets[5], KV_ENTRIES)?));
let (shared, tile) = o.module(4)?.configure_gr()?;
o.shared_limit = shared;
o.plain_tile = tile;
Ok(())
})();
let pop = g.finish();
if load.is_err() || pop.is_err() {
o.poison();
}
combine(load, pop)?;
o.scratch = Some(match &exact {
Some(e) => Rc::clone(&e.bank),
None => Rc::new(c.allocate(o.layout.bytes)?),
});
o.records = Some(Rc::new(c.allocate(prompt_cap * 29 * 4)?));
o.status = Some(Rc::new(c.allocate((prompt_cap.div_ceil(max_t) + 1) * SCALAR_FLAGS * 4)?));
o.stream = Some(c.original_gpu_stream(OriginalStreamKind::NonBlocking)?);
let g = c.enter().inspect_err(|_| {
c.poison();
c.retain_driver();
})?;
let allocation = match &exact {
Some(e) => Ok(mapped_records::MappedOwner::original(max_t, Rc::clone(&e.mapped))),
None => mapped_records::MappedOwner::new(max_t),
};
let pop = g.finish();
if pop.is_err() {
o.poison();
let original = match allocation {
Ok(owner) => {
std::mem::forget(owner);
Ok(())
}
Err(e) => Err(e),
};
return combine(original, pop);
}
if allocation.is_err() {
o.poison();
}
*o.mapped.borrow_mut() = Some(allocation?);
o.graphs = Some(OriginalGraphRegistry::new(c, 2 * max_t)?);
o.pending_host.try_reserve_exact(2).map_err(|_| Error::HostAllocation)?;
if c.memory_info()?.0 < RESERVE_BYTES {
return Err(Error::BudgetExceeded);
}
Ok(())
})();
if let Err(e) = operation {
return combine(Err(e), o.close());
})
}
fn init_device(&mut self, c: &'ctx Context<'ctx>, exact: Option<&OriginalLayoutSetup<'ctx>>) -> Result<()> {
self.init_modules(c)?;
self.scratch = Some(match exact {
Some(e) => Rc::clone(&e.bank),
None => Rc::new(c.allocate(self.layout.bytes)?),
});
self.records = Some(Rc::new(c.allocate(self.prompt_cap * 29 * 4)?));
self.status = Some(Rc::new(c.allocate((self.prompt_cap.div_ceil(self.max_t) + 1) * SCALAR_FLAGS * 4)?));
self.stream = Some(c.original_gpu_stream(OriginalStreamKind::NonBlocking)?);
self.init_mapped(c, exact)?;
self.graphs = Some(OriginalGraphRegistry::new(c, 2 * self.max_t)?);
self.pending_host.try_reserve_exact(2).map_err(|_| Error::HostAllocation)?;
if c.memory_info()?.0 < RESERVE_BYTES {
return Err(Error::BudgetExceeded);
}
Ok(o)
Ok(())
}
fn init_modules(&mut self, c: &'ctx Context<'ctx>) -> Result<()> {
let g = c.enter().inspect_err(|_| {
c.poison();
c.retain_driver();
})?;
let load = self.load_modules();
let pop = g.finish();
if load.is_err() || pop.is_err() {
self.poison();
}
combine(load, pop)
}
fn load_modules(&mut self) -> Result<()> {
let assets = Self::scalar_assets();
self.modules[0] = Some(Rc::new(Module::load(assets[0], ENTRIES)?));
self.modules[1] = Some(Rc::new(Module::load(assets[1], &[c"mtp_prefill_native_embedding"])?));
self.modules[2] = Some(Rc::new(Module::load(assets[2], TAIL_ENTRIES)?));
self.modules[3] = Some(Rc::new(Module::load(
assets[3],
&[c"mtp_scalar_norm1024", c"mtp_scalar_rope", c"mtp_scalar_copy_i32", c"mtp_scalar_encoded"],
)?));
self.modules[4] = Some(Rc::new(Module::load(assets[4], GR_ENTRIES)?));
self.modules[5] = Some(Rc::new(Module::load(assets[5], KV_ENTRIES)?));
let (shared, tile) = self.module(4)?.configure_gr()?;
self.shared_limit = shared;
self.plain_tile = tile;
Ok(())
}
fn init_mapped(&mut self, c: &'ctx Context<'ctx>, exact: Option<&OriginalLayoutSetup<'ctx>>) -> Result<()> {
let g = c.enter().inspect_err(|_| {
c.poison();
c.retain_driver();
})?;
let allocation = match exact {
Some(e) => Ok(mapped_records::MappedOwner::original(self.max_t, Rc::clone(&e.mapped))),
None => mapped_records::MappedOwner::new(self.max_t),
};
let pop = g.finish();
if pop.is_err() {
self.poison();
let original = match allocation {
Ok(owner) => {
std::mem::forget(owner);
Ok(())
}
Err(e) => Err(e),
};
return combine(original, pop);
}
if allocation.is_err() {
self.poison();
}
*self.mapped.borrow_mut() = Some(allocation?);
Ok(())
}
pub fn allocated_bytes(&self) -> usize {
[&self.scratch, &self.records, &self.status].into_iter().map(|s| s.as_ref().map_or(0, |b| b.len())).sum()