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
6 changes: 3 additions & 3 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const golden_build = @import("golden.zig");
const catalog = @import("cuda_kernels.zig");
pub const Kernel = catalog.Kernel;
pub const kernels = catalog.kernels;

pub const runtime = @import("cuda_runtime.zig").runtime;
const stagger = @import("cuda_runtime.zig").stagger;
const engines_build = @import("cuda_engines.zig");
Expand Down Expand Up @@ -131,12 +130,13 @@ pub fn targets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu
q35.addImport("cuda", cuda);
q35.addImport("core", mods.core);
q35.addImport("qwen35", mods.qwen35);
b.installArtifact(b.addExecutable(.{ .name = "tf-qwen35-cuda", .root_module = q35 }));
const q35_install = b.addInstallArtifact(b.addExecutable(.{ .name = "tf-qwen35-cuda", .root_module = q35 }), .{});
b.getInstallStep().dependOn(&q35_install.step);
b.step("qwen35-cuda", "Build the Qwen CUDA forward and raw state receipt tool").dependOn(&q35_install.step);
const d3_tool = b.createModule(.{ .root_source_file = b.path("zig/src/families/drama3/generate.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{ .{ .name = "core", .module = mods.core }, .{ .name = "drama3", .module = @import("drama3_host.zig").module(b, target, optimize, cuda, mods.core) } } });
const d3_exe = b.addExecutable(.{ .name = "drama3-generate", .root_module = d3_tool });
b.step("drama3-generate", "Drama generation and replay controls on CUDA").dependOn(&b.addInstallArtifact(d3_exe, .{}).step);
}

/// The Kolibri-1 CUDA family over the runtime, the core and the lane core.
const kolibri1Module = @import("kolibri.zig").module;

Expand Down
9 changes: 9 additions & 0 deletions zig/build/kernel_contract.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,14 @@ pub fn cuda(b: *std.Build, runner: *std.Build.Step.Compile, controls: ?std.Build
const run = b.addRunArtifact(runner);
run.addArg("kernel-contract");
b.step("kernel-contract", "Every registered CUDA kernel through the contract's gates on this GPU, each control red").dependOn(&run.step);
const prefill_many = b.addRunArtifact(runner);
prefill_many.addArg("qmm-prefill-many-contract");
b.step("qmm-prefill-many-contract", "GB10 bit-exact multi-projection prompt launch against separate launches").dependOn(&prefill_many.step);
const prefill_tile22 = b.addRunArtifact(runner);
prefill_tile22.addArg("qmm-prefill-tile22-contract");
b.step("qmm-prefill-tile22-contract", "GB10 large-prompt 2x2 tile against the default tile").dependOn(&prefill_tile22.step);
const prefill_many_bench = b.addRunArtifact(runner);
prefill_many_bench.addArg("qmm-prefill-many-bench");
b.step("qmm-prefill-many-bench", "Same-process A-B-A screening for the GB10 prompt launch fusion").dependOn(&prefill_many_bench.step);
return install;
}
2 changes: 1 addition & 1 deletion zig/dense_serving_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test "dense serving: owned CUDA images retain their reviewed lane and decode byt
const group = @embedFile("kernels/cuda/qmm_group.cu");
try std.testing.expectEqualStrings("3b27b8e964f6e7453e962cd7a259253ae7e1940297439b4b518f05da04dd1041", &hash(group[0..16132]));
try std.testing.expectEqualStrings("25172d5cd98fe2768a216183336f7eb9c23a66b0a56cfff77b9825ef66a481a6", &hash(group));
try std.testing.expectEqualStrings("2e4f98ea301a4d87639c55b93c37577a9fd7f3cda0cbae055c7ba4958f019bfd", &hash(@embedFile("kernels/cuda/qmm_prefill.cu")));
try std.testing.expectEqualStrings("b5c2016899f701ba865f9bac6ba1e240b98256633356b95f958f8a014804b229", &hash(@embedFile("kernels/cuda/qmm_prefill.cu")));
try std.testing.expectEqualStrings("0a78ac9b394106ef27053a786660e440292f42cc3a49abca432f4522d4488b63", &hash(@embedFile("kernels/cuda/qwen35.cu")));
try std.testing.expectEqualStrings("c145572fa895e22c32960a1f6f65bf6bb11ced11b7d65121d7261a4f84a0faa2", &hash(@embedFile("kernels/cuda/qwen35.cuh")));
try std.testing.expectEqualStrings("60ffcb12aaf8155d8b82274299a0e21cb007d06e540c47cfeaeb6de2e69a6317", &hash(@embedFile("kernels/cuda/qwen35_draft.cu")));
Expand Down
144 changes: 144 additions & 0 deletions zig/kernels/cuda/qmm_prefill.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ namespace tf_qmm_prefill {

using namespace qmm_frag;

constexpr int PARTS = 4;

struct PrefillPart {
const uint32_t* w;
const __nv_bfloat16* scales;
const __nv_bfloat16* biases;
void* out;
int n, npad, first;
};

struct PrefillParts {
PrefillPart p[PARTS];
int count;
};

template <int GS, int BM, int BN, int WM, int WN, int STAGES>
struct Tile {
static constexpr int THREADS = WM * WN * 32;
Expand All @@ -30,6 +45,122 @@ __device__ __forceinline__ uint32_t fma2(uint32_t a, uint32_t b, uint32_t c) {
return d;
}

template <int GS, int BM, int BN, int WM, int WN, int STAGES, bool F32>
__device__ __forceinline__ void prefill_body(
const __nv_bfloat16* __restrict__ x, const uint32_t* __restrict__ w, const __nv_bfloat16* __restrict__ scales,
const __nv_bfloat16* __restrict__ biases, void* __restrict__ out, int M, int N, int K, int npad, int ldx,
int group, unsigned int block) {
using T = Tile<GS, BM, BN, WM, WN, STAGES>;
extern __shared__ __align__(128) unsigned char buf[];
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
const int wm = warp / WN, wn = warp % WN;
const int KG = K / GS, per = KG, g0 = 0;
const int2 at = tile_of(block, M, N, BM, BN, group);
const int m0 = at.x, n0 = at.y;

auto stage = [&](int s) { return buf + s * T::STAGE; };
auto load = [&](int s, int g) {
unsigned char* p = stage(s);
for (int c = tid; c < BM * T::CHUNKS; c += T::THREADS) {
const int r = c / T::CHUNKS, ch = c % T::CHUNKS;
const int row = min(m0 + r, M - 1);
cp16z(p + r * T::ROW + swz<T::CHUNKS>(r, ch) * 16, x + static_cast<size_t>(row) * ldx + g * GS + ch * 8,
m0 + r < M);
}
unsigned char* pw = p + T::X;
constexpr int TILE_BYTES = 64 * GS / 2; // one stored 64-column tile's group block
for (int c = tid; c < T::W / 16; c += T::THREADS) {
const int t = c / (TILE_BYTES / 16), off = c % (TILE_BYTES / 16);
const size_t tile = static_cast<size_t>(n0 / 64 + t) * KG + g;
if (n0 + t * 64 < npad) // a 256-wide block's last tiles may pass the padded columns
cp16(pw + c * 16, reinterpret_cast<const unsigned char*>(w) + tile * TILE_BYTES + off * 16);
}
unsigned char* ps = pw + T::W;
for (int c = tid; c < 2 * (T::S / 16); c += T::THREADS) {
const int which = c / (T::S / 16), off = c % (T::S / 16);
const __nv_bfloat16* src = (which ? biases : scales) + static_cast<size_t>(g) * npad + n0 + off * 8;
if (n0 + off * 8 < npad) cp16(ps + which * T::S + off * 16, src);
}
};

float acc[T::MT][T::NT][4];
#pragma unroll
for (int i = 0; i < T::MT; ++i)
#pragma unroll
for (int j = 0; j < T::NT; ++j)
#pragma unroll
for (int e = 0; e < 4; ++e) acc[i][j][e] = 0.0f;
#pragma unroll
for (int s = 0; s < STAGES - 1; ++s) {
if (s < per) load(s, g0 + s);
commit();
}
for (int it = 0; it < per; ++it) {
wait<STAGES - 2>();
__syncthreads();
const int next = it + STAGES - 1;
if (next < per) load(next % STAGES, g0 + next);
commit();
const unsigned char* p = stage(it % STAGES);
const uint32_t* pw = reinterpret_cast<const uint32_t*>(p + T::X);
const uint16_t* ps = reinterpret_cast<const uint16_t*>(p + T::X + T::W);
uint32_t words[T::NT][GS / 32], sv[T::NT], bv[T::NT];
#pragma unroll
for (int j = 0; j < T::NT; ++j) {
#pragma unroll
for (int v = 0; v < GS / 32; ++v) words[j][v] = pw[((wn * T::NT + j) * 32 + lane) * (GS / 32) + v];
const int col = wn * (BN / WN) + j * 8 + (lane >> 2); // this lane's B-fragment column
sv[j] = ps[col] * 0x10001u; // (s, s) and (b, b) as bf16 pairs
bv[j] = ps[BN + col] * 0x10001u;
}
#pragma unroll
for (int kt = 0; kt < GS / 16; ++kt) {
uint32_t a[T::MT][4];
#pragma unroll
for (int i = 0; i < T::MT; ++i) {
const int r = wm * (BM / WM) + i * 16 + (lane & 7) + ((lane >> 3) & 1) * 8;
const int ch = kt * 2 + (lane >> 4);
ldmatrix4(a[i], p + r * T::ROW + swz<T::CHUNKS>(r, ch) * 16);
}
#pragma unroll
for (int j = 0; j < T::NT; ++j) {
const uint32_t b0 = fma2(pair(words[j][kt / 2], (kt & 1) * 8), sv[j], bv[j]);
const uint32_t b1 = fma2(pair(words[j][kt / 2], (kt & 1) * 8 + 4), sv[j], bv[j]);
#pragma unroll
for (int i = 0; i < T::MT; ++i) mma(acc[i][j], a[i], b0, b1);
}
}
}
wait<0>();
__syncthreads();
#pragma unroll
for (int i = 0; i < T::MT; ++i)
#pragma unroll
for (int j = 0; j < T::NT; ++j) {
const int col = n0 + wn * (BN / WN) + j * 8 + (lane & 3) * 2;
#pragma unroll
for (int h = 0; h < 2; ++h) {
const int row = m0 + wm * (BM / WM) + i * 16 + (lane >> 2) + h * 8;
if (row >= M) continue;
const float v0 = acc[i][j][2 * h], v1 = acc[i][j][2 * h + 1];
if (F32) {
float* dst = reinterpret_cast<float*>(out) + static_cast<size_t>(row) * N + col;
if (col < N) dst[0] = v0;
if (col + 1 < N) dst[1] = v1;
} else {
__nv_bfloat16* dst = reinterpret_cast<__nv_bfloat16*>(out) + static_cast<size_t>(row) * N + col;
if (col + 1 < N && (N & 1) == 0)
*reinterpret_cast<__nv_bfloat162*>(dst) = __floats2bfloat162_rn(v0, v1);
else {
if (col < N) dst[0] = __float2bfloat16_rn(v0);
if (col + 1 < N) dst[1] = __float2bfloat16_rn(v1);
}
}
}
}
}

// Keep the shipped kernel body literal: sharing its helper changed Blackwell SASS under nvcc 13.0 and 13.3.
template <int GS, int BM, int BN, int WM, int WN, int STAGES, bool F32>
__global__ void __launch_bounds__(WM * WN * 32) prefill_kernel(
const __nv_bfloat16* __restrict__ x, const uint32_t* __restrict__ w, const __nv_bfloat16* __restrict__ scales,
Expand Down Expand Up @@ -147,6 +278,19 @@ __global__ void __launch_bounds__(WM * WN * 32) prefill_kernel(

} // namespace tf_qmm_prefill

// One grid runs up to four projections through the shipped body with projection-local block indices.
extern "C" __global__ __launch_bounds__(256) void tf_qmm_prefill_many(
const __nv_bfloat16* __restrict__ x, const __grid_constant__ tf_qmm_prefill::PrefillParts parts, int M, int K,
int ldx, int group) {
int q = 0;
#pragma unroll
for (int i = 1; i < tf_qmm_prefill::PARTS; ++i)
if (i < parts.count && blockIdx.x >= parts.p[i].first) q = i;
const tf_qmm_prefill::PrefillPart P = parts.p[q];
tf_qmm_prefill::prefill_body<64, 128, 128, 2, 4, 3, false>(x, P.w, P.scales, P.biases, P.out, M, P.n, K,
P.npad, ldx, group, blockIdx.x - P.first);
}

// The instantiation prefill_matmul launches (tile 0: 128x128 on 2x4 warps, 3 stages), bf16 out.
template __global__ void tf_qmm_prefill::prefill_kernel<64, 128, 128, 2, 4, 3, false>(
const __nv_bfloat16* __restrict__, const uint32_t* __restrict__, const __nv_bfloat16* __restrict__, const __nv_bfloat16* __restrict__, void* __restrict__, int, int, int,
Expand Down
6 changes: 6 additions & 0 deletions zig/src/core/contract/measurements/qmm_prefill_132.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! nvcc 13.2 five-SM receipts for Qwen prefill; nvcc 13.3 produced the same usage.
const Override = @import("../registry.zig").Override;
pub const records = [_]Override{
.{ .name = "qmm_prefill many bit-exact BF16 launch fusion", .usage = .{ .{ .regs = 172, .static = 0, .bound = 256 }, .{ .regs = 172, .static = 0, .bound = 256 }, .{ .regs = 172, .static = 0, .bound = 256 }, .{ .regs = 128, .static = 1024, .bound = 256 }, .{ .regs = 128, .static = 0, .bound = 256 } } },
.{ .name = "qmm_prefill 2x2 bit-exact BF16 GB10 large tile", .usage = .{ .{ .regs = 255, .static = 0, .bound = 128 }, .{ .regs = 255, .static = 0, .bound = 128 }, .{ .regs = 255, .static = 0, .bound = 128 }, .{ .regs = 220, .static = 0, .bound = 128 }, .{ .regs = 220, .static = 0, .bound = 128 } } },
};
2 changes: 1 addition & 1 deletion zig/src/core/contract/pending_132.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Explicit pending-launch measurements; absent names and earlier compilers stay pending.
pub const records = @import("measurements/qwen_decode_132.zig").records ++ @import("measurements/deepseek_a_132.zig").records ++ @import("measurements/deepseek_b_132.zig").records ++ @import("measurements/glm_sampling_132.zig").records ++ @import("measurements/qwen_lanes_132.zig").records;
pub const records = @import("measurements/qwen_decode_132.zig").records ++ @import("measurements/deepseek_a_132.zig").records ++ @import("measurements/deepseek_b_132.zig").records ++ @import("measurements/glm_sampling_132.zig").records ++ @import("measurements/qwen_lanes_132.zig").records ++ @import("measurements/qmm_prefill_132.zig").records;

const std = @import("std");
const limits = @import("limits.zig");
Expand Down
2 changes: 2 additions & 0 deletions zig/src/core/contract/registry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub const cuda = @import("flashnext_cuda_records.zig").cuda ++ [_]CudaRecord{
.{ .launch = .{ .name = "latent tf_super_topk_few", .symbol = "tf_super_topk_few", .image = "nemotron_latent", .threads = 1024 }, .usage = .{ .{ .regs = 32, .static = 4352, .bound = 1024 }, .{ .regs = 38, .static = 4352, .bound = 1024 }, .{ .regs = 38, .static = 4352, .bound = 1024 }, .{ .regs = 30, .static = 4352, .bound = 1024 }, .{ .regs = 30, .static = 4352, .bound = 1024 } } },
.{ .launch = .{ .name = "qmm_group cluster of 8", .symbol = group_symbol, .image = "qmm_group", .threads = 128, .dynamic = 35328, .cluster = 8, .min_sm = 90 }, .usage = .{ .{ .regs = 54, .static = 0, .bound = 128 }, .{ .regs = 54, .static = 0, .bound = 128 }, .{ .regs = 54, .static = 0, .bound = 128 }, .{ .regs = 48, .static = 0, .bound = 128 }, .{ .regs = 48, .static = 0, .bound = 128 } } },
.{ .launch = .{ .name = "qmm_group one CTA a tile", .symbol = group_symbol, .image = "qmm_group", .threads = 128, .dynamic = 35328 }, .usage = .{ .{ .regs = 54, .static = 0, .bound = 128 }, .{ .regs = 54, .static = 0, .bound = 128 }, .{ .regs = 54, .static = 0, .bound = 128 }, .{ .regs = 48, .static = 0, .bound = 128 }, .{ .regs = 48, .static = 0, .bound = 128 } } },
.{ .launch = .{ .name = "qmm_prefill many bit-exact BF16 launch fusion", .symbol = "tf_qmm_prefill_many", .image = "qmm_prefill", .threads = 256, .dynamic = 62976, .opt_in = true, .min_sm = 121 }, .usage = .{ .{ .regs = 171, .static = 0, .bound = 256 }, .{ .regs = 171, .static = 0, .bound = 256 }, .{ .regs = 171, .static = 0, .bound = 256 }, .{ .regs = 128, .static = 1024, .bound = 256 }, .{ .regs = 128, .static = 0, .bound = 256 } }, .later_pending = true },
.{ .launch = .{ .name = "qmm_prefill 2x2 bit-exact BF16 GB10 large tile", .symbol = "_ZN14tf_qmm_prefill14prefill_kernelILi64ELi128ELi128ELi2ELi2ELi2ELb0EEEvPK13__nv_bfloat16PKjS3_S3_Pviiiiii", .image = "qmm_prefill", .threads = 128, .dynamic = 41984 }, .usage = .{ .{ .regs = 255, .static = 0, .bound = 128 }, .{ .regs = 255, .static = 0, .bound = 128 }, .{ .regs = 255, .static = 0, .bound = 128 }, .{ .regs = 214, .static = 0, .bound = 128 }, .{ .regs = 214, .static = 0, .bound = 128 } }, .later_pending = true },
.{ .launch = .{ .name = "lane_gemv", .symbol = gemv_symbol, .image = "lane_gemv", .threads = 128, .dynamic = 35328 }, .usage = .{ .{ .regs = 64, .static = 0, .bound = 128 }, .{ .regs = 57, .static = 0, .bound = 128 }, .{ .regs = 57, .static = 0, .bound = 128 }, .{ .regs = 64, .static = 0, .bound = 128 }, .{ .regs = 64, .static = 0, .bound = 128 } } },
.{ .launch = .{ .name = "lane_gemv split-K", .symbol = "tf_lane_gemv_split", .image = "lane_gemv", .threads = 128, .dynamic = 17664 }, .usage = .{ .{ .regs = 62, .static = 128, .bound = 128 }, .{ .regs = 56, .static = 128, .bound = 128 }, .{ .regs = 56, .static = 128, .bound = 128 }, .{ .regs = 44, .static = 128, .bound = 128 }, .{ .regs = 44, .static = 128, .bound = 128 } } },
.{ .launch = .{ .name = "lane_gemv segmented split-K", .symbol = "tf_lane_gemv_seg", .image = "lane_gemv", .threads = 128, .dynamic = 17664 }, .usage = .{ .{ .regs = 62, .static = 128, .bound = 128 }, .{ .regs = 56, .static = 128, .bound = 128 }, .{ .regs = 56, .static = 128, .bound = 128 }, .{ .regs = 54, .static = 128, .bound = 128 }, .{ .regs = 54, .static = 128, .bound = 128 } } },
Expand Down
Loading