Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
881ccca
cuda: a process-wide cap on DeviceBuffer and pinned HostBuffer bytes
plotarmordev Oct 16, 2026
b00dd4e
drama: the temporal codebook tower
plotarmordev Oct 16, 2026
c8af505
drama: the speech codec, with codec.pth read without Python
plotarmordev Oct 16, 2026
e192c0f
engine_api: speech requests, speech info and a submit_speech hook
plotarmordev Oct 16, 2026
ab45904
native: Drama 3 (fish_omni) on CUDA
plotarmordev Oct 16, 2026
7553d00
server: POST /v1/audio/speech
plotarmordev Oct 16, 2026
8ba4307
tests: Drama 3 backbone and tower oracles, replays and stage traces
plotarmordev Oct 16, 2026
20249e9
drama: incremental codec decode, bit-identical to the whole decode
plotarmordev Oct 16, 2026
93fcaab
tests: Drama 3 codec oracle, replays and stage trace
plotarmordev Oct 16, 2026
d84284d
engine_api: audio events, so a speech engine can publish PCM while it…
plotarmordev Oct 16, 2026
71c31a9
build: Drama 3 backbone and tower kernels, the drama module, the nati…
plotarmordev Oct 16, 2026
588f1f1
server: stream: true on /v1/audio/speech sends raw PCM chunks as they…
plotarmordev Oct 16, 2026
d8c6a47
drama: the backbone's one-row GEMM stages its weights through shared …
plotarmordev Oct 16, 2026
10796d4
build: the Drama 3 codec kernels and the codec tool steps
plotarmordev Oct 16, 2026
8bc6814
tests: tf-drama-codec-stream-oracle, whole against chunked codec decodes
plotarmordev Oct 16, 2026
40c7c87
core: a direct read the file system refuses goes through the page cache
plotarmordev Oct 16, 2026
23a7fcc
cuda: Module.holds, whether a loaded image defines a device global
plotarmordev Oct 16, 2026
6f85731
drama: the Drama 3 text backbone on CUDA
plotarmordev Oct 16, 2026
05a715f
drama: one-row backbone GEMVs stream weights through a swizzled cp.as…
plotarmordev Oct 16, 2026
9dc8202
drama: add-norm fused with the routed experts' FP8 activation quantiz…
plotarmordev Oct 16, 2026
705541c
drama: one SwiGLU launch for the routed and shared experts
plotarmordev Oct 16, 2026
08291b5
drama: MoE combine on 8-wide vectors
plotarmordev Oct 16, 2026
641b18b
drama: decode attention keeps the next keys' loads in flight
plotarmordev Oct 16, 2026
01b7f3b
drama: the tower's one decode row attends as a thread-block cluster
plotarmordev Oct 16, 2026
b89dc67
drama: checkpoint weights stream onto the GPU with direct I/O in file…
plotarmordev Oct 16, 2026
0f9345e
speech: a Drama 3 reply that reaches max_frames returns its clipped a…
plotarmordev Oct 16, 2026
9b3934c
drama: one-row attention runs its Q/K norm and RoPE prologue in the s…
plotarmordev Oct 16, 2026
ea06b8b
drama: the tower samples with a stable radix sort
plotarmordev Oct 16, 2026
68aeb3e
drama: the codec builds its weights on its own thread while the backb…
plotarmordev Oct 16, 2026
fdee05c
Merge upstream main (6ee828c2) into feat/drama3-cuda
plotarmordev Oct 17, 2026
0f81a2b
Merge align/920-merge-main (upstream main 6ee828c2) into feat/drama3-…
plotarmordev Oct 17, 2026
15bceca
Merge align/921-merge-main (upstream main 6ee828c2) into fix/drama3-c…
plotarmordev Oct 17, 2026
64e8ff8
Merge align/922-merge-main (upstream main 6ee828c2) into perf/drama3-…
plotarmordev Oct 17, 2026
2d9232a
Merge align/923-merge-main (upstream main 6ee828c2) into perf/drama3-…
plotarmordev Oct 17, 2026
42c548c
Merge align/924-merge-main (upstream main 6ee828c2) into perf/drama3-…
plotarmordev Oct 17, 2026
b56fc8c
drama: below sm_90 the tower's decode row runs drama_attention, and i…
plotarmordev Oct 17, 2026
996637c
Merge align/924-merge-main into perf/drama3-load: the tower's sm_90 c…
plotarmordev Oct 17, 2026
65cd256
engine_api and native/cuda.zig back under 600 lines: speech and visio…
plotarmordev Oct 17, 2026
f5aa7a5
Merge align/920-merge-main into feat/drama3-streaming: speech and vis…
plotarmordev Oct 17, 2026
fd87532
Merge align/921-merge-main into fix/drama3-clipped-replies: speech an…
plotarmordev Oct 17, 2026
572afe3
Merge align/922-merge-main into perf/drama3-backbone-one-row: speech …
plotarmordev Oct 17, 2026
1ecf53b
Merge align/923-merge-main into perf/drama3-tower-decode: speech and …
plotarmordev Oct 17, 2026
bd11ba4
Merge align/924-merge-main into perf/drama3-load: speech and vision t…
plotarmordev Oct 17, 2026
118086a
core: lane tests ignore the speech audio event
plotarmordev Oct 17, 2026
4d41f5a
Merge align/921-merge-main into fix/drama3-clipped-replies: lane test…
plotarmordev Oct 17, 2026
2b27b19
Merge align/922-merge-main into perf/drama3-backbone-one-row: lane te…
plotarmordev Oct 17, 2026
e45d71b
Merge align/923-merge-main into perf/drama3-tower-decode: lane tests …
plotarmordev Oct 17, 2026
809e103
Merge align/924-merge-main into perf/drama3-load: lane tests ignore t…
plotarmordev Oct 17, 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
drama: one-row attention runs its Q/K norm and RoPE prologue in the s…
…ame launch

For a single row, the attention launch also does drama_attn_prep's norm, RoPE and cache append, taking the new
key and value from registers, with the same roundings as the separate launch.
  • Loading branch information
plotarmordev committed Oct 16, 2026
commit 9b3934cb85c5a48fe85a5f111ee39c3f803499cd
124 changes: 82 additions & 42 deletions zig/kernels/cuda/drama_backbone_attention.cuh
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
// Backbone full attention: Q/K norm and RoPE prologue, KV append and causal GQA; included by drama_backbone.cu.
#pragma once

// Gemma Q/K norm (bf16), NeoX RoPE on the first 64 dims with fp32 cos/sin; k and v go to the caches at pos0 + row.
extern "C" __global__ void __launch_bounds__(32) drama_attn_prep(const __nv_bfloat16* qkv, int ld_qkv,
const __nv_bfloat16* qn_w, const __nv_bfloat16* kn_w,
const float* inv_freq, __nv_bfloat16* q_out,
__nv_bfloat16* kc, __nv_bfloat16* vc, int pos0, int H,
int HK, float eps) {
// fused_qk_gemma_rmsnorm_rope_gate for one head: bf16 Gemma norm, NeoX RoPE on 64 dims in fp32, rounded at the store.
__device__ __forceinline__ void qk_norm_rope(const __nv_bfloat16* src, const __nv_bfloat16* w, const float* inv_freq,
int pos, float eps, int lane, float (&o)[8]) {
constexpr int D = 256, ROT = 64, HALF = 32;
const int r = blockIdx.x, h = blockIdx.y, lane = threadIdx.x;
const int pos = pos0 + r;
const __nv_bfloat16* row = qkv + static_cast<size_t>(r) * ld_qkv;
const bool is_q = h < H;
const int kh = h - H;
const __nv_bfloat16* src = is_q ? row + h * 2 * D : row + H * 2 * D + kh * D;
const __nv_bfloat16* w = is_q ? qn_w : kn_w;
float v[8];
float ss = 0.f;
#pragma unroll
Expand All @@ -27,7 +17,6 @@ extern "C" __global__ void __launch_bounds__(32) drama_attn_prep(const __nv_bflo
#pragma unroll
for (int i = 0; i < 8; ++i) v[i] = round_bf(v[i] * rcp * (1.0f + bf(w[lane * 8 + i])));
// lanes 0..3 hold dims 0..31, lanes 4..7 dims 32..63: partner lane ^ 4
float o[8];
#pragma unroll
for (int i = 0; i < 8; ++i) {
const float other = __shfl_xor_sync(0xffffffffu, v[i], 4);
Expand All @@ -40,6 +29,23 @@ extern "C" __global__ void __launch_bounds__(32) drama_attn_prep(const __nv_bflo
o[i] = v[i];
}
}
}

// Q/K prologue for each head of a row: q to q_out [rows][H][256], k and v to the caches at pos0 + row.
extern "C" __global__ void __launch_bounds__(32) drama_attn_prep(const __nv_bfloat16* qkv, int ld_qkv,
const __nv_bfloat16* qn_w, const __nv_bfloat16* kn_w,
const float* inv_freq, __nv_bfloat16* q_out,
__nv_bfloat16* kc, __nv_bfloat16* vc, int pos0, int H,
int HK, float eps) {
constexpr int D = 256;
const int r = blockIdx.x, h = blockIdx.y, lane = threadIdx.x;
const int pos = pos0 + r;
const __nv_bfloat16* row = qkv + static_cast<size_t>(r) * ld_qkv;
const bool is_q = h < H;
const int kh = h - H;
const __nv_bfloat16* src = is_q ? row + h * 2 * D : row + H * 2 * D + kh * D;
float o[8];
qk_norm_rope(src, is_q ? qn_w : kn_w, inv_freq, pos, eps, lane, o);
__nv_bfloat16* dst = is_q ? q_out + (static_cast<size_t>(r) * H + h) * D
: kc + (static_cast<size_t>(pos) * HK + kh) * D;
#pragma unroll
Expand All @@ -52,37 +58,77 @@ extern "C" __global__ void __launch_bounds__(32) drama_attn_prep(const __nv_bflo
}
}

// Causal GQA attention with fp32 online softmax and bf16(p) in P.V, as Triton's tl.dot, then the sigmoid gate.
extern "C" __global__ void __launch_bounds__(256) drama_attention(const __nv_bfloat16* q, const __nv_bfloat16* kc,
const __nv_bfloat16* vc, const __nv_bfloat16* qkv,
// Causal GQA attention as Triton's (bf16(p) in P.V) with the sigmoid gate; given qn_w, one row runs the prologue too.
extern "C" __global__ void __launch_bounds__(256) drama_attention(const __nv_bfloat16* q, __nv_bfloat16* kc,
__nv_bfloat16* vc, const __nv_bfloat16* qkv,
int ld_qkv, __nv_bfloat16* out, int pos0, int H,
int HK, float scale) {
int HK, float scale, const __nv_bfloat16* qn_w,
const __nv_bfloat16* kn_w, const float* inv_freq,
float eps) {
constexpr int D = 256, NW = 8, U = 8;
__shared__ float sm_m[NW], sm_l[NW];
__shared__ float sm_o[NW][D];
const int r = blockIdx.x, h = blockIdx.y;
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
const int kh = h / (H / HK);
const int last = pos0 + r;
float qv[8], o[8];
const __nv_bfloat16* qr = q + (static_cast<size_t>(r) * H + h) * D + lane * 8;
#pragma unroll
for (int i = 0; i < 8; ++i) {
qv[i] = bf(qr[i]);
o[i] = 0.f;
}
float m = -INFINITY, l = 0.f;
// Keys go U at a time with the next batch's loads in flight; the softmax update stays in key order.
const bool prologue = qn_w != nullptr;
const __nv_bfloat16* row = qkv + static_cast<size_t>(r) * ld_qkv;
// Cached keys go U at a time with loads in flight; with no cached key (the fused row at 0) nothing is loaded.
const int cached = prologue ? last - 1 : last;
const __nv_bfloat16* kp = kc + kh * D + lane * 8;
const __nv_bfloat16* vp = vc + kh * D + lane * 8;
uint4 kw[U], vw[U];
if (cached >= 0) {
#pragma unroll
for (int u = 0; u < U; ++u) {
const size_t at = static_cast<size_t>(min(warp + u * NW, cached)) * HK * D;
kw[u] = *reinterpret_cast<const uint4*>(kp + at);
vw[u] = *reinterpret_cast<const uint4*>(vp + at);
}
}
float qv[8], o[8];
if (prologue) {
qk_norm_rope(row + h * 2 * D, qn_w, inv_freq, last, eps, lane, qv);
#pragma unroll
for (int u = 0; u < U; ++u) {
const size_t at = static_cast<size_t>(min(warp + u * NW, last)) * HK * D;
kw[u] = *reinterpret_cast<const uint4*>(kp + at);
vw[u] = *reinterpret_cast<const uint4*>(vp + at);
for (int i = 0; i < 8; ++i) qv[i] = round_bf(qv[i]);
} else {
const __nv_bfloat16* qr = q + (static_cast<size_t>(r) * H + h) * D + lane * 8;
#pragma unroll
for (int i = 0; i < 8; ++i) qv[i] = bf(qr[i]);
}
// The row's own key is the last key of warp last % NW; scored now, while the first loads are in flight.
const bool own = prologue && warp == last % NW;
float own_score = 0.f;
uint4 own_value{};
if (own) {
float kv[8];
qk_norm_rope(row + H * 2 * D + kh * D, kn_w, inv_freq, last, eps, lane, kv);
own_value = *reinterpret_cast<const uint4*>(row + H * 2 * D + HK * D + kh * D + lane * 8);
float dot = 0.f;
#pragma unroll
for (int i = 0; i < 8; ++i) dot += qv[i] * round_bf(kv[i]);
own_score = warp_sum(dot) * scale;
if (h % (H / HK) == 0) {
const size_t at = (static_cast<size_t>(last) * HK + kh) * D + lane * 8;
store8(kc + at, kv);
*reinterpret_cast<uint4*>(vc + at) = own_value;
}
}
for (int j0 = warp; j0 <= last; j0 += NW * U) {
#pragma unroll
for (int i = 0; i < 8; ++i) o[i] = 0.f;
float m = -INFINITY, l = 0.f;
const auto step = [&](float score, const uint4& value) {
const float mn = fmaxf(m, score);
const float corr = expf(m - mn); // m = -inf on the first key: 0
const float p = expf(score - mn);
l = l * corr + p;
const __nv_bfloat16* v = reinterpret_cast<const __nv_bfloat16*>(&value);
#pragma unroll
for (int i = 0; i < 8; ++i) o[i] = o[i] * corr + round_bf(p) * bf(v[i]);
m = mn;
};
for (int j0 = warp; j0 <= cached; j0 += NW * U) {
float sc[U];
uint4 vb[U];
#pragma unroll
Expand All @@ -93,26 +139,20 @@ extern "C" __global__ void __launch_bounds__(256) drama_attention(const __nv_bfl
for (int i = 0; i < 8; ++i) dot += qv[i] * bf(k[i]);
sc[u] = dot;
vb[u] = vw[u];
const size_t at = static_cast<size_t>(min(j0 + (U + u) * NW, last)) * HK * D;
const size_t at = static_cast<size_t>(min(j0 + (U + u) * NW, cached)) * HK * D;
kw[u] = *reinterpret_cast<const uint4*>(kp + at);
vw[u] = *reinterpret_cast<const uint4*>(vp + at);
}
#pragma unroll
for (int u = 0; u < U; ++u) sc[u] = warp_sum(sc[u]) * scale;
const int n = min(U, (last - j0) / NW + 1);
const int n = min(U, (cached - j0) / NW + 1);
#pragma unroll
for (int u = 0; u < U; ++u) {
if (u >= n) break;
const float mn = fmaxf(m, sc[u]);
const float corr = expf(m - mn); // m = -inf on the first key: 0
const float p = expf(sc[u] - mn);
l = l * corr + p;
const __nv_bfloat16* v = reinterpret_cast<const __nv_bfloat16*>(&vb[u]);
#pragma unroll
for (int i = 0; i < 8; ++i) o[i] = o[i] * corr + round_bf(p) * bf(v[i]);
m = mn;
step(sc[u], vb[u]);
}
}
if (own) step(own_score, own_value);
if (lane == 0) {
sm_m[warp] = m;
sm_l[warp] = l;
Expand Down
37 changes: 22 additions & 15 deletions zig/src/families/drama/backbone_kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,25 @@ pub const Kernels = struct {
scale: f32,
};

/// q/k norm + RoPE + cache append, then causal attention with the output gate.
/// q/k norm + RoPE + cache append, then causal attention with the output gate; one row takes a single launch.
pub fn attention(k: *const Kernels, s: cuda.Stream, t: Attn) !void {
var a: cuda.Args = .{};
a.add(t.qkv);
a.add(int(t.ld_qkv));
a.add(t.q_norm);
a.add(t.k_norm);
a.add(t.inv_freq);
a.add(t.q);
a.add(t.kc);
a.add(t.vc);
a.add(int(t.pos0));
a.add(int(t.heads));
a.add(int(t.kv_heads));
a.add(t.eps);
try go(s, k.attn_prep_fn, .{ t.rows, t.heads + t.kv_heads, 1 }, 32, &a);
const one = t.rows == 1;
if (!one) {
var a: cuda.Args = .{};
a.add(t.qkv);
a.add(int(t.ld_qkv));
a.add(t.q_norm);
a.add(t.k_norm);
a.add(t.inv_freq);
a.add(t.q);
a.add(t.kc);
a.add(t.vc);
a.add(int(t.pos0));
a.add(int(t.heads));
a.add(int(t.kv_heads));
a.add(t.eps);
try go(s, k.attn_prep_fn, .{ t.rows, t.heads + t.kv_heads, 1 }, 32, &a);
}
var b: cuda.Args = .{};
b.add(t.q);
b.add(t.kc);
Expand All @@ -298,6 +301,10 @@ pub const Kernels = struct {
b.add(int(t.heads));
b.add(int(t.kv_heads));
b.add(t.scale);
b.add(if (one) t.q_norm else 0);
b.add(if (one) t.k_norm else 0);
b.add(t.inv_freq);
b.add(t.eps);
try go(s, k.attention_fn, .{ t.rows, t.heads, 1 }, 256, &b);
}

Expand Down
Loading