Deconvolution.cu 3.15 KB
Newer Older
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Copyright 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the license found in the
// LICENSE file in the root directory of this source tree.

#ifndef TH_GENERIC_FILE_
#define TH_GENERIC_FILE_ "generic/GPU/Deconvolution.cu"
#else
#include "Convolution.h"
#include "Deconvolution.h"

#include <algorithm>

extern "C" double scn_DR_(Deconvolution_updateOutput)(
    THLongTensor *inputSize, THLongTensor *outputSize, THLongTensor *filterSize,
    THLongTensor *filterStride, void **m, THCTensor *input_features,
    THCTensor *output_features, THCTensor *weight, THCTensor *bias,
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
19
    long filterVolume) {
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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
  SCN_INITIALIZE_AND_REFERENCE(Metadata<Dimension>, m)
  auto _rules =
      _m.getRuleBook(outputSize, inputSize, filterSize, filterStride, true);
  uInt nActive = _m.getNActive(outputSize);
  THCTensor_(resize2d)(state, output_features, nActive, weight->size[1]);
  if (not bias)
    THCTensor_(zero)(state, output_features);

  auto iF = THCTensor_(data)(state, input_features);
  auto oF = THCTensor_(data)(state, output_features);
  auto ip = input_features->size[1];
  auto op = output_features->size[1];
  auto w = THCTensor_(data)(state, weight);
  double flops = 0;

  if (bias) {
    auto b = THCTensor_(data)(state, bias);
    for (uInt i = 0; i < op; i += 32) {
      uInt blockDim = min(32L, op - i);
      uInt gridDim = min(4096, nActive);
      Convolution_fp_bias
              << <gridDim, blockDim, 0, THCState_getCurrentStream(state)>>>
          (oF + i, b + i, op, op, nActive);
    }
  }
  uInt c = ip * op;
  RULEBOOKITERATOR(
      dDeconvolution_forward2<real>(iF, oF, w, rbB, nHotB, ip, ip, op, op,
                                    THCState_getCurrentStream(state));
      , w += c; flops += nHotB * c;)
  return flops;
}

extern "C" void scn_DR_(Deconvolution_backward)(
    THLongTensor *inputSize, THLongTensor *outputSize, THLongTensor *filterSize,
    THLongTensor *filterStride, void **m, THCTensor *input_features,
    THCTensor *d_input_features, THCTensor *d_output_features,
    THCTensor *weight, THCTensor *d_weight, THCTensor *d_bias,
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
58
    long filterVolume) {
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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
  SCN_INITIALIZE_AND_REFERENCE(Metadata<Dimension>, m)
  auto _rules =
      _m.getRuleBook(outputSize, inputSize, filterSize, filterStride, true);
  uInt nActive = _m.getNActive(outputSize);
  THCTensor_(resizeAs)(state, d_input_features, input_features);
  THCTensor_(zero)(state, d_input_features);

  auto iF = THCTensor_(data)(state, input_features);
  auto diF = THCTensor_(data)(state, d_input_features);
  auto doF = THCTensor_(data)(state, d_output_features);
  auto ip = input_features->size[1];
  auto op = d_output_features->size[1];
  auto w = THCTensor_(data)(state, weight);
  auto dw = THCTensor_(data)(state, d_weight);
  uInt c = ip * op;
  RULEBOOKITERATOR(dDeconvolution_backward_dW2<real>(
                       iF, diF, doF, w, dw, rbB, nHotB, ip, ip, op, op,
                       THCState_getCurrentStream(state));

                   , w += c; dw += c;)

  if (d_bias) {
    auto db = THCTensor_(data)(state, d_bias);
    Convolution_bp_bias(doF, db, op, op, nActive,
                        THCState_getCurrentStream(state));
  }
}

#endif