summaryrefslogtreecommitdiff
path: root/volta_kernels
diff options
context:
space:
mode:
authorjdj <jdj@kunix.org>2026-10-29 11:50:22 -0700
committerjdj <jdj@kunix.org>2026-10-29 11:50:22 -0700
commita0ddfb1b2f6242fac72f98e8d6ee1888e79667f3 (patch)
treea6e19658bae8466cb177d2ade41ad9176d23c2f8 /volta_kernels
parent2a02fecc86a87a6f1639b80da932b1f9daed8f17 (diff)
downloadvolta-train-kernels-master.tar.gz
volta-train-kernels-master.zip
Validate offsets and canon_rope_attention shapes; default test device cuda:0 with safe skip; narrow README claimsHEADmaster
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'volta_kernels')
-rw-r--r--volta_kernels/__init__.py27
1 files changed, 22 insertions, 5 deletions
diff --git a/volta_kernels/__init__.py b/volta_kernels/__init__.py
index 4d183b0..ed3247e 100644
--- a/volta_kernels/__init__.py
+++ b/volta_kernels/__init__.py
@@ -27,7 +27,17 @@ def _load():
return _ext
-def tile_table(offsets: torch.Tensor):
+def _check_offsets(offsets: torch.Tensor, n_tokens: int) -> None:
+ """One host sync per offsets tensor (the table is cached): a wrong boundary list would
+ otherwise leave output rows unwritten or read past the end of q/k/v."""
+ if offsets.dim() != 1 or offsets.numel() < 2:
+ raise RuntimeError(f"offsets must be 1-D with at least 2 entries, got shape {tuple(offsets.shape)}")
+ o = offsets.tolist()
+ if o[0] != 0 or o[-1] != n_tokens or any(b < a for a, b in zip(o, o[1:])):
+ raise RuntimeError(f"offsets must rise from 0 to the token count {n_tokens}; got {o[0]} .. {o[-1]}")
+
+
+def tile_table(offsets: torch.Tensor, n_tokens: int):
"""(cu, tcum, perm) int32 for a document-offset tensor, cached on the tensor.
tcum is the cumulative 64-tile count over documents sorted LONGEST FIRST and
@@ -41,13 +51,14 @@ def tile_table(offsets: torch.Tensor):
tab = getattr(offsets, "_volta_tab", None)
# Keyed on the version counter too: an in-place edit of the offsets must
# never be served a stale table.
- if tab is None or tab[3] != offsets._version:
+ if tab is None or tab[3] != offsets._version or tab[4] != n_tokens:
+ _check_offsets(offsets, n_tokens)
cu = offsets.to(torch.int32)
tiles = (cu[1:] - cu[:-1] + 63) // 64
order = torch.argsort(tiles, descending=True, stable=True)
tcum = torch.zeros_like(cu)
tcum[1:] = torch.cumsum(tiles[order], 0)
- tab = (cu, tcum, order.to(torch.int32), offsets._version)
+ tab = (cu, tcum, order.to(torch.int32), offsets._version, n_tokens)
offsets._volta_tab = tab
return tab[0], tab[1], tab[2]
@@ -76,7 +87,7 @@ def varlen_causal_attention(q, k, v, offsets, scale: float | None = None):
"""
if scale is None:
scale = q.shape[-1] ** -0.5
- cu, tcum, perm = tile_table(offsets)
+ cu, tcum, perm = tile_table(offsets, q.shape[0])
return _VarlenAttn.apply(q, k, v, cu, tcum, perm, scale)
@@ -99,7 +110,7 @@ class _CanonRopeAttn(torch.autograd.Function):
m1f, m2f = m1.reshape(N).contiguous(), m2.reshape(N).contiguous()
cf, sf = cos.reshape(N, 32).contiguous(), sin.reshape(N, 32).contiguous()
q, k, v = canon_ext.fwd(qkv, w.contiguous(), m1f, m2f, cf, sf, 4, 64, 4, 1)
- cu, tcum, perm = tile_table(offsets)
+ cu, tcum, perm = tile_table(offsets, N)
qv, kv, vv = q.view(N, 8, 64), k.view(N, 8, 64), v.view(N, 8, 64)
o, lse = _load().fwd(qv, kv, vv, cu, tcum, perm, scale)
ctx.save_for_backward(qkv, w, m1f, m2f, cf, sf, qv, kv, vv, o, lse, cu, tcum, perm)
@@ -120,4 +131,10 @@ class _CanonRopeAttn(torch.autograd.Function):
def canon_rope_attention(qkv, canon_w, m1, m2, cos, sin, offsets):
"""qkv (B, T, 1536) fp16 -> Canon + RoPE -> causal varlen attention -> (N, 8, 64)."""
+ if qkv.dim() != 3 or qkv.shape[-1] != 1536:
+ raise RuntimeError(f"qkv must be (B, T, 1536) (8 heads x 64 x q|k|v), got {tuple(qkv.shape)}")
+ if qkv.dtype != torch.float16 or not qkv.is_contiguous():
+ raise RuntimeError("qkv must be contiguous fp16")
+ if qkv.shape[1] % 128:
+ raise RuntimeError(f"T must be a multiple of 128 (the Canon backward's chunk), got {qkv.shape[1]}")
return _CanonRopeAttn.apply(qkv, canon_w, m1, m2, cos, sin, offsets, 0.125)