11import math
2+ from typing import Any
23
34import torch
45
4243
4344
4445def 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
147148def 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 ,
0 commit comments