vol2col.cc 7.9 KB
Newer Older
C
chengduoZH 已提交
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
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/operators/math/vol2col.h"

namespace paddle {
namespace operators {
namespace math {

/*
 * vol = [input_channels, input_depth, input_height, input_width]
 * col =
 *   [input_channels, filter_depth, filter_height, filter_width,
 *                    output_depth, output_height, output_width]
 */
template <class T>
class Vol2ColFunctor<platform::CPUPlace, T> {
 public:
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& vol, framework::Tensor& col,
C
chengduoZH 已提交
32
                  int dilation_d, int dilation_h, int dilation_w,
C
chengduoZH 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
                  int stride_depth, int stride_height, int stride_width,
                  int padding_depth, int padding_height,
                  int padding_width) const {
    PADDLE_ENFORCE(vol.dims().size() == 4);
    PADDLE_ENFORCE(col.dims().size() == 7);

    int input_channels = vol.dims()[0];
    int input_depth = vol.dims()[1];
    int input_height = vol.dims()[2];
    int input_width = vol.dims()[3];
    int filter_depth = col.dims()[1];
    int filter_height = col.dims()[2];
    int filter_width = col.dims()[3];
    int output_depth = col.dims()[4];
    int output_height = col.dims()[5];
    int output_width = col.dims()[6];
    int channels_col =
        input_channels * filter_depth * filter_height * filter_width;

C
chengduoZH 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    PADDLE_ENFORCE_EQ((input_depth + 2 * padding_depth -
                       ((dilation_d * (filter_depth - 1) + 1))) /
                              stride_depth +
                          1,
                      output_depth,
                      "input_depth and output_depth are "
                      "Mismatching.");
    PADDLE_ENFORCE_EQ((input_height + 2 * padding_height -
                       ((dilation_h * (filter_height - 1) + 1))) /
                              stride_height +
                          1,
                      output_height,
                      "input_height and output_height are "
                      "Mismatching.");
    PADDLE_ENFORCE_EQ((input_width + 2 * padding_width -
                       ((dilation_w * (filter_width - 1) + 1))) /
                              stride_width +
                          1,
                      output_width,
                      "input_width and output_width are "
                      "Mismatching.");

C
chengduoZH 已提交
74 75 76 77 78 79 80 81 82
    const T* vol_data = vol.data<T>();
    T* col_data = col.data<T>();

    for (int c = 0; c < channels_col; ++c) {
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int d_offset = (c / filter_width / filter_height) % filter_depth;
      int c_in = c / filter_width / filter_height / filter_depth;
      for (int d = 0; d < output_depth; ++d) {
C
chengduoZH 已提交
83
        int d_pad = d * stride_depth - padding_depth + d_offset * dilation_d;
C
chengduoZH 已提交
84
        for (int h = 0; h < output_height; ++h) {
C
chengduoZH 已提交
85 86
          int h_pad =
              h * stride_height - padding_height + h_offset * dilation_h;
C
chengduoZH 已提交
87
          for (int w = 0; w < output_width; ++w) {
C
chengduoZH 已提交
88 89
            int w_pad =
                w * stride_width - padding_width + w_offset * dilation_w;
C
chengduoZH 已提交
90 91 92

            int col_idx =
                ((c * output_depth + d) * output_height + h) * output_width + w;
C
chengduoZH 已提交
93 94 95 96 97 98 99 100 101
            int vol_idx =
                ((c_in * input_depth + d_pad) * input_height + h_pad) *
                    input_width +
                w_pad;
            col_data[col_idx] =
                (h_pad < 0 || h_pad >= input_height || w_pad < 0 ||
                 w_pad >= input_width || d_pad < 0 || d_pad >= input_depth)
                    ? static_cast<T>(0)
                    : vol_data[vol_idx];
C
chengduoZH 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
          }
        }
      }
    }
  }
};

/*
 * vol = [input_channels,input_depth, input_height, input_width]
 * col =
 *   [input_channels, filter_depth, filter_height, filter_width,
 *                    output_depth, output_height, output_width]
 */
template <class T>
class Col2VolFunctor<platform::CPUPlace, T> {
 public:
  void operator()(const platform::DeviceContext& context,
                  framework::Tensor& vol, const framework::Tensor& col,
C
chengduoZH 已提交
120
                  int dilation_d, int dilation_h, int dilation_w,
C
chengduoZH 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
                  int stride_depth, int stride_height, int stride_width,
                  int padding_depth, int padding_height,
                  int padding_width) const {
    PADDLE_ENFORCE(vol.dims().size() == 4);
    PADDLE_ENFORCE(col.dims().size() == 7);

    int input_channels = vol.dims()[0];
    int input_depth = vol.dims()[1];
    int input_height = vol.dims()[2];
    int input_width = vol.dims()[3];
    int filter_depth = col.dims()[1];
    int filter_height = col.dims()[2];
    int filter_width = col.dims()[3];
    int output_depth = col.dims()[4];
    int output_height = col.dims()[5];
    int output_width = col.dims()[6];
    int channels_col =
        input_channels * filter_depth * filter_height * filter_width;

C
chengduoZH 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    PADDLE_ENFORCE_EQ((input_depth + 2 * padding_depth -
                       ((dilation_d * (filter_depth - 1) + 1))) /
                              stride_depth +
                          1,
                      output_depth,
                      "input_depth and output_depth are "
                      "Mismatching.");
    PADDLE_ENFORCE_EQ((input_height + 2 * padding_height -
                       ((dilation_h * (filter_height - 1) + 1))) /
                              stride_height +
                          1,
                      output_height,
                      "input_height and output_height are "
                      "Mismatching.");
    PADDLE_ENFORCE_EQ((input_width + 2 * padding_width -
                       ((dilation_w * (filter_width - 1) + 1))) /
                              stride_width +
                          1,
                      output_width,
                      "input_width and output_width are "
                      "Mismatching.");
C
chengduoZH 已提交
161 162 163 164 165 166 167 168 169
    T* vol_data = vol.data<T>();
    const T* col_data = col.data<T>();

    for (int c = 0; c < channels_col; ++c) {
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int d_offset = (c / filter_width / filter_height) % filter_depth;
      int cIm = c / filter_width / filter_height / filter_depth;
      for (int d = 0; d < output_depth; ++d) {
C
chengduoZH 已提交
170
        int d_pad = d * stride_depth - padding_depth + d_offset * dilation_d;
C
chengduoZH 已提交
171
        for (int h = 0; h < output_height; ++h) {
C
chengduoZH 已提交
172 173
          int h_pad =
              h * stride_height - padding_height + h_offset * dilation_h;
C
chengduoZH 已提交
174
          for (int w = 0; w < output_width; ++w) {
C
chengduoZH 已提交
175 176
            int w_pad =
                w * stride_width - padding_width + w_offset * dilation_w;
C
chengduoZH 已提交
177 178 179 180 181 182 183

            if (h_pad >= 0 && h_pad < input_height && w_pad >= 0 &&
                w_pad < input_width && d_pad >= 0 && d_pad < input_depth) {
              int vol_idx =
                  ((cIm * input_depth + d_pad) * input_height + h_pad) *
                      input_width +
                  w_pad;
C
chengduoZH 已提交
184

C
chengduoZH 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
              int col_idx =
                  ((c * output_depth + d) * output_height + h) * output_width +
                  w;
              vol_data[vol_idx] += col_data[col_idx];
            }
          }
        }
      }
    }
  }
};

template class Vol2ColFunctor<platform::CPUPlace, float>;
template class Vol2ColFunctor<platform::CPUPlace, double>;
template class Col2VolFunctor<platform::CPUPlace, float>;
template class Col2VolFunctor<platform::CPUPlace, double>;

}  // namespace math
}  // namespace operators
}  // namespace paddle