@@ -30,14 +30,7 @@ def flash_attn_no_pad(qkv,
3030 nheads = qkv .shape [- 2 ]
3131 x = rearrange (qkv , "b s three h d -> b s (three h d)" )
3232
33- # Handle version differences in flash_attn unpad_input
34- unpad_output = unpad_input (x , key_padding_mask )
35- if len (unpad_output ) == 5 :
36- x_unpad , indices , cu_seqlens , max_s , used_seqlens_in_batch = unpad_output
37- else :
38- # Older versions return 4 values or different order?
39- # Typically: x_unpad, indices, cu_seqlens, max_s
40- x_unpad , indices , cu_seqlens , max_s = unpad_output [:4 ]
33+ x_unpad , indices , cu_seqlens , max_s , _ = unpad_input (x , key_padding_mask )
4134
4235 x_unpad = rearrange (x_unpad ,
4336 "nnz (three h d) -> nnz three h d" ,
@@ -76,32 +69,12 @@ def flash_attn_no_pad_v3(qkv,
7669 batch_size , seqlen , _ , nheads , head_dim = qkv .shape
7770 query , key , value = qkv .unbind (dim = 2 )
7871
79- # Handle version differences in flash_attn unpad_input for query
80- unpad_output_q = unpad_input (rearrange (query , "b s h d -> b s (h d)" ),
81- key_padding_mask )
82- if len (unpad_output_q ) >= 4 :
83- query_unpad , indices , cu_seqlens_q , max_seqlen_q = unpad_output_q [:4 ]
84- else :
85- raise ValueError (
86- f"Unexpected unpad_input output length: { len (unpad_output_q )} " )
87-
88- # Handle version differences for key
89- unpad_output_k = unpad_input (rearrange (key , "b s h d -> b s (h d)" ),
90- key_padding_mask )
91- if len (unpad_output_k ) >= 3 :
92- key_unpad , _ , cu_seqlens_k = unpad_output_k [:3 ]
93- else :
94- raise ValueError (
95- f"Unexpected unpad_input output length: { len (unpad_output_k )} " )
96-
97- # Handle version differences for value
98- unpad_output_v = unpad_input (rearrange (value , "b s h d -> b s (h d)" ),
99- key_padding_mask )
100- if len (unpad_output_v ) >= 1 :
101- value_unpad = unpad_output_v [0 ]
102- else :
103- raise ValueError (
104- f"Unexpected unpad_input output length: { len (unpad_output_v )} " )
72+ query_unpad , indices , cu_seqlens_q , max_seqlen_q , _ = unpad_input (
73+ rearrange (query , "b s h d -> b s (h d)" ), key_padding_mask )
74+ key_unpad , _ , cu_seqlens_k , _ , _ = unpad_input (
75+ rearrange (key , "b s h d -> b s (h d)" ), key_padding_mask )
76+ value_unpad , _ , _ , _ , _ = unpad_input (
77+ rearrange (value , "b s h d -> b s (h d)" ), key_padding_mask )
10578
10679 query_unpad = rearrange (query_unpad , "nnz (h d) -> nnz h d" , h = nheads )
10780 key_unpad = rearrange (key_unpad , "nnz (h d) -> nnz h d" , h = nheads )
0 commit comments