Convolution.cpp 11.6 KB
Newer Older
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
1
2
3
// Copyright 2016-present, Facebook, Inc.
// All rights reserved.
//
Benjamin Graham's avatar
Benjamin Graham committed
4
// This source code is licensed under the BSD-style license found in the
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
5
6
7
// LICENSE file in the root directory of this source tree.

template <typename T>
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
8
void Convolution_fp_bias(T *oF, T *b, Int nPlanes, Int nActive);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
9
template <typename T>
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
10
void Convolution_bp_bias(T *d_oF, T *d_b, Int nPlanes, Int nActive);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
template <typename T>
double dConvolution_forward2(T *inFeatures, T *outFeatures, T *w,
                             RuleBook _rules, Int input_nPlanes,
                             Int input_stride, Int output_nPlanes,
                             Int output_stride);

template <typename T>
void dConvolution_backward_dW2(T *inFeatures, T *dInFeatures, T *dOutFeatures,
                               T *w, T *dw, RuleBook _rules, Int input_nPlanes,
                               Int input_stride, Int output_nPlanes,
                               Int output_stride);

template <typename T, Int Dimension>
double cuda_Convolution_updateOutput(
    /*long*/ at::Tensor inputSize, /*long*/ at::Tensor outputSize,
    /*long*/ at::Tensor filterSize,
    /*long*/ at::Tensor filterStride, Metadata<Dimension> &m,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor output_features, /*cuda float*/ at::Tensor weight,
    /*cuda float*/ at::Tensor bias) {

  auto _rules =
      m.getRuleBook(inputSize, outputSize, filterSize, filterStride, true);
  Int nActiveOut = m.getNActive(outputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
35
36
37
  Int ip = weight.size(1);
  Int op = weight.size(2);
  output_features.resize_({nActiveOut, op});
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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

  if (nActiveOut) {
    auto iF = input_features.data<T>();
    auto oF = output_features.data<T>();
    auto w = weight.data<T>();

    if (bias.numel())
      Convolution_fp_bias(oF, bias.data<T>(), op, nActiveOut);
    else
      output_features.zero_();

    return dConvolution_forward2<T>(iF, oF, w, _rules, ip, ip, op, op);
  } else {
    return 0;
  }
}

template <typename T, Int Dimension>
void cuda_Convolution_backward(
    /*long*/ at::Tensor inputSize, /*long*/ at::Tensor outputSize,
    /*long*/ at::Tensor filterSize,
    /*long*/ at::Tensor filterStride, Metadata<Dimension> &m,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor d_input_features,
    /*cuda float*/ at::Tensor d_output_features,
    /*cuda float*/ at::Tensor weight, /*cuda float*/ at::Tensor d_weight,
    /*cuda float*/ at::Tensor d_bias) {

  auto _rules =
      m.getRuleBook(inputSize, outputSize, filterSize, filterStride, true);
  Int nActiveIn = m.getNActive(inputSize);
  Int nActiveOut = m.getNActive(outputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
70
71
72
73
  Int ip = weight.size(1);
  Int op = weight.size(2);
  d_input_features.resize_({nActiveIn, ip});
  d_input_features.zero_();
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
74
75
76
77
78
79
80
81
82
83
84
85

  if (nActiveOut) {
    auto iF = input_features.data<T>();
    auto diF = d_input_features.data<T>();
    auto doF = d_output_features.data<T>();
    auto w = weight.data<T>();
    auto dw = d_weight.data<T>();

    dConvolution_backward_dW2<T>(iF, diF, doF, w, dw, _rules, ip, ip, op, op);

    if (d_bias.numel()) {
      auto db = d_bias.data<T>();
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
86
      Convolution_bp_bias(doF, db, op, nActiveOut);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    }
  }
}

template <typename T, Int Dimension>
double cuda_SubmanifoldConvolution_updateOutput(
    /*long*/ at::Tensor inputSize, /*long*/ at::Tensor filterSize,
    Metadata<Dimension> &m,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor output_features, /*cuda float*/ at::Tensor weight,
    /*cuda float*/ at::Tensor bias) {

  auto _rules = m.getSubmanifoldRuleBook(inputSize, filterSize, true);
  Int nActive = m.getNActive(inputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
101
102
103
  Int ip = weight.size(1);
  Int op = weight.size(2);
  output_features.resize_({nActive, op});
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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

  if (nActive) {
    auto iF = input_features.data<T>();
    auto oF = output_features.data<T>();
    auto w = weight.data<T>();

    if (bias.numel())
      Convolution_fp_bias(oF, bias.data<T>(), op, nActive);
    else
      output_features.zero_();

    return dConvolution_forward2<T>(iF, oF, w, _rules, ip, ip, op, op);
  } else {
    return 0;
  }
}

template <typename T, Int Dimension>
void cuda_SubmanifoldConvolution_backward(
    /*long*/ at::Tensor inputSize, /*long*/ at::Tensor filterSize,
    Metadata<Dimension> &m,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor d_input_features,
    /*cuda float*/ at::Tensor d_output_features,
    /*cuda float*/ at::Tensor weight, /*cuda float*/ at::Tensor d_weight,
    /*cuda float*/ at::Tensor d_bias) {

  auto _rules = m.getSubmanifoldRuleBook(inputSize, filterSize, true);
  Int nActive = m.getNActive(inputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
133
134
135
136
  Int ip = weight.size(1);
  Int op = weight.size(2);
  d_input_features.resize_({nActive, ip});
  d_input_features.zero_();
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
137
138
139
140
141

  if (nActive) {
    auto iF = input_features.data<T>();
    auto diF = d_input_features.data<T>();
    auto doF = d_output_features.data<T>();
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    auto w = weight.data<T>();
    auto dw = d_weight.data<T>();

    dConvolution_backward_dW2<T>(iF, diF, doF, w, dw, _rules, ip, ip, op, op);

    if (d_bias.numel()) {
      auto db = d_bias.data<T>();
      Convolution_bp_bias(doF, db, op, nActive);
    }
  }
}

template <typename T, Int Dimension>
double cuda_PermutohedralSubmanifoldConvolution_updateOutput(
    /*long*/ at::Tensor inputSize, Metadata<Dimension> &m,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor output_features, /*cuda float*/ at::Tensor weight,
    /*cuda float*/ at::Tensor bias) {

  auto _rules = m.getPermutohedralSubmanifoldRuleBook(inputSize, true);
  Int nActive = m.getNActive(inputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
163
164
165
  Int ip = weight.size(1);
  Int op = weight.size(2);
  output_features.resize_({nActive, op});
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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

  if (nActive) {
    auto iF = input_features.data<T>();
    auto oF = output_features.data<T>();
    auto w = weight.data<T>();

    if (bias.numel())
      Convolution_fp_bias(oF, bias.data<T>(), op, nActive);
    else
      output_features.zero_();

    return dConvolution_forward2<T>(iF, oF, w, _rules, ip, ip, op, op);
  } else {
    return 0;
  }
}

template <typename T, Int Dimension>
void cuda_PermutohedralSubmanifoldConvolution_backward(
    /*long*/ at::Tensor inputSize, Metadata<Dimension> &m,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor d_input_features,
    /*cuda float*/ at::Tensor d_output_features,
    /*cuda float*/ at::Tensor weight, /*cuda float*/ at::Tensor d_weight,
    /*cuda float*/ at::Tensor d_bias) {

  auto _rules = m.getPermutohedralSubmanifoldRuleBook(inputSize, true);
  Int nActive = m.getNActive(inputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
194
195
196
197
  Int ip = weight.size(1);
  Int op = weight.size(2);
  d_input_features.resize_({nActive, ip});
  d_input_features.zero_();
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
198
199
200
201
202

  if (nActive) {
    auto iF = input_features.data<T>();
    auto diF = d_input_features.data<T>();
    auto doF = d_output_features.data<T>();
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
203
204
205
206
207
208
209
    auto w = weight.data<T>();
    auto dw = d_weight.data<T>();

    dConvolution_backward_dW2<T>(iF, diF, doF, w, dw, _rules, ip, ip, op, op);

    if (d_bias.numel()) {
      auto db = d_bias.data<T>();
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
210
      Convolution_bp_bias(doF, db, op, nActive);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    }
  }
}

template <typename T, Int Dimension>
double cuda_FullConvolution_updateOutput(
    /*long*/ at::Tensor inputSize, /*long*/ at::Tensor outputSize,
    /*long*/ at::Tensor filterSize,
    /*long*/ at::Tensor filterStride, Metadata<Dimension> &mIn,
    Metadata<Dimension> &mOut,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor output_features, /*cuda float*/ at::Tensor weight,
    /*cuda float*/ at::Tensor bias) {

  auto _rules = mIn.getFullConvolutionRuleBook(inputSize, outputSize,
                                               filterSize, filterStride, mOut);
  Int nActiveOut = mOut.getNActive(outputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
228
229
230
231
  Int ip = weight.size(1);
  Int op = weight.size(2);
  output_features.resize_({nActiveOut, op});

Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  if (nActiveOut) {
    auto iF = input_features.data<T>();
    auto oF = output_features.data<T>();
    auto w = weight.data<T>();

    if (bias.numel())
      Convolution_fp_bias(oF, bias.data<T>(), op, nActiveOut);
    else
      output_features.zero_();

    return dConvolution_forward2<T>(iF, oF, w, _rules, ip, ip, op, op);
  } else {
    return 0;
  }
}

template <typename T, Int Dimension>
void cuda_FullConvolution_backward(
    /*long*/ at::Tensor inputSize, /*long*/ at::Tensor outputSize,
    /*long*/ at::Tensor filterSize,
    /*long*/ at::Tensor filterStride, Metadata<Dimension> &mIn,
    Metadata<Dimension> &mOut,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor d_input_features,
    /*cuda float*/ at::Tensor d_output_features,
    /*cuda float*/ at::Tensor weight, /*cuda float*/ at::Tensor d_weight,
    /*cuda float*/ at::Tensor d_bias) {

  auto _rules = mIn.getFullConvolutionRuleBook(inputSize, outputSize,
                                               filterSize, filterStride, mOut);
  Int nActiveIn = mIn.getNActive(inputSize);
  Int nActiveOut = mOut.getNActive(outputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
264
265
266
267
  Int ip = weight.size(1);
  Int op = weight.size(2);
  d_input_features.resize_({nActiveIn, ip});
  d_input_features.zero_();
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
268
269
270
271
272
273
274
275
276
277
278
279

  if (nActiveOut) {
    auto iF = input_features.data<T>();
    auto diF = d_input_features.data<T>();
    auto doF = d_output_features.data<T>();
    auto w = weight.data<T>();
    auto dw = d_weight.data<T>();

    dConvolution_backward_dW2<T>(iF, diF, doF, w, dw, _rules, ip, ip, op, op);

    if (d_bias.numel()) {
      auto db = d_bias.data<T>();
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
280
      Convolution_bp_bias(doF, db, op, nActiveOut);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    }
  }
}
template <typename T, Int Dimension>
double cuda_RandomizedStrideConvolution_updateOutput(
    /*long*/ at::Tensor inputSize, /*long*/ at::Tensor outputSize,
    /*long*/ at::Tensor filterSize,
    /*long*/ at::Tensor filterStride, Metadata<Dimension> &m,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor output_features,
    /*cuda float*/ at::Tensor weight, /*cuda float*/ at::Tensor bias) {

  auto _rules = m.getRandomizedStrideRuleBook(inputSize, outputSize, filterSize,
                                              filterStride, true);
  Int nActiveOut = m.getNActive(outputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
296
297
298
  Int ip = weight.size(1);
  Int op = weight.size(2);
  output_features.resize_({nActiveOut, op});
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

  if (nActiveOut) {
    auto iF = input_features.data<T>();
    auto oF = output_features.data<T>();
    auto w = weight.data<T>();

    if (bias.numel())
      Convolution_fp_bias(oF, bias.data<T>(), op, nActiveOut);
    else
      output_features.zero_();

    return dConvolution_forward2<T>(iF, oF, w, _rules, ip, ip, op, op);
  } else {
    return 0;
  }
}

template <typename T, Int Dimension>
void cuda_RandomizedStrideConvolution_backward(
    /*long*/ at::Tensor inputSize, /*long*/ at::Tensor outputSize,
    /*long*/ at::Tensor filterSize,
    /*long*/ at::Tensor filterStride, Metadata<Dimension> &m,
    /*cuda float*/ at::Tensor input_features,
    /*cuda float*/ at::Tensor d_input_features,
    /*cuda float*/ at::Tensor d_output_features,
    /*cuda float*/ at::Tensor weight, /*cuda float*/ at::Tensor d_weight,
    /*cuda float*/ at::Tensor d_bias) {

  auto _rules = m.getRandomizedStrideRuleBook(inputSize, outputSize, filterSize,
                                              filterStride, true);
  Int nActiveIn = m.getNActive(inputSize);
  Int nActiveOut = m.getNActive(outputSize);
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
331
332
333
334
  Int ip = weight.size(1);
  Int op = weight.size(2);
  d_input_features.resize_({nActiveIn, ip});
  d_input_features.zero_();
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
335
336
337
338
339
340
341
342
343
344
345
346

  if (nActiveOut) {
    auto iF = input_features.data<T>();
    auto diF = d_input_features.data<T>();
    auto doF = d_output_features.data<T>();
    auto w = weight.data<T>();
    auto dw = d_weight.data<T>();

    dConvolution_backward_dW2<T>(iF, diF, doF, w, dw, _rules, ip, ip, op, op);

    if (d_bias.numel()) {
      auto db = d_bias.data<T>();
Benjamin Thomas Graham's avatar
fixes  
Benjamin Thomas Graham committed
347
      Convolution_bp_bias(doF, db, op, nActiveOut);
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
348
349
350
    }
  }
}