STRA-338 size: cuda-driver OriginalMtpPrefill::project split into tile/grid/operand/launch helpers (<= 30 lines)
This commit is contained in:
+94
-69
@@ -20,6 +20,33 @@ impl<'ctx> OriginalMtpPrefill<'ctx> {
|
||||
if rows == 0 || rows > r.b * 4 || !matches!(out, 512 | 2560) {
|
||||
return Err(Error::InvalidProjectionShape);
|
||||
}
|
||||
let (index, shared, ntx) = self.project_tile(rows)?;
|
||||
let (grid, fix) = self.project_grid(out, ntx)?;
|
||||
let (xq, ids, bound) = Self::project_operands(r, rows, bound_slot)?;
|
||||
self.project_quantize(cmds, (x, xq), rows, stage)?;
|
||||
let a = (w, y, xq, ids, bound);
|
||||
self.project_launch(cmds, a, (index, shared, grid, fix), (rows, out, ntx))
|
||||
}
|
||||
|
||||
/// The stream-k scratch, the projection kernel and (when the grid leaves partial tiles) the fixup kernel.
|
||||
fn project_launch(
|
||||
&self,
|
||||
cmds: &mut Vec<Cmd>,
|
||||
(w, y, xq, ids, bound): (u64, u64, u64, u64, u64),
|
||||
(index, shared, grid, fix): (usize, usize, usize, bool),
|
||||
(rows, out, ntx): (usize, usize, usize),
|
||||
) -> Result<()> {
|
||||
let tmp = self.project_tmp(fix)?;
|
||||
let a = (w, y, xq, ids, bound, tmp);
|
||||
Self::project_gemm(cmds, a, (index, shared, grid), (rows, out, ntx))?;
|
||||
if fix {
|
||||
Self::project_fixup(cmds, (ids, bound, y, tmp), (index, grid), (rows, out, ntx))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The widest-enough tile: `(width index, shared bytes, tiles along the token axis)`.
|
||||
fn project_tile(&self, rows: usize) -> Result<(usize, usize, usize)> {
|
||||
let mut best = None;
|
||||
let mut ntx = usize::MAX;
|
||||
for (i, j) in WIDTHS.into_iter().enumerate() {
|
||||
@@ -31,21 +58,35 @@ impl<'ctx> OriginalMtpPrefill<'ctx> {
|
||||
continue;
|
||||
}
|
||||
if rows.div_ceil(j) < ntx {
|
||||
best = Some((i, j, shared));
|
||||
best = Some((i, shared));
|
||||
ntx = rows.div_ceil(j);
|
||||
}
|
||||
}
|
||||
let (index, j, shared) = best.ok_or(Error::UnsupportedProjection)?;
|
||||
let (index, shared) = best.ok_or(Error::UnsupportedProjection)?;
|
||||
Ok((index, shared, ntx))
|
||||
}
|
||||
|
||||
/// `(grid, needs fixup)`: complete tiles, or the stream-k grid when the tile count fills the SMs poorly.
|
||||
fn project_grid(&self, out: usize, ntx: usize) -> Result<(usize, bool)> {
|
||||
let tiles = (out / 128) * ntx;
|
||||
let efficiency = 100 * tiles / (self.sms * tiles.div_ceil(self.sms));
|
||||
let grid = if efficiency >= 90 || crate::arch_ptx::stream_k_off() { tiles } else { self.sms };
|
||||
let fix = !tiles.is_multiple_of(grid);
|
||||
if tiles.checked_mul(80).is_none_or(|n| n >= 1 << 30) {
|
||||
return Err(Error::InvalidProjectionShape);
|
||||
}
|
||||
Ok((grid, !tiles.is_multiple_of(grid)))
|
||||
}
|
||||
|
||||
/// `(xq, ids, bound)` device addresses of the quantized activations, row ids and expert bound.
|
||||
fn project_operands(r: &Region, rows: usize, bound_slot: usize) -> Result<(u64, u64, u64)> {
|
||||
let xq = r.address(17, q8_bytes(rows)?)?;
|
||||
let ids = r.address(18, rows * 4)?;
|
||||
let bound = r.handle(19, 16)?.checked_subspan(16, bound_slot * 8, 8)?.qwen_address();
|
||||
Ok((xq, ids, bound))
|
||||
}
|
||||
|
||||
/// Quantizes `x` into `xq`, zeroes the 128-block tail and scans the result.
|
||||
fn project_quantize(&self, cmds: &mut Vec<Cmd>, (x, xq): (u64, u64), rows: usize, stage: usize) -> Result<()> {
|
||||
Self::kernel(
|
||||
cmds,
|
||||
2,
|
||||
@@ -67,6 +108,10 @@ impl<'ctx> OriginalMtpPrefill<'ctx> {
|
||||
A::I32(1),
|
||||
],
|
||||
)?;
|
||||
self.project_scan(cmds, xq, rows, stage)
|
||||
}
|
||||
|
||||
fn project_scan(&self, cmds: &mut Vec<Cmd>, xq: u64, rows: usize, stage: usize) -> Result<()> {
|
||||
// Original D4 data includes a fixed128-block tail. Initialize that exact
|
||||
// tail before a literal vendor kernel can speculatively load it.
|
||||
Self::kernel(
|
||||
@@ -86,72 +131,52 @@ impl<'ctx> OriginalMtpPrefill<'ctx> {
|
||||
[256, 1, 1],
|
||||
0,
|
||||
vec![A::U64(xq), A::I32((rows * 20) as i32), A::U64(self.status(stage)?.qwen_address())],
|
||||
)?;
|
||||
let tmp = if fix {
|
||||
self.tmp.as_ref().ok_or(Error::ContextPoisoned)?.handle.ok_or(Error::ContextPoisoned)?.qwen_address()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let fd = fastdiv;
|
||||
Self::kernel(
|
||||
cmds,
|
||||
3,
|
||||
index,
|
||||
[grid as u32, 1, 1],
|
||||
[32, 8, 1],
|
||||
shared as u32,
|
||||
vec![
|
||||
A::U64(w),
|
||||
A::U64(xq),
|
||||
A::U64(ids),
|
||||
A::U64(bound),
|
||||
A::U64(y),
|
||||
A::U64(tmp),
|
||||
A::U64(0),
|
||||
A::U3(fd(80)),
|
||||
A::I32(out as i32),
|
||||
A::I32(rows as i32),
|
||||
A::I32(80),
|
||||
A::I32(rows as i32),
|
||||
A::I32(out as i32),
|
||||
A::U3(fd(1)),
|
||||
A::U3(fd(1)),
|
||||
A::I32((out * 80) as i32),
|
||||
A::I32(0),
|
||||
A::I32(0),
|
||||
A::U3(fd(1)),
|
||||
A::U3(fd(1)),
|
||||
A::I32(0),
|
||||
A::I32(0),
|
||||
A::I32(0),
|
||||
A::U3(fd(ntx as u32)),
|
||||
],
|
||||
)?;
|
||||
if fix {
|
||||
Self::kernel(
|
||||
cmds,
|
||||
4,
|
||||
index,
|
||||
[grid as u32, 4, 1],
|
||||
[32, 4, 1],
|
||||
0,
|
||||
vec![
|
||||
A::U64(ids),
|
||||
A::U64(bound),
|
||||
A::U64(y),
|
||||
A::U64(tmp),
|
||||
A::U3(fd(80)),
|
||||
A::I32(out as i32),
|
||||
A::I32(rows as i32),
|
||||
A::I32(out as i32),
|
||||
A::U3(fd(1)),
|
||||
A::I32(0),
|
||||
A::U3(fd(1)),
|
||||
A::I32(0),
|
||||
A::U3(fd(ntx as u32)),
|
||||
],
|
||||
)?;
|
||||
)
|
||||
}
|
||||
|
||||
/// The stream-k fixup scratch address, or zero when no fixup runs.
|
||||
fn project_tmp(&self, fix: bool) -> Result<u64> {
|
||||
if !fix {
|
||||
return Ok(0);
|
||||
}
|
||||
Ok(())
|
||||
Ok(self.tmp.as_ref().ok_or(Error::ContextPoisoned)?.handle.ok_or(Error::ContextPoisoned)?.qwen_address())
|
||||
}
|
||||
|
||||
fn project_gemm(
|
||||
cmds: &mut Vec<Cmd>,
|
||||
(w, y, xq, ids, bound, tmp): (u64, u64, u64, u64, u64, u64),
|
||||
(index, shared, grid): (usize, usize, usize),
|
||||
(rows, out, ntx): (usize, usize, usize),
|
||||
) -> Result<()> {
|
||||
let fd = fastdiv;
|
||||
let mut args = Vec::with_capacity(24);
|
||||
args.extend([
|
||||
A::U64(w),
|
||||
A::U64(xq),
|
||||
A::U64(ids),
|
||||
A::U64(bound),
|
||||
A::U64(y),
|
||||
A::U64(tmp),
|
||||
A::U64(0),
|
||||
A::U3(fd(80)),
|
||||
]);
|
||||
args.extend([A::I32(out as i32), A::I32(rows as i32), A::I32(80), A::I32(rows as i32), A::I32(out as i32)]);
|
||||
args.extend([A::U3(fd(1)), A::U3(fd(1)), A::I32((out * 80) as i32), A::I32(0), A::I32(0), A::U3(fd(1))]);
|
||||
args.extend([A::U3(fd(1)), A::I32(0), A::I32(0), A::I32(0), A::U3(fd(ntx as u32))]);
|
||||
Self::kernel(cmds, 3, index, [grid as u32, 1, 1], [32, 8, 1], shared as u32, args)
|
||||
}
|
||||
|
||||
fn project_fixup(
|
||||
cmds: &mut Vec<Cmd>,
|
||||
(ids, bound, y, tmp): (u64, u64, u64, u64),
|
||||
(index, grid): (usize, usize),
|
||||
(rows, out, ntx): (usize, usize, usize),
|
||||
) -> Result<()> {
|
||||
let fd = fastdiv;
|
||||
let mut args = Vec::with_capacity(13);
|
||||
args.extend([A::U64(ids), A::U64(bound), A::U64(y), A::U64(tmp), A::U3(fd(80)), A::I32(out as i32)]);
|
||||
args.extend([A::I32(rows as i32), A::I32(out as i32), A::U3(fd(1)), A::I32(0), A::U3(fd(1)), A::I32(0)]);
|
||||
args.push(A::U3(fd(ntx as u32)));
|
||||
Self::kernel(cmds, 4, index, [grid as u32, 4, 1], [32, 4, 1], 0, args)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user