Skip to content

Commit cbc3a6b

Browse files
authored
[Feat] Support VSA with any resolution. (hao-ai-lab#650)
1 parent 2fa8d4a commit cbc3a6b

25 files changed

Lines changed: 982 additions & 1964 deletions

.github/workflows/pr-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ jobs:
326326
volume_size: 100
327327
disk_size: 100
328328
image: "ghcr.io/${{ github.repository }}/fastvideo-dev:py3.12-latest"
329-
test_command: "uv pip install -e .[test] && python csrc/attn/tests/test_block_sparse.py"
329+
test_command: "uv pip install -e .[test] && python csrc/attn/tests/test_vsa.py"
330330
timeout_minutes: 30
331331
secrets:
332332
RUNPOD_API_KEY: ${{ secrets.RUNPOD_API_KEY }}

csrc/attn/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
# Attention Kernel Used in FastVideo
44

5-
## Sliding Tile Attention (STA)
6-
We only support H100 for STA.
7-
```bash
8-
git submodule update --init --recursive
9-
python setup_sta.py install
10-
```
5+
116

127
## Video Sparse Attention (VSA)
138
We support H100 (via TK) and RTX 4090 (via triton) for VSA.
@@ -16,6 +11,7 @@ git submodule update --init --recursive
1611
python setup_vsa.py install
1712
```
1813

14+
1915
If you encounter error during installation, try below:
2016
Install C++20 for ThunderKittens:
2117
```bash
@@ -35,9 +31,17 @@ export LD_LIBRARY_PATH=${CUDA_HOME}/lib64:$LD_LIBRARY_PATH
3531
```
3632

3733

34+
## Sliding Tile Attention (STA)
35+
We only support H100 for STA.
36+
```bash
37+
git submodule update --init --recursive
38+
python setup_sta.py install
39+
```
40+
41+
3842

39-
## Usage
40-
### STA
43+
44+
### Usage
4145
End-2-end inference with FastVideo:
4246
```bash
4347
bash scripts/inference/v1_inference_wan_STA.sh
@@ -57,22 +61,19 @@ out = sliding_tile_attention(q, k, v, window_size, text_length)
5761
out = sliding_tile_attention(q, k, v, window_size, 0, False)
5862
```
5963

60-
### VSA
61-
We do not officially supoort end-2-end inference with VSA in FastVideo yet. Stay tuned.
62-
6364

64-
## Test
65+
### Test
6566
```bash
6667
python tests/test_sta.py # test STA
6768
python tests/test_block_sparse.py # test VSA
6869
```
69-
## Benchmark
70+
### Benchmark
7071
```bash
7172
python benchmarks/bench_sta.py
7273
```
7374

7475

75-
## How Does STA Work?
76+
### How Does STA Work?
7677
We give a demo for 2D STA with window size (6,6) operating on a (10, 10) image.
7778

7879

csrc/attn/benchmarks/bench_vsa_hopper.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import torch
22
import argparse
3-
from flash_attn.utils.benchmark import benchmark_forward
3+
from triton.testing import do_bench
44
from vsa import block_sparse_fwd, block_sparse_bwd
55
from vsa import BLOCK_M, BLOCK_N
6-
6+
import triton
77
import numpy as np
88
import random
99

@@ -23,7 +23,7 @@ def parse_arguments():
2323
parser = argparse.ArgumentParser(description='Benchmark Block Sparse Attention')
2424
parser.add_argument('--batch_size', type=int, default=1, help='Batch size')
2525
parser.add_argument('--num_heads', type=int, default=12, help='Number of heads')
26-
parser.add_argument('--head_dim', type=int, default=64, help='Head dimension')
26+
parser.add_argument('--head_dim', type=int, default=128, help='Head dimension')
2727
parser.add_argument('--topk', type=int, default=None, help='Number of kv blocks each q block attends to')
2828
parser.add_argument('--seq_lengths', type=int, nargs='+', default=[49152], help='Sequence lengths to benchmark')
2929
return parser.parse_args()
@@ -134,15 +134,14 @@ def benchmark_block_sparse_attention(q, k, v, q2k_block_sparse_index, q2k_block_
134134
torch.cuda.synchronize()
135135

136136
# Benchmark forward
137-
_, fwd_time = benchmark_forward(
138-
block_sparse_fwd,
139-
q, k, v, q2k_block_sparse_index, q2k_block_sparse_num,
140-
repeats=20,
141-
verbose=False,
142-
desc='Block Sparse Forward'
137+
fwd_time = do_bench(
138+
lambda: block_sparse_fwd(q, k, v, q2k_block_sparse_index, q2k_block_sparse_num),
139+
warmup=5,
140+
rep=20,
141+
quantiles=None
143142
)
144143

145-
sparse_tflops = flops / fwd_time.mean * 1e-12
144+
sparse_tflops = flops / fwd_time * 1e-12 * 1e3
146145
print(f"Block Sparse Forward - TFLOPS: {sparse_tflops:.2f}")
147146

148147
# Backward pass
@@ -154,16 +153,15 @@ def benchmark_block_sparse_attention(q, k, v, q2k_block_sparse_index, q2k_block_
154153
torch.cuda.synchronize()
155154

156155
# Benchmark backward
157-
_, bwd_time = benchmark_forward(
158-
block_sparse_bwd,
159-
q, k, v, o, l_vec, grad_output, k2q_block_sparse_index, k2q_block_sparse_num,
160-
repeats=20,
161-
verbose=False,
162-
desc='Block Sparse Backward'
156+
bwd_time = do_bench(
157+
lambda: block_sparse_bwd(q, k, v, o, l_vec, grad_output, k2q_block_sparse_index, k2q_block_sparse_num),
158+
warmup=5,
159+
rep=20,
160+
quantiles=None
163161
)
164162
bwd_flops = 2.5 * flops # Approximation
165163

166-
sparse_bwd_tflops = bwd_flops / bwd_time.mean * 1e-12
164+
sparse_bwd_tflops = bwd_flops / bwd_time * 1e-12 * 1e3
167165
print(f"Block Sparse Backward - TFLOPS: {sparse_bwd_tflops:.2f}")
168166

169167
return sparse_tflops, sparse_bwd_tflops

0 commit comments

Comments
 (0)