|
| 1 | +mod activation_descriptor; |
| 2 | +mod activation_mode; |
| 3 | + |
| 4 | +pub use activation_descriptor::*; |
| 5 | +pub use activation_mode::*; |
| 6 | + |
| 7 | +use crate::{private, sys, CudnnContext, CudnnError, DataType, IntoResult, TensorDescriptor}; |
| 8 | +use cust::memory::GpuBuffer; |
| 9 | +use std::mem::MaybeUninit; |
| 10 | + |
| 11 | +impl CudnnContext { |
| 12 | + /// Applies a specific neuron activation functions element wise of the provided tensor. |
| 13 | + /// |
| 14 | + /// # Arguments |
| 15 | + /// |
| 16 | + /// * `activation_desc` - activation descriptor. |
| 17 | + /// |
| 18 | + /// * `alpha` - scaling factor for the result. |
| 19 | + /// |
| 20 | + /// * `x_desc` - tensor descriptor for the input. |
| 21 | + /// |
| 22 | + /// * `x` - data for the input tensor. |
| 23 | + /// |
| 24 | + /// * `beta` - scaling factor for the destination tensor. |
| 25 | + /// |
| 26 | + /// * `y_desc` - tensor descriptor for the output. |
| 27 | + /// |
| 28 | + /// * `y` - data for the output. |
| 29 | + /// |
| 30 | + /// # Errors |
| 31 | + /// |
| 32 | + /// Returns errors if the shapes of the `y` and `x` tensors do not match or an unsupported |
| 33 | + /// configuration of arguments is detected. |
| 34 | + /// |
| 35 | + /// # Examples |
| 36 | + /// |
| 37 | + /// ``` |
| 38 | + /// # use std::error::Error; |
| 39 | + /// # |
| 40 | + /// # fn main() -> Result<(), Box<dyn Error>> { |
| 41 | + /// use cudnn::{ActivationDescriptor, ActivationMode, CudnnContext, NanPropagation, TensorDescriptor}; |
| 42 | + /// use cust::memory::DeviceBuffer; |
| 43 | + /// |
| 44 | + /// let ctx = CudnnContext::new()?; |
| 45 | + /// |
| 46 | + /// let mode = ActivationMode::Swish; |
| 47 | + /// let nan_opt = NanPropagation::PropagateNaN; |
| 48 | + /// let coefficient = None; |
| 49 | + /// |
| 50 | + /// let desc = ActivationDescriptor::new(mode, nan_opt, coefficient)?; |
| 51 | + /// |
| 52 | + /// let alpha: f32 = 1.0; |
| 53 | + /// let x_desc = TensorDescriptor::<i8>::new_strides(&[1, 1, 1, 5], &[5, 5, 5, 1])?; |
| 54 | + /// let x = DeviceBuffer::<i8>::from_slice(&[10, 10, 10, 10, 10])?; |
| 55 | + /// |
| 56 | + /// let beta: f32 = 0.0; |
| 57 | + /// let y_desc = TensorDescriptor::<i8>::new_strides(&[1, 1, 1, 5], &[5, 5, 5, 1])?; |
| 58 | + /// let mut y = DeviceBuffer::<i8>::from_slice(&[0, 0, 0, 0, 0])?; |
| 59 | + /// |
| 60 | + /// ctx.activation_forward(&desc, alpha, &x_desc, &x, beta, &y_desc, &mut y)?; |
| 61 | + /// |
| 62 | + /// let y_host = y.as_host_vec()?; |
| 63 | + /// |
| 64 | + /// assert!(y_host.iter().all(|el| *el == 10)); |
| 65 | + /// # Ok(()) |
| 66 | + /// # } |
| 67 | + /// ``` |
| 68 | + pub fn activation_forward<CompT, T>( |
| 69 | + &self, |
| 70 | + activation_desc: &ActivationDescriptor, |
| 71 | + alpha: CompT, |
| 72 | + x_desc: &TensorDescriptor<T>, |
| 73 | + x: &impl GpuBuffer<T>, |
| 74 | + beta: CompT, |
| 75 | + y_desc: &TensorDescriptor<T>, |
| 76 | + y: &mut impl GpuBuffer<T>, |
| 77 | + ) -> Result<(), CudnnError> |
| 78 | + where |
| 79 | + CompT: SupportedActFwd<T>, |
| 80 | + T: DataType, |
| 81 | + { |
| 82 | + let alpha_ptr = &alpha as *const CompT as *const _; |
| 83 | + let x_ptr = x.as_device_ptr().as_ptr() as *const _; |
| 84 | + |
| 85 | + let beta_ptr = &beta as *const CompT as *const _; |
| 86 | + let y_ptr = y.as_device_ptr().as_mut_ptr() as *mut _; |
| 87 | + |
| 88 | + unsafe { |
| 89 | + sys::cudnnActivationForward( |
| 90 | + self.raw, |
| 91 | + activation_desc.raw, |
| 92 | + alpha_ptr, |
| 93 | + x_desc.raw, |
| 94 | + x_ptr, |
| 95 | + beta_ptr, |
| 96 | + y_desc.raw, |
| 97 | + y_ptr, |
| 98 | + ) |
| 99 | + .into_result() |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// Computes the gradient of a neuron activation function. |
| 104 | + /// |
| 105 | + /// # Arguments |
| 106 | + /// |
| 107 | + /// * `activation_descriptor` - descriptor of a neuron activation operation. |
| 108 | + /// |
| 109 | + /// * `alpha` - scaling factor for the result. |
| 110 | + /// |
| 111 | + /// * `y_desc` - tensor descriptor for the output map. |
| 112 | + /// |
| 113 | + /// * `y` - data for the output map. |
| 114 | + /// |
| 115 | + /// * `dy_desc` - tensor descriptor for the differential of the output map. |
| 116 | + /// |
| 117 | + /// * `dy` - data foe the differential of the output map. |
| 118 | + /// |
| 119 | + /// * `x_desc` - tensor descriptor for the activation input. |
| 120 | + /// |
| 121 | + /// * `x` - data for the activation input. |
| 122 | + /// |
| 123 | + /// * `beta` - scaling factor for the destination tensor. |
| 124 | + /// |
| 125 | + /// * `dx_desc` - tensor descriptor for the input differential. |
| 126 | + /// |
| 127 | + /// * `dx` - data for the input differential. |
| 128 | + /// |
| 129 | + /// # Errors |
| 130 | + /// |
| 131 | + /// Returns errors if the shapes of the `dx` and `x` tensors do not match, the strides of the |
| 132 | + /// tensors and their differential do not match, or an unsupported configuration of arguments |
| 133 | + /// is detected. |
| 134 | + pub fn activation_backward<CompT, T>( |
| 135 | + &self, |
| 136 | + activation_desc: &ActivationDescriptor, |
| 137 | + alpha: CompT, |
| 138 | + y_desc: &TensorDescriptor<T>, |
| 139 | + y: &impl GpuBuffer<T>, |
| 140 | + dy_desc: &TensorDescriptor<T>, |
| 141 | + dy: &impl GpuBuffer<T>, |
| 142 | + x_desc: &TensorDescriptor<T>, |
| 143 | + x: &impl GpuBuffer<T>, |
| 144 | + beta: CompT, |
| 145 | + dx_desc: &TensorDescriptor<T>, |
| 146 | + dx: &mut impl GpuBuffer<T>, |
| 147 | + ) -> Result<(), CudnnError> |
| 148 | + where |
| 149 | + CompT: SupportedActBwd<T>, |
| 150 | + T: DataType, |
| 151 | + { |
| 152 | + let alpha_ptr = &alpha as *const CompT as *const _; |
| 153 | + |
| 154 | + let y_ptr = y.as_device_ptr().as_ptr() as *const _; |
| 155 | + let dy_ptr = dy.as_device_ptr().as_ptr() as *const _; |
| 156 | + let x_ptr = x.as_device_ptr().as_ptr() as *const _; |
| 157 | + |
| 158 | + let beta_ptr = &beta as *const CompT as *const _; |
| 159 | + |
| 160 | + let dx_ptr = dx.as_device_ptr().as_mut_ptr() as *mut _; |
| 161 | + |
| 162 | + unsafe { |
| 163 | + sys::cudnnActivationBackward( |
| 164 | + self.raw, |
| 165 | + activation_desc.raw, |
| 166 | + alpha_ptr, |
| 167 | + y_desc.raw, |
| 168 | + y_ptr, |
| 169 | + dy_desc.raw, |
| 170 | + dy_ptr, |
| 171 | + x_desc.raw, |
| 172 | + x_ptr, |
| 173 | + beta_ptr, |
| 174 | + dx_desc.raw, |
| 175 | + dx_ptr, |
| 176 | + ) |
| 177 | + .into_result() |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +/// Supported data type configurations for the activation forward operation. |
| 183 | +pub trait SupportedActFwd<T>: DataType + private::Sealed |
| 184 | +where |
| 185 | + T: DataType, |
| 186 | +{ |
| 187 | +} |
| 188 | + |
| 189 | +impl SupportedActFwd<i8> for f32 {} |
| 190 | +impl SupportedActFwd<u8> for f32 {} |
| 191 | +impl SupportedActFwd<i32> for f32 {} |
| 192 | +impl SupportedActFwd<i64> for f32 {} |
| 193 | +impl SupportedActFwd<f32> for f32 {} |
| 194 | +impl SupportedActFwd<f64> for f32 {} |
| 195 | + |
| 196 | +impl SupportedActFwd<i8> for f64 {} |
| 197 | +impl SupportedActFwd<u8> for f64 {} |
| 198 | +impl SupportedActFwd<i32> for f64 {} |
| 199 | +impl SupportedActFwd<i64> for f64 {} |
| 200 | +impl SupportedActFwd<f32> for f64 {} |
| 201 | +impl SupportedActFwd<f64> for f64 {} |
| 202 | + |
| 203 | +/// Supported type configurations for the activation backward operation. |
| 204 | +pub trait SupportedActBwd<T>: DataType + private::Sealed |
| 205 | +where |
| 206 | + T: DataType, |
| 207 | +{ |
| 208 | +} |
| 209 | + |
| 210 | +impl SupportedActBwd<f32> for f32 {} |
| 211 | +impl SupportedActBwd<f64> for f64 {} |
0 commit comments