xuan-luo's picture
download
raw
12.7 kB
"""Exact split-score decode: sum base/delta logits BEFORE online softmax.
One partial-attention CTA per query head and context partition, followed by a
log-sum-exp merge. No history concatenation, expanded KV, or T-wide logits/probs
are written to HBM. GQA uses the SAME kernels with DELTA=0. FP32 accumulation.
"""
import torch
import triton as tr
import triton.language as tl
@tr.jit
def _rms_norm(X, W, Y, D: tl.constexpr, EPS: tl.constexpr, BLOCK: tl.constexpr):
row = tl.program_id(0)
d = tl.arange(0, BLOCK)
x = tl.load(X + row * D + d, d < D, 0).to(tl.float32)
variance = tl.sum(x * x, 0) / D
normalized = (x * tl.rsqrt(variance + EPS)).to(Y.dtype.element_ty).to(tl.float32)
w = tl.load(W + d, d < D, 0).to(tl.float32)
tl.store(Y + row * D + d, normalized * w, d < D)
@tr.jit
def _silu_mul(GATE, UP, OUT, N: tl.constexpr, BLOCK: tl.constexpr):
i = tl.program_id(0) * BLOCK + tl.arange(0, BLOCK)
g = tl.load(GATE + i, i < N, 0).to(tl.float32)
up = tl.load(UP + i, i < N, 0).to(tl.float32)
# Match the existing two operations' rounding, without their HBM temporary.
silu = (g / (1 + tl.exp(-g))).to(OUT.dtype.element_ty).to(tl.float32)
tl.store(OUT + i, silu * up, i < N)
def rms_norm(x, module):
out = torch.empty_like(x)
d = x.shape[-1]
_rms_norm[(x.numel() // d,)](x, module.weight, out, d, module.variance_epsilon,
tr.next_power_of_2(d), num_warps=4)
return out
def swiglu(gate, up):
out = torch.empty_like(gate)
_silu_mul[(tr.cdiv(gate.numel(), 256),)](gate, up, out, gate.numel(), 256)
return out
@tr.jit
def _append_rope(M, Q, KB, VB, KD, VD, COS, SIN, POS,
MS: tl.constexpr, H: tl.constexpr, G: tl.constexpr,
CAP: tl.constexpr, BASE: tl.constexpr, DELTA: tl.constexpr,
ANCHOR: tl.constexpr, BD: tl.constexpr, BR: tl.constexpr):
b = tl.program_id(0)
h = tl.program_id(1)
group = h // (H // G)
pos = tl.load(POS)
width: tl.constexpr = BASE + DELTA
qsize: tl.constexpr = H * width
d = tl.arange(0, BD)
if DELTA > 0:
q = tl.load(M + b * MS + h * width + d, d < BASE, 0)
tl.store(Q + (b * H + h) * width + d, q, d < BASE)
r = tl.arange(0, BR)
other = (r + DELTA // 2) % DELTA
cos = tl.load(COS + pos * DELTA + r, r < DELTA, 0)
sin = tl.load(SIN + pos * DELTA + r, r < DELTA, 0)
qr = tl.load(M + b * MS + h * width + BASE + r, r < DELTA, 0).to(tl.float32)
qo = tl.load(M + b * MS + h * width + BASE + other, r < DELTA, 0).to(tl.float32)
qr = qr * cos + tl.where(r < DELTA // 2, -qo, qo) * sin
tl.store(Q + (b * H + h) * width + BASE + r, qr, r < DELTA)
if h % (H // G) == 0:
offset = b * MS + qsize + group * 2 * DELTA
kr = tl.load(M + offset + r, r < DELTA, 0).to(tl.float32)
ko = tl.load(M + offset + other, r < DELTA, 0).to(tl.float32)
kr = kr * cos + tl.where(r < DELTA // 2, -ko, ko) * sin
vr = tl.load(M + offset + DELTA + r, r < DELTA, 0)
out = ((b * G + group) * CAP + pos) * DELTA + r
tl.store(KD + out, kr, r < DELTA)
tl.store(VD + out, vr, r < DELTA)
if ANCHOR:
offset = b * MS + qsize + 2 * G * DELTA + group * 2 * BASE
k = tl.load(M + offset + d, d < BASE, 0)
v = tl.load(M + offset + BASE + d, d < BASE, 0)
out = ((b * G + group) * CAP + pos) * BASE + d
tl.store(KB + out, k, d < BASE)
tl.store(VB + out, v, d < BASE)
else:
other = (d + BASE // 2) % BASE
cos = tl.load(COS + pos * BASE + d, d < BASE, 0)
sin = tl.load(SIN + pos * BASE + d, d < BASE, 0)
q = tl.load(M + b * MS + h * BASE + d, d < BASE, 0).to(tl.float32)
qo = tl.load(M + b * MS + h * BASE + other, d < BASE, 0).to(tl.float32)
q = q * cos + tl.where(d < BASE // 2, -qo, qo) * sin
tl.store(Q + (b * H + h) * BASE + d, q, d < BASE)
if h % (H // G) == 0:
offset = b * MS + qsize + group * BASE
k = tl.load(M + offset + d, d < BASE, 0).to(tl.float32)
ko = tl.load(M + offset + other, d < BASE, 0).to(tl.float32)
k = k * cos + tl.where(d < BASE // 2, -ko, ko) * sin
v = tl.load(M + offset + G * BASE + d, d < BASE, 0)
out = ((b * G + group) * CAP + pos) * BASE + d
tl.store(KB + out, k, d < BASE)
tl.store(VB + out, v, d < BASE)
@tr.jit
def _partial(Q, KB, VB, KD, VD, POS, PART, LSE,
H: tl.constexpr, G: tl.constexpr, CAP: tl.constexpr,
BASE: tl.constexpr, DELTA: tl.constexpr, SCALE: tl.constexpr,
SPLIT: tl.constexpr, NS: tl.constexpr, BT: tl.constexpr,
BD: tl.constexpr, BR: tl.constexpr):
bh = tl.program_id(0)
split = tl.program_id(1)
bg = (bh // H) * G + (bh % H) // (H // G)
length = tl.load(POS) + 1
d = tl.arange(0, BD)
r = tl.arange(0, BR)
q = tl.load(Q + bh * (BASE + DELTA) + d, d < BASE, 0).to(tl.float32)
if DELTA > 0:
qr = tl.load(Q + bh * (BASE + DELTA) + BASE + r, r < DELTA, 0).to(tl.float32)
ar = tl.full((BR,), 0, tl.float32)
acc = tl.full((BD,), 0, tl.float32)
m = tl.full((), -1.0e30, tl.float32)
z = tl.full((), 0, tl.float32)
for start in range(split * SPLIT, tl.minimum((split + 1) * SPLIT, length), BT):
t = start + tl.arange(0, BT)
k = tl.load(KB + (bg * CAP + t[:, None]) * BASE + d[None, :],
(t[:, None] < length) & (d[None, :] < BASE), 0).to(tl.float32)
score = tl.sum(k * q[None, :], 1)
if DELTA > 0:
kd = tl.load(KD + (bg * CAP + t[:, None]) * DELTA + r[None, :],
(t[:, None] < length) & (r[None, :] < DELTA), 0).to(tl.float32)
score += tl.sum(kd * qr[None, :], 1)
score = tl.where(t < length, score * SCALE, -float('inf'))
new_m = tl.maximum(m, tl.max(score, 0))
alpha = tl.exp(m - new_m)
p = tl.exp(score - new_m)
v = tl.load(VB + (bg * CAP + t[:, None]) * BASE + d[None, :],
(t[:, None] < length) & (d[None, :] < BASE), 0).to(tl.float32)
acc = acc * alpha + tl.sum(p[:, None] * v, 0)
if DELTA > 0:
vd = tl.load(VD + (bg * CAP + t[:, None]) * DELTA + r[None, :],
(t[:, None] < length) & (r[None, :] < DELTA), 0).to(tl.float32)
ar = ar * alpha + tl.sum(p[:, None] * vd, 0)
z = z * alpha + tl.sum(p, 0)
m = new_m
offset = (bh * NS + split) * (BASE + DELTA)
tl.store(PART + offset + d, acc / tl.maximum(z, 1.0e-30), d < BASE)
if DELTA > 0:
tl.store(PART + offset + BASE + r, ar / tl.maximum(z, 1.0e-30), r < DELTA)
tl.store(LSE + bh * NS + split, tl.where(z > 0, m + tl.log(z), -float('inf')))
@tr.jit
def _merge(PART, LSE, OUT, NS: tl.constexpr, D: tl.constexpr,
BS: tl.constexpr, BD: tl.constexpr):
bh = tl.program_id(0)
s = tl.arange(0, BS)
d = tl.arange(0, BD)
lse = tl.load(LSE + bh * NS + s, s < NS, -float('inf'))
weights = tl.exp(lse - tl.max(lse, 0))
part = tl.load(PART + (bh * NS + s[:, None]) * D + d[None, :],
(s[:, None] < NS) & (d[None, :] < D), 0)
out = tl.sum(part * weights[:, None], 0) / tl.sum(weights, 0)
tl.store(OUT + bh * D + d, out, d < D)
@tr.jit
def _partial_grouped(Q, KB, VB, KD, VD, POS, PART, LSE,
H: tl.constexpr, G: tl.constexpr, CAP: tl.constexpr,
BASE: tl.constexpr, DELTA: tl.constexpr, SCALE: tl.constexpr,
SPLIT: tl.constexpr, NS: tl.constexpr, BT: tl.constexpr,
BD: tl.constexpr, BR: tl.constexpr, BH: tl.constexpr):
# All query heads sharing one KV head run together. Each K/V tile is
# loaded once for those heads; dot products use tensor cores.
bg = tl.program_id(0)
split = tl.program_id(1)
rep: tl.constexpr = H // G
h = tl.arange(0, BH)
bh = (bg // G) * H + (bg % G) * rep + h
length = tl.load(POS) + 1
d = tl.arange(0, BD)
r = tl.arange(0, BR)
q = tl.load(Q + bh[:, None] * (BASE + DELTA) + d[None, :],
(h[:, None] < rep) & (d[None, :] < BASE), 0)
if DELTA > 0:
qr = tl.load(Q + bh[:, None] * (BASE + DELTA) + BASE + r[None, :],
(h[:, None] < rep) & (r[None, :] < DELTA), 0)
ar = tl.full((BH, BR), 0, tl.float32)
acc = tl.full((BH, BD), 0, tl.float32)
m = tl.full((BH,), -1.0e30, tl.float32)
z = tl.full((BH,), 0, tl.float32)
for start in range(split * SPLIT, tl.minimum((split + 1) * SPLIT, length), BT):
t = start + tl.arange(0, BT)
k = tl.load(KB + (bg * CAP + t[None, :]) * BASE + d[:, None],
(t[None, :] < length) & (d[:, None] < BASE), 0)
score = tl.dot(q, k, input_precision="tf32x3")
if DELTA > 0:
kd = tl.load(KD + (bg * CAP + t[None, :]) * DELTA + r[:, None],
(t[None, :] < length) & (r[:, None] < DELTA), 0)
score += tl.dot(qr, kd, input_precision="tf32x3")
score = tl.where(t[None, :] < length, score * SCALE, -float('inf'))
new_m = tl.maximum(m, tl.max(score, 1))
alpha = tl.exp(m - new_m)
p = tl.exp(score - new_m[:, None])
v = tl.load(VB + (bg * CAP + t[:, None]) * BASE + d[None, :],
(t[:, None] < length) & (d[None, :] < BASE), 0)
acc = acc * alpha[:, None] + tl.dot(p.to(v.dtype), v, input_precision="tf32x3")
if DELTA > 0:
vd = tl.load(VD + (bg * CAP + t[:, None]) * DELTA + r[None, :],
(t[:, None] < length) & (r[None, :] < DELTA), 0)
ar = ar * alpha[:, None] + tl.dot(p.to(vd.dtype), vd, input_precision="tf32x3")
z = z * alpha + tl.sum(p, 1)
m = new_m
offset = (bh * NS + split) * (BASE + DELTA)
tl.store(PART + offset[:, None] + d[None, :], acc / tl.maximum(z[:, None], 1.0e-30),
(h[:, None] < rep) & (d[None, :] < BASE))
if DELTA > 0:
tl.store(PART + offset[:, None] + BASE + r[None, :], ar / tl.maximum(z[:, None], 1.0e-30),
(h[:, None] < rep) & (r[None, :] < DELTA))
tl.store(LSE + bh * NS + split, tl.where(z > 0, m + tl.log(z), -float('inf')), h < rep)
def append_rope(mixed, base_pair, delta_pair, cos, sin, position, heads, anchor=True):
kb, vb = base_pair
b, g, capacity, base = kb.shape
delta = delta_pair[0].shape[-1] if delta_pair is not None else 0
kd, vd = delta_pair if delta_pair is not None else (kb, vb)
q = torch.empty((b, heads, base + delta), dtype=mixed.dtype, device=mixed.device)
_append_rope[(b, heads)](mixed, q, kb, vb, kd, vd, cos, sin, position,
mixed.stride(0), heads, g, capacity, base, delta, anchor,
tr.next_power_of_2(base), tr.next_power_of_2(max(delta, 1)),
num_warps=4, enable_fp_fusion=False)
return q
def split_decode(q, base_pair, delta_pair, position, scale, split_size=512, grouped=False):
kb, vb = base_pair
b, g, capacity, base = kb.shape
delta = delta_pair[0].shape[-1] if delta_pair is not None else 0
kd, vd = delta_pair if delta_pair is not None else (kb, vb)
heads = q.shape[1]
block = 64 if grouped else 128
if split_size < block or split_size % block:
raise ValueError(f"split_size must be a positive multiple of {block}")
ns = tr.cdiv(capacity, split_size)
partial = torch.empty((b * heads, ns, base + delta), dtype=torch.float32, device=q.device)
lse = torch.empty((b * heads, ns), dtype=torch.float32, device=q.device)
out = torch.empty_like(q)
if grouped:
_partial_grouped[(b * g, ns)](q, kb, vb, kd, vd, position, partial, lse,
heads, g, capacity, base, delta, scale, split_size, ns, block,
tr.next_power_of_2(base), max(32, tr.next_power_of_2(max(delta, 1))),
max(16, tr.next_power_of_2(heads // g)), num_warps=4, num_stages=1)
else:
_partial[(b * heads, ns)](q, kb, vb, kd, vd, position, partial, lse,
heads, g, capacity, base, delta, scale, split_size, ns, block,
tr.next_power_of_2(base), tr.next_power_of_2(max(delta, 1)), num_warps=4)
_merge[(b * heads,)](partial, lse, out, ns, base + delta,
tr.next_power_of_2(ns), tr.next_power_of_2(base + delta), num_warps=4)
return out

Xet Storage Details

Size:
12.7 kB
·
Xet hash:
dd5509db711cb95edd528a6512302ab90e1e4ac78706948900bf7d9bcd241414

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.