STRA-338 size: cuda-driver GrMicrobatchRead::read/micro_launch end <= 30 lines, complexity <= 10
This commit is contained in:
+87
-44
@@ -16,8 +16,22 @@ impl<'ctx> GrMicrobatchRead<'ctx> {
|
||||
let zero = vec![0u8; 32 * self.tokens];
|
||||
let mut readback = vec![0u8; 32 * self.tokens];
|
||||
let guard = self.context.enter()?;
|
||||
let kernels = match self.micro_kernels() {
|
||||
Ok(kernels) => kernels,
|
||||
let (kernels, guard) = self.micro_kernels_guarded(guard)?;
|
||||
let (commands, completed_scans, status_read) =
|
||||
self.micro_enqueue(&kernels, (&mut arguments, &mut scans), (&zero, &mut readback))?;
|
||||
let sync = self.context.driver.api.synchronize();
|
||||
let pop = guard.finish();
|
||||
let keep = (arguments, scans, zero, readback);
|
||||
self.micro_complete((commands, sync, pop), keep, (status_read, completed_scans))
|
||||
}
|
||||
|
||||
/// The registered kernels; a failure pops the context guard first (and fails the read when the pop fails).
|
||||
fn micro_kernels_guarded<'g>(
|
||||
&mut self,
|
||||
guard: crate::CurrentGuard<'g, 'ctx>,
|
||||
) -> Result<(Vec<FixedKernel>, crate::CurrentGuard<'g, 'ctx>)> {
|
||||
match self.micro_kernels() {
|
||||
Ok(kernels) => Ok((kernels, guard)),
|
||||
Err(error) => {
|
||||
let pop = guard.finish();
|
||||
if pop.is_err() {
|
||||
@@ -25,40 +39,63 @@ impl<'ctx> GrMicrobatchRead<'ctx> {
|
||||
self.context.poison();
|
||||
self.context.retain_driver();
|
||||
}
|
||||
return combine(Err(error), pop);
|
||||
combine(Err(error), pop)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Resets the status words, enqueues all rows and reads the status back; returns the enqueue status, the
|
||||
/// number of scans enqueued and whether the status readback succeeded.
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn micro_enqueue(
|
||||
&self,
|
||||
kernels: &[FixedKernel],
|
||||
(arguments, scans): (&mut [[Arguments; 6]], &mut [ffi::MmqResidentArguments]),
|
||||
(zero, readback): (&[u8], &mut [u8]),
|
||||
) -> Result<(Result<()>, usize, bool)> {
|
||||
let status = self.buffer(10)?.handle.ok_or(Error::ContextPoisoned)?;
|
||||
let finite = self.finite.ok_or(Error::ContextPoisoned)?;
|
||||
let commands = self.context.driver.api.copy_from(status, &zero);
|
||||
let commands = self.context.driver.api.copy_from(status, zero);
|
||||
let initialized = commands.is_ok();
|
||||
let (commands, completed_scans) = self.micro_launch(&kernels, finite, (&mut arguments, &mut scans), commands);
|
||||
let read_status = if initialized { self.context.driver.api.copy_to(status, &mut readback) } else { Ok(()) };
|
||||
let (commands, completed_scans) = self.micro_launch(kernels, finite, (arguments, scans), commands);
|
||||
let read_status = if initialized { self.context.driver.api.copy_to(status, readback) } else { Ok(()) };
|
||||
let status_read = initialized && read_status.is_ok();
|
||||
let commands = combine(commands, read_status);
|
||||
let sync = self.context.driver.api.synchronize();
|
||||
let pop = guard.finish();
|
||||
Ok((combine(commands, read_status), completed_scans, status_read))
|
||||
}
|
||||
|
||||
/// Settles the drain: a failed sync or pop leaves the owned host ABI leaked; otherwise the finite statuses
|
||||
/// are checked before any enqueue error and the products are decoded.
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn micro_complete(
|
||||
&mut self,
|
||||
(commands, sync, pop): (Result<()>, Result<()>, Result<()>),
|
||||
keep: (Vec<[Arguments; 6]>, Vec<ffi::MmqResidentArguments>, Vec<u8>, Vec<u8>),
|
||||
(status_read, completed_scans): (bool, usize),
|
||||
) -> Result<GrMicroValues> {
|
||||
if commands.is_err() || sync.is_err() || pop.is_err() {
|
||||
self.failed = true;
|
||||
}
|
||||
if sync.is_err() || pop.is_err() {
|
||||
self.context.poison();
|
||||
self.context.retain_driver();
|
||||
std::mem::forget((arguments, scans, zero, readback));
|
||||
} else {
|
||||
// A later enqueue failure cannot replace an earlier completed
|
||||
// numeric refusal. Only actually enqueued scans, successful
|
||||
// status readback and confirmed sync/pop authorize this ordering.
|
||||
if status_read {
|
||||
self.micro_check_flags(&readback, completed_scans)?;
|
||||
}
|
||||
combine(combine(commands, sync), pop)?;
|
||||
let mixed = decode(self.buffer(6)?)?;
|
||||
let inject = decode(self.buffer(7)?)?;
|
||||
self.phase = 1;
|
||||
return Ok(GrMicroValues { mixed, inject });
|
||||
std::mem::forget(keep);
|
||||
return combine(combine(commands, sync), pop).map(|()| unreachable!("uncertain completion is an error"));
|
||||
}
|
||||
combine(combine(commands, sync), pop).map(|()| unreachable!("uncertain completion is an error"))
|
||||
// A later enqueue failure cannot replace an earlier completed
|
||||
// numeric refusal. Only actually enqueued scans, successful
|
||||
// status readback and confirmed sync/pop authorize this ordering.
|
||||
if status_read {
|
||||
self.micro_check_flags(&keep.3, completed_scans)?;
|
||||
}
|
||||
combine(combine(commands, sync), pop)?;
|
||||
self.micro_values()
|
||||
}
|
||||
|
||||
fn micro_values(&mut self) -> Result<GrMicroValues> {
|
||||
let mixed = decode(self.buffer(6)?)?;
|
||||
let inject = decode(self.buffer(7)?)?;
|
||||
self.phase = 1;
|
||||
Ok(GrMicroValues { mixed, inject })
|
||||
}
|
||||
|
||||
/// Per-row kernel arguments and the seven finite scans of every row.
|
||||
@@ -117,26 +154,8 @@ impl<'ctx> GrMicrobatchRead<'ctx> {
|
||||
if commands.is_err() {
|
||||
break;
|
||||
}
|
||||
commands =
|
||||
self.context.driver.api.launch_qwen(kernels[stage].function, OPS[stage], &mut row_args[stage]);
|
||||
if commands.is_ok() {
|
||||
let scan_stage = if stage == 5 { 6 } else { stage };
|
||||
let words = SIZES[[1, 2, 3, 4, 5, 7][stage]] / 4;
|
||||
commands = self.context.driver.api.launch_mmq_resident(
|
||||
finite,
|
||||
words as u32,
|
||||
&mut scans[row * 7 + scan_stage],
|
||||
);
|
||||
if commands.is_ok() {
|
||||
completed_scans += 1;
|
||||
}
|
||||
}
|
||||
if stage == 4 && commands.is_ok() {
|
||||
commands = self.context.driver.api.launch_mmq_resident(finite, 2560, &mut scans[row * 7 + 5]);
|
||||
if commands.is_ok() {
|
||||
completed_scans += 1;
|
||||
}
|
||||
}
|
||||
let args = (&mut row_args[stage], &mut *scans);
|
||||
commands = self.micro_stage(kernels, finite, (row, stage), args, &mut completed_scans);
|
||||
}
|
||||
if commands.is_err() {
|
||||
break;
|
||||
@@ -145,6 +164,30 @@ impl<'ctx> GrMicrobatchRead<'ctx> {
|
||||
(commands, completed_scans)
|
||||
}
|
||||
|
||||
/// One kernel of a row and its finite scan (the products stage carries a second scan).
|
||||
fn micro_stage(
|
||||
&self,
|
||||
kernels: &[FixedKernel],
|
||||
finite: ffi::FunctionHandle,
|
||||
(row, stage): (usize, usize),
|
||||
(args, scans): (&mut Arguments, &mut [ffi::MmqResidentArguments]),
|
||||
completed_scans: &mut usize,
|
||||
) -> Result<()> {
|
||||
let mut commands = self.context.driver.api.launch_qwen(kernels[stage].function, OPS[stage], args);
|
||||
if commands.is_ok() {
|
||||
let scan_stage = if stage == 5 { 6 } else { stage };
|
||||
let words = SIZES[[1, 2, 3, 4, 5, 7][stage]] / 4;
|
||||
commands =
|
||||
self.context.driver.api.launch_mmq_resident(finite, words as u32, &mut scans[row * 7 + scan_stage]);
|
||||
*completed_scans += usize::from(commands.is_ok());
|
||||
}
|
||||
if stage == 4 && commands.is_ok() {
|
||||
commands = self.context.driver.api.launch_mmq_resident(finite, 2560, &mut scans[row * 7 + 5]);
|
||||
*completed_scans += usize::from(commands.is_ok());
|
||||
}
|
||||
commands
|
||||
}
|
||||
|
||||
/// Reports the first non-zero finite status among the scans that were actually enqueued.
|
||||
fn micro_check_flags(&mut self, readback: &[u8], completed_scans: usize) -> Result<()> {
|
||||
let mut scan_index = 0usize;
|
||||
|
||||
Reference in New Issue
Block a user