quantuminfer 0.0.0-stage → 0.1.0
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 +10 -4
- package/results/ANALYSIS.md +156 -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_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/c12.py
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
# COPIED UNCHANGED from gpu/exp_o3_latency (lane O3, round 6); used by wcode.py as the lossless weight-code feature.
|
|
2
|
+
"""C12: a lossless 12-bit weight format read directly by the verify GEMM (lane O3).
|
|
3
|
+
|
|
4
|
+
bf16 bits = [sign | e7..e1 | e0 m6..m0] = hi byte | lo byte. The lo byte is stored raw (8 bits). The hi byte = sign
|
|
5
|
+
plus the top 7 exponent bits; inside a (row, 128-column) group the top exponent bits span only a few values (the
|
|
6
|
+
exponent carries ~2.7 bits of entropy, lane R1), so hi = sign<<7 | (base + c) with a 3-bit offset c: 4 bits per weight.
|
|
7
|
+
Total 8 + 4 + 8/128 = 12.06 bits (75.4% of bf16). Weights whose top exponent bits fall below the 8-value window
|
|
8
|
+
(|w| < ~2^-14 of the group max; 0.016% of Qwen3-8B's weights) are OUTLIERS: their slot decodes to v = 2^(2 base - 127)
|
|
9
|
+
and the GEMM epilogue adds x*(-v) + x*w_true from a per-row list (both products exact in fp32). So every output is the
|
|
10
|
+
fp32 accumulation of exact products of x with the TRUE bf16 weights (lossless up to accumulation order, like any GEMM
|
|
11
|
+
kernel choice).
|
|
12
|
+
|
|
13
|
+
Decode is SWAR inline PTX: 4 weights per 32-bit register, ~2 instructions per weight (and/add/shl/or + 2 prmt), right
|
|
14
|
+
before the tensor-core dot. No extra memory: the C12 planes REPLACE the bf16 weights (12 GB instead of 16 GB for 8B)."""
|
|
15
|
+
import torch
|
|
16
|
+
import triton
|
|
17
|
+
import triton.language as tl
|
|
18
|
+
|
|
19
|
+
DEV = "cuda"
|
|
20
|
+
BF = torch.bfloat16
|
|
21
|
+
G = 128
|
|
22
|
+
H = 64
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def encode(w):
|
|
26
|
+
"""w (N, K) bf16 -> dict(lo (N,K) u8, ex (N,K/2) u8, base (N,K/128) u8, roff/rcnt (N,) i32, ok (n_out,) i32,
|
|
27
|
+
oa/ob (n_out,) f32, maxc int)."""
|
|
28
|
+
N, K = w.shape
|
|
29
|
+
assert K % G == 0
|
|
30
|
+
b = w.contiguous().view(torch.int16).to(torch.int32) & 0xFFFF
|
|
31
|
+
lo = (b & 0xFF).to(torch.uint8)
|
|
32
|
+
hi = b >> 8
|
|
33
|
+
sign = hi >> 7
|
|
34
|
+
eh = hi & 0x7F
|
|
35
|
+
base = (eh.view(N, K // G, G).amax(-1) - 7).clamp(min=0) # (N, K/G)
|
|
36
|
+
bb = base.repeat_interleave(G, 1)
|
|
37
|
+
off = eh - bb
|
|
38
|
+
out = off < 0
|
|
39
|
+
code = torch.where(out, torch.zeros_like(off), (sign << 3) | off)
|
|
40
|
+
lo = torch.where(out, torch.zeros_like(lo), lo)
|
|
41
|
+
cv = code.view(N, K // G, 2, H)
|
|
42
|
+
ex = (cv[:, :, 0] | (cv[:, :, 1] << 4)).reshape(N, K // 2).to(torch.uint8)
|
|
43
|
+
# outliers: slot decodes to v = bits(base << 8); correction x*(-v) + x*w
|
|
44
|
+
r, k = out.nonzero(as_tuple=True)
|
|
45
|
+
vbits = (bb[r, k] << 8)
|
|
46
|
+
v = (vbits - ((vbits >> 15) << 16)).to(torch.int16).view(BF).float()
|
|
47
|
+
wt = w[r, k].float()
|
|
48
|
+
cnt = torch.bincount(r, minlength=N).to(torch.int32)
|
|
49
|
+
roff = (torch.cumsum(cnt, 0) - cnt).to(torch.int32)
|
|
50
|
+
order = torch.argsort(r, stable=True)
|
|
51
|
+
C = dict(lo=lo.contiguous(), ex=ex.contiguous(), base=base.to(torch.uint8).contiguous(), roff=roff, rcnt=cnt,
|
|
52
|
+
ok=k[order].to(torch.int32).contiguous(), oa=(-v[order]).contiguous(), ob=wt[order].contiguous(),
|
|
53
|
+
maxc=int(cnt.max()) if N else 0, N=N, K=K)
|
|
54
|
+
if C["ok"].numel() == 0: # keep valid pointers
|
|
55
|
+
C["ok"] = torch.zeros(1, dtype=torch.int32, device=w.device)
|
|
56
|
+
C["oa"] = torch.zeros(1, dtype=torch.float32, device=w.device)
|
|
57
|
+
C["ob"] = torch.zeros(1, dtype=torch.float32, device=w.device)
|
|
58
|
+
return C
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def decode(C):
|
|
62
|
+
"""Reference decode to bf16 (for checks and for prefill of long prompts)."""
|
|
63
|
+
N, K = C["N"], C["K"]
|
|
64
|
+
lo = C["lo"].to(torch.int32)
|
|
65
|
+
exv = C["ex"].to(torch.int32).view(N, K // G, H)
|
|
66
|
+
code = torch.stack([exv & 15, exv >> 4], 2).reshape(N, K)
|
|
67
|
+
bb = C["base"].to(torch.int32).repeat_interleave(G, 1)
|
|
68
|
+
hi = ((code & 7) + bb) | ((code & 8) << 4)
|
|
69
|
+
bits = (hi << 8) | lo
|
|
70
|
+
w = (bits - ((bits >> 15) << 16)).to(torch.int16).view(BF).clone()
|
|
71
|
+
if C["rcnt"].sum() > 0:
|
|
72
|
+
r = torch.repeat_interleave(torch.arange(N, device=lo.device), C["rcnt"].long())
|
|
73
|
+
w[r, C["ok"].long()] = C["ob"].to(BF)
|
|
74
|
+
return w
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def nbytes(C):
|
|
78
|
+
return sum(C[k].numel() * C[k].element_size() for k in ("lo", "ex", "base", "roff", "rcnt", "ok", "oa", "ob"))
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
_ASM_LO = """
|
|
82
|
+
{
|
|
83
|
+
.reg .b32 c, t, s, h;
|
|
84
|
+
and.b32 c, $3, 0x0F0F0F0F;
|
|
85
|
+
and.b32 t, c, 0x07070707;
|
|
86
|
+
add.u32 t, t, $4;
|
|
87
|
+
and.b32 s, c, 0x08080808;
|
|
88
|
+
shl.b32 s, s, 4;
|
|
89
|
+
or.b32 h, t, s;
|
|
90
|
+
prmt.b32 $0, $2, h, 0x5140;
|
|
91
|
+
prmt.b32 $1, $2, h, 0x7362;
|
|
92
|
+
}
|
|
93
|
+
"""
|
|
94
|
+
_ASM_HI = """
|
|
95
|
+
{
|
|
96
|
+
.reg .b32 c, t, s, h;
|
|
97
|
+
shr.b32 c, $3, 4;
|
|
98
|
+
and.b32 c, c, 0x0F0F0F0F;
|
|
99
|
+
and.b32 t, c, 0x07070707;
|
|
100
|
+
add.u32 t, t, $4;
|
|
101
|
+
and.b32 s, c, 0x08080808;
|
|
102
|
+
shl.b32 s, s, 4;
|
|
103
|
+
or.b32 h, t, s;
|
|
104
|
+
prmt.b32 $0, $2, h, 0x5140;
|
|
105
|
+
prmt.b32 $1, $2, h, 0x7362;
|
|
106
|
+
}
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@triton.jit
|
|
111
|
+
def _c12_kernel(X, LO, EX, BASE, ROFF, RCNT, OK, OA, OB, Y, RES, M, N, K, sxm, sym, srm, KSPL, MAXC,
|
|
112
|
+
BM: tl.constexpr, BN: tl.constexpr, ATOMIC: tl.constexpr, HASRES: tl.constexpr):
|
|
113
|
+
pn = tl.program_id(0)
|
|
114
|
+
pk = tl.program_id(1)
|
|
115
|
+
rm = tl.arange(0, BM)
|
|
116
|
+
rn = pn * BN + tl.arange(0, BN)
|
|
117
|
+
rh = tl.arange(0, 64)
|
|
118
|
+
mm = rm[:, None] < M
|
|
119
|
+
acc = tl.zeros([BM, BN], tl.float32)
|
|
120
|
+
k0 = pk * KSPL
|
|
121
|
+
KG = K // 128
|
|
122
|
+
for k in range(k0, k0 + KSPL, 128):
|
|
123
|
+
xl = tl.load(X + rm[:, None] * sxm + (k + rh)[None, :], mask=mm, other=0.0)
|
|
124
|
+
xh = tl.load(X + rm[:, None] * sxm + (k + 64 + rh)[None, :], mask=mm, other=0.0)
|
|
125
|
+
ll = tl.load(LO + rn[:, None] * K + (k + rh)[None, :])
|
|
126
|
+
lh = tl.load(LO + rn[:, None] * K + (k + 64 + rh)[None, :])
|
|
127
|
+
ex = tl.load(EX + rn[:, None] * (K // 2) + (k // 2 + rh)[None, :])
|
|
128
|
+
b = tl.broadcast_to(tl.load(BASE + rn * KG + k // 128)[:, None], [BN, 64])
|
|
129
|
+
wl = tl.inline_asm_elementwise(_ASM_LO_C, "=r,=r,r,r,r", [ll, ex, b], dtype=tl.bfloat16, is_pure=True, pack=4)
|
|
130
|
+
wh = tl.inline_asm_elementwise(_ASM_HI_C, "=r,=r,r,r,r", [lh, ex, b], dtype=tl.bfloat16, is_pure=True, pack=4)
|
|
131
|
+
acc += tl.dot(xl, tl.trans(wl))
|
|
132
|
+
acc += tl.dot(xh, tl.trans(wh))
|
|
133
|
+
if pk == 0: # outlier corrections (exact products with the true weights)
|
|
134
|
+
ro = tl.load(ROFF + rn)
|
|
135
|
+
rc = tl.load(RCNT + rn)
|
|
136
|
+
for c in range(0, MAXC):
|
|
137
|
+
val = c < rc
|
|
138
|
+
idx = ro + c
|
|
139
|
+
kk = tl.load(OK + idx, mask=val, other=0)
|
|
140
|
+
a = tl.load(OA + idx, mask=val, other=0.0)
|
|
141
|
+
bb = tl.load(OB + idx, mask=val, other=0.0)
|
|
142
|
+
xv = tl.load(X + rm[:, None] * sxm + kk[None, :], mask=mm & val[None, :], other=0.0).to(tl.float32)
|
|
143
|
+
acc += xv * a[None, :]
|
|
144
|
+
acc += xv * bb[None, :]
|
|
145
|
+
msk = mm & (rn[None, :] < N)
|
|
146
|
+
if HASRES:
|
|
147
|
+
if pk == 0:
|
|
148
|
+
acc += tl.load(RES + rm[:, None] * srm + rn[None, :], mask=msk, other=0.0).to(tl.float32)
|
|
149
|
+
if ATOMIC:
|
|
150
|
+
tl.atomic_add(Y + rm[:, None] * sym + rn[None, :], acc, mask=msk)
|
|
151
|
+
else:
|
|
152
|
+
tl.store(Y + rm[:, None] * sym + rn[None, :], acc.to(Y.dtype.element_ty), mask=msk)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
_ASM_LO_C = tl.constexpr(_ASM_LO)
|
|
156
|
+
_ASM_HI_C = tl.constexpr(_ASM_HI)
|
|
157
|
+
|
|
158
|
+
CFG = {} # (N, K, Mbucket) -> (BN, split, warps, stages)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def mbucket(M):
|
|
162
|
+
return max(16, triton.next_power_of_2(M))
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def mm(x, C, res=None, cfg=None, out_dtype=BF):
|
|
166
|
+
"""y = x @ W^T (+ res). x (M, K) bf16 contiguous rows."""
|
|
167
|
+
M, K = x.shape
|
|
168
|
+
N = C["N"]
|
|
169
|
+
BM = mbucket(M)
|
|
170
|
+
BN, split, warps, stages = cfg or CFG.get((N, K, BM), (64, 1, 4, 3))
|
|
171
|
+
if split > 1:
|
|
172
|
+
y = torch.zeros(M, N, device=x.device, dtype=torch.float32)
|
|
173
|
+
else:
|
|
174
|
+
y = torch.empty(M, N, device=x.device, dtype=out_dtype)
|
|
175
|
+
r = res if res is not None else y
|
|
176
|
+
_c12_kernel[(triton.cdiv(N, BN), split)](
|
|
177
|
+
x, C["lo"], C["ex"], C["base"], C["roff"], C["rcnt"], C["ok"], C["oa"], C["ob"], y, r, M, N, K,
|
|
178
|
+
x.stride(0), y.stride(0), r.stride(0), K // split, C["maxc"],
|
|
179
|
+
BM=BM, BN=BN, ATOMIC=split > 1, HASRES=res is not None, num_warps=warps, num_stages=stages)
|
|
180
|
+
return y if split == 1 else y.to(out_dtype)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def encode_chunked(w, rows=8192):
|
|
184
|
+
if w.shape[0] <= rows:
|
|
185
|
+
return encode(w)
|
|
186
|
+
parts = [encode(w[i:i + rows]) for i in range(0, w.shape[0], rows)]
|
|
187
|
+
C = dict(N=w.shape[0], K=w.shape[1])
|
|
188
|
+
for k in ("lo", "ex", "base", "rcnt"):
|
|
189
|
+
C[k] = torch.cat([p[k] for p in parts]).contiguous()
|
|
190
|
+
tot, ok, oa, ob, roff = 0, [], [], [], []
|
|
191
|
+
for p in parts:
|
|
192
|
+
n_o = int(p["rcnt"].sum())
|
|
193
|
+
roff.append(p["roff"] + tot)
|
|
194
|
+
ok.append(p["ok"][:n_o])
|
|
195
|
+
oa.append(p["oa"][:n_o])
|
|
196
|
+
ob.append(p["ob"][:n_o])
|
|
197
|
+
tot += n_o
|
|
198
|
+
C["roff"] = torch.cat(roff).to(torch.int32).contiguous()
|
|
199
|
+
C["ok"] = torch.cat(ok).contiguous() if tot else torch.zeros(1, dtype=torch.int32, device=w.device)
|
|
200
|
+
C["oa"] = torch.cat(oa).contiguous() if tot else torch.zeros(1, dtype=torch.float32, device=w.device)
|
|
201
|
+
C["ob"] = torch.cat(ob).contiguous() if tot else torch.zeros(1, dtype=torch.float32, device=w.device)
|
|
202
|
+
C["maxc"] = int(C["rcnt"].max())
|
|
203
|
+
return C
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
# ----------------------------------------------------------------------------- v3: explicit register prefetch
|
|
207
|
+
@triton.jit
|
|
208
|
+
def _ldg(LO, EX, BASE, rn, k, K, rh, ok):
|
|
209
|
+
m2 = ok & (rn[:, None] >= 0)
|
|
210
|
+
ll = tl.load(LO + rn[:, None] * K + (k + rh)[None, :], mask=m2, other=0)
|
|
211
|
+
lh = tl.load(LO + rn[:, None] * K + (k + 64 + rh)[None, :], mask=m2, other=0)
|
|
212
|
+
ex = tl.load(EX + rn[:, None] * (K // 2) + (k // 2 + rh)[None, :], mask=m2, other=0)
|
|
213
|
+
b = tl.load(BASE + rn * (K // 128) + k // 128, mask=ok & (rn >= 0), other=0)
|
|
214
|
+
return ll, lh, ex, b
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
@triton.jit
|
|
218
|
+
def _c12_pf(X, LO, EX, BASE, ROFF, RCNT, OK, OA, OB, Y, M, N, K, sxm, sym, KSPL, MAXC,
|
|
219
|
+
BM: tl.constexpr, BN: tl.constexpr, ATOMIC: tl.constexpr, PF: tl.constexpr):
|
|
220
|
+
pn = tl.program_id(0)
|
|
221
|
+
pk = tl.program_id(1)
|
|
222
|
+
rm = tl.arange(0, BM)
|
|
223
|
+
rn = pn * BN + tl.arange(0, BN)
|
|
224
|
+
rh = tl.arange(0, 64)
|
|
225
|
+
mm = rm[:, None] < M
|
|
226
|
+
acc = tl.zeros([BM, BN], tl.float32)
|
|
227
|
+
k0 = pk * KSPL
|
|
228
|
+
kend = k0 + KSPL
|
|
229
|
+
a_ll, a_lh, a_ex, a_b = _ldg(LO, EX, BASE, rn, k0, K, rh, k0 < kend)
|
|
230
|
+
if PF == 2:
|
|
231
|
+
b_ll, b_lh, b_ex, b_b = _ldg(LO, EX, BASE, rn, k0 + 128, K, rh, k0 + 128 < kend)
|
|
232
|
+
for k in range(k0, kend, 128):
|
|
233
|
+
if PF == 2:
|
|
234
|
+
c_ll, c_lh, c_ex, c_b = _ldg(LO, EX, BASE, rn, k + 256, K, rh, k + 256 < kend)
|
|
235
|
+
else:
|
|
236
|
+
c_ll, c_lh, c_ex, c_b = _ldg(LO, EX, BASE, rn, k + 128, K, rh, k + 128 < kend)
|
|
237
|
+
xl = tl.load(X + rm[:, None] * sxm + (k + rh)[None, :], mask=mm, other=0.0)
|
|
238
|
+
xh = tl.load(X + rm[:, None] * sxm + (k + 64 + rh)[None, :], mask=mm, other=0.0)
|
|
239
|
+
bb = tl.broadcast_to(a_b[:, None], [BN, 64])
|
|
240
|
+
wl = tl.inline_asm_elementwise(_ASM_LO_C, "=r,=r,r,r,r", [a_ll, a_ex, bb], dtype=tl.bfloat16, is_pure=True,
|
|
241
|
+
pack=4)
|
|
242
|
+
wh = tl.inline_asm_elementwise(_ASM_HI_C, "=r,=r,r,r,r", [a_lh, a_ex, bb], dtype=tl.bfloat16, is_pure=True,
|
|
243
|
+
pack=4)
|
|
244
|
+
acc += tl.dot(xl, tl.trans(wl))
|
|
245
|
+
acc += tl.dot(xh, tl.trans(wh))
|
|
246
|
+
if PF == 2:
|
|
247
|
+
a_ll, a_lh, a_ex, a_b = b_ll, b_lh, b_ex, b_b
|
|
248
|
+
b_ll, b_lh, b_ex, b_b = c_ll, c_lh, c_ex, c_b
|
|
249
|
+
else:
|
|
250
|
+
a_ll, a_lh, a_ex, a_b = c_ll, c_lh, c_ex, c_b
|
|
251
|
+
if pk == 0:
|
|
252
|
+
ro = tl.load(ROFF + rn)
|
|
253
|
+
rc = tl.load(RCNT + rn)
|
|
254
|
+
for c in range(0, MAXC):
|
|
255
|
+
val = c < rc
|
|
256
|
+
idx = ro + c
|
|
257
|
+
kk = tl.load(OK + idx, mask=val, other=0)
|
|
258
|
+
a = tl.load(OA + idx, mask=val, other=0.0)
|
|
259
|
+
b2 = tl.load(OB + idx, mask=val, other=0.0)
|
|
260
|
+
xv = tl.load(X + rm[:, None] * sxm + kk[None, :], mask=mm & val[None, :], other=0.0).to(tl.float32)
|
|
261
|
+
acc += xv * a[None, :]
|
|
262
|
+
acc += xv * b2[None, :]
|
|
263
|
+
msk = mm & (rn[None, :] < N)
|
|
264
|
+
if ATOMIC:
|
|
265
|
+
tl.atomic_add(Y + rm[:, None] * sym + rn[None, :], acc, mask=msk)
|
|
266
|
+
else:
|
|
267
|
+
tl.store(Y + rm[:, None] * sym + rn[None, :], acc.to(Y.dtype.element_ty), mask=msk)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def mm_pf(x, C, cfg, out_dtype=BF):
|
|
271
|
+
"""cfg = (BN, split, warps, PF)."""
|
|
272
|
+
M, K = x.shape
|
|
273
|
+
N = C["N"]
|
|
274
|
+
BM = mbucket(M)
|
|
275
|
+
BN, split, warps, pf = cfg
|
|
276
|
+
if split > 1:
|
|
277
|
+
y = torch.zeros(M, N, device=x.device, dtype=torch.float32)
|
|
278
|
+
else:
|
|
279
|
+
y = torch.empty(M, N, device=x.device, dtype=out_dtype)
|
|
280
|
+
_c12_pf[(triton.cdiv(N, BN), split)](
|
|
281
|
+
x, C["lo"], C["ex"], C["base"], C["roff"], C["rcnt"], C["ok"], C["oa"], C["ob"], y, M, N, K,
|
|
282
|
+
x.stride(0), y.stride(0), K // split, C["maxc"], BM=BM, BN=BN, ATOMIC=split > 1, PF=pf,
|
|
283
|
+
num_warps=warps, num_stages=1)
|
|
284
|
+
return y if split == 1 else y.to(out_dtype)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
# ----------------------------------------------------------------------------- torch op wrapper (opaque to inductor)
|
|
288
|
+
@torch.library.custom_op("o3::c12mm", mutates_args=())
|
|
289
|
+
def c12mm_op(x: torch.Tensor, lo: torch.Tensor, ex: torch.Tensor, base: torch.Tensor, roff: torch.Tensor,
|
|
290
|
+
rcnt: torch.Tensor, ok: torch.Tensor, oa: torch.Tensor, ob: torch.Tensor, maxc: int, BN: int, split: int,
|
|
291
|
+
warps: int, pf: int) -> torch.Tensor:
|
|
292
|
+
C = dict(lo=lo, ex=ex, base=base, roff=roff, rcnt=rcnt, ok=ok, oa=oa, ob=ob, maxc=maxc, N=lo.shape[0],
|
|
293
|
+
K=lo.shape[1])
|
|
294
|
+
return mm_pf(x.contiguous(), C, (BN, split, warps, pf))
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
@c12mm_op.register_fake
|
|
298
|
+
def _c12mm_fake(x, lo, ex, base, roff, rcnt, ok, oa, ob, maxc, BN, split, warps, pf):
|
|
299
|
+
return x.new_empty(x.shape[0], lo.shape[0])
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
PFCFG = {} # (N, K, BM) -> (BN, split, warps, pf)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def lin(x, w):
|
|
306
|
+
"""F.linear for a bf16 weight or a C12 dict."""
|
|
307
|
+
if isinstance(w, dict) and w.get("core"):
|
|
308
|
+
import c12w
|
|
309
|
+
x2 = x.reshape(-1, x.shape[-1])
|
|
310
|
+
if x2.shape[0] > 64:
|
|
311
|
+
return torch.nn.functional.linear(x, c12w.decode(w))
|
|
312
|
+
return c12w.lin(x, w)
|
|
313
|
+
if isinstance(w, dict) and w.get("frag"):
|
|
314
|
+
import c12cu
|
|
315
|
+
return c12cu.lin(x, w)
|
|
316
|
+
if isinstance(w, dict):
|
|
317
|
+
x2 = x.reshape(-1, x.shape[-1])
|
|
318
|
+
if x2.shape[0] > 64: # prefill of long prompts: decode this matrix (exact bf16) and use cuBLAS
|
|
319
|
+
return torch.nn.functional.linear(x, decode(w))
|
|
320
|
+
cfg = PFCFG.get((w["N"], w["K"], mbucket(x2.shape[0])), (32, 2, 4, 2))
|
|
321
|
+
y = c12mm_op(x2, w["lo"], w["ex"], w["base"], w["roff"], w["rcnt"], w["ok"], w["oa"], w["ob"], w["maxc"],
|
|
322
|
+
*cfg)
|
|
323
|
+
return y.view(*x.shape[:-1], w["N"])
|
|
324
|
+
return torch.nn.functional.linear(x, w)
|