Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 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
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
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
b56fc8c
drama: below sm_90 the tower's decode row runs drama_attention, and i…
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
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
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: add-norm fused with the routed experts' FP8 activation quantiz…
…ation

The residual add and RMS norm before the MoE also write the normed rows' FP8 groups, in the same order and
rounding as the separate quantization launch they replace.
  • Loading branch information
plotarmordev committed Oct 16, 2026
commit 9dc8202665988b91014c74fcffb3d2b95cc1c08d
81 changes: 65 additions & 16 deletions zig/kernels/cuda/drama_backbone.cu
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ __device__ __forceinline__ float block_sum(float x, float* scratch, int nwarps)
__device__ __forceinline__ float sigmoid(float x) { return 1.0f / (1.0f + expf(-x)); }
__device__ __forceinline__ float silu(float x) { return x / (1.0f + expf(-x)); }

__device__ __forceinline__ void load8(const __nv_bfloat16* p, float (&v)[8]) {
const uint4 u = *reinterpret_cast<const uint4*>(p);
const __nv_bfloat16* b = reinterpret_cast<const __nv_bfloat16*>(&u);
#pragma unroll
for (int i = 0; i < 8; ++i) v[i] = bf(b[i]);
}

__device__ __forceinline__ void store8(__nv_bfloat16* p, const float (&v)[8]) {
uint4 u;
__nv_bfloat16* b = reinterpret_cast<__nv_bfloat16*>(&u);
#pragma unroll
for (int i = 0; i < 8; ++i) b[i] = tobf(v[i]);
*reinterpret_cast<uint4*>(p) = u;
}

// drama_act_quant's e4m3 byte of v under its group's multiplier inv = 448 / amax.
__device__ __forceinline__ uint32_t e4m3(float v, float inv) {
const float c = fminf(fmaxf(v * inv, -448.0f), 448.0f);
return static_cast<uint32_t>(__nv_cvt_float_to_fp8(c, __NV_SATFINITE, __NV_E4M3));
}

} // namespace drama

using namespace drama;
Expand All @@ -58,21 +79,26 @@ extern "C" __global__ void __launch_bounds__(256) drama_semantic_mix(const int*
}
}

// GemmaRMSNorm in FlashInfer's CuTe order (8 values a lane, FMA squares, XOR 1..16) with one bf16 residual store.
// GemmaRMSNorm in FlashInfer's CuTe order with one bf16 residual store; q / s (both or neither) add its FP8 groups.
extern "C" __global__ void __launch_bounds__(256) drama_add_norm(const __nv_bfloat16* x, __nv_bfloat16* res,
const __nv_bfloat16* w, __nv_bfloat16* y,
int width, float eps, int first) {
uint8_t* q, float* s, int width, float eps,
int first) {
__shared__ float square_sum;
const size_t base = static_cast<size_t>(blockIdx.x) * width;
const int t = threadIdx.x;
if (t < 32) {
// Unrolled so every load is in flight before the FMA chain, which keeps the serial reference order.
float sum = 0.f;
#pragma unroll 8
for (int block = 0; block < width; block += 256) {
float value[8], other[8];
load8(x + base + block + 8 * t, value);
if (!first) load8(res + base + block + 8 * t, other);
#pragma unroll
for (int v = 0; v < 8; ++v) {
const int i = block + 8 * t + v;
float value = bf(x[base + i]);
if (!first) value += bf(res[base + i]);
sum = fmaf(value, value, sum);
if (!first) value[v] += other[v];
sum = fmaf(value[v], value[v], sum);
}
}
for (int offset = 1; offset < 32; offset <<= 1)
Expand All @@ -81,11 +107,38 @@ extern "C" __global__ void __launch_bounds__(256) drama_add_norm(const __nv_bflo
}
__syncthreads();
const float rcp = rsqrtf(square_sum / static_cast<float>(width) + eps);
for (int i = t; i < width; i += blockDim.x) {
float value = bf(x[base + i]);
if (!first) value += bf(res[base + i]);
res[base + i] = tobf(value);
y[base + i] = tobf(value * rcp * (1.0f + bf(w[i])));
for (int c = 8 * t; c < width; c += 8 * blockDim.x) {
float value[8], other[8], weight[8], out[8];
load8(x + base + c, value);
if (!first) load8(res + base + c, other);
load8(w + c, weight);
#pragma unroll
for (int i = 0; i < 8; ++i) {
if (!first) value[i] += other[i];
out[i] = value[i] * rcp * (1.0f + weight[i]);
}
store8(res + base + c, value);
store8(y + base + c, out);
if (q == nullptr) continue;
// A 128-wide group is 16 lanes; the max is exact in any order.
float amax = 0.f;
#pragma unroll
for (int i = 0; i < 8; ++i) {
out[i] = round_bf(out[i]);
amax = fmaxf(amax, fabsf(out[i]));
}
#pragma unroll
for (int m = 8; m; m >>= 1) amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, m));
amax = fmaxf(amax, 1e-10f);
const float inv = __fdividef(448.0f, amax);
uint2 packed = {0u, 0u};
#pragma unroll
for (int i = 0; i < 4; ++i) {
packed.x |= e4m3(out[i], inv) << (8 * i);
packed.y |= e4m3(out[4 + i], inv) << (8 * i);
}
*reinterpret_cast<uint2*>(q + base + c) = packed;
if ((t & 15) == 0) s[static_cast<size_t>(blockIdx.x) * (width / 128) + c / 128] = amax * (1.0f / 448.0f);
}
}

Expand Down Expand Up @@ -348,11 +401,7 @@ extern "C" __global__ void __launch_bounds__(128) drama_act_quant(const __nv_bfl
const float inv = __fdividef(448.0f, amax);
uint32_t packed = 0;
#pragma unroll
for (int i = 0; i < 4; ++i) {
const float c = fminf(fmaxf(v[i] * inv, -448.0f), 448.0f);
const uint32_t b = static_cast<uint32_t>(__nv_cvt_float_to_fp8(c, __NV_SATFINITE, __NV_E4M3));
packed |= b << (8 * i);
}
for (int i = 0; i < 4; ++i) packed |= e4m3(v[i], inv) << (8 * i);
*reinterpret_cast<uint32_t*>(q + static_cast<size_t>(r) * K + group * 128 + lane * 4) = packed;
if (lane == 0) s[static_cast<size_t>(r) * (K / 128) + group] = sc;
}
Expand Down
16 changes: 9 additions & 7 deletions zig/src/families/drama/backbone.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub const Stage = enum { attention_input, attention_residual, attention_output,
/// Hashed by the trace driver so a capture names the exact forward it observed; unreferenced in production.
pub const trace_source = @embedFile("backbone.zig");

/// Diagnostic selector for Model.replayOperator: a layer's attention or moe; tf-drama-backbone-replay only.
/// Diagnostic replayOperator selector: a layer's attention, or moe on the FP8 groups addNorm writes in forward.
pub const ReplayOperator = enum { attention, moe };

/// Bump offsets in one device allocation, 256-byte aligned.
Expand Down Expand Up @@ -289,18 +289,18 @@ pub const Model = struct {
try k.embed.bf16(s, sc.ids, self.w.embed, sc.x, n, H);
if (extra) |e| try k.semanticMix(s, sc.ids, sc.x, e.ptr, n, H, self.semantic_start, self.semantic_end);
for (&self.w.layers, 0..) |*l, i| {
try k.addNorm(s, sc.x, sc.residual, l.ln1, sc.normed, n, H, config.eps, i == 0);
try k.addNorm(s, sc.x, sc.residual, l.ln1, sc.normed, null, null, n, H, config.eps, i == 0);
try self.stage(trace, s, i, .attention_input, sc.normed, n);
try self.stage(trace, s, i, .attention_residual, sc.residual, n);
if (config.linear(i)) try self.deltaNet(s, phase, l, config.slot(i), n) else try self.fullAttention(s, l, config.slot(i), n);
try self.stage(trace, s, i, .attention_output, sc.x, n);
try k.addNorm(s, sc.x, sc.residual, l.ln2, sc.normed, n, H, config.eps, false);
try k.addNorm(s, sc.x, sc.residual, l.ln2, sc.normed, sc.xq, sc.xs, n, H, config.eps, false);
try self.stage(trace, s, i, .mlp_input, sc.normed, n);
try self.stage(trace, s, i, .mlp_residual, sc.residual, n);
try self.moe(s, l, n);
try self.stage(trace, s, i, .mlp_output, sc.x, n);
}
try k.addNorm(s, sc.x, sc.residual, self.w.final_norm, self.hidden_buf.ptr, n, H, config.eps, false);
try k.addNorm(s, sc.x, sc.residual, self.w.final_norm, self.hidden_buf.ptr, null, null, n, H, config.eps, false);
try k.head(s, self.hidden_buf.ptr + (n - 1) * H * 2, self.w.head, weights.head_cols, H, self.logits_buf.ptr);
}

Expand Down Expand Up @@ -338,7 +338,10 @@ pub const Model = struct {
try self.deltaNet(stream, .prefill, l, config.slot(layer), n)
else
try self.fullAttention(stream, l, config.slot(layer), n),
.moe => try self.moe(stream, l, n),
.moe => {
try self.k.actQuant(stream, self.s.normed, H, self.s.xq, self.s.xs, n, H);
try self.moe(stream, l, n);
},
}
const x: cuda.DeviceBuffer = .{ .d = self.d, .ptr = self.s.x, .len = bytes, .borrowed = true };
try x.downloadAsync(0, output[at..][0..bytes], stream.handle);
Expand Down Expand Up @@ -410,7 +413,7 @@ pub const Model = struct {
try k.gemm(s, sc.gated, width, n, l.attn_out, H, width, sc.x, H);
}

/// Qwen2MoeSparseMoeBlock: top-8 routing, block-FP8 experts, SwiGLU, weighted down, sigmoid-gated shared expert.
/// Top-8 block-FP8 experts and the gated shared expert on Scratch.normed and its FP8 groups in xq / xs.
fn moe(self: *Model, s: cuda.Stream, l: *const weights.Layer, n: usize) !void {
const k = &self.k;
const sc = &self.s;
Expand All @@ -419,7 +422,6 @@ pub const Model = struct {
const I = config.expert_inner;
try k.gemm(s, sc.normed, H, n, l.moe_in, ld, H, sc.moe_in, ld);
try k.topk(s, sc.moe_in + weights.router_col * 2, ld, sc.picks, sc.routed, n, top);
try k.actQuant(s, sc.normed, H, sc.xq, sc.xs, n, H);
const plan: cuda.grouped.Plan = .{ .members = sc.members, .items = sc.items, .counts = sc.counts, .rank = sc.rank, .hist = sc.hist };
try k.router.route(s, sc.picks, pairs, config.experts, tile, plan);
const items = cuda.grouped.maxItems(pairs, config.experts, tile);
Expand Down
8 changes: 5 additions & 3 deletions zig/src/families/drama/backbone_kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ pub const Kernels = struct {
try go(s, k.semantic_mix_fn, .{ rows, 1, 1 }, 256, &a);
}

/// res = x (first) or bf16(x + res); y = bf16(norm(x + res) * (1 + w)).
pub fn addNorm(k: *const Kernels, s: cuda.Stream, x: u64, res: u64, w: u64, y: u64, rows: usize, width: usize, eps: f32, first: bool) !void {
if (width > 256 * 16) return error.DramaRowShape;
/// res = x (first) or bf16(x + res); y = bf16(norm(x + res) * (1 + w)), with q / scales also y's FP8 groups.
pub fn addNorm(k: *const Kernels, s: cuda.Stream, x: u64, res: u64, w: u64, y: u64, q: ?u64, scales: ?u64, rows: usize, width: usize, eps: f32, first: bool) !void {
if (width % 256 != 0 or (x | res | w | y | (q orelse 0)) % 16 != 0) return error.DramaRowShape;
var a: cuda.Args = .{};
a.add(x);
a.add(res);
a.add(w);
a.add(y);
a.add(q orelse 0);
a.add(scales orelse 0);
a.add(int(width));
a.add(eps);
a.add(@as(i32, @intFromBool(first)));
Expand Down