Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
62 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
2757724
drama: the tower's one-row projections on drama_tower_gemv, in cuBLAS…
plotarmordev Oct 16, 2026
bef432b
drama: the backbone chain launches with programmatic dependent launch
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
96f2b38
Merge align/925-merge-main (upstream main 6ee828c2) into perf/drama3-…
plotarmordev Oct 17, 2026
7c9fd59
Merge align/926-merge-main (upstream main 6ee828c2) into perf/drama3-pdl
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
bc31ec2
Merge align/926-merge-main into perf/drama3-pdl: the tower's sm_90 cl…
plotarmordev Oct 17, 2026
4d3c4f5
Merge align/925-merge-main into perf/drama3-tower-gemv: the tower's s…
plotarmordev Oct 17, 2026
2d62476
CHANGELOG: the tower's cluster decode attention runs from sm_90 on, t…
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
f495030
Merge align/925-merge-main into perf/drama3-tower-gemv: speech and vi…
plotarmordev Oct 17, 2026
f64f02d
Merge align/926-merge-main into perf/drama3-pdl: speech and vision ty…
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
0ed8946
Merge align/925-merge-main into perf/drama3-tower-gemv: lane tests ig…
plotarmordev Oct 17, 2026
d986914
Merge align/926-merge-main into perf/drama3-pdl: lane tests ignore th…
plotarmordev Oct 17, 2026
9d6a54c
drama: backbone attention runs the reference's 64-key softmax tiles
plotarmordev Oct 17, 2026
d2734fd
Merge align/927-merge-main into align/attention-tiles: lane tests ign…
plotarmordev Oct 17, 2026
391c64c
changelog: drop the attention tiles' speed sentence, no change measur…
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: the tower samples with a stable radix sort
The sampler's ranking comes from a stable LSD radix sort under the same strict total order the bitonic network
used, so the permutation and every sum over it are unchanged.
  • Loading branch information
plotarmordev committed Oct 16, 2026
commit ea06b8b5239c27cba340d7633e3f71e530350770
150 changes: 114 additions & 36 deletions zig/kernels/cuda/drama_tower.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <cuda_bf16.h>
#include <stdint.h>
#include <math.h>
#include <limits.h>

using bf16 = __nv_bfloat16;
__device__ inline float f(bf16 x) { return __bfloat162float(x); }
Expand Down Expand Up @@ -343,27 +342,44 @@ __device__ uint64_t random_word(uint64_t x) {
return x ^ (x >> 31);
}

// FP32 semantic logits, top-p over the whole vocabulary before top-k; 64 KiB dynamic shared (allowDynamicShared).
extern "C" __global__ void drama_sample(
// The sampler's order (score descending; equal scores, ±0 alike, by ascending id) as a key ascending in it.
__device__ inline unsigned order_key(float x) {
const unsigned bits = __float_as_uint(x), magnitude = bits & 0x7fffffffu;
return bits >> 31 ? 0x80000000u + magnitude : 0x80000000u - magnitude;
}

// Top-p over the whole vocabulary before top-k, ranked by a stable LSD radix sort; 64 KiB dynamic shared memory.
constexpr int sample_rounds = 6;

extern "C" __global__ void __launch_bounds__(1024) drama_sample(
const void* logits, int is_bf16, int vocab, float temperature, int top_k,
float top_p, uint64_t seed, uint64_t step, int output_index,
int begin, int eos, int* history, int* frame, int* error_flag) {
extern __shared__ unsigned char storage[];
float* scores = reinterpret_cast<float*>(storage);
int* ids = reinterpret_cast<int*>(scores + 8192);
// Padded ids are no longer needed after the sort; reuse them for reductions.
float* partial = reinterpret_cast<float*>(ids + vocab);
unsigned short* counts = reinterpret_cast<unsigned short*>(storage); // [digit][warp]
float* scores = reinterpret_cast<float*>(counts + 256 * 32);
unsigned short* ids = reinterpret_cast<unsigned short*>(scores + vocab);
unsigned short* next = ids + vocab;
// The counts are no longer needed after the sort; reuse them for reductions.
float* partial = reinterpret_cast<float*>(counts);
__shared__ int invalid, selected[2];
__shared__ unsigned key_and, key_or, warp_offsets[32];
__shared__ float total;
const int t = threadIdx.x;
if (t == 0) invalid = 0;
const int t = threadIdx.x, warp = t / 32, lane = t % 32;
if (t == 0) { invalid = 0; key_and = ~0u; key_or = 0; }
__syncthreads();
for (int i = t; i < 8192; i += blockDim.x) {
const float x = i < vocab ? (is_bf16 ? f(static_cast<const bf16*>(logits)[i]) : static_cast<const float*>(logits)[i]) : -INFINITY;
if (i < vocab && !isfinite(x)) atomicExch(&invalid, 1);
unsigned all = ~0u, any = 0;
for (int i = t; i < vocab; i += blockDim.x) {
const float x = is_bf16 ? f(static_cast<const bf16*>(logits)[i]) : static_cast<const float*>(logits)[i];
if (!isfinite(x)) atomicExch(&invalid, 1);
scores[i] = x;
ids[i] = i < vocab ? i : INT_MAX;
ids[i] = i;
all &= order_key(x);
any |= order_key(x);
}
all = __reduce_and_sync(0xffffffff, all);
any = __reduce_or_sync(0xffffffff, any);
if (lane == 0) { atomicAnd(&key_and, all); atomicOr(&key_or, any); }
__syncthreads();
if (invalid) {
if (t == 0) {
Expand All @@ -373,45 +389,107 @@ extern "C" __global__ void drama_sample(
}
return;
}
for (int size = 2; size <= 8192; size <<= 1) {
for (int distance = size >> 1; distance; distance >>= 1) {
for (int i = t; i < 8192; i += blockDim.x) {
const int j = i ^ distance;
if (j > i) {
const bool before = scores[i] > scores[j] || (scores[i] == scores[j] && ids[i] < ids[j]);
if (before != ((i & size) == 0)) {
const float s = scores[i]; scores[i] = scores[j]; scores[j] = s;
const int id = ids[i]; ids[i] = ids[j]; ids[j] = id;
}
}
// Warp w ranks its run 32 at a time; equal digits keep their order (BF16 keys skip the two zero digits).
const int run = (vocab + 31) / 32, from = warp * run, end = min(vocab, from + run);
const unsigned lower = (1u << lane) - 1;
for (int shift = 0; shift < 32; shift += 8) {
if (!((key_and ^ key_or) >> shift & 255)) continue;
reinterpret_cast<uint4*>(counts)[t] = make_uint4(0, 0, 0, 0);
__syncthreads();
// Each element's place among its warp's equal digits: the earlier rounds' count, then the lower lanes'.
int digit[sample_rounds], place[sample_rounds];
for (int r = 0; r < sample_rounds && from + 32 * r < end; ++r) {
const int i = from + 32 * r + lane;
digit[r] = i < end ? order_key(scores[ids[i]]) >> shift & 255 : 0;
unsigned peers = __ballot_sync(0xffffffff, i < end);
for (int bit = 0; bit < 8; ++bit) {
const unsigned set = __ballot_sync(0xffffffff, digit[r] >> bit & 1);
peers &= digit[r] >> bit & 1 ? set : ~set;
}
const int leader = __ffs(peers) - 1;
unsigned counted = 0;
if (lane == leader) {
counted = counts[digit[r] * 32 + warp];
counts[digit[r] * 32 + warp] = counted + __popc(peers);
}
place[r] = __shfl_sync(0xffffffff, counted, leader) + __popc(peers & lower);
__syncwarp();
}
__syncthreads();
// Exclusive scan of the counts in [digit][warp] order: eight per thread, then across threads.
const uint4 packed = reinterpret_cast<const uint4*>(counts)[t];
unsigned c[8] = {packed.x & 0xffff, packed.x >> 16, packed.y & 0xffff, packed.y >> 16,
packed.z & 0xffff, packed.z >> 16, packed.w & 0xffff, packed.w >> 16};
unsigned sum = 0;
for (int i = 0; i < 8; ++i) { const unsigned n = c[i]; c[i] = sum; sum += n; }
unsigned before = sum;
for (int d = 1; d < 32; d <<= 1) {
const unsigned other = __shfl_up_sync(0xffffffff, before, d);
if (lane >= d) before += other;
}
if (lane == 31) warp_offsets[warp] = before;
__syncthreads();
if (warp == 0) {
const unsigned own = warp_offsets[lane];
unsigned prefix = own;
for (int d = 1; d < 32; d <<= 1) {
const unsigned other = __shfl_up_sync(0xffffffff, prefix, d);
if (lane >= d) prefix += other;
}
__syncthreads();
warp_offsets[lane] = prefix - own;
}
__syncthreads();
before += warp_offsets[warp] - sum;
reinterpret_cast<uint4*>(counts)[t] = make_uint4(
(c[0] + before) | (c[1] + before) << 16, (c[2] + before) | (c[3] + before) << 16,
(c[4] + before) | (c[5] + before) << 16, (c[6] + before) | (c[7] + before) << 16);
__syncthreads();
for (int r = 0; r < sample_rounds && from + 32 * r < end; ++r) {
const int i = from + 32 * r + lane;
if (i < end) next[counts[digit[r] * 32 + warp] + place[r]] = ids[i];
}
__syncthreads();
unsigned short* const sorted = next; next = ids; ids = sorted;
}
const float top = scores[ids[0]];
const bool main = output_index == 0;
for (int draw = 0; draw < (main ? 2 : 1); ++draw) {
const float inv_t = 1.0f / fmaxf(draw == 1 ? 1.0f : temperature, 1e-5f);
const float p_limit = draw == 1 ? 0.95f : top_p;
const int k = min(vocab, draw == 1 ? 30 : top_k);
float mass = 0;
for (int i = t; i < vocab; i += blockDim.x) mass += expf((scores[i] - scores[0]) * inv_t);
for (int i = t; i < vocab; i += blockDim.x) mass += expf((scores[ids[i]] - top) * inv_t);
const float summed = sum_block(mass, partial);
if (t == 0) total = summed;
__syncthreads();
if (t == 0) {
// Warp 0 takes 32 ranks at a time; lane 0 runs the in-order cumulative sum, cutoff and pick.
if (warp == 0) {
float* const weights = partial;
float* const candidates = partial + 32;
float cumulative = 0, best = -INFINITY;
int pick = ids[0];
const uint64_t stream = main ? uint64_t(draw) : uint64_t(output_index);
for (int rank = 0; rank < k; ++rank) {
const float log_weight = (scores[rank] - scores[0]) * inv_t;
cumulative += expf(log_weight) / total;
if (rank > 0 && cumulative > p_limit) break;
const uint64_t word = random_word(seed ^ random_word((step << 20) | (stream << 14) | uint64_t(rank)));
const float u = (float(word >> 41) + 0.5f) * (1.0f / 8388608.0f);
const float candidate = log_weight - logf(-logf(u));
if (candidate > best) { best = candidate; pick = ids[rank]; }
bool cut = false;
for (int first = 0; first < k && !cut; first += 32) {
const int rank = first + lane;
if (rank < k) {
const float log_weight = (scores[ids[rank]] - top) * inv_t;
weights[lane] = expf(log_weight) / total;
const uint64_t word = random_word(seed ^ random_word((step << 20) | (stream << 14) | uint64_t(rank)));
const float u = (float(word >> 41) + 0.5f) * (1.0f / 8388608.0f);
candidates[lane] = log_weight - logf(-logf(u));
}
__syncwarp();
if (lane == 0)
for (int i = 0; i < 32 && first + i < k; ++i) {
cumulative += weights[i];
if (first + i > 0 && cumulative > p_limit) { cut = true; break; }
if (candidates[i] > best) { best = candidates[i]; pick = ids[first + i]; }
}
__syncwarp();
cut = __shfl_sync(0xffffffff, cut, 0);
}
selected[draw] = pick;
if (lane == 0) selected[draw] = pick;
}
__syncthreads();
}
Expand Down
Loading