STRA-338 size: cuda-driver native_prompt_device: read_gr_impl command phases move to GrReadPlan (fn_lines 196 -> 48)

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 14:08:50 +01:00
parent 526eb70386
commit 4618b501e5
3 changed files with 174 additions and 159 deletions
@@ -3,6 +3,7 @@
#[allow(unused_imports)]
use super::*;
mod gr_read_phases;
mod impl_originalpromptdevice_2;
mod impl_originalpromptdevice_2_2;
mod impl_originalpromptdevice_3;
@@ -0,0 +1,136 @@
//! STRA-338 size: the phases of `OriginalPromptDevice::read_gr_impl`, extracted in command order.
#[allow(unused_imports)]
use super::*;
/// Spans, weights and scalars every command of one GR read shares.
pub(super) struct GrReadPlan {
h: [BufferHandle; 15],
w: [BufferHandle; 4],
flags: BufferHandle,
t: usize,
low_half: bool,
}
impl GrReadPlan {
pub(super) fn new(
h: [BufferHandle; 15],
w: [BufferHandle; 4],
flags: BufferHandle,
t: usize,
low_half: bool,
) -> Self {
Self { h, w, flags, t, low_half }
}
fn low(&self, i: usize) -> A {
if self.low_half { A::Address(self.h[i]) } else { A::NullAddress }
}
fn gemm(&self, v: &mut Vec<C>, (input, lo): (usize, usize), weight: BufferHandle, out: usize, (n, k): (i32, i32)) {
let (h, t) = (&self.h, self.t as i32);
v.push(C::Gemm { input: h[input], weight, output: h[out], t, n, k, alpha: 1., beta: 0. });
if self.low_half {
v.push(C::Gemm { input: h[lo], weight, output: h[out], t, n, k, alpha: 1., beta: 1. })
}
}
/// RMS-norm of R into the row scales and the high/low X; the scan rides on it when fused.
pub(super) fn push_norm(&self, v: &mut Vec<C>, epsilon: f32, normed: bool, fused: bool) {
if fused {
self.push_norm_fused(v, epsilon, normed);
} else {
self.push_norm_scanned(v, epsilon, normed);
}
}
fn push_norm_fused(&self, v: &mut Vec<C>, epsilon: f32, normed: bool) {
let (h, w) = (&self.h, &self.w);
// the norm guard rides on the norm kernel; a normed read follows `inject_gr_fused`, which already guarded
// this very norm (same R, rs and gamma), so nothing is left to scan
if !normed {
let args = vec![
A::Address(h[0]),
A::Address(w[0]),
A::F32(epsilon),
A::Address(h[1]),
A::Address(h[2]),
self.low(3),
A::Address(self.flags),
];
kernel(v, 3, 3, self.t * 4, 256, args);
}
}
fn push_norm_scanned(&self, v: &mut Vec<C>, epsilon: f32, normed: bool) {
let (h, w, t) = (&self.h, &self.w, self.t);
if !normed {
let args = vec![
A::Address(h[0]),
A::Address(w[0]),
A::F32(epsilon),
A::Address(h[1]),
A::Address(h[2]),
self.low(3),
];
kernel(v, 2, 0, t * 4, 256, args);
}
let scan = vec![A::Address(h[0]), A::Address(h[1]), A::Address(w[0]), A::Address(self.flags), A::I64(t as i64)];
kernel(v, 2, 5, (t * 10240).div_ceil(256), 256, scan);
}
/// Down projection, SiLU, up projection and the injection GEMM, each followed by its guard.
pub(super) fn push_projections(&self, v: &mut Vec<C>, fused: bool) {
let (h, w, t, flags) = (&self.h, &self.w, self.t, self.flags);
self.gemm(v, (2, 3), w[1], 4, (320, 10240));
scan_gr(v, h[4], flags, 320, t, 1);
let silu = vec![A::Address(h[4]), A::Address(h[5]), self.low(6), A::I64((t * 320) as i64)];
kernel(v, 2, 1, (t * 320).div_ceil(256), 256, silu);
kernel(v, 2, 6, (t * 320).div_ceil(256), 256, vec![A::Address(h[4]), A::Address(flags), A::I64(t as i64)]);
self.gemm(v, (5, 6), w[2], 7, (10240, 320));
if !fused {
scan_gr(v, h[7], flags, 10240, t, 3);
}
// Original injection GEMM precedes mixed production.
self.gemm(v, (2, 3), w[3], 12, (4, 10240));
scan_gr(v, h[12], flags, 4, t, 6);
}
/// Mixed production; fused folds the gated, product and mixed-row guards into the mix kernel.
pub(super) fn push_mix(&self, v: &mut Vec<C>, fused: bool) {
let (h, t, flags) = (&self.h, self.t, self.flags);
let mut mix = vec![
A::Address(h[0]),
A::Address(h[1]),
A::Address(self.w[0]),
A::Address(h[7]),
A::Address(h[8]),
A::Address(h[9]),
A::I64(t as i64),
A::Address(h[11]),
self.low(10),
];
if fused {
// mix + the guards of the gated tensor (3), the products (4) and the mixed row (5)
mix.push(A::Address(flags));
kernel(v, 3, 4, (t * 2560).div_ceil(256), 256, mix);
} else {
kernel(v, 2, 2, (t * 2560).div_ceil(256), 256, mix);
self.push_mix_guards(v);
}
}
fn push_mix_guards(&self, v: &mut Vec<C>) {
let (h, w, t, flags) = (&self.h, &self.w, self.t, self.flags);
let guard = vec![
A::Address(h[0]),
A::Address(h[1]),
A::Address(w[0]),
A::Address(h[7]),
A::Address(flags),
A::I64(t as i64),
];
kernel(v, 2, 7, (t * 10240).div_ceil(256), 256, guard);
scan_gr(v, h[8], flags, 2560, t, 5);
}
}
@@ -1,6 +1,7 @@
//! Moved out of the parent module by STRA-338: impl OriginalPromptDevice (5).
#[allow(unused_imports)]
use super::gr_read_phases::GrReadPlan;
use super::*;
impl<'ctx> OriginalPromptDevice<'ctx> {
@@ -19,6 +20,41 @@ impl<'ctx> OriginalPromptDevice<'ctx> {
return Err(Error::InvalidQwenShape);
}
let t = lease.rows();
let h = self.gr_read_spans(lease)?;
let mut w = [h[0]; 4];
for (i, n) in [40960, 6553600, 6553600, 81920].into_iter().enumerate() {
w[i] = self.weight(lease, workspace, weights[i], n, if i == 0 { 4 } else { 2 })?;
}
let low_half = self.profile == GrNativeProfile::HighAndLow;
let plan = GrReadPlan::new(h, w, self.status()?, t, low_half);
let mut v = Vec::new();
v.try_reserve_exact(20).map_err(|_| Error::HostAllocation)?;
let fused = fused_scans();
plan.push_norm(&mut v, epsilon, normed, fused);
plan.push_projections(&mut v, fused);
plan.push_mix(&mut v, fused);
self.run(
lease,
workspace,
v,
t,
8,
&[0, 1, 2, 3, 6, 4, 5],
&[
"GR native norm",
"GR native down",
"GR native silu",
"GR native up",
"GR native products",
"GR native mixed",
"GR native inject",
"GR native candidate",
],
)?;
Ok(OriginalDeviceGrRead { lease: residual.lease })
}
/// The device spans a GR read touches; the low-half ones only when the profile keeps a low half.
fn gr_read_spans(&self, lease: &OriginalPromptRows<'_, '_, 'ctx>) -> Result<[BufferHandle; 15]> {
let mut h = [self.status()?; 15];
for (i, r, n) in [
(0, S::R, 40960),
@@ -40,164 +76,6 @@ impl<'ctx> OriginalPromptDevice<'ctx> {
h[i] = Self::span(lease, r, n)?;
}
}
let mut w = [h[0]; 4];
for (i, n) in [40960, 6553600, 6553600, 81920].into_iter().enumerate() {
w[i] = self.weight(lease, workspace, weights[i], n, if i == 0 { 4 } else { 2 })?;
}
let low = |i| {
if self.profile == GrNativeProfile::HighAndLow { A::Address(h[i]) } else { A::NullAddress }
};
let flags = self.status()?;
let mut v = Vec::new();
v.try_reserve_exact(20).map_err(|_| Error::HostAllocation)?;
let fused = fused_scans();
if fused {
// the norm guard rides on the norm kernel; a normed read follows `inject_gr_fused`, which already guarded
// this very norm (same R, rs and gamma), so nothing is left to scan
if !normed {
kernel(
&mut v,
3,
3,
t * 4,
256,
vec![
A::Address(h[0]),
A::Address(w[0]),
A::F32(epsilon),
A::Address(h[1]),
A::Address(h[2]),
low(3),
A::Address(flags),
],
);
}
} else {
if !normed {
kernel(
&mut v,
2,
0,
t * 4,
256,
vec![
A::Address(h[0]),
A::Address(w[0]),
A::F32(epsilon),
A::Address(h[1]),
A::Address(h[2]),
low(3),
],
);
}
kernel(
&mut v,
2,
5,
(t * 10240).div_ceil(256),
256,
vec![A::Address(h[0]), A::Address(h[1]), A::Address(w[0]), A::Address(flags), A::I64(t as i64)],
);
}
let gemm = |v: &mut Vec<C>, input, lo, weight, out, n, k| {
v.push(C::Gemm { input: h[input], weight, output: h[out], t: t as i32, n, k, alpha: 1., beta: 0. });
if self.profile == GrNativeProfile::HighAndLow {
v.push(C::Gemm { input: h[lo], weight, output: h[out], t: t as i32, n, k, alpha: 1., beta: 1. })
}
};
gemm(&mut v, 2, 3, w[1], 4, 320, 10240);
scan_gr(&mut v, h[4], flags, 320, t, 1);
kernel(
&mut v,
2,
1,
(t * 320).div_ceil(256),
256,
vec![A::Address(h[4]), A::Address(h[5]), low(6), A::I64((t * 320) as i64)],
);
kernel(&mut v, 2, 6, (t * 320).div_ceil(256), 256, vec![A::Address(h[4]), A::Address(flags), A::I64(t as i64)]);
gemm(&mut v, 5, 6, w[2], 7, 10240, 320);
if !fused {
scan_gr(&mut v, h[7], flags, 10240, t, 3);
}
// Original injection GEMM precedes mixed production.
gemm(&mut v, 2, 3, w[3], 12, 4, 10240);
scan_gr(&mut v, h[12], flags, 4, t, 6);
if fused {
// mix + the guards of the gated tensor (3), the products (4) and the mixed row (5)
kernel(
&mut v,
3,
4,
(t * 2560).div_ceil(256),
256,
vec![
A::Address(h[0]),
A::Address(h[1]),
A::Address(w[0]),
A::Address(h[7]),
A::Address(h[8]),
A::Address(h[9]),
A::I64(t as i64),
A::Address(h[11]),
low(10),
A::Address(flags),
],
);
} else {
kernel(
&mut v,
2,
2,
(t * 2560).div_ceil(256),
256,
vec![
A::Address(h[0]),
A::Address(h[1]),
A::Address(w[0]),
A::Address(h[7]),
A::Address(h[8]),
A::Address(h[9]),
A::I64(t as i64),
A::Address(h[11]),
low(10),
],
);
kernel(
&mut v,
2,
7,
(t * 10240).div_ceil(256),
256,
vec![
A::Address(h[0]),
A::Address(h[1]),
A::Address(w[0]),
A::Address(h[7]),
A::Address(flags),
A::I64(t as i64),
],
);
scan_gr(&mut v, h[8], flags, 2560, t, 5);
}
self.run(
lease,
workspace,
v,
t,
8,
&[0, 1, 2, 3, 6, 4, 5],
&[
"GR native norm",
"GR native down",
"GR native silu",
"GR native up",
"GR native products",
"GR native mixed",
"GR native inject",
"GR native candidate",
],
)?;
Ok(OriginalDeviceGrRead { lease: residual.lease })
Ok(h)
}
}