MaxPooling.cpp 4.43 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
// 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/CPU/MaxPooling.cpp"
#else
#include "MaxPooling.h"

extern "C" void scn_DR_(MaxPooling_updateOutput)(
    THLongTensor *inputSize, THLongTensor *outputSize, THLongTensor *poolSize,
    THLongTensor *poolStride, void **m, THTensor *input_features,
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
15
    THTensor *output_features, long nFeaturesToDrop) {
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

  SCN_INITIALIZE_AND_REFERENCE(Metadata<Dimension>, m)
  uInt nPlanes = input_features->size[1] - nFeaturesToDrop;
  auto _rules =
      _m.getRuleBook(inputSize, outputSize, poolSize, poolStride, true);
  uInt nActive = _m.getNActive(outputSize);
  THTensor_(resize2d)(output_features, nActive,
                      input_features->size[1] - nFeaturesToDrop);
  THTensor_(zero)(output_features);

  auto iF = THTensor_(data)(input_features) + nFeaturesToDrop;
  auto oF = THTensor_(data)(output_features);

  for (auto &r : _rules) {
    uInt nHot = r.size() / 2;
    MaxPooling_ForwardPass<real>(iF, oF, nPlanes, input_features->stride[0],
                                 output_features->stride[0], &r[0], nHot);
  }
}
extern "C" void scn_DR_(MaxPooling_updateGradInput)(
    THLongTensor *inputSize, THLongTensor *outputSize, THLongTensor *poolSize,
    THLongTensor *poolStride, void **m, THTensor *input_features,
    THTensor *d_input_features, THTensor *output_features,
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
39
    THTensor *d_output_features, long nFeaturesToDrop) {
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

  SCN_INITIALIZE_AND_REFERENCE(Metadata<Dimension>, m)
  uInt nPlanes = input_features->size[1] - nFeaturesToDrop;
  auto _rules =
      _m.getRuleBook(inputSize, outputSize, poolSize, poolStride, true);
  uInt nActive = _m.getNActive(outputSize);
  THTensor_(resizeAs)(d_input_features, input_features);
  THTensor_(zero)(d_input_features);

  auto iF = THTensor_(data)(input_features);
  auto oF = THTensor_(data)(output_features);
  auto diF = THTensor_(data)(d_input_features);
  auto doF = THTensor_(data)(d_output_features);

  for (auto &r : _rules) {
    uInt nHot = r.size() / 2;
    MaxPooling_BackwardPass<real>(iF, diF, oF, doF, nPlanes,
                                  input_features->stride[0],
                                  output_features->stride[0], &r[0], nHot);
  }
}
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
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
extern "C" void scn_DR_(RandomizedStrideMaxPooling_updateOutput)(
    THLongTensor *inputSize, THLongTensor *outputSize, THLongTensor *poolSize,
    THLongTensor *poolStride, void **m, THTensor *input_features,
    THTensor *output_features, long nFeaturesToDrop) {

  SCN_INITIALIZE_AND_REFERENCE(Metadata<Dimension>, m)
  uInt nPlanes = input_features->size[1] - nFeaturesToDrop;
  auto _rules =
      _m.getRandomizedStrideRuleBook(inputSize, outputSize, poolSize, poolStride, true);
  uInt nActive = _m.getNActive(outputSize);
  THTensor_(resize2d)(output_features, nActive,
                      input_features->size[1] - nFeaturesToDrop);
  THTensor_(zero)(output_features);

  auto iF = THTensor_(data)(input_features) + nFeaturesToDrop;
  auto oF = THTensor_(data)(output_features);

  for (auto &r : _rules) {
    uInt nHot = r.size() / 2;
    MaxPooling_ForwardPass<real>(iF, oF, nPlanes, input_features->stride[0],
                                 output_features->stride[0], &r[0], nHot);
  }
}
extern "C" void scn_DR_(RandomizedStrideMaxPooling_updateGradInput)(
    THLongTensor *inputSize, THLongTensor *outputSize, THLongTensor *poolSize,
    THLongTensor *poolStride, void **m, THTensor *input_features,
    THTensor *d_input_features, THTensor *output_features,
    THTensor *d_output_features, long nFeaturesToDrop) {

  SCN_INITIALIZE_AND_REFERENCE(Metadata<Dimension>, m)
  uInt nPlanes = input_features->size[1] - nFeaturesToDrop;
  auto _rules =
      _m.getRandomizedStrideRuleBook(inputSize, outputSize, poolSize, poolStride, true);
  uInt nActive = _m.getNActive(outputSize);
  THTensor_(resizeAs)(d_input_features, input_features);
  THTensor_(zero)(d_input_features);

  auto iF = THTensor_(data)(input_features);
  auto oF = THTensor_(data)(output_features);
  auto diF = THTensor_(data)(d_input_features);
  auto doF = THTensor_(data)(d_output_features);

  for (auto &r : _rules) {
    uInt nHot = r.size() / 2;
    MaxPooling_BackwardPass<real>(iF, diF, oF, doF, nPlanes,
                                  input_features->stride[0],
                                  output_features->stride[0], &r[0], nHot);
  }
}
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
110
#endif