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
4 changes: 3 additions & 1 deletion bench/cuda/check/check_attn64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ int main() {
// Lockstep guard: attn_dpos (split-KV on the capacity-static grid,
// *d_pos = pos so ctx matches) must be BIT-identical to attn — the
// device attn_dpos_chunk twins the host attn_split_count/attn_split_chunk
// heuristic, and the capacity grid's empty splits combine as exact zeros.
// heuristic, and an empty split writes its m (attn_m_empty), so the
// combine never reads the partials the windowed passes after the
// unwindowed one leave behind in its later splits.
upload_u32(dp, (unsigned)pos);
cache.attn_dpos({q, 0}, {o2, 0}, HQ, {dp, 0}, scale);
flush();
Expand Down
6 changes: 3 additions & 3 deletions bench/cuda/speed/bench_qwen_ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ int main(int argc, char** argv) {
void* d_pos = cu::alloc(4, nullptr);
// Value guard alongside the timing: at each ctx, layer 0's host-path output
// vs the dpos-path output must be BIT-IDENTICAL (the dpos kernel replicates
// the host chunk heuristic; empty splits combine as exact zeros). This is the
// long-ctx complement to bench_qwen_decode's dpos-vs-hostpos check (pos 32,
// which never leaves the S=1 regime).
// the host chunk heuristic, and the combine's sums stop at the empty
// splits). This is the long-ctx complement to bench_qwen_decode's
// dpos-vs-hostpos check (pos 32, which never leaves the S=1 regime).
float *ref_host = nullptr, *got_host = nullptr;
void* refb = cu::alloc(C.NH * C.HD * 4, &ref_host);
void* gotb = cu::alloc(C.NH * C.HD * 4, &got_host);
Expand Down
7 changes: 4 additions & 3 deletions include/cuda_llm.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ inline unsigned attn_dpos_splits(int64_t n_q_heads, int64_t n_kv_heads, int64_t
// gridDim.y = S_max = attn_dpos_splits at max_ctx, a constant for the cache's
// lifetime — so the launch is capturable — while each block derives its live
// split bounds from *d_pos (attn_dpos_chunk, under attn_split_rule passed as
// arguments). Splits past the live count write neutral partials that combine
// as exact zeros, so the output is bit-identical to the host attn_decode at
// every pos, and the captured path stays split-KV-flat at long ctx.
// arguments). Splits past the live count are empty (attn_partials), which the
// combine's sums stop before, so the output is bit-identical to the host
// attn_decode at every pos, and the captured path stays split-KV-flat at long
// ctx.
// `partials` is a caller-OWNED device buffer of attn_dpos_partials_bytes —
// like d_pos, it is graph-lifetime state (a captured graph bakes its address
// in), so it must not be a shared growable scratch; kv_cache owns one per
Expand Down
6 changes: 5 additions & 1 deletion include/cuda_nn.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,11 @@ inline unsigned attn_split_chunk(unsigned rows, int64_t ctx) {
// Split-KV partials scratch: ONE buffer laid out pm[H*S] | pl[H*S] | pacc[H*S*D]
// (hs = H*S). bytes/the carve constructor/combine() keep that layout contract in
// a single place — attn_decode's split branch and attn_decode_dpos both bake
// these pointers into launches (the dpos ones into a captured graph).
// these pointers into launches (the dpos ones into a captured graph). An empty
// split (a graph step's past its live count) writes its m alone, at
// attn_m_empty, and empty splits come after the rest (a split's first key
// grows with its index), so the combine's sums stop after the last split that
// is not: a short cache reads its own splits' partials, not the capacity's.
struct attn_partials {
float *pm, *pl, *pacc;
attn_partials(float* base, size_t hs) : pm(base), pl(pm + hs), pacc(pl + hs) {}
Expand Down
7 changes: 3 additions & 4 deletions kernels/cuda_llm.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
// DYNAMIC position: each block recomputes the host launcher's split heuristic
// from *d_pos, so one instantiated graph replays correctly at every pos AND
// stays split-KV-flat at long ctx. Splits past ceil(ctx/chunk) see k0 >= ctx
// and write the neutral partial that contributes exact zeros in
// tl_attn_combine, so the output is bit-identical to the host path (single
// pass or split) at the same pos. Body is attn_dec_core, as the by-value
// kernels'.
// and are empty (attn_partials, cuda_nn.h), which tl_attn_combine sums past,
// so the output is bit-identical to the host path (single pass or split) at
// the same pos. Body is attn_dec_core, as the by-value kernels'.
//
// attn_dpos_chunk is gpu::policy::split_parts + split_chunk on the device, under
// the rule the launcher passes (cuda_nn.h's attn_split_rule), so the host and
Expand Down
37 changes: 26 additions & 11 deletions kernels/cuda_nn.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -3092,6 +3092,9 @@ template <int AD>
constexpr int attn_dec_warps = AD == 512 ? 4 : 8;
template <int AD>
constexpr int attn_dec_threads = 32 * attn_dec_warps<AD>;
// A softmax state's m before any key, and all an empty split writes of its
// partial (attn_partials, cuda_nn.h): it weighs exp(-1e30 - max) = 0 in a merge.
constexpr float attn_m_empty = -1e30f;

// The decode attention's core. Block blockIdx.x is one kv head and a slice of
// GB of the q heads that share it (GQA): it reads each of its keys [k0, k1)
Expand All @@ -3107,8 +3110,8 @@ constexpr int attn_dec_threads = 32 * attn_dec_warps<AD>;
// memory, one barrier for all heads. FINAL normalizes and stores the output;
// else each head writes its partial (m, l, acc at the local max) for
// tl_attn_combine, at split blockIdx.y of gridDim.y. An empty range (k0 >=
// k1, a dpos split past the live count) writes the neutral partial (m = -1e30,
// l = 0, acc = 0), which combines as exact zeros.
// k1, a dpos split past the live count) writes its heads' m alone, at
// attn_m_empty, and returns before any barrier (the whole block alike).
template <int AD, typename KT, bool FINAL>
__device__ __forceinline__ void attn_dec_core(const float* __restrict__ q, const KT* __restrict__ K,
const KT* __restrict__ V, float* __restrict__ out,
Expand All @@ -3124,14 +3127,18 @@ __device__ __forceinline__ void attn_dec_core(const float* __restrict__ q, const
const KT* Kh = K + (size_t)kv_h * kv_stride;
const KT* Vh = V + (size_t)kv_h * kv_stride;
const unsigned tid = threadIdx.x, lane = tid & 31u, warp = tid >> 5;
if (!FINAL && k0 >= k1) {
if (tid < gn) pm[(size_t)(h0 + tid) * gridDim.y + blockIdx.y] = attn_m_empty;
return;
}

// The queries straight from global (each warp reads the same few KB, from
// L1), so their loads go out with the first keys' rather than behind a
// barrier.
float qr[GB][NR], acc[GB][NR], m[GB], l[GB];
#pragma unroll
for (int g = 0; g < GB; g++) {
m[g] = -1e30f;
m[g] = attn_m_empty;
l[g] = 0.0f;
#pragma unroll
for (int r = 0; r < NR; r++) {
Expand Down Expand Up @@ -3188,8 +3195,8 @@ __device__ __forceinline__ void attn_dec_core(const float* __restrict__ q, const
}
}

// Merge the warps' states (a warp whose keys were none carries m = -1e30,
// l = 0 and adds exp(-inf) = 0), every head at once.
// Merge the warps' states (a warp whose keys were none carries m =
// attn_m_empty, l = 0 and adds exp(-inf) = 0), every head at once.
__shared__ float sm[NWB][GB], sl[NWB][GB], sacc[NWB][GB][AD];
#pragma unroll
for (int g = 0; g < GB; g++) {
Expand All @@ -3203,7 +3210,7 @@ __device__ __forceinline__ void attn_dec_core(const float* __restrict__ q, const
__syncthreads();
for (unsigned e = tid; e < gn * AD; e += attn_dec_threads<AD>) {
const unsigned g = e / AD, d = e % AD;
float gm = -1e30f;
float gm = attn_m_empty;
#pragma unroll
for (int w = 0; w < NWB; w++) gm = fmaxf(gm, sm[w][g]);
float gl = 0.0f, o = 0.0f;
Expand Down Expand Up @@ -3274,18 +3281,26 @@ TL_ATTN_SPLIT(256, __half, f16)
TL_ATTN_SPLIT(512, __half, f16)
#undef TL_ATTN_SPLIT

// Merge the S per-head partials (each already at its local max) into out. Not
// templated: the per-dim thread stride D == blockDim.x, so head_dim is implicit.
// Merge the S per-head partials (each already at its local max) into out, the
// sums stopping after the last split that is not empty (attn_partials). Not
// templated: the per-dim thread stride D == blockDim.x, so head_dim is
// implicit.
extern "C" __global__ void tl_attn_combine(const float* __restrict__ pm,
const float* __restrict__ pl,
const float* __restrict__ pacc,
float* __restrict__ out, unsigned S) {
const unsigned h = blockIdx.x, tid = threadIdx.x, D = blockDim.x;
const size_t base = (size_t)h * S;
float gm = -1e30f;
for (unsigned s = 0; s < S; s++) gm = fmaxf(gm, pm[base + s]);
float gl = 0.0f, o = 0.0f;
float gm = attn_m_empty;
unsigned n = 0;
#pragma unroll 16 // as many m loads in flight as the max alone gets
for (unsigned s = 0; s < S; s++) {
const float m = pm[base + s];
gm = fmaxf(gm, m);
n = m != attn_m_empty ? s + 1 : n;
}
float gl = 0.0f, o = 0.0f;
for (unsigned s = 0; s < n; s++) {
float e = __expf(pm[base + s] - gm);
gl += pl[base + s] * e;
o += pacc[(base + s) * D + tid] * e;
Expand Down
Loading