STRA-338 params q02: GR planners take GrWindow, GrConfig::from_device takes GrDeviceAttributes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-11-08 12:54:20 +01:00
co-authored by Claude Sonnet 5.5
parent 5a0b447b16
commit f24482d64a
9 changed files with 108 additions and 84 deletions
@@ -134,6 +134,16 @@ pub(crate) const ATTR_COMPUTE_CAPABILITY_MAJOR: i32 = 75;
pub(crate) const ATTR_COMPUTE_CAPABILITY_MINOR: i32 = 76;
pub(crate) const ATTR_MAX_SHARED_PER_BLOCK_OPTIN: i32 = 97;
/// The four `cuDeviceGetAttribute` values `down_chunk` reads: the compute capability and the dynamic shared memory
/// limits (opt-in and per block, in bytes).
#[derive(Clone, Copy, Debug)]
pub(crate) struct GrDeviceAttributes {
pub cc_major: i32,
pub cc_minor: i32,
pub optin: i32,
pub per_block: i32,
}
/// The hc-read decisions: `fused_gr_variant()` plus the result of `down_chunk` on this card.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct GrConfig {
@@ -155,6 +165,15 @@ pub(crate) struct GrConfig {
pub attr_staged: Option<u32>,
}
/// What every gated-residual (GR) planner reads: the arena the spans come from, the window geometry and the
/// device-derived GR configuration.
#[derive(Clone, Copy)]
pub(crate) struct GrWindow<'a> {
pub arena: &'a dyn ArenaMap,
pub geo: &'a OriginalVerifyGeometry,
pub cfg: &'a GrConfig,
}
/// PLE settings of the Original (`--native`: `ple_set_native_bf16` + `ple_set_native_postops`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PleConfig {
@@ -29,14 +29,8 @@ impl GrConfig {
/// Replicates the one-time per-device block of `down_chunk` from the four device attributes
/// (`ATTR_*`). An attribute call is only emitted when the card reports an opt-in (Original
/// issues it unconditionally but ignores its failure and drops the error).
pub(crate) fn from_device(
variant: GrVariant,
native_mmvf: bool,
cc_major: i32,
cc_minor: i32,
optin: i32,
per_block: i32,
) -> Self {
pub(crate) fn from_device(variant: GrVariant, native_mmvf: bool, attributes: GrDeviceAttributes) -> Self {
let GrDeviceAttributes { cc_major, cc_minor, optin, per_block } = attributes;
let optin = optin.max(0) as u32;
let per_block = per_block.max(0) as u32;
let tile: u32 = if cc_major * 10 + cc_minor == 75 { 1280 } else { 2560 };
@@ -68,10 +62,12 @@ impl GrConfig {
Ok(Self::from_device(
variant,
native_mmvf,
journal.window_device_attribute(ATTR_COMPUTE_CAPABILITY_MAJOR)?,
journal.window_device_attribute(ATTR_COMPUTE_CAPABILITY_MINOR)?,
journal.window_device_attribute(ATTR_MAX_SHARED_PER_BLOCK_OPTIN)?,
journal.window_device_attribute(ATTR_MAX_SHARED_PER_BLOCK)?,
GrDeviceAttributes {
cc_major: journal.window_device_attribute(ATTR_COMPUTE_CAPABILITY_MAJOR)?,
cc_minor: journal.window_device_attribute(ATTR_COMPUTE_CAPABILITY_MINOR)?,
optin: journal.window_device_attribute(ATTR_MAX_SHARED_PER_BLOCK_OPTIN)?,
per_block: journal.window_device_attribute(ATTR_MAX_SHARED_PER_BLOCK)?,
},
))
}
@@ -10,14 +10,13 @@ use super::*;
/// `rs = rs_ + t*HC`, `mixed = mixed_ + t*N`; the attention half reads `inj2_` and writes `inj_`, the
/// FFN half the reverse.
pub(crate) fn plan_hc_read(
arena: &dyn ArenaMap,
geo: &OriginalVerifyGeometry,
window: GrWindow<'_>,
w: &HcAddrs,
cfg: &GrConfig,
half: HcHalf,
apply: bool,
rows: Rows,
) -> Result<HcReadPlan> {
let GrWindow { arena, geo, cfg } = window;
check_rows(geo, rows)?;
let n = rows.len();
let per_row = hc_read_rows(arena, half, rows)?;
@@ -211,13 +210,12 @@ pub(crate) fn plan_final_write(
/// (xn, gated, lo) is carved from arena spans that are dead after the layers: `xn` slot `t`, `lo`
/// slot `t` and the first `HC*N` floats of `parts` slot `t`.
pub(crate) fn plan_head_mix(
arena: &dyn ArenaMap,
geo: &OriginalVerifyGeometry,
window: GrWindow<'_>,
w: &HeadAddrs,
cfg: &GrConfig,
mixed_out_base: u64,
rows: usize,
) -> Result<Vec<Launch>> {
let GrWindow { arena, geo, cfg } = window;
check_geometry(geo)?;
if !(1..=geo.max_t).contains(&rows) {
return Err(Error::InvalidQwenShape);
@@ -23,14 +23,8 @@ use super::*;
/// from the workspace and performs the identical shift-and-append (pure data movement, same
/// 40x256 launch), so the memcpy node is not recorded. `native_ple_postops_batch`'s kernels at
/// `T = 1` have the single-token kernels' launch geometry and per-element arithmetic.
pub(crate) fn plan_ple_row(
arena: &dyn ArenaMap,
geo: &OriginalVerifyGeometry,
cfg: &GrConfig,
ple: &PleConfig,
a: &PleAddrs,
row: usize,
) -> Result<Vec<Launch>> {
pub(crate) fn plan_ple_row(window: GrWindow<'_>, ple: &PleConfig, a: &PleAddrs, row: usize) -> Result<Vec<Launch>> {
let GrWindow { arena, geo, cfg } = window;
check_ple_row(geo, ple, row)?;
let mut out = Vec::with_capacity(14);
out.push(write_row(arena, cfg, row, "ple_layer1")?);
@@ -39,7 +33,7 @@ pub(crate) fn plan_ple_row(
let emb = row_span(arena, "ple", row, N * 4)?;
let scratch = PleScratch::at(a.scratch);
plan_ple_key(arena, ple, a, emb, scratch.key, &mut out)?;
plan_ple_key(arena, ple, a, emb, &mut out)?;
// value = ple_value @ emb
out.push(plan_bf16_gemv(
StridedRows::new(emb, N),
@@ -88,14 +82,8 @@ impl PleScratch {
}
/// key = ple_key @ emb (BF16 MMVF, or q8_1 quantize + MMVQ for a native key).
fn plan_ple_key(
arena: &dyn ArenaMap,
ple: &PleConfig,
a: &PleAddrs,
emb: u64,
key: u64,
out: &mut Vec<Launch>,
) -> Result<()> {
fn plan_ple_key(arena: &dyn ArenaMap, ple: &PleConfig, a: &PleAddrs, emb: u64, out: &mut Vec<Launch>) -> Result<()> {
let key = PleScratch::at(a.scratch).key;
match a.key {
PleKey::Bf16(w) => out.push(plan_bf16_gemv(
StridedRows::new(emb, N),
@@ -149,15 +137,13 @@ fn plan_ple_postops(a: &PleAddrs, r: u64, s: &PleScratch) -> Result<[Launch; 7]>
/// `pre(1, group)`'s PLE loop for the rows `[tb, te)`: one `(row, launches)` entry per row, to be
/// submitted in order, each followed by `capture_ple_snapshot(row)`.
pub(crate) fn plan_ple_layer1(
arena: &dyn ArenaMap,
geo: &OriginalVerifyGeometry,
cfg: &GrConfig,
window: GrWindow<'_>,
ple: &PleConfig,
a: &PleAddrs,
rows: Rows,
) -> Result<Vec<(usize, Vec<Launch>)>> {
check_rows(geo, rows)?;
(rows.begin..rows.end).map(|t| Ok((t, plan_ple_row(arena, geo, cfg, ple, a, t)?))).collect()
check_rows(window.geo, rows)?;
(rows.begin..rows.end).map(|t| Ok((t, plan_ple_row(window, ple, a, t)?))).collect()
}
impl<'a, 'ctx> OriginalVerifyGrBinding<'a, 'ctx> {
pub fn new(
@@ -23,9 +23,22 @@ fn words(l: &Launch) -> &[u64] {
fn rows(begin: usize, end: usize, group: usize) -> Rows {
Rows { begin, end, group }
}
/// The GR planners' inputs: the arena, the geometry and the device configuration.
fn window<'a>(arena: &'a dyn ArenaMap, geo: &'a OriginalVerifyGeometry, cfg: &'a GrConfig) -> GrWindow<'a> {
GrWindow { arena, geo, cfg }
}
/// A native-MMVF card of compute capability `cc` (major, minor) with the given dynamic shared memory limits.
fn from_card(variant: GrVariant, cc: (i32, i32), optin: i32, per_block: i32) -> GrConfig {
let attributes = GrDeviceAttributes { cc_major: cc.0, cc_minor: cc.1, optin, per_block };
GrConfig::from_device(variant, true, attributes)
}
/// RTX-class sm_120: 99 KiB opt-in, 48 KiB default per block.
fn sm120(variant: GrVariant) -> GrConfig {
GrConfig::from_device(variant, true, 12, 0, 101_376, 49_152)
GrConfig::from_device(
variant,
true,
GrDeviceAttributes { cc_major: 12, cc_minor: 0, optin: 101_376, per_block: 49_152 },
)
}
fn entry_declared(ptx: &str, e: &str) -> bool {
ptx.contains(&format!(".entry {e}(")) || ptx.contains(&format!(".visible .entry {e}("))
@@ -34,13 +34,13 @@ fn assert_head_mix_row(m: &LayoutMap, k: &[Launch], t: usize, mixed_base: u64) {
/// The whole window: 1..=8 rows; none beyond max_t; the non-native mmvf refuses.
fn assert_head_mix_row_bounds(m: &LayoutMap, w: &HeadAddrs, mixed_base: u64) {
let g = test_geometry();
assert!(plan_head_mix(m, &g, w, &sm120(GrVariant::Staged), mixed_base, 8).is_ok());
assert!(plan_head_mix(m, &g, w, &sm120(GrVariant::Staged), mixed_base, 9).is_err());
assert!(plan_head_mix(m, &g, w, &sm120(GrVariant::Staged), mixed_base, 0).is_err());
assert!(plan_head_mix(window(m, &g, &sm120(GrVariant::Staged)), w, mixed_base, 8).is_ok());
assert!(plan_head_mix(window(m, &g, &sm120(GrVariant::Staged)), w, mixed_base, 9).is_err());
assert!(plan_head_mix(window(m, &g, &sm120(GrVariant::Staged)), w, mixed_base, 0).is_err());
let mut non_native = sm120(GrVariant::Staged);
non_native.native_mmvf = false;
assert!(matches!(
plan_head_mix(m, &g, w, &non_native, mixed_base, 1),
plan_head_mix(window(m, &g, &non_native), w, mixed_base, 1),
Err(Error::InvalidDriverValue(s)) if s.contains("head")
));
}
@@ -51,7 +51,7 @@ fn head_mix_is_the_native_unfused_gr_read_per_token() {
let g = test_geometry();
let w = HeadAddrs { norm: 0xA000, down: 0xB000, up: 0xC000 };
let mixed_base = 0x9999_0000u64;
let l = plan_head_mix(&m, &g, &w, &sm120(GrVariant::Staged), mixed_base, 3).unwrap();
let l = plan_head_mix(window(&m, &g, &sm120(GrVariant::Staged)), &w, mixed_base, 3).unwrap();
assert_eq!(l.len(), 15);
for t in 0..3usize {
assert_head_mix_row(&m, &l[t * 5..t * 5 + 5], t, mixed_base);
@@ -141,7 +141,7 @@ fn ple_row_with_a_bf16_key_follows_ple_block_native_order() {
let m = arena();
let g = test_geometry();
let a = ple_addrs(PleKey::Bf16(0x7000_0000));
let l = plan_ple_row(&m, &g, &sm120(GrVariant::Staged), &PleConfig::mode17(), &a, 2).unwrap();
let l = plan_ple_row(window(&m, &g, &sm120(GrVariant::Staged)), &PleConfig::mode17(), &a, 2).unwrap();
assert_ple_bf16_row_names(&l);
let r = at(&m, "R", 2 * 40_960);
let emb = at(&m, "ple", 2 * 10_240);
@@ -157,7 +157,7 @@ fn ple_row_with_a_native_key_quantizes_into_the_arena_and_runs_mmvq() {
let m = arena();
let g = test_geometry();
let a = ple_addrs(PleKey::Native { ty: 42, data: 0x7100_0000 });
let l = plan_ple_row(&m, &g, &sm120(GrVariant::Staged), &PleConfig::mode17(), &a, 0).unwrap();
let l = plan_ple_row(window(&m, &g, &sm120(GrVariant::Staged)), &PleConfig::mode17(), &a, 0).unwrap();
let names: Vec<&str> = l.iter().map(|x| x.entry).collect();
assert_eq!(&names[..4], &["mtp_full_native_write", "head_quant", "head_n42_s1", "mtp_full_mmvf256"]);
assert_eq!(l.len(), 11);
@@ -170,7 +170,7 @@ fn ple_row_with_a_native_key_quantizes_into_the_arena_and_runs_mmvq() {
// an unsupported native key type fails closed
let bad = ple_addrs(PleKey::Native { ty: 14, data: 1 });
assert_eq!(
plan_ple_row(&m, &g, &sm120(GrVariant::Staged), &PleConfig::mode17(), &bad, 0).unwrap_err(),
plan_ple_row(window(&m, &g, &sm120(GrVariant::Staged)), &PleConfig::mode17(), &bad, 0).unwrap_err(),
Error::UnsupportedQwenOperation
);
}
@@ -181,19 +181,19 @@ fn ple_layer1_walks_the_group_rows_in_order_and_fails_closed_on_policy() {
let g = test_geometry();
let a = ple_addrs(PleKey::Bf16(0x7000_0000));
let c = sm120(GrVariant::Staged);
let out = plan_ple_layer1(&m, &g, &c, &PleConfig::mode17(), &a, rows(4, 7, 1)).unwrap();
let out = plan_ple_layer1(window(&m, &g, &c), &PleConfig::mode17(), &a, rows(4, 7, 1)).unwrap();
assert_eq!(out.iter().map(|(t, _)| *t).collect::<Vec<_>>(), [4, 5, 6]);
assert!(out.iter().all(|(_, l)| l.len() == 10));
let mut legacy = PleConfig::mode17();
legacy.native_postops = false;
assert!(matches!(
plan_ple_layer1(&m, &g, &c, &legacy, &a, rows(0, 1, 0)),
plan_ple_layer1(window(&m, &g, &c), &legacy, &a, rows(0, 1, 0)),
Err(Error::InvalidDriverValue(s)) if s.contains("ple_layer1")
));
legacy = PleConfig::mode17();
legacy.native_value_bf16 = false;
assert!(plan_ple_row(&m, &g, &c, &legacy, &a, 0).is_err());
assert!(plan_ple_row(&m, &g, &c, &PleConfig::mode17(), &a, 8).is_err());
assert!(plan_ple_row(window(&m, &g, &c), &legacy, &a, 0).is_err());
assert!(plan_ple_row(window(&m, &g, &c), &PleConfig::mode17(), &a, 8).is_err());
}
// ---------------------------------------------------------------------------- byte sizes
@@ -68,7 +68,7 @@ fn down_chunk_on_big_cards_runs_the_2560_tile_with_all_eight_tokens() {
#[test]
fn down_chunk_on_turing_slices_staged_tokens_and_uses_the_1280_tile() {
// sm_75: 64 KiB opt-in
let c = GrConfig::from_device(GrVariant::Staged, true, 7, 5, 65_536, 49_152);
let c = from_card(GrVariant::Staged, (7, 5), 65_536, 49_152);
assert_eq!(c.tile, 1280);
assert_eq!(c.attr_down, Some(8 * 1280 * 4)); // 40960 fits
assert_eq!(c.attr_staged, Some(65_536)); // 81920 > opt-in -> opt-in
@@ -79,21 +79,22 @@ fn down_chunk_on_turing_slices_staged_tokens_and_uses_the_1280_tile() {
#[test]
fn down_chunk_below_volta_uses_the_per_block_limit_and_never_less_than_one_token() {
// sm_61 reports no usable opt-in: per-block limit 48 KiB
let c = GrConfig::from_device(GrVariant::Plain, true, 6, 1, 0, 49_152);
let c = from_card(GrVariant::Plain, (6, 1), 0, 49_152);
assert_eq!(c.tile, 2560);
assert_eq!((c.chunk_plain, c.chunk_staged), (4, 4)); // 49152 / 10240
assert_eq!((c.attr_down, c.attr_staged), (None, None));
// sm_60 answers a 64 KiB opt-in it does not keep: the limit is still per block
let c60 = GrConfig::from_device(GrVariant::Plain, true, 6, 0, 65_536, 49_152);
let c60 = from_card(GrVariant::Plain, (6, 0), 65_536, 49_152);
assert_eq!(c60.usable_shared, 49_152);
assert_eq!(c60.chunk_plain, 4);
assert_eq!((c60.attr_down, c60.attr_staged), (None, None));
// a card that cannot hold one token still reports a chunk of 1 (the launch is then refused)
let tiny = GrConfig::from_device(GrVariant::Plain, true, 6, 1, 0, 4096);
let tiny = from_card(GrVariant::Plain, (6, 1), 0, 4096);
assert_eq!((tiny.chunk_plain, tiny.chunk_staged), (1, 1));
let m = arena();
assert_eq!(
plan_hc_read(&m, &test_geometry(), &weights(), &tiny, HcHalf::Attention, false, rows(0, 2, 0)).unwrap_err(),
plan_hc_read(window(&m, &test_geometry(), &tiny), &weights(), HcHalf::Attention, false, rows(0, 2, 0))
.unwrap_err(),
Error::InvalidProjectionShape
);
}
@@ -105,7 +106,7 @@ fn shared_attributes_name_the_tile_kernel_of_the_card_and_the_staged_kernel() {
assert_eq!((big[0].1, big[0].2), (E_DOWN2560, 81_920));
assert_eq!((big[1].1, big[1].2), (E_STAGED, 81_920));
assert!(big.iter().all(|a| std::ptr::eq(a.0, &GR_FUSED)));
let turing = shared_attributes(&GrConfig::from_device(GrVariant::Staged, true, 7, 5, 65_536, 49_152));
let turing = shared_attributes(&from_card(GrVariant::Staged, (7, 5), 65_536, 49_152));
assert_eq!((turing[0].1, turing[0].2), (E_DOWN1280, 40_960));
assert_eq!((turing[1].1, turing[1].2), (E_STAGED, 65_536));
}
@@ -144,7 +145,8 @@ fn staged_read_of_eight_rows_is_three_launch_kinds_with_the_multi_struct() {
let m = arena();
let g = test_geometry();
let w = weights();
let plan = plan_hc_read(&m, &g, &w, &sm120(GrVariant::Staged), HcHalf::Attention, true, rows(0, 8, 0)).unwrap();
let plan =
plan_hc_read(window(&m, &g, &sm120(GrVariant::Staged)), &w, HcHalf::Attention, true, rows(0, 8, 0)).unwrap();
let full = assert_staged_read_launch_kinds(&plan);
assert_eq!(full.len(), 114);
// trailer: xn = xn_ + tb*HC*N*4, T = 8
@@ -171,7 +173,8 @@ fn staged_read_of_eight_rows_is_three_launch_kinds_with_the_multi_struct() {
fn ffn_half_swaps_the_injection_buffers_and_apply_is_carried() {
let m = arena();
let g = test_geometry();
let plan = plan_hc_read(&m, &g, &weights(), &sm120(GrVariant::Staged), HcHalf::Ffn, false, rows(0, 1, 0)).unwrap();
let plan =
plan_hc_read(window(&m, &g, &sm120(GrVariant::Staged)), &weights(), HcHalf::Ffn, false, rows(0, 1, 0)).unwrap();
let a = &words(&plan.norm)[..14];
assert_eq!(a[2], 0);
assert_eq!(a[4], at(&m, "inj", 0));
@@ -187,7 +190,8 @@ fn split_windows_read_their_own_rows_and_offset_the_scratch() {
let m = arena();
let g = test_geometry();
// T = 7 -> groups [0,4) and [4,7): (T+1)/2 = 4
let g1 = plan_hc_read(&m, &g, &weights(), &sm120(GrVariant::Staged), HcHalf::Ffn, true, rows(4, 7, 1)).unwrap();
let g1 =
plan_hc_read(window(&m, &g, &sm120(GrVariant::Staged)), &weights(), HcHalf::Ffn, true, rows(4, 7, 1)).unwrap();
let w1 = words(&g1.norm);
assert_eq!(w1[112], at(&m, "xn", 4 * 40_960));
assert_eq!(w1[113], 3);
@@ -204,15 +208,17 @@ fn plain_and_split_variants_pick_the_norm_and_the_tile_kernel() {
let m = arena();
let g = test_geometry();
let plain =
plan_hc_read(&m, &g, &weights(), &sm120(GrVariant::Plain), HcHalf::Attention, false, rows(0, 5, 0)).unwrap();
plan_hc_read(window(&m, &g, &sm120(GrVariant::Plain)), &weights(), HcHalf::Attention, false, rows(0, 5, 0))
.unwrap();
assert_eq!((plain.norm.entry, plain.norm.grid), (E_NORM, [5, 1, 1]));
assert_eq!((plain.down[0].entry, plain.down[0].shared), (E_DOWN2560, 5 * 2560 * 4));
let split =
plan_hc_read(&m, &g, &weights(), &sm120(GrVariant::Split), HcHalf::Attention, false, rows(0, 5, 0)).unwrap();
plan_hc_read(window(&m, &g, &sm120(GrVariant::Split)), &weights(), HcHalf::Attention, false, rows(0, 5, 0))
.unwrap();
assert_eq!((split.norm.entry, split.norm.grid), (E_SPLIT, [5, 4, 1]));
assert_eq!(split.down[0].entry, E_DOWN2560);
let turing = GrConfig::from_device(GrVariant::Plain, true, 7, 5, 65_536, 49_152);
let t = plan_hc_read(&m, &g, &weights(), &turing, HcHalf::Attention, false, rows(0, 8, 0)).unwrap();
let turing = from_card(GrVariant::Plain, (7, 5), 65_536, 49_152);
let t = plan_hc_read(window(&m, &g, &turing), &weights(), HcHalf::Attention, false, rows(0, 8, 0)).unwrap();
assert_eq!((t.down[0].entry, t.down[0].shared), (E_DOWN1280, 8 * 1280 * 4));
}
@@ -220,8 +226,8 @@ fn plain_and_split_variants_pick_the_norm_and_the_tile_kernel() {
fn down_projection_is_chunked_by_the_card_capacity() {
let m = arena();
let g = test_geometry();
let turing = GrConfig::from_device(GrVariant::Staged, true, 7, 5, 65_536, 49_152);
let plan = plan_hc_read(&m, &g, &weights(), &turing, HcHalf::Attention, true, rows(0, 8, 0)).unwrap();
let turing = from_card(GrVariant::Staged, (7, 5), 65_536, 49_152);
let plan = plan_hc_read(window(&m, &g, &turing), &weights(), HcHalf::Attention, true, rows(0, 8, 0)).unwrap();
// 8 tokens, 6 per launch -> 6 + 2
assert_eq!(plan.down.len(), 2);
assert_eq!((plan.down[0].shared, plan.down[1].shared), (61_440, 20_480));
@@ -253,13 +259,13 @@ fn hc_read_validates_rows_and_geometry() {
let g = test_geometry();
let c = sm120(GrVariant::Staged);
let w = weights();
assert!(plan_hc_read(&m, &g, &w, &c, HcHalf::Attention, false, rows(3, 3, 0)).is_err());
assert!(plan_hc_read(&m, &g, &w, &c, HcHalf::Attention, false, rows(0, 9, 0)).is_err());
assert!(plan_hc_read(&m, &g, &w, &c, HcHalf::Attention, false, rows(7, 9, 1)).is_err());
assert!(plan_hc_read(window(&m, &g, &c), &w, HcHalf::Attention, false, rows(3, 3, 0)).is_err());
assert!(plan_hc_read(window(&m, &g, &c), &w, HcHalf::Attention, false, rows(0, 9, 0)).is_err());
assert!(plan_hc_read(window(&m, &g, &c), &w, HcHalf::Attention, false, rows(7, 9, 1)).is_err());
let mut odd = g;
odd.hc = 8;
assert_eq!(
plan_hc_read(&m, &odd, &w, &c, HcHalf::Attention, false, rows(0, 1, 0)).unwrap_err(),
plan_hc_read(window(&m, &odd, &c), &w, HcHalf::Attention, false, rows(0, 1, 0)).unwrap_err(),
Error::InvalidQwenShape
);
// every T = 1..=8 and both groups of the split window plan
@@ -267,7 +273,7 @@ fn hc_read_validates_rows_and_geometry() {
let first = (t + 1) / 2;
let groups = if t >= 2 { vec![(0, first), (first, t)] } else { vec![(0, t)] };
for (gi, (b, e)) in groups.into_iter().enumerate() {
let plan = plan_hc_read(&m, &g, &w, &c, HcHalf::Attention, true, rows(b, e, gi)).unwrap();
let plan = plan_hc_read(window(&m, &g, &c), &w, HcHalf::Attention, true, rows(b, e, gi)).unwrap();
assert_eq!(words(&plan.up)[113], (e - b) as u64);
assert_eq!(plan.norm.grid[0], (e - b) as u32);
}
@@ -72,6 +72,10 @@ impl<'p> Planner<'p> {
dispatch: self.policy.dispatch,
}
}
/// The arena, geometry and GR configuration the GR planners read.
fn gr_window(&self) -> gr::GrWindow<'_> {
gr::GrWindow { arena: self.arena, geo: &self.geo, cfg: &self.gr }
}
fn qsa_window(&self) -> Result<QsaWindow<'p>> {
QsaWindow::new(self.arena, self.geo, self.policy.qsa, self.weights.rope()?)
}
@@ -79,7 +83,7 @@ impl<'p> Planner<'p> {
// ---- GR
pub(crate) fn hc_read(&self, layer: usize, half: HcHalf, apply: bool, rows: Rows) -> Result<Vec<Launch>> {
let w = self.weights.hc(layer, half)?;
Ok(gr::plan_hc_read(self.arena, &self.geo, &w, &self.gr, half, apply, rows)?.launches())
Ok(gr::plan_hc_read(self.gr_window(), &w, half, apply, rows)?.launches())
}
pub(crate) fn final_write(&self, rows: Rows) -> Result<Vec<Launch>> {
gr::plan_final_write(self.arena, &self.geo, &self.gr, rows)
@@ -87,14 +91,14 @@ impl<'p> Planner<'p> {
/// The per-token `lm_head_mix` launches writing the head rows' `mixed` buffer at `mixed_out_base`.
pub(crate) fn head_mix(&self, mixed_out_base: u64, rows: usize) -> Result<Vec<Launch>> {
let w = self.weights.head()?;
gr::plan_head_mix(self.arena, &self.geo, &w, &self.gr, mixed_out_base, rows)
gr::plan_head_mix(self.gr_window(), &w, mixed_out_base, rows)
}
/// One entry per window row: that row's launches, to be followed by the PLE history snapshot copy of the row.
pub(crate) fn ple_layer1(&self, rows: Rows) -> Result<Vec<(usize, Vec<Launch>)>> {
let ple = self.weights.ple()?.ok_or(Error::InvalidDriverValue(
"Original verifier producer unbound: ple_layer1: the model has a PLE block but no PLE weights are attached",
))?;
gr::plan_ple_layer1(self.arena, &self.geo, &self.gr, &self.ple_config(), &ple, rows)
gr::plan_ple_layer1(self.gr_window(), &self.ple_config(), &ple, rows)
}
// ---- projections
@@ -153,11 +157,11 @@ impl<'p> Planner<'p> {
// ---- FFN
pub(crate) fn router(&self, layer: usize, rows: Rows, batched: bool) -> Result<Vec<Launch>> {
let w = self.weights.moe(layer)?;
moe::plan_router(self.arena, &self.geo, &self.moe_knobs(), w.router, rows, batched)
moe::plan_router(self.arena, &self.geo, &self.moe_knobs(), w.router, moe::BatchedRows::new(rows, batched))
}
pub(crate) fn shared_expert(&self, layer: usize, rows: Rows, dec_batch: bool) -> Result<Vec<Launch>> {
let w = self.weights.moe(layer)?;
moe::plan_shared_expert(self.arena, &self.geo, &self.moe_knobs(), &w, rows, dec_batch)
moe::plan_shared_expert(self.arena, &self.geo, &self.moe_knobs(), &w, moe::BatchedRows::new(rows, dec_batch))
}
pub(crate) fn quantize_expert_input(&self, rows: Rows) -> Result<Vec<Launch>> {
moe::plan_quantize_expert_input(self.arena, &self.geo, &self.moe_knobs(), rows)
@@ -168,9 +172,7 @@ impl<'p> Planner<'p> {
&self.geo,
&self.moe_knobs(),
self.weights.expert_formats()?,
layer,
rows,
tier,
moe::ExpertGroupSite { layer, rows, tier },
)
}
pub(crate) fn hit_add(&self, rows: Rows) -> Result<Vec<Launch>> {
@@ -3,7 +3,7 @@
use super::super::kernels::{LayoutMap, test_geometry};
use super::super::moe::MoeLayerAddrs;
use super::super::proj::NativeWeight;
use super::super::qsa::{OriginalVerifyQsaKvBinding, QsaWeightAddrs};
use super::super::qsa::{OriginalVerifyQsaKvBinding, QsaWeightAddrs, RopeCorrectionRange};
use super::super::window_plan::WINDOW_LAYERS;
use super::super::window_record::{WindowOps, record_window};
use super::*;
@@ -92,7 +92,7 @@ impl WindowWeights for Fake {
Ok(QsaLayerAddrs { weights: fake_qsa_weights(b), kv: fake_qsa_kv(b) })
}
fn rope(&self) -> Result<QsaRope> {
QsaRope::new(0.7498942, 1.0, 0.0, 0.0, 0.0, 1.0)
QsaRope::new(0.7498942, 1.0, RopeCorrectionRange { low: 0.0, high: 0.0 }, 0.0, 1.0)
}
fn moe(&self, layer: usize) -> Result<MoeLayerAddrs> {
let b = 0x60_0000_0000u64 + layer as u64 * 0x1_0000_0000;
@@ -294,7 +294,11 @@ fn geometry_and_fake(ple: bool) -> (LayoutMap, Fake) {
(m, Fake { formats, ple })
}
fn sm120() -> GrConfig {
GrConfig::from_device(gr::GrVariant::Staged, true, 12, 0, 99 * 1024, 48 * 1024)
GrConfig::from_device(
gr::GrVariant::Staged,
true,
gr::GrDeviceAttributes { cc_major: 12, cc_minor: 0, optin: 99 * 1024, per_block: 48 * 1024 },
)
}
fn record(t: usize, groups: usize, ple: bool) -> Vec<String> {
let (m, fake) = geometry_and_fake(ple);