Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 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
f24a651
glm cuda: DFlash2 drafts on families/glm (--drafter): taps after laye…
BobClawblaw Oct 16, 2026
9f638dc
glm cuda: DFlash2 drafts sized by their chances (the selector's, chai…
BobClawblaw Oct 17, 2026
70faca3
glm cuda: DFlash2's linears at 4 bits by default (--drafter-bits 4|0)…
BobClawblaw 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
glm cuda: EXL3 routed experts (Mia-AiLab's checkpoint: 4-bit mcg trel…
…lis experts, the rest bf16): admitted beside ModelOpt NVFP4, loaded by the rank's 128-column cut, run by glm_exl3 (grouped decode, fused prompt) on an 8-slot plan into the MoE block's fp32 slot rows; bf16 dense layers through b16; the SwiGLU limits keep a NaN; tf-cuda-test glm-exl3 against float64 from core's EXL3 decoder
  • Loading branch information
BobClawblaw committed Oct 16, 2026
commit 653bd37f53122ba82372bb129c43e9149a76525e
1 change: 1 addition & 0 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const kernels = [_]Kernel{
.{ .name = "glm_glue", .flags = &.{"-O3"} }, // ours: the Python family's Triton glue by hand, its PTX op for op
.{ .name = "glm_ops", .flags = &.{"-O3"} }, // ours: GLM's integer work the Python family did in torch (the DSA token lists)
.{ .name = "glm_quant", .flags = torch_ops }, // ours: GLM's draft-only weight quantizers, torch at load in the Python family
.{ .name = "glm_exl3", .flags = &.{"-O3"} }, // ours: GLM's EXL3 routed experts (Mia-AiLab's checkpoint), decode and prompt
.{ .name = "torch_argmax", .src = "torch_ops/argmax", .flags = torch_ops },
.{ .name = "torch_topk", .src = "torch_ops/topk", .flags = torch_ops },
.{ .name = "torch_pointwise", .src = "torch_ops/pointwise", .flags = torch_ops },
Expand Down
363 changes: 363 additions & 0 deletions zig/kernels/cuda/glm_exl3.cu

Large diffs are not rendered by default.

168 changes: 168 additions & 0 deletions zig/kernels/cuda/glm_exl3_transposed.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Transposed prompt helpers after MiaAI-Lab (GLM-5.3-Flash-EXL3-2x-DGX-Sparks-TensorFold 0004, 0009, Apache-2.0).
#pragma once
// Included by exl3.cu after its shared helpers (mcg decode, cp.async, ldmatrix), in the same translation unit.

namespace {

constexpr int TPM = 64, TNW = 8, TMT = 16 / TNW, TNB = TPM / 8, TT = TNW * 32, TPH = TPM / 2;
constexpr int TBK = 32, TKT = TBK / 16, TSTAGES = 3, TXS = TBK + 8; // a stage: 32 k values a row; +16 B: ldmatrix rows on distinct banks
constexpr int TWORDS = 16 * TKT * 32; // a stage's trellis words (16 tiles x 2 k tiles x 32)
constexpr int GU_ES = 128 + 4, DN_ES = 256 + 4; // the epilogue sums' row strides (floats)
template <int XR>
constexpr int tstage_halves() { return XR * TXS + 2 * TWORDS; }
constexpr int tsmem_bytes() {
const int gu = TSTAGES * tstage_halves<2 * TPM>() * 2; // gate and up rows apart (no shared rotation)
const int ep = 2 * TPH * GU_ES * 4, dn = TPH * DN_ES * 4;
return gu > ep ? (gu > dn ? gu : dn) : (ep > dn ? ep : dn);
}

// Which (matrix, weight tile) warp-tile q (0..15) of column block cb is.
template <int MATS>
__device__ __forceinline__ void ttile(int q, int cb, int& mat, int& nt) {
mat = MATS == 2 ? q >> 3 : 0;
nt = MATS == 2 ? cb * 8 + (q & 7) : cb * 16 + q;
}

// Main loop: acc[i][nb] = warp tile i x members nb * 8 .. + 7 over K; XR rows a stage (TPM if X0 == X1, else 2 TPM).
template <int MATS, int XR>
__device__ __forceinline__ void tmain(const half* __restrict__ X0, const half* __restrict__ X1, const uint32_t* __restrict__ T0,
const uint32_t* __restrict__ T1, const int* rows_sh, int cnt, int e, int K, int NTILES, int cb, int xshift,
half* stage, float (&acc)[TMT][TNB][4]) {
constexpr int SH = tstage_halves<XR>();
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
int mat, nt_unused;
ttile<MATS>(warp * TMT, cb, mat, nt_unused);
const int KT = K >> 4, chunks = K / TBK;
const int nblocks = (cnt + 15) >> 4; // 16-member blocks in use
#pragma unroll
for (int i = 0; i < TMT; ++i)
#pragma unroll
for (int j = 0; j < TNB; ++j)
#pragma unroll
for (int c = 0; c < 4; ++c) acc[i][j][c] = 0.f;
auto load = [&](int chunk) {
half* dst = stage + (size_t)(chunk % TSTAGES) * SH;
const int k0 = chunk * TBK;
for (int idx = threadIdx.x; idx < XR * (TBK / 8); idx += TT) {
const int m = idx / (TPM * (TBK / 8)), rem = idx % (TPM * (TBK / 8)), i = rem / (TBK / 8), j = rem % (TBK / 8);
if (i >= nblocks * 16) continue; // a 16-member block without a member: never read
const int r = rows_sh[i];
const half* src = (m ? X1 : X0) + (size_t)(r < 0 ? 0 : (r >> xshift)) * K + k0 + j * 8;
cp_async16(dst + ((size_t)m * TPM + i) * TXS + j * 8, src, r >= 0);
}
uint32_t* wdst = reinterpret_cast<uint32_t*>(dst + (size_t)XR * TXS);
for (int idx = threadIdx.x; idx < 16 * TKT * 8; idx += TT) { // 16-byte pieces of the 128-byte tile words
const int q = idx / (TKT * 8), kk = (idx / 8) % TKT, part = idx % 8;
int wm, nt;
ttile<MATS>(q, cb, wm, nt);
const uint32_t* src = (wm ? T1 : T0) + (((size_t)e * KT + chunk * TKT + kk) * NTILES + nt) * 32 + part * 4;
cp_async16(wdst + ((size_t)q * TKT + kk) * 32 + part * 4, src, true);
}
};
#pragma unroll
for (int c = 0; c < TSTAGES - 1; ++c) {
if (c < chunks) load(c);
cp_async_commit();
}
for (int c = 0; c < chunks; ++c) {
cp_async_wait<TSTAGES - 2>();
__syncthreads();
if (c + TSTAGES - 1 < chunks) load(c + TSTAGES - 1);
cp_async_commit();
const half* base = stage + (size_t)(c % TSTAGES) * SH;
const half* xs = base + (size_t)(XR == TPM ? 0 : mat) * TPM * TXS;
const uint32_t* ws = reinterpret_cast<const uint32_t*>(base + (size_t)XR * TXS);
#pragma unroll
for (int kk = 0; kk < TKT; ++kk) {
uint32_t a[TMT][4];
#pragma unroll
for (int i = 0; i < TMT; ++i) {
uint32_t b0[2], b1[2];
decode_tile_p(ws[((size_t)(warp * TMT + i) * TKT + kk) * 32 + lane], lane, b0, b1);
a[i][0] = b0[0]; a[i][1] = b1[0]; a[i][2] = b0[1]; a[i][3] = b1[1];
}
#pragma unroll
for (int jb = 0; jb < TPM / 16; ++jb) {
if (jb < nblocks) {
uint32_t r[4];
const int row = jb * 16 + (lane & 7) + ((lane >> 4) & 1) * 8;
ldmatrix_x4(r, xs + (size_t)row * TXS + kk * 16 + ((lane >> 3) & 1) * 8);
const uint32_t bl[2] = {r[0], r[1]}, bh[2] = {r[2], r[3]};
#pragma unroll
for (int i = 0; i < TMT; ++i) {
mma16816(acc[i][2 * jb], a[i], bl);
mma16816(acc[i][2 * jb + 1], a[i], bh);
}
}
}
}
}
cp_async_wait<0>();
__syncthreads(); // the stages become the epilogue's sums
}

// acc (members TPH * hf .. + TPH - 1) -> E[member - TPH * hf][col] fp32, row stride ES; col0: first tile's column.
__device__ __forceinline__ void tspill(float* E, int ES, int col0, const float (&acc)[TMT][TNB][4], int hf) {
const int lane = threadIdx.x & 31, g = lane >> 2, t = lane & 3;
#pragma unroll
for (int i = 0; i < TMT; ++i)
#pragma unroll
for (int q = 0; q < TPM / 16; ++q) {
const int nb = hf * (TPM / 16) + q;
const int tok = q * 8 + 2 * t, col = col0 + i * 16 + g;
E[(size_t)tok * ES + col] = acc[i][nb][0];
E[(size_t)(tok + 1) * ES + col] = acc[i][nb][1];
E[(size_t)tok * ES + col + 8] = acc[i][nb][2];
E[(size_t)(tok + 1) * ES + col + 8] = acc[i][nb][3];
}
}

template <int XR>
__device__ __forceinline__ void tgateup(const half* __restrict__ Xg, const half* __restrict__ Xu, const uint32_t* __restrict__ Tg,
const uint32_t* __restrict__ Tu, const int* __restrict__ items, const int* __restrict__ counts, const int* __restrict__ members,
const half* __restrict__ svh_g, const half* __restrict__ svh_u, const half* __restrict__ suh_d, half* __restrict__ xd,
int K, int N, int xshift, float limit, unsigned char* smem_raw, int* rows_sh) {
const int cb = blockIdx.x, item = blockIdx.y;
if (item >= counts[0]) return;
const int e = items[3 * item], first = items[3 * item + 1], cnt = items[3 * item + 2];
if (threadIdx.x < TPM) rows_sh[threadIdx.x] = (int)threadIdx.x < cnt ? members[first + threadIdx.x] : -1;
__syncthreads();
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
float acc[TMT][TNB][4];
tmain<2, XR>(Xg, Xu, Tg, Tu, rows_sh, cnt, e, K, N >> 4, cb, xshift, reinterpret_cast<half*>(smem_raw), acc);
float* Eg = reinterpret_cast<float*>(smem_raw);
int mat, nt;
ttile<2>(warp * TMT, cb, mat, nt);
const int col0 = (nt - cb * 8) * 16;
const int n = cb * 128 + 4 * lane;
for (int hf = 0; hf < 2 && hf * TPH < cnt; ++hf) {
if (hf) __syncthreads(); // the first half's rows are read
tspill(Eg + (size_t)mat * TPH * GU_ES, GU_ES, col0, acc, hf);
__syncthreads();
for (int i = warp; i < TPH && hf * TPH + i < cnt; i += TNW) {
const int p = rows_sh[hf * TPH + i];
float gv[4], uv[4];
#pragma unroll
for (int j = 0; j < 4; ++j) {
gv[j] = Eg[(size_t)i * GU_ES + 4 * lane + j];
uv[j] = Eg[(size_t)(TPH + i) * GU_ES + 4 * lane + j];
}
fwht128(gv, lane);
fwht128(uv, lane);
float v[4];
#pragma unroll
for (int j = 0; j < 4; ++j) {
float gg = clamp_hi(bf16r(gv[j] * HAD_SCALE * __half2float(svh_g[(size_t)e * N + n + j])), limit);
float uu = clamp_both(bf16r(uv[j] * HAD_SCALE * __half2float(svh_u[(size_t)e * N + n + j])), limit);
float act = bf16r(bf16r(gg / (1.f + expf(-gg))) * uu);
v[j] = act * __half2float(suh_d[(size_t)e * N + n + j]);
}
fwht128(v, lane);
half* o = xd + (size_t)p * N + n;
#pragma unroll
for (int j = 0; j < 4; ++j) o[j] = __float2half_rn(v[j] * HAD_SCALE);
}
}
}

} // namespace
1 change: 1 addition & 0 deletions zig/src/cuda/kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub const modelopt_pack: []const u8 = if (available) &Blob("fatbin_modelopt_pack
pub const glm_glue: []const u8 = if (available) &Blob("fatbin_glm_glue").bytes else &.{};
pub const glm_ops: []const u8 = if (available) &Blob("fatbin_glm_ops").bytes else &.{};
pub const glm_quant: []const u8 = if (available) &Blob("fatbin_glm_quant").bytes else &.{};
pub const glm_exl3: []const u8 = if (available) &Blob("fatbin_glm_exl3").bytes else &.{};
pub const glm_kda: []const u8 = if (available) &Blob("fatbin_glm_kda").bytes else &.{};
pub const torch_argmax: []const u8 = if (available) &Blob("fatbin_torch_argmax").bytes else &.{};
pub const torch_topk: []const u8 = if (available) &Blob("fatbin_torch_topk").bytes else &.{};
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/glm/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub const Config = struct {
run: u32 = 45, // backbone layers loaded and run: all, or the first few for a check that must fit (GLM_LAYERS)
vocab: u32 = 154880,
head_unit: u32 = 64, // the vocabulary's TP cut in whole units: 128 for EXL3, whose matrices split at 128 columns
exl3: bool = false, // routed experts in EXL3 (quant_method "exl3", Mia-AiLab's checkpoint), the rest bf16
eps: f32 = 1e-5,
dense_layers: u32 = 3,
dense_inter: u32 = 12288, // this Mac's dense MLP rows: all, or its half in TP2
Expand Down Expand Up @@ -151,6 +152,7 @@ pub fn parse(gpa: std.mem.Allocator, json: []const u8) !Config {
c.vocab = @intCast(try int(t, "vocab_size"));
if (root.get("quantization_config")) |q| if (q == .object) if (q.object.get("quant_method")) |m| if (m == .string and std.mem.eql(u8, m.string, "exl3")) {
c.head_unit = 128;
c.exl3 = true;
};
c.eps = @floatCast(try float(t, "rms_norm_eps"));
c.dense_layers = @intCast(try int(t, "first_k_dense_replace"));
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/glm/cuda.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! GLM-5.3-Flash on CUDA: the family's parts, ported from the Python family's CUDA path.

pub const experts = @import("cuda_experts.zig");
pub const exl3 = @import("cuda_exl3.zig");
pub const glue = @import("cuda_glue.zig");
pub const kda = @import("cuda_kda.zig");
pub const b16 = @import("cuda_b16.zig");
Expand All @@ -25,6 +26,7 @@ pub const native = @import("cuda_native.zig");

test {
_ = experts;
_ = exl3;
_ = glue;
_ = kda;
_ = b16;
Expand Down
4 changes: 3 additions & 1 deletion zig/src/families/glm/cuda_engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ranks = @import("cuda_ranks.zig");
const sampler = @import("cuda_sampler.zig");
const heat = @import("heat");
const modelopt = @import("core").modelopt;
const exl3 = @import("cuda_exl3.zig");

pub const Options = struct {
capacity: usize, // tokens a sequence holds
Expand Down Expand Up @@ -101,7 +102,8 @@ pub const Engine = struct {
const cfg_path = try std.fs.path.join(a, &.{ checkpoint, "config.json" });
const text = try std.Io.Dir.cwd().readFileAlloc(io, cfg_path, a, .limited(1 << 24));
var why: modelopt.Why = .{};
modelopt.admit(gpa, text, &why) catch |err| { // a quant_algo other than NVFP4 is refused before anything loads
const admitted = if (exl3.isExl3(gpa, text)) exl3.admit(gpa, text, &why) else modelopt.admit(gpa, text, &why);
admitted catch |err| { // anything but ModelOpt NVFP4 or EXL3 (4-bit mcg routed experts) is refused before loading
std.log.err("{s}: {s}", .{ cfg_path, why.text() });
return err;
};
Expand Down
Loading