vol2col.cc 11.3 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
C
chengduoZH 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

15
#include "paddle/phi/kernels/funcs/vol2col.h"
W
wanghuancoder 已提交
16

H
hong 已提交
17 18
#include "paddle/phi/backends/cpu/cpu_context.h"

19 20
namespace phi {
namespace funcs {
C
chengduoZH 已提交
21 22 23 24 25 26 27

/*
 * vol = [input_channels, input_depth, input_height, input_width]
 * col =
 *   [input_channels, filter_depth, filter_height, filter_width,
 *                    output_depth, output_height, output_width]
 */
H
hong 已提交
28 29 30
template <class T>
class Vol2ColFunctor<phi::CPUContext, T> {
 public:
31
  void operator()(const phi::CPUContext& context,
32
                  const phi::DenseTensor& vol,
H
hong 已提交
33 34
                  const std::vector<int>& dilations,
                  const std::vector<int>& strides,
35
                  const std::vector<int>& paddings,
36
                  phi::DenseTensor* col,
H
hong 已提交
37
                  const DataLayout data_layout) const {
38 39
    PADDLE_ENFORCE_EQ(vol.dims().size(),
                      4,
40
                      phi::errors::InvalidArgument(
H
hong 已提交
41 42 43
                          "The dimension of vol should be 4, but received %d.",
                          vol.dims().size()));

44 45
    PADDLE_ENFORCE_EQ(col->dims().size(),
                      7,
46
                      phi::errors::InvalidArgument(
H
hong 已提交
47 48 49 50 51
                          "The dimension of col should be 7, but received %d.",
                          col->dims().size()));

    int input_channels =
        (data_layout != DataLayout::kNHWC ? vol.dims()[0] : vol.dims()[3]);
Y
yeliang2258 已提交
52
    int64_t input_depth =
H
hong 已提交
53
        (data_layout != DataLayout::kNHWC ? vol.dims()[1] : vol.dims()[0]);
Y
yeliang2258 已提交
54
    int64_t input_height =
H
hong 已提交
55
        (data_layout != DataLayout::kNHWC ? vol.dims()[2] : vol.dims()[1]);
Y
yeliang2258 已提交
56
    int64_t input_width =
H
hong 已提交
57 58 59 60
        (data_layout != DataLayout::kNHWC ? vol.dims()[3] : vol.dims()[2]);
    int filter_depth = col->dims()[1];
    int filter_height = col->dims()[2];
    int filter_width = col->dims()[3];
Y
yeliang2258 已提交
61 62 63
    int64_t output_depth = col->dims()[4];
    int64_t output_height = col->dims()[5];
    int64_t output_width = col->dims()[6];
H
hong 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    int channels_col =
        input_channels * filter_depth * filter_height * filter_width;

    // changed
    bool paddings_size_is_6 = (paddings.size() == 6);
    int pad_d_forth = paddings_size_is_6 ? paddings[0] : paddings[0];
    int pad_d_back = paddings_size_is_6 ? paddings[1] : paddings[0];
    int pad_h_up = paddings_size_is_6 ? paddings[2] : paddings[1];
    int pad_h_down = paddings_size_is_6 ? paddings[3] : paddings[1];
    int pad_w_left = paddings_size_is_6 ? paddings[4] : paddings[2];
    int pad_w_right = paddings_size_is_6 ? paddings[5] : paddings[2];

    auto input_depth_tmp = (input_depth + pad_d_forth + pad_d_back -
                            ((dilations[0] * (filter_depth - 1) + 1))) /
                               strides[0] +
                           1;
    PADDLE_ENFORCE_EQ(
81 82
        input_depth_tmp,
        output_depth,
83
        phi::errors::InvalidArgument(
H
hong 已提交
84
            "input_depth(%d) and output_depth(%d) are mismatching.",
85 86
            input_depth_tmp,
            output_depth));
H
hong 已提交
87 88 89 90 91
    auto input_height_tmp = (input_height + pad_h_up + pad_h_down -
                             ((dilations[1] * (filter_height - 1) + 1))) /
                                strides[1] +
                            1;
    PADDLE_ENFORCE_EQ(
92 93
        input_height_tmp,
        output_height,
94
        phi::errors::InvalidArgument(
H
hong 已提交
95
            "input_height(%d) and output_height(%d) are mismatching.",
96 97
            input_height_tmp,
            output_height));
H
hong 已提交
98 99 100 101 102
    auto input_width_tmp = (input_width + pad_w_left + pad_w_right -
                            ((dilations[2] * (filter_width - 1) + 1))) /
                               strides[2] +
                           1;
    PADDLE_ENFORCE_EQ(
103 104
        input_width_tmp,
        output_width,
105
        phi::errors::InvalidArgument(
H
hong 已提交
106
            "input_width(%d) and output_width(%d) are mismatching.",
107 108
            input_width_tmp,
            output_width));
H
hong 已提交
109 110
    const T* vol_data = vol.data<T>();
    T* col_data = col->data<T>();
Y
yeliang2258 已提交
111
    for (auto c = 0; c < channels_col; ++c) {
H
hong 已提交
112 113 114
      int w_offset = c % filter_width;
      int h_offset = (c / filter_width) % filter_height;
      int d_offset = (c / filter_width / filter_height) % filter_depth;
Y
yeliang2258 已提交
115 116 117 118 119 120 121 122
      int64_t c_in = c / filter_width / filter_height / filter_depth;
      for (auto d = 0; d < output_depth; ++d) {
        int64_t d_pad = d * strides[0] - pad_d_forth + d_offset * dilations[0];
        for (auto h = 0; h < output_height; ++h) {
          int64_t h_pad = h * strides[1] - pad_h_up + h_offset * dilations[1];
          for (auto w = 0; w < output_width; ++w) {
            int64_t w_pad =
                w * strides[2] - pad_w_left + w_offset * dilations[2];
H
hong 已提交
123

Y
yeliang2258 已提交
124
            int64_t col_idx =
H
hong 已提交
125
                ((c * output_depth + d) * output_height + h) * output_width + w;
Y
yeliang2258 已提交
126
            int64_t vol_idx;
H
hong 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
            if (data_layout != DataLayout::kNHWC) {
              vol_idx = ((c_in * input_depth + d_pad) * input_height + h_pad) *
                            input_width +
                        w_pad;
            } else {
              vol_idx = ((d_pad * input_height + h_pad) * input_width + w_pad) *
                            input_channels +
                        c_in;
            }
            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 已提交
148 149 150 151 152 153
/*
 * vol = [input_channels,input_depth, input_height, input_width]
 * col =
 *   [input_channels, filter_depth, filter_height, filter_width,
 *                    output_depth, output_height, output_width]
 */
H
hong 已提交
154 155 156
template <class T>
class Col2VolFunctor<phi::CPUContext, T> {
 public:
157
  void operator()(const phi::CPUContext& context,
158
                  const phi::DenseTensor& col,
H
hong 已提交
159 160
                  const std::vector<int>& dilations,
                  const std::vector<int>& strides,
161
                  const std::vector<int>& paddings,
162
                  phi::DenseTensor* vol,
H
hong 已提交
163
                  const DataLayout data_layout) const {
164 165
    PADDLE_ENFORCE_EQ(vol->dims().size(),
                      4,
166
                      phi::errors::InvalidArgument(
H
hong 已提交
167 168 169
                          "The dimension of vol should be 4, but received %d.",
                          vol->dims().size()));

170 171
    PADDLE_ENFORCE_EQ(col.dims().size(),
                      7,
172
                      phi::errors::InvalidArgument(
H
hong 已提交
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
                          "The dimension of col  should be 7, but received %d.",
                          col.dims().size()));

    int input_channels =
        (data_layout != DataLayout::kNHWC ? vol->dims()[0] : vol->dims()[3]);
    int input_depth =
        (data_layout != DataLayout::kNHWC ? vol->dims()[1] : vol->dims()[0]);
    int input_height =
        (data_layout != DataLayout::kNHWC ? vol->dims()[2] : vol->dims()[1]);
    int input_width =
        (data_layout != DataLayout::kNHWC ? vol->dims()[3] : vol->dims()[2]);
    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;

    bool paddings_size_is_6 = (paddings.size() == 6);
    int pad_d_forth = paddings_size_is_6 ? paddings[0] : paddings[0];
    int pad_d_back = paddings_size_is_6 ? paddings[1] : paddings[0];
    int pad_h_up = paddings_size_is_6 ? paddings[2] : paddings[1];
    int pad_h_down = paddings_size_is_6 ? paddings[3] : paddings[1];
    int pad_w_left = paddings_size_is_6 ? paddings[4] : paddings[2];
    int pad_w_right = paddings_size_is_6 ? paddings[5] : paddings[2];

    auto input_depth_tmp = (input_depth + pad_d_forth + pad_d_back -
                            ((dilations[0] * (filter_depth - 1) + 1))) /
                               strides[0] +
                           1;
    PADDLE_ENFORCE_EQ(
206 207
        input_depth_tmp,
        output_depth,
208
        phi::errors::InvalidArgument(
H
hong 已提交
209
            "input_depth(%d) and output_depth(%d) are mismatching.",
210 211
            input_depth_tmp,
            output_depth));
H
hong 已提交
212 213 214 215 216
    auto input_height_tmp = (input_height + pad_h_up + pad_h_down -
                             ((dilations[1] * (filter_height - 1) + 1))) /
                                strides[1] +
                            1;
    PADDLE_ENFORCE_EQ(
217 218
        input_height_tmp,
        output_height,
219
        phi::errors::InvalidArgument(
H
hong 已提交
220
            "input_height(%d) and output_height(%d) are mismatching.",
221 222
            input_height_tmp,
            output_height));
H
hong 已提交
223 224 225 226 227
    auto input_width_tmp = (input_width + pad_w_left + pad_w_right -
                            ((dilations[2] * (filter_width - 1) + 1))) /
                               strides[2] +
                           1;
    PADDLE_ENFORCE_EQ(
228 229
        input_width_tmp,
        output_width,
230
        phi::errors::InvalidArgument(
H
hong 已提交
231
            "input_width(%d)  and output_width(%d) are mismatching.",
232 233
            input_width_tmp,
            output_width));
H
hong 已提交
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    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) {
        int d_pad = d * strides[0] - pad_d_forth + d_offset * dilations[0];
        for (int h = 0; h < output_height; ++h) {
          int h_pad = h * strides[1] - pad_h_up + h_offset * dilations[1];
          for (int w = 0; w < output_width; ++w) {
            int w_pad = w * strides[2] - pad_w_left + w_offset * dilations[2];

            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;
              if (data_layout != DataLayout::kNHWC) {
                vol_idx = ((cIm * input_depth + d_pad) * input_height + h_pad) *
                              input_width +
                          w_pad;
              } else {
                vol_idx =
                    ((d_pad * input_height + h_pad) * input_width + w_pad) *
                        input_channels +
                    cIm;
              }
              int col_idx =
                  ((c * output_depth + d) * output_height + h) * output_width +
                  w;
              vol_data[vol_idx] += col_data[col_idx];
            }
          }
        }
      }
    }
  }
};

template class Vol2ColFunctor<phi::CPUContext, float>;
template class Vol2ColFunctor<phi::CPUContext, double>;

template class Col2VolFunctor<phi::CPUContext, float>;
template class Col2VolFunctor<phi::CPUContext, double>;
C
chengduoZH 已提交
279

280 281
}  // namespace funcs
}  // namespace phi