chain: register-row GR down kernel in the plan, 8-column up default

This commit is contained in:
Pavlo Ratushnyi
2026-10-24 02:23:48 +02:00
parent ce7eed156a
commit 548b2083e6
3 changed files with 34 additions and 6 deletions
@@ -24,6 +24,8 @@ pub(crate) const E_GDN_AB: &str = "chain_gdn_ab_multi_kernel";
pub(crate) const E_GR_SPLIT: &str = "chain_gr_split";
pub(crate) const E_GR_UP16: &str = "chain_gr_up16";
pub(crate) const E_GR_UP8: &str = "chain_gr_up8";
pub(crate) const E_GR_DOWN_NB2: &str = "chain_gr_down_w8_nb2";
pub(crate) const E_GR_DOWN_NB3: &str = "chain_gr_down_w8_nb3";
pub(crate) const E_QSA_CHUNK: &str = "chain_qsa_decode_chunk";
pub(crate) const E_SWIGLU_QUANT: &str = "chain_swiglu_quant_kernel";
pub(crate) const E_SCALE_SIGMOID: &str = "chain_scale_rows_sigmoid_kernel";
@@ -36,7 +38,7 @@ pub(crate) static CHAIN: AssetSpec = AssetSpec {
name: "chain_window.ptx",
ptx: concat!(include_str!("chain_window.ptx"), "\0").as_bytes(),
sha: [179, 71, 154, 144, 78, 250, 34, 150, 223, 208, 102, 129, 88, 17, 29, 166, 253, 74, 178, 94, 35, 165, 15, 209, 196, 213, 62, 218, 241, 50, 6, 41],
entries: &[E_COPY_I32, E_WAIT_COPY_I32, E_COPY_ROWS, E_DOORBELL, E_GDN_AB, E_GR_SPLIT, E_GR_UP16, E_GR_UP8, E_QSA_CHUNK, E_SWIGLU_QUANT, E_SCALE_SIGMOID, E_SAMPLER_GREEDY, E_ROW_TOP_PROB, E_QSA_MERGE_N],
entries: &[E_COPY_I32, E_WAIT_COPY_I32, E_COPY_ROWS, E_DOORBELL, E_GDN_AB, E_GR_SPLIT, E_GR_UP16, E_GR_UP8, E_GR_DOWN_NB2, E_GR_DOWN_NB3, E_QSA_CHUNK, E_SWIGLU_QUANT, E_SCALE_SIGMOID, E_SAMPLER_GREEDY, E_ROW_TOP_PROB, E_QSA_MERGE_N],
unsealed: "Original verifier asset unsealed: chain_window.ptx (latency-hoisted chain kernels)",
};
@@ -99,10 +101,11 @@ pub(crate) fn with_flags<R>(flags: ChainFlags, f: impl FnOnce() -> R) -> R {
r
}
/// `LELEKA_CHAIN_UP8=1`: the hyper-connection up projection with 8 columns per block (320 blocks) instead of 16 (160).
/// The hyper-connection up projection with 8 columns per block (320 blocks, 5.7 us at 4 rows against 7.7 us for 16 columns and 8.9 us for
/// the Original's, job 9c1); `LELEKA_CHAIN_UP8=0` selects the 16-column shape.
pub(crate) fn up8() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var("LELEKA_CHAIN_UP8").is_ok_and(|v| v == "1"))
*ON.get_or_init(|| std::env::var("LELEKA_CHAIN_UP8").map_or(true, |v| v != "0"))
}
/// The chain asset when any group is on (the window preloads it before capture).
@@ -122,7 +125,7 @@ mod tests {
fn the_chain_asset_is_sealed_and_exports_every_entry() {
CHAIN.assert_sealed_and_fail_closed();
assert!(CHAIN.sealed().is_ok());
assert_eq!(CHAIN.entries.len(), 14);
assert_eq!(CHAIN.entries.len(), 16);
}
#[test]
@@ -256,6 +256,12 @@ pub(crate) fn shared_attributes(cfg: &GrConfig) -> Vec<(&'static AssetSpec, &'st
if let Some(bytes) = cfg.attr_staged {
out.push((&GR_FUSED, E_STAGED, bytes as usize));
}
if super::chain::flags().gr {
// the chain down kernels stage up to 3 x 6 tokens (92,160 B) or 2 x 8 tokens (81,920 B) of activations
let bytes = cfg.usable_shared.min(3 * 6 * (STAGED_TILE * 4)) as usize;
out.push((&super::chain::CHAIN, super::chain::E_GR_DOWN_NB3, bytes));
out.push((&super::chain::CHAIN, super::chain::E_GR_DOWN_NB2, cfg.usable_shared.min(2 * 8 * (STAGED_TILE * 4)) as usize));
}
out
}
@@ -503,6 +509,24 @@ pub(crate) fn plan_hc_read(
// the card cannot launch it (Original would fail the launch)
return Err(Error::InvalidProjectionShape);
}
if chain_gr && staged {
// same arithmetic as the staged kernel; the whole weight row in registers and three (two above six tokens) tile buffers in flight
let (entry, buffers) = if ct <= 6 { (super::chain::E_GR_DOWN_NB3, 3) } else { (super::chain::E_GR_DOWN_NB2, 2) };
let shared = buffers * ct as u32 * (STAGED_TILE * 4);
if shared > cfg.usable_shared {
return Err(Error::InvalidProjectionShape);
}
down.push(Launch::new(
&super::chain::CHAIN,
entry,
[DOWN_BLOCKS, 1, 1],
[THREADS, 1, 1],
shared,
vec![A::Words(chunk_words(&full, n, c0, ct))],
)?);
c0 += ct;
continue;
}
down.push(Launch::new(
&GR_FUSED,
cfg.down_entry(),
@@ -521,8 +521,9 @@ mod tests {
let on = chain::with_flags(ChainFlags { glue: true, ab: true, gr: true, qsa: true, tiny: true, head: true, mtp: true }, || record(4, 1, false));
// every hc read: split norm -> (unchanged staged down) -> up; 96 reads per window
assert_eq!(on.iter().filter(|s| *s == "chain_gr_split").count(), 96 + 0);
assert_eq!(on.iter().filter(|s| *s == "chain_gr_up16").count(), 96);
assert_eq!(on.iter().filter(|s| *s == "mtp_scalar_gr_staged").count(), 96, "the down projection keeps the staged kernel");
assert_eq!(on.iter().filter(|s| *s == "chain_gr_up8").count(), 96);
assert_eq!(on.iter().filter(|s| s.starts_with("chain_gr_down_w8_nb")).count(), 96, "the down projection is the 8-warp register-row kernel");
assert!(!on.iter().any(|s| s == "mtp_scalar_gr_staged"));
assert!(!on.iter().any(|s| s == "mtp_scalar_gr_split" || s == "mtp_scalar_gr_up"));
assert_eq!(on.iter().filter(|s| *s == "chain_qsa_decode_chunk").count(), 12);
assert!(!on.iter().any(|s| s == "leleka_qsa_decode_chunk"));