|
| 1 | +mod attention_descriptor; |
| 2 | +mod seq_data_axis; |
| 3 | +mod seq_data_descriptor; |
| 4 | + |
| 5 | +pub use attention_descriptor::*; |
| 6 | +pub use seq_data_axis::*; |
| 7 | +pub use seq_data_descriptor::*; |
| 8 | + |
| 9 | +use crate::{sys, CudnnContext, CudnnError, DataType, IntoResult}; |
| 10 | +use cust::memory::GpuBuffer; |
| 11 | +use std::mem::MaybeUninit; |
| 12 | + |
| 13 | +impl CudnnContext { |
| 14 | + /// This function computes weight, work, and reserve space buffer sizes used by the following |
| 15 | + /// functions: |
| 16 | + /// |
| 17 | + /// * `multi_head_attn_forward()` |
| 18 | + /// |
| 19 | + /// * `multi_head_attn_backward_data()` |
| 20 | + /// |
| 21 | + /// * `multi_head_attn_backward_weights()` |
| 22 | + /// |
| 23 | + /// # Arguments |
| 24 | + /// |
| 25 | + /// `desc` - multi-head attention descriptor. |
| 26 | + /// |
| 27 | + /// # Errors |
| 28 | + /// |
| 29 | + /// Returns errors if invalid arguments are detected. |
| 30 | + pub fn get_attn_buffers_size<T, U, D1, D2>( |
| 31 | + &self, |
| 32 | + desc: &AttentionDescriptor<T, U, D1, D2>, |
| 33 | + ) -> Result<(usize, usize, usize), CudnnError> |
| 34 | + where |
| 35 | + T: SeqDataType, |
| 36 | + U: SupportedAttn<T>, |
| 37 | + D1: GpuBuffer<u8>, |
| 38 | + D2: GpuBuffer<u8>, |
| 39 | + { |
| 40 | + let mut weight_space_size = MaybeUninit::uninit(); |
| 41 | + let mut work_space_size = MaybeUninit::uninit(); |
| 42 | + let mut reserve_space_size = MaybeUninit::uninit(); |
| 43 | + |
| 44 | + unsafe { |
| 45 | + sys::cudnnGetMultiHeadAttnBuffers( |
| 46 | + self.raw, |
| 47 | + desc.raw, |
| 48 | + weight_space_size.as_mut_ptr(), |
| 49 | + work_space_size.as_mut_ptr(), |
| 50 | + reserve_space_size.as_mut_ptr(), |
| 51 | + ) |
| 52 | + .into_result()?; |
| 53 | + |
| 54 | + Ok(( |
| 55 | + weight_space_size.assume_init(), |
| 56 | + work_space_size.assume_init(), |
| 57 | + reserve_space_size.assume_init(), |
| 58 | + )) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// Computes the forward response of a multi-head attention layer. |
| 63 | + /// |
| 64 | + /// When `reserve_space` is `None` the function operates in the inference mode in which backward |
| 65 | + /// functions are not invoked, otherwise, the training mode is assumed. |
| 66 | + /// |
| 67 | + /// # Arguments |
| 68 | + /// |
| 69 | + /// * `attn_desc` - multi-head attention descriptor. |
| 70 | + /// |
| 71 | + /// * `current_idx` - time-step in queries to process. When the such argument is negative, |
| 72 | + /// all Q time-steps are processed. When `current_idx` is zero or positive, the forward response |
| 73 | + /// is computed for the selected time-step only. |
| 74 | + /// |
| 75 | + /// * `lo_win_idx` - integer array specifying the start indices of the attention window for |
| 76 | + /// each Q time-step. The start index in K, V sets is inclusive. |
| 77 | + /// |
| 78 | + /// * `hi_win_idx` - integer array specifying the end indices of the attention window for each |
| 79 | + /// Q time-step. The end index is exclusive. |
| 80 | + /// |
| 81 | + /// * `device_seq_lengths_qo` - device array specifying sequence lengths of query, residual, |
| 82 | + /// and output sequence data. |
| 83 | + /// |
| 84 | + /// * `device_seq_lengths_kv` - device array specifying sequence lengths of key and value \ |
| 85 | + /// input data. |
| 86 | + /// |
| 87 | + /// * `q_desc` - descriptor for the query and residual sequence data. |
| 88 | + /// |
| 89 | + /// * `queries` - queries data in the device memory. |
| 90 | + /// |
| 91 | + /// * `residuals` - residual data in device memory. Set this argument to `None` if no residual |
| 92 | + /// connections are required. |
| 93 | + /// |
| 94 | + /// * `k_desc` - descriptor for the keys sequence data. |
| 95 | + /// |
| 96 | + /// * `keys` - keys data in device memory. |
| 97 | + /// |
| 98 | + /// * `v_desc` - descriptor for the values sequence data. |
| 99 | + /// |
| 100 | + /// * `values` - values data in device memory. |
| 101 | + /// |
| 102 | + /// * `o_desc` - descriptor for the out sequence data. |
| 103 | + /// |
| 104 | + /// * `out` - out data in device memory. |
| 105 | + /// |
| 106 | + /// * `weights` - weight buffer in device memory. |
| 107 | + /// |
| 108 | + /// * `work_space` - work space buffer in device memory. |
| 109 | + /// |
| 110 | + /// * `reserve_space` - reserve space buffer in device memory. This argument should be `None` in |
| 111 | + /// inference mode. |
| 112 | + pub fn multi_head_attn_forward<T, U, D1, D2>( |
| 113 | + &self, |
| 114 | + attn_desc: &AttentionDescriptor<T, U, D1, D2>, |
| 115 | + current_idx: i32, |
| 116 | + lo_win_idx: &[i32], |
| 117 | + hi_win_idx: &[i32], |
| 118 | + device_seq_lengths_qo: &impl GpuBuffer<i32>, |
| 119 | + device_seq_lengths_kv: &impl GpuBuffer<i32>, |
| 120 | + q_desc: &SeqDataDescriptor<T>, |
| 121 | + queries: &impl GpuBuffer<T>, |
| 122 | + residuals: Option<&impl GpuBuffer<T>>, |
| 123 | + k_desc: &SeqDataDescriptor<T>, |
| 124 | + keys: &impl GpuBuffer<T>, |
| 125 | + v_desc: &SeqDataDescriptor<T>, |
| 126 | + values: &impl GpuBuffer<T>, |
| 127 | + o_desc: &SeqDataDescriptor<T>, |
| 128 | + out: &mut impl GpuBuffer<T>, |
| 129 | + weights: &impl GpuBuffer<T>, |
| 130 | + work_space: &mut impl GpuBuffer<T>, |
| 131 | + reserve_space: Option<&mut impl GpuBuffer<T>>, |
| 132 | + ) -> Result<(), CudnnError> |
| 133 | + where |
| 134 | + T: SeqDataType, |
| 135 | + U: SupportedAttn<T>, |
| 136 | + D1: GpuBuffer<u8>, |
| 137 | + D2: GpuBuffer<u8>, |
| 138 | + { |
| 139 | + let device_seq_lenghts_qo_ptr = device_seq_lengths_qo.as_device_ptr().as_ptr() as *const _; |
| 140 | + let device_seq_lengths_kv_ptr = device_seq_lengths_kv.as_device_ptr().as_ptr() as *const _; |
| 141 | + |
| 142 | + let queries_ptr = queries.as_device_ptr().as_ptr() as *const _; |
| 143 | + let residuals_ptr = residuals.map_or(std::ptr::null(), |buff| { |
| 144 | + buff.as_device_ptr().as_ptr() as *const _ |
| 145 | + }); |
| 146 | + let keys_ptr = keys.as_device_ptr().as_ptr() as *const _; |
| 147 | + let values_ptr = values.as_device_ptr().as_ptr() as *const _; |
| 148 | + let out_ptr = out.as_device_ptr().as_mut_ptr() as *mut _; |
| 149 | + |
| 150 | + let weights_ptr = weights.as_device_ptr().as_ptr() as *const _; |
| 151 | + let work_space_ptr = work_space.as_device_ptr().as_mut_ptr() as *mut _; |
| 152 | + |
| 153 | + let (reserve_space_ptr, reserve_space_size) = reserve_space |
| 154 | + .map_or((std::ptr::null_mut(), 0), |buff| { |
| 155 | + (buff.as_device_ptr().as_mut_ptr() as *mut _, 0) |
| 156 | + }); |
| 157 | + |
| 158 | + unsafe { |
| 159 | + sys::cudnnMultiHeadAttnForward( |
| 160 | + self.raw, |
| 161 | + attn_desc.raw, |
| 162 | + current_idx, |
| 163 | + lo_win_idx.as_ptr(), |
| 164 | + hi_win_idx.as_ptr(), |
| 165 | + device_seq_lenghts_qo_ptr, |
| 166 | + device_seq_lengths_kv_ptr, |
| 167 | + q_desc.raw, |
| 168 | + queries_ptr, |
| 169 | + residuals_ptr, |
| 170 | + k_desc.raw, |
| 171 | + keys_ptr, |
| 172 | + v_desc.raw, |
| 173 | + values_ptr, |
| 174 | + o_desc.raw, |
| 175 | + out_ptr, |
| 176 | + weights.len(), |
| 177 | + weights_ptr, |
| 178 | + work_space.len(), |
| 179 | + work_space_ptr, |
| 180 | + reserve_space_size, |
| 181 | + reserve_space_ptr, |
| 182 | + ) |
| 183 | + .into_result()?; |
| 184 | + |
| 185 | + Ok(()) |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments