STRA-338 size: cuda-driver OriginalMtpForwardProgram capture/validate_flags/stage_coupled <= 30 lines, complexity <= 10
This commit is contained in:
+80
-48
@@ -10,6 +10,23 @@ impl OriginalMtpForwardProgram<'_> {
|
||||
self.validate_context(self.context)?;
|
||||
capture.validate_context(self.context)?;
|
||||
let stream = capture.stream_handle(self.context)?;
|
||||
self.capture_prefix(capture, stream)?;
|
||||
// These fixed device transports exactly occupy Original's Q split and
|
||||
// window_ids positions in record_forward, before attention launch.
|
||||
for command in self.tail_commands.iter_mut() {
|
||||
Self::launch_tail_command(
|
||||
(&self.tail, &self.ring),
|
||||
(&self.args.split, &mut self.args.window),
|
||||
stream,
|
||||
command,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The coupled sampler stage, the tail flag reset and the scalar prefix commands (the native embedding is
|
||||
/// captured before the second one).
|
||||
fn capture_prefix(&mut self, capture: &mut OriginalCapture<'_, '_>, stream: usize) -> Result<()> {
|
||||
if let Some(stage) = &mut self.coupled_stage {
|
||||
self.tail.modules[20].launch(1, [1, 1, 1], [256, 1, 1], 0, stream, stage)?;
|
||||
}
|
||||
@@ -20,40 +37,49 @@ impl OriginalMtpForwardProgram<'_> {
|
||||
{
|
||||
e.capture(capture)?;
|
||||
}
|
||||
match command {
|
||||
ScalarCommand::Kernel { asset, index, grid, block, shared, args } => {
|
||||
self.owners.modules[*asset].launch(*index, *grid, *block, *shared, stream, args)?
|
||||
}
|
||||
ScalarCommand::Copy { .. } => return Err(Error::InvalidQwenShape),
|
||||
}
|
||||
}
|
||||
// These fixed device transports exactly occupy Original's Q split and
|
||||
// window_ids positions in record_forward, before attention launch.
|
||||
for command in self.tail_commands.iter_mut() {
|
||||
if command.asset == 16 && command.index == 0 {
|
||||
self.tail.modules[13].copy2d(&self.args.split, stream)?;
|
||||
}
|
||||
if (command.asset == 19 || command.asset == symbols::DAILY_ASSET) && command.index == 0 {
|
||||
self.ring.physical.modules[1].as_ref().ok_or(Error::ContextPoisoned)?.launch(
|
||||
2,
|
||||
[8, 1, 1],
|
||||
[256, 1, 1],
|
||||
0,
|
||||
stream,
|
||||
&mut self.args.window,
|
||||
)?;
|
||||
}
|
||||
self.tail.modules[command.asset].launch(
|
||||
command.index,
|
||||
command.grid,
|
||||
command.block,
|
||||
command.shared,
|
||||
stream,
|
||||
&mut command.args,
|
||||
)?;
|
||||
Self::launch_scalar(&self.owners.modules, stream, command)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn launch_scalar(modules: &[Rc<Module>; 6], stream: usize, command: &mut ScalarCommand) -> Result<()> {
|
||||
match command {
|
||||
ScalarCommand::Kernel { asset, index, grid, block, shared, args } => {
|
||||
modules[*asset].launch(*index, *grid, *block, *shared, stream, args)
|
||||
}
|
||||
ScalarCommand::Copy { .. } => Err(Error::InvalidQwenShape),
|
||||
}
|
||||
}
|
||||
|
||||
/// One tail command, preceded by the Q split copy or the window-ids transport where Original puts them.
|
||||
fn launch_tail_command(
|
||||
(tail, ring): (&FullOwned<'_>, &MtpOriginalRingResources<'_>),
|
||||
(split, window): (&full_abi::Copy2D, &mut abi::Arguments),
|
||||
stream: usize,
|
||||
command: &mut FullCommand,
|
||||
) -> Result<()> {
|
||||
if command.asset == 16 && command.index == 0 {
|
||||
tail.modules[13].copy2d(split, stream)?;
|
||||
}
|
||||
if (command.asset == 19 || command.asset == symbols::DAILY_ASSET) && command.index == 0 {
|
||||
ring.physical.modules[1].as_ref().ok_or(Error::ContextPoisoned)?.launch(
|
||||
2,
|
||||
[8, 1, 1],
|
||||
[256, 1, 1],
|
||||
0,
|
||||
stream,
|
||||
window,
|
||||
)?;
|
||||
}
|
||||
tail.modules[command.asset].launch(
|
||||
command.index,
|
||||
command.grid,
|
||||
command.block,
|
||||
command.shared,
|
||||
stream,
|
||||
&mut command.args,
|
||||
)
|
||||
}
|
||||
pub fn metadata_bytes(&self) -> Result<usize> {
|
||||
let mut n = self.args.window.metadata_bytes()
|
||||
+ self.args.reset.metadata_bytes()
|
||||
@@ -85,17 +111,7 @@ impl OriginalMtpForwardProgram<'_> {
|
||||
self.context.poison();
|
||||
self.context.retain_driver();
|
||||
})?;
|
||||
let copies = (|| {
|
||||
self.context.driver.api.copy_to(
|
||||
self.owners.status.handle.ok_or(Error::ContextPoisoned)?.checked_subspan(
|
||||
self.owners.status.bytes,
|
||||
0,
|
||||
64,
|
||||
)?,
|
||||
&mut prefix,
|
||||
)?;
|
||||
self.context.driver.api.copy_to(h, &mut flags)
|
||||
})();
|
||||
let copies = self.copy_flags(h, &mut prefix, &mut flags);
|
||||
let pop = g.finish();
|
||||
if copies.is_err() || pop.is_err() {
|
||||
self.tail.failed.set(true);
|
||||
@@ -112,6 +128,13 @@ impl OriginalMtpForwardProgram<'_> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reads the prefix status words and the tail flags back.
|
||||
fn copy_flags(&self, h: crate::ffi::BufferHandle, prefix: &mut [u8; 64], flags: &mut [u8]) -> Result<()> {
|
||||
let status = self.owners.status.handle.ok_or(Error::ContextPoisoned)?;
|
||||
self.context.driver.api.copy_to(status.checked_subspan(self.owners.status.bytes, 0, 64)?, prefix)?;
|
||||
self.context.driver.api.copy_to(h, flags)
|
||||
}
|
||||
pub(crate) fn output_after_actual_drain(&self) -> Result<OriginalMtpStepOutput> {
|
||||
self.validate_flags_after_actual_drain()?;
|
||||
let mapped_borrow = self.tail.mapped.try_borrow().map_err(|_| Error::InvalidQwenShape)?;
|
||||
@@ -147,16 +170,25 @@ impl OriginalMtpForwardProgram<'_> {
|
||||
if history.iter().any(|id| *id < -1 || *id >= 0 && *id as usize >= self.tail.weights.id_vocab) {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
let mut bytes = [0u8; 4096 * 4];
|
||||
for (i, id) in history.iter().enumerate() {
|
||||
bytes[i * 4..i * 4 + 4].copy_from_slice(&id.to_le_bytes());
|
||||
}
|
||||
self.write_coupled(¶ms.bytes(), &history_bytes(history))
|
||||
}
|
||||
|
||||
fn write_coupled(&self, params: &[u8], history: &[u8; 4096 * 4]) -> Result<()> {
|
||||
let mut borrow = self.tail.mapped.try_borrow_mut().map_err(|_| Error::InvalidQwenShape)?;
|
||||
let mapped = borrow.as_mut().ok_or(Error::ContextPoisoned)?;
|
||||
let base = self.tail.max_t * 236 + 384;
|
||||
mapped.write(base, ¶ms.bytes())?;
|
||||
mapped.write(base + 64, &bytes)?;
|
||||
mapped.write(base, params)?;
|
||||
mapped.write(base + 64, history)?;
|
||||
std::sync::atomic::fence(std::sync::atomic::Ordering::SeqCst);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The history ids as little-endian words.
|
||||
fn history_bytes(history: &[i32; 4096]) -> [u8; 4096 * 4] {
|
||||
let mut bytes = [0u8; 4096 * 4];
|
||||
for (i, id) in history.iter().enumerate() {
|
||||
bytes[i * 4..i * 4 + 4].copy_from_slice(&id.to_le_bytes());
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user