Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
751e7f8
cuda: tree attention and DFlash block attention for every family (tre…
jschmied Oct 12, 2026
de31ba9
cuda/tree_attention: a group's 4 slices write their own partials, the…
jschmied Oct 13, 2026
6c3d49d
cuda/tree_attention: four-warp blocks combine a group's slices themse…
jschmied Oct 13, 2026
197cacc
cuda/tree_attention: one-warp blocks fetch their tiles 2 ahead (3 cp.…
jschmied Oct 13, 2026
403549d
cuda/tree_attention: 512-key groups (4x the warps in flight at one ro…
jschmied Oct 13, 2026
591065e
cuda/tree_attention: the merge issues a chunk's partial loads (16 slo…
jschmied Oct 14, 2026
df0ef6b
cuda: sliding-window attention, sigmoid top-k MoE routing and combine…
jschmied Oct 13, 2026
492716b
cuda/tree_attention: each row's own max flag, so a NaN head leaves ot…
jschmied Oct 17, 2026
f812fbe
cuda/block_attention: keys a row cannot see add nothing, so an infini…
jschmied Oct 17, 2026
d18a720
cuda/tree_attention, block_attention: registered with the kernel cont…
jschmied Oct 17, 2026
ce16098
cuda/window_attention: the tensor-core decode keeps each head's own m…
jschmied Oct 17, 2026
63d1507
cuda/window_attention: the split decode's slots outside the window ho…
jschmied Oct 17, 2026
40de67d
cuda/window_attention: prompt rows' keys a row cannot see add nothing…
jschmied Oct 17, 2026
8f93377
cuda/window_attention, moe_glue, dense_rows: registered with the kern…
jschmied Oct 17, 2026
7d59918
cuda/window_attention: the tensor-core decode's per-head max needs no…
jschmied Oct 17, 2026
0ad4f17
tests/cuda/contract_attn: the reference's constructor frees what it t…
jschmied Oct 17, 2026
2ae3b69
cuda/block_attention: the replay takes every NaN and infinite value o…
jschmied Oct 18, 2026
462d952
cuda/window_attention: the prompt replay takes every NaN and infinite…
jschmied Oct 18, 2026
3c82eb3
cuda/block_attention: the replay's block tile keeps the infinities ad…
jschmied Oct 18, 2026
0facb0f
cuda/tree_attention: a NaN or infinite partial runs again with those …
jschmied Oct 18, 2026
8cba69e
cuda/window_attention: the decode kernels run a NaN or infinite tile …
jschmied Oct 18, 2026
965c8cd
cuda/tree_attention, block_attention: the replay's comments on one li…
jschmied Oct 18, 2026
74b749f
cuda/window_attention: the split decode and its merge move to window_…
jschmied Oct 18, 2026
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
Prev Previous commit
Next Next commit
cuda/window_attention: prompt rows' keys a row cannot see add nothing…
…, so a later row's infinite value stays out of earlier rows' sums

A block whose output holds a NaN or infinity runs again, out of line, with the values of the tiles some row cannot see all of taken out of the mma; each row adds back the ones it sees. On finite values the first pass is the kernel as it was, byte for byte.
  • Loading branch information
jschmied committed Oct 17, 2026
commit 40de67ddac1e8b2fba4b8e901296abc3d88c08eb
58 changes: 44 additions & 14 deletions zig/kernels/cuda/window_attention.cu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <math.h>
#include <stdint.h>

#include "attn_odd.cuh"
#include "qmm_frag.cuh"

namespace tf_window {
Expand Down Expand Up @@ -164,16 +165,16 @@ __device__ __forceinline__ float rowsum(const float (&c)[8]) {
__fadd_rn(__fadd_rn(w[1], w[5]), __fadd_rn(w[3], w[7])));
}

template <int D, int WARPS, int HPC, int NS>
__global__ void __launch_bounds__(32 * WARPS, 1)
wattn_kernel(const __nv_bfloat16* __restrict__ q, const __nv_bfloat16* __restrict__ kc,
const __nv_bfloat16* __restrict__ vc, __nv_bfloat16* __restrict__ out, int p0, int W, int H, int HK,
int G, float scale, int win, int ring) {
// ODD: the edge tiles' NaN and infinite values leave the mma, and the rows that see them add them back.
template <int D, int WARPS, int HPC, int NS, bool ODD>
__device__ __forceinline__ bool wattn_rows(const __nv_bfloat16* __restrict__ q, const __nv_bfloat16* __restrict__ kc,
const __nv_bfloat16* __restrict__ vc, __nv_bfloat16* __restrict__ out,
int p0, int W, int H, int HK, int G, float scale, int win, int ring) {
constexpr int C = D / 8, K16 = D / 16, NB = D / 8, RB = WARPS / HPC, THREADS = 32 * WARPS;
constexpr bool QREG = D <= 128; // the queries' fragments stay in registers, else in shared memory
extern __shared__ uint4 smem[];
uint4* slots = smem;
uint4* qs = smem + NS * HALF * C;
extern __shared__ uint4 wsmem[];
uint4* slots = wsmem;
uint4* qs = wsmem + NS * HALF * C;
const int warp = threadIdx.x / 32, lane = threadIdx.x % 32, g = lane / 4, qd = lane % 4, mt = lane / 8;
const int groups = G / HPC, hk = blockIdx.y / groups, hg = blockIdx.y % groups;
const int row0 = blockIdx.x * 16 * RB, last = min(row0 + 16 * RB, W) - 1;
Expand Down Expand Up @@ -224,8 +225,9 @@ wattn_kernel(const __nv_bfloat16* __restrict__ q, const __nv_bfloat16* __restric
uint32_t pa[4][4];
float m0 = -INFINITY, m1 = -INFINITY, l0 = 0.0f, l1 = 0.0f;
const int pos0 = p0 + wrow + g, pos1 = pos0 + 8;
auto take = [&](int it) -> const uint4* { // item it's slot, once every thread's copy has landed
auto take = [&](int it, bool odd) -> const uint4* { // item it's slot, once every thread's copy has landed
wait_group<NS - 1>();
if (odd) attn_odd::clean<D>(slots + (it % NS) * HALF * C, THREADS); // NaN and infinities out of the mma
__syncthreads();
return slots + (it % NS) * HALF * C;
};
Expand Down Expand Up @@ -273,11 +275,14 @@ wattn_kernel(const __nv_bfloat16* __restrict__ q, const __nv_bfloat16* __restric
mma(o[i + 1], pa1, b[2], b[3]);
}
};
const auto edge = [&](int t) { // tile t, where some row of the block cannot see some key
return (t0 + t) * BN < (win > 0 ? p0 + last - win + 1 : 0) || (t0 + t) * BN + BN - 1 > p0 + row0;
};
for (int t = 0; t < tiles; ++t) {
const int it = 4 * t;
scores(*reinterpret_cast<float(*)[4][4]>(&sc[0]), take(it));
scores(*reinterpret_cast<float(*)[4][4]>(&sc[0]), take(it, false));
done(it);
scores(*reinterpret_cast<float(*)[4][4]>(&sc[4]), take(it + 1));
scores(*reinterpret_cast<float(*)[4][4]>(&sc[4]), take(it + 1, false));
done(it + 1);
{ // _tile's fold of the tile's 64 scores
const int kb = (t0 + t) * BN + 2 * qd;
Expand Down Expand Up @@ -331,13 +336,21 @@ wattn_kernel(const __nv_bfloat16* __restrict__ q, const __nv_bfloat16* __restric
pa[j][3] = pack(sc[2 * j + 1][2], sc[2 * j + 1][3]);
}
}
values(pa[0], pa[1], take(it + 2));
values(pa[0], pa[1], take(it + 2, ODD && edge(t)));
done(it + 2);
values(pa[2], pa[3], take(it + 3));
values(pa[2], pa[3], take(it + 3, ODD && edge(t)));
done(it + 3);
}
wait_group<0>();
const int r0 = wrow + g, r1 = r0 + 8;
const int r0 = wrow + g, r1 = r0 + 8, pw = p0 + wrow;
if constexpr (!ODD) { // a NaN or infinite output: the block runs again as ODD (finite values: the same bits)
if (__syncthreads_or(attn_odd::nonfinite_out<NB>(o, r0 < W, r1 < W))) return true;
} else {
const auto row = [&](int k) { return vc + ((int64_t)(ring > 0 ? k % ring : k) * HK + hk) * D; };
const auto vis = [&](int r, int k) { return k >= (win > 0 ? pw + r - win + 1 : 0) && k <= pw + r; };
for (int t = 0; t < tiles; ++t)
if (edge(t)) attn_odd::add_back<D>(o, row, (t0 + t) * BN, min((t0 + t + 1) * BN, limit), lane, vis, l0, l1);
}
#pragma unroll
for (int i = 0; i < NB; ++i) {
const int d = 8 * i + 2 * qd;
Expand All @@ -348,8 +361,25 @@ wattn_kernel(const __nv_bfloat16* __restrict__ q, const __nv_bfloat16* __restric
*reinterpret_cast<uint32_t*>(out + ((int64_t)r1 * H + head) * D + d) =
pack(tdiv(o[i][2], l1), tdiv(o[i][3], l1));
}
return false;
}

// A block with a NaN or infinite output, run again with ODD on (out of the common path).
template <int D, int WARPS, int HPC, int NS>
__device__ __noinline__ void wattn_odd(const __nv_bfloat16* q, const __nv_bfloat16* kc, const __nv_bfloat16* vc,
__nv_bfloat16* out, int p0, int W, int H, int HK, int G, float scale, int win,
int ring) {
wattn_rows<D, WARPS, HPC, NS, true>(q, kc, vc, out, p0, W, H, HK, G, scale, win, ring);
}

template <int D, int WARPS, int HPC, int NS>
__global__ void __launch_bounds__(32 * WARPS, 1)
wattn_kernel(const __nv_bfloat16* __restrict__ q, const __nv_bfloat16* __restrict__ kc,
const __nv_bfloat16* __restrict__ vc, __nv_bfloat16* __restrict__ out, int p0, int W, int H, int HK,
int G, float scale, int win, int ring) {
if (wattn_rows<D, WARPS, HPC, NS, false>(q, kc, vc, out, p0, W, H, HK, G, scale, win, ring))
wattn_odd<D, WARPS, HPC, NS>(q, kc, vc, out, p0, W, H, HK, G, scale, win, ring);
}

// Decode on tensor cores (d 128): a row's heads one mma tile; warps take every 4th 32-key tile, fold in warp order.
extern "C" __global__ void __launch_bounds__(128) tf_window_decode_tc(
Expand Down