KernelBench hard deprecated · H100
KDA CUTLASS GPT-5.5
passdid not score
agent session20mtotal wall22mcheck2mbenchmark12soutput tokens86,611gpu-lock wait2mgpu-lock held30sregimecompute
Per-shape vs governing ceilingeach shape graded against whichever binds — bf16 compute or HBM bandwidth
2×1024×8×128×128×640.198 ms1.4%0.13 TB/s · 6% of 2.0 TB/s HBM · also 11 TFLOPS (1% of compute)
2×2048×8×128×128×640.308 ms1.8%0.16 TB/s · 8% of 2.0 TB/s HBM · also 14 TFLOPS (2% of compute)
1×4096×8×128×128×640.389 ms1.5%0.13 TB/s · 6% of 2.0 TB/s HBM · also 11 TFLOPS (1% of compute)
1×2048×4×128×128×640.172 ms0.8%0.07 TB/s · 4% of 2.0 TB/s HBM · also 6 TFLOPS (1% of compute)
compute-bound memory-bound · bar + right column = official fraction of the ceiling (the geomean input)
geomean(1.4% · 1.8% · 1.5% · 0.8%) = 1.3%
Kernel source (redacted)
from __future__ import annotations
import torch
import torch.nn as nn
import triton
import triton.language as tl
@triton.jit
def _gate_cumsum_kernel(g, gout, T: tl.constexpr, H: tl.constexpr, K: tl.constexpr, BT: tl.constexpr, BK: tl.constexpr):
i_t = tl.program_id(0)
i_bh = tl.program_id(1)
i_b = i_bh // H
i_h = i_bh - i_b * H
o_t = i_t * BT + tl.arange(0, BT)
o_k = tl.arange(0, BK)
p = ((i_b * T + o_t[:, None]) * H + i_h) * K + o_k[None, :]
x = tl.load(g + p, mask=o_k[None, :] < K, other=0.0).to(tl.float32)
y = tl.cumsum(x, 0) * 1.4426950408889634
tl.store(gout + p, y, mask=o_k[None, :] < K)
@triton.jit
def _diag_block_kernel(
q,
k,
g,
beta,
aq,
ai_diag,
scale: tl.constexpr,
T: tl.constexpr,
H: tl.constexpr,
K: tl.constexpr,
BT: tl.constexpr,
BC: tl.constexpr,
BK: tl.constexpr,
):
i_chunk = tl.program_id(0)
i_sub = tl.program_id(1)
i_bh = tl.program_id(2)
i_b = i_bh // H
i_h = i_bh - i_b * H
start = i_chunk * BT + i_sub * BC
base = i_b * T * H + i_h
p_q = tl.make_block_ptr(q + base * K, (T, K), (H * K, 1), (start, 0), (BC, BK), (1, 0))
p_k = tl.make_block_ptr(k + base * K, (T, K), (H * K, 1), (start, 0), (BC, BK), (1, 0))
p_g = tl.make_block_ptr(g + base * K, (T, K), (H * K, 1), (start, 0), (BC, BK), (1, 0))
b_q = tl.load(p_q, boundary_check=(0, 1))
b_k = tl.load(p_k, boundary_check=(0, 1))
b_g = tl.load(p_g, boundary_check=(0, 1)).to(tl.float32)
o_k = tl.arange(0, BK)
b_gn = tl.load(g + ((i_b * T + start + BC // 2) * H + i_h) * K + o_k, mask=o_k < K, other=0.0).to(tl.float32)
b_shift = b_g - b_gn[None, :]
b_gq = tl.exp2(b_shift)
b_gk = tl.exp2(-b_shift)
b_kgt = tl.trans(b_k * b_gk)
b_aq = tl.dot(b_q * b_gq, b_kgt) * scale
o_i = tl.arange(0, BC)
b_b = tl.load(beta + (i_b * T + start + o_i) * H + i_h).to(tl.float32)
b_akk = tl.dot(b_k * b_gq, b_kgt) * b_b[:, None]
incl = o_i[:, None] >= o_i[None, :]
lower = o_i[:, None] > o_i[None, :]
ident = o_i[:, None] == o_i[None, :]
b_aq = tl.where(incl, b_aq, 0.0)
p_aq = tl.make_block_ptr(
aq + (i_b * T * H + i_h) * BT,
(T, BT),
(H * BT, 1),
(start, i_sub * BC),
(BC, BC),
(1, 0),
)
tl.store(p_aq, b_aq.to(aq.dtype.element_ty), boundary_check=(0, 1))
b_inv = -tl.where(lower, b_akk, 0.0)
for r in range(2, BC):
row = -tl.sum(tl.where((o_i == r)[:, None], b_akk, 0.0), 0)
row = tl.where(o_i < r, row, 0.0)
row += tl.sum(row[:, None] * b_inv, 0)
b_inv = tl.where((o_i == r)[:, None], row, b_inv)
b_inv += ident
p_ai = tl.make_block_ptr(
ai_diag + (i_b * T * H + i_h) * BC,
(T, BC),
(H * BC, 1),
(start, 0),
(BC, BC),
(1, 0),
)
tl.store(p_ai, b_inv, boundary_check=(0, 1))
@triton.jit
def _solve_kernel(
q,
k,
g,
beta,
aq,
ai_diag,
ai,
scale: tl.constexpr,
T: tl.constexpr,
H: tl.constexpr,
K: tl.constexpr,
BT: tl.constexpr,
BC: tl.constexpr,
BK: tl.constexpr,
):
i_chunk = tl.program_id(0)
i_bh = tl.program_id(1)
i_b = i_bh // H
i_h = i_bh - i_b * H
tc0: tl.constexpr = 0
tc1: tl.constexpr = 16
tc2: tl.constexpr = 32
tc3: tl.constexpr = 48
c0 = i_chunk * BT + tc0
c1 = i_chunk * BT + tc1
c2 = i_chunk * BT + tc2
c3 = i_chunk * BT + tc3
bh_base = i_b * T * H + i_h
b_aq10 = tl.zeros([BC, BC], dtype=tl.float32)
b_ai10 = tl.zeros([BC, BC], dtype=tl.float32)
b_aq20 = tl.zeros([BC, BC], dtype=tl.float32)
b_ai20 = tl.zeros([BC, BC], dtype=tl.float32)
b_aq21 = tl.zeros([BC, BC], dtype=tl.float32)
b_ai21 = tl.zeros([BC, BC], dtype=tl.float32)
b_aq30 = tl.zeros([BC, BC], dtype=tl.float32)
b_ai30 = tl.zeros([BC, BC], dtype=tl.float32)
b_aq31 = tl.zeros([BC, BC], dtype=tl.float32)
b_ai31 = tl.zeros([BC, BC], dtype=tl.float32)
b_aq32 = tl.zeros([BC, BC], dtype=tl.float32)
b_ai32 = tl.zeros([BC, BC], dtype=tl.float32)
for kk in range(0, K, BK):
p_k0 = tl.make_block_ptr(k + bh_base * K, (T, K), (H * K, 1), (c0, kk), (BC, BK), (1, 0))
p_g0 = tl.make_block_ptr(g + bh_base * K, (T, K), (H * K, 1), (c0, kk), (BC, BK), (1, 0))
b_k0 = tl.load(p_k0, boundary_check=(0, 1)).to(tl.float32)
b_g0 = tl.load(p_g0, boundary_check=(0, 1)).to(tl.float32)
o_k = kk + tl.arange(0, BK)
p_q1 = tl.make_block_ptr(q + bh_base * K, (T, K), (H * K, 1), (c1, kk), (BC, BK), (1, 0))
p_k1 = tl.make_block_ptr(k + bh_base * K, (T, K), (H * K, 1), (c1, kk), (BC, BK), (1, 0))
p_g1 = tl.make_block_ptr(g + bh_base * K, (T, K), (H * K, 1), (c1, kk), (BC, BK), (1, 0))
b_q1 = tl.load(p_q1, boundary_check=(0, 1)).to(tl.float32)
b_k1 = tl.load(p_k1, boundary_check=(0, 1)).to(tl.float32)
b_g1 = tl.load(p_g1, boundary_check=(0, 1)).to(tl.float32)
b_gn1 = tl.load(g + ((i_b * T + c1) * H + i_h) * K + o_k, mask=o_k < K, other=0.0).to(tl.float32)
b_tgt = tl.exp2(b_g1 - b_gn1[None, :])
b_src = tl.trans(b_k0 * tl.exp2(b_gn1[None, :] - b_g0))
b_aq10 += tl.dot(b_q1 * b_tgt, b_src)
b_ai10 += tl.dot(b_k1 * b_tgt, b_src)
p_q2 = tl.make_block_ptr(q + bh_base * K, (T, K), (H * K, 1), (c2, kk), (BC, BK), (1, 0))
p_k2 = tl.make_block_ptr(k + bh_base * K, (T, K), (H * K, 1), (c2, kk), (BC, BK), (1, 0))
p_g2 = tl.make_block_ptr(g + bh_base * K, (T, K), (H * K, 1), (c2, kk), (BC, BK), (1, 0))
b_q2 = tl.load(p_q2, boundary_check=(0, 1)).to(tl.float32)
b_k2 = tl.load(p_k2, boundary_check=(0, 1)).to(tl.float32)
b_g2 = tl.load(p_g2, boundary_check=(0, 1)).to(tl.float32)
b_gn2 = tl.load(g + ((i_b * T + c2) * H + i_h) * K + o_k, mask=o_k < K, other=0.0).to(tl.float32)
b_tgt2 = tl.exp2(b_g2 - b_gn2[None, :])
b_qg2 = b_q2 * b_tgt2
b_kg2 = b_k2 * b_tgt2
b_src20 = tl.trans(b_k0 * tl.exp2(b_gn2[None, :] - b_g0))
b_src21 = tl.trans(b_k1 * tl.exp2(b_gn2[None, :] - b_g1))
b_aq20 += tl.dot(b_qg2, b_src20)
b_ai20 += tl.dot(b_kg2, b_src20)
b_aq21 += tl.dot(b_qg2, b_src21)
b_ai21 += tl.dot(b_kg2, b_src21)
p_q3 = tl.make_block_ptr(q + bh_base * K, (T, K), (H * K, 1), (c3, kk), (BC, BK), (1, 0))
p_k3 = tl.make_block_ptr(k + bh_base * K, (T, K), (H * K, 1), (c3, kk), (BC, BK), (1, 0))
p_g3 = tl.make_block_ptr(g + bh_base * K, (T, K), (H * K, 1), (c3, kk), (BC, BK), (1, 0))
b_q3 = tl.load(p_q3, boundary_check=(0, 1)).to(tl.float32)
b_k3 = tl.load(p_k3, boundary_check=(0, 1)).to(tl.float32)
b_g3 = tl.load(p_g3, boundary_check=(0, 1)).to(tl.float32)
b_gn3 = tl.load(g + ((i_b * T + c3) * H + i_h) * K + o_k, mask=o_k < K, other=0.0).to(tl.float32)
b_tgt3 = tl.exp2(b_g3 - b_gn3[None, :])
b_qg3 = b_q3 * b_tgt3
b_kg3 = b_k3 * b_tgt3
b_src30 = tl.trans(b_k0 * tl.exp2(b_gn3[None, :] - b_g0))
b_src31 = tl.trans(b_k1 * tl.exp2(b_gn3[None, :] - b_g1))
b_src32 = tl.trans(b_k2 * tl.exp2(b_gn3[None, :] - b_g2))
b_aq30 += tl.dot(b_qg3, b_src30)
b_ai30 += tl.dot(b_kg3, b_src30)
b_aq31 += tl.dot(b_qg3, b_src31)
b_ai31 += tl.dot(b_kg3, b_src31)
b_aq32 += tl.dot(b_qg3, b_src32)
b_ai32 += tl.dot(b_kg3, b_src32)
o_i = tl.arange(0, BC)
p_b1 = beta + (i_b * T + c1 + o_i) * H + i_h
p_b2 = beta + (i_b * T + c2 + o_i) * H + i_h
p_b3 = beta + (i_b * T + c3 + o_i) * H + i_h
b_b1 = tl.load(p_b1).to(tl.float32)
b_b2 = tl.load(p_b2).to(tl.float32)
b_b3 = tl.load(p_b3).to(tl.float32)
b_ai10 *= b_b1[:, None]
b_ai20 *= b_b2[:, None]
b_ai21 *= b_b2[:, None]
b_ai30 *= b_b3[:, None]
b_ai31 *= b_b3[:, None]
b_ai32 *= b_b3[:, None]
p_aq10 = tl.make_block_ptr(aq + (i_b * T * H + i_h) * BT, (T, BT), (H * BT, 1), (c1, tc0), (BC, BC), (1, 0))
p_aq20 = tl.make_block_ptr(aq + (i_b * T * H + i_h) * BT, (T, BT), (H * BT, 1), (c2, tc0), (BC, BC), (1, 0))
p_aq21 = tl.make_block_ptr(aq + (i_b * T * H + i_h) * BT, (T, BT), (H * BT, 1), (c2, tc1), (BC, BC), (1, 0))
p_aq30 = tl.make_block_ptr(aq + (i_b * T * H + i_h) * BT, (T, BT), (H * BT, 1), (c3, tc0), (BC, BC), (1, 0))
p_aq31 = tl.make_block_ptr(aq + (i_b * T * H + i_h) * BT, (T, BT), (H * BT, 1), (c3, tc1), (BC, BC), (1, 0))
p_aq32 = tl.make_block_ptr(aq + (i_b * T * H + i_h) * BT, (T, BT), (H * BT, 1), (c3, tc2), (BC, BC), (1, 0))
tl.store(p_aq10, (b_aq10 * scale).to(aq.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_aq20, (b_aq20 * scale).to(aq.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_aq21, (b_aq21 * scale).to(aq.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_aq30, (b_aq30 * scale).to(aq.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_aq31, (b_aq31 * scale).to(aq.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_aq32, (b_aq32 * scale).to(aq.dtype.element_ty), boundary_check=(0, 1))
p_d0 = tl.make_block_ptr(ai_diag + (i_b * T * H + i_h) * BC, (T, BC), (H * BC, 1), (c0, 0), (BC, BC), (1, 0))
p_d1 = tl.make_block_ptr(ai_diag + (i_b * T * H + i_h) * BC, (T, BC), (H * BC, 1), (c1, 0), (BC, BC), (1, 0))
p_d2 = tl.make_block_ptr(ai_diag + (i_b * T * H + i_h) * BC, (T, BC), (H * BC, 1), (c2, 0), (BC, BC), (1, 0))
p_d3 = tl.make_block_ptr(ai_diag + (i_b * T * H + i_h) * BC, (T, BC), (H * BC, 1), (c3, 0), (BC, BC), (1, 0))
b_m0 = tl.load(p_d0, boundary_check=(0, 1)).to(tl.float32)
b_m1 = tl.load(p_d1, boundary_check=(0, 1)).to(tl.float32)
b_m2 = tl.load(p_d2, boundary_check=(0, 1)).to(tl.float32)
b_m3 = tl.load(p_d3, boundary_check=(0, 1)).to(tl.float32)
b_i10 = -tl.dot(tl.dot(b_m1, b_ai10, input_precision="tf32"), b_m0, input_precision="tf32")
b_i21 = -tl.dot(tl.dot(b_m2, b_ai21, input_precision="tf32"), b_m1, input_precision="tf32")
b_i20 = -tl.dot(
b_m2,
tl.dot(b_ai20, b_m0, input_precision="tf32") + tl.dot(b_ai21, b_i10, input_precision="tf32"),
input_precision="tf32",
)
b_i32 = -tl.dot(tl.dot(b_m3, b_ai32, input_precision="tf32"), b_m2, input_precision="tf32")
b_i31 = -tl.dot(
b_m3,
tl.dot(b_ai31, b_m1, input_precision="tf32") + tl.dot(b_ai32, b_i21, input_precision="tf32"),
input_precision="tf32",
)
b_i30 = -tl.dot(
b_m3,
tl.dot(b_ai30, b_m0, input_precision="tf32")
+ tl.dot(b_ai31, b_i10, input_precision="tf32")
+ tl.dot(b_ai32, b_i20, input_precision="tf32"),
input_precision="tf32",
)
ai_base = ai + (i_b * T * H + i_h) * BT
p_i00 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c0, tc0), (BC, BC), (1, 0))
p_i10 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c1, tc0), (BC, BC), (1, 0))
p_i11 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c1, tc1), (BC, BC), (1, 0))
p_i20 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c2, tc0), (BC, BC), (1, 0))
p_i21 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c2, tc1), (BC, BC), (1, 0))
p_i22 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c2, tc2), (BC, BC), (1, 0))
p_i30 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c3, tc0), (BC, BC), (1, 0))
p_i31 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c3, tc1), (BC, BC), (1, 0))
p_i32 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c3, tc2), (BC, BC), (1, 0))
p_i33 = tl.make_block_ptr(ai_base, (T, BT), (H * BT, 1), (c3, tc3), (BC, BC), (1, 0))
tl.store(p_i00, b_m0.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i10, b_i10.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i11, b_m1.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i20, b_i20.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i21, b_i21.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i22, b_m2.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i30, b_i30.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i31, b_i31.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i32, b_i32.to(ai.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_i33, b_m3.to(ai.dtype.element_ty), boundary_check=(0, 1))
@triton.jit
def _uw_kernel(
k,
v,
beta,
a,
g,
w,
u,
kg,
T: tl.constexpr,
H: tl.constexpr,
K: tl.constexpr,
V: tl.constexpr,
BT: tl.constexpr,
BK: tl.constexpr,
BV: tl.constexpr,
):
i_chunk = tl.program_id(0)
i_bh = tl.program_id(1)
i_b = i_bh // H
i_h = i_bh - i_b * H
start = i_chunk * BT
o_t = tl.arange(0, BT)
p_b = beta + (i_b * T + start + o_t) * H + i_h
b_b = tl.load(p_b).to(tl.float32)
p_a = tl.make_block_ptr(a + (i_b * T * H + i_h) * BT, (T, BT), (H * BT, 1), (start, 0), (BT, BT), (1, 0))
b_a = tl.load(p_a, boundary_check=(0, 1))
b_a = tl.where(o_t[:, None] >= o_t[None, :], b_a, 0.0)
for vv in range(0, V, BV):
p_v = tl.make_block_ptr(v + (i_b * T * H + i_h) * V, (T, V), (H * V, 1), (start, vv), (BT, BV), (1, 0))
p_u = tl.make_block_ptr(u + (i_b * T * H + i_h) * V, (T, V), (H * V, 1), (start, vv), (BT, BV), (1, 0))
b_v = tl.load(p_v, boundary_check=(0, 1))
b_vb = (b_v * b_b[:, None]).to(b_v.dtype)
b_u = tl.dot(b_a, b_vb)
tl.store(p_u, b_u.to(u.dtype.element_ty), boundary_check=(0, 1))
for kk in range(0, K, BK):
o_k = kk + tl.arange(0, BK)
p_k = tl.make_block_ptr(k + (i_b * T * H + i_h) * K, (T, K), (H * K, 1), (start, kk), (BT, BK), (1, 0))
p_g = tl.make_block_ptr(g + (i_b * T * H + i_h) * K, (T, K), (H * K, 1), (start, kk), (BT, BK), (1, 0))
b_k = tl.load(p_k, boundary_check=(0, 1))
b_g = tl.load(p_g, boundary_check=(0, 1)).to(tl.float32)
b_kb = b_k * b_b[:, None]
b_kb = b_kb * tl.exp2(b_g)
p_w = tl.make_block_ptr(w + (i_b * T * H + i_h) * K, (T, K), (H * K, 1), (start, kk), (BT, BK), (1, 0))
b_w = tl.dot(b_a, b_kb.to(b_k.dtype))
tl.store(p_w, b_w.to(w.dtype.element_ty), boundary_check=(0, 1))
b_gl = tl.load(g + ((i_b * T + start + BT - 1) * H + i_h) * K + o_k, mask=o_k < K, other=0.0).to(tl.float32)
b_kg = b_k * tl.exp2(b_gl[None, :] - b_g)
p_kg = tl.make_block_ptr(kg + (i_b * T * H + i_h) * K, (T, K), (H * K, 1), (start, kk), (BT, BK), (1, 0))
tl.store(p_kg, b_kg.to(kg.dtype.element_ty), boundary_check=(0, 1))
@triton.jit
def _state_kernel(
kg,
w,
u,
g,
h,
vnew,
T: tl.constexpr,
H: tl.constexpr,
K: tl.constexpr,
V: tl.constexpr,
BT: tl.constexpr,
BV: tl.constexpr,
NT: tl.constexpr,
):
i_v = tl.program_id(0)
i_bh = tl.program_id(1)
i_b = i_bh // H
i_h = i_bh - i_b * H
b_h0 = tl.zeros([64, BV], dtype=tl.float32)
b_h1 = tl.zeros([64, BV], dtype=tl.float32)
vv = i_v * BV
for it in range(0, NT):
start = it * BT
h_base = (((i_b * NT + it) * H + i_h) * K) * V
p_h0 = tl.make_block_ptr(h + h_base, (K, V), (V, 1), (0, vv), (64, BV), (1, 0))
p_h1 = tl.make_block_ptr(h + h_base, (K, V), (V, 1), (64, vv), (64, BV), (1, 0))
tl.store(p_h0, b_h0.to(h.dtype.element_ty), boundary_check=(0, 1))
tl.store(p_h1, b_h1.to(h.dtype.element_ty), boundary_check=(0, 1))
p_w0 = tl.make_block_ptr(w + (i_b * T * H + i_h) * K, (T, K), (H * K, 1), (start, 0), (BT, 64), (1, 0))
p_w1 = tl.make_block_ptr(w + (i_b * T * H + i_h) * K, (T, K), (H * K, 1), (start, 64), (BT, 64), (1, 0))
b_w0 = tl.load(p_w0, boundary_check=(0, 1))
b_w1 = tl.load(p_w1, boundary_check=(0, 1))
b_v = tl.dot(b_w0, b_h0.to(b_w0.dtype)) + tl.dot(b_w1, b_h1.to(b_w1.dtype))
p_u = tl.make_block_ptr(u + (i_b * T * H + i_h) * V, (T, V), (H * V, 1), (start, vv), (BT, BV), (1, 0))
b_v = tl.load(p_u, boundary_check=(0, 1)) - b_v
p_vn = tl.make_block_ptr(vnew + (i_b * T * H + i_h) * V, (T, V), (H * V, 1), (start, vv), (BT, BV), (1, 0))
tl.store(p_vn, b_v.to(vnew.dtype.element_ty), boundary_check=(0, 1))
o_k = tl.arange(0, 64)
b_g0 = tl.load(g + ((i_b * T + start + BT - 1) * H + i_h) * K + o_k).to(tl.float32)
b_g1 = tl.load(g + ((i_b * T + start + BT - 1) * H + i_h) * K + 64 + o_k).to(tl.float32)
b_h0 *= tl.exp2(b_g0)[:, None]
b_h1 *= tl.exp2(b_g1)[:, None]
b_v_bf = b_v.to(kg.dtype.element_ty)
p_k0 = tl.make_block_ptr(kg + (i_b * T * H + i_h) * K, (K, T), (1, H * K), (0, start), (64, BT), (0, 1))
p_k1 = tl.make_block_ptr(kg + (i_b * T * H + i_h) * K, (K, T), (1, H * K), (64, start), (64, BT), (0, 1))
b_k0 = tl.load(p_k0, boundary_check=(0, 1))
b_k1 = tl.load(p_k1, boundary_check=(0, 1))
b_h0 += tl.dot(b_k0, b_v_bf)
b_h1 += tl.dot(b_k1, b_v_bf)
@triton.jit
def _out_kernel(
q,
g,
vnew,
h,
aq,
out,
scale: tl.constexpr,
T: tl.constexpr,
H: tl.constexpr,
K: tl.constexpr,
V: tl.constexpr,
BT: tl.constexpr,
BK: tl.constexpr,
BV: tl.constexpr,
NT: tl.constexpr,
):
i_v = tl.program_id(0)
it = tl.program_id(1)
i_bh = tl.program_id(2)
i_b = i_bh // H
i_h = i_bh - i_b * H
start = it * BT
vv = i_v * BV
b_o = tl.zeros([BT, BV], dtype=tl.float32)
for kk in range(0, K, BK):
p_q = tl.make_block_ptr(q + (i_b * T * H + i_h) * K, (T, K), (H * K, 1), (start, kk), (BT, BK), (1, 0))
p_g = tl.make_block_ptr(g + (i_b * T * H + i_h) * K, (T, K), (H * K, 1), (start, kk), (BT, BK), (1, 0))
h_base = (((i_b * NT + it) * H + i_h) * K) * V
p_h = tl.make_block_ptr(h + h_base, (K, V), (V, 1), (kk, vv), (BK, BV), (1, 0))
b_q = tl.load(p_q, boundary_check=(0, 1))
b_g = tl.load(p_g, boundary_check=(0, 1)).to(tl.float32)
b_qg = (b_q * tl.exp2(b_g)).to(b_q.dtype)
b_h = tl.load(p_h, boundary_check=(0, 1))
b_o += tl.dot(b_qg, b_h)
b_o *= scale
o_i = tl.arange(0, BT)
lower = o_i[:, None] >= o_i[None, :]
p_a = tl.make_block_ptr(aq + (i_b * T * H + i_h) * BT, (T, BT), (H * BT, 1), (start, 0), (BT, BT), (1, 0))
p_v = tl.make_block_ptr(vnew + (i_b * T * H + i_h) * V, (T, V), (H * V, 1), (start, vv), (BT, BV), (1, 0))
b_a = tl.load(p_a, boundary_check=(0, 1))
b_a = tl.where(lower, b_a, 0.0).to(vnew.dtype.element_ty)
b_v = tl.load(p_v, boundary_check=(0, 1))
b_o += tl.dot(b_a, b_v)
p_o = tl.make_block_ptr(out + (i_b * T * H + i_h) * V, (T, V), (H * V, 1), (start, vv), (BT, BV), (1, 0))
tl.store(p_o, b_o.to(out.dtype.element_ty), boundary_check=(0, 1))
def _triton_forward(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, g: torch.Tensor, beta: torch.Tensor, scale: float) -> torch.Tensor:
B, T, H, K = q.shape
V = v.shape[-1]
BT = 64
BC = 16
BK = 64
BV = 64
NT = T // BT
g2 = torch.empty_like(g, dtype=torch.float32)
aq = torch.empty((B, T, H, BT), device=q.device, dtype=q.dtype)
ai_diag = torch.empty((B, T, H, BC), device=q.device, dtype=torch.float32)
ai = torch.empty((B, T, H, BT), device=q.device, dtype=q.dtype)
w = torch.empty((B, T, H, K), device=q.device, dtype=q.dtype)
u = torch.empty((B, T, H, V), device=q.device, dtype=q.dtype)
kg = torch.empty((B, T, H, K), device=q.device, dtype=q.dtype)
h = torch.empty((B, NT, H, K, V), device=q.device, dtype=q.dtype)
vnew = torch.empty_like(v)
out = torch.empty_like(v)
_gate_cumsum_kernel[(NT, B * H)](g, g2, T=T, H=H, K=K, BT=BT, BK=128, num_warps=8)
_diag_block_kernel[(NT, BT // BC, B * H)](
q, k, g2, beta, aq, ai_diag,
scale=scale, T=T, H=H, K=K, BT=BT, BC=BC, BK=128,
num_warps=1,
)
_solve_kernel[(NT, B * H)](
q, k, g2, beta, aq, ai_diag, ai,
scale=scale, T=T, H=H, K=K, BT=BT, BC=BC, BK=BK,
num_warps=2,
)
_uw_kernel[(NT, B * H)](
k, v, beta, ai, g2, w, u, kg,
T=T, H=H, K=K, V=V, BT=BT, BK=BK, BV=BV,
num_warps=4,
)
_state_kernel[(triton.cdiv(V, BV), B * H)](
kg, w, u, g2, h, vnew,
T=T, H=H, K=K, V=V, BT=BT, BV=BV, NT=NT,
num_warps=4,
)
_out_kernel[(triton.cdiv(V, BV), NT, B * H)](
q, g2, vnew, h, aq, out,
scale=scale, T=T, H=H, K=K, V=V, BT=BT, BK=BK, BV=BV, NT=NT,
num_warps=4,
)
return out
class Model(nn.Module):
def __init__(self, B: int, T: int, H: int, K: int, V: int, chunk_size: int = 64):
super().__init__()
self.B, self.T, self.H, self.K, self.V = B, T, H, K, V
self.chunk_size = chunk_size
self.scale = float(K) ** -0.5
self.register_buffer("_dummy", torch.zeros(1), persistent=False)
def forward(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
g: torch.Tensor,
beta: torch.Tensor,
) -> torch.Tensor:
return _triton_forward(q, k, v, g, beta, self.scale)
B = 2
T = 1024
H = 8
K = 128
V = 128
CHUNK_SIZE = 64
def get_inputs():
torch.manual_seed(0)
q = torch.randn(B, T, H, K, dtype=torch.bfloat16) * 0.1
k = torch.randn(B, T, H, K, dtype=torch.bfloat16) * 0.1
v = torch.randn(B, T, H, V, dtype=torch.bfloat16) * 0.1
g = torch.randn(B, T, H, K, dtype=torch.float32) * 0.1 - 0.05
beta = torch.sigmoid(torch.randn(B, T, H, dtype=torch.bfloat16))
return [q, k, v, g, beta]
def get_init_inputs():
return [B, T, H, K, V, CHUNK_SIZE]
20260618_041809_codex_gpt-5.5_02_kda_cutlass