Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
6636e69
cuda: a one-shot RoCE all-gather for two ranks (roce.zig, roce_gather…
BobClawblaw Oct 14, 2026
39de039
lane core and native CUDA host for two-rank families with a vision to…
BobClawblaw Oct 14, 2026
bdc47c8
Merge branch 'pr/roce-gather' into pr/glm-flash-cuda
BobClawblaw Oct 14, 2026
cbe08cf
GLM-5.3-Flash on two CUDA ranks: the native family (zig/src/families/…
BobClawblaw Oct 14, 2026
a76f16a
glm kernels: the tensor-core fragment header beside them (qmm_frag.cu…
BobClawblaw Oct 14, 2026
abb8bbe
glm: the three Flash Next kernels the family loads (experts, experts_…
BobClawblaw Oct 14, 2026
2903dd7
cuda abi: the driver entries the GLM family uses (module function enu…
BobClawblaw Oct 14, 2026
87ef757
tf-cuda-test glm-*: exit right after the pass or summary line (the te…
BobClawblaw Oct 14, 2026
a759ccd
Merge branch 'pr/roce-gather' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
db750c4
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
a2eefa2
glm cuda: zig fmt
BobClawblaw Oct 15, 2026
22d92e4
lane host: a warm pass that steps aside is not launched after its can…
BobClawblaw Oct 15, 2026
a76651a
glm cuda: comments one line each, files under 600 lines, the long hea…
BobClawblaw Oct 15, 2026
ef73ec3
tf_video: the includes back under its one-line header
BobClawblaw Oct 15, 2026
e0d3baa
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
0e50498
cuda: RoCE gather comments on one line each, the protocol notes in do…
BobClawblaw Oct 15, 2026
cc5d9d1
vision inputs: a server without --vision refuses image parts in the f…
BobClawblaw Oct 15, 2026
89c68c8
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
aaed024
qwen_image.formatSeconds: t * 10 rounded in integers, not f128, so te…
BobClawblaw Oct 15, 2026
2cc423f
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
9e6dfcd
lane host: a set-up job's kept entry stays pinned until its restore r…
BobClawblaw Oct 15, 2026
a8ea350
Merge remote-tracking branch 'fork/pr/lane-host-vision' into pr/glm-f…
BobClawblaw Oct 15, 2026
d069277
cuda: RoCE gather ranks agree one shard cap (each sends its max_bytes…
BobClawblaw Oct 15, 2026
22d2a30
Merge remote-tracking branch 'fork/pr/roce-gather' into pr/glm-flash-…
BobClawblaw Oct 15, 2026
b07b11b
glm cuda: the review's four fixes: a RoCE check after every sync that…
BobClawblaw Oct 15, 2026
52b82fc
prompt cache: an entry removed while pinned is dead to lookups and fr…
BobClawblaw Oct 15, 2026
468fa88
Merge branch 'pr/lane-host-vision' into pr/glm-flash-cuda
BobClawblaw Oct 15, 2026
c525ea9
glm cuda: the gather check and the agreed settings' doc comments on o…
BobClawblaw Oct 15, 2026
7c9d845
Merge remote-tracking branch 'origin/main' into pr/roce-gather
BobClawblaw Oct 16, 2026
b8d45de
cuda: the RoCE gather joins tp_group: its setup crosses the group's N…
BobClawblaw Oct 16, 2026
bb7e2a2
Merge origin/main (1.0.12) into pr/lane-host-vision: main's image pat…
BobClawblaw Oct 16, 2026
b416ec8
lane core: several queued prompts in one pass for a backend with pref…
BobClawblaw Oct 16, 2026
1ac97f6
Merge remote-tracking branch 'fork/pr/roce-gather' into tmp-855-base
BobClawblaw Oct 16, 2026
b982a3b
Merge #853 and #852 (both on main 1.0.12 with #804) into pr/glm-flash…
BobClawblaw Oct 16, 2026
aa9d4a7
glm cuda: the ranks' communicator is a tp_group.Group; on two ranks t…
BobClawblaw Oct 16, 2026
653bd37
glm cuda: EXL3 routed experts (Mia-AiLab's checkpoint: 4-bit mcg trel…
BobClawblaw Oct 16, 2026
8278074
kernel contract: glm_exl3's eight launches with their sm_80/86/89/120…
BobClawblaw Oct 16, 2026
084c253
glm cuda: the ranks watch each other (tp_watch): a rank that fails or…
BobClawblaw Oct 16, 2026
b090a7b
glm cuda: verify windows to the decode buffers' 8 rows, so the lane c…
BobClawblaw Oct 16, 2026
93f5261
glm cuda: the image tower (model.visual.*, rank 0's) on cuBLASLt and …
BobClawblaw Oct 16, 2026
f9d4f9f
glm cuda: images on two ranks (--vision): rank 0's tower encodes a pr…
BobClawblaw Oct 16, 2026
3cc4581
tools/glm_vision_ref.py: the torch reference tf-cuda-test glm-vision …
BobClawblaw Oct 16, 2026
edc2285
glm cuda: the bf16 linears on glm_mm (ours, from the glm_cuda family)…
BobClawblaw Oct 16, 2026
875b695
glm cuda: TF_GLM_PROF=1 also times prompt chunks by section (gate, fo…
BobClawblaw Oct 16, 2026
526ffd4
Merge the glm_cuda family's head (c525ea99): its work now lives in fa…
BobClawblaw Oct 16, 2026
999ae3b
glm: video on the CUDA family (main's planner shared as video_core, o…
BobClawblaw Oct 16, 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
glm: the three Flash Next kernels the family loads (experts, experts_…
…prefill, sample) and no Flash Next test commands
  • Loading branch information
BobClawblaw committed Oct 14, 2026
commit abb8bbe6efe73a411f75f555d2de590bbbe0dc32
4 changes: 4 additions & 0 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const kernels = [_]Kernel{
.{ .name = "qmmf", .flags = &.{"-O3"} }, // nvfp4/qmmf.cu's device code (FP8G, FP4), tensorfold_nvfp4_v3
.{ .name = "prompt16", .flags = &.{"-O3"} }, // nvfp4/prompt.cu's FP4 and FP8 tile 4, tensorfold_nvfp4_prompt_v1
.{ .name = "modelopt_pack", .flags = &.{"-O3"} }, // ours: ModelOpt tensors into the lane, prompt and expert layouts
// the routed experts' grouped kernels and the greedy candidates the GLM family shares with Flash Next (zig/kernels/cuda/flashnext)
.{ .name = "fn_experts", .src = "flashnext/experts", .flags = &.{"-O3"} },
.{ .name = "fn_experts_prefill", .src = "flashnext/experts_prefill", .flags = &.{"-O3"} },
.{ .name = "fn_sample", .src = "flashnext/sample", .flags = &.{ "-O3", "--fmad=false", "--ftz=false" } },
// GLM-5.3-Flash (glm5_next): the device copy of the Python engine's KDA extension (zig/tests/cuda/glm/gen_copies.py)
.{ .name = "glm_kda", .src = "glm/kda", .flags = &.{ "-O3", "--fmad=false" } }, // glm5_next/cuda/kda.py, tensorfold_glm_kda_v2
.{ .name = "glm_ops", .src = "glm/ops", .flags = &.{ "-O3", "--fmad=false" } }, // ours: the token lists, casts and positions torch ops make
Expand Down
294 changes: 294 additions & 0 deletions zig/kernels/cuda/flashnext/experts.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
// Device code of src/tensorfold/cuda/experts.cu (generated by zig/tests/cuda/flashnext/gen_copies.py; do not edit).
// Decode form: acc = fma(xs, b, fma(p, s, acc)) per group in order, one pair an mma row, so no pair affects another.

#include <cuda_bf16.h>
#include <stdint.h>

#include "experts.cuh"

namespace {

__device__ __forceinline__ uint4 ld_w(const uint4* p) {
uint4 r;
asm volatile("ld.global.nc.L1::no_allocate.v4.u32 {%0, %1, %2, %3}, [%4];\n"
: "=r"(r.x), "=r"(r.y), "=r"(r.z), "=r"(r.w)
: "l"(p));
return r;
}

__device__ __forceinline__ uint4 ld_x(const __nv_bfloat16* p) { return __ldg(reinterpret_cast<const uint4*>(p)); }

// a row's group sum: the lane's inputs in order, then the quad (every lane of the quad gets the same bits)
template <int XV>
__device__ __forceinline__ float group_sum(const uint4 (&v)[XV]) {
float s = 0.f;
#pragma unroll
for (int c = 0; c < XV; ++c)
#pragma unroll
for (int i = 0; i < 4; ++i) {
const uint32_t u = comp(v[c], i);
s += lo_f(u);
s += hi_f(u);
}
s += __shfl_xor_sync(0xffffffffu, s, 1);
s += __shfl_xor_sync(0xffffffffu, s, 2);
return s;
}

template <int GS, int M>
struct Stage {
uint4 w[M][Geo<GS>::WV];
uint4 sb[M][Geo<GS>::SBV];
uint4 xa[Geo<GS>::XV]; // row gq of the tile: the lane's inputs of the group
uint4 xb[Geo<GS>::XV]; // row gq + 8
};

template <int GS, int M>
__device__ __forceinline__ void load_stage(Stage<GS, M>& st, const uint4* blk, int g, int lane, int t,
const __nv_bfloat16* x0, const __nv_bfloat16* x1, bool v0, bool v1) {
using G = Geo<GS>;
const uint4* b = blk + (size_t)g * (M * G::BLOCK);
#pragma unroll
for (int m = 0; m < M; ++m) {
#pragma unroll
for (int c = 0; c < G::WV; ++c) st.w[m][c] = ld_w(b + m * G::BLOCK + c * 32 + lane);
#pragma unroll
for (int c = 0; c < G::SBV; ++c) st.sb[m][c] = ld_w(b + m * G::BLOCK + 32 * G::WV + t * G::SBV + c);
}
const uint4 zero = make_uint4(0u, 0u, 0u, 0u);
const int k0 = g * GS;
#pragma unroll
for (int c = 0; c < G::XV; ++c) {
st.xa[c] = v0 ? ld_x(x0 + k0 + 8 * c) : zero;
st.xb[c] = v1 ? ld_x(x1 + k0 + 8 * c) : zero;
}
}

template <int GS, int M>
__device__ __forceinline__ void compute_stage(float (&acc)[M][1][NTW][4], const Stage<GS, M>& st, bool hi) {
using G = Geo<GS>;
const float xsa = group_sum<G::XV>(st.xa);
const float xsb = group_sum<G::XV>(st.xb);
#pragma unroll
for (int m = 0; m < M; ++m) {
float p[NTW][4];
#pragma unroll
for (int j = 0; j < NTW; ++j) p[j][0] = p[j][1] = p[j][2] = p[j][3] = 0.f;
#pragma unroll
for (int ks = 0; ks < G::KS; ++ks) {
// k-step ks: the lane's inputs 4 ks .. 4 ks + 3 (pairs at the mma's k positions 2t and 2t + 8)
const uint32_t a0 = comp(st.xa[ks >> 1], 2 * (ks & 1));
const uint32_t a2 = comp(st.xa[ks >> 1], 2 * (ks & 1) + 1);
const uint32_t a1 = comp(st.xb[ks >> 1], 2 * (ks & 1));
const uint32_t a3 = comp(st.xb[ks >> 1], 2 * (ks & 1) + 1);
const int sh = (ks & 1) ? 8 : 0;
#pragma unroll
for (int j = 0; j < NTW; ++j) {
const int wi = j * (GS / 32) + (ks >> 1);
const uint32_t word = comp(st.w[m][wi >> 2], wi & 3);
mma(p[j], a0, a1, a2, a3, nib2(word, sh), nib2(word, sh + 4));
}
}
#pragma unroll
for (int j = 0; j < NTW; ++j) {
const uint32_t sp = comp(st.sb[m][j >> 2], j & 3);
const uint32_t bp = comp(st.sb[m][(NTW + j) >> 2], (NTW + j) & 3);
const float s0 = lo_f(sp), s1 = hi_f(sp), b0 = lo_f(bp), b1 = hi_f(bp);
float(&a)[4] = acc[m][0][j];
a[0] = fmaf(xsa, b0, fmaf(p[j][0], s0, a[0]));
a[1] = fmaf(xsa, b1, fmaf(p[j][1], s1, a[1]));
if (hi) {
a[2] = fmaf(xsb, b0, fmaf(p[j][2], s0, a[2]));
a[3] = fmaf(xsb, b1, fmaf(p[j][3], s1, a[3]));
}
}
}
}

template <int GS, int M, int D>
__device__ __forceinline__ void k_loop(float (&acc)[M][1][NTW][4], const uint4* blk, int KG, int lane, int t,
const __nv_bfloat16* x0, const __nv_bfloat16* x1, bool v0, bool v1) {
Stage<GS, M> st[D];
#pragma unroll
for (int d = 0; d < D; ++d)
if (d < KG) load_stage<GS, M>(st[d], blk, d, lane, t, x0, x1, v0, v1);
for (int g0 = 0; g0 < KG; g0 += D) {
#pragma unroll
for (int d = 0; d < D; ++d) {
const int g = g0 + d;
if (g < KG) {
compute_stage<GS, M>(acc, st[d], v1);
if (g + D < KG) load_stage<GS, M>(st[d], blk, g + D, lane, t, x0, x1, v0, v1);
}
}
}
}

// Pair p reads X row p / slots when slots > 0 (X holds tokens), else X row p (X holds a row a pair).
template <int GS, int M, int EPI, int D, int WARPS>
__global__ void __launch_bounds__(WARPS * 32)
expert_kernel(const __nv_bfloat16* __restrict__ X, int x_stride, int slots, const uint4* __restrict__ W, int KG,
int NB, const int* __restrict__ items, const int* __restrict__ counts,
const int* __restrict__ members, void* __restrict__ out, int N, float limit) {
const int lane = threadIdx.x & 31, gq = lane >> 2, t = lane & 3;
const int units = __ldg(counts) * NB;
for (int unit = blockIdx.x * WARPS + (threadIdx.x >> 5); unit < units; unit += gridDim.x * WARPS) {
const int it = unit / NB, cb = unit - it * NB;
const int e = __ldg(items + 3 * it), first = __ldg(items + 3 * it + 1), cnt = __ldg(items + 3 * it + 2);
const bool v0 = gq < cnt, v1 = gq + 8 < cnt;
const int pr0 = v0 ? __ldg(members + first + gq) : 0;
const int pr1 = v1 ? __ldg(members + first + gq + 8) : 0;
const int r0 = slots ? pr0 / slots : pr0, r1 = slots ? pr1 / slots : pr1;
const __nv_bfloat16* x0 = X + (size_t)r0 * x_stride + t * (GS / 4);
const __nv_bfloat16* x1 = X + (size_t)r1 * x_stride + t * (GS / 4);
const uint4* blk = W + ((size_t)e * NB + cb) * (size_t)KG * (M * Geo<GS>::BLOCK);
float acc[M][1][NTW][4];
#pragma unroll
for (int m = 0; m < M; ++m)
#pragma unroll
for (int j = 0; j < NTW; ++j) acc[m][0][j][0] = acc[m][0][j][1] = acc[m][0][j][2] = acc[m][0][j][3] = 0.f;
k_loop<GS, M, D>(acc, blk, KG, lane, t, x0, x1, v0, v1);
epilogue<EPI, M, 1>(acc, 0, out, N, cb * COLS + 2 * t, pr0, pr1, v0, v1, limit);
}
}

constexpr int PLAN_THREADS = 1024;
constexpr int EMAX = 1024;
constexpr int PSMALL = 1024; // pairs the one-block plan takes; wider calls rank in blocks of PLAN_THREADS

// Whole block: thread e gives expert e's pair count, gets its first member; items of <= T pairs land in expert order.
__device__ int place_items(int c, int E, int T, int* __restrict__ items, int* __restrict__ counts) {
__shared__ int wsum[3][32];
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
const int tiles = (c + T - 1) / T;
int a = c, b = tiles, u = c > 0;
#pragma unroll
for (int o = 1; o < 32; o <<= 1) {
const int ua = __shfl_up_sync(0xffffffffu, a, o), ub = __shfl_up_sync(0xffffffffu, b, o);
const int uu = __shfl_up_sync(0xffffffffu, u, o);
if (lane >= o) {
a += ua;
b += ub;
u += uu;
}
}
if (lane == 31) {
wsum[0][warp] = a;
wsum[1][warp] = b;
wsum[2][warp] = u;
}
__syncthreads();
if (warp == 0) {
#pragma unroll
for (int s = 0; s < 3; ++s) {
int v = wsum[s][lane];
#pragma unroll
for (int o = 1; o < 32; o <<= 1) {
const int w = __shfl_up_sync(0xffffffffu, v, o);
if (lane >= o) v += w;
}
wsum[s][lane] = v;
}
}
__syncthreads();
const int off = (warp ? wsum[0][warp - 1] : 0) + a - c;
const int ioff = (warp ? wsum[1][warp - 1] : 0) + b - tiles;
if (tid == 0) {
counts[0] = wsum[1][31];
counts[1] = wsum[2][31];
}
if (tid < E)
for (int j = 0; j < tiles; ++j) {
int* it = items + 3 * (ioff + j);
it[0] = tid;
it[1] = off + T * j;
it[2] = min(T, c - T * j);
}
return off;
}

// One block: pairs p = row * slots + slot grouped by expert (pair order within an expert).
__global__ void __launch_bounds__(PLAN_THREADS)
plan_kernel(const int* __restrict__ picks, int P, int E, int T, int* __restrict__ members,
int* __restrict__ items, int* __restrict__ counts) {
__shared__ int pk[PSMALL];
__shared__ int cnt[EMAX];
__shared__ int off[EMAX];
const int tid = threadIdx.x;
for (int e = tid; e < EMAX; e += PLAN_THREADS) cnt[e] = 0;
__syncthreads();
for (int p = tid; p < P; p += PLAN_THREADS) {
const int e = picks[p];
pk[p] = e;
if (e < E) atomicAdd(&cnt[e], 1); // a pick past the table (e >= E) is left out of the plan
}
__syncthreads();
const int o = place_items(tid < E ? cnt[tid] : 0, E, T, items, counts);
if (tid < E) off[tid] = o;
__syncthreads();
for (int p = tid; p < P; p += PLAN_THREADS) {
const int e = pk[p];
if (e >= E) continue;
int rank = 0;
for (int q = 0; q < p; ++q) rank += pk[q] == e;
members[off[e] + rank] = p;
}
}

// Wide calls, pass 1: block b ranks its pairs within each expert in pair order and writes its counts to hist[b][e].
__global__ void __launch_bounds__(PLAN_THREADS)
plan_rank(const int* __restrict__ picks, int P, int E, int* __restrict__ rank, int* __restrict__ hist) {
__shared__ int cnt[EMAX];
const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5;
for (int e = tid; e < E; e += PLAN_THREADS) cnt[e] = 0;
__syncthreads();
const int p = blockIdx.x * PLAN_THREADS + tid;
const bool ok = p < P && picks[p] < E; // a pick past the table is left out of the plan
const int e = ok ? picks[p] : -1 - lane;
const unsigned same = __match_any_sync(0xffffffffu, e);
const int below = __popc(same & ((1u << lane) - 1u));
for (int w = 0; w < PLAN_THREADS / 32; ++w) {
if (warp == w) {
const int base = ok ? cnt[e] : 0;
__syncwarp();
if (ok && below == 0) cnt[e] = base + __popc(same);
if (ok) rank[p] = base + below;
}
__syncthreads();
}
for (int x = tid; x < E; x += PLAN_THREADS) hist[(size_t)blockIdx.x * E + x] = cnt[x];
}

// Pass 2, one block: each block's first member for each expert in place of its count, and the items.
__global__ void __launch_bounds__(PLAN_THREADS)
plan_offsets(int nblk, int E, int T, int* __restrict__ hist, int* __restrict__ items, int* __restrict__ counts) {
const int tid = threadIdx.x;
int c = 0;
if (tid < E)
for (int b = 0; b < nblk; ++b) {
int* h = hist + (size_t)b * E + tid;
const int v = *h;
*h = c;
c += v;
}
const int off = place_items(c, E, T, items, counts);
if (tid < E)
for (int b = 0; b < nblk; ++b) hist[(size_t)b * E + tid] += off;
}

// Pass 3: every pair to its place.
__global__ void plan_scatter(const int* __restrict__ picks, int P, int E, const int* __restrict__ rank,
const int* __restrict__ hist, int* __restrict__ members) {
const int p = blockIdx.x * blockDim.x + threadIdx.x;
if (p < P && picks[p] < E) members[hist[(size_t)(p / PLAN_THREADS) * E + picks[p]] + rank[p]] = p;
}
} // namespace

// The specializations the Python build instantiated.
namespace {
template __global__ void expert_kernel<(int)32, (int)1, (int)0, (int)2, (int)4>(const __nv_bfloat16 *, int, int, const uint4 *, int, int, const int *, const int *, const int *, void *, int, float);
template __global__ void expert_kernel<(int)32, (int)2, (int)2, (int)2, (int)4>(const __nv_bfloat16 *, int, int, const uint4 *, int, int, const int *, const int *, const int *, void *, int, float);
template __global__ void expert_kernel<(int)64, (int)1, (int)0, (int)2, (int)4>(const __nv_bfloat16 *, int, int, const uint4 *, int, int, const int *, const int *, const int *, void *, int, float);
template __global__ void expert_kernel<(int)64, (int)1, (int)1, (int)2, (int)4>(const __nv_bfloat16 *, int, int, const uint4 *, int, int, const int *, const int *, const int *, void *, int, float);
template __global__ void expert_kernel<(int)64, (int)2, (int)2, (int)2, (int)4>(const __nv_bfloat16 *, int, int, const uint4 *, int, int, const int *, const int *, const int *, void *, int, float);
} // namespace
Loading