Skip to content

Commit db76ae3

Browse files
committed
pre-commit
1 parent 3eb096d commit db76ae3

12 files changed

Lines changed: 259 additions & 219 deletions

fastvideo/attention/ring/capabilities.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
HAS_FLASHINFER = True
3636

37-
def get_cuda_arch():
37+
def get_cuda_arch() -> str:
3838
major, minor = torch.cuda.get_device_capability()
3939
return f"{major}.{minor}"
4040

fastvideo/attention/ring/kernels/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def fn(
264264
raise ImportError("SparseSageAttention is only available with a SparseAttentionProcessor class passed in")
265265
if stage == "fwd-only":
266266

267-
def fn(q, k, v, causal=False, softmax_scale=None, *args, **kwargs):
267+
def sparse_sage_fn(q, k, v, causal=False, softmax_scale=None, *args, **kwargs):
268268
return (
269269
attn_processor(
270270
q,
@@ -277,7 +277,7 @@ def fn(q, k, v, causal=False, softmax_scale=None, *args, **kwargs):
277277
None,
278278
)
279279

280-
return fn
280+
return sparse_sage_fn
281281
else:
282282
raise ValueError(f"Unknown/Unsupported stage: {stage}")
283283

fastvideo/attention/ring/kernels/attention.py

Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import math
2+
from typing import Any
23

34
import torch
45

@@ -42,18 +43,18 @@
4243

4344

4445
def pytorch_attn_forward(
45-
q: torch.Tensor,
46-
k: torch.Tensor,
47-
v: torch.Tensor,
48-
dropout_p=0.0,
49-
softmax_scale=None,
50-
causal=True,
51-
window_size=(-1, -1),
52-
softcap=None,
53-
alibi_slopes=None,
54-
return_softmax=False,
55-
op_type="flash",
56-
):
46+
q: torch.Tensor,
47+
k: torch.Tensor,
48+
v: torch.Tensor,
49+
dropout_p: float = 0.0,
50+
softmax_scale: float | None = None,
51+
causal: bool = True,
52+
window_size: tuple[int, int] = (-1, -1),
53+
softcap: float | None = None,
54+
alibi_slopes: torch.Tensor | None = None,
55+
return_softmax: bool = False,
56+
op_type: str = "flash",
57+
) -> tuple[torch.Tensor, torch.Tensor]:
5758
assert op_type in ["flash", "efficient", "math", "cudnn"], f"Invalid op_type: {op_type}"
5859
"""
5960
q shape (bs, seqlen, nhead, hs)
@@ -145,42 +146,42 @@ def pytorch_attn_forward(
145146

146147

147148
def pytorch_attn_backward(
148-
dout,
149-
q,
150-
k,
151-
v,
152-
out,
153-
softmax_lse,
154-
block_dq_buffer=None, # Add new parameters with default values
155-
block_dk_buffer=None,
156-
block_dv_buffer=None,
157-
dropout_p=0.0,
158-
softmax_scale=None,
159-
bwd_causal=None, # This will replace the original causal parameter
160-
window_size=None,
161-
softcap=None,
162-
alibi_slopes=None,
163-
deterministic=True,
164-
rng_state=None,
165-
*args,
166-
**kwargs,
167-
):
149+
dout: torch.Tensor,
150+
q: torch.Tensor,
151+
k: torch.Tensor,
152+
v: torch.Tensor,
153+
out: torch.Tensor,
154+
softmax_lse: torch.Tensor,
155+
block_dq_buffer: torch.Tensor | None = None, # Add new parameters with default values
156+
block_dk_buffer: torch.Tensor | None = None,
157+
block_dv_buffer: torch.Tensor | None = None,
158+
dropout_p: float = 0.0,
159+
softmax_scale: float | None = None,
160+
bwd_causal: bool | None = None, # This will replace the original causal parameter
161+
window_size: tuple[int, int] | None = None,
162+
softcap: float | None = None,
163+
alibi_slopes: torch.Tensor | None = None,
164+
deterministic: bool = True,
165+
rng_state: Any | None = None,
166+
*args: Any,
167+
**kwargs: Any,
168+
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
168169
raise RuntimeError("Not implemented backward for PyTorch attention types")
169170
# TODO(optim): use pytorch _scaled_dot_product_efficient_attention_backward
170171
# Use efficient attention backward
171172
# https://github.com/pytorch/pytorch/blob/main/tools/autograd/derivatives.yaml#L2874
172173

173174

174-
def flash_attn_forward(q,
175-
k,
176-
v,
177-
dropout_p=0.0,
178-
softmax_scale=None,
179-
causal=False,
180-
window_size=(-1, -1),
181-
softcap=None,
182-
alibi_slopes=None,
183-
return_softmax=False):
175+
def flash_attn_forward(q: torch.Tensor,
176+
k: torch.Tensor,
177+
v: torch.Tensor,
178+
dropout_p: float = 0.0,
179+
softmax_scale: float | None = None,
180+
causal: bool = False,
181+
window_size: tuple[int, int] = (-1, -1),
182+
softcap: float | None = None,
183+
alibi_slopes: torch.Tensor | None = None,
184+
return_softmax: bool = False) -> tuple[torch.Tensor, torch.Tensor]:
184185
assert HAS_FLASH_ATTN, "FlashAttention is not available"
185186
if softmax_scale is None:
186187
softmax_scale = q.shape[-1]**(-0.5)
@@ -214,8 +215,11 @@ def flash_attn_forward(q,
214215
return block_out, block_lse
215216

216217

217-
def flash_attn_backward(dout, q, k, v, out, softmax_lse, block_dq_buffer, block_dk_buffer, block_dv_buffer, dropout_p,
218-
softmax_scale, bwd_causal, window_size, softcap, alibi_slopes, deterministic, rng_state):
218+
def flash_attn_backward(dout: torch.Tensor, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, out: torch.Tensor,
219+
softmax_lse: torch.Tensor, block_dq_buffer: torch.Tensor, block_dk_buffer: torch.Tensor,
220+
block_dv_buffer: torch.Tensor, dropout_p: float, softmax_scale: float | None, bwd_causal: bool,
221+
window_size: tuple[int, int], softcap: float | None, alibi_slopes: torch.Tensor | None,
222+
deterministic: bool, rng_state: Any) -> None:
219223
if softmax_scale is None:
220224
softmax_scale = q.shape[-1]**(-0.5)
221225
assert HAS_FLASH_ATTN
@@ -262,8 +266,10 @@ def flash_attn_backward(dout, q, k, v, out, softmax_lse, block_dq_buffer, block_
262266
)
263267

264268

265-
def flash_attn3_func_forward(q, k, v, dropout_p, softmax_scale, causal, window_size, softcap, alibi_slopes,
266-
return_softmax):
269+
def flash_attn3_func_forward(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, dropout_p: float,
270+
softmax_scale: float | None, causal: bool, window_size: tuple[int, int],
271+
softcap: float | None, alibi_slopes: torch.Tensor | None,
272+
return_softmax: bool) -> tuple[torch.Tensor, torch.Tensor]:
267273
assert HAS_FLASH_ATTN_HOPPER
268274
# current signature of flash_attn_forward_hopper:
269275
# (q, k, v, softmax_scale, causal, window_size, descale_q=None, descale_k=None, descale_v=None, gqa_parallel=False)
@@ -307,9 +313,11 @@ def flash_attn3_func_forward(q, k, v, dropout_p, softmax_scale, causal, window_s
307313
return out, softmax_lse
308314

309315

310-
def flash_attn3_func_backward(dout, q, k, v, out, softmax_lse, block_dq_buffer, block_dk_buffer, block_dv_buffer,
311-
dropout_p, softmax_scale, bwd_causal, window_size, softcap, alibi_slopes, deterministic,
312-
rng_state):
316+
def flash_attn3_func_backward(dout: torch.Tensor, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, out: torch.Tensor,
317+
softmax_lse: torch.Tensor, block_dq_buffer: torch.Tensor, block_dk_buffer: torch.Tensor,
318+
block_dv_buffer: torch.Tensor, dropout_p: float, softmax_scale: float | None,
319+
bwd_causal: bool, window_size: tuple[int, int], softcap: float | None,
320+
alibi_slopes: torch.Tensor | None, deterministic: bool, rng_state: Any) -> None:
313321
# (dout, q, k, v, out, softmax_lse, dq, dk, dv, softmax_scale, causal):
314322
assert HAS_FLASH_ATTN_HOPPER, "FlashAttention Hopper is not available"
315323

@@ -338,16 +346,16 @@ def flash_attn3_func_backward(dout, q, k, v, out, softmax_lse, block_dq_buffer,
338346
)
339347

340348

341-
def flash_attn_forward_aiter(q,
342-
k,
343-
v,
344-
dropout_p=0.0,
345-
softmax_scale=None,
346-
causal=False,
347-
window_size=(-1, -1),
348-
softcap=None,
349-
alibi_slopes=None,
350-
return_softmax=False):
349+
def flash_attn_forward_aiter(q: torch.Tensor,
350+
k: torch.Tensor,
351+
v: torch.Tensor,
352+
dropout_p: float = 0.0,
353+
softmax_scale: float | None = None,
354+
causal: bool = False,
355+
window_size: tuple[int, int] = (-1, -1),
356+
softcap: float | None = None,
357+
alibi_slopes: torch.Tensor | None = None,
358+
return_softmax: bool = False) -> tuple[torch.Tensor, torch.Tensor]:
351359
assert HAS_AITER, "Aiter is not available"
352360
block_out, block_lse = flash_attn_func_aiter(
353361
q,
@@ -425,15 +433,15 @@ def flashinfer_attn_backbward(
425433
raise RuntimeError("Not implemented backward for AttnType.FLASHINFER")
426434

427435

428-
def npu_fused_attn_forward(q,
429-
k,
430-
v,
431-
head_num=None,
432-
input_layout="BSND",
433-
scale=None,
434-
pre_tokens=65535,
435-
next_tokens=65535):
436-
assert HAS_NPU, "torch_npu is not avaliable"
436+
def npu_fused_attn_forward(q: torch.Tensor,
437+
k: torch.Tensor,
438+
v: torch.Tensor,
439+
head_num: int | None = None,
440+
input_layout: str = "BSND",
441+
scale: float | None = None,
442+
pre_tokens: int = 65535,
443+
next_tokens: int = 65535) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
444+
assert HAS_NPU, "torch_npu is not available"
437445
attention_out, softmax_max, softmax_sum, _, _, _, _ = torch_npu.npu_fusion_attention_v2(q,
438446
k,
439447
v,
@@ -447,17 +455,17 @@ def npu_fused_attn_forward(q,
447455
return attention_out, softmax_max, softmax_sum
448456

449457

450-
def npu_fused_attn_backward(q,
451-
k,
452-
v,
453-
grad_attention_out,
454-
head_num=None,
455-
input_layout="BSND",
456-
softmax_max=None,
457-
softmax_sum=None,
458-
attention_in=None,
459-
scale_value=None):
460-
assert HAS_NPU, "torch_npu is not avaliable"
458+
def npu_fused_attn_backward(q: torch.Tensor,
459+
k: torch.Tensor,
460+
v: torch.Tensor,
461+
grad_attention_out: torch.Tensor,
462+
head_num: int | None = None,
463+
input_layout: str = "BSND",
464+
softmax_max: torch.Tensor | None = None,
465+
softmax_sum: torch.Tensor | None = None,
466+
attention_in: torch.Tensor | None = None,
467+
scale_value: float | None = None) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
468+
assert HAS_NPU, "torch_npu is not available"
461469
dq, dk, dv, _, _, _ = torch_npu.npu_fusion_attention_grad_v2(q,
462470
k,
463471
v,

fastvideo/attention/ring/ring_flash_attn.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ def ring_flash_attn_forward(
4040
out = None
4141
lse = None
4242

43-
next_k, next_v = None, None
43+
next_k: torch.Tensor | None = None
44+
next_v: torch.Tensor | None = None
4445

4546
for step in range(comm.world_size):
4647
if step + 1 != comm.world_size:
47-
next_k: torch.Tensor = comm.send_recv(k)
48-
next_v: torch.Tensor = comm.send_recv(v)
48+
next_k = comm.send_recv(k)
49+
next_v = comm.send_recv(v)
4950
comm.commit()
5051

5152
if not causal or step <= comm.rank:

fastvideo/attention/ring/ring_flash_attn_varlen.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
unflatten_varlen_lse,
2727
)
2828
except:
29-
from .utils import (
30-
flatten_varlen_lse,
31-
unflatten_varlen_lse,
29+
from .utils import ( # type: ignore[no-redef]
30+
flatten_varlen_lse, unflatten_varlen_lse,
3231
)
3332

3433

@@ -51,12 +50,13 @@ def ring_flash_attn_varlen_forward(
5150

5251
out = None
5352
lse = None
54-
next_k, next_v = None, None
53+
next_k: torch.Tensor | None = None
54+
next_v: torch.Tensor | None = None
5555

5656
for step in range(comm.world_size):
5757
if step + 1 != comm.world_size:
58-
next_k: torch.Tensor = comm.send_recv(k)
59-
next_v: torch.Tensor = comm.send_recv(v)
58+
next_k = comm.send_recv(k)
59+
next_v = comm.send_recv(v)
6060
comm.commit()
6161
if not causal or step <= comm.rank:
6262
assert HAS_FLASH_ATTN, "FlashAttention is not available"
@@ -93,34 +93,37 @@ def ring_flash_attn_varlen_forward(
9393

9494

9595
def ring_flash_attn_varlen_backward(
96-
process_group,
97-
dout,
98-
q,
99-
k,
100-
v,
101-
out,
102-
softmax_lse,
103-
cu_seqlens,
104-
max_seqlen,
105-
softmax_scale,
106-
dropout_p=0,
107-
causal=True,
108-
window_size=(-1, -1),
109-
softcap=0.0,
110-
alibi_slopes=None,
111-
deterministic=False,
112-
):
96+
process_group,
97+
dout: torch.Tensor,
98+
q: torch.Tensor,
99+
k: torch.Tensor,
100+
v: torch.Tensor,
101+
out: torch.Tensor,
102+
softmax_lse: torch.Tensor,
103+
cu_seqlens: torch.Tensor,
104+
max_seqlen: int,
105+
softmax_scale: float | None,
106+
dropout_p: float = 0,
107+
causal: bool = True,
108+
window_size: tuple[int, int] = (-1, -1),
109+
softcap: float = 0.0,
110+
alibi_slopes: torch.Tensor | None = None,
111+
deterministic: bool = False,
112+
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
113113
kv_comm = RingComm(process_group)
114114
d_kv_comm = RingComm(process_group)
115-
dq, dk, dv = None, None, None
116-
next_dk, next_dv = None, None
115+
dq: torch.Tensor | None = None
116+
dk: torch.Tensor | None = None
117+
dv: torch.Tensor | None = None
118+
next_dk: torch.Tensor | None = None
119+
next_dv: torch.Tensor | None = None
120+
next_k: torch.Tensor | None = None
121+
next_v: torch.Tensor | None = None
117122

118123
block_dq_buffer = torch.empty(q.shape, dtype=q.dtype, device=q.device)
119124
block_dk_buffer = torch.empty(k.shape, dtype=k.dtype, device=k.device)
120125
block_dv_buffer = torch.empty(v.shape, dtype=v.dtype, device=v.device)
121126

122-
next_dk, next_dv = None, None
123-
next_k, next_v = None, None
124127
for step in range(kv_comm.world_size):
125128
if step + 1 != kv_comm.world_size:
126129
next_k = kv_comm.send_recv(k)

fastvideo/attention/ring/ring_flashinfer_attn.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ def ring_flashinfer_attn_forward(
3232
out = None
3333
lse = None
3434

35-
next_k, next_v = None, None
35+
next_k: torch.Tensor | None = None
36+
next_v: torch.Tensor | None = None
3637

3738
for step in range(comm.world_size):
3839
if step + 1 != comm.world_size:
39-
next_k: torch.Tensor = comm.send_recv(k)
40-
next_v: torch.Tensor = comm.send_recv(v)
40+
next_k = comm.send_recv(k)
41+
next_v = comm.send_recv(v)
4142
comm.commit()
4243

4344
if not causal or step <= comm.rank:

0 commit comments

Comments
 (0)