Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 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
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's one-row projections on drama_tower_gemv, in cuBLAS…
…Lt's summation order

drama_tower_gemv sums in the order of the gemv kernel cuBLASLt picks for the tower's one-row plans. At init each
projection is checked against its cuBLASLt plan bit for bit and uses the kernel only if they agree.

The qualification and its probe weights live in tower_gemv.zig.
  • Loading branch information
plotarmordev committed Oct 16, 2026
commit 2757724d5dc6bb5f9ac01db5052da50b6ff2594c
1 change: 1 addition & 0 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const kernels = [_]Kernel{
.{ .name = "drama_backbone", .flags = glue },
.{ .name = "drama_backbone_gemm", .flags = &.{"-O3"} },
.{ .name = "drama_tower", .flags = glue },
.{ .name = "drama_tower_gemv", .flags = glue },
.{ .name = "drama_codec", .flags = glue },
};

Expand Down
74 changes: 74 additions & 0 deletions zig/kernels/cuda/drama_tower_gemv.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// One-row bf16 y = W x in cuBLASLt 13.1.1's gemvx order (16 strided fma chains, halving tree); qualified at init.
#include <cuda_bf16.h>
#include <stdint.h>

namespace {

constexpr int kRows = 8;
constexpr int kChunk = 256; // k a stage
constexpr int kStages = 3;
constexpr int kRowBytes = kChunk * 2;
constexpr int kStageBytes = kRows * kRowBytes;
static_assert(kRowBytes == 32 * 16, "a stage row is one 16-byte copy a lane");

__device__ __forceinline__ void cp_async16(void* smem, const void* gmem) {
const uint32_t s = static_cast<uint32_t>(__cvta_generic_to_shared(smem));
asm volatile("cp.async.cg.shared.global [%0], [%1], 16;\n" ::"r"(s), "l"(gmem));
}

// bf16 to fp32 is exact: the low and the high element of a little-endian pair.
__device__ __forceinline__ float lo(uint32_t w) { return __uint_as_float(w << 16); }
__device__ __forceinline__ float hi(uint32_t w) { return __uint_as_float(w & 0xffff0000u); }

} // namespace

// Lane (g, c) runs chains 4c..4c + 3 of row g; rows are swizzled by p ^ 2(r % 4) so reads hit distinct banks.
extern "C" __global__ void __launch_bounds__(32) drama_tower_gemv(const __nv_bfloat16* __restrict__ x,
const __nv_bfloat16* __restrict__ w,
__nv_bfloat16* __restrict__ y, int k) {
__shared__ __align__(128) unsigned char ring[kStages * kStageBytes];
const int lane = threadIdx.x, g = lane >> 2, c = lane & 3;
const unsigned char* rows = reinterpret_cast<const unsigned char*>(w + static_cast<size_t>(blockIdx.x) * kRows * k);
const int chunks = k / kChunk;
auto issue = [&](int s) {
unsigned char* stage = ring + (s % kStages) * kStageBytes;
#pragma unroll
for (int r = 0; r < kRows; ++r)
cp_async16(stage + r * kRowBytes + 16 * (lane ^ (2 * (r & 3))),
rows + static_cast<size_t>(r) * k * 2 + static_cast<size_t>(s) * kRowBytes + 16 * lane);
};
#pragma unroll
for (int s = 0; s < kStages - 1; ++s) {
if (s < chunks) issue(s);
asm volatile("cp.async.commit_group;\n");
}
float p[4] = {0.f, 0.f, 0.f, 0.f};
for (int s = 0; s < chunks; ++s) {
if (s + kStages - 1 < chunks) issue(s + kStages - 1);
asm volatile("cp.async.commit_group;\n");
asm volatile("cp.async.wait_group %0;\n" ::"n"(kStages - 1));
__syncwarp();
const unsigned char* row = ring + (s % kStages) * kStageBytes + g * kRowBytes;
const uint2* xs = reinterpret_cast<const uint2*>(x + s * kChunk) + c;
#pragma unroll
for (int j = 0; j < kChunk / 16; ++j) {
// k = 256 s + 16 j + 4 c ..: bytes 32 j + 8 c of the stage row, piece 2 j + c / 2
const uint2 a = *reinterpret_cast<const uint2*>(row + 16 * ((2 * j + (c >> 1)) ^ (2 * (g & 3))) + 8 * (c & 1));
const uint2 b = __ldg(xs + 4 * j);
p[0] = fmaf(lo(a.x), lo(b.x), p[0]);
p[1] = fmaf(hi(a.x), hi(b.x), p[1]);
p[2] = fmaf(lo(a.y), lo(b.y), p[2]);
p[3] = fmaf(hi(a.y), hi(b.y), p[3]);
}
__syncwarp();
}
// chain t = 4c + i: t + 8 is lane c + 2's p[i], t + 4 is lane c + 1's
#pragma unroll
for (int i = 0; i < 4; ++i) p[i] += __shfl_down_sync(0xffffffffu, p[i], 2);
#pragma unroll
for (int i = 0; i < 4; ++i) p[i] += __shfl_down_sync(0xffffffffu, p[i], 1);
if (c != 0) return;
const float sum = (p[0] + p[2]) + (p[1] + p[3]);
// the epilogue's fma(alpha = 1, sum, 0) and its +0 bias are exactly sum + 0 (-0 becomes +0)
y[blockIdx.x * kRows + g] = __float2bfloat16_rn(sum + 0.0f);
}
1 change: 1 addition & 0 deletions zig/src/cuda/kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub const embed_rows: []const u8 = if (available) &Blob("fatbin_embed_rows").byt
pub const drama_backbone: []const u8 = if (available) &Blob("fatbin_drama_backbone").bytes else &.{};
pub const drama_backbone_gemm: []const u8 = if (available) &Blob("fatbin_drama_backbone_gemm").bytes else &.{};
pub const drama_tower: []const u8 = if (available) &Blob("fatbin_drama_tower").bytes else &.{};
pub const drama_tower_gemv: []const u8 = if (available) &Blob("fatbin_drama_tower_gemv").bytes else &.{};
pub const drama_codec: []const u8 = if (available) &Blob("fatbin_drama_codec").bytes else &.{};

/// Symbols in the gdn image as cuobjdump lists them for the built fatbin (named namespace tf_gdn).
Expand Down
1 change: 1 addition & 0 deletions zig/src/families/drama/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ test {
_ = @import("codec_pth.zig");
_ = @import("codec_load.zig");
_ = @import("stager.zig");
_ = @import("tower_gemv.zig");
}
50 changes: 39 additions & 11 deletions zig/src/families/drama/tower.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const Stager = @import("stager.zig").Stager;
const Buffer = cuda.DeviceBuffer;
const Linear = cuda.cublaslt.Linear;
const Allocator = std.mem.Allocator;
const gemv = @import("tower_gemv.zig");

pub const Frame = [11]u32;
pub const Options = struct { context: usize = 8192, max_rows: usize = 128 };
pub const Sampling = struct { temperature: f32 = 1, top_p: f32 = 0.9, top_k: u32 = 30 };
const hidden = 2048;
const intermediate = 6144;
pub const intermediate = 6144;
const query_width = 4096;
const kv_width = 512;
const qkv_width = 9216;
Expand All @@ -20,7 +21,7 @@ const workspace_bytes = 8 << 20;
const sample_shared_bytes = 65536;
/// drama_tower.cu's attention_split: the CTAs of drama_attention_decode's cluster for one query head.
const attention_split = 4;
const projections = [_][2]usize{
pub const projections = [_][2]usize{
.{ hidden, 2 * hidden }, .{ qkv_width, hidden }, .{ hidden, query_width },
.{ 2 * intermediate, hidden }, .{ hidden, intermediate }, .{ 4096, hidden },
};
Expand Down Expand Up @@ -123,7 +124,7 @@ const Kernels = struct {
}
};

const Impl = struct {
pub const Impl = struct {
gpa: Allocator,
driver: *const cuda.Driver,
options: Options,
Expand All @@ -135,6 +136,10 @@ const Impl = struct {
buffers: std.ArrayList(Buffer) = .empty,
plans: std.ArrayList(Linear) = .empty,
module: ?cuda.Module = null,
gemv_module: ?cuda.Module = null,
gemv: cuda.Function = undefined,
/// Per projection kind: qualifyGemv found drama_tower_gemv bit-identical to its one-row plan.
gemv_exact: [projections.len]bool = @splat(false),
library: cuda.cublaslt.Library = undefined,
library_open: bool = false,
kernels: Kernels = undefined,
Expand Down Expand Up @@ -172,6 +177,7 @@ const Impl = struct {
for (p.buffers.items) |*buffer| buffer.free();
p.buffers.deinit(p.gpa);
if (p.module) |*module| module.unload();
if (p.gemv_module) |*module| module.unload();
const gpa = p.gpa;
gpa.destroy(p);
}
Expand Down Expand Up @@ -224,11 +230,24 @@ const Impl = struct {
};
}

fn project(p: *Impl, stream: cuda.Stream, rows: usize, kind: usize, x: Buffer, w: Buffer, out: Buffer) !void {
pub fn project(p: *Impl, stream: cuda.Stream, rows: usize, kind: usize, x: Buffer, w: Buffer, out: Buffer) !void {
if (rows == 1 and p.gemv_exact[kind]) return p.gemvRow(stream, kind, x, w, out);
const plan = &p.plans.items[(rows - 1) * projections.len + kind];
try plan.run(x.ptr, w.ptr, out.ptr, p.workspace.ptr, p.workspace.len, stream.handle);
}

pub fn gemvRow(p: *Impl, stream: cuda.Stream, kind: usize, x: Buffer, w: Buffer, out: Buffer) !void {
comptime for (projections) |shape| std.debug.assert(shape[0] % 8 == 0 and shape[1] % 256 == 0);
var a: cuda.Args = .{};
a.add(x.ptr);
a.add(w.ptr);
a.add(out.ptr);
a.add(@as(i32, @intCast(projections[kind][1])));
try launch(p.gemv, stream, projections[kind][0] / 8, 32, 0, &a);
}

const qualifyGemv = gemv.qualifyGemv;

fn norm(p: *Impl, stream: cuda.Stream, rows: usize, x: Buffer, residual: ?Buffer, w: Buffer, out: Buffer, residual_out: ?Buffer) !void {
var a: cuda.Args = .{};
a.add(x.ptr);
Expand Down Expand Up @@ -371,6 +390,16 @@ fn launch(function: cuda.Function, stream: cuda.Stream, blocks: usize, threads:
try cuda.launch.launch(function, .{ .grid = .{ .x = @intCast(blocks), .y = 1, .z = 1 }, .block = .{ .x = threads, .y = 1, .z = 1 }, .shared = shared }, stream, args);
}

/// `file` from `kernel_dir`, else the embedded image.
pub fn loadModule(gpa: Allocator, io: std.Io, d: *const cuda.Driver, kernel_dir: []const u8, file: []const u8, embedded: []const u8) !cuda.Module {
if (kernel_dir.len == 0) return cuda.Module.load(d, embedded);
const path = try std.fs.path.join(gpa, &.{ kernel_dir, file });
defer gpa.free(path);
const image = try std.Io.Dir.cwd().readFileAllocOptions(io, path, gpa, .limited(1 << 26), .@"16", null);
defer gpa.free(image);
return cuda.Module.load(d, image);
}

pub const Model = struct {
impl: *Impl,

Expand All @@ -392,13 +421,7 @@ pub const Model = struct {
const p = try gpa.create(Impl);
p.* = .{ .gpa = gpa, .driver = ctx.d, .options = options, .semantic_start = parsed.value.semantic_start_token_id, .semantic_end = parsed.value.semantic_end_token_id, .eos = parsed.value.eos_token_id, .eps = c.norm_eps, .rope_base = c.rope_base };
errdefer p.release();
p.module = if (kernel_dir.len == 0) try cuda.Module.load(ctx.d, cuda.kernels.drama_tower) else blk: {
const kernel_path = try std.fs.path.join(gpa, &.{ kernel_dir, "drama_tower.fatbin" });
defer gpa.free(kernel_path);
const image = try std.Io.Dir.cwd().readFileAllocOptions(io, kernel_path, gpa, .limited(1 << 26), .@"16", null);
defer gpa.free(image);
break :blk try cuda.Module.load(ctx.d, image);
};
p.module = try loadModule(gpa, io, ctx.d, kernel_dir, "drama_tower.fatbin", cuda.kernels.drama_tower);
p.kernels = try Kernels.resolve(p.module.?);
try p.kernels.sample.allowDynamicShared(sample_shared_bytes);
p.library = try cuda.cublaslt.Library.open();
Expand Down Expand Up @@ -444,6 +467,11 @@ pub const Model = struct {
p.output_hidden = try p.allocate(rows * hidden * 2);
p.head_logits = try p.allocate(4096 * 2);
p.result = try p.allocate(48 + 16 * 4);
// The probe is an optimization: a failure keeps every plan, but a sticky fault poisons the context: fatal.
p.qualifyGemv(io, ctx, kernel_dir) catch |e| {
try ctx.synchronize();
std.log.warn("drama tower: one-row projections on cuBLASLt: drama_tower_gemv qualification failed: {s}", .{@errorName(e)});
};
return .{ .impl = p };
}

Expand Down
Loading
Loading