Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -16,28 +16,26 @@ size_categories:
|
|
| 16 |
- 1K<n<10K
|
| 17 |
---
|
| 18 |
|
| 19 |
-
# Wan2.2 Production Values — VSA Block-Sparse Attention Inputs
|
| 20 |
|
| 21 |
Real, captured **production** inputs for the Video Sparse Attention (VSA) fine
|
| 22 |
block-sparse attention stage of **Wan2.2 T2V-A14B**, recorded from an actual
|
| 23 |
-
`generate.py` run at **832×480 / 81 frames**
|
| 24 |
-
|
| 25 |
-
kernel: optimize on **these tensors**, score against the reference at the stated
|
| 26 |
-
tolerance.
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
fast in a microbenchmark by exploiting an *input generator* (sliding-window /
|
| 32 |
-
head-broadcast `q2k_idx`, small-magnitude `N(0,1)` q/k, all-full blocks, loose
|
| 33 |
-
tolerance) — and then be wrong on real data. These captures remove that escape
|
| 34 |
-
hatch:
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
## Shapes (t2v-A14B @ 832×480 / 81f)
|
| 43 |
|
|
@@ -51,18 +49,9 @@ hatch:
|
|
| 51 |
S_padded = 39936 = 624 cubes × 64 (cube-major tiled/padded)
|
| 52 |
real tokens = 32760 = 21 × 30 × 52 (before cube padding)
|
| 53 |
Nq = Nkv = 624 = ceil(21/4)·ceil(30/4)·ceil(52/4) = 6 × 8 × 13
|
| 54 |
-
topk = 78 (87.5% sparsity
|
| 55 |
```
|
| 56 |
|
| 57 |
-
## Files
|
| 58 |
-
|
| 59 |
-
| file | schema | notes |
|
| 60 |
-
|---|---|---|
|
| 61 |
-
| `call_0021.pt` | `q,k,v,topk_idx,vbs` + `call,step,block` | step 0, block 21 |
|
| 62 |
-
| `call_0101.pt` | same | step 2, block 21 |
|
| 63 |
-
| `call_0139.pt` | same | step 3, block 19 |
|
| 64 |
-
| `call_outlier.pt` | `q,k,v,idx,vbs` + `o_cuda,o_triton,m_cuda,m_triton` | **outlier/overflow** capture; `idx` == `topk_idx`. Bundles the Triton reference output. A one-pass (no row-max-subtraction) kernel produces **833,360 non-finite values in 1 of 40 heads** here — the case any deployable kernel must survive. |
|
| 65 |
-
|
| 66 |
## Operator contract
|
| 67 |
|
| 68 |
Non-causal block-sparse attention, BSHD:
|
|
@@ -70,24 +59,57 @@ Non-causal block-sparse attention, BSHD:
|
|
| 70 |
out = softmax((q @ k_selected^T) / sqrt(D)) @ v_selected
|
| 71 |
```
|
| 72 |
For each `(batch, head, query_cube)`, `topk_idx` names the selected KV cubes;
|
| 73 |
-
`vbs[c]` masks padded tokens inside cube `c` (attend only the first `vbs[c]`
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
online softmax with row-max subtraction.
|
| 77 |
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
- **elementwise** max|Δ| ≤ ~3e-2, mean|Δ| ≤ ~1e-4 (bf16-level),
|
| 85 |
-
- **all outputs finite on every capture, including `call_outlier.pt`** (no NaN/Inf),
|
| 86 |
-
- timing measured warmed, kernel-only (CUDA events), same GPU (B200 / sm_100a).
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
## Load
|
| 93 |
|
|
@@ -97,8 +119,7 @@ d = torch.load("call_0021.pt", weights_only=True)
|
|
| 97 |
q, k, v = d["q"], d["k"], d["v"] # (1, 39936, 40, 128) bf16
|
| 98 |
topk_idx = d["topk_idx"] # (1, 40, 624, 78) int32
|
| 99 |
vbs = d["vbs"] # (624,) int32
|
| 100 |
-
# call_outlier.pt uses key "idx" instead of "topk_idx" and
|
| 101 |
-
# o_triton / m_triton as ground truth.
|
| 102 |
```
|
| 103 |
|
| 104 |
Captured from Wan2.2 T2V-A14B (Apache-2.0 model). Tensors are intermediate
|
|
|
|
| 16 |
- 1K<n<10K
|
| 17 |
---
|
| 18 |
|
| 19 |
+
# Wan2.2 Production Values — VSA Block-Sparse Attention Inputs + Reference
|
| 20 |
|
| 21 |
Real, captured **production** inputs for the Video Sparse Attention (VSA) fine
|
| 22 |
block-sparse attention stage of **Wan2.2 T2V-A14B**, recorded from an actual
|
| 23 |
+
`generate.py` run at **832×480 / 81 frames**, plus the **reference Triton kernel**
|
| 24 |
+
and the **scoring rule**.
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
This is a *fixed, non-gameable* benchmark for proposing a faster VSA forward
|
| 27 |
+
kernel: **optimize on these exact tensors, score against the reference at the
|
| 28 |
+
tolerance below.** You do **not** get to substitute your own input generator —
|
| 29 |
+
that loophole (sliding-window / head-broadcast `q2k_idx`, small-magnitude N(0,1)
|
| 30 |
+
q/k, all-full blocks, loose tolerance) is exactly what these captures close.
|
| 31 |
|
| 32 |
+
## Files
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
| file | contents |
|
| 35 |
+
|---|---|
|
| 36 |
+
| `standalone_kernel.py` | **reference kernel** `_attn_fwd_sparse` (`wan22:triton64`, verbatim from production) + host launcher `triton_block_sparse_attn_forward(q,k,v,topk_idx,vbs) -> (o, M)` |
|
| 37 |
+
| `call_0021.pt` / `call_0101.pt` / `call_0139.pt` | clean captures (steps 0/2/3): `q,k,v,topk_idx,vbs` (+ `call,step,block`) |
|
| 38 |
+
| `call_outlier.pt` | **outlier/overflow** capture: `q,k,v,idx,vbs` **plus bundled `o_triton`/`m_triton` ground truth** (`idx` == `topk_idx`). A one-pass softmax (no row-max subtraction) produces **833,360 non-finite values in 1 of 40 heads** here — the case any deployable kernel must survive. |
|
| 39 |
|
| 40 |
## Shapes (t2v-A14B @ 832×480 / 81f)
|
| 41 |
|
|
|
|
| 49 |
S_padded = 39936 = 624 cubes × 64 (cube-major tiled/padded)
|
| 50 |
real tokens = 32760 = 21 × 30 × 52 (before cube padding)
|
| 51 |
Nq = Nkv = 624 = ceil(21/4)·ceil(30/4)·ceil(52/4) = 6 × 8 × 13
|
| 52 |
+
topk = 78 (87.5% sparsity), D = 128, sm_scale = 1/sqrt(128)
|
| 53 |
```
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
## Operator contract
|
| 56 |
|
| 57 |
Non-causal block-sparse attention, BSHD:
|
|
|
|
| 59 |
out = softmax((q @ k_selected^T) / sqrt(D)) @ v_selected
|
| 60 |
```
|
| 61 |
For each `(batch, head, query_cube)`, `topk_idx` names the selected KV cubes;
|
| 62 |
+
`vbs[c]` masks padded tokens inside cube `c` (attend only the first `vbs[c]` of 64).
|
| 63 |
+
|
| 64 |
+
## Scoring / loss (the rule)
|
|
|
|
| 65 |
|
| 66 |
+
A candidate kernel `cand(q, k, v, topk_idx, vbs) -> o` (bf16, `(1,39936,40,128)`)
|
| 67 |
+
**passes** a capture iff, against the reference output `o_ref`:
|
| 68 |
|
| 69 |
+
1. **finite:** every element of `o` is finite (no NaN/Inf) — enforced on **every**
|
| 70 |
+
capture, including `call_outlier.pt`;
|
| 71 |
+
2. **per-head cosine ≥ 0.9999:** `min over (b,h)` of `cos(o[b,:,h,:], o_ref[b,:,h,:]) ≥ 0.9999`
|
| 72 |
+
(a global average can hide a few wrong heads — it is checked **per head**);
|
| 73 |
+
3. **elementwise:** `max|o − o_ref| ≤ 3e-2` and `mean|o − o_ref| ≤ 1e-4` (bf16-level).
|
| 74 |
|
| 75 |
+
`o_ref` is `triton_block_sparse_attn_forward(...)[0]` for the clean captures, and
|
| 76 |
+
the bundled `o_triton` for `call_outlier.pt`.
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
**Win condition:** pass all four captures, and have lower **warmed, kernel-only**
|
| 79 |
+
latency (CUDA events, B200 / sm_100a) than the deployed baseline. For reference,
|
| 80 |
+
the deployed CUDA kernel hits **cosine ≈ 0.999999** on the clean captures, stays
|
| 81 |
+
finite on the outlier head, and runs at **~4.7–5.0 ms** on these inputs.
|
| 82 |
+
|
| 83 |
+
### Reference scorer
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
import torch, glob, os
|
| 87 |
+
from standalone_kernel import triton_block_sparse_attn_forward
|
| 88 |
+
|
| 89 |
+
def per_head_cos(a, b): # a,b: (1, S, H, D)
|
| 90 |
+
a = a.float(); b = b.float()
|
| 91 |
+
a = a.permute(0,2,1,3).reshape(a.shape[2], -1) # (H, S*D)
|
| 92 |
+
b = b.permute(0,2,1,3).reshape(b.shape[2], -1)
|
| 93 |
+
return torch.nn.functional.cosine_similarity(a, b, dim=1) # (H,)
|
| 94 |
+
|
| 95 |
+
def score(cand, path):
|
| 96 |
+
d = torch.load(path, weights_only=True)
|
| 97 |
+
q, k, v = d["q"].cuda(), d["k"].cuda(), d["v"].cuda()
|
| 98 |
+
idx = d.get("topk_idx", d.get("idx")).cuda().contiguous()
|
| 99 |
+
vbs = d["vbs"].cuda().contiguous()
|
| 100 |
+
o_ref = d["o_triton"].cuda() if "o_triton" in d else \
|
| 101 |
+
triton_block_sparse_attn_forward(q, k, v, idx, vbs)[0]
|
| 102 |
+
o = cand(q, k, v, idx, vbs) # <-- your kernel
|
| 103 |
+
finite = bool(torch.isfinite(o).all())
|
| 104 |
+
coss = per_head_cos(o, o_ref)
|
| 105 |
+
diff = (o.float() - o_ref.float()).abs()
|
| 106 |
+
ok = finite and coss.min() >= 0.9999 and diff.max() <= 3e-2 and diff.mean() <= 1e-4
|
| 107 |
+
print(f"{os.path.basename(path):16s} finite={finite} "
|
| 108 |
+
f"min_head_cos={coss.min():.6f} max|Δ|={diff.max():.2e} -> {'PASS' if ok else 'FAIL'}")
|
| 109 |
+
return ok
|
| 110 |
+
|
| 111 |
+
# all(score(my_kernel, f) for f in sorted(glob.glob('call_*.pt')))
|
| 112 |
+
```
|
| 113 |
|
| 114 |
## Load
|
| 115 |
|
|
|
|
| 119 |
q, k, v = d["q"], d["k"], d["v"] # (1, 39936, 40, 128) bf16
|
| 120 |
topk_idx = d["topk_idx"] # (1, 40, 624, 78) int32
|
| 121 |
vbs = d["vbs"] # (624,) int32
|
| 122 |
+
# call_outlier.pt uses key "idx" instead of "topk_idx" and ships o_triton/m_triton.
|
|
|
|
| 123 |
```
|
| 124 |
|
| 125 |
Captured from Wan2.2 T2V-A14B (Apache-2.0 model). Tensors are intermediate
|