Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,23 @@ pub trait Backend: Send {
) -> anyhow::Result<()> {
anyhow::bail!("{}: Inkling 2 device decode not implemented", self.name())
}
/// Graph-capturable `ink_clip_n`: reads `pos0` from device `i32` pointer `pos_ptr` (`dst_at0 = pos0 % dst_ring`).
fn ink_clip_pos(
&self, _x: u64, _scale: u64, _w: usize, _rows: usize, _nt: usize,
_thr: f32, _eps: f32, _mul: f32,
_dst: u64, _dst_ring: usize,
_pos_ptr: u64, _log_alpha: f32, _log_floor: f32,
) -> anyhow::Result<()> {
anyhow::bail!("{}: Inkling 2 device decode not implemented", self.name())
}
/// Attention of one token over `rows` key and value rows (`nkv * hd` wide in active KV format; the token's own row is `at`, the
/// row before it `at - 1` modulo `rows`). Logit: q.k / sqrt(hd) + r.bias[distance] (distance < `ext`) - sink, a
/// softmax with one more logit of 0, times sigmoid(gate). `hd` divides 128. `sc`: `nh * rows.div_ceil(64) * (hd + 2)`
/// floats of scratch.
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<()> { anyhow::bail!("{}: Inkling 2 device decode not implemented", self.name()) }
/// Graph-capturable `ink_attn`: reads `pos` from device `i32` pointer `pos_ptr`, sets `rows = min(pos + 1, ring)` and `at = pos % ring`,
/// dispatching `max_rows.div_ceil(64)` chunks.
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<()> { anyhow::bail!("{}: Inkling 2 device decode not implemented", self.name()) }
/// Batched causal Inkling 2 attention over `nt` query tokens starting at position `pos` (`hd` 32/64/128).
/// When `kc_new != 0`, keys/values for `kp >= pos` read row `kp - pos` of `kc_new`/`vc_new` so SWA history `< pos` in `kc`/`vc` is preserved.
/// `q`/`gate`/`out`: `[nt][nh][hd]`, `r`: `[nt][nh][nr]`, `sink`: `[nt][nh]`, `bias`: `[ext][nr]`.
Expand Down
17 changes: 12 additions & 5 deletions src/cuda/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2470,20 +2470,22 @@ __device__ __forceinline__ float ldkv1(const u16* c, size_t off, int hd) {

// One block of 128 per (row, tok) of w values (w at most 128, w % 32 == 0 when KV_MODE > 0 and dst != 0).
extern "C" __global__ void ink_clip(float* x, const float* scale, int w, float thr, float eps, float mul, u16* dst,
int dst_at0, int dst_ring, int pos0, float log_alpha, float log_floor) {
int dst_at0, int dst_ring, int pos0, float log_alpha, float log_floor, const int* pos_ptr) {
int t = threadIdx.x, row = blockIdx.x, tok = blockIdx.y, rows = gridDim.x;
size_t at = ((size_t)tok * rows + row) * w + t, sc_at = (size_t)row * w + t;
float v = 0.f;
if (t < w) { v = x[at]; if (scale) v *= scale[sc_at]; }
float ss = block_sum(v * v);
int eff_pos0 = pos_ptr ? *pos_ptr : pos0;
int eff_dst_at0 = pos_ptr ? (eff_pos0 % dst_ring) : dst_at0;
float eff_mul = mul;
if (log_alpha != 0.f) eff_mul *= 1.f + log_alpha * logf(fmaxf((float)(pos0 + tok + 1) / log_floor, 1.f));
if (log_alpha != 0.f) eff_mul *= 1.f + log_alpha * logf(fmaxf((float)(eff_pos0 + tok + 1) / log_floor, 1.f));
float f = (thr > 0.f ? fminf(thr / (sqrtf(ss) + 1e-6f), 1.f) : 1.f / sqrtf(ss / (float)w + eps)) * eff_mul;
if (t < w) {
v *= f;
x[at] = v;
if (dst) {
size_t dst_pos = (size_t)((dst_at0 + tok) % dst_ring);
size_t dst_pos = (size_t)((eff_dst_at0 + tok) % dst_ring);
size_t dat = (dst_pos * rows + row) * w + t;
kv_put(dst, dat, w, v);
}
Expand All @@ -2492,10 +2494,15 @@ extern "C" __global__ void ink_clip(float* x, const float* scale, int w, float t

// Attention of one token in two launches. Supports KV_MODE 0/1/2/3 via ldkv1().
extern "C" __global__ void ink_attn(const float* q, const float* r, const float* sink, const u16* k, const u16* v,
const float* bias, float* sc, int rows, int at, int nkv, int hd, int nr, int ext) {
const float* bias, float* sc, int rows, int at, int ring, int nkv, int hd, int nr, int ext, const int* pos_ptr) {
__shared__ float qs[128], ps[64], acc[128];
int h = blockIdx.x, kh = h / (gridDim.x / nkv), kw = nkv * hd, t = threadIdx.x, lane = t & 31;
int i0 = blockIdx.y * 64, n = min(64, rows - i0);
if (pos_ptr) {
int pos = *pos_ptr;
rows = min(pos + 1, ring);
at = pos % ring;
}
int i0 = blockIdx.y * 64, n = i0 < rows ? min(64, rows - i0) : 0;
if (t < hd) qs[t] = q[h * hd + t];
__syncthreads();
float scale = 1.f / sqrtf((float)hd);
Expand Down
24 changes: 22 additions & 2 deletions src/cuda/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,32 @@ impl Backend for Gpu {
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, dst_at0 as i32, ring as i32, pos0 as i32, log_alpha, fl);
x, scale, w as i32, thr, eps, mul, dst, dst_at0 as i32, ring as i32, pos0 as i32, log_alpha, fl, 0u64);
Ok(())
}
fn ink_clip_pos(
&self, x: u64, scale: u64, w: usize, rows: usize, nt: usize,
thr: f32, eps: f32, mul: f32,
dst: u64, dst_ring: usize,
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})");
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, nkv as i32, hd as i32, nr as i32, ext as i32);
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);
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);
launch!(self, "ink_attn_merge", cfg((nh as u32, 1), 128, 0), sc, gate, out, chunks as i32, hd as i32);
Ok(())
}
Expand Down
Loading
Loading