STRA-338 size: cuda-driver qsa_prompt_index: split run phases into run_phases and run_launches, helpers within limits

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 13:41:45 +01:00
parent 79f49258ae
commit 6f1e20e4e1
3 changed files with 292 additions and 226 deletions
@@ -83,4 +83,5 @@ mod impl_qsapromptindexworkspace;
mod impl_qsapromptindexworkspace_2;
mod impl_qsapromptindexworkspace_3;
mod impl_qsapromptindexworkspace_4;
mod run_launches;
mod run_phases;
@@ -0,0 +1,267 @@
//! STRA-338 size: the launch builders of `QsaPromptIndexWorkspace::run`, one function per phase.
use super::run_phases::{Commands, RunPlan, push_launch};
#[allow(unused_imports)]
use super::*;
struct ScoreTile {
count: usize,
reach: usize,
iq: ffi::BufferHandle,
steps: ffi::BufferHandle,
ids: ffi::BufferHandle,
}
pub(super) fn push_scan(
commands: &mut Commands,
plan: &RunPlan,
buf: ffi::BufferHandle,
count: usize,
which: u32,
) -> Result<()> {
push_launch(
commands,
Launch::Scan(count as u32),
vec![A::Address(buf), A::I64(count as i64), A::Address(plan.status), A::U32(which)],
)
}
pub(super) fn push_norm_rope(
commands: &mut Commands,
plan: &RunPlan,
bufs: (ffi::BufferHandle, ffi::BufferHandle),
shape: (u32, i32, i64),
) -> Result<()> {
match plan.positions {
Some(positions) => push_decode_norm_rope(commands, plan, positions, bufs, shape),
None => push_prompt_norm_rope(commands, plan, bufs, shape),
}
}
fn push_decode_norm_rope(
commands: &mut Commands,
plan: &RunPlan,
positions: ffi::BufferHandle,
(buf, gamma): (ffi::BufferHandle, ffi::BufferHandle),
(heads, dim, _): (u32, i32, i64),
) -> Result<()> {
let rms = vec![A::Address(buf), A::Address(gamma), A::Address(buf), A::I32(dim), A::F32(1e-6)];
push_launch(commands, Launch::DecodeRms(heads), rms)?;
let rope = vec![
A::Address(buf),
A::Address(buf),
A::I32(heads as i32),
A::I32(dim),
A::F32(plan.theta),
A::Address(positions),
];
push_launch(commands, Launch::DecodeRope(heads), rope)
}
fn push_prompt_norm_rope(
commands: &mut Commands,
plan: &RunPlan,
(buf, gamma): (ffi::BufferHandle, ffi::BufferHandle),
(heads, dim, stride): (u32, i32, i64),
) -> Result<()> {
let rms = vec![A::Address(buf), A::Address(gamma), A::I64(dim as i64), A::I64(dim as i64), A::F32(1e-6)];
push_launch(commands, Launch::Rms(plan.t * heads), rms)?;
let rope = vec![
A::Address(buf),
A::I64(heads as i64),
A::I64(dim as i64),
A::I64(stride),
A::I64(plan.p0 as i64),
A::F32(plan.theta),
];
push_launch(commands, Launch::Rope(plan.t * heads), rope)
}
impl<'ctx> QsaPromptIndexWorkspace<'ctx> {
pub(super) fn push_pool(
&self,
commands: &mut Commands,
plan: &RunPlan,
gamma: ffi::BufferHandle,
out4: ffi::BufferHandle,
) -> Result<()> {
if plan.decode {
self.push_pool_decode(commands, plan, gamma, out4)?;
} else {
self.push_pool_prompt(commands, plan, gamma)?;
}
let nb = (plan.p0 as usize + plan.t as usize) / 4;
for (h, count) in [(plan.pooled, (nb + 1) * 128), (plan.tail, 384), (plan.dead, 128)] {
push_scan(commands, plan, h, count, 3)?;
}
Ok(())
}
fn push_pool_decode(
&self,
commands: &mut Commands,
plan: &RunPlan,
gamma: ffi::BufferHandle,
out4: ffi::BufferHandle,
) -> Result<()> {
let args = vec![
A::Address(plan.raw),
A::Address(out4),
A::I32(0),
A::Address(gamma),
A::F32(1e-6),
A::Address(plan.tail),
A::Address(plan.dead),
A::Address(plan.pooled),
A::Address(self.handle(3, 4)?),
A::I32(crate::serving_context::geometry().cells() as i32),
A::F32(plan.theta),
];
push_launch(commands, Launch::DecodeAppend, args)
}
fn push_pool_prompt(&self, commands: &mut Commands, plan: &RunPlan, gamma: ffi::BufferHandle) -> Result<()> {
let (t, p0) = (plan.t, plan.p0);
let p = p0 as usize;
let end = p + t as usize;
if p == 0 {
push_pool_first(commands, plan, gamma)?;
}
let first = if p <= 3 { 0 } else { (p - 3).div_ceil(4) };
let hi = if end - 1 >= 3 { Some((end - 4) / 4) } else { None };
if let Some(hi) = hi.filter(|hi| *hi >= first) {
self.push_pool_blocks(commands, plan, gamma, (first, hi))?;
}
let tail = vec![A::Address(plan.raw), A::I64(t as i64), A::I64(p0 as i64), A::Address(plan.tail)];
push_launch(commands, Launch::Tail, tail)
}
fn push_pool_blocks(
&self,
commands: &mut Commands,
plan: &RunPlan,
gamma: ffi::BufferHandle,
(first, hi): (usize, usize),
) -> Result<()> {
let args = vec![
A::Address(plan.raw),
A::I64(plan.t as i64),
A::I64(plan.p0 as i64),
A::I32(0),
A::Address(gamma),
A::F32(1e-6),
A::Address(plan.tail),
A::Address(plan.dead),
A::Address(plan.pooled),
A::Address(self.handle(3, 4)?),
A::I64(first as i64),
A::I64(hi as i64),
A::F32(plan.theta),
];
push_launch(commands, Launch::Blocks((hi - first + 1) as u32), args)
}
pub(super) fn push_scores(
&self,
commands: &mut Commands,
plan: &RunPlan,
out: [ffi::BufferHandle; 5],
) -> Result<()> {
let n = plan.t as usize;
// Original prompt selection reuses one 256-row score tile. Every
// offset is checked against the actual whole producer allocation.
let tile_rows = if plan.decode { 1 } else { n.min(256) };
let scores = self.handle(9, tile_rows * max_blocks() * 4)?;
for first_row in (0..n).step_by(256) {
let count = (n - first_row).min(256);
let tile = ScoreTile {
count,
reach: (plan.p0 as usize + first_row + count) / 4 + 1,
iq: plan.iq.checked_subspan(n * 512 * 4, first_row * 512 * 4, count * 512 * 4)?,
steps: out[4].checked_subspan(n * 16, first_row * 16, count * 16)?,
ids: out[3].checked_subspan(n * CAP * 4, first_row * CAP * 4, count * CAP * 4)?,
};
push_tile_scores(commands, plan, scores, &tile)?;
push_tile_select(commands, plan, scores, &tile)?;
}
Ok(())
}
}
fn push_pool_first(commands: &mut Commands, plan: &RunPlan, gamma: ffi::BufferHandle) -> Result<()> {
let args = vec![
A::Address(plan.raw),
A::Address(gamma),
A::F32(1e-6),
A::Address(plan.dead),
A::Address(plan.pooled),
A::F32(plan.theta),
];
push_launch(commands, Launch::First, args)
}
fn push_tile_scores(
commands: &mut Commands,
plan: &RunPlan,
scores: ffi::BufferHandle,
tile: &ScoreTile,
) -> Result<()> {
if plan.decode {
let args = vec![
A::Address(plan.pooled),
A::Address(plan.dead),
A::Address(tile.iq),
A::Address(tile.steps),
A::I32(1),
A::I64(max_blocks() as i64),
A::Address(scores),
];
return push_launch(commands, Launch::DecodeScores, args);
}
push_tile_tc(commands, plan, scores, tile)?;
let tail = vec![
A::Address(plan.dead),
A::Address(tile.iq),
A::Address(tile.steps),
A::I64(max_blocks() as i64),
A::Address(scores),
];
push_launch(commands, Launch::ScoreTail(tile.count as u32), tail)
}
fn push_tile_tc(commands: &mut Commands, plan: &RunPlan, scores: ffi::BufferHandle, tile: &ScoreTile) -> Result<()> {
let tc = vec![
A::Address(plan.pooled),
A::Address(tile.iq),
A::Address(tile.steps),
A::I64(tile.count as i64),
A::I64(max_blocks() as i64),
A::I64(tile.reach as i64),
A::Address(scores),
];
push_launch(commands, Launch::Tc { t: tile.count as u32, reach: tile.reach as u32 }, tc)
}
fn push_tile_select(
commands: &mut Commands,
plan: &RunPlan,
scores: ffi::BufferHandle,
tile: &ScoreTile,
) -> Result<()> {
let scan = vec![
A::Address(scores),
A::Address(tile.steps),
A::I32(tile.count as i32),
A::I64(max_blocks() as i64),
A::Address(plan.status),
A::U32(4),
];
push_launch(commands, Launch::ScoreScan((tile.count * max_blocks()) as u32), scan)?;
let topk = vec![
A::Address(scores),
A::Address(tile.steps),
A::I64(max_blocks() as i64),
A::I64(CAP as i64),
A::Address(tile.ids),
];
push_launch(commands, Launch::Topk(tile.count as u32), topk)
}
@@ -1,5 +1,6 @@
//! STRA-338 size: the phases of `QsaPromptIndexWorkspace::run`, extracted without reordering any call.
use super::run_launches::{push_norm_rope, push_scan};
#[allow(unused_imports)]
use super::*;
use crate::ple_batch_ffi::Blas;
@@ -34,14 +35,6 @@ pub(super) struct HostStaging {
pub(super) decode_positions: Vec<u8>,
}
struct ScoreTile {
count: usize,
reach: usize,
iq: ffi::BufferHandle,
steps: ffi::BufferHandle,
ids: ffi::BufferHandle,
}
pub(super) fn push_launch(commands: &mut Commands, shape: Launch, args: Vec<A>) -> Result<()> {
commands.push((shape, Arguments::for_launch(shape, args)?));
Ok(())
@@ -67,49 +60,6 @@ pub(super) fn step_bytes(p: usize, n: usize) -> Result<Vec<u8>> {
Ok(steps)
}
fn push_scan(commands: &mut Commands, plan: &RunPlan, buf: ffi::BufferHandle, count: usize, which: u32) -> Result<()> {
push_launch(
commands,
Launch::Scan(count as u32),
vec![A::Address(buf), A::I64(count as i64), A::Address(plan.status), A::U32(which)],
)
}
fn push_norm_rope(
commands: &mut Commands,
plan: &RunPlan,
(buf, gamma): (ffi::BufferHandle, ffi::BufferHandle),
(heads, dim, stride): (u32, i32, i64),
) -> Result<()> {
let theta = plan.theta;
if let Some(positions) = plan.positions {
let rms = vec![A::Address(buf), A::Address(gamma), A::Address(buf), A::I32(dim), A::F32(1e-6)];
push_launch(commands, Launch::DecodeRms(heads), rms)?;
let rope = vec![
A::Address(buf),
A::Address(buf),
A::I32(heads as i32),
A::I32(dim),
A::F32(theta),
A::Address(positions),
];
push_launch(commands, Launch::DecodeRope(heads), rope)?;
} else {
let rms = vec![A::Address(buf), A::Address(gamma), A::I64(dim as i64), A::I64(dim as i64), A::F32(1e-6)];
push_launch(commands, Launch::Rms(plan.t * heads), rms)?;
let rope = vec![
A::Address(buf),
A::I64(heads as i64),
A::I64(dim as i64),
A::I64(stride),
A::I64(plan.p0 as i64),
A::F32(theta),
];
push_launch(commands, Launch::Rope(plan.t * heads), rope)?;
}
Ok(())
}
impl<'ctx> QsaPromptIndexWorkspace<'ctx> {
#[allow(clippy::too_many_arguments)]
pub(super) fn upload_run_inputs(
@@ -219,180 +169,23 @@ impl<'ctx> QsaPromptIndexWorkspace<'ctx> {
push_scan(commands, plan, out[2], n * 6144, 1)?;
push_norm_rope(commands, plan, (plan.iq, gamma[2]), (4, 128, 512))?;
push_scan(commands, plan, plan.iq, n * 512, 2)?;
self.push_pool(commands, plan, gamma[3], out[4])?;
self.push_pool_scores_round(commands, plan, gamma[3], out)
}
fn push_pool_scores_round(
&self,
commands: &mut Commands,
plan: &RunPlan,
gamma: ffi::BufferHandle,
out: [ffi::BufferHandle; 5],
) -> Result<()> {
let n = plan.t as usize;
self.push_pool(commands, plan, gamma, out[4])?;
self.push_scores(commands, plan, out)?;
push_launch(commands, Launch::Round((n * 128) as u32), vec![A::Address(plan.raw), A::I64((n * 128) as i64)])?;
push_scan(commands, plan, plan.raw, n * 128, 5)
}
fn push_pool(
&self,
commands: &mut Commands,
plan: &RunPlan,
gamma: ffi::BufferHandle,
out4: ffi::BufferHandle,
) -> Result<()> {
if plan.decode {
self.push_pool_decode(commands, plan, gamma, out4)?;
} else {
self.push_pool_prompt(commands, plan, gamma)?;
}
let nb = (plan.p0 as usize + plan.t as usize) / 4;
for (h, count) in [(plan.pooled, (nb + 1) * 128), (plan.tail, 384), (plan.dead, 128)] {
push_scan(commands, plan, h, count, 3)?;
}
Ok(())
}
fn push_pool_decode(
&self,
commands: &mut Commands,
plan: &RunPlan,
gamma: ffi::BufferHandle,
out4: ffi::BufferHandle,
) -> Result<()> {
let args = vec![
A::Address(plan.raw),
A::Address(out4),
A::I32(0),
A::Address(gamma),
A::F32(1e-6),
A::Address(plan.tail),
A::Address(plan.dead),
A::Address(plan.pooled),
A::Address(self.handle(3, 4)?),
A::I32(crate::serving_context::geometry().cells() as i32),
A::F32(plan.theta),
];
push_launch(commands, Launch::DecodeAppend, args)
}
fn push_pool_prompt(&self, commands: &mut Commands, plan: &RunPlan, gamma: ffi::BufferHandle) -> Result<()> {
let (t, p0) = (plan.t, plan.p0);
let p = p0 as usize;
let end = p + t as usize;
if p == 0 {
let args = vec![
A::Address(plan.raw),
A::Address(gamma),
A::F32(1e-6),
A::Address(plan.dead),
A::Address(plan.pooled),
A::F32(plan.theta),
];
push_launch(commands, Launch::First, args)?;
}
let first = if p <= 3 { 0 } else { (p - 3).div_ceil(4) };
let hi = if end - 1 >= 3 { Some((end - 4) / 4) } else { None };
if let Some(hi) = hi.filter(|hi| *hi >= first) {
let args = vec![
A::Address(plan.raw),
A::I64(t as i64),
A::I64(p0 as i64),
A::I32(0),
A::Address(gamma),
A::F32(1e-6),
A::Address(plan.tail),
A::Address(plan.dead),
A::Address(plan.pooled),
A::Address(self.handle(3, 4)?),
A::I64(first as i64),
A::I64(hi as i64),
A::F32(plan.theta),
];
push_launch(commands, Launch::Blocks((hi - first + 1) as u32), args)?;
}
let tail = vec![A::Address(plan.raw), A::I64(t as i64), A::I64(p0 as i64), A::Address(plan.tail)];
push_launch(commands, Launch::Tail, tail)
}
fn push_scores(&self, commands: &mut Commands, plan: &RunPlan, out: [ffi::BufferHandle; 5]) -> Result<()> {
let n = plan.t as usize;
// Original prompt selection reuses one 256-row score tile. Every
// offset is checked against the actual whole producer allocation.
let tile_rows = if plan.decode { 1 } else { n.min(256) };
let scores = self.handle(9, tile_rows * max_blocks() * 4)?;
for first_row in (0..n).step_by(256) {
let count = (n - first_row).min(256);
let tile = ScoreTile {
count,
reach: (plan.p0 as usize + first_row + count) / 4 + 1,
iq: plan.iq.checked_subspan(n * 512 * 4, first_row * 512 * 4, count * 512 * 4)?,
steps: out[4].checked_subspan(n * 16, first_row * 16, count * 16)?,
ids: out[3].checked_subspan(n * CAP * 4, first_row * CAP * 4, count * CAP * 4)?,
};
push_tile_scores(commands, plan, scores, &tile)?;
push_tile_select(commands, plan, scores, &tile)?;
}
Ok(())
}
}
fn push_tile_scores(
commands: &mut Commands,
plan: &RunPlan,
scores: ffi::BufferHandle,
tile: &ScoreTile,
) -> Result<()> {
let ct = tile.count as u32;
if plan.decode {
let args = vec![
A::Address(plan.pooled),
A::Address(plan.dead),
A::Address(tile.iq),
A::Address(tile.steps),
A::I32(1),
A::I64(max_blocks() as i64),
A::Address(scores),
];
return push_launch(commands, Launch::DecodeScores, args);
}
let tc = vec![
A::Address(plan.pooled),
A::Address(tile.iq),
A::Address(tile.steps),
A::I64(tile.count as i64),
A::I64(max_blocks() as i64),
A::I64(tile.reach as i64),
A::Address(scores),
];
push_launch(commands, Launch::Tc { t: ct, reach: tile.reach as u32 }, tc)?;
let tail = vec![
A::Address(plan.dead),
A::Address(tile.iq),
A::Address(tile.steps),
A::I64(max_blocks() as i64),
A::Address(scores),
];
push_launch(commands, Launch::ScoreTail(ct), tail)
}
fn push_tile_select(
commands: &mut Commands,
plan: &RunPlan,
scores: ffi::BufferHandle,
tile: &ScoreTile,
) -> Result<()> {
let scan = vec![
A::Address(scores),
A::Address(tile.steps),
A::I32(tile.count as i32),
A::I64(max_blocks() as i64),
A::Address(plan.status),
A::U32(4),
];
push_launch(commands, Launch::ScoreScan((tile.count * max_blocks()) as u32), scan)?;
let topk = vec![
A::Address(scores),
A::Address(tile.steps),
A::I64(max_blocks() as i64),
A::I64(CAP as i64),
A::Address(tile.ids),
];
push_launch(commands, Launch::Topk(tile.count as u32), topk)
}
impl<'ctx> QsaPromptIndexWorkspace<'ctx> {
/// Copies the staged host bytes up and launches every command on the shared stream.
pub(super) fn submit_run(
&self,
@@ -401,6 +194,17 @@ impl<'ctx> QsaPromptIndexWorkspace<'ctx> {
out: [ffi::BufferHandle; 5],
host: &mut HostStaging,
) -> Result<()> {
self.copy_run_inputs(plan, out, host)?;
// Pageable uploads/status have a real dependency before the nonblocking shared stream.
self.context.driver.api.synchronize()?;
let module = self.module.as_ref().ok_or(Error::ContextPoisoned)?;
for (shape, args) in &mut host.commands {
module.launch(*shape, blas.stream(), args)?;
}
Ok(())
}
fn copy_run_inputs(&self, plan: &RunPlan, out: [ffi::BufferHandle; 5], host: &HostStaging) -> Result<()> {
let api = &self.context.driver.api;
api.copy_from(plan.status, &host.zero)?;
api.copy_from(out[4], &host.steps)?;
@@ -411,12 +215,6 @@ impl<'ctx> QsaPromptIndexWorkspace<'ctx> {
if let Some(positions) = plan.positions {
api.copy_from(positions, &host.decode_positions)?;
}
// Pageable uploads/status have a real dependency before the nonblocking shared stream.
api.synchronize()?;
let module = self.module.as_ref().ok_or(Error::ContextPoisoned)?;
for (shape, args) in &mut host.commands {
module.launch(*shape, blas.stream(), args)?;
}
Ok(())
}