STRA-338 size: cuda-driver OriginalMtpFullForward::new_internal split into admit and device-init phases in full_init (159 -> 24 lines)
This commit is contained in:
@@ -242,6 +242,8 @@ mod role_map_tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[path = "mtp_original_full_forward/full_init.rs"]
|
||||
mod full_init;
|
||||
#[path = "mtp_original_full_forward/impl_context.rs"]
|
||||
mod impl_context;
|
||||
#[path = "mtp_original_full_forward/impl_fullowned.rs"]
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
//! Construction phases of `OriginalMtpFullForward::new_internal`, split out by STRA-338 (size). Each phase keeps the
|
||||
//! original context-guard, poison and error ordering; `FullOwned::init_device` runs them in the former closure order.
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
/// What `new_internal` decides before any device memory exists.
|
||||
pub(super) struct FullGeometry {
|
||||
pub(super) layout: TailLayout,
|
||||
pub(super) original_layout: Option<physical_layout::Layout>,
|
||||
pub(super) sizes: [usize; 7],
|
||||
}
|
||||
|
||||
impl FullGeometry {
|
||||
/// Validates the weights, shape and sealed assets and charges the device budget for the owner.
|
||||
pub(super) fn admit<'ctx>(
|
||||
c: &'ctx Context<'ctx>,
|
||||
weights: &OriginalMtpRetainedFullWeights<'ctx>,
|
||||
max_t: usize,
|
||||
(coupled, original): (bool, bool),
|
||||
) -> Result<Self> {
|
||||
weights.validate_context(c)?;
|
||||
if !(1..=8).contains(&max_t) {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
if symbols::ASSETS.iter().any(|(p, _)| p.windows(8).any(|w| w == b"UNSEALED")) {
|
||||
return Err(Error::UnsupportedQwenOperation);
|
||||
}
|
||||
let layout = TailLayout::new(weights.vocab, max_t, coupled)?;
|
||||
let original_layout = if original { Some(physical_layout::Layout::new(max_t)?) } else { None };
|
||||
let sizes = physical_layout::Layout::extra_sizes(weights.vocab, max_t, coupled)?;
|
||||
let own_bytes = match &original_layout {
|
||||
Some(l) => sizes.iter().try_fold(l.bytes, |a, n| a.checked_add(*n).ok_or(Error::InvalidSize))?,
|
||||
None => layout.bytes,
|
||||
};
|
||||
let charge = own_bytes.checked_add(RESERVE_BYTES).ok_or(Error::BudgetExceeded)?;
|
||||
if c.allocated_bytes().checked_add(charge).is_none_or(|n| n > c.budget) || c.memory_info()?.0 < charge {
|
||||
return Err(Error::BudgetExceeded);
|
||||
}
|
||||
Ok(Self { layout, original_layout, sizes })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ctx> FullOwned<'ctx> {
|
||||
pub(super) fn init_device(
|
||||
&mut self,
|
||||
c: &'ctx Context<'ctx>,
|
||||
(original, max_t, coupled): (bool, usize, bool),
|
||||
sizes: [usize; 7],
|
||||
) -> Result<()> {
|
||||
self.init_modules(c)?;
|
||||
self.init_gr_config(c)?;
|
||||
self.init_mapped(c, (original, max_t, coupled))?;
|
||||
self.init_scratch(c, original, sizes)?;
|
||||
self.zero_scratch(c)?;
|
||||
self.init_step_ids()
|
||||
}
|
||||
|
||||
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 pop.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
}
|
||||
combine(load, pop)
|
||||
}
|
||||
|
||||
fn load_modules(&mut self) -> Result<()> {
|
||||
for (p, e) in &symbols::ASSETS[..symbols::CHAIN_ASSET] {
|
||||
self.modules.push(Rc::new(FM::load(p, e)?));
|
||||
}
|
||||
if chain_mtp_assets() || daily_mtp() {
|
||||
// chain_window.ptx: loaded only when selected, and only if it is the sealed one
|
||||
crate::original_verify::chain::CHAIN.sealed()?;
|
||||
let (p, e) = &symbols::ASSETS[symbols::CHAIN_ASSET];
|
||||
self.modules.push(Rc::new(FM::load(p, e)?));
|
||||
}
|
||||
if daily_mtp() {
|
||||
// sm120_attn_daily.ptx: the draft step's attention (opt-in, LELEKA_SM120_ATTN_DAILY_MTP=1); slot 22 follows the chain slot
|
||||
crate::original_verify::sm120::ATTN_DAILY.sealed()?;
|
||||
let (p, e) = &symbols::ASSETS[symbols::DAILY_ASSET];
|
||||
self.modules.push(Rc::new(FM::load(p, e)?));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_gr_config(&self, c: &'ctx Context<'ctx>) -> Result<()> {
|
||||
let g = c.enter().inspect_err(|_| {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
})?;
|
||||
let config = self.modules[17].configure_gr();
|
||||
let pop = g.finish();
|
||||
if pop.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
}
|
||||
let _actual = combine(config, pop)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_mapped(&mut self, c: &'ctx Context<'ctx>, (original, max_t, coupled): (bool, usize, bool)) -> Result<()> {
|
||||
let g = c.enter().inspect_err(|_| {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
})?;
|
||||
let mapped = if original {
|
||||
full_mapped::MappedOwner::original(max_t, coupled)
|
||||
} else {
|
||||
full_mapped::MappedOwner::new(max_t, coupled)
|
||||
};
|
||||
let pop = g.finish();
|
||||
if pop.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
let primary = match mapped {
|
||||
Ok(actual) => {
|
||||
std::mem::forget(actual);
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => Err(error),
|
||||
};
|
||||
return combine(primary, pop);
|
||||
}
|
||||
if original && mapped.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
}
|
||||
*self.mapped.get_mut() = Some(mapped?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_scratch(&mut self, c: &'ctx Context<'ctx>, original: bool, sizes: [usize; 7]) -> Result<()> {
|
||||
self.scratch = Some(Rc::new(c.allocate(self.original_layout.as_ref().map_or(self.layout.bytes, |l| l.bytes))?));
|
||||
if original {
|
||||
for (i, n) in sizes.into_iter().enumerate() {
|
||||
if n != 0 {
|
||||
self.extras[i] = Some(c.allocate(n)?);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn zero_scratch(&self, c: &'ctx Context<'ctx>) -> Result<()> {
|
||||
let g = c.enter().inspect_err(|_| {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
})?;
|
||||
// cuMemsetD8_v2 on device memory is asynchronous with respect to the host (it only orders on the legacy
|
||||
// default stream). Fence it inside the same guard: the non-original layout has no later synchronous copy
|
||||
// (the original one does, `copy_from_range` below), and the first kernel of a non-blocking stream must not
|
||||
// race the zeroing. Same pattern as `zero_state` + `synchronize` in mtp_original_ring_state.rs.
|
||||
let zero = self.modules[0]
|
||||
.zero(
|
||||
self.scratch
|
||||
.as_ref()
|
||||
.ok_or(Error::ContextPoisoned)?
|
||||
.handle
|
||||
.ok_or(Error::ContextPoisoned)?
|
||||
.qwen_address(),
|
||||
self.original_layout.as_ref().map_or(self.layout.bytes, |l| l.bytes),
|
||||
)
|
||||
.and_then(|()| c.driver.api.synchronize());
|
||||
let pop = g.finish();
|
||||
if zero.is_err() || pop.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
}
|
||||
combine(zero, pop)
|
||||
}
|
||||
|
||||
/// Uploads the identity step ids of the original layout.
|
||||
fn init_step_ids(&mut self) -> Result<()> {
|
||||
if let Some(l) = &self.original_layout {
|
||||
let mut ids = Vec::new();
|
||||
ids.try_reserve_exact(l.t * l.cap * 4).map_err(|_| Error::HostAllocation)?;
|
||||
for _ in 0..l.t {
|
||||
for i in 0..l.cap {
|
||||
ids.extend_from_slice(&(i as i32).to_le_bytes());
|
||||
}
|
||||
}
|
||||
Rc::get_mut(self.scratch.as_mut().ok_or(Error::ContextPoisoned)?)
|
||||
.ok_or(Error::InvalidQwenShape)?
|
||||
.copy_from_range(l.offsets[4], &ids)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
//! Moved out of the parent module by STRA-338: impl OriginalMtpFullForward.
|
||||
|
||||
use super::full_init::FullGeometry;
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
@@ -25,24 +26,8 @@ impl<'ctx> OriginalMtpFullForward<'ctx> {
|
||||
coupled: bool,
|
||||
original: bool,
|
||||
) -> Result<Self> {
|
||||
weights.validate_context(c)?;
|
||||
if !(1..=8).contains(&max_t) {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
if symbols::ASSETS.iter().any(|(p, _)| p.windows(8).any(|w| w == b"UNSEALED")) {
|
||||
return Err(Error::UnsupportedQwenOperation);
|
||||
}
|
||||
let layout = TailLayout::new(weights.vocab, max_t, coupled)?;
|
||||
let original_layout = if original { Some(physical_layout::Layout::new(max_t)?) } else { None };
|
||||
let sizes = physical_layout::Layout::extra_sizes(weights.vocab, max_t, coupled)?;
|
||||
let own_bytes = match &original_layout {
|
||||
Some(l) => sizes.iter().try_fold(l.bytes, |a, n| a.checked_add(*n).ok_or(Error::InvalidSize))?,
|
||||
None => layout.bytes,
|
||||
};
|
||||
let charge = own_bytes.checked_add(RESERVE_BYTES).ok_or(Error::BudgetExceeded)?;
|
||||
if c.allocated_bytes().checked_add(charge).is_none_or(|n| n > c.budget) || c.memory_info()?.0 < charge {
|
||||
return Err(Error::BudgetExceeded);
|
||||
}
|
||||
let FullGeometry { layout, original_layout, sizes } =
|
||||
FullGeometry::admit(c, &weights, max_t, (coupled, original))?;
|
||||
let mut o = FullOwned {
|
||||
context: c,
|
||||
modules: Vec::new(),
|
||||
@@ -58,121 +43,7 @@ impl<'ctx> OriginalMtpFullForward<'ctx> {
|
||||
source: RefCell::new(None),
|
||||
};
|
||||
o.modules.try_reserve_exact(symbols::ASSETS.len()).map_err(|_| Error::HostAllocation)?;
|
||||
let op = (|| {
|
||||
let g = c.enter().inspect_err(|_| {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
})?;
|
||||
let load = (|| {
|
||||
for (p, e) in &symbols::ASSETS[..symbols::CHAIN_ASSET] {
|
||||
o.modules.push(Rc::new(FM::load(p, e)?));
|
||||
}
|
||||
if chain_mtp_assets() || daily_mtp() {
|
||||
// chain_window.ptx: loaded only when selected, and only if it is the sealed one
|
||||
crate::original_verify::chain::CHAIN.sealed()?;
|
||||
let (p, e) = &symbols::ASSETS[symbols::CHAIN_ASSET];
|
||||
o.modules.push(Rc::new(FM::load(p, e)?));
|
||||
}
|
||||
if daily_mtp() {
|
||||
// sm120_attn_daily.ptx: the draft step's attention (opt-in, LELEKA_SM120_ATTN_DAILY_MTP=1); slot 22 follows the chain slot
|
||||
crate::original_verify::sm120::ATTN_DAILY.sealed()?;
|
||||
let (p, e) = &symbols::ASSETS[symbols::DAILY_ASSET];
|
||||
o.modules.push(Rc::new(FM::load(p, e)?));
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
let pop = g.finish();
|
||||
if pop.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
}
|
||||
combine(load, pop)?;
|
||||
let g = c.enter().inspect_err(|_| {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
})?;
|
||||
let config = o.modules[17].configure_gr();
|
||||
let pop = g.finish();
|
||||
if pop.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
}
|
||||
let _actual = combine(config, pop)?;
|
||||
let g = c.enter().inspect_err(|_| {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
})?;
|
||||
let mapped = if original {
|
||||
full_mapped::MappedOwner::original(max_t, coupled)
|
||||
} else {
|
||||
full_mapped::MappedOwner::new(max_t, coupled)
|
||||
};
|
||||
let pop = g.finish();
|
||||
if pop.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
let primary = match mapped {
|
||||
Ok(actual) => {
|
||||
std::mem::forget(actual);
|
||||
Ok(())
|
||||
}
|
||||
Err(error) => Err(error),
|
||||
};
|
||||
return combine(primary, pop);
|
||||
}
|
||||
if original && mapped.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
}
|
||||
*o.mapped.get_mut() = Some(mapped?);
|
||||
o.scratch = Some(Rc::new(c.allocate(o.original_layout.as_ref().map_or(o.layout.bytes, |l| l.bytes))?));
|
||||
if original {
|
||||
for (i, n) in sizes.into_iter().enumerate() {
|
||||
if n != 0 {
|
||||
o.extras[i] = Some(c.allocate(n)?);
|
||||
}
|
||||
}
|
||||
}
|
||||
let g = c.enter().inspect_err(|_| {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
})?;
|
||||
// cuMemsetD8_v2 on device memory is asynchronous with respect to the host (it only orders on the legacy
|
||||
// default stream). Fence it inside the same guard: the non-original layout has no later synchronous copy
|
||||
// (the original one does, `copy_from_range` below), and the first kernel of a non-blocking stream must not
|
||||
// race the zeroing. Same pattern as `zero_state` + `synchronize` in mtp_original_ring_state.rs.
|
||||
let zero = o.modules[0]
|
||||
.zero(
|
||||
o.scratch
|
||||
.as_ref()
|
||||
.ok_or(Error::ContextPoisoned)?
|
||||
.handle
|
||||
.ok_or(Error::ContextPoisoned)?
|
||||
.qwen_address(),
|
||||
o.original_layout.as_ref().map_or(o.layout.bytes, |l| l.bytes),
|
||||
)
|
||||
.and_then(|()| c.driver.api.synchronize());
|
||||
let pop = g.finish();
|
||||
if zero.is_err() || pop.is_err() {
|
||||
c.poison();
|
||||
c.retain_driver();
|
||||
}
|
||||
combine(zero, pop)?;
|
||||
if let Some(l) = &o.original_layout {
|
||||
let mut ids = Vec::new();
|
||||
ids.try_reserve_exact(l.t * l.cap * 4).map_err(|_| Error::HostAllocation)?;
|
||||
for _ in 0..l.t {
|
||||
for i in 0..l.cap {
|
||||
ids.extend_from_slice(&(i as i32).to_le_bytes());
|
||||
}
|
||||
}
|
||||
Rc::get_mut(o.scratch.as_mut().ok_or(Error::ContextPoisoned)?)
|
||||
.ok_or(Error::InvalidQwenShape)?
|
||||
.copy_from_range(l.offsets[4], &ids)?;
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
if let Err(e) = op {
|
||||
if let Err(e) = o.init_device(c, (original, max_t, coupled), sizes) {
|
||||
return combine(Err(e), o.close());
|
||||
}
|
||||
Ok(Self { inner: Rc::new(o) })
|
||||
|
||||
Reference in New Issue
Block a user