Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,39 @@ AMD cards clock down in the gaps while the other card works; `echo high >
/sys/class/drm/cardN/device/power_dpm_force_performance_level` (as root, per card) took a two-card split from 44 to 57
tok/s in one report.

### Inkling 2 on several GPUs

Inkling 2 is the model this work targets, and `--device 0,1` is opt-in: the default is always one card, even on a
machine with two. With two cards the 24 expert layers are placed as whole slabs (the load line says how many and
where), a layer's FFN runs on the card that holds it, and the attention and tail stay on GPU 0.

`STRATA_DS_FAST_KERNELS` selects the kernels:

- `auto` (default): the tiled controllers, `ink_attn_128` and the 16-query prefill (`kp16`) only when the run has more
than one device. One card keeps the `main` kernels, so the greedy output does not change.
- `1`: force them on one card (the one-card kernel numbers in the PR).
- `0`: force the old kernels on two cards (the A/B).

Per-kernel overrides: `STRATA_CTRL_TILE=0` (per-token controllers), `STRATA_DS_ATTN_OLD=1` (scalar decode and
one-query-a-warp prefill), `STRATA_DS_ATTN_KP4=1` (four-query prefill).

The two-card plan uses these knobs:

| variable | default | effect |
|---|---|---|
| `STRATA_DS_FAST_KERNELS` | `auto` | as above; one card keeps the old kernels unless forced |
| `STRATA_DS_PLAIN_FIRST` | unset; `chat`/`serve`/`bench` set `1` | keep the plain weights on GPU 0, where the decode state lives |
| `STRATA_DS_PF_TOKENS` | 256 one card, 512 two | tokens of one prompt chunk (at most 2048) |
| `STRATA_DS_KV_TOKENS` | `--ctx` | KV rows GPU 0 must still be able to take before it gives up a slab |
| `STRATA_DS_PROF` | unset | print `inkling2: preloaded N/24 ...` and the per-chunk profile |
| `STRATA_DS_NO_AFFINITY` | unset | pick the GPU per expert instead of per stacked tensor |
| `STRATA_DS_NO_SEG_GRAPH` | unset | do not capture the segmented decode graph |
| `STRATA_CTRL_TILE` | follows `FAST_KERNELS` | `0` forces the per-token controllers |
| `STRATA_DS_ATTN_OLD` | off | `1` keeps the scalar decode and one-query-a-warp prefill |
| `STRATA_DS_ATTN_KP4` | off | `1` selects the four-query 128-key prefill for the A/B |

`STRATA_INKLING2_MODEL` (path to the GGUF) only selects the model for `tests/inkling2_forward.rs`.

## Page cache

Experts that do not fit in VRAM are read from the GGUF through the page cache. A file system that keeps file data out
Expand Down
375 changes: 372 additions & 3 deletions src/cuda/kernels.cu

Large diffs are not rendered by default.

125 changes: 112 additions & 13 deletions src/cuda/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,57 @@ pub(crate) const QTYPES: [GgmlType; 29] = {
use GgmlType::*;
[F32, BF16, F8E4M3, Q8_0, Q2K, Q3K, Q4K, Q5K, Q6K, IQ1S, IQ1M, IQ2XXS, IQ2XS, IQ2S, IQ3XXS, IQ3S, IQ4XS, IQ4NL, Q2_0, TQ1_0, TQ2_0, TQ2T, TQK6, TQK7, Q4_1, Q5_1, Q4_0, Q5_0, NVFP4]
};
pub(crate) const KERNELS: [&str; 58] = [
pub(crate) const KERNELS: [&str; 64] = [
"rope_freq", "qsa_pool", "qsa_score", "qsa_select", "rms_norm", "quant_xq", "argmax_part", "argmax_rows", "l2_norm", "rope_neox", "rms_norm_rope", "add", "swiglu", "situ", "situ_q", "softcap", "swiglu_q", "ssm_gates", "conv_silu", "deltanet_seq", "norm_gate",
"kv_store", "attn_merge", "router_topk", "moe_combine", "moe_route", "conv_seq", "conv_state",
"moe_combine_seq", "moe_prefetch", "attn_prefill_64", "attn_prefill_128", "attn_prefill_256", "quant_q8",
"hc_silu", "hc_mix", "hc_write", "ple_gate", "ple_conv", "dflash2_conv", "hadamard",
"ink_mix", "ink_ctrl_gate", "ink_ctrl_out", "ink_conv", "ink_conv_batch", "ink_clip", "ink_attn", "ink_attn_merge",
"ink_attn_prefill_32", "ink_attn_prefill_64", "ink_attn_prefill_128", "ink_attn_prefill_sh_32", "ink_attn_prefill_sh_64", "ink_attn_prefill_sh_128", "ink_ffn_sum", "ink_route", "attn_sel",
"ink_mix", "ink_ctrl_gate", "ink_ctrl_out", "ink_ctrl_gate_b", "ink_ctrl_out_b", "ink_conv", "ink_conv_batch", "ink_clip", "ink_attn", "ink_attn_128", "ink_attn_merge",
"ink_attn_prefill_32", "ink_attn_prefill_64", "ink_attn_prefill_128", "ink_attn_prefill_128_old", "ink_attn_prefill_128_kp", "ink_attn_prefill_128_kp16", "ink_attn_prefill_sh_32", "ink_attn_prefill_sh_64", "ink_attn_prefill_sh_128", "ink_ffn_sum", "ink_route", "attn_sel",
];

pub use crate::backend::{ATT_GQA_MIN, ATT_SPLITS, GEMM_BM, GpuTensor};
use crate::backend::ATT_SPLITS_1;

/// Kernel-choice flags read from the environment once, when the `Gpu` is made: they sit on the token path, so a
/// lookup per launch is not worth it.
#[derive(Clone, Copy)]
struct InkFlags {
/// `STRATA_DS_FAST_KERNELS=auto` (default) uses the tiled controllers, `ink_attn_128` and `kp16` only when
/// the run has more than one device; `1` forces them on, `0` forces the old kernels. One card keeps the
/// `main` kernels (the greedy output must not change).
fast: bool,
/// `STRATA_CTRL_TILE=0` forces the per-token `ink_ctrl_*` pair; unset follows `fast`.
ctrl_tile: bool,
/// `STRATA_DS_ATTN_OLD=1` keeps the scalar/one-query-a-warp attention kernels for a measurement window.
attn_old: bool,
/// `STRATA_DS_ATTN_KP4=1` selects the four-query 128-key tile prefill kernel.
attn_kp4: bool,
}

impl InkFlags {
fn from_parts(fast: Option<&str>, multi: bool, ctrl: Option<&str>, attn_old: Option<&str>, attn_kp4: Option<&str>) -> Self {
let on = |v: Option<&str>, default: bool| v.map_or(default, |v| v != "0");
let fast = match fast {
None | Some("auto") => multi,
Some("0") => false,
Some(_) => true,
};
Self { fast, ctrl_tile: on(ctrl, fast), attn_old: on(attn_old, false), attn_kp4: on(attn_kp4, false) }
}
fn from_env() -> Self {
let multi = std::env::var("STRATA_DS_DEVICES")
.map_or(false, |v| v.split(',').filter(|d| !d.trim().is_empty()).count() > 1);
Self::from_parts(
std::env::var("STRATA_DS_FAST_KERNELS").ok().as_deref(),
multi,
std::env::var("STRATA_CTRL_TILE").ok().as_deref(),
std::env::var("STRATA_DS_ATTN_OLD").ok().as_deref(),
std::env::var("STRATA_DS_ATTN_KP4").ok().as_deref(),
)
}
}

pub struct Gpu {
pub ctx: Arc<CudaContext>,
pub stream: Arc<CudaStream>,
Expand All @@ -47,6 +86,8 @@ pub struct Gpu {
gd: std::sync::OnceLock<CudaSlice<u8>>,
/// The kernels were compiled for KV cache mode (0 = f16, 1 = q8, 2 = q4).
pub kv_mode: u32,
/// The environment's kernel-choice flags, read once at creation.
flags: InkFlags,
/// The GPU has int8 tensor-core mma (sm_80+). Turing runs the prefill GEMM on the f16 path instead.
pub int8_mma: bool,
/// sm_75: m16n8k8 mma, 64 KB of shared memory a block (no int8 mma, unlike sm_80+; unlike sm_70 it has no m8n8k4).
Expand Down Expand Up @@ -532,7 +573,7 @@ impl Gpu {
let am = stream.alloc_zeros(64 * AMAX_PARTS)?;
let ring = std::sync::Mutex::new((unsafe { ctx.alloc_pinned::<u8>(RING)? }, 0, VecDeque::new()));
let done = ctx.new_event(None)?;
Ok(Gpu { ctx, side: stream.fork()?, stream, nb: AtomicUsize::new(usize::MAX), fns, i8_gemm, q8, gd: Default::default(), kv_mode, int8_mma, turing, max_smem: max_optin, am, graphs: Default::default(), ring, done, lag: Default::default(), fr: Default::default() })
Ok(Gpu { ctx, side: stream.fork()?, stream, nb: AtomicUsize::new(usize::MAX), fns, i8_gemm, q8, gd: Default::default(), kv_mode, flags: InkFlags::from_env(), int8_mma, turing, max_smem: max_optin, am, graphs: Default::default(), ring, done, lag: Default::default(), fr: Default::default() })
}

fn f(&self, name: &str) -> &CudaFunction { &self.fns[name] }
Expand Down Expand Up @@ -781,6 +822,16 @@ impl Gpu {
}
}

impl Gpu {
/// The decode attention entry point and its key positions per block: the f16 head_dim 128 kernel scores a
/// 128-key tile a block, the scalar one 64 keys (`STRATA_DS_ATTN_OLD=1` keeps it for a measurement window;
/// the flag is cached in `flags` at `Gpu` creation). The 128-key kernel keeps the relative-bias row in 32
/// shared floats, so a wider `nr` uses the scalar kernel.
pub(crate) fn ink_attn_kernel(&self, hd: usize, nr: usize) -> (&'static str, usize) {
if hd == 128 && nr <= 32 && self.kv_mode == 0 && self.flags.fast && !self.flags.attn_old { ("ink_attn_128", 128) } else { ("ink_attn", 64) }
}
}

impl Backend for Gpu {
fn name(&self) -> &'static str { "cuda" }
fn device(&self) -> String { self.ctx.name().unwrap_or_default() }
Expand All @@ -799,6 +850,7 @@ impl Backend for Gpu {
q8,
gd: Default::default(),
kv_mode: self.kv_mode,
flags: self.flags,
int8_mma: self.int8_mma,
turing: self.turing,
max_smem: self.max_smem,
Expand Down Expand Up @@ -1106,6 +1158,18 @@ impl Backend for Gpu {
Ok(())
}
fn ink_ctrl_n(&self, down: u64, gain: u64, forget: u64, cur: u64, nins: usize, msg: u64, gate: u64, out: u64, n: usize, rank: usize, count: usize, nt: usize) -> anyhow::Result<()> {
// A prompt chunk uses the tiled pair (a weight row is read once per 64 tokens); a decoded token the
// per-token pair, which has no tile to reuse. Both pairs read and write the same gate layout.
// STRATA_CTRL_TILE=0 forces the per-token pair, to compare the two on the same text (the flag is cached
// in `flags` at `Gpu` creation).
let tiled = nt >= 64 && rank <= 64 && n % 64 == 0 && self.flags.ctrl_tile;
if tiled {
let g1 = cfg((count as u32, nt.div_ceil(64) as u32), 256, 0);
let g2 = LaunchConfig { grid_dim: (count as u32, n.div_ceil(64) as u32, nt.div_ceil(64) as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
launch!(self, "ink_ctrl_gate_b", g1, down, cur, nins as i32, msg, gate, n as i32, rank as i32, nt as i32);
launch!(self, "ink_ctrl_out_b", g2, gain, forget, gate, cur, nins as i32, msg, out, n as i32, rank as i32, nt as i32);
return Ok(());
}
let g1 = LaunchConfig { grid_dim: (rank as u32, count as u32, nt as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
let g2 = LaunchConfig { grid_dim: (n.div_ceil(256) as u32, count as u32, nt as u32), block_dim: (256, 1, 1), shared_mem_bytes: 0 };
launch!(self, "ink_ctrl_gate", g1, down, cur, nins as i32, msg, gate, n as i32, rank as i32, nt as i32);
Expand All @@ -1130,7 +1194,7 @@ impl Backend for Gpu {
pos0: usize, log_alpha: f32, log_floor: f32,
) -> anyhow::Result<()> {
anyhow::ensure!(w <= 128, "ink_clip_n: w={w} > 128");
anyhow::ensure!(kv_mode() == 0 || dst == 0 || w % 32 == 0, "ink_clip_n: quantized KV requires w % 32 == 0 (got {w})");
anyhow::ensure!(self.kv_mode == 0 || dst == 0 || w % 32 == 0, "ink_clip_n: quantized KV requires w % 32 == 0 (got {w})");
let ring = if dst != 0 { dst_ring.max(1) } else { 1 };
let fl = if log_alpha == 0.0 && log_floor == 0.0 { 1.0 } else { log_floor };
launch!(self, "ink_clip", cfg((rows as u32, nt as u32), 128, 0),
Expand All @@ -1144,22 +1208,24 @@ impl Backend for Gpu {
pos_ptr: u64, log_alpha: f32, log_floor: f32,
) -> anyhow::Result<()> {
anyhow::ensure!(w <= 128, "ink_clip_pos: w={w} > 128");
anyhow::ensure!(kv_mode() == 0 || dst == 0 || w % 32 == 0, "ink_clip_pos: quantized KV requires w % 32 == 0 (got {w})");
anyhow::ensure!(self.kv_mode == 0 || dst == 0 || w % 32 == 0, "ink_clip_pos: quantized KV requires w % 32 == 0 (got {w})");
let ring = if dst != 0 { dst_ring.max(1) } else { 1 };
let fl = if log_alpha == 0.0 && log_floor == 0.0 { 1.0 } else { log_floor };
launch!(self, "ink_clip", cfg((rows as u32, nt as u32), 128, 0),
x, scale, w as i32, thr, eps, mul, dst, 0i32, ring as i32, 0i32, log_alpha, fl, pos_ptr);
Ok(())
}
fn ink_attn(&self, q: u64, r: u64, sink: u64, gate: u64, k: u64, v: u64, bias: u64, sc: u64, out: u64, rows: usize, at: usize, nh: usize, nkv: usize, hd: usize, nr: usize, ext: usize) -> anyhow::Result<()> {
let chunks = rows.div_ceil(64) as u32;
launch!(self, "ink_attn", cfg((nh as u32, chunks), 128, 0), q, r, sink, k, v, bias, sc, rows as i32, at as i32, 1i32, nkv as i32, hd as i32, nr as i32, ext as i32, 0u64);
let (kn, ck) = self.ink_attn_kernel(hd, nr);
let chunks = rows.div_ceil(ck) as u32;
launch!(self, kn, cfg((nh as u32, chunks), 128, 0), q, r, sink, k, v, bias, sc, rows as i32, at as i32, 1i32, nkv as i32, hd as i32, nr as i32, ext as i32, 0u64);
launch!(self, "ink_attn_merge", cfg((nh as u32, 1), 128, 0), sc, gate, out, chunks as i32, hd as i32);
Ok(())
}
fn ink_attn_pos(&self, q: u64, r: u64, sink: u64, gate: u64, k: u64, v: u64, bias: u64, sc: u64, out: u64, max_rows: usize, ring: usize, pos_ptr: u64, nh: usize, nkv: usize, hd: usize, nr: usize, ext: usize) -> anyhow::Result<()> {
let chunks = max_rows.div_ceil(64) as u32;
launch!(self, "ink_attn", cfg((nh as u32, chunks), 128, 0), q, r, sink, k, v, bias, sc, 0i32, 0i32, ring.max(1) as i32, nkv as i32, hd as i32, nr as i32, ext as i32, pos_ptr);
let (kn, ck) = self.ink_attn_kernel(hd, nr);
let chunks = max_rows.div_ceil(ck) as u32;
launch!(self, kn, cfg((nh as u32, chunks), 128, 0), q, r, sink, k, v, bias, sc, 0i32, 0i32, ring.max(1) as i32, nkv as i32, hd as i32, nr as i32, ext as i32, pos_ptr);
launch!(self, "ink_attn_merge", cfg((nh as u32, 1), 128, 0), sc, gate, out, chunks as i32, hd as i32);
Ok(())
}
Expand All @@ -1169,7 +1235,18 @@ impl Backend for Gpu {
) -> anyhow::Result<()> {
anyhow::ensure!(matches!(hd, 32 | 64 | 128), "ink_attn_prefill: unsupported hd={hd}");
let g = nh / nkv.max(1);
if win == 0 && nkv > 0 && nh % nkv == 0 && g <= 4 {
if hd == 128 && self.kv_mode == 0 && (self.flags.fast || self.flags.attn_kp4) && !self.flags.attn_old {
// The key-parallel kernels: kp16 (16 queries a block) by default, kp (4) for the A/B. The wrapper
// falls back to the four-query template for a relative-bias width it cannot read as float4.
let (kn, qb) = if self.flags.attn_kp4 { ("ink_attn_prefill_128_kp", 4) } else { ("ink_attn_prefill_128_kp16", 16) };
launch!(self, kn, cfg((nt.div_ceil(qb) as u32, nh as u32), 128, 0),
q, r, sink, gate, kc, vc, kc_new, vc_new, bias, out,
pos as i32, nt as i32, nh as i32, nkv as i32, nr as i32, ext as i32, ring.max(1) as i32, win as i32);
} else if hd == 128 && self.flags.attn_old {
launch!(self, "ink_attn_prefill_128_old", cfg((nt.div_ceil(4) as u32, nh as u32), 128, 0),
q, r, sink, gate, kc, vc, kc_new, vc_new, bias, out,
pos as i32, nt as i32, nh as i32, nkv as i32, nr as i32, ext as i32, ring.max(1) as i32, win as i32);
} else if win == 0 && nkv > 0 && nh % nkv == 0 && g <= 4 {
// Every key: a block is one K/V head and `tb` tokens, with 32 keys of K and V in shared memory.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Correctness · 1 of 4

The new sh prefill kernel is chosen for every full-attention layer, not only with fast kernels, so one card changes kernels. The branch win == 0 && nkv > 0 && g <= 4 routes hd 32/64/128 full-attention prefill to ink_attn_prefill_sh_* without checking self.flags.fast. A single-card run with default flags now uses a different kernel from main, although the PR says one card keeps the main kernels so greedy output does not change. Any bit difference shows up in the output.

Prompt for your AI agent
File: src/cuda/mod.rs
Line: 1250
Symbol: attn_sel
Issue: The new sh prefill kernel is chosen for every full-attention layer, not only with fast kernels, so one card changes kernels. The branch `win == 0 && nkv > 0 && g <= 4` routes hd 32/64/128 full-attention prefill to `ink_attn_prefill_sh_*` without checking `self.flags.fast`. A single-card run with default flags now uses a different kernel from main, although the PR says one card keeps the main kernels so greedy output does not change. Any bit difference shows up in the output.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Correctness · 2 of 4

The sh branch has no kv_mode guard, so quantized K/V takes the f16-only path. The first branch only uses the key-parallel kernels when kv_mode == 0, and the old code sent kv_mode != 0 at hd 128 to the _kp kernel. The new fall-through sends a q8/q4 cache with win == 0 and g <= 4 to ink_attn_prefill_sh_*, which has no kv_mode check and reads the rows as f16. The result is garbage attention or an out-of-bounds read.

Prompt for your AI agent
File: src/cuda/mod.rs
Line: 1250
Symbol: attn_sel
Issue: The sh branch has no kv_mode guard, so quantized K/V takes the f16-only path. The first branch only uses the key-parallel kernels when `kv_mode == 0`, and the old code sent `kv_mode != 0` at hd 128 to the `_kp` kernel. The new fall-through sends a q8/q4 cache with win == 0 and g <= 4 to `ink_attn_prefill_sh_*`, which has no kv_mode check and reads the rows as f16. The result is garbage attention or an out-of-bounds read.
Make the smallest change that fixes this, add a test, and change nothing unrelated.

Reply /dismiss (optionally with a reason) if this isn't helpful - Aletheore won't raise it again on this repo.

let tb = (32 / g).min(8);
launch!(self, &format!("ink_attn_prefill_sh_{hd}"), cfg((nt.div_ceil(tb) as u32, nkv as u32), (32 * tb * g) as u32, (2 * 32 * hd * 4) as u32),
Expand Down Expand Up @@ -1539,8 +1616,8 @@ mod tests {
#[test]
fn kernel_names() {
let (sm70, sm80) = (super::kernel_names(false), super::kernel_names(true));
assert_eq!(sm80.len(), 397);
assert_eq!(sm70.len(), 328);
assert_eq!(sm80.len(), 403);
assert_eq!(sm70.len(), 334);
assert!(sm70.iter().all(|n| !n.starts_with("gemmi") && !n.starts_with("gemmd")),
"sm_70 must not load the int8 prefill GEMM: {:?}",
sm70.iter().filter(|n| n.starts_with("gemmi")).collect::<Vec<_>>());
Expand Down Expand Up @@ -1717,4 +1794,26 @@ mod tests {
assert_eq!(super::smem_log("attn_decode_*", 98304, 2112, None, None),
"cuda shared memory: opt-in 98304 B per block, attn_decode_*: 2112 B static, dynamic max default (48 KiB), carveout default");
}

/// `STRATA_CTRL_TILE` is read as text: unset or any value but "0" keeps the tiled `ink_ctrl_*` pair. The
/// attention flags are plain presence checks and must pass through unchanged.
#[test]
fn ink_flags_parse_ctrl_tile() {
// One card: the default is the old kernels (greedy output must match main).
let one = super::InkFlags::from_parts(None, false, None, None, None);
assert!(!one.fast && !one.ctrl_tile && !one.attn_old && !one.attn_kp4);
// Two cards: `auto` turns the fast kernels on.
let two = super::InkFlags::from_parts(None, true, None, None, None);
assert!(two.fast && two.ctrl_tile && !two.attn_old && !two.attn_kp4);
// Explicit opt-in/opt-out and per-kernel overrides.
assert!(super::InkFlags::from_parts(Some("1"), false, None, None, None).fast);
assert!(!super::InkFlags::from_parts(Some("0"), true, None, None, None).fast);
assert!(!super::InkFlags::from_parts(None, true, Some("0"), None, None).ctrl_tile);
let old = super::InkFlags::from_parts(None, true, None, Some("1"), None);
assert!(old.attn_old && !old.attn_kp4);
let kp4 = super::InkFlags::from_parts(None, true, None, None, Some("1"));
assert!(kp4.attn_kp4 && !kp4.attn_old);
let off = super::InkFlags::from_parts(None, true, Some("0"), Some("0"), Some("0"));
assert!(!off.ctrl_tile && !off.attn_old && !off.attn_kp4);
}
}
Loading