From 0c30105d4297c2f58f5a35d3deb51145fc2a0690 Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Mon, 9 Nov 2026 14:49:06 +0100 Subject: [PATCH] Speed program: SIDE_MIXER QSA part (LELEKA_SM120_SIDE_QSA, experimental, off): indexer and query chains of each QSA layer on the two side streams Co-Authored-By: Claude Sonnet 5.5 --- .../src/original_verify_window_ops.rs | 4 +- .../original_verifier.rs | 7 +-- .../side_qsa_branches.rs | 54 +++++++++++++++++++ .../window_ops_kernels_and_pcie_plan.rs | 31 +++++++++++ .../src/original_verify_window_plan.rs | 6 +++ .../src/original_verify_window_record.rs | 5 +- .../src/original_verify_window_sm120.rs | 20 +++++-- scripts/e2e/codex/speed/spd_ab_job.sh | 7 +-- 8 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 crates/cuda-driver/src/original_verify_window_ops/side_qsa_branches.rs diff --git a/crates/cuda-driver/src/original_verify_window_ops.rs b/crates/cuda-driver/src/original_verify_window_ops.rs index 7e6242b0..c5836b92 100644 --- a/crates/cuda-driver/src/original_verify_window_ops.rs +++ b/crates/cuda-driver/src/original_verify_window_ops.rs @@ -29,7 +29,7 @@ use super::planner::{Planner, WindowWeights}; use super::proj::{self, GdnProjectionAddrs, OriginalVerifyProjBinding, QsaProjectionAddrs}; use super::qsa::{self, OriginalVerifyQsaBinding, OriginalVerifyQsaKvBinding, QsaLayerAddrs, QsaRope}; use super::sm120; -use super::window_plan::gdn_index; +use super::window_plan::{gdn_index, qsa_layer}; use super::window_plan::{PlanLayout, WINDOW_LAYERS, staging_blobs_per_group}; use super::window_record::{ExpertTier, HcHalf, Projection, QuantSource, Rows, WindowOps, WindowPolicy, record_window}; use super::*; @@ -106,6 +106,8 @@ pub use all_window_assets::unsealed_window_assets; mod original_verifier; #[path = "original_verify_window_ops/original_verify_window_ops_methods.rs"] mod original_verify_window_ops_methods; +#[path = "original_verify_window_ops/side_qsa_branches.rs"] +mod side_qsa_branches; #[path = "original_verify_window_ops/window_ops_kernels_and_pcie_plan.rs"] mod window_ops_kernels_and_pcie_plan; #[allow(unused_imports)] diff --git a/crates/cuda-driver/src/original_verify_window_ops/original_verifier.rs b/crates/cuda-driver/src/original_verify_window_ops/original_verifier.rs index 57a03b72..65e7fc0f 100644 --- a/crates/cuda-driver/src/original_verify_window_ops/original_verifier.rs +++ b/crates/cuda-driver/src/original_verify_window_ops/original_verifier.rs @@ -26,7 +26,7 @@ impl<'a, 'ctx> OriginalVerifier<'ctx, OriginalVerifyWindowResources<'a, 'ctx>> { let mut resources = resources; resources.prepare(&policy)?; // SIDE_MIXER: this recording's capture creates the two side streams (a split window just leaves them unused) - crate::original_gpu_schedule::set_side_streams_wanted(policy.side_mixer); + crate::original_gpu_schedule::set_side_streams_wanted(policy.side_mixer || policy.side_qsa); let recorded = self.record_window(t, resources, |capture, resources, groups| { // The recorder re-validates the shape; the groups come from `set_split`. let mut ops = OriginalVerifyWindowOps { capture, resources, t, groups, policy }; @@ -81,12 +81,13 @@ fn resolve_sm120_switches(resources: &OriginalVerifyWindowResources<'_, '_>, pol policy.qsa.attn_daily = (policy.qsa.attn_daily || want.attn_daily) && sm120::ATTN_DAILY.sealed().is_ok(); policy.gu_swiglu = (policy.gu_swiglu || want.gu_swiglu) && sm120::GU_SWIGLU.sealed().is_ok(); policy.side_mixer = policy.side_mixer || want.side_mixer; + policy.side_qsa = policy.side_qsa || want.side_qsa; static SAID: std::sync::Once = std::sync::Once::new(); SAID.call_once(|| { eprintln!( - "leleka-serve sm120 switches: attn_daily={} gu_swiglu={} side_mixer={} \ + "leleka-serve sm120 switches: attn_daily={} gu_swiglu={} side_mixer={} side_qsa={} \ (compute capability {major}.{minor}, requested {want:?})", - policy.qsa.attn_daily, policy.gu_swiglu, policy.side_mixer + policy.qsa.attn_daily, policy.gu_swiglu, policy.side_mixer, policy.side_qsa ) }); Ok(()) diff --git a/crates/cuda-driver/src/original_verify_window_ops/side_qsa_branches.rs b/crates/cuda-driver/src/original_verify_window_ops/side_qsa_branches.rs new file mode 100644 index 00000000..28029097 --- /dev/null +++ b/crates/cuda-driver/src/original_verify_window_ops/side_qsa_branches.rs @@ -0,0 +1,54 @@ +//! SIDE_MIXER, QSA part (upstream `STRATA_SIDE_MIXER=2`): the branches of a sparse-attention layer of the graph. +//! +//! In the literal order the layer is one chain on the capture stream. Its indexer chain (indexer key, tail snapshot, +//! indexer append, indexer query path, block scores and top-k) reads only the layer's mixed activations and the +//! indexer's own resident state, and its query chain (query projection, q/gate split, norm and RoPE) reads only the +//! q8_1 blocks of the mixed activations. Neither touches what the K/V projections, the K norm/RoPE and the K/V append +//! write (`kcur`, `vcur`, the KV pools), so both fork from the capture stream right after the layer's quantization and +//! join where they are first read: the indexer chain before the KV resolve (it selects the blocks), the query chain +//! before the decode attention. The launches are the literal ones and run in the same order inside each chain, so the +//! outputs are bitwise those of the in-line order; only the graph's dependency edges change. + +#[allow(unused_imports)] +use super::*; + +/// One sparse-attention layer of a window: its index among the QSA layers, the window rows and whether the rows run as +/// one batched launch. +#[derive(Clone, Copy)] +pub(super) struct QsaSideLayer { + pub(super) q: usize, + pub(super) rows: Rows, + pub(super) batched: bool, +} + +impl<'a, 'ctx> OriginalVerifyWindowOps<'_, '_, 'a, 'ctx> { + /// The QSA layers run their indexer and query chains on side streams (not in a split window, as upstream). + pub(super) fn side_qsa(&self) -> bool { + self.policy.side_qsa && self.groups == 1 && self.capture.has_side() + } + /// Fork the indexer chain on side stream 0 and the query chain on side stream 1. + pub(super) fn issue_qsa_side(&mut self, layer: QsaSideLayer) -> Result<()> { + self.capture.side_fork(0)?; + self.issue_indexer_chain(layer)?; + self.capture.side_end(0)?; + self.capture.side_fork(1)?; + self.issue_query_chain(layer)?; + self.capture.side_end(1) + } + /// The indexer's key, tail snapshot (before the first append), append, query path, block scores and top-k. + fn issue_indexer_chain(&mut self, layer: QsaSideLayer) -> Result<()> { + let QsaSideLayer { q, rows, batched } = layer; + self.plan_launch(|p| p.qsa_index_k(q, rows, batched))?; + let base = &self.resources.base; + base.qsa.capture_tail_snapshot(&base.journal, self.capture, q)?; + base.qsa.capture_append_rows(&base.journal, self.capture, q, rows.begin..rows.end)?; + self.plan_launch(|p| p.qsa_index_q_path(q, rows, batched))?; + self.plan_launch(|p| p.qsa_block_scores_topk(q, rows)) + } + /// The query projection and its q/gate split, norm and RoPE. + fn issue_query_chain(&mut self, layer: QsaSideLayer) -> Result<()> { + let QsaSideLayer { q, rows, batched } = layer; + self.plan_launch(|p| p.mmvq(qsa_layer(q), Projection::QsaQ, rows))?; + self.plan_launch(|p| p.qsa_q_path(q, rows, batched)) + } +} diff --git a/crates/cuda-driver/src/original_verify_window_ops/window_ops_kernels_and_pcie_plan.rs b/crates/cuda-driver/src/original_verify_window_ops/window_ops_kernels_and_pcie_plan.rs index 41d96132..3e084e36 100644 --- a/crates/cuda-driver/src/original_verify_window_ops/window_ops_kernels_and_pcie_plan.rs +++ b/crates/cuda-driver/src/original_verify_window_ops/window_ops_kernels_and_pcie_plan.rs @@ -1,5 +1,7 @@ //! Moved out of the parent module by STRA-338: impl WindowOps_for_OriginalVerifyWindowOps. +#[allow(unused_imports)] +use super::side_qsa_branches::QsaSideLayer; #[allow(unused_imports)] use super::*; @@ -33,6 +35,9 @@ impl WindowOps for OriginalVerifyWindowOps<'_, '_, '_, '_> { self.plan_launch(|p| p.quantize_q8_1(source, rows)) } fn mmvq(&mut self, layer: usize, projection: Projection, rows: Rows) -> Result<()> { + if self.side_qsa() && matches!(projection, Projection::QsaQ) { + return Ok(()); // issued on side stream 1 by `issue_qsa_side` + } if self.side_gdn() { match projection { // SIDE_MIXER: the gates and the gate projection of this GDN layer are issued here, beside the qkv @@ -78,12 +83,20 @@ impl WindowOps for OriginalVerifyWindowOps<'_, '_, '_, '_> { ) } fn qsa_index_k(&mut self, q: usize, rows: Rows, batched: bool) -> Result<()> { + if self.side_qsa() { + // SIDE_MIXER, QSA part: the indexer chain and the query chain of this layer are issued here, beside the + // K/V projections + return self.issue_qsa_side(QsaSideLayer { q, rows, batched }); + } self.plan_launch(|p| p.qsa_index_k(q, rows, batched)) } fn qsa_k_norm_rope(&mut self, q: usize, rows: Rows, batched: bool) -> Result<()> { self.plan_launch(|p| p.qsa_k_norm_rope(q, rows, batched)) } fn qsa_tail_snapshot(&mut self, layer: usize) -> Result<()> { + if self.side_qsa() { + return Ok(()); // issued on side stream 0 by `issue_qsa_side` + } let base = &self.resources.base; base.qsa.capture_tail_snapshot(&base.journal, self.capture, layer) } @@ -91,22 +104,40 @@ impl WindowOps for OriginalVerifyWindowOps<'_, '_, '_, '_> { self.plan_launch(|p| p.qsa_kv_append(q, rows)) } fn qsa_indexer_append(&mut self, layer: usize, rows: Rows) -> Result<()> { + if self.side_qsa() { + return Ok(()); // issued on side stream 0 by `issue_qsa_side` + } let base = &self.resources.base; base.qsa.capture_append_rows(&base.journal, self.capture, layer, rows.begin..rows.end) } fn qsa_q_path(&mut self, q: usize, rows: Rows, batched: bool) -> Result<()> { + if self.side_qsa() { + return Ok(()); // issued on side stream 1 by `issue_qsa_side` + } self.plan_launch(|p| p.qsa_q_path(q, rows, batched)) } fn qsa_index_q_path(&mut self, q: usize, rows: Rows, batched: bool) -> Result<()> { + if self.side_qsa() { + return Ok(()); // issued on side stream 0 by `issue_qsa_side` + } self.plan_launch(|p| p.qsa_index_q_path(q, rows, batched)) } fn qsa_block_scores_topk(&mut self, q: usize, rows: Rows) -> Result<()> { + if self.side_qsa() { + return Ok(()); // issued on side stream 0 by `issue_qsa_side` + } self.plan_launch(|p| p.qsa_block_scores_topk(q, rows)) } fn qsa_kv_resolve(&mut self, q: usize, rows: Rows) -> Result<()> { + if self.side_qsa() { + self.capture.side_join(0)?; // the resolve reads the blocks the indexer chain selected + } self.plan_launch(|p| p.qsa_kv_resolve(q, rows)) } fn qsa_decode_attention(&mut self, q: usize, rows: Rows) -> Result<()> { + if self.side_qsa() { + self.capture.side_join(1)?; // the attention reads the query the query chain produced + } self.plan_launch(|p| p.qsa_decode_attention(q, rows)) } fn qsa_gate(&mut self, q: usize, rows: Rows, batched: bool) -> Result<()> { diff --git a/crates/cuda-driver/src/original_verify_window_plan.rs b/crates/cuda-driver/src/original_verify_window_plan.rs index 5f9ef9a6..d01af694 100644 --- a/crates/cuda-driver/src/original_verify_window_plan.rs +++ b/crates/cuda-driver/src/original_verify_window_plan.rs @@ -28,6 +28,11 @@ pub const fn qsa_index(layer: usize) -> Option { if is_qsa_layer(layer) { Some(layer / 4) } else { None } } +/// Layer index of QSA layer `q` among all layers (the inverse of `qsa_index`). +pub const fn qsa_layer(q: usize) -> usize { + q * 4 + 3 +} + /// Groups of a T-token window: split windows of at least two rows use two. pub const fn window_groups(t: usize, split: bool) -> usize { if split && t >= 2 { 2 } else { 1 } @@ -135,6 +140,7 @@ mod tests { assert_eq!(gdn, (0..36).collect::>(), "GDN indices are dense and ordered"); let q: Vec = (0..WINDOW_LAYERS).filter_map(qsa_index).collect(); assert_eq!(q, (0..12).collect::>()); + assert!((0..12).all(|i| qsa_index(qsa_layer(i)) == Some(i))); assert_eq!((gdn_index(4), gdn_index(3), qsa_index(3), qsa_index(4)), (Some(3), None, Some(0), None)); } diff --git a/crates/cuda-driver/src/original_verify_window_record.rs b/crates/cuda-driver/src/original_verify_window_record.rs index 38e9e013..3f36d17a 100644 --- a/crates/cuda-driver/src/original_verify_window_record.rs +++ b/crates/cuda-driver/src/original_verify_window_record.rs @@ -98,9 +98,9 @@ pub struct WindowPolicy { /// the layer's gate/up format has the fused kernel. Off in `mode17()`; `record_literal_window` resolves it from the /// card and `LELEKA_SM120_GU_SWIGLU`. pub gu_swiglu: bool, - /// sm_120 `SIDE_MIXER` (GDN part of upstream's `STRATA_SIDE_MIXER=2`): the GDN gates and gate projection on side - /// streams of the window's graph. + /// sm_120 `SIDE_MIXER` (upstream `SIDE_MIXER=2`): GDN part `side_mixer`, QSA part `side_qsa`, on side streams. pub side_mixer: bool, + pub side_qsa: bool, } impl WindowPolicy { pub const fn mode17() -> Self { @@ -123,6 +123,7 @@ impl WindowPolicy { qsa: super::qsa::OriginalVerifyQsaPolicy::mode17(), gu_swiglu: false, side_mixer: false, + side_qsa: false, } } pub fn validate_selected(&self) -> Result<()> { diff --git a/crates/cuda-driver/src/original_verify_window_sm120.rs b/crates/cuda-driver/src/original_verify_window_sm120.rs index 481efe61..27b6bcc2 100644 --- a/crates/cuda-driver/src/original_verify_window_sm120.rs +++ b/crates/cuda-driver/src/original_verify_window_sm120.rs @@ -76,12 +76,15 @@ pub(crate) fn gu_swiglu_entry(ty: u32) -> Option<&'static str> { pub(crate) const DEFAULT_ON_ATTN_DAILY: bool = false; pub(crate) const DEFAULT_ON_GU_SWIGLU: bool = false; pub(crate) const DEFAULT_ON_SIDE_MIXER: bool = false; +pub(crate) const DEFAULT_ON_SIDE_QSA: bool = false; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Sm120Switches { pub attn_daily: bool, pub gu_swiglu: bool, pub side_mixer: bool, + /// The QSA part of upstream's `STRATA_SIDE_MIXER=2`: the indexer chain and the query chain on side streams. + pub side_qsa: bool, } fn one(get: &impl Fn(&str) -> Option, var: &str, default: bool) -> bool { @@ -117,10 +120,12 @@ impl Sm120Switches { // the kernels are sm_120 PTX (no sm_86 companion): a card below 12.x never takes a switch, whatever the // variable says let capable = major >= 12; + let switch = |variable: &str, default_on: bool| capable && one(&get, variable, sm120 && default_on); Self { - attn_daily: capable && one(&get, "LELEKA_SM120_ATTN_DAILY", sm120 && DEFAULT_ON_ATTN_DAILY), - gu_swiglu: capable && one(&get, "LELEKA_SM120_GU_SWIGLU", sm120 && DEFAULT_ON_GU_SWIGLU), - side_mixer: capable && one(&get, "LELEKA_SM120_SIDE_MIXER", sm120 && DEFAULT_ON_SIDE_MIXER), + attn_daily: switch("LELEKA_SM120_ATTN_DAILY", DEFAULT_ON_ATTN_DAILY), + gu_swiglu: switch("LELEKA_SM120_GU_SWIGLU", DEFAULT_ON_GU_SWIGLU), + side_mixer: switch("LELEKA_SM120_SIDE_MIXER", DEFAULT_ON_SIDE_MIXER), + side_qsa: switch("LELEKA_SM120_SIDE_QSA", DEFAULT_ON_SIDE_QSA), } } pub fn from_env(major: i32, minor: i32) -> Self { @@ -129,6 +134,7 @@ impl Sm120Switches { attn_daily: overridden(&OVERRIDE_ATTN_DAILY, s.attn_daily), gu_swiglu: overridden(&OVERRIDE_GU_SWIGLU, s.gu_swiglu), side_mixer: overridden(&OVERRIDE_SIDE_MIXER, s.side_mixer), + side_qsa: s.side_qsa, } } } @@ -189,6 +195,14 @@ mod tests { assert!(!Sm120Switches::resolve(12, 0, env(&[("LELEKA_SM120_PERF", "0")])).side_mixer); } + #[test] + fn side_qsa_follows_its_own_variable_and_the_master_switch() { + assert_eq!(Sm120Switches::resolve(12, 0, env(&[])).side_qsa, DEFAULT_ON_SIDE_QSA); + assert!(Sm120Switches::resolve(12, 0, env(&[("LELEKA_SM120_SIDE_QSA", "1")])).side_qsa); + assert!(!Sm120Switches::resolve(8, 6, env(&[("LELEKA_SM120_SIDE_QSA", "1")])).side_qsa); + assert!(!Sm120Switches::resolve(12, 0, env(&[("LELEKA_SM120_SIDE_QSA", "0")])).side_qsa); + } + #[test] fn the_gu_swiglu_asset_is_sealed_and_exports_every_entry() { GU_SWIGLU.assert_sealed_and_fail_closed(); diff --git a/scripts/e2e/codex/speed/spd_ab_job.sh b/scripts/e2e/codex/speed/spd_ab_job.sh index f107d80e..ecffc369 100644 --- a/scripts/e2e/codex/speed/spd_ab_job.sh +++ b/scripts/e2e/codex/speed/spd_ab_job.sh @@ -75,9 +75,10 @@ for v in $VARS; do W3) export LELEKA_ADAPT_DECAY=0.85;; # longer memory of the routing counts (default 0.7) W4) export LELEKA_ADAPT_MARGIN=1.0;; # swap on a smaller count lead (default 1.5) X) export LELEKA_ADAPT_TIE_SPREAD=on;; # equal-gain swaps share the 96-swap cap across layers (needs a binary with the switch: SPD_BIN) - C|CL) [ "$vb" = CL ] && export LELEKA_SM120_ATTN_DAILY=1 LELEKA_SM120_GU_SWIGLU=1; export CUDA_INJECTION64_PATH=$DRT/tools/libleleka_cupti_trace.so LELEKA_CUPTI_TRIGGER=$D/cupti.trigger LCT_MAX_SECONDS=60 LD_LIBRARY_PATH=$DRT/cupti/lib:$LD_LIBRARY_PATH;; # one CUPTI kernel window (WIN_S seconds) over request SPD_TRIG_AT of the replay; a profiling run, never an A/B arm + C|CL|CM) [ "$vb" != C ] && export LELEKA_SM120_ATTN_DAILY=1 LELEKA_SM120_GU_SWIGLU=1; [ "$vb" = CM ] && export LELEKA_SM120_SIDE_MIXER=1; export CUDA_INJECTION64_PATH=$DRT/tools/libleleka_cupti_trace.so LELEKA_CUPTI_TRIGGER=$D/cupti.trigger LCT_MAX_SECONDS=60 LD_LIBRARY_PATH=$DRT/cupti/lib:$LD_LIBRARY_PATH;; # one CUPTI kernel window (WIN_S seconds) over request SPD_TRIG_AT of the replay; a profiling run, never an A/B arm P) export LELEKA_SERVE_PROFILE=1 STRATA_VERIFY_PROFILE=1;; Q) export LELEKA_SERVE_PROFILE=1 LELEKA_BRINGUP_TRACE=1 LELEKA_PREFILL_PROFILE_SYNC=1;; # per-stage prefill timing (not for A/B pairs: the sync drains change timings) + M) export LELEKA_SM120_ATTN_DAILY=1 LELEKA_SM120_GU_SWIGLU=1 LELEKA_SM120_SIDE_MIXER=1;; # L + the verify graph's side streams (SIDE_MIXER); base of the pair is L R) export LELEKA_REFILL_DEFER=on;; # lever 5: the lent-slot refill queued inside the decode phase's assembly (same copies, other time) S) export LELEKA_ROUTE_TAIL_SKIP=7 LELEKA_ROUTE_TAIL_SKIP_MAX_CACHED=100;; T*) export LELEKA_ROUTE_TAIL_SKIP=7 LELEKA_ROUTE_TAIL_SKIP_MAX_CACHED=100; p=${vb#T}; p=${p%[a-z]}; [ "$p" != 45 ] && export LELEKA_ORIGINAL_SERVE_PCIE_FRAC=0.$p;; @@ -110,10 +111,10 @@ for v in $VARS; do done touch "$D/replay.jsonl" else - if [ "$vb" = C ] || [ "$vb" = CL ]; then spd_watch "$D/replay.jsonl" "$D/cupti.trigger" "$D" "$TRIG_AT" & WATCH_PID=$!; fi + if [ "$vb" = C ] || [ "$vb" = CL ] || [ "$vb" = CM ]; then spd_watch "$D/replay.jsonl" "$D/cupti.trigger" "$D" "$TRIG_AT" & WATCH_PID=$!; fi python3 -I "$SPD/tools/replay_hash.py" "${args[@]}" --url http://127.0.0.1:8081 --model qwen-iq3 --key-file "$ST/api-key" --out "$D/replay.jsonl" --max-requests "$MAXR" >"$D/replay.out" 2>&1 log "replay rc=$? $(wc -l <"$D/replay.jsonl") requests" - if [ "$vb" = C ] || [ "$vb" = CL ]; then for _ in $(seq 1 60); do [ -e "$D/cupti.trigger.done" ] && break; sleep 1; done; kill "$WATCH_PID" 2>/dev/null; fi + if [ "$vb" = C ] || [ "$vb" = CL ] || [ "$vb" = CM ]; then for _ in $(seq 1 60); do [ -e "$D/cupti.trigger.done" ] && break; sleep 1; done; kill "$WATCH_PID" 2>/dev/null; fi fi cp "$ST/logs/engine.log" "$D/engine.log" 2>/dev/null; cp "$ST/logs/requests.log" "$D/requests.log" 2>/dev/null stop_stack; kill "$MON_PID" 2>/dev/null; MON_PID=