Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
3688f3c
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
b7be793
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
faf5901
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
3cc0397
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
c58d83c
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
25c02d3
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
6b5404d
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
06c4e75
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
062af8b
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
d7510ef
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
e16e331
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
649e788
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
664a8e2
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
4ad5029
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
9bbc21a
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
cf19042
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
0ba7d30
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
0918809
Let a family's round planner drive the lane host with prompt pieces
jayleaton Oct 10, 2026
361f063
Lane host halt: a stop's drain deadline ends the rounds at the next b…
jayleaton Oct 10, 2026
e9f9210
Round planner test: both prompts queue before the host starts, and it…
jayleaton Oct 10, 2026
96ae865
Server hooks for a model family's own chat rules.
jayleaton Oct 10, 2026
df480e0
Server: a stop logs its signal and the open connections when the acce…
jayleaton Oct 10, 2026
2c4d837
Server: SIGTERM / SIGINT drains before stopping.
jayleaton Oct 10, 2026
55bf0a7
Server tests: a stop that lands between a request's count and its dra…
jayleaton Oct 10, 2026
9fbc2b3
Server: a stream that has not sent its first token gets keepalives ev…
jayleaton Oct 10, 2026
bb53ff4
Server tests: the drain test's reply in progress is held until its dr…
jayleaton Oct 10, 2026
9eb765d
DeepSeek-V4.1 host side: release config, EXL3 pack headers, a rank's …
jayleaton Oct 11, 2026
865335e
DeepSeek-V4.1 weight forms built at load, and prepared weight folders…
jayleaton Oct 11, 2026
7ccfc5c
DeepSeek-V4.1 Engram host side: table layout, n-gram hashes of a wind…
jayleaton Oct 11, 2026
b775815
DeepSeek-V4.1 phase timers: where a serving round's time goes, by nam…
jayleaton Oct 11, 2026
def4859
DeepSeek-V4.1 Engram rows from packed per-rank shards on local NVMe, …
jayleaton Oct 11, 2026
c7d29ea
DeepSeek-V4.1 CUDA kernel sources: CSA2 attention and top-k, mHC, rou…
jayleaton Oct 11, 2026
b17df97
DeepSeek-V4.1 kernel tooling: nvcc in a container for any checkout la…
jayleaton Oct 11, 2026
73be75f
DeepSeek-V4.1 kernel launch bindings and their loader: each kernel's …
jayleaton Oct 11, 2026
e687ad5
DeepSeek-V4.1 device kernel tests: tf-dsv41-test, bit-exact against t…
jayleaton Oct 11, 2026
a7674d2
DeepSeek-V4.1 kernel prototypes: attention merge and phase timing, x3…
jayleaton Oct 11, 2026
c0605f0
DeepSeek-V4.1 device-side load: named weights in device buffers, run-…
jayleaton Oct 11, 2026
46bff5e
DeepSeek-V4.1 block emitter: a decode window as calls (attention, the…
jayleaton Oct 11, 2026
6d44dab
CUDA runtime: prioritized streams, external event records, 2D async c…
jayleaton Oct 11, 2026
63e708f
CUDA AOT sets: aot.json specs parsed and matched without cubins, and …
jayleaton Oct 11, 2026
2eeabf7
CUDA memory: a driver allocation count, the arena's first failed chun…
jayleaton Oct 11, 2026
a89bd4f
Graph cache: named caches publish their counts, the floor's state is …
jayleaton Oct 11, 2026
05977fa
Session store: historyOf returns an entry's ids from RAM or from the …
jayleaton Oct 11, 2026
063e855
Graph cache registry: slots claimed, published, released and read und…
jayleaton Oct 11, 2026
d800c62
DeepSeek-V4.1 KV memory: the family's pages, split KV's exchange, the…
jayleaton Oct 11, 2026
bf5a8e8
DeepSeek-V4.1 forward: decode windows over the block emitter, split K…
jayleaton Oct 11, 2026
cd1e3d8
DeepSeek-V4.1 prefill: prompt segments over the prefill emitter, CED …
jayleaton Oct 11, 2026
989ca7e
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
05c5213
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
a3303c7
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
69e8a65
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
d7e89df
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
5826f89
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
5806e55
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
d335520
Graph cache: TF_GRAPH_CACHE=0 or off runs every step eagerly in any f…
jayleaton Oct 14, 2026
7ff83d8
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
9af2ea2
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
7c9a26e
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
8ebb0be
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
e9e0056
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
0472cbf
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
9688398
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
72132f0
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
e65583c
Server hooks for a model family's own chat rules.
jayleaton Oct 10, 2026
bf19238
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
9a486bb
Server: a stop logs its signal and the open connections when the acce…
jayleaton Oct 10, 2026
beae7e8
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
467504a
Server: SIGTERM / SIGINT drains before stopping.
jayleaton Oct 10, 2026
602aad5
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
23bf7d8
Server tests: a stop that lands between a request's count and its dra…
jayleaton Oct 10, 2026
890dfc6
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
5410eac
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
a5b8b35
Server: a stream that has not sent its first token gets keepalives ev…
jayleaton Oct 10, 2026
a3e2fb1
TP NCCL: all_reduce's doc says NCCL adds in its ring's order, so its …
jayleaton Oct 14, 2026
0d0bdb2
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
2b3cccf
Server tests: the drain test's reply in progress is held until its dr…
jayleaton Oct 10, 2026
62a75ae
TP: keep Linux-only transport host tests off macOS builds
jayleaton Oct 15, 2026
2cf61d3
Sessions: honor zero residency budgets and reserve uncapped requests
jayleaton Oct 15, 2026
9367784
server: keep shared halt and drain on the existing template path
jayleaton Oct 15, 2026
864fe25
Graph cache: effective settings have one digest for ranks to compare …
jayleaton Oct 16, 2026
e2504f6
TP: bootstrap compares the graph cache's effective settings, so a ran…
jayleaton Oct 16, 2026
dcdc30c
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
368a0d3
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
7a6b32f
server: main's drain stop closes the engine without the closed lane-h…
jayleaton Oct 16, 2026
abcad7d
Merge ashhart/main into stack/02-graph-cache-arena
jayleaton Oct 16, 2026
02d455e
Merge stack/02-graph-cache-arena (with ashhart/main) into stack/03-tp…
jayleaton Oct 16, 2026
9266f3f
Merge stack/03-tp-fabric (with ashhart/main) into stack/04-sessions
jayleaton Oct 16, 2026
001ac2e
Merge stack/04-sessions (with ashhart/main) into stack/07-server
jayleaton Oct 16, 2026
33c0ef6
Merge stack/07-server (sessions, TP, graph cache, current main) into …
jayleaton Oct 16, 2026
af0bc53
Server tests: the drain test imports the named json module
jayleaton Oct 16, 2026
7051a3c
Merge stack/07-server's drain-test json import fix into stack/08a-config
jayleaton Oct 16, 2026
e6df8ad
merge origin/main: the round planner and halt on main's split lane host
jayleaton Oct 16, 2026
4da2618
Merge shared/lane-host-core (#861, on current main) into stack/08a-co…
jayleaton Oct 16, 2026
6b9bede
Merge stack/08a-config (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
f17db02
Merge stack/08b-engram (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
0137ac2
Merge stack/08c-kernels (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
cf649ff
Merge stack/08e-block (current main, shared layers, #861) into stack/…
jayleaton Oct 16, 2026
4a02606
Merge stack/08r-runtime (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
968a4f2
Merge stack/09a-kv (current main, shared layers, #861) into stack/09b…
jayleaton Oct 16, 2026
1d0b2d7
Merge stack/09b-forward (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
8900c7c
Sessions: a failed prefix insert leaves the index as it was
jayleaton Oct 17, 2026
3aeebb9
CUDA arena: a reuse whose address-map insertion fails keeps its range…
jayleaton Oct 17, 2026
46161ac
Merge stack/02-graph-cache-arena (the #635 arena fix) into stack/03-t…
jayleaton Oct 17, 2026
78ce706
Merge stack/03-tp-fabric (the #635 arena and #637 prefix fixes) into …
jayleaton Oct 17, 2026
9c5bd60
Merge stack/04-sessions (the #635 arena and #637 prefix fixes) into s…
jayleaton Oct 17, 2026
eaa7953
Merge stack/07-server (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
4370f91
Merge stack/08a-config (the #635 arena and #637 prefix fixes) into st…
jayleaton Oct 17, 2026
a0f9598
Merge stack/08b-engram (the #635 arena and #637 prefix fixes) into st…
jayleaton Oct 17, 2026
98a1695
Merge stack/08c-kernels (the #635 arena and #637 prefix fixes) into s…
jayleaton Oct 17, 2026
28e33cc
Merge stack/08e-block (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
f5a0b8b
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
3b7681f
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
334ce65
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
88418d8
test(tp): reject malformed runner commands before configuration
cwschroeder Oct 17, 2026
4c69b0d
fix(tp): parse runner commands before opening sessions
cwschroeder Oct 17, 2026
4df28ce
test(tp): retain verbs load failures in HCA diagnostics
cwschroeder Oct 17, 2026
0a80da0
fix(tp): distinguish verbs load errors from absent HCA ports
cwschroeder Oct 17, 2026
999cb03
Merge stack/03-tp-fabric (adopted #636 runner CLI validation and HCA …
jayleaton Oct 18, 2026
5f14a64
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
749240a
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
7122563
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
6390cf1
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
829c4c9
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
b81382d
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
2be028e
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
e47ed0e
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
e548a60
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
cdde1a4
Merge ashhart/main (1.1.0, 76aafcf2) into stack/02-graph-cache-arena
jayleaton Oct 18, 2026
adbdb8a
CUDA launches: a not-ready launch returns NotReady unlogged, as Drive…
jayleaton Oct 18, 2026
3bd6a6a
Merge stack/02-graph-cache-arena (adbdb8ab, main 76aafcf2) into stack…
jayleaton Oct 18, 2026
54141a1
Merge stack/03-tp-fabric (3bd6a6ab, main 76aafcf2) into stack/04-sess…
jayleaton Oct 18, 2026
80d4ed5
Merge stack/04-sessions (54141a1e, main 76aafcf2) into stack/07-server
jayleaton Oct 18, 2026
aebceae
Merge stack/07-server (80d4ed5c, main 76aafcf2) into stack/08a-config
jayleaton Oct 18, 2026
1003f08
Build: DeepSeek-V4.1's host tests on hosts other than Windows, whose …
jayleaton Oct 18, 2026
aa96782
Merge stack/08a-config (1003f08f, main 76aafcf2) into stack/08b-engram
jayleaton Oct 18, 2026
3f758dc
Merge stack/08b-engram (aa967828, main 76aafcf2) into stack/08c-kernels
jayleaton Oct 18, 2026
ea54d42
Merge stack/08c-kernels (3f758dcf, main 76aafcf2) into stack/08e-block
jayleaton Oct 18, 2026
d6190dc
Merge stack/08e-block (ea54d42d, main 76aafcf2) into stack/08r-runtime
jayleaton Oct 18, 2026
6742872
Merge stack/08r-runtime (d6190dcf, main 76aafcf2) into stack/09a-kv
jayleaton Oct 18, 2026
a88da5d
Build: DeepSeek-V4.1's KV host tests also skip Windows, like the fami…
jayleaton Oct 18, 2026
696e58a
Merge stack/09a-kv (a88da5de, main 76aafcf2) into stack/09b-forward
jayleaton Oct 18, 2026
4ac0c37
Merge stack/09b-forward (696e58a7, main 76aafcf2) into stack/10-prefill
jayleaton Oct 18, 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
DeepSeek-V4.1 kernel prototypes: attention merge and phase timing, x3…
…gm ablations, glue and MMA rate benches.

zig/kernels/cuda/deepseek_v41/proto keeps the standalone benches the kernels were tuned with: attention with a
latency-hiding merge and with phase timestamps, x3gm timing ablations and per-rank-shape benches, the decode glue
at 1-16 rows, mma.sync issue rates, a router glue oracle, a smaller-GPU emulation header (blocker CTAs on the
other SMs) and an nsys export summary. None is part of the build.
  • Loading branch information
jayleaton committed Oct 11, 2026
commit a7674d2489fdd657e79dfbe3185f176cc6be8df1
499 changes: 499 additions & 0 deletions zig/kernels/cuda/deepseek_v41/proto/attn_fastmerge.cuh

Large diffs are not rendered by default.

489 changes: 489 additions & 0 deletions zig/kernels/cuda/deepseek_v41/proto/attn_phases.cuh

Large diffs are not rendered by default.

132 changes: 132 additions & 0 deletions zig/kernels/cuda/deepseek_v41/proto/gb10emu.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Emulates a 48-SM, bandwidth-limited GB10-class part on a bigger sm_120 GPU: blocker CTAs park or hog the other SMs.

// Call gb10::eager() before any CUDA call: with lazy loading a first launch waits on the parked blockers (deadlock).
#pragma once
#include <cuda_runtime.h>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <vector>

#define CK(x) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { \
fprintf(stderr, "%s:%d %s: %s\n", __FILE__, __LINE__, #x, cudaGetErrorString(e_)); exit(1); } } while (0)

namespace gb10 {

constexpr int FREE = 48; // the GB10's SMs
constexpr int BLOCK_SMEM = 99 * 1024 - 64; // one blocker an SM (sm_120: 99 KB a CTA with the static flag)

__device__ __forceinline__ unsigned smid() { unsigned s; asm volatile("mov.u32 %0, %%smid;" : "=r"(s)); return s; }

// hogs: the first `hogs` blocked SMs stream `buf` (16-byte loads, 512 threads); the others spin
__global__ void __launch_bounds__(512) blocker(volatile int* release, int* ready, const int4* __restrict__ buf,
size_t n16, int hogs, unsigned long long* sink) {
extern __shared__ int4 pad[];
const unsigned s = smid();
if (s < FREE) return;
if (threadIdx.x == 0) atomicAdd(ready, 1);
const bool hog = (int)(s - FREE) < hogs;
int4 acc = make_int4(0, 0, 0, 0);
size_t i = ((size_t)(s - FREE) * 512 + threadIdx.x) * 8;
const size_t stride = (size_t)hogs * 512 * 8;
__shared__ int stop;
for (;;) { // one thread polls the host flag, every 64 sweeps
if (threadIdx.x == 0) stop = *release;
__syncthreads();
if (stop) break;
__syncthreads();
for (int k = 0; k < 64; ++k) {
if (hog) {
#pragma unroll
for (int u = 0; u < 8; ++u) {
const int4 v = __ldcs(buf + ((i + u) & (n16 - 1))); // n16: a power of two
acc.x ^= v.x; acc.y ^= v.y; acc.z ^= v.z; acc.w ^= v.w;
}
i = (i + stride) & (n16 - 1);
} else {
__nanosleep(2000);
}
}
}
if (acc.x == 0x7fffffff) sink[0] = (unsigned long long)(acc.y ^ acc.z ^ acc.w ^ pad[0].x);
}

// a plain stream read of n16 16-byte words on the kernel's SMs: the bandwidth the kernel under test sees
__global__ void __launch_bounds__(512) stream_read(const int4* __restrict__ p, size_t n16, unsigned long long* sink) {
int4 acc = make_int4(0, 0, 0, 0);
for (size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x; i < n16; i += (size_t)gridDim.x * blockDim.x) {
const int4 v = __ldcs(p + i);
acc.x ^= v.x; acc.y ^= v.y; acc.z ^= v.z; acc.w ^= v.w;
}
if (acc.x == 0x7fffffff) sink[1] = (unsigned long long)(acc.y ^ acc.z ^ acc.w);
}

inline void eager() { setenv("CUDA_MODULE_LOADING", "EAGER", 1); }

enum class Mode { Full, Sms, Gb10 };

// begin() parks the blockers, end() releases them; wrap a timed region: emu.begin(); time(kernel); emu.end();
struct Emu {
Mode mode = Mode::Full;
int sms = 0, hogs = 0;
cudaStream_t side{};
int* release_h = nullptr; int* release_d = nullptr; int* ready = nullptr;
int4* buf = nullptr; size_t n16 = 0; unsigned long long* sink = nullptr;

void init(Mode m, size_t hog_bytes = (size_t)4 << 30) {
mode = m;
cudaDeviceProp p{}; CK(cudaGetDeviceProperties(&p, 0)); sms = p.multiProcessorCount;
CK(cudaStreamCreateWithFlags(&side, cudaStreamNonBlocking));
CK(cudaHostAlloc(&release_h, sizeof(int), cudaHostAllocMapped));
CK(cudaHostGetDevicePointer(&release_d, release_h, 0));
CK(cudaMalloc(&ready, sizeof(int)));
CK(cudaMalloc(&sink, 16));
if (m == Mode::Gb10) {
n16 = hog_bytes / 16; CK(cudaMalloc(&buf, n16 * 16)); CK(cudaMemset(buf, 1, n16 * 16));
}
CK(cudaFuncSetAttribute(blocker, cudaFuncAttributeMaxDynamicSharedMemorySize, BLOCK_SMEM));
}
int grid_sms() const { return mode == Mode::Full ? sms : FREE; } // what the kernel under test may size for
void begin() {
if (mode == Mode::Full) return;
*release_h = 0; CK(cudaMemset(ready, 0, sizeof(int))); CK(cudaDeviceSynchronize());
blocker<<<sms, 512, BLOCK_SMEM, side>>>(release_h ? release_d : nullptr, ready, buf, n16,
mode == Mode::Gb10 ? hogs : 0, sink);
CK(cudaGetLastError());
int r = 0;
for (int spin = 0; r < sms - FREE && spin < 200000; ++spin) CK(cudaMemcpy(&r, ready, 4, cudaMemcpyDeviceToHost));
if (r < sms - FREE) { fprintf(stderr, "gb10emu: only %d of %d blockers resident\n", r, sms - FREE); exit(1); }
}
void end() {
if (mode == Mode::Full) return;
*release_h = 1; __sync_synchronize(); CK(cudaDeviceSynchronize());
}
// GB/s of a plain 16-byte stream read of `bytes` on the kernel's SMs (inside begin / end)
double stream_gbps(const void* p, size_t bytes, cudaStream_t st) {
cudaEvent_t a, b; CK(cudaEventCreate(&a)); CK(cudaEventCreate(&b));
const int grid = grid_sms() * 4;
stream_read<<<grid, 512, 0, st>>>((const int4*)p, bytes / 16, sink);
CK(cudaEventRecord(a, st));
stream_read<<<grid, 512, 0, st>>>((const int4*)p, bytes / 16, sink);
CK(cudaEventRecord(b, st)); CK(cudaEventSynchronize(b));
float ms = 0; CK(cudaEventElapsedTime(&ms, a, b));
CK(cudaEventDestroy(a)); CK(cudaEventDestroy(b));
return bytes / (ms * 1e6);
}
// Gb10 mode: the hog count that leaves the kernel's SMs closest to `target` GB/s of stream read
void calibrate(const void* p, size_t bytes, cudaStream_t st, double target) {
if (mode != Mode::Gb10) return;
int best = 0; double bestd = 1e30;
for (int h = 0; h <= sms - FREE; h += 4) {
hogs = h; begin(); const double g = stream_gbps(p, bytes, st); end();
printf(" calib hogs %3d -> stream %.1f GB/s\n", h, g);
if (fabs(g - target) < bestd) { bestd = fabs(g - target); best = h; }
if (g < target * 0.9) break;
}
hogs = best;
begin(); const double g = stream_gbps(p, bytes, st); end();
printf("gb10emu: %d hog SMs, the 48 SMs stream %.1f GB/s (target %.0f)\n", hogs, g, target);
}
};

} // namespace gb10
186 changes: 186 additions & 0 deletions zig/kernels/cuda/deepseek_v41/proto/glue_bench.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Latency-bound decode kernels (CSA2 attention core, mHC boundary) at 1-16 rows on 48 emulated SMs, cold L2 a launch.
#include "gb10emu.cuh"
#include "../attn_cuda.cu"
#include "../mhc_cuda.cu"
#include "attn_phases.cuh"
#include "attn_fastmerge.cuh"
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>

namespace {

__global__ void fill_bytes(uint8_t* p, size_t n, uint32_t seed, uint8_t mask) {
for (size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t)gridDim.x * blockDim.x) {
uint32_t x = (uint32_t)i * 0x9E3779B1u ^ seed;
x ^= x >> 16; x *= 0x7feb352du; x ^= x >> 15;
p[i] = (uint8_t)(x & mask);
}
}
__global__ void fill_f32(float* p, size_t n, uint32_t seed, float lo, float hi) {
for (size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t)gridDim.x * blockDim.x) {
uint32_t x = (uint32_t)i * 0x9E3779B1u ^ seed;
x ^= x >> 16; x *= 0x7feb352du; x ^= x >> 15;
p[i] = lo + (hi - lo) * (x & 0xffffff) / 16777216.f;
}
}
__global__ void flush(int4* p, size_t n) {
for (size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x; i < n; i += (size_t)gridDim.x * blockDim.x)
p[i] = make_int4((int)i, 0, 0, 0);
}

template <typename T> T* dalloc(size_t n) { T* p; CK(cudaMalloc(&p, n * sizeof(T) + 256)); CK(cudaMemset(p, 0, n * sizeof(T) + 256)); return p; }
uint8_t* rbytes(size_t n, uint32_t seed, uint8_t mask) { auto* p = dalloc<uint8_t>(n); fill_bytes<<<512, 256>>>(p, n, seed, mask); return p; }
float* rf32(size_t n, uint32_t seed, float lo, float hi) { auto* p = dalloc<float>(n); fill_f32<<<512, 256>>>(p, n, seed, lo, hi); return p; }

// times fn() `reps` times, each after a cold-L2 write; returns the median in us
template <typename F>
double time_us(gb10::Emu& emu, cudaStream_t st, int reps, int4* fl, size_t fln, F fn) {
cudaEvent_t a, b; CK(cudaEventCreate(&a)); CK(cudaEventCreate(&b));
std::vector<double> v;
fn(); // warm (first-launch attributes)
CK(cudaStreamSynchronize(st));
emu.begin();
for (int i = 0; i < reps; ++i) {
flush<<<emu.grid_sms() * 4, 512, 0, st>>>(fl, fln);
CK(cudaEventRecord(a, st));
fn();
CK(cudaEventRecord(b, st));
CK(cudaEventSynchronize(b));
float ms; CK(cudaEventElapsedTime(&ms, a, b));
v.push_back(ms * 1e3);
}
emu.end();
CK(cudaGetLastError());
std::sort(v.begin(), v.end());
return v[v.size() / 2];
}

} // namespace

int main(int argc, char** argv) { // glue_bench [--mode full|sms] [--reps 40]
gb10::eager();
std::string mode = "sms"; int reps = 40;
for (int i = 1; i + 1 < argc; i += 2) {
const std::string k = argv[i];
if (k == "--mode") mode = argv[i + 1]; else if (k == "--reps") reps = atoi(argv[i + 1]);
}
gb10::Emu emu; emu.init(mode == "full" ? gb10::Mode::Full : gb10::Mode::Sms);
cudaStream_t st; CK(cudaStreamCreateWithFlags(&st, cudaStreamNonBlocking));
const size_t fln = (256u << 20) / 16; int4* fl = dalloc<int4>(fln);
constexpr int RMAX = 16, H = 32, NCOMP_ROWS = 16384, RING = 128, POS = 32768;
using namespace dsv41_attn;
// ---- attention: 512 selected compressed rows + the 128-row window a row, 32 heads (one rank) ----
uint8_t* cv = rbytes((size_t)NCOMP_ROWS * VB, 1, 0x3b);
uint8_t* csc = dalloc<uint8_t>((size_t)NCOMP_ROWS * SB); CK(cudaMemset(csc, 127, (size_t)NCOMP_ROWS * SB));
uint8_t* sv = rbytes((size_t)RMAX * RING * VB, 2, 0x3b);
uint8_t* ssc = dalloc<uint8_t>((size_t)RMAX * RING * SB); CK(cudaMemset(ssc, 127, (size_t)RMAX * RING * SB));
std::vector<int> tok((size_t)RMAX * 512), cnt(RMAX, 512), lo(RMAX, 0);
for (size_t i = 0; i < tok.size(); ++i) tok[i] = (int)((i * 2654435761u) % NCOMP_ROWS);
int *dtok = dalloc<int>(tok.size()), *dcnt = dalloc<int>(RMAX), *dlo = dalloc<int>(RMAX), *dpos = dalloc<int>(1);
CK(cudaMemcpy(dtok, tok.data(), tok.size() * 4, cudaMemcpyHostToDevice));
CK(cudaMemcpy(dcnt, cnt.data(), RMAX * 4, cudaMemcpyHostToDevice));
CK(cudaMemcpy(dlo, lo.data(), RMAX * 4, cudaMemcpyHostToDevice));
const int pos = POS; CK(cudaMemcpy(dpos, &pos, 4, cudaMemcpyHostToDevice));
uint8_t* q = rbytes((size_t)RMAX * H * D * 2, 3, 0x3b);
float* cs = rf32((size_t)(POS + RMAX + 8) * 64, 4, -1.f, 1.f);
float* sink = rf32(H, 5, -1.f, 1.f);
float* po = dalloc<float>((size_t)NCH * RMAX * H * D);
float *pm = dalloc<float>((size_t)NCH * RMAX * H), *pl = dalloc<float>((size_t)NCH * RMAX * H);
uint16_t* out = dalloc<uint16_t>((size_t)RMAX * H * D);
int* ticket = dalloc<int>(RMAX * H / BMQ);
// ---- mHC boundary (mode 0: post of 2 ranks' partials, collapse, 24 mixes, finish, norm) ----
constexpr int DM = dsv41_mhc::D, NBM = dsv41_mhc::NB;
uint8_t* x = rbytes((size_t)RMAX * 4 * DM * 2, 6, 0x3b);
uint16_t* xout = dalloc<uint16_t>((size_t)RMAX * 4 * DM);
uint8_t* gp = rbytes((size_t)2 * RMAX * DM * 2, 7, 0x3b);
float *post = rf32(RMAX * 4, 8, 0.1f, 1.f), *comb = rf32(RMAX * 16, 9, 0.f, 0.5f), *pre = rf32(RMAX * 4, 10, 0.f, 1.f);
uint8_t* fn = rbytes((size_t)24 * 4 * DM * 2, 11, 0x3b);
float *base = rf32(24, 12, -0.1f, 0.1f), *scale = rf32(3, 13, 0.5f, 1.f);
float* part = dalloc<float>((size_t)RMAX * 4 * NBM * 32);
uint16_t *cc = dalloc<uint16_t>((size_t)RMAX * DM), *mout = dalloc<uint16_t>((size_t)RMAX * DM);
uint8_t* nw = rbytes((size_t)DM * 2, 14, 0x3b);
float *opre = dalloc<float>(RMAX * 4), *opost = dalloc<float>(RMAX * 4), *ocomb = dalloc<float>(RMAX * 16);
int* mcnt = dalloc<int>(1);
CK(cudaDeviceSynchronize());

// a CTA's time against its key count: chunk 0 holds `kc` selected keys (chunks 1-3 exit at once), the window kc
if (getenv("ATTN_KEYS")) {
for (int kc : {128, 64, 32, 16}) {
std::vector<int> c1(RMAX, kc), l1(RMAX);
for (int r = 0; r < RMAX; ++r) l1[r] = POS + r - (kc - 1);
CK(cudaMemcpy(dcnt, c1.data(), RMAX * 4, cudaMemcpyHostToDevice));
CK(cudaMemcpy(dlo, l1.data(), RMAX * 4, cudaMemcpyHostToDevice));
Args a{};
a.q = reinterpret_cast<const uint16_t*>(q); a.cv = cv; a.csc = csc; a.cvs = VB; a.css = SB;
a.tok = dtok; a.ts = 512; a.cnt = dcnt; a.sv = sv; a.ssc = ssc; a.svs = VB; a.sss = SB;
a.lo = dlo; a.pos = dpos; a.sink = sink; a.cs = cs; a.csst = 64; a.po = po; a.pm = pm; a.pl = pl;
a.out = out; a.ticket = ticket; a.R = 1; a.H = H; a.ring = RING; a.has_comp = 1;
const double t = time_us(emu, st, reps, fl, fln, [&] { CK(dispatch(a, 4, false, false, st)); });
printf("KEYS %3d a chunk (2 chunks x 2 head tiles working): %.1f us\n", kc, t);
}
CK(cudaMemcpy(dcnt, cnt.data(), RMAX * 4, cudaMemcpyHostToDevice));
CK(cudaMemcpy(dlo, lo.data(), RMAX * 4, cudaMemcpyHostToDevice));
}
if (getenv("ATTN_PHASES")) { // per-phase times of the R = 1 launch's CTAs
dsv41_attn_ph::Args a{};
a.q = reinterpret_cast<const uint16_t*>(q); a.cv = cv; a.csc = csc; a.cvs = VB; a.css = SB;
a.tok = dtok; a.ts = 512; a.cnt = dcnt; a.sv = sv; a.ssc = ssc; a.svs = VB; a.sss = SB;
a.lo = dlo; a.pos = dpos; a.sink = sink; a.cs = cs; a.csst = 64; a.po = po; a.pm = pm; a.pl = pl;
a.out = out; a.ticket = ticket; a.R = 1; a.H = H; a.ring = RING; a.has_comp = 1;
const double t = time_us(emu, st, reps, fl, fln, [&] { CK(dsv41_attn_ph::dispatch(a, 4, false, false, st)); });
unsigned long long hs[256][10];
CK(cudaMemcpyFromSymbol(hs, dsv41_attn_ph::g_stamp, sizeof(hs)));
unsigned long long t0 = ~0ull;
for (int i = 0; i < 10; ++i) t0 = std::min(t0, hs[i][0]);
printf("PHASES launch %.1f us; per CTA (us from the first CTA's start): start rows+q keys S softmax PV ticket merge\n", t);
for (int i = 0; i < 10; ++i) {
printf("PHASES cta %d:", i);
for (int k = 0; k < 8; ++k) printf(" %6.2f", hs[i][k] ? (hs[i][k] - t0) / 1e3 : -1.0);
printf("\n");
}
}
{ // fast merge: same bits, time at 1-16 rows
std::vector<uint16_t> ref((size_t)RMAX * H * D), got((size_t)RMAX * H * D);
for (int R : {1, 4, 16}) {
Args a{};
a.q = reinterpret_cast<const uint16_t*>(q); a.cv = cv; a.csc = csc; a.cvs = VB; a.css = SB;
a.tok = dtok; a.ts = 512; a.cnt = dcnt; a.sv = sv; a.ssc = ssc; a.svs = VB; a.sss = SB;
a.lo = dlo; a.pos = dpos; a.sink = sink; a.cs = cs; a.csst = 64; a.po = po; a.pm = pm; a.pl = pl;
a.out = out; a.ticket = ticket; a.R = R; a.H = H; a.ring = RING; a.has_comp = 1;
CK(cudaMemset(out, 0, (size_t)RMAX * H * D * 2));
const double t0 = time_us(emu, st, reps, fl, fln, [&] { CK(dispatch(a, 4, false, false, st)); });
CK(cudaMemcpy(ref.data(), out, ref.size() * 2, cudaMemcpyDeviceToHost));
dsv41_attn_fm::Args b{};
static_assert(sizeof(b) == sizeof(a), "same Args");
memcpy(&b, &a, sizeof(a));
CK(cudaMemset(out, 0, (size_t)RMAX * H * D * 2));
const double t1 = time_us(emu, st, reps, fl, fln, [&] { CK(dsv41_attn_fm::dispatch(b, 4, false, false, st)); });
CK(cudaMemcpy(got.data(), out, got.size() * 2, cudaMemcpyDeviceToHost));
size_t diff = 0, nz = 0;
for (size_t i = 0; i < (size_t)R * H * D; ++i) { diff += ref[i] != got[i]; nz += ref[i] != 0; }
printf("FASTMERGE R %2d: attn_cuda %.1f us, fast merge %.1f us, %zu of %zu outputs differ (%zu nonzero)\n",
R, t0, t1, diff, (size_t)R * H * D, nz);
}
}
printf("mode %s: per launch, median of %d cold launches (us)\n rows | attn_kernel | mhc boundary\n", mode.c_str(), reps);
for (int R : {1, 2, 4, 8, 16}) {
Args a{};
a.q = reinterpret_cast<const uint16_t*>(q); a.cv = cv; a.csc = csc; a.cvs = VB; a.css = SB;
a.tok = dtok; a.ts = 512; a.cnt = dcnt; a.sv = sv; a.ssc = ssc; a.svs = VB; a.sss = SB;
a.lo = dlo; a.hi = nullptr; a.pos = dpos; a.sl = nullptr; a.pt = nullptr; a.pts = 0; a.psh = 0;
a.sink = sink; a.cs = cs; a.csst = 64; a.po = po; a.pm = pm; a.pl = pl; a.out = out; a.ticket = ticket;
a.R = R; a.H = H; a.ring = RING; a.has_comp = 1;
const double ta = time_us(emu, st, reps, fl, fln, [&] { CK(dispatch(a, 4, false, false, st)); });
dsv41_mhc::Args m{};
m.x = reinterpret_cast<const uint16_t*>(x); m.xs = 4 * DM; m.xout = xout; m.g = gp; m.gr = (long long)R * DM;
m.world = 2; m.gbf16 = 1; m.post = post; m.comb = comb; m.pre = pre; m.fn = fn; m.base = base; m.scale = scale;
m.part = part; m.c = cc; m.tap = nullptr; m.ts = 0; m.nw = nw; m.nwbf16 = 1; m.out = mout;
m.opre = opre; m.opost = opost; m.ocomb = ocomb; m.cnt = mcnt; m.R = R;
m.eps = 1e-20f; m.hc_eps = 1e-6f; m.post_alpha = 2.f; m.iters = 20;
const double tm = time_us(emu, st, reps, fl, fln, [&] { CK(dsv41_mhc::dispatch(m, 0, false, st)); });
printf("RESULT %4d | %9.1f | %9.1f\n", R, ta, tm);
}
return 0;
}
Loading
Loading