Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 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
1dd63e3
cuda: act_rows, gdn_prep and qk_rows for every family (SwiGLU, output…
jschmied Oct 13, 2026
bffc704
cuda: qk_rows and gdn_prep take 1/sqrt by IEEE division and square ro…
jschmied Oct 13, 2026
a97b9c0
cuda: decode-sized row calls take small blocks (launch.rowThreads, Tr…
jschmied Oct 13, 2026
7bf17c6
cuda: act_rows and gdn_prep say why their sigmoid divides exactly
jschmied Oct 13, 2026
011c851
cuda/gdn_prep: every conv tap's loads issued before any is used, the …
jschmied Oct 13, 2026
df0ef6b
cuda: sliding-window attention, sigmoid top-k MoE routing and combine…
jschmied Oct 13, 2026
31d3baf
cuda/act_rows, gdn_prep, qk_rows: registered with the kernel contract
jschmied Oct 17, 2026
de748b4
Merge remote-tracking branch 'fork/cuda-moe-dense-window' into kolibr…
jschmied Oct 17, 2026
c4051cc
Merge remote-tracking branch 'fork/cuda-glue-rows' into kolibri1-family
jschmied Oct 17, 2026
90aebfc
cuda/tree_attention: each row's own max flag, so a NaN head leaves ot…
jschmied Oct 17, 2026
fc48c41
cuda/block_attention: keys a row cannot see add nothing, so an infini…
jschmied Oct 17, 2026
ee81000
cuda/tree_attention, block_attention: registered with the kernel cont…
jschmied Oct 17, 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
3c7a186
kolibri1: config checks, the tensor map and the loader on the shared …
jschmied Oct 17, 2026
de56c8d
kolibri1: the layer graph on shared CUDA operators (window, tree, qk …
jschmied Oct 17, 2026
0a267b0
kolibri1: the lane backend (prompt steps, shared verify windows, keep…
jschmied Oct 17, 2026
6498344
native: kolibri1 in the CUDA family registry, its refusals in words, …
jschmied Oct 17, 2026
0101e1f
tests: kolibri1 on the checkpoint: layer 0 against float64, the windo…
jschmied Oct 17, 2026
60d5bdd
tests: kolibri1's float64 check bounds a row at 0.3 % of the logits' …
jschmied Oct 17, 2026
6856bfa
Merge remote-tracking branch 'fork/cuda-moe-dense-window' into kolibr…
jschmied Oct 17, 2026
cb605f8
Merge remote-tracking branch 'fork/cuda-attn-shared' into kolibri1-fa…
jschmied Oct 17, 2026
5775551
docs: Kolibri-1 on native CUDA (research checkpoint): the pinned revi…
jschmied Oct 17, 2026
7d59918
cuda/window_attention: the tensor-core decode's per-head max needs no…
jschmied Oct 17, 2026
3dd1d1a
Merge remote-tracking branch 'fork/cuda-moe-dense-window' into kolibr…
jschmied Oct 17, 2026
7633646
kolibri1: prompt passes of 8,192 rows, as the Python engine ran them …
jschmied Oct 17, 2026
1796484
kolibri1: plan the streams inside the room the weights leave before a…
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
d2f6230
Merge remote-tracking branch 'fork/cuda-moe-dense-window' into kolibr…
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/block_attention: keys a row cannot see add nothing, so an infini…
…te value there stays out of its sum

A block whose output holds a NaN or infinity runs again 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 f812fbe7b1eacfca39b99e7bbf22fc056a54a1d9
63 changes: 63 additions & 0 deletions zig/kernels/cuda/attn_odd.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// NaN and infinite values in a tensor-core attention tile's V: a key a row cannot see adds nothing to that row's sum.
#pragma once

#include <cuda_bf16.h>
#include <cuda_runtime.h>
#include <stdint.h>

namespace attn_odd {

// Each bf16 half of x that is NaN or an infinity (all-ones exponent) as +0, the finite halves as they are.
__device__ __forceinline__ uint32_t finite2(uint32_t x) {
return x & (((x & 0x7f80u) == 0x7f80u ? 0u : 0xffffu) | ((x & 0x7f800000u) == 0x7f800000u ? 0u : 0xffff0000u));
}

// Some NaN or infinite value in a warp's mma rows g (r0: a real row) and g + 8 (r1) of its fold.
template <int NB>
__device__ __forceinline__ bool nonfinite_out(const float (&o)[NB][4], bool r0, bool r1) {
float a = 0.0f, b = 0.0f; // x - x is 0 for a finite x, NaN for NaN and the infinities
#pragma unroll
for (int i = 0; i < NB; ++i) {
a = __fadd_rn(a, __fadd_rn(__fsub_rn(o[i][0], o[i][0]), __fsub_rn(o[i][1], o[i][1])));
b = __fadd_rn(b, __fadd_rn(__fsub_rn(o[i][2], o[i][2]), __fsub_rn(o[i][3], o[i][3])));
}
return (r0 && a != a) || (r1 && b != b);
}

// A 32-key tile staged [key][D] (xor swizzle): the chunks this thread copied, their NaN and infinite values set to 0.
template <int D>
__device__ __forceinline__ void clean(uint4* vs, int threads) {
for (int i = threadIdx.x; i < 32 * (D / 8); i += threads) {
uint4* p = vs + (i / (D / 8)) * (D / 8) + ((i % (D / 8)) ^ ((i / (D / 8)) & 7));
const uint4 x = *p;
*p = make_uint4(finite2(x.x), finite2(x.y), finite2(x.z), finite2(x.w));
}
}

// Keys k0..k1 (V row at vrow(k)) that clean() emptied: their NaN and infinite values to the rows that see them.
template <int D, typename Row, typename Vis>
__device__ __forceinline__ void add_back(float (&o)[D / 8][4], Row vrow, int k0, int k1, int lane, Vis vis, float l0,
float l1) {
const int g = lane / 4, qd = lane % 4;
for (int k = k0; k < k1; ++k) {
const bool v0 = l0 > 0.0f && vis(g, k), v1 = l1 > 0.0f && vis(g + 8, k); // l 0: no key weighs, 0 / 0 stays NaN
if (!v0 && !v1) continue;
const uint32_t* v = reinterpret_cast<const uint32_t*>(vrow(k));
#pragma unroll
for (int i = 0; i < D / 8; ++i) {
const uint32_t x = v[4 * i + qd];
if (finite2(x) == x) continue;
const float lo = __uint_as_float(x << 16), hi = __uint_as_float(x & 0xffff0000u);
if ((x & 0x7f80u) == 0x7f80u) {
if (v0) o[i][0] = __fadd_rn(o[i][0], lo);
if (v1) o[i][2] = __fadd_rn(o[i][2], lo);
}
if ((x & 0x7f800000u) == 0x7f800000u) {
if (v0) o[i][1] = __fadd_rn(o[i][1], hi);
if (v1) o[i][3] = __fadd_rn(o[i][3], hi);
}
}
}
}

} // namespace attn_odd
60 changes: 42 additions & 18 deletions zig/kernels/cuda/block_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_block {
Expand Down Expand Up @@ -166,30 +167,53 @@ extern "C" __global__ void __launch_bounds__(256) tf_block_attention(
}
Fold f;
const int lo = max(0, s - window); // the earliest context key any row sees (row 0: s - k <= window)
for (int k0 = lo / KT * KT; k0 < s; k0 += KT) {
const auto vis = [&](int r, int key) { return r < L && key < s && s + r - key <= window; };
// the context's edge tiles, where some row cannot see some key: e0..e1
const int e0 = lo / KT * KT, e1 = min(s, e0 + (max(0, s + L - 1 - window - e0) + KT - 1) / KT * KT);
auto context = [&](bool odd) { // odd: the edge tiles' NaN and infinite values leave the mma (added back below)
for (int k0 = e0; k0 < s; k0 += KT) {
for (int i = threadIdx.x; i < KT * C; i += blockDim.x) {
const int key = k0 + i / C;
const bool in = key < s;
cp16z(ks + sw(i / C, i % C), kc + (int64_t)(in ? key : 0) * D + (i % C) * 8, in);
cp16z(vs + sw(i / C, i % C), vc + (int64_t)(in ? key : 0) * D + (i % C) * 8, in);
}
commit();
wait_all();
if (odd && k0 < e1) attn_odd::clean<D>(vs, blockDim.x);
__syncthreads();
f.tile(qa, ks, vs, scale, lane, k0, vis);
__syncthreads();
}
};
auto block_keys = [&] { // the block's own keys
for (int i = threadIdx.x; i < KT * C; i += blockDim.x) {
const int key = k0 + i / C;
const bool in = key < s;
cp16z(ks + sw(i / C, i % C), kc + (int64_t)(in ? key : 0) * D + (i % C) * 8, in);
cp16z(vs + sw(i / C, i % C), vc + (int64_t)(in ? key : 0) * D + (i % C) * 8, in);
const int c = i / C;
const bool in = c < L;
const int64_t at = ((int64_t)g * rows + j * L + (in ? c : 0)) * D + (i % C) * 8;
cp16z(ks + sw(c, i % C), kb + at, in);
cp16z(vs + sw(c, i % C), vb + at, in);
}
commit();
wait_all();
};
const auto own = [&](int r, int c) { return r < L && c < L && (!causal || c <= r); };
context(false);
block_keys();
__syncthreads();
f.tile(qa, ks, vs, scale, lane, 0, own);
// a NaN or infinite output: again, values some row cannot see out of the mma, added back (finite values: same bits)
if (__syncthreads_or(attn_odd::nonfinite_out<NB>(f.o, g8 < L, g8 + 8 < L))) {
f = Fold();
context(true);
attn_odd::add_back<D>(f.o, [&](int k) { return vc + (int64_t)k * D; }, e0, e1, lane, vis, f.l0, f.l1);
block_keys();
if (causal) attn_odd::clean<D>(vs, blockDim.x); // row r cannot see keys past r
__syncthreads();
f.tile(qa, ks, vs, scale, lane, k0, [&](int r, int key) { return r < L && key < s && s + r - key <= window; });
__syncthreads();
}
for (int i = threadIdx.x; i < KT * C; i += blockDim.x) { // the block's own keys
const int c = i / C;
const bool in = c < L;
const int64_t at = ((int64_t)g * rows + j * L + (in ? c : 0)) * D + (i % C) * 8;
cp16z(ks + sw(c, i % C), kb + at, in);
cp16z(vs + sw(c, i % C), vb + at, in);
f.tile(qa, ks, vs, scale, lane, 0, own);
const auto row = [&](int c) { return vb + ((int64_t)g * rows + j * L + c) * D; };
if (causal) attn_odd::add_back<D>(f.o, row, 0, L, lane, own, f.l0, f.l1);
}
commit();
wait_all();
__syncthreads();
f.tile(qa, ks, vs, scale, lane, 0, [&](int r, int c) { return r < L && c < L && (!causal || c <= r); });
if (g8 < L)
#pragma unroll
for (int i = 0; i < NB; ++i)
Expand Down