Skip to content

GLM-5.3-Flash EXL3 on 2x DGX Spark with TensorFold

Python

0084: launch order of the expert decode kernel (TF_GLM_EXL3_ORDER=1, off by default)

Lukasz Raczyloe7b760f48e71
diff --git a/patches/0084-glm-expert-launch-order.patch b/patches/0084-glm-expert-launch-order.patch
new file mode 100644
index 0000000..3d67de8
--- /dev/null
+++ b/patches/0084-glm-expert-launch-order.patch
@@ -0,0 +1,121 @@
+diff --git tensorfold/families/glm5_next/cuda/exl3.cu tensorfold/families/glm5_next/cuda/exl3.cu
+index 5ef3fa0..c14c1b3 100644
+--- tensorfold/families/glm5_next/cuda/exl3.cu
++++ tensorfold/families/glm5_next/cuda/exl3.cu
+@@ -347,7 +347,7 @@ __global__ void down_epilogue_kernel(const float* __restrict__ Z, const int* __r
+ // ld.global.nc.L1::no_allocate.v4 (lane l: bytes 16 l .. of each 512 B of a step, NT / 4 loads a lane), PD
+ // steps in flight, through a 1 KB staging area in the warp's own slice of `red` (two __syncwarp a step), read
+ // back as word `lane` of each tile.
+-template <int NT, int W, int STEPS, int FUSE, bool XROW, int LD = 0, int PD = 1>
++template <int NT, int W, int STEPS, int FUSE, bool XROW, int LD = 0, int PD = 1, int SWZ = 0>
+ __global__ void __launch_bounds__(W * 32) dec_kernel(
+ const half* __restrict__ X0, const half* __restrict__ X1, const uint32_t* __restrict__ T0,
+ const uint32_t* __restrict__ T1, const int* __restrict__ items, const int* __restrict__ counts,
+@@ -356,7 +356,14 @@ __global__ void __launch_bounds__(W * 32) dec_kernel(
+ float limit, int* __restrict__ done) {
+ static_assert(FUSE == 0 || NT * 16 == 128, "a fused epilogue needs one 128-column Hadamard block a program");
+ static_assert(LD == 0 || (NT % 4 == 0 && PD >= 1 && STEPS % PD == 0), "LD 1: NT a multiple of 4, PD | STEPS");
+- const int item = blockIdx.x, nblock = blockIdx.y;
++ int item = blockIdx.x, nblock = blockIdx.y, zz = blockIdx.z;
++ if constexpr (SWZ != 0) { // launch order only: the blocks of one k slice run together
++ const unsigned lid = blockIdx.x + gridDim.x * (blockIdx.y + gridDim.y * blockIdx.z);
++ nblock = (int)(lid % gridDim.y);
++ const unsigned r = lid / gridDim.y;
++ if (SWZ == 1) { item = (int)(r % gridDim.x); zz = (int)(r / gridDim.x); }
++ else { zz = (int)(r % gridDim.z); item = (int)(r / gridDim.z); }
++ }
+ int e, first, cnt;
+ if constexpr (LD == 0) {
+ if (item >= counts[0]) return;
+@@ -367,7 +374,7 @@ __global__ void __launch_bounds__(W * 32) dec_kernel(
+ if (item >= total) return;
+ }
+ if (e >= E) return; // the shared expert (id E) is not EXL3
+- const int mat = blockIdx.z / SK, split = blockIdx.z % SK;
++ const int mat = zz / SK, split = zz % SK;
+ const half* X = mat ? X1 : X0;
+ const uint32_t* T = mat ? T1 : T0;
+ const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
+@@ -999,18 +1006,22 @@ static void dec_launch(const at::Tensor& X0, const at::Tensor& X1, const at::Ten
+ const at::Tensor& items, const at::Tensor& counts, const at::Tensor& members, at::Tensor& Z,
+ int64_t mats, int64_t K, int64_t N, int64_t P, int64_t SK, int64_t max_items, int64_t E,
+ int64_t slots, int64_t fuse, bool xrow, const at::Tensor& sv0, const at::Tensor& sv1,
+- const at::Tensor& su2, at::Tensor& out, double limit, at::Tensor& done, int64_t ld) {
++ const at::Tensor& su2, at::Tensor& out, double limit, at::Tensor& done, int64_t ld, int swz) {
+ constexpr int NT = 8, W = 4;
+ constexpr int PD4 = STEPS % 4 == 0 ? 4 : 2;
+ dim3 grid((unsigned)max_items, (unsigned)(N / (16 * NT)), (unsigned)(mats * SK));
+ auto stream = at::cuda::getCurrentCUDAStream();
+ auto h = [](const at::Tensor& t) { return reinterpret_cast<const half*>(t.data_ptr()); };
+ auto w = [](const at::Tensor& t) { return reinterpret_cast<const uint32_t*>(t.data_ptr()); };
+-#define DEC1(F_, XR_, LD_, PD_) \
+- dec_kernel<NT, W, STEPS, F_, XR_, LD_, PD_><<<grid, W * 32, 0, stream>>>( \
++#define DEC1S(F_, XR_, LD_, PD_, SW_) \
++ dec_kernel<NT, W, STEPS, F_, XR_, LD_, PD_, SW_><<<grid, W * 32, 0, stream>>>( \
+ h(X0), h(X1), w(T0), w(T1), items.data_ptr<int>(), counts.data_ptr<int>(), members.data_ptr<int>(), \
+ Z.data_ptr<float>(), (int)K, (int)N, (int)P, (int)SK, (int)E, (int)slots, h(sv0), h(sv1), h(su2), \
+ out.data_ptr(), (float)limit, done.data_ptr<int>())
++#define DEC1(F_, XR_, LD_, PD_) \
++ if (swz == 0) DEC1S(F_, XR_, LD_, PD_, 0); \
++ else if (swz == 1) DEC1S(F_, XR_, LD_, PD_, 1); \
++ else DEC1S(F_, XR_, LD_, PD_, 2)
+ #define DEC(F_, XR_) \
+ if (ld == 0) DEC1(F_, XR_, 0, 1); \
+ else if (ld == 1) DEC1(F_, XR_, 1, 1); \
+@@ -1027,6 +1038,7 @@ static void dec_launch(const at::Tensor& X0, const at::Tensor& X1, const at::Ten
+ }
+ #undef DEC
+ #undef DEC1
++#undef DEC1S
+ }
+
+ void exl3_dec_cuda(const at::Tensor& X0, const at::Tensor& X1, const at::Tensor& T0, const at::Tensor& T1,
+@@ -1035,13 +1047,16 @@ void exl3_dec_cuda(const at::Tensor& X0, const at::Tensor& X1, const at::Tensor&
+ int64_t slots, int64_t fuse, bool xrow, const at::Tensor& sv0, const at::Tensor& sv1,
+ const at::Tensor& su2, at::Tensor& out, double limit, at::Tensor& done, int64_t ld) {
+ constexpr int NT = 8, W = 4;
++ const int swz = (int)(ld >> 2); // launch order: 0 as launched, 1 / 2 a k slice's blocks together
++ ld &= 3;
++ TORCH_CHECK(swz >= 0 && swz <= 2, "decode expert kernel: launch order 0, 1 or 2");
+ TORCH_CHECK(ld >= 0 && ld <= 3, "decode expert kernel: loads 0 (32-bit), 1 / 2 / 3 (16-byte, 1 / 2 / 4 steps ahead)");
+ TORCH_CHECK(K % (16 * SK * W) == 0 && N % (16 * NT) == 0, "decode expert kernel: whole k steps a warp");
+ TORCH_CHECK(fuse != 1 || (SK == 1 && mats == 1), "the fused down epilogue takes one split of one matrix");
+ TORCH_CHECK(fuse != 2 || mats == 2, "the fused gate/up epilogue takes gate and up");
+ const int64_t steps = K / (16 * SK * W);
+ #define ARGS X0, X1, T0, T1, items, counts, members, Z, mats, K, N, P, SK, max_items, E, slots, fuse, xrow, sv0, sv1, \
+- su2, out, limit, done, ld
++ su2, out, limit, done, ld, swz
+ if (steps == 16) dec_launch<16>(ARGS);
+ else if (steps == 12) dec_launch<12>(ARGS);
+ else if (steps == 10) dec_launch<10>(ARGS);
+diff --git tensorfold/families/glm5_next/cuda/exl3_mm.py tensorfold/families/glm5_next/cuda/exl3_mm.py
+index 2bc0b6b..be27418 100644
+--- tensorfold/families/glm5_next/cuda/exl3_mm.py
++++ tensorfold/families/glm5_next/cuda/exl3_mm.py
+@@ -49,6 +49,17 @@ def dec_settings() -> tuple[bool, int, bool]:
+ return on, fuse, os.environ.get("TF_GLM_EXL3_XROW", "1") != "0"
+
+
++def dec_order() -> int:
++ """TF_GLM_EXL3_ORDER: the decode kernel's block launch order (the same bits for every value). ``0`` (default):
++ items fastest; ``1`` / ``2``: the 128-column blocks of one k slice first, then the items (1) or the matrices and
++ splits (2)."""
++
++ v = os.environ.get("TF_GLM_EXL3_ORDER", "0").strip()
++ if v not in ("0", "1", "2"):
++ raise ValueError(f"TF_GLM_EXL3_ORDER: 0, 1 or 2, not {v!r}")
++ return int(v)
++
++
+ LOADS = {"0": 0, "ldg": 0, "1": 1, "nc": 1, "nc1": 1, "nc2": 2, "nc4": 3}
+
+
+@@ -215,7 +226,7 @@ def routed(x: torch.Tensor, pick: torch.Tensor, plan: grouped.Plan, ex: Exl3Expe
+ else:
+ ext.rot_in(x, x.stride(0), pick, ex.suh_g, ex.suh_u, s.xg, s.xu, R, D, slots)
+ xg, xu = s.xg, s.xu
+- ld = dec_loads()
++ ld = dec_loads() | (dec_order() << 2)
+ ext.dec(xg, xu, ex.gt, ex.ut, plan.items, plan.counts, plan.members, s.z, 2, D, NI, P, 4, items, slots,
+ 2 if fuse & 2 else 0, xrow, ex.svh_g, ex.svh_u, ex.suh_d, s.xd, float(limit), s.done, ld)
+ if not fuse & 2: