@@ -95,6 +95,7 @@ impl FlashAttn {
9595 _ => 0 ,
9696 } ;
9797
98+ let stream = dev. cuda_stream ( ) ;
9899 let alibi_slopes_ptr = if let Some ( alibi_slopes) = & self . alibi_slopes {
99100 if alibi_slopes. dtype ( ) != DType :: F32 {
100101 candle:: bail!(
@@ -121,7 +122,9 @@ impl FlashAttn {
121122
122123 let alibi_slopes = alibi_slopes. slice ( alibi_slopes_layout. start_offset ( ) ..) ;
123124
124- * alibi_slopes. device_ptr ( ) as * const core:: ffi:: c_void
125+ // Dropping the guard here doesn't seem very safe.
126+ let ( ptr, _guard) = alibi_slopes. device_ptr ( & stream) ;
127+ ptr as * const core:: ffi:: c_void
125128 } else {
126129 std:: ptr:: null ( )
127130 } ;
@@ -168,17 +171,17 @@ impl FlashAttn {
168171 }
169172
170173 unsafe {
171- let q_ptr = * q. device_ptr ( ) as * const core :: ffi :: c_void ;
172- let k_ptr = * k. device_ptr ( ) as * const core :: ffi :: c_void ;
173- let v_ptr = * v. device_ptr ( ) as * const core :: ffi :: c_void ;
174- let dst_ptr = * dst. device_ptr ( ) as * const core :: ffi :: c_void ;
175- let softmax_lse_ptr = * softmax_lse. device_ptr ( ) as * const core :: ffi :: c_void ;
174+ let ( q_ptr, _guard ) = q. device_ptr ( & stream ) ;
175+ let ( k_ptr, _guard ) = k. device_ptr ( & stream ) ;
176+ let ( v_ptr, _guard ) = v. device_ptr ( & stream ) ;
177+ let ( dst_ptr, _guard ) = dst. device_ptr ( & stream ) ;
178+ let ( softmax_lse_ptr, _guard ) = softmax_lse. device_ptr ( & stream ) ;
176179 ffi:: run_mha (
177- q_ptr,
178- k_ptr,
179- v_ptr,
180- dst_ptr,
181- softmax_lse_ptr,
180+ q_ptr as * const core :: ffi :: c_void ,
181+ k_ptr as * const core :: ffi :: c_void ,
182+ v_ptr as * const core :: ffi :: c_void ,
183+ dst_ptr as * const core :: ffi :: c_void ,
184+ softmax_lse_ptr as * const core :: ffi :: c_void ,
182185 /* alibi_slopes_ptr */ alibi_slopes_ptr,
183186 /* cu_seqlens_q_ptr */ std:: ptr:: null ( ) ,
184187 /* cu_seqlens_k_ptr */ std:: ptr:: null ( ) ,
@@ -529,6 +532,7 @@ impl FlashAttnVarLen {
529532
530533 let batch_size = nseqlens_q - 1 ;
531534
535+ let stream = dev. cuda_stream ( ) ;
532536 let alibi_slopes_ptr = if let Some ( alibi_slopes) = & self . alibi_slopes {
533537 if alibi_slopes. dtype ( ) != DType :: F32 {
534538 candle:: bail!(
@@ -555,7 +559,9 @@ impl FlashAttnVarLen {
555559
556560 let alibi_slopes = alibi_slopes. slice ( alibi_slopes_layout. start_offset ( ) ..) ;
557561
558- * alibi_slopes. device_ptr ( ) as * const core:: ffi:: c_void
562+ // Dropping the guard here doesn't seem very safe.
563+ let ( ptr, _guard) = alibi_slopes. device_ptr ( & stream) ;
564+ ptr as * const core:: ffi:: c_void
559565 } else {
560566 std:: ptr:: null ( )
561567 } ;
@@ -605,22 +611,22 @@ impl FlashAttnVarLen {
605611 window_size_right = self . max_seqlen_k as i32 ;
606612 }
607613 unsafe {
608- let q_ptr = * q. device_ptr ( ) as * const core :: ffi :: c_void ;
609- let k_ptr = * k. device_ptr ( ) as * const core :: ffi :: c_void ;
610- let v_ptr = * v. device_ptr ( ) as * const core :: ffi :: c_void ;
611- let dst_ptr = * dst. device_ptr ( ) as * const core :: ffi :: c_void ;
612- let softmax_lse_ptr = * softmax_lse. device_ptr ( ) as * const core :: ffi :: c_void ;
613- let seqlens_q_ptr = * seqlens_q. device_ptr ( ) as * const core :: ffi :: c_int ;
614- let seqlens_k_ptr = * seqlens_k. device_ptr ( ) as * const core :: ffi :: c_int ;
614+ let ( q_ptr, _guard ) = q. device_ptr ( & stream ) ;
615+ let ( k_ptr, _guard ) = k. device_ptr ( & stream ) ;
616+ let ( v_ptr, _guard ) = v. device_ptr ( & stream ) ;
617+ let ( dst_ptr, _guard ) = dst. device_ptr ( & stream ) ;
618+ let ( softmax_lse_ptr, _guard ) = softmax_lse. device_ptr ( & stream ) ;
619+ let ( seqlens_q_ptr, _guard ) = seqlens_q. device_ptr ( & stream ) ;
620+ let ( seqlens_k_ptr, _guard ) = seqlens_k. device_ptr ( & stream ) ;
615621 ffi:: run_mha (
616- q_ptr,
617- k_ptr,
618- v_ptr,
619- dst_ptr,
620- softmax_lse_ptr,
622+ q_ptr as * const core :: ffi :: c_void ,
623+ k_ptr as * const core :: ffi :: c_void ,
624+ v_ptr as * const core :: ffi :: c_void ,
625+ dst_ptr as * const core :: ffi :: c_void ,
626+ softmax_lse_ptr as * const core :: ffi :: c_void ,
621627 /* alibi_slopes_ptr */ alibi_slopes_ptr,
622- /* cu_seqlens_q_ptr */ seqlens_q_ptr,
623- /* cu_seqlens_k_ptr */ seqlens_k_ptr,
628+ /* cu_seqlens_q_ptr */ seqlens_q_ptr as * const i32 ,
629+ /* cu_seqlens_k_ptr */ seqlens_k_ptr as * const i32 ,
624630 /* q_batch_stride */ 0 ,
625631 /* k_batch_stride */ 0 ,
626632 /* v_batch_stride */ 0 ,
0 commit comments