|
| 1 | +mod pooling_descriptor; |
| 2 | +mod pooling_mode; |
| 3 | + |
| 4 | +use cust::memory::GpuBuffer; |
| 5 | +pub use pooling_descriptor::*; |
| 6 | +pub use pooling_mode::*; |
| 7 | + |
| 8 | +use crate::{private, sys, CudnnContext, CudnnError, DataType, IntoResult, TensorDescriptor}; |
| 9 | + |
| 10 | +impl CudnnContext { |
| 11 | + /// This function computes the pooling of the input tensor and produces a smaller tensor in |
| 12 | + /// output. |
| 13 | + /// |
| 14 | + /// # Arguments |
| 15 | + /// |
| 16 | + /// * `pooling_desc` - descriptor of the pooling operation. |
| 17 | + /// |
| 18 | + /// * `alpha` - scaling factor for the result. |
| 19 | + /// |
| 20 | + /// * `x_desc` - descriptor for the input tensor. |
| 21 | + /// |
| 22 | + /// * `x` - data for the input tensor. |
| 23 | + /// |
| 24 | + /// * `beta` - scaling factor for the destination tensor. |
| 25 | + /// |
| 26 | + /// * `y_desc` - descriptor for the destination tensor. |
| 27 | + /// |
| 28 | + /// * `y` - data for the destination tensor. |
| 29 | + /// |
| 30 | + /// # Errors |
| 31 | + /// |
| 32 | + /// Returns errors if the batch size or channels dimensions of the two tensor differ or an |
| 33 | + /// invalid combination of arguments is detected. |
| 34 | + pub fn pooling_forward<T, U>( |
| 35 | + &self, |
| 36 | + pooling_desc: &PoolingDescriptor, |
| 37 | + alpha: T, |
| 38 | + x_desc: &TensorDescriptor<U>, |
| 39 | + x: &impl GpuBuffer<U>, |
| 40 | + beta: T, |
| 41 | + y_desc: &TensorDescriptor<U>, |
| 42 | + y: &mut impl GpuBuffer<U>, |
| 43 | + ) -> Result<(), CudnnError> |
| 44 | + where |
| 45 | + T: SupportedPoolFwd<U>, |
| 46 | + U: DataType, |
| 47 | + { |
| 48 | + let alpha_ptr = &alpha as *const T as *const _; |
| 49 | + let x_ptr = x.as_device_ptr().as_ptr() as *const _; |
| 50 | + |
| 51 | + let beta_ptr = &beta as *const T as *const _; |
| 52 | + let y_ptr = y.as_device_ptr().as_mut_ptr() as *mut _; |
| 53 | + |
| 54 | + unsafe { |
| 55 | + sys::cudnnPoolingForward( |
| 56 | + self.raw, |
| 57 | + pooling_desc.raw, |
| 58 | + alpha_ptr, |
| 59 | + x_desc.raw, |
| 60 | + x_ptr, |
| 61 | + beta_ptr, |
| 62 | + y_desc.raw, |
| 63 | + y_ptr, |
| 64 | + ) |
| 65 | + .into_result() |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Computes the gradient of a pooling operation. |
| 70 | + /// |
| 71 | + /// # Arguments |
| 72 | + /// |
| 73 | + /// * `pooling_desc` - descriptor of the pooling operation. |
| 74 | + /// |
| 75 | + /// * `alpha` - scaling factor for the result. |
| 76 | + /// |
| 77 | + /// * `y_desc` - tensor descriptor for the output map. |
| 78 | + /// |
| 79 | + /// * `y` - data for the output map. |
| 80 | + /// |
| 81 | + /// * `dy_desc` - tensor descriptor for the differential of the output map. |
| 82 | + /// |
| 83 | + /// * `dy` - data foe the differential of the output map. |
| 84 | + /// |
| 85 | + /// * `x_desc` - tensor descriptor for the dropout input. |
| 86 | + /// |
| 87 | + /// * `x` - data for the dropout input. |
| 88 | + /// |
| 89 | + /// * `beta` - scaling factor for the destination tensor. |
| 90 | + /// |
| 91 | + /// * `dx_desc` - tensor descriptor for the input differential. |
| 92 | + /// |
| 93 | + /// * `dx` - data for the input differential. |
| 94 | + /// |
| 95 | + /// # Errors |
| 96 | + /// |
| 97 | + /// Returns errors if the dimensions or the strides of `y` and `dy` tensors differ or if the |
| 98 | + /// dimensions or the strides of `x` and `dx` tensors differ or if an unsupported combination |
| 99 | + /// of arguments is detected. |
| 100 | + pub fn pooling_backward<T, U>( |
| 101 | + &self, |
| 102 | + pooling_desc: &PoolingDescriptor, |
| 103 | + alpha: T, |
| 104 | + y_desc: &TensorDescriptor<U>, |
| 105 | + y: &impl GpuBuffer<U>, |
| 106 | + dy_desc: &TensorDescriptor<U>, |
| 107 | + dy: &impl GpuBuffer<U>, |
| 108 | + x_desc: &TensorDescriptor<U>, |
| 109 | + x: &impl GpuBuffer<U>, |
| 110 | + beta: T, |
| 111 | + dx_desc: &TensorDescriptor<U>, |
| 112 | + dx: &mut impl GpuBuffer<U>, |
| 113 | + ) -> Result<(), CudnnError> |
| 114 | + where |
| 115 | + T: SupportedPoolBwd<U>, |
| 116 | + U: DataType, |
| 117 | + { |
| 118 | + let alpha_ptr = &alpha as *const T as *const _; |
| 119 | + |
| 120 | + let y_ptr = y.as_device_ptr().as_ptr() as *const _; |
| 121 | + let dy_ptr = dy.as_device_ptr().as_ptr() as *const _; |
| 122 | + let x_ptr = x.as_device_ptr().as_ptr() as *const _; |
| 123 | + |
| 124 | + let beta_ptr = &beta as *const T as *const _; |
| 125 | + |
| 126 | + let dx_ptr = dx.as_device_ptr().as_mut_ptr() as *mut _; |
| 127 | + |
| 128 | + unsafe { |
| 129 | + sys::cudnnPoolingBackward( |
| 130 | + self.raw, |
| 131 | + pooling_desc.raw, |
| 132 | + alpha_ptr, |
| 133 | + y_desc.raw, |
| 134 | + y_ptr, |
| 135 | + dy_desc.raw, |
| 136 | + dy_ptr, |
| 137 | + x_desc.raw, |
| 138 | + x_ptr, |
| 139 | + beta_ptr, |
| 140 | + dx_desc.raw, |
| 141 | + dx_ptr, |
| 142 | + ) |
| 143 | + .into_result() |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/// Supported data type configurations for the pooling forward operation. |
| 149 | +pub trait SupportedPoolFwd<T>: DataType + private::Sealed |
| 150 | +where |
| 151 | + T: DataType, |
| 152 | +{ |
| 153 | +} |
| 154 | + |
| 155 | +impl SupportedPoolFwd<i8> for f32 {} |
| 156 | +impl SupportedPoolFwd<u8> for f32 {} |
| 157 | +impl SupportedPoolFwd<i32> for f32 {} |
| 158 | +impl SupportedPoolFwd<i64> for f32 {} |
| 159 | +impl SupportedPoolFwd<f32> for f32 {} |
| 160 | +impl SupportedPoolFwd<f64> for f32 {} |
| 161 | + |
| 162 | +impl SupportedPoolFwd<i8> for f64 {} |
| 163 | +impl SupportedPoolFwd<u8> for f64 {} |
| 164 | +impl SupportedPoolFwd<i32> for f64 {} |
| 165 | +impl SupportedPoolFwd<i64> for f64 {} |
| 166 | +impl SupportedPoolFwd<f32> for f64 {} |
| 167 | +impl SupportedPoolFwd<f64> for f64 {} |
| 168 | + |
| 169 | +/// Supported type configurations for the pooling backward operation. |
| 170 | +pub trait SupportedPoolBwd<T>: DataType + private::Sealed |
| 171 | +where |
| 172 | + T: DataType, |
| 173 | +{ |
| 174 | +} |
| 175 | + |
| 176 | +impl SupportedPoolBwd<f32> for f32 {} |
| 177 | +impl SupportedPoolBwd<f64> for f64 {} |
0 commit comments