quantuminfer 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +96 -2
- package/bin/quantuminfer.js +61 -0
- package/c27/adapters.py +161 -0
- package/c27/harness27.py +349 -0
- package/data/input.txt +40000 -0
- package/data/wikitext/test.txt +7249 -0
- package/engine/API.txt +208 -0
- package/engine/PLUGINS.txt +4 -0
- package/engine/__init__.py +82 -0
- package/engine/adapter.py +505 -0
- package/engine/blocks.py +1180 -0
- package/engine/c12.py +324 -0
- package/engine/c12cu.py +1074 -0
- package/engine/ctx.py +87 -0
- package/engine/dev.py +11 -0
- package/engine/ds4.py +389 -0
- package/engine/kernels.py +833 -0
- package/engine/model.py +281 -0
- package/engine/record.py +140 -0
- package/engine/server.py +183 -0
- package/engine/spec.py +1030 -0
- package/engine/wcode.py +122 -0
- package/p27/SPEC.txt +66 -0
- package/p27/STATUS.txt +91 -0
- package/p27/bench_glm.py +354 -0
- package/p27/bench_multi.py +76 -0
- package/p27/mkfinal.py +98 -0
- package/p27/modal_comp.py +173 -0
- package/p27/modal_dl.py +63 -0
- package/p27/modal_p27.py +168 -0
- package/p27/modal_probe.py +49 -0
- package/p27/p27rules.py +29 -0
- package/p27/pblocks.py +1242 -0
- package/p27/pmodel.py +402 -0
- package/p27/pref.py +151 -0
- package/p27/prof_glm.py +34 -0
- package/p27/quant.py +850 -0
- package/p27/recipes/vllm_b300_single_node_tep_b300_agentx_ep.md +353 -0
- package/p27/recipes/vllm_b300_single_node_tp_b300_agentx.md +351 -0
- package/p27/recipes/vllm_b300_single_node_tp_baseline.md +250 -0
- package/p27/recipes/vllm_default_gb300.md +281 -0
- package/p27/recipes/vllm_glm53_h200.md +301 -0
- package/p27/run_window1.sh +21 -0
- package/p27/run_window2.sh +13 -0
- package/p27/sweep_glm.py +64 -0
- package/p27/t_align.py +42 -0
- package/p27/t_combine.py +28 -0
- package/p27/t_echo.py +31 -0
- package/p27/t_fp8dense.py +59 -0
- package/p27/t_fp8speed.py +48 -0
- package/p27/t_glm.py +340 -0
- package/p27/t_glu.py +30 -0
- package/p27/t_gsuf.py +23 -0
- package/p27/t_jac.py +22 -0
- package/p27/t_moebm.py +30 -0
- package/p27/t_nccl.py +36 -0
- package/p27/t_online_jac.py +31 -0
- package/p27/t_probe.py +4 -0
- package/p27/t_router.py +28 -0
- package/p27/t_symm.py +50 -0
- package/p27/t_tiny.py +116 -0
- package/package.json +34 -4
- package/results/ANALYSIS.md +160 -0
- package/results/comp_vllm_mtp_k5_b300mtp.json +235 -0
- package/results/glm53_b300x8_opt4.json +624 -0
- package/results/res_glm_c32w3_b300.json +638 -0
- package/results/res_glm_final_c32_b300.json +652 -0
- package/results/res_glm_multi_final_b300.json +1821 -0
- package/results/res_glm_online_b300.json +638 -0
- package/results/res_glm_online_fixed_b300.json +638 -0
- package/results/res_glm_opt4_b300.json +624 -0
- package/results/res_glm_t1_j8_b300.json +340 -0
- package/results/res_glm_text_plain_batch_b300.json +5792 -0
- package/results/res_glm_text_w1_b300.json +1635 -0
- package/results/res_glm_text_w3_b300.json +1639 -0
- package/results/res_sweep_echo_b300.json +129 -0
- package/results/res_sweep_final_b300.json +153 -0
- package/results/res_sweep_glm_b300.json +153 -0
- package/results/res_techo.json +29 -0
- package/results/res_tgsuf.json +21 -0
- package/results/res_tjac.json +17 -0
- package/results/res_tonline_jac.json +13 -0
package/engine/c12cu.py
ADDED
|
@@ -0,0 +1,1074 @@
|
|
|
1
|
+
# COPIED from gpu/exp_o3_latency (lane O3, round 6); ONE FIX by S27: decode_frag stacked the nibbles on dim 3 (kt = 2j + e,
|
|
2
|
+
# wrong weights for M > 64 rows and for checks) -> dim 4. The CUDA kernel itself was correct. Used by wcode.py.
|
|
3
|
+
"""C12 GEMM in CUDA (lane O3): tensor-core mma.m16n8k16 with the B operand (weights) decoded from the lossless
|
|
4
|
+
12-bit planes straight into mma fragments in registers (no shared memory for weights). Planes are stored in
|
|
5
|
+
FRAGMENT ORDER so each lane loads its own 16-byte lo word + 8-byte code word per 64 columns per 8-row tile, with an
|
|
6
|
+
8-deep register prefetch ring (two 512-column X chunks in flight). X (M <= 64 rows) is staged in shared memory per
|
|
7
|
+
512-column chunk and read with ldmatrix. fp32 accumulate; outliers corrected exactly in the epilogue."""
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
import torch
|
|
11
|
+
|
|
12
|
+
import c12
|
|
13
|
+
|
|
14
|
+
DEV = "cuda"
|
|
15
|
+
BF = torch.bfloat16
|
|
16
|
+
|
|
17
|
+
SRC = r"""
|
|
18
|
+
#include <cuda_bf16.h>
|
|
19
|
+
#include <stdint.h>
|
|
20
|
+
#include <torch/extension.h>
|
|
21
|
+
#include <c10/cuda/CUDAStream.h>
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
__device__ __forceinline__ uint4 ldnc4(const uint4* p) {
|
|
25
|
+
uint4 r;
|
|
26
|
+
asm volatile("ld.global.nc.L1::no_allocate.v4.u32 {%0,%1,%2,%3}, [%4];" : "=r"(r.x), "=r"(r.y), "=r"(r.z), "=r"(r.w) : "l"(p));
|
|
27
|
+
return r;
|
|
28
|
+
}
|
|
29
|
+
__device__ __forceinline__ uint2 ldnc2(const uint2* p) {
|
|
30
|
+
uint2 r;
|
|
31
|
+
asm volatile("ld.global.nc.L1::no_allocate.v2.u32 {%0,%1}, [%2];" : "=r"(r.x), "=r"(r.y) : "l"(p));
|
|
32
|
+
return r;
|
|
33
|
+
}
|
|
34
|
+
__device__ __forceinline__ void dec(uint32_t lo, uint32_t c, uint32_t base4, uint32_t& b0, uint32_t& b1) {
|
|
35
|
+
uint32_t t = (c & 0x07070707u) + base4;
|
|
36
|
+
uint32_t s = (c & 0x08080808u) << 4;
|
|
37
|
+
uint32_t h = t | s;
|
|
38
|
+
asm("prmt.b32 %0, %1, %2, 0x5140;" : "=r"(b0) : "r"(lo), "r"(h));
|
|
39
|
+
asm("prmt.b32 %0, %1, %2, 0x7362;" : "=r"(b1) : "r"(lo), "r"(h));
|
|
40
|
+
}
|
|
41
|
+
__device__ __forceinline__ void mma(float* c, uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, uint32_t b0, uint32_t b1) {
|
|
42
|
+
asm("mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, {%0,%1,%2,%3};"
|
|
43
|
+
: "+f"(c[0]), "+f"(c[1]), "+f"(c[2]), "+f"(c[3]) : "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(b0), "r"(b1));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
template <int MT, int NTW, int NWARP, int PD>
|
|
47
|
+
__global__ void __launch_bounds__(NWARP * 32) c12k(const __nv_bfloat16* __restrict__ X, int M, int K, int sxm,
|
|
48
|
+
const uint4* __restrict__ LOF, const uint2* __restrict__ COF, const uint8_t* __restrict__ BASEF,
|
|
49
|
+
const int* __restrict__ ROFF, const int* __restrict__ RCNT, const int* __restrict__ OK,
|
|
50
|
+
const float* __restrict__ OA, const float* __restrict__ OB, int maxc,
|
|
51
|
+
__nv_bfloat16* __restrict__ Y, int N, int sym, const __nv_bfloat16* __restrict__ RES, int srm,
|
|
52
|
+
float* __restrict__ WS, int* __restrict__ CNT, int dbg) {
|
|
53
|
+
// dbg (timing only): 1 skip mma, 2 skip decode, 4 skip X staging + its barriers, 8 skip outliers, 16 skip split-K fixup
|
|
54
|
+
constexpr int KC = PD * 64;
|
|
55
|
+
constexpr int XS = KC + 8;
|
|
56
|
+
extern __shared__ __align__(16) __nv_bfloat16 xs[]; // [2][MT*16][XS]
|
|
57
|
+
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
|
|
58
|
+
const int g = lane >> 2, cq = lane & 3;
|
|
59
|
+
const int jt0 = (blockIdx.x * NWARP + warp) * NTW;
|
|
60
|
+
const int NKB = K >> 6, NG = K >> 7;
|
|
61
|
+
const int S = gridDim.y, Ks = K / S, kbeg = blockIdx.y * Ks, kb_end = (kbeg + Ks) >> 6;
|
|
62
|
+
float acc[MT][NTW][4];
|
|
63
|
+
#pragma unroll
|
|
64
|
+
for (int a = 0; a < MT; a++)
|
|
65
|
+
#pragma unroll
|
|
66
|
+
for (int b = 0; b < NTW; b++)
|
|
67
|
+
#pragma unroll
|
|
68
|
+
for (int c = 0; c < 4; c++) acc[a][b][c] = 0.f;
|
|
69
|
+
uint4 L[PD][NTW];
|
|
70
|
+
uint2 C[PD][NTW];
|
|
71
|
+
uint32_t Bq[PD][NTW];
|
|
72
|
+
#pragma unroll
|
|
73
|
+
for (int s = 0; s < PD; s++)
|
|
74
|
+
#pragma unroll
|
|
75
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
76
|
+
const int kb = (kbeg >> 6) + s;
|
|
77
|
+
size_t idx = ((size_t)(jt0 + nt) * NKB + kb) * 32 + lane;
|
|
78
|
+
L[s][nt] = ldnc4(LOF + idx);
|
|
79
|
+
C[s][nt] = ldnc2(COF + idx);
|
|
80
|
+
Bq[s][nt] = BASEF[((size_t)(jt0 + nt) * NG + (kb >> 1)) * 8 + g];
|
|
81
|
+
}
|
|
82
|
+
// X staging: double-buffered cp.async (zero-fill rows >= M)
|
|
83
|
+
const int XBUF = MT * 16 * XS;
|
|
84
|
+
auto stage = [&](int kc, int buf) {
|
|
85
|
+
for (int i = threadIdx.x; i < MT * 16 * (KC / 8); i += NWARP * 32) {
|
|
86
|
+
int r = i / (KC / 8), c8 = (i % (KC / 8)) * 8;
|
|
87
|
+
int rr = r < M ? r : 0;
|
|
88
|
+
const void* src = X + (size_t)rr * sxm + kc + c8;
|
|
89
|
+
uint32_t dst = (uint32_t)__cvta_generic_to_shared(xs + buf * XBUF + r * XS + c8);
|
|
90
|
+
int nb = r < M ? 16 : 0;
|
|
91
|
+
asm volatile("cp.async.cg.shared.global [%0], [%1], 16, %2;" :: "r"(dst), "l"(src), "r"(nb));
|
|
92
|
+
}
|
|
93
|
+
asm volatile("cp.async.commit_group;");
|
|
94
|
+
};
|
|
95
|
+
if (!(dbg & 4)) stage(kbeg, 0);
|
|
96
|
+
int buf = 0;
|
|
97
|
+
for (int kc = kbeg; kc < kbeg + Ks; kc += KC, buf ^= 1) {
|
|
98
|
+
if (!(dbg & 4)) {
|
|
99
|
+
asm volatile("cp.async.wait_group 0;");
|
|
100
|
+
__syncthreads();
|
|
101
|
+
if (kc + KC < kbeg + Ks) stage(kc + KC, buf ^ 1);
|
|
102
|
+
}
|
|
103
|
+
const __nv_bfloat16* xb = xs + buf * XBUF;
|
|
104
|
+
#pragma unroll
|
|
105
|
+
for (int s = 0; s < PD; s++) {
|
|
106
|
+
const int kb = (kc >> 6) + s;
|
|
107
|
+
uint4 lw[NTW];
|
|
108
|
+
uint2 cw[NTW];
|
|
109
|
+
uint32_t base4[NTW];
|
|
110
|
+
#pragma unroll
|
|
111
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
112
|
+
lw[nt] = L[s % PD][nt];
|
|
113
|
+
cw[nt] = C[s % PD][nt];
|
|
114
|
+
base4[nt] = Bq[s % PD][nt] * 0x01010101u;
|
|
115
|
+
if (kb + PD < kb_end) {
|
|
116
|
+
size_t idx = ((size_t)(jt0 + nt) * NKB + kb + PD) * 32 + lane;
|
|
117
|
+
L[s % PD][nt] = ldnc4(LOF + idx);
|
|
118
|
+
C[s % PD][nt] = ldnc2(COF + idx);
|
|
119
|
+
Bq[s % PD][nt] = BASEF[((size_t)(jt0 + nt) * NG + ((kb + PD) >> 1)) * 8 + g];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
#pragma unroll
|
|
123
|
+
for (int kt = 0; kt < 4; kt++) {
|
|
124
|
+
uint32_t a[MT][4];
|
|
125
|
+
const int kcol = s * 64 + kt * 16;
|
|
126
|
+
#pragma unroll
|
|
127
|
+
for (int mt = 0; mt < MT; mt++) {
|
|
128
|
+
const __nv_bfloat16* p = xb + (mt * 16 + (lane & 15)) * XS + kcol + (lane >> 4) * 8;
|
|
129
|
+
uint32_t sa = (uint32_t)__cvta_generic_to_shared(p);
|
|
130
|
+
asm("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];"
|
|
131
|
+
: "=r"(a[mt][0]), "=r"(a[mt][1]), "=r"(a[mt][2]), "=r"(a[mt][3]) : "r"(sa));
|
|
132
|
+
}
|
|
133
|
+
#pragma unroll
|
|
134
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
135
|
+
uint32_t lo = (kt == 0) ? lw[nt].x : (kt == 1) ? lw[nt].y : (kt == 2) ? lw[nt].z : lw[nt].w;
|
|
136
|
+
uint32_t cwrd = (kt < 2) ? cw[nt].x : cw[nt].y;
|
|
137
|
+
uint32_t c = ((kt & 1) ? (cwrd >> 4) : cwrd) & 0x0F0F0F0Fu;
|
|
138
|
+
uint32_t b0, b1;
|
|
139
|
+
if (dbg & 2) { b0 = lo; b1 = c ^ base4[nt]; } else dec(lo, c, base4[nt], b0, b1);
|
|
140
|
+
if (dbg & 1) { acc[0][nt][0] += __uint_as_float(b0 & 0x3F000000u) + __uint_as_float(b1 & 0x3F000000u) + __uint_as_float(a[0][0] & 0x3F000000u); }
|
|
141
|
+
else {
|
|
142
|
+
#pragma unroll
|
|
143
|
+
for (int mt = 0; mt < MT; mt++) mma(acc[mt][nt], a[mt][0], a[mt][1], a[mt][2], a[mt][3], b0, b1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (!(dbg & 4)) __syncthreads();
|
|
149
|
+
}
|
|
150
|
+
if (S > 1 && !(dbg & 16)) { // serial split-K: partials to workspace, the last CTA of this n-group reduces in fixed order
|
|
151
|
+
const int MP = MT * 16;
|
|
152
|
+
#pragma unroll
|
|
153
|
+
for (int nt = 0; nt < NTW; nt++)
|
|
154
|
+
#pragma unroll
|
|
155
|
+
for (int mt = 0; mt < MT; mt++)
|
|
156
|
+
#pragma unroll
|
|
157
|
+
for (int h = 0; h < 2; h++) {
|
|
158
|
+
const int m = mt * 16 + g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
159
|
+
float2 v = make_float2(acc[mt][nt][2 * h], acc[mt][nt][2 * h + 1]);
|
|
160
|
+
__stcg(reinterpret_cast<float2*>(WS + ((size_t)blockIdx.y * MP + m) * N + n), v);
|
|
161
|
+
}
|
|
162
|
+
__threadfence();
|
|
163
|
+
__syncthreads();
|
|
164
|
+
__shared__ int s_last;
|
|
165
|
+
if (threadIdx.x == 0) s_last = (atomicAdd(CNT + blockIdx.x, 1) == S - 1);
|
|
166
|
+
__syncthreads();
|
|
167
|
+
if (!s_last) return;
|
|
168
|
+
__threadfence();
|
|
169
|
+
#pragma unroll
|
|
170
|
+
for (int nt = 0; nt < NTW; nt++)
|
|
171
|
+
#pragma unroll
|
|
172
|
+
for (int mt = 0; mt < MT; mt++)
|
|
173
|
+
#pragma unroll
|
|
174
|
+
for (int h = 0; h < 2; h++) {
|
|
175
|
+
const int m = mt * 16 + g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
176
|
+
float a0 = 0.f, a1 = 0.f;
|
|
177
|
+
for (int ss = 0; ss < S; ss++) {
|
|
178
|
+
float2 v = __ldcg(reinterpret_cast<const float2*>(WS + ((size_t)ss * MP + m) * N + n));
|
|
179
|
+
a0 += v.x;
|
|
180
|
+
a1 += v.y;
|
|
181
|
+
}
|
|
182
|
+
acc[mt][nt][2 * h] = a0;
|
|
183
|
+
acc[mt][nt][2 * h + 1] = a1;
|
|
184
|
+
}
|
|
185
|
+
if (threadIdx.x == 0) CNT[blockIdx.x] = 0;
|
|
186
|
+
}
|
|
187
|
+
// epilogue: thread holds C[g][2cq..2cq+1] and C[g+8][2cq..] of each (mt, nt) tile
|
|
188
|
+
#pragma unroll
|
|
189
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
190
|
+
#pragma unroll
|
|
191
|
+
for (int j = 0; j < 2; j++) {
|
|
192
|
+
const int n = (jt0 + nt) * 8 + 2 * cq + j;
|
|
193
|
+
const int ro = ROFF[n], rc = (dbg & 8) ? 0 : RCNT[n];
|
|
194
|
+
for (int o = 0; o < rc; o++) {
|
|
195
|
+
const int k = OK[ro + o];
|
|
196
|
+
const float fa = OA[ro + o], fb = OB[ro + o];
|
|
197
|
+
#pragma unroll
|
|
198
|
+
for (int mt = 0; mt < MT; mt++) {
|
|
199
|
+
#pragma unroll
|
|
200
|
+
for (int h = 0; h < 2; h++) {
|
|
201
|
+
const int m = mt * 16 + g + 8 * h;
|
|
202
|
+
if (m < M) {
|
|
203
|
+
float xv = __bfloat162float(X[(size_t)m * sxm + k]);
|
|
204
|
+
acc[mt][nt][2 * h + j] += xv * fa;
|
|
205
|
+
acc[mt][nt][2 * h + j] += xv * fb;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
#pragma unroll
|
|
212
|
+
for (int mt = 0; mt < MT; mt++) {
|
|
213
|
+
#pragma unroll
|
|
214
|
+
for (int h = 0; h < 2; h++) {
|
|
215
|
+
const int m = mt * 16 + g + 8 * h;
|
|
216
|
+
const int n = (jt0 + nt) * 8 + 2 * cq;
|
|
217
|
+
if (m < M) {
|
|
218
|
+
float v0 = acc[mt][nt][2 * h], v1 = acc[mt][nt][2 * h + 1];
|
|
219
|
+
if (RES != nullptr) {
|
|
220
|
+
__nv_bfloat162 r = *reinterpret_cast<const __nv_bfloat162*>(RES + (size_t)m * srm + n);
|
|
221
|
+
v0 += __bfloat162float(r.x);
|
|
222
|
+
v1 += __bfloat162float(r.y);
|
|
223
|
+
}
|
|
224
|
+
*reinterpret_cast<__nv_bfloat162*>(Y + (size_t)m * sym + n) = __floats2bfloat162_rn(v0, v1);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
template <int MT, int NTW, int NWARP, int PD>
|
|
232
|
+
void launch(torch::Tensor x, torch::Tensor lof, torch::Tensor cof, torch::Tensor basef, torch::Tensor roff,
|
|
233
|
+
torch::Tensor rcnt, torch::Tensor ok, torch::Tensor oa, torch::Tensor ob, int maxc, torch::Tensor y,
|
|
234
|
+
int N, torch::Tensor res, bool hasres, int S, torch::Tensor ws, torch::Tensor cnt, int dbg) {
|
|
235
|
+
int M = x.size(0), K = x.size(1);
|
|
236
|
+
int smem = 2 * MT * 16 * (PD * 64 + 8) * 2;
|
|
237
|
+
auto kern = c12k<MT, NTW, NWARP, PD>;
|
|
238
|
+
static bool set = false;
|
|
239
|
+
if (!set) { cudaFuncSetAttribute(kern, cudaFuncAttributeMaxDynamicSharedMemorySize, 140 * 1024); set = true; }
|
|
240
|
+
dim3 grid(N / (8 * NWARP * NTW), S);
|
|
241
|
+
kern<<<grid, NWARP * 32, smem, c10::cuda::getCurrentCUDAStream()>>>(
|
|
242
|
+
(const __nv_bfloat16*)x.data_ptr(), M, K, x.stride(0), (const uint4*)lof.data_ptr(), (const uint2*)cof.data_ptr(),
|
|
243
|
+
(const uint8_t*)basef.data_ptr(), roff.data_ptr<int>(), rcnt.data_ptr<int>(), ok.data_ptr<int>(),
|
|
244
|
+
oa.data_ptr<float>(), ob.data_ptr<float>(), maxc, (__nv_bfloat16*)y.data_ptr(), N, y.stride(0),
|
|
245
|
+
hasres ? (const __nv_bfloat16*)res.data_ptr() : nullptr, hasres ? res.stride(0) : 0,
|
|
246
|
+
ws.data_ptr<float>(), cnt.data_ptr<int>(), dbg);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
// ------------------------------------------------------------------------------------------------------------------
|
|
251
|
+
// c12p: the small-M (<= 16 rows) kernel for plain decoding. Weight fragments stream through a DEEP per-warp smem ring
|
|
252
|
+
// filled by cp.async (each lane copies only its own fragment bytes, so the ring needs no __syncthreads), X fragments
|
|
253
|
+
// come straight from global/L1 (X is a few KB), so the main loop has no CTA-wide barrier at all. Many CTAs per SM
|
|
254
|
+
// x DS k-blocks in flight keep the memory system full on every projection (the register ring of c12k could not).
|
|
255
|
+
template <int NTW, int NWARP, int DS>
|
|
256
|
+
__global__ void __launch_bounds__(NWARP * 32) c12p_k(const __nv_bfloat16* __restrict__ X, int M, int K, int sxm,
|
|
257
|
+
const uint4* __restrict__ LOF, const uint2* __restrict__ COF, const uint8_t* __restrict__ BASEF,
|
|
258
|
+
const int* __restrict__ ROFF, const int* __restrict__ RCNT, const int* __restrict__ OK,
|
|
259
|
+
const float* __restrict__ OA, const float* __restrict__ OB,
|
|
260
|
+
__nv_bfloat16* __restrict__ Y, int N, int sym, float* __restrict__ WS, int* __restrict__ CNT) {
|
|
261
|
+
extern __shared__ __align__(16) uint8_t ring_raw[];
|
|
262
|
+
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
|
|
263
|
+
const int g = lane >> 2, cq = lane & 3;
|
|
264
|
+
const int jt0 = (blockIdx.x * NWARP + warp) * NTW;
|
|
265
|
+
const int NKB = K >> 6, NG = K >> 7;
|
|
266
|
+
const int S = gridDim.y, Ks = K / S, kb0 = (blockIdx.y * Ks) >> 6, nkb = Ks >> 6;
|
|
267
|
+
constexpr int SLOTB = (NTW * 32 * 24 + NTW * 8 + 15) / 16 * 16; // per warp per k-block: lo 16 B + code 8 B per lane per nt + bases (16 B aligned)
|
|
268
|
+
uint8_t* ring = ring_raw + warp * DS * SLOTB;
|
|
269
|
+
float acc[NTW][4];
|
|
270
|
+
#pragma unroll
|
|
271
|
+
for (int b = 0; b < NTW; b++)
|
|
272
|
+
#pragma unroll
|
|
273
|
+
for (int c = 0; c < 4; c++) acc[b][c] = 0.f;
|
|
274
|
+
auto issue = [&](int i) { // local k-block i -> slot i % DS
|
|
275
|
+
if (i < nkb) {
|
|
276
|
+
const int kb = kb0 + i;
|
|
277
|
+
uint8_t* sl = ring + (i % DS) * SLOTB;
|
|
278
|
+
#pragma unroll
|
|
279
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
280
|
+
const size_t idx = ((size_t)(jt0 + nt) * NKB + kb) * 32 + lane;
|
|
281
|
+
uint32_t d1 = (uint32_t)__cvta_generic_to_shared(sl + (nt * 32 + lane) * 16);
|
|
282
|
+
uint32_t d2 = (uint32_t)__cvta_generic_to_shared(sl + NTW * 512 + (nt * 32 + lane) * 8);
|
|
283
|
+
asm volatile("cp.async.cg.shared.global [%0], [%1], 16;" ::"r"(d1), "l"(LOF + idx));
|
|
284
|
+
if ((lane & 1) == 0) asm volatile("cp.async.cg.shared.global [%0], [%1], 16;" ::"r"(d2), "l"(COF + idx));
|
|
285
|
+
if (lane < 2) {
|
|
286
|
+
uint32_t d3 = (uint32_t)__cvta_generic_to_shared(sl + NTW * 768 + nt * 8 + lane * 4);
|
|
287
|
+
asm volatile("cp.async.ca.shared.global [%0], [%1], 4;" ::"r"(d3),
|
|
288
|
+
"l"(BASEF + ((size_t)(jt0 + nt) * NG + (kb >> 1)) * 8 + lane * 4));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
asm volatile("cp.async.commit_group;");
|
|
293
|
+
};
|
|
294
|
+
#pragma unroll
|
|
295
|
+
for (int i = 0; i < DS - 1; i++) issue(i);
|
|
296
|
+
// A fragments (rows g and g + 8 of the 16-row tile; rows >= M are zero) for k-block i, k-tile kt
|
|
297
|
+
const bool r0ok = g < M, r1ok = g + 8 < M;
|
|
298
|
+
const __nv_bfloat16* x0 = X + (size_t)(r0ok ? g : 0) * sxm;
|
|
299
|
+
const __nv_bfloat16* x1 = X + (size_t)(r1ok ? g + 8 : 0) * sxm;
|
|
300
|
+
uint32_t an[4][4];
|
|
301
|
+
auto lda = [&](int i) {
|
|
302
|
+
const int kbase = (kb0 + i) * 64 + 2 * cq;
|
|
303
|
+
#pragma unroll
|
|
304
|
+
for (int kt = 0; kt < 4; kt++) {
|
|
305
|
+
const int k = kbase + kt * 16;
|
|
306
|
+
an[kt][0] = r0ok ? __ldg(reinterpret_cast<const uint32_t*>(x0 + k)) : 0u;
|
|
307
|
+
an[kt][1] = r1ok ? __ldg(reinterpret_cast<const uint32_t*>(x1 + k)) : 0u;
|
|
308
|
+
an[kt][2] = r0ok ? __ldg(reinterpret_cast<const uint32_t*>(x0 + k + 8)) : 0u;
|
|
309
|
+
an[kt][3] = r1ok ? __ldg(reinterpret_cast<const uint32_t*>(x1 + k + 8)) : 0u;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
lda(0);
|
|
313
|
+
for (int i = 0; i < nkb; i++) {
|
|
314
|
+
uint32_t a[4][4];
|
|
315
|
+
#pragma unroll
|
|
316
|
+
for (int kt = 0; kt < 4; kt++)
|
|
317
|
+
#pragma unroll
|
|
318
|
+
for (int q = 0; q < 4; q++) a[kt][q] = an[kt][q];
|
|
319
|
+
if (i + 1 < nkb) lda(i + 1);
|
|
320
|
+
asm volatile("cp.async.wait_group %0;" ::"n"(DS - 2) : "memory");
|
|
321
|
+
__syncwarp();
|
|
322
|
+
const uint8_t* sl = ring + (i % DS) * SLOTB;
|
|
323
|
+
uint4 lw[NTW];
|
|
324
|
+
uint2 cw[NTW];
|
|
325
|
+
uint32_t base4[NTW];
|
|
326
|
+
#pragma unroll
|
|
327
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
328
|
+
lw[nt] = *reinterpret_cast<const uint4*>(sl + (nt * 32 + lane) * 16);
|
|
329
|
+
cw[nt] = *reinterpret_cast<const uint2*>(sl + NTW * 512 + (nt * 32 + lane) * 8);
|
|
330
|
+
base4[nt] = (uint32_t)sl[NTW * 768 + nt * 8 + g] * 0x01010101u;
|
|
331
|
+
}
|
|
332
|
+
__syncwarp();
|
|
333
|
+
issue(i + DS - 1); // refills slot (i - 1) % DS, already consumed by every lane of this warp
|
|
334
|
+
#pragma unroll
|
|
335
|
+
for (int kt = 0; kt < 4; kt++)
|
|
336
|
+
#pragma unroll
|
|
337
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
338
|
+
uint32_t lo = (kt == 0) ? lw[nt].x : (kt == 1) ? lw[nt].y : (kt == 2) ? lw[nt].z : lw[nt].w;
|
|
339
|
+
uint32_t cwrd = (kt < 2) ? cw[nt].x : cw[nt].y;
|
|
340
|
+
uint32_t c = ((kt & 1) ? (cwrd >> 4) : cwrd) & 0x0F0F0F0Fu;
|
|
341
|
+
uint32_t b0, b1;
|
|
342
|
+
dec(lo, c, base4[nt], b0, b1);
|
|
343
|
+
mma(acc[nt], a[kt][0], a[kt][1], a[kt][2], a[kt][3], b0, b1);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
asm volatile("cp.async.wait_group 0;" ::: "memory");
|
|
347
|
+
if (S > 1) {
|
|
348
|
+
#pragma unroll
|
|
349
|
+
for (int nt = 0; nt < NTW; nt++)
|
|
350
|
+
#pragma unroll
|
|
351
|
+
for (int h = 0; h < 2; h++) {
|
|
352
|
+
const int m = g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
353
|
+
__stcg(reinterpret_cast<float2*>(WS + ((size_t)blockIdx.y * 16 + m) * N + n),
|
|
354
|
+
make_float2(acc[nt][2 * h], acc[nt][2 * h + 1]));
|
|
355
|
+
}
|
|
356
|
+
__threadfence();
|
|
357
|
+
__syncthreads();
|
|
358
|
+
__shared__ int s_last;
|
|
359
|
+
if (threadIdx.x == 0) s_last = (atomicAdd(CNT + blockIdx.x, 1) == S - 1);
|
|
360
|
+
__syncthreads();
|
|
361
|
+
if (!s_last) return;
|
|
362
|
+
__threadfence();
|
|
363
|
+
#pragma unroll
|
|
364
|
+
for (int nt = 0; nt < NTW; nt++)
|
|
365
|
+
#pragma unroll
|
|
366
|
+
for (int h = 0; h < 2; h++) {
|
|
367
|
+
const int m = g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
368
|
+
float a0 = 0.f, a1 = 0.f;
|
|
369
|
+
for (int ss = 0; ss < S; ss++) {
|
|
370
|
+
float2 v = __ldcg(reinterpret_cast<const float2*>(WS + ((size_t)ss * 16 + m) * N + n));
|
|
371
|
+
a0 += v.x;
|
|
372
|
+
a1 += v.y;
|
|
373
|
+
}
|
|
374
|
+
acc[nt][2 * h] = a0;
|
|
375
|
+
acc[nt][2 * h + 1] = a1;
|
|
376
|
+
}
|
|
377
|
+
if (threadIdx.x == 0) CNT[blockIdx.x] = 0;
|
|
378
|
+
}
|
|
379
|
+
#pragma unroll
|
|
380
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
381
|
+
#pragma unroll
|
|
382
|
+
for (int j = 0; j < 2; j++) {
|
|
383
|
+
const int n = (jt0 + nt) * 8 + 2 * cq + j;
|
|
384
|
+
const int ro = ROFF[n], rc = RCNT[n];
|
|
385
|
+
for (int o = 0; o < rc; o++) {
|
|
386
|
+
const int k = OK[ro + o];
|
|
387
|
+
const float fa = OA[ro + o], fb = OB[ro + o];
|
|
388
|
+
#pragma unroll
|
|
389
|
+
for (int h = 0; h < 2; h++) {
|
|
390
|
+
const int m = g + 8 * h;
|
|
391
|
+
if (m < M) {
|
|
392
|
+
float xv = __bfloat162float(X[(size_t)m * sxm + k]);
|
|
393
|
+
acc[nt][2 * h + j] += xv * fa;
|
|
394
|
+
acc[nt][2 * h + j] += xv * fb;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
#pragma unroll
|
|
400
|
+
for (int h = 0; h < 2; h++) {
|
|
401
|
+
const int m = g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
402
|
+
if (m < M)
|
|
403
|
+
*reinterpret_cast<__nv_bfloat162*>(Y + (size_t)m * sym + n) =
|
|
404
|
+
__floats2bfloat162_rn(acc[nt][2 * h], acc[nt][2 * h + 1]);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
template <int NTW, int NWARP, int DS, int MTP>
|
|
410
|
+
__global__ void __launch_bounds__(NWARP * 32) c12q_k(const __nv_bfloat16* __restrict__ X, int M, int K, int sxm,
|
|
411
|
+
const uint4* __restrict__ LOF, const uint2* __restrict__ COF, const uint8_t* __restrict__ BASEF,
|
|
412
|
+
const int* __restrict__ ROFF, const int* __restrict__ RCNT, const int* __restrict__ OK,
|
|
413
|
+
const float* __restrict__ OA, const float* __restrict__ OB,
|
|
414
|
+
__nv_bfloat16* __restrict__ Y, int N, int sym, float* __restrict__ WS, int* __restrict__ CNT) {
|
|
415
|
+
extern __shared__ __align__(16) uint8_t ring_raw[];
|
|
416
|
+
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
|
|
417
|
+
const int g = lane >> 2, cq = lane & 3;
|
|
418
|
+
const int jt0 = (blockIdx.x * NWARP + warp) * NTW;
|
|
419
|
+
const int NKB = K >> 6, NG = K >> 7;
|
|
420
|
+
const int S = gridDim.y, Ks = K / S, kb0 = (blockIdx.y * Ks) >> 6, nkb = Ks >> 6;
|
|
421
|
+
constexpr int SLOTB = (NTW * 32 * 24 + NTW * 8 + 15) / 16 * 16; // per warp per k-block: lo 16 B + code 8 B per lane per nt + bases (16 B aligned)
|
|
422
|
+
uint8_t* ring = ring_raw + warp * DS * SLOTB;
|
|
423
|
+
float acc[MTP][NTW][4];
|
|
424
|
+
#pragma unroll
|
|
425
|
+
for (int a_ = 0; a_ < MTP; a_++)
|
|
426
|
+
#pragma unroll
|
|
427
|
+
for (int b = 0; b < NTW; b++)
|
|
428
|
+
#pragma unroll
|
|
429
|
+
for (int c = 0; c < 4; c++) acc[a_][b][c] = 0.f;
|
|
430
|
+
auto issue = [&](int i) { // local k-block i -> slot i % DS
|
|
431
|
+
if (i < nkb) {
|
|
432
|
+
const int kb = kb0 + i;
|
|
433
|
+
uint8_t* sl = ring + (i % DS) * SLOTB;
|
|
434
|
+
#pragma unroll
|
|
435
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
436
|
+
const size_t idx = ((size_t)(jt0 + nt) * NKB + kb) * 32 + lane;
|
|
437
|
+
uint32_t d1 = (uint32_t)__cvta_generic_to_shared(sl + (nt * 32 + lane) * 16);
|
|
438
|
+
uint32_t d2 = (uint32_t)__cvta_generic_to_shared(sl + NTW * 512 + (nt * 32 + lane) * 8);
|
|
439
|
+
asm volatile("cp.async.cg.shared.global [%0], [%1], 16;" ::"r"(d1), "l"(LOF + idx));
|
|
440
|
+
if ((lane & 1) == 0) asm volatile("cp.async.cg.shared.global [%0], [%1], 16;" ::"r"(d2), "l"(COF + idx));
|
|
441
|
+
if (lane < 2) {
|
|
442
|
+
uint32_t d3 = (uint32_t)__cvta_generic_to_shared(sl + NTW * 768 + nt * 8 + lane * 4);
|
|
443
|
+
asm volatile("cp.async.ca.shared.global [%0], [%1], 4;" ::"r"(d3),
|
|
444
|
+
"l"(BASEF + ((size_t)(jt0 + nt) * NG + (kb >> 1)) * 8 + lane * 4));
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
asm volatile("cp.async.commit_group;");
|
|
449
|
+
};
|
|
450
|
+
#pragma unroll
|
|
451
|
+
for (int i = 0; i < DS - 1; i++) issue(i);
|
|
452
|
+
// A fragments (rows g and g + 8 of the 16-row tile; rows >= M are zero) for k-block i, k-tile kt
|
|
453
|
+
bool rok[MTP][2];
|
|
454
|
+
const __nv_bfloat16* xr[MTP][2];
|
|
455
|
+
#pragma unroll
|
|
456
|
+
for (int mt = 0; mt < MTP; mt++)
|
|
457
|
+
#pragma unroll
|
|
458
|
+
for (int h = 0; h < 2; h++) {
|
|
459
|
+
const int r = mt * 16 + g + 8 * h;
|
|
460
|
+
rok[mt][h] = r < M;
|
|
461
|
+
xr[mt][h] = X + (size_t)(r < M ? r : 0) * sxm;
|
|
462
|
+
}
|
|
463
|
+
for (int i = 0; i < nkb; i++) {
|
|
464
|
+
asm volatile("cp.async.wait_group %0;" ::"n"(DS - 2) : "memory");
|
|
465
|
+
__syncwarp();
|
|
466
|
+
const uint8_t* sl = ring + (i % DS) * SLOTB;
|
|
467
|
+
const int kbase = (kb0 + i) * 64 + 2 * cq;
|
|
468
|
+
uint32_t a[4][MTP][4];
|
|
469
|
+
#pragma unroll
|
|
470
|
+
for (int kt = 0; kt < 4; kt++)
|
|
471
|
+
#pragma unroll
|
|
472
|
+
for (int mt = 0; mt < MTP; mt++) {
|
|
473
|
+
const int k = kbase + kt * 16;
|
|
474
|
+
a[kt][mt][0] = rok[mt][0] ? __ldg(reinterpret_cast<const uint32_t*>(xr[mt][0] + k)) : 0u;
|
|
475
|
+
a[kt][mt][1] = rok[mt][1] ? __ldg(reinterpret_cast<const uint32_t*>(xr[mt][1] + k)) : 0u;
|
|
476
|
+
a[kt][mt][2] = rok[mt][0] ? __ldg(reinterpret_cast<const uint32_t*>(xr[mt][0] + k + 8)) : 0u;
|
|
477
|
+
a[kt][mt][3] = rok[mt][1] ? __ldg(reinterpret_cast<const uint32_t*>(xr[mt][1] + k + 8)) : 0u;
|
|
478
|
+
}
|
|
479
|
+
uint4 lw[NTW];
|
|
480
|
+
uint2 cw[NTW];
|
|
481
|
+
uint32_t base4[NTW];
|
|
482
|
+
#pragma unroll
|
|
483
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
484
|
+
lw[nt] = *reinterpret_cast<const uint4*>(sl + (nt * 32 + lane) * 16);
|
|
485
|
+
cw[nt] = *reinterpret_cast<const uint2*>(sl + NTW * 512 + (nt * 32 + lane) * 8);
|
|
486
|
+
base4[nt] = (uint32_t)sl[NTW * 768 + nt * 8 + g] * 0x01010101u;
|
|
487
|
+
}
|
|
488
|
+
__syncwarp();
|
|
489
|
+
issue(i + DS - 1); // refills slot (i - 1) % DS, already consumed by every lane of this warp
|
|
490
|
+
#pragma unroll
|
|
491
|
+
for (int kt = 0; kt < 4; kt++)
|
|
492
|
+
#pragma unroll
|
|
493
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
494
|
+
uint32_t lo = (kt == 0) ? lw[nt].x : (kt == 1) ? lw[nt].y : (kt == 2) ? lw[nt].z : lw[nt].w;
|
|
495
|
+
uint32_t cwrd = (kt < 2) ? cw[nt].x : cw[nt].y;
|
|
496
|
+
uint32_t c = ((kt & 1) ? (cwrd >> 4) : cwrd) & 0x0F0F0F0Fu;
|
|
497
|
+
uint32_t b0, b1;
|
|
498
|
+
dec(lo, c, base4[nt], b0, b1);
|
|
499
|
+
#pragma unroll
|
|
500
|
+
for (int mt = 0; mt < MTP; mt++) mma(acc[mt][nt], a[kt][mt][0], a[kt][mt][1], a[kt][mt][2], a[kt][mt][3], b0, b1);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
asm volatile("cp.async.wait_group 0;" ::: "memory");
|
|
504
|
+
if (S > 1) {
|
|
505
|
+
#pragma unroll
|
|
506
|
+
for (int mt = 0; mt < MTP; mt++)
|
|
507
|
+
#pragma unroll
|
|
508
|
+
for (int nt = 0; nt < NTW; nt++)
|
|
509
|
+
#pragma unroll
|
|
510
|
+
for (int h = 0; h < 2; h++) {
|
|
511
|
+
const int m = mt * 16 + g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
512
|
+
__stcg(reinterpret_cast<float2*>(WS + ((size_t)blockIdx.y * 16 * MTP + m) * N + n),
|
|
513
|
+
make_float2(acc[mt][nt][2 * h], acc[mt][nt][2 * h + 1]));
|
|
514
|
+
}
|
|
515
|
+
__threadfence();
|
|
516
|
+
__syncthreads();
|
|
517
|
+
__shared__ int s_last;
|
|
518
|
+
if (threadIdx.x == 0) s_last = (atomicAdd(CNT + blockIdx.x, 1) == S - 1);
|
|
519
|
+
__syncthreads();
|
|
520
|
+
if (!s_last) return;
|
|
521
|
+
__threadfence();
|
|
522
|
+
#pragma unroll
|
|
523
|
+
for (int mt = 0; mt < MTP; mt++)
|
|
524
|
+
#pragma unroll
|
|
525
|
+
for (int nt = 0; nt < NTW; nt++)
|
|
526
|
+
#pragma unroll
|
|
527
|
+
for (int h = 0; h < 2; h++) {
|
|
528
|
+
const int m = mt * 16 + g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
529
|
+
float a0 = 0.f, a1 = 0.f;
|
|
530
|
+
for (int ss = 0; ss < S; ss++) {
|
|
531
|
+
float2 v = __ldcg(reinterpret_cast<const float2*>(WS + ((size_t)ss * 16 * MTP + m) * N + n));
|
|
532
|
+
a0 += v.x;
|
|
533
|
+
a1 += v.y;
|
|
534
|
+
}
|
|
535
|
+
acc[mt][nt][2 * h] = a0;
|
|
536
|
+
acc[mt][nt][2 * h + 1] = a1;
|
|
537
|
+
}
|
|
538
|
+
if (threadIdx.x == 0) CNT[blockIdx.x] = 0;
|
|
539
|
+
}
|
|
540
|
+
#pragma unroll
|
|
541
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
542
|
+
#pragma unroll
|
|
543
|
+
for (int j = 0; j < 2; j++) {
|
|
544
|
+
const int n = (jt0 + nt) * 8 + 2 * cq + j;
|
|
545
|
+
const int ro = ROFF[n], rc = RCNT[n];
|
|
546
|
+
for (int o = 0; o < rc; o++) {
|
|
547
|
+
const int k = OK[ro + o];
|
|
548
|
+
const float fa = OA[ro + o], fb = OB[ro + o];
|
|
549
|
+
#pragma unroll
|
|
550
|
+
for (int mt = 0; mt < MTP; mt++)
|
|
551
|
+
#pragma unroll
|
|
552
|
+
for (int h = 0; h < 2; h++) {
|
|
553
|
+
const int m = mt * 16 + g + 8 * h;
|
|
554
|
+
if (m < M) {
|
|
555
|
+
float xv = __bfloat162float(X[(size_t)m * sxm + k]);
|
|
556
|
+
acc[mt][nt][2 * h + j] += xv * fa;
|
|
557
|
+
acc[mt][nt][2 * h + j] += xv * fb;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
#pragma unroll
|
|
563
|
+
for (int mt = 0; mt < MTP; mt++)
|
|
564
|
+
#pragma unroll
|
|
565
|
+
for (int h = 0; h < 2; h++) {
|
|
566
|
+
const int m = mt * 16 + g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
567
|
+
if (m < M)
|
|
568
|
+
*reinterpret_cast<__nv_bfloat162*>(Y + (size_t)m * sym + n) =
|
|
569
|
+
__floats2bfloat162_rn(acc[mt][nt][2 * h], acc[mt][nt][2 * h + 1]);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
template <int NTW, int NWARP, int DS, int MTP>
|
|
575
|
+
void launchq(torch::Tensor x, torch::Tensor lof, torch::Tensor cof, torch::Tensor basef, torch::Tensor roff,
|
|
576
|
+
torch::Tensor rcnt, torch::Tensor ok, torch::Tensor oa, torch::Tensor ob, torch::Tensor y, int N, int S,
|
|
577
|
+
torch::Tensor ws, torch::Tensor cnt) {
|
|
578
|
+
int M = x.size(0), K = x.size(1);
|
|
579
|
+
int smem = NWARP * DS * ((NTW * 32 * 24 + NTW * 8 + 15) / 16 * 16);
|
|
580
|
+
auto kern = c12q_k<NTW, NWARP, DS, MTP>;
|
|
581
|
+
static bool set = false;
|
|
582
|
+
if (!set) { cudaFuncSetAttribute(kern, cudaFuncAttributeMaxDynamicSharedMemorySize, smem); set = true; }
|
|
583
|
+
dim3 grid(N / (8 * NWARP * NTW), S);
|
|
584
|
+
kern<<<grid, NWARP * 32, smem, c10::cuda::getCurrentCUDAStream()>>>(
|
|
585
|
+
(const __nv_bfloat16*)x.data_ptr(), M, K, x.stride(0), (const uint4*)lof.data_ptr(), (const uint2*)cof.data_ptr(),
|
|
586
|
+
(const uint8_t*)basef.data_ptr(), roff.data_ptr<int>(), rcnt.data_ptr<int>(), ok.data_ptr<int>(),
|
|
587
|
+
oa.data_ptr<float>(), ob.data_ptr<float>(), (__nv_bfloat16*)y.data_ptr(), N, y.stride(0),
|
|
588
|
+
ws.data_ptr<float>(), cnt.data_ptr<int>());
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
template <int NTW, int NWARP, int DS>
|
|
592
|
+
void launchp(torch::Tensor x, torch::Tensor lof, torch::Tensor cof, torch::Tensor basef, torch::Tensor roff,
|
|
593
|
+
torch::Tensor rcnt, torch::Tensor ok, torch::Tensor oa, torch::Tensor ob, torch::Tensor y, int N, int S,
|
|
594
|
+
torch::Tensor ws, torch::Tensor cnt) {
|
|
595
|
+
int M = x.size(0), K = x.size(1);
|
|
596
|
+
int smem = NWARP * DS * ((NTW * 32 * 24 + NTW * 8 + 15) / 16 * 16);
|
|
597
|
+
auto kern = c12p_k<NTW, NWARP, DS>;
|
|
598
|
+
static bool set = false;
|
|
599
|
+
if (!set) { cudaFuncSetAttribute(kern, cudaFuncAttributeMaxDynamicSharedMemorySize, smem); set = true; }
|
|
600
|
+
dim3 grid(N / (8 * NWARP * NTW), S);
|
|
601
|
+
kern<<<grid, NWARP * 32, smem, c10::cuda::getCurrentCUDAStream()>>>(
|
|
602
|
+
(const __nv_bfloat16*)x.data_ptr(), M, K, x.stride(0), (const uint4*)lof.data_ptr(), (const uint2*)cof.data_ptr(),
|
|
603
|
+
(const uint8_t*)basef.data_ptr(), roff.data_ptr<int>(), rcnt.data_ptr<int>(), ok.data_ptr<int>(),
|
|
604
|
+
oa.data_ptr<float>(), ob.data_ptr<float>(), (__nv_bfloat16*)y.data_ptr(), N, y.stride(0),
|
|
605
|
+
ws.data_ptr<float>(), cnt.data_ptr<int>());
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
// ------------------------------------------------------------------------------------------------------------------
|
|
610
|
+
// c12s: INTRA-CTA split-K. A CTA of NG x KW warps covers NG*NTW n-tiles; the KW warps of an n-group each stream a
|
|
611
|
+
// contiguous K slice (register prefetch ring as in c12k), X for all slices is staged per round, and the KW partial
|
|
612
|
+
// accumulators are reduced in shared memory at the end -- no global split-K workspace / counters / fix-up kernel tail.
|
|
613
|
+
template <int MT, int NTW, int KW, int NG, int PD>
|
|
614
|
+
__global__ void __launch_bounds__(KW * NG * 32) c12s_k(const __nv_bfloat16* __restrict__ X, int M, int K, int sxm,
|
|
615
|
+
const uint4* __restrict__ LOF, const uint2* __restrict__ COF, const uint8_t* __restrict__ BASEF,
|
|
616
|
+
const int* __restrict__ ROFF, const int* __restrict__ RCNT, const int* __restrict__ OK,
|
|
617
|
+
const float* __restrict__ OA, const float* __restrict__ OB,
|
|
618
|
+
__nv_bfloat16* __restrict__ Y, int N, int sym) {
|
|
619
|
+
constexpr int KC = PD * 64; // columns per slice per round
|
|
620
|
+
constexpr int XS = KC + 8;
|
|
621
|
+
constexpr int XB = MT * 16 * XS; // elements per (buffer, slice)
|
|
622
|
+
extern __shared__ __align__(16) __nv_bfloat16 xs[]; // [2][KW][MT*16][XS], reused for the reduction
|
|
623
|
+
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
|
|
624
|
+
const int ng = warp / KW, kw = warp % KW;
|
|
625
|
+
const int g = lane >> 2, cq = lane & 3;
|
|
626
|
+
const int jt0 = (blockIdx.x * NG + ng) * NTW;
|
|
627
|
+
const int NKB = K >> 6, NG8 = K >> 7;
|
|
628
|
+
const int Kw = K / KW, kbeg = kw * Kw, kb_end = (kbeg + Kw) >> 6;
|
|
629
|
+
float acc[MT][NTW][4];
|
|
630
|
+
#pragma unroll
|
|
631
|
+
for (int a = 0; a < MT; a++)
|
|
632
|
+
#pragma unroll
|
|
633
|
+
for (int b = 0; b < NTW; b++)
|
|
634
|
+
#pragma unroll
|
|
635
|
+
for (int c = 0; c < 4; c++) acc[a][b][c] = 0.f;
|
|
636
|
+
uint4 L[PD][NTW];
|
|
637
|
+
uint2 C[PD][NTW];
|
|
638
|
+
uint32_t Bq[PD][NTW];
|
|
639
|
+
#pragma unroll
|
|
640
|
+
for (int s = 0; s < PD; s++)
|
|
641
|
+
#pragma unroll
|
|
642
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
643
|
+
const int kb = (kbeg >> 6) + s;
|
|
644
|
+
size_t idx = ((size_t)(jt0 + nt) * NKB + kb) * 32 + lane;
|
|
645
|
+
L[s][nt] = ldnc4(LOF + idx);
|
|
646
|
+
C[s][nt] = ldnc2(COF + idx);
|
|
647
|
+
Bq[s][nt] = BASEF[((size_t)(jt0 + nt) * NG8 + (kb >> 1)) * 8 + g];
|
|
648
|
+
}
|
|
649
|
+
auto stage = [&](int r, int buf) { // round r: X[:, slice_kw + r*KC .. +KC] for every slice kw
|
|
650
|
+
for (int i = threadIdx.x; i < KW * MT * 16 * (KC / 8); i += KW * NG * 32) {
|
|
651
|
+
const int sl = i / (MT * 16 * (KC / 8)), rem = i % (MT * 16 * (KC / 8));
|
|
652
|
+
const int row = rem / (KC / 8), c8 = (rem % (KC / 8)) * 8;
|
|
653
|
+
const int rr = row < M ? row : 0;
|
|
654
|
+
const void* src = X + (size_t)rr * sxm + sl * Kw + r * KC + c8;
|
|
655
|
+
uint32_t dst = (uint32_t)__cvta_generic_to_shared(xs + (buf * KW + sl) * XB + row * XS + c8);
|
|
656
|
+
int nb = row < M ? 16 : 0;
|
|
657
|
+
asm volatile("cp.async.cg.shared.global [%0], [%1], 16, %2;" :: "r"(dst), "l"(src), "r"(nb));
|
|
658
|
+
}
|
|
659
|
+
asm volatile("cp.async.commit_group;");
|
|
660
|
+
};
|
|
661
|
+
const int nr = Kw / KC;
|
|
662
|
+
stage(0, 0);
|
|
663
|
+
for (int r = 0; r < nr; r++) {
|
|
664
|
+
const int buf = r & 1;
|
|
665
|
+
asm volatile("cp.async.wait_group 0;");
|
|
666
|
+
__syncthreads();
|
|
667
|
+
if (r + 1 < nr) stage(r + 1, buf ^ 1);
|
|
668
|
+
const __nv_bfloat16* xb = xs + (buf * KW + kw) * XB;
|
|
669
|
+
const int kc = kbeg + r * KC;
|
|
670
|
+
#pragma unroll
|
|
671
|
+
for (int s = 0; s < PD; s++) {
|
|
672
|
+
const int kb = (kc >> 6) + s;
|
|
673
|
+
uint4 lw[NTW];
|
|
674
|
+
uint2 cw[NTW];
|
|
675
|
+
uint32_t base4[NTW];
|
|
676
|
+
#pragma unroll
|
|
677
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
678
|
+
lw[nt] = L[s][nt];
|
|
679
|
+
cw[nt] = C[s][nt];
|
|
680
|
+
base4[nt] = Bq[s][nt] * 0x01010101u;
|
|
681
|
+
if (kb + PD < kb_end) {
|
|
682
|
+
size_t idx = ((size_t)(jt0 + nt) * NKB + kb + PD) * 32 + lane;
|
|
683
|
+
L[s][nt] = ldnc4(LOF + idx);
|
|
684
|
+
C[s][nt] = ldnc2(COF + idx);
|
|
685
|
+
Bq[s][nt] = BASEF[((size_t)(jt0 + nt) * NG8 + ((kb + PD) >> 1)) * 8 + g];
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
#pragma unroll
|
|
689
|
+
for (int kt = 0; kt < 4; kt++) {
|
|
690
|
+
uint32_t a[MT][4];
|
|
691
|
+
const int kcol = s * 64 + kt * 16;
|
|
692
|
+
#pragma unroll
|
|
693
|
+
for (int mt = 0; mt < MT; mt++) {
|
|
694
|
+
const __nv_bfloat16* p = xb + (mt * 16 + (lane & 15)) * XS + kcol + (lane >> 4) * 8;
|
|
695
|
+
uint32_t sa = (uint32_t)__cvta_generic_to_shared(p);
|
|
696
|
+
asm("ldmatrix.sync.aligned.m8n8.x4.shared.b16 {%0,%1,%2,%3}, [%4];"
|
|
697
|
+
: "=r"(a[mt][0]), "=r"(a[mt][1]), "=r"(a[mt][2]), "=r"(a[mt][3]) : "r"(sa));
|
|
698
|
+
}
|
|
699
|
+
#pragma unroll
|
|
700
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
701
|
+
uint32_t lo = (kt == 0) ? lw[nt].x : (kt == 1) ? lw[nt].y : (kt == 2) ? lw[nt].z : lw[nt].w;
|
|
702
|
+
uint32_t cwrd = (kt < 2) ? cw[nt].x : cw[nt].y;
|
|
703
|
+
uint32_t c = ((kt & 1) ? (cwrd >> 4) : cwrd) & 0x0F0F0F0Fu;
|
|
704
|
+
uint32_t b0, b1;
|
|
705
|
+
dec(lo, c, base4[nt], b0, b1);
|
|
706
|
+
#pragma unroll
|
|
707
|
+
for (int mt = 0; mt < MT; mt++) mma(acc[mt][nt], a[mt][0], a[mt][1], a[mt][2], a[mt][3], b0, b1);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
// intra-CTA reduction over the KW slices (fixed order kw = 0..KW-1 -> deterministic)
|
|
713
|
+
__syncthreads();
|
|
714
|
+
float* red = reinterpret_cast<float*>(xs); // [NG][KW][MT][NTW][4][32]
|
|
715
|
+
constexpr int PER = MT * NTW * 4 * 32;
|
|
716
|
+
#pragma unroll
|
|
717
|
+
for (int mt = 0; mt < MT; mt++)
|
|
718
|
+
#pragma unroll
|
|
719
|
+
for (int nt = 0; nt < NTW; nt++)
|
|
720
|
+
#pragma unroll
|
|
721
|
+
for (int c = 0; c < 4; c++) red[((ng * KW + kw) * PER) + ((mt * NTW + nt) * 4 + c) * 32 + lane] = acc[mt][nt][c];
|
|
722
|
+
__syncthreads();
|
|
723
|
+
if (kw != 0) return;
|
|
724
|
+
#pragma unroll
|
|
725
|
+
for (int mt = 0; mt < MT; mt++)
|
|
726
|
+
#pragma unroll
|
|
727
|
+
for (int nt = 0; nt < NTW; nt++)
|
|
728
|
+
#pragma unroll
|
|
729
|
+
for (int c = 0; c < 4; c++) {
|
|
730
|
+
float v = 0.f;
|
|
731
|
+
#pragma unroll
|
|
732
|
+
for (int q = 0; q < KW; q++) v += red[((ng * KW + q) * PER) + ((mt * NTW + nt) * 4 + c) * 32 + lane];
|
|
733
|
+
acc[mt][nt][c] = v;
|
|
734
|
+
}
|
|
735
|
+
#pragma unroll
|
|
736
|
+
for (int nt = 0; nt < NTW; nt++) {
|
|
737
|
+
#pragma unroll
|
|
738
|
+
for (int j = 0; j < 2; j++) {
|
|
739
|
+
const int n = (jt0 + nt) * 8 + 2 * cq + j;
|
|
740
|
+
const int ro = ROFF[n], rc = RCNT[n];
|
|
741
|
+
for (int o = 0; o < rc; o++) {
|
|
742
|
+
const int k = OK[ro + o];
|
|
743
|
+
const float fa = OA[ro + o], fb = OB[ro + o];
|
|
744
|
+
#pragma unroll
|
|
745
|
+
for (int mt = 0; mt < MT; mt++)
|
|
746
|
+
#pragma unroll
|
|
747
|
+
for (int h = 0; h < 2; h++) {
|
|
748
|
+
const int m = mt * 16 + g + 8 * h;
|
|
749
|
+
if (m < M) {
|
|
750
|
+
float xv = __bfloat162float(X[(size_t)m * sxm + k]);
|
|
751
|
+
acc[mt][nt][2 * h + j] += xv * fa;
|
|
752
|
+
acc[mt][nt][2 * h + j] += xv * fb;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
#pragma unroll
|
|
758
|
+
for (int mt = 0; mt < MT; mt++)
|
|
759
|
+
#pragma unroll
|
|
760
|
+
for (int h = 0; h < 2; h++) {
|
|
761
|
+
const int m = mt * 16 + g + 8 * h, n = (jt0 + nt) * 8 + 2 * cq;
|
|
762
|
+
if (m < M)
|
|
763
|
+
*reinterpret_cast<__nv_bfloat162*>(Y + (size_t)m * sym + n) =
|
|
764
|
+
__floats2bfloat162_rn(acc[mt][nt][2 * h], acc[mt][nt][2 * h + 1]);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
template <int MT, int NTW, int KW, int NG, int PD>
|
|
770
|
+
void launchs(torch::Tensor x, torch::Tensor lof, torch::Tensor cof, torch::Tensor basef, torch::Tensor roff,
|
|
771
|
+
torch::Tensor rcnt, torch::Tensor ok, torch::Tensor oa, torch::Tensor ob, torch::Tensor y, int N) {
|
|
772
|
+
int M = x.size(0), K = x.size(1);
|
|
773
|
+
TORCH_CHECK(K % (KW * PD * 64) == 0, "c12s: K must split into KW slices of whole rounds");
|
|
774
|
+
int smem_x = 2 * KW * MT * 16 * (PD * 64 + 8) * 2;
|
|
775
|
+
int smem_r = NG * KW * MT * NTW * 4 * 32 * 4;
|
|
776
|
+
int smem = smem_x > smem_r ? smem_x : smem_r;
|
|
777
|
+
TORCH_CHECK(smem <= 200 * 1024, "c12s: shared memory too large for this config");
|
|
778
|
+
auto kern = c12s_k<MT, NTW, KW, NG, PD>;
|
|
779
|
+
static bool set = false;
|
|
780
|
+
if (!set) { cudaFuncSetAttribute(kern, cudaFuncAttributeMaxDynamicSharedMemorySize, 200 * 1024); set = true; }
|
|
781
|
+
dim3 grid(N / (8 * NG * NTW));
|
|
782
|
+
kern<<<grid, KW * NG * 32, smem, c10::cuda::getCurrentCUDAStream()>>>(
|
|
783
|
+
(const __nv_bfloat16*)x.data_ptr(), M, K, x.stride(0), (const uint4*)lof.data_ptr(), (const uint2*)cof.data_ptr(),
|
|
784
|
+
(const uint8_t*)basef.data_ptr(), roff.data_ptr<int>(), rcnt.data_ptr<int>(), ok.data_ptr<int>(),
|
|
785
|
+
oa.data_ptr<float>(), ob.data_ptr<float>(), (__nv_bfloat16*)y.data_ptr(), N, y.stride(0));
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
torch::Tensor c12gemm(torch::Tensor x, torch::Tensor lof, torch::Tensor cof, torch::Tensor basef, torch::Tensor roff,
|
|
789
|
+
torch::Tensor rcnt, torch::Tensor ok, torch::Tensor oa, torch::Tensor ob, int64_t maxc, int64_t N,
|
|
790
|
+
int64_t ntw, c10::optional<torch::Tensor> res, int64_t S, torch::Tensor ws, torch::Tensor cnt) {
|
|
791
|
+
int M = x.size(0);
|
|
792
|
+
auto y = torch::empty({M, N}, x.options());
|
|
793
|
+
int MT = (M + 15) / 16;
|
|
794
|
+
bool hr = res.has_value();
|
|
795
|
+
torch::Tensor r = hr ? res.value() : y;
|
|
796
|
+
const int dbg = (int)(ntw / 100000);
|
|
797
|
+
ntw = ntw % 100000;
|
|
798
|
+
#define L(mt, nw, w, pd) launch<mt, nw, w, pd>(x, lof, cof, basef, roff, rcnt, ok, oa, ob, (int)maxc, y, (int)N, r, hr, (int)S, ws, cnt, dbg)
|
|
799
|
+
#define LM(nw, w, pd) { if (MT == 1) L(1, nw, w, pd); else if (MT == 2) L(2, nw, w, pd); else if (MT == 3) L(3, nw, w, pd); else L(4, nw, w, pd); }
|
|
800
|
+
if (ntw >= 5000) { // c12s intra-CTA split-K: 5000 + 100 * kw_code + 10 * ng_code + pd_code (S must be 1)
|
|
801
|
+
// kw_code: 0 -> KW=2, 1 -> 4, 2 -> 8; ng_code: 0 -> NG=1, 1 -> 2, 2 -> 4; pd_code: 0 -> PD=4, 1 -> PD=8
|
|
802
|
+
int kc = (ntw / 100) % 10, gc = (ntw / 10) % 10, pc = ntw % 10;
|
|
803
|
+
TORCH_CHECK(S == 1 && MT <= 2);
|
|
804
|
+
#define LS(mt, kw, ngp, pd) launchs<mt, 1, kw, ngp, pd>(x, lof, cof, basef, roff, rcnt, ok, oa, ob, y, (int)N)
|
|
805
|
+
#define LSM(kw, ngp, pd) { if (MT == 1) LS(1, kw, ngp, pd); else LS(2, kw, ngp, pd); }
|
|
806
|
+
#define LSG(kw, pd) { if (gc == 0) LSM(kw, 1, pd) else if (gc == 1) LSM(kw, 2, pd) else LSM(kw, 4, pd) }
|
|
807
|
+
if (kc == 0) { if (pc) LSG(2, 8) else LSG(2, 4) }
|
|
808
|
+
else if (kc == 1) { if (pc) LSG(4, 8) else LSG(4, 4) }
|
|
809
|
+
else { if (pc) LSG(8, 8) else LSG(8, 4) }
|
|
810
|
+
return y;
|
|
811
|
+
}
|
|
812
|
+
if (ntw >= 3000) { // c12q (M <= 64, MTP = ceil(M/16) m16 tiles): 3000 + 100 * ds_code + 10 * (nwarp == 8) + ntw
|
|
813
|
+
int nw = ntw % 10, w8 = (ntw / 10) % 10, dc = (ntw / 100) % 10;
|
|
814
|
+
TORCH_CHECK(MT <= 4);
|
|
815
|
+
#define LQ(a, b, c) { if (MT == 1) launchq<a, b, c, 1>(x, lof, cof, basef, roff, rcnt, ok, oa, ob, y, (int)N, (int)S, ws, cnt); \
|
|
816
|
+
else if (MT == 2) launchq<a, b, c, 2>(x, lof, cof, basef, roff, rcnt, ok, oa, ob, y, (int)N, (int)S, ws, cnt); \
|
|
817
|
+
else if (MT == 3) launchq<a, b, c, 3>(x, lof, cof, basef, roff, rcnt, ok, oa, ob, y, (int)N, (int)S, ws, cnt); \
|
|
818
|
+
else launchq<a, b, c, 4>(x, lof, cof, basef, roff, rcnt, ok, oa, ob, y, (int)N, (int)S, ws, cnt); }
|
|
819
|
+
if (w8) { if (nw == 2) LQ(2, 8, 8) else if (dc == 0) LQ(1, 8, 8) else LQ(1, 8, 16) }
|
|
820
|
+
else { if (nw == 2) LQ(2, 4, 8) else if (dc == 0) LQ(1, 4, 8) else LQ(1, 4, 16) }
|
|
821
|
+
return y;
|
|
822
|
+
}
|
|
823
|
+
if (ntw >= 1000) { // c12p (M <= 16): 1000 + 100 * ds_code + 10 * (nwarp == 8) + ntw; ds_code 0: 8, 1: 16, 2: 24
|
|
824
|
+
TORCH_CHECK(M <= 16, "c12p needs M <= 16");
|
|
825
|
+
int nw = ntw % 10, w8 = (ntw / 10) % 10, dc = (ntw / 100) % 10;
|
|
826
|
+
#define LP(a, b, c) launchp<a, b, c>(x, lof, cof, basef, roff, rcnt, ok, oa, ob, y, (int)N, (int)S, ws, cnt)
|
|
827
|
+
if (w8) { if (nw == 2) { if (dc == 0) LP(2, 8, 8); else LP(2, 8, 16); }
|
|
828
|
+
else { if (dc == 0) LP(1, 8, 8); else if (dc == 1) LP(1, 8, 16); else LP(1, 8, 24); } }
|
|
829
|
+
else { if (nw == 2) { if (dc == 0) LP(2, 4, 8); else LP(2, 4, 16); }
|
|
830
|
+
else { if (dc == 0) LP(1, 4, 8); else if (dc == 1) LP(1, 4, 16); else LP(1, 4, 24); } }
|
|
831
|
+
return y;
|
|
832
|
+
}
|
|
833
|
+
// variant code: ntw + 10 * (nwarp == 8) + 100 * (pd == 4)
|
|
834
|
+
int nw = ntw % 10, w8 = (ntw / 10) % 10, p4 = (ntw / 100) % 10;
|
|
835
|
+
if (nw == 1 && !w8 && !p4) LM(1, 4, 8) else if (nw == 2 && !w8 && !p4) LM(2, 4, 8)
|
|
836
|
+
else if (nw == 1 && w8 && !p4) LM(1, 8, 8) else if (nw == 2 && w8 && !p4) LM(2, 8, 8)
|
|
837
|
+
else if (nw == 1 && !w8 && p4) LM(1, 4, 4) else if (nw == 2 && !w8 && p4) LM(2, 4, 4)
|
|
838
|
+
else if (nw == 1 && w8 && p4) LM(1, 8, 4) else if (nw == 2 && w8 && p4) LM(2, 8, 4)
|
|
839
|
+
else if (nw == 4 && !w8) LM(4, 4, 4) else LM(4, 8, 4)
|
|
840
|
+
return y;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { m.def("c12gemm", &c12gemm); }
|
|
844
|
+
"""
|
|
845
|
+
|
|
846
|
+
_EXT = None
|
|
847
|
+
WS = None
|
|
848
|
+
CNT = None
|
|
849
|
+
SPLIT = {} # (N, K, MT, ntw) -> S
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
def ext():
|
|
853
|
+
global _EXT, WS, CNT
|
|
854
|
+
if WS is None and torch.cuda.is_available():
|
|
855
|
+
WS = torch.zeros(16 * 1024 * 1024, dtype=torch.float32, device=DEV) # split-K partials (64 MB scratch)
|
|
856
|
+
CNT = torch.zeros(16384, dtype=torch.int32, device=DEV) # self-resetting tile counters
|
|
857
|
+
if _EXT is None:
|
|
858
|
+
from torch.utils.cpp_extension import load_inline
|
|
859
|
+
os.makedirs("/tmp/ext", exist_ok=True)
|
|
860
|
+
os.environ["TORCH_CUDA_ARCH_LIST"] = "9.0a" # wgmma / setmaxnreg need sm_90a, not plain sm_90
|
|
861
|
+
_EXT = load_inline("c12cu_ext", cpp_sources="", cuda_sources=SRC, functions=None, build_directory="/tmp/ext",
|
|
862
|
+
extra_cuda_cflags=["-O3", "-arch=sm_90a", "--use_fast_math", "-lineinfo", "-Xptxas=-v"],
|
|
863
|
+
verbose=os.environ.get("C12_VERBOSE") == "1")
|
|
864
|
+
return _EXT
|
|
865
|
+
|
|
866
|
+
|
|
867
|
+
_IDX = {}
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
def frag_layout(C):
|
|
871
|
+
"""C12 planes (c12.encode) -> fragment-order planes for the CUDA kernel."""
|
|
872
|
+
N, K = C["N"], C["K"]
|
|
873
|
+
assert N % 8 == 0 and K % 512 == 0
|
|
874
|
+
dev = C["lo"].device
|
|
875
|
+
key = (N, K)
|
|
876
|
+
lo = C["lo"]
|
|
877
|
+
exv = C["ex"].view(N, K // 128, 64)
|
|
878
|
+
code = torch.stack([exv & 15, exv >> 4], 2).reshape(N, K) # (N, K) nibble per weight
|
|
879
|
+
t = torch.arange(32, device=dev)
|
|
880
|
+
g, cq = t // 4, t % 4
|
|
881
|
+
kt = torch.arange(4, device=dev)
|
|
882
|
+
i4 = torch.tensor([0, 1, 8, 9], device=dev)
|
|
883
|
+
# k offset inside a 64-col block for (lane, kt, i): 16 kt + 2 cq + i4
|
|
884
|
+
koff = (16 * kt[None, :, None] + 2 * cq[:, None, None] + i4[None, None, :]) # (32, 4, 4)
|
|
885
|
+
nb, kb = N // 8, K // 64
|
|
886
|
+
rows = (torch.arange(nb, device=dev)[:, None, None, None, None] * 8 + g[None, None, :, None, None])
|
|
887
|
+
cols = (torch.arange(kb, device=dev)[None, :, None, None, None] * 64 + koff[None, None])
|
|
888
|
+
rows = rows.expand(nb, kb, 32, 4, 4)
|
|
889
|
+
cols = cols.expand(nb, kb, 32, 4, 4)
|
|
890
|
+
lof = lo[rows, cols].reshape(nb, kb, 32, 16).contiguous()
|
|
891
|
+
cd = code[rows, cols] # (nb, kb, 32, kt, i)
|
|
892
|
+
cof = (cd[:, :, :, 0::2, :] | (cd[:, :, :, 1::2, :] << 4)).to(torch.uint8).reshape(nb, kb, 32, 8).contiguous()
|
|
893
|
+
basef = C["base"].view(nb, 8, K // 128).transpose(1, 2).contiguous() # (nb, NG, 8)
|
|
894
|
+
F = dict(lof=lof.view(-1), cof=cof.view(-1), basef=basef.view(-1), roff=C["roff"], rcnt=C["rcnt"], ok=C["ok"],
|
|
895
|
+
oa=C["oa"], ob=C["ob"], maxc=C["maxc"], N=N, K=K, frag=True)
|
|
896
|
+
return F
|
|
897
|
+
|
|
898
|
+
|
|
899
|
+
def encode_frag(w, rows=4096):
|
|
900
|
+
"""bf16 (N, K) -> fragment-order C12 (chunked over rows to bound temporaries)."""
|
|
901
|
+
parts = []
|
|
902
|
+
for i in range(0, w.shape[0], rows):
|
|
903
|
+
parts.append(frag_layout(c12.encode(w[i:i + rows])))
|
|
904
|
+
if len(parts) == 1:
|
|
905
|
+
return parts[0]
|
|
906
|
+
F = dict(N=w.shape[0], K=w.shape[1], frag=True)
|
|
907
|
+
for k in ("lof", "cof", "basef", "rcnt"):
|
|
908
|
+
F[k] = torch.cat([p[k] for p in parts]).contiguous()
|
|
909
|
+
tot, ok, oa, ob, roff = 0, [], [], [], []
|
|
910
|
+
for p in parts:
|
|
911
|
+
n_o = int(p["rcnt"].sum())
|
|
912
|
+
roff.append(p["roff"] + tot)
|
|
913
|
+
ok.append(p["ok"][:n_o])
|
|
914
|
+
oa.append(p["oa"][:n_o])
|
|
915
|
+
ob.append(p["ob"][:n_o])
|
|
916
|
+
tot += n_o
|
|
917
|
+
F["roff"] = torch.cat(roff).to(torch.int32).contiguous()
|
|
918
|
+
z = lambda dt: torch.zeros(1, dtype=dt, device=w.device) # noqa: E731
|
|
919
|
+
F["ok"] = torch.cat(ok).contiguous() if tot else z(torch.int32)
|
|
920
|
+
F["oa"] = torch.cat(oa).contiguous() if tot else z(torch.float32)
|
|
921
|
+
F["ob"] = torch.cat(ob).contiguous() if tot else z(torch.float32)
|
|
922
|
+
F["maxc"] = int(F["rcnt"].max())
|
|
923
|
+
return F
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
def nbytes(F):
|
|
927
|
+
return sum(F[k].numel() * F[k].element_size() for k in ("lof", "cof", "basef", "roff", "rcnt", "ok", "oa", "ob"))
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
def pick_split(N, K, M, ntw):
|
|
931
|
+
key = (N, K, 0 if M == 1 else (M + 15) // 16, ntw)
|
|
932
|
+
if key in SPLIT:
|
|
933
|
+
return SPLIT[key]
|
|
934
|
+
if ntw >= 5000:
|
|
935
|
+
return 1
|
|
936
|
+
nwarp = 8 if (ntw // 10) % 10 else 4
|
|
937
|
+
G = N // (8 * nwarp * (ntw % 10))
|
|
938
|
+
best = 1
|
|
939
|
+
for sp in (1, 2, 3, 4, 6, 8):
|
|
940
|
+
if K % (512 * sp) == 0 and G * sp <= 1056:
|
|
941
|
+
best = sp
|
|
942
|
+
if G * sp >= 528:
|
|
943
|
+
break
|
|
944
|
+
return best
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
def mm(x, F, ntw=2, res=None, split=None):
|
|
948
|
+
x = x.contiguous()
|
|
949
|
+
e = ext()
|
|
950
|
+
S = split or pick_split(F["N"], F["K"], x.shape[0], ntw)
|
|
951
|
+
assert S * ((x.shape[0] + 15) // 16) * 16 * F["N"] <= WS.numel()
|
|
952
|
+
return e.c12gemm(x, F["lof"], F["cof"], F["basef"], F["roff"], F["rcnt"], F["ok"], F["oa"], F["ob"],
|
|
953
|
+
F["maxc"], F["N"], ntw, res, S, WS, CNT)
|
|
954
|
+
|
|
955
|
+
|
|
956
|
+
@torch.library.custom_op("o3::c12cu", mutates_args=())
|
|
957
|
+
def c12cu_op(x: torch.Tensor, lof: torch.Tensor, cof: torch.Tensor, basef: torch.Tensor, roff: torch.Tensor,
|
|
958
|
+
rcnt: torch.Tensor, ok: torch.Tensor, oa: torch.Tensor, ob: torch.Tensor, maxc: int, N: int,
|
|
959
|
+
ntw: int) -> torch.Tensor:
|
|
960
|
+
e = ext()
|
|
961
|
+
S = pick_split(N, x.shape[1], x.shape[0], ntw)
|
|
962
|
+
return e.c12gemm(x.contiguous(), lof, cof, basef, roff, rcnt, ok, oa, ob, maxc, N, ntw, None, S, WS, CNT)
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
@c12cu_op.register_fake
|
|
966
|
+
def _c12cu_fake(x, lof, cof, basef, roff, rcnt, ok, oa, ob, maxc, N, ntw):
|
|
967
|
+
return x.new_empty(x.shape[0], N)
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
NTW = {} # (N, K, MT) -> ntw
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
def lin(x, F):
|
|
974
|
+
x2 = x.reshape(-1, x.shape[-1])
|
|
975
|
+
M = x2.shape[0]
|
|
976
|
+
if M > 64:
|
|
977
|
+
return torch.nn.functional.linear(x, decode_frag(F))
|
|
978
|
+
ntw = NTW.get((F["N"], F["K"], 0 if M == 1 else (M + 15) // 16), 2 if F["N"] % 64 == 0 else 1)
|
|
979
|
+
y = c12cu_op(x2, F["lof"], F["cof"], F["basef"], F["roff"], F["rcnt"], F["ok"], F["oa"], F["ob"], F["maxc"],
|
|
980
|
+
F["N"], ntw)
|
|
981
|
+
return y.view(*x.shape[:-1], F["N"])
|
|
982
|
+
|
|
983
|
+
|
|
984
|
+
def decode_frag(F):
|
|
985
|
+
"""Exact bf16 weights from fragment-order planes (prefill of long prompts, checks)."""
|
|
986
|
+
N, K = F["N"], F["K"]
|
|
987
|
+
dev = F["lof"].device
|
|
988
|
+
t = torch.arange(32, device=dev)
|
|
989
|
+
g, cq = t // 4, t % 4
|
|
990
|
+
kt = torch.arange(4, device=dev)
|
|
991
|
+
i4 = torch.tensor([0, 1, 8, 9], device=dev)
|
|
992
|
+
koff = (16 * kt[None, :, None] + 2 * cq[:, None, None] + i4[None, None, :])
|
|
993
|
+
nb, kb = N // 8, K // 64
|
|
994
|
+
rows = (torch.arange(nb, device=dev)[:, None, None, None, None] * 8 + g[None, None, :, None, None])
|
|
995
|
+
cols = (torch.arange(kb, device=dev)[None, :, None, None, None] * 64 + koff[None, None])
|
|
996
|
+
rows = rows.expand(nb, kb, 32, 4, 4).reshape(-1)
|
|
997
|
+
cols = cols.expand(nb, kb, 32, 4, 4).reshape(-1)
|
|
998
|
+
lo = torch.empty(N, K, dtype=torch.int32, device=dev)
|
|
999
|
+
lo[rows, cols] = F["lof"].view(-1).to(torch.int32)
|
|
1000
|
+
cw = F["cof"].view(nb, kb, 32, 2, 4).to(torch.int32)
|
|
1001
|
+
cd = torch.stack([cw & 15, cw >> 4], 4).reshape(nb, kb, 32, 4, 4) # (e, j, i) -> (kt = 2e + j, i) [S27 fix: was dim 3]
|
|
1002
|
+
code = torch.empty(N, K, dtype=torch.int32, device=dev)
|
|
1003
|
+
code[rows, cols] = cd.reshape(-1)
|
|
1004
|
+
base = F["basef"].view(nb, K // 128, 8).transpose(1, 2).reshape(N, K // 128).to(torch.int32)
|
|
1005
|
+
bb = base.repeat_interleave(128, 1)
|
|
1006
|
+
hi = ((code & 7) + bb) | ((code & 8) << 4)
|
|
1007
|
+
bits = (hi << 8) | lo
|
|
1008
|
+
w = (bits - ((bits >> 15) << 16)).to(torch.int16).view(BF).clone()
|
|
1009
|
+
if int(F["rcnt"].sum()) > 0:
|
|
1010
|
+
r = torch.repeat_interleave(torch.arange(N, device=dev), F["rcnt"].long())
|
|
1011
|
+
w[r, F["ok"].long()] = F["ob"].to(BF)
|
|
1012
|
+
return w
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
VARIANTS = (1, 2, 101, 102, 111, 104, 114, 5100, 5110, 5120, 5111, 5210) # c12p/c12q never won (res_c12p8*.json)
|
|
1016
|
+
TUNE_LOG = {}
|
|
1017
|
+
|
|
1018
|
+
|
|
1019
|
+
def autotune(mats, Ms, reps=8):
|
|
1020
|
+
"""mats: {name: [F, ...]} (fragment-order C12, all layers of one shape); picks (variant, split) per (N, K, MT) by
|
|
1021
|
+
timing the whole 36-layer stream in a CUDA graph (kernel-config tuning only; outputs are identical up to fp32
|
|
1022
|
+
accumulation order)."""
|
|
1023
|
+
import time
|
|
1024
|
+
ext()
|
|
1025
|
+
for nm, Fs in mats.items():
|
|
1026
|
+
N, K = Fs[0]["N"], Fs[0]["K"]
|
|
1027
|
+
for M in Ms:
|
|
1028
|
+
MT = (M + 15) // 16
|
|
1029
|
+
x = torch.randn(M, K, device=DEV, dtype=BF)
|
|
1030
|
+
ref = mm(x, Fs[0], ntw=1, split=1).float() # known-good variant as the correctness reference
|
|
1031
|
+
tol = 4 * float(ref.abs().max()) * 2 ** -8 + 1e-3
|
|
1032
|
+
best = None
|
|
1033
|
+
for v in VARIANTS:
|
|
1034
|
+
if v >= 5000:
|
|
1035
|
+
kwv, ngv, pdv = (2, 4, 8)[(v // 100) % 10], (1, 2, 4)[(v // 10) % 10], (4, 8)[v % 10]
|
|
1036
|
+
if N % (8 * ngv) or MT > 2 or K % (kwv * pdv * 64):
|
|
1037
|
+
continue
|
|
1038
|
+
else:
|
|
1039
|
+
nwarp = 8 if (v // 10) % 10 else 4
|
|
1040
|
+
if N % (8 * nwarp * (v % 10)):
|
|
1041
|
+
continue
|
|
1042
|
+
if 1000 <= v < 3000 and MT > 1:
|
|
1043
|
+
continue
|
|
1044
|
+
for sp in (1, 2, 4, 8):
|
|
1045
|
+
if K % (512 * sp) or sp * MT * 16 * N > WS.numel() or (v >= 5000 and sp > 1):
|
|
1046
|
+
continue
|
|
1047
|
+
fn = lambda: [mm(x, f, ntw=v, split=sp) for f in Fs] # noqa: E731
|
|
1048
|
+
try:
|
|
1049
|
+
if float((mm(x, Fs[0], ntw=v, split=sp).float() - ref).abs().max()) > tol:
|
|
1050
|
+
TUNE_LOG[f"{nm}_M{M}_v{v}_s{sp}_WRONG"] = True
|
|
1051
|
+
continue
|
|
1052
|
+
fn()
|
|
1053
|
+
torch.cuda.synchronize()
|
|
1054
|
+
g = torch.cuda.CUDAGraph()
|
|
1055
|
+
with torch.cuda.graph(g):
|
|
1056
|
+
fn()
|
|
1057
|
+
g.replay()
|
|
1058
|
+
torch.cuda.synchronize()
|
|
1059
|
+
t = time.perf_counter()
|
|
1060
|
+
for _ in range(reps):
|
|
1061
|
+
g.replay()
|
|
1062
|
+
torch.cuda.synchronize()
|
|
1063
|
+
ms = (time.perf_counter() - t) / reps * 1e3
|
|
1064
|
+
del g
|
|
1065
|
+
except Exception: # noqa
|
|
1066
|
+
continue
|
|
1067
|
+
if best is None or ms < best[0]:
|
|
1068
|
+
best = (ms, v, sp)
|
|
1069
|
+
mk = 0 if M == 1 else MT # plain decode (1 row) tuned separately from small trees
|
|
1070
|
+
NTW[(N, K, mk)] = best[1]
|
|
1071
|
+
SPLIT[(N, K, mk, best[1])] = best[2]
|
|
1072
|
+
TUNE_LOG[f"{nm}_M{M}"] = dict(ms=best[0], variant=best[1], split=best[2])
|
|
1073
|
+
print("autotune", nm, M, TUNE_LOG[f"{nm}_M{M}"], flush=True)
|
|
1074
|
+
return TUNE_LOG
|