1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::{ConvolutionDescriptor, NormalizationDescriptor, FilterDescriptor, PoolingDescriptor};
use ffi::*;
use std::marker::PhantomData;
#[derive(Debug, Copy, Clone)]
pub enum DataType {
Float,
Double,
Half,
}
#[allow(missing_debug_implementations, missing_copy_implementations)]
pub struct ConvolutionConfig {
forward_algo: cudnnConvolutionFwdAlgo_t,
backward_filter_algo: cudnnConvolutionBwdFilterAlgo_t,
backward_data_algo: cudnnConvolutionBwdDataAlgo_t,
forward_workspace_size: usize,
backward_filter_workspace_size: usize,
backward_data_workspace_size: usize,
conv_desc: ConvolutionDescriptor,
filter_desc: FilterDescriptor,
}
impl ConvolutionConfig {
pub fn new(
algo_fwd: cudnnConvolutionFwdAlgo_t,
workspace_size_fwd: usize,
algo_filter_bwd: cudnnConvolutionBwdFilterAlgo_t,
workspace_filter_size_bwd: usize,
algo_data_bwd: cudnnConvolutionBwdDataAlgo_t,
workspace_data_size_bwd: usize,
conv_desc: ConvolutionDescriptor,
filter_desc: FilterDescriptor,
) -> ConvolutionConfig {
ConvolutionConfig {
forward_algo: algo_fwd,
forward_workspace_size: workspace_size_fwd,
backward_filter_algo: algo_filter_bwd,
backward_filter_workspace_size: workspace_filter_size_bwd,
backward_data_algo: algo_data_bwd,
backward_data_workspace_size: workspace_data_size_bwd,
conv_desc: conv_desc,
filter_desc: filter_desc,
}
}
pub fn largest_workspace_size(&self) -> &usize {
if self.backward_data_workspace_size() >= self.backward_filter_workspace_size() && self.backward_data_workspace_size() >= self.forward_workspace_size() {
self.backward_data_workspace_size()
} else if self.backward_filter_workspace_size() >= self.backward_data_workspace_size() && self.backward_filter_workspace_size() >= self.forward_workspace_size() {
self.backward_filter_workspace_size()
} else {
self.forward_workspace_size()
}
}
pub fn forward_algo(&self) -> &cudnnConvolutionFwdAlgo_t {
&self.forward_algo
}
pub fn forward_workspace_size(&self) -> &usize {
&self.forward_workspace_size
}
pub fn backward_filter_algo(&self) -> &cudnnConvolutionBwdFilterAlgo_t {
&self.backward_filter_algo
}
pub fn backward_filter_workspace_size(&self) -> &usize {
&self.backward_filter_workspace_size
}
pub fn backward_data_algo(&self) -> &cudnnConvolutionBwdDataAlgo_t {
&self.backward_data_algo
}
pub fn backward_data_workspace_size(&self) -> &usize {
&self.backward_data_workspace_size
}
pub fn conv_desc(&self) -> &ConvolutionDescriptor {
&self.conv_desc
}
pub fn filter_desc(&self) -> &FilterDescriptor {
&self.filter_desc
}
}
#[allow(missing_debug_implementations, missing_copy_implementations)]
pub struct NormalizationConfig {
lrn_desc: NormalizationDescriptor,
}
impl NormalizationConfig {
pub fn new(lrn_desc: NormalizationDescriptor) -> NormalizationConfig {
NormalizationConfig {
lrn_desc: lrn_desc,
}
}
pub fn lrn_desc(&self) -> &NormalizationDescriptor {
&self.lrn_desc
}
}
#[allow(missing_debug_implementations, missing_copy_implementations)]
pub struct PoolingConfig {
pooling_avg_desc: PoolingDescriptor,
pooling_max_desc: PoolingDescriptor,
}
impl PoolingConfig {
pub fn new(
pooling_avg_desc: PoolingDescriptor,
pooling_max_desc: PoolingDescriptor,
) -> PoolingConfig {
PoolingConfig {
pooling_avg_desc: pooling_avg_desc,
pooling_max_desc: pooling_max_desc,
}
}
pub fn pooling_avg_desc(&self) -> &PoolingDescriptor {
&self.pooling_avg_desc
}
pub fn pooling_max_desc(&self) -> &PoolingDescriptor {
&self.pooling_max_desc
}
}
#[allow(missing_debug_implementations, missing_copy_implementations)]
pub struct ScalParams<T> {
pub a: *const ::libc::c_void,
pub b: *const ::libc::c_void,
scal_type: PhantomData<T>,
}
impl Default for ScalParams<f32> {
fn default() -> ScalParams<f32> {
let alpha_ptr: *const ::libc::c_void = *&[1.0f32].as_ptr() as *const ::libc::c_void;
let beta_ptr: *const ::libc::c_void = *&[0.0f32].as_ptr() as *const ::libc::c_void;
ScalParams {
a: alpha_ptr,
b: beta_ptr,
scal_type: PhantomData,
}
}
}
impl Default for ScalParams<f64> {
fn default() -> ScalParams<f64> {
let alpha_ptr: *const ::libc::c_void = *&[1.0f64].as_ptr() as *const ::libc::c_void;
let beta_ptr: *const ::libc::c_void = *&[0.0f64].as_ptr() as *const ::libc::c_void;
ScalParams {
a: alpha_ptr,
b: beta_ptr,
scal_type: PhantomData,
}
}
}