conv_transpose_kernel_impl.h 11.0 KB
Newer Older
F
From00 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once

#include "paddle/phi/common/layout.h"
#include "paddle/phi/core/ddim.h"
19
#include "paddle/phi/kernels/conv_transpose_kernel.h"
F
From00 已提交
20 21 22
#include "paddle/phi/kernels/cpu/conv_util.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/concat_and_split_functor.h"
23
#include "paddle/phi/kernels/funcs/im2col.h"
F
From00 已提交
24
#include "paddle/phi/kernels/funcs/slice.h"
25
#include "paddle/phi/kernels/funcs/vol2col.h"
F
From00 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39

namespace phi {

template <typename T, typename Context>
void ConvTransposeRawKernel(const Context& ctx,
                            const DenseTensor& x,
                            const DenseTensor& filter,
                            const std::vector<int>& strides,
                            const std::vector<int>& paddings,
                            const std::string& padding_algorithm,
                            int groups,
                            const std::vector<int>& dilations,
                            const std::string& data_format,
                            DenseTensor* out) {
40
  const DataLayout data_layout = phi::StringToDataLayout(data_format);
F
From00 已提交
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 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  // The filter will be reshaped, so it should not be constant
  DenseTensor filter_ = filter;
  std::vector<int> paddings_ = paddings;
  std::vector<int> dilations_ = dilations;

  auto x_dims = x.dims();
  auto filter_dims = filter_.dims();
  auto out_dims = out->dims();
  const int batch_size = static_cast<int>(x.dims()[0]);

  DDim in_data_dims;
  if (data_layout != DataLayout::kNHWC) {
    in_data_dims = slice_ddim(x_dims, 2, x_dims.size());
  } else {
    in_data_dims = slice_ddim(x_dims, 1, x_dims.size() - 1);
  }
  DDim filter_data_dims = slice_ddim(filter_dims, 2, filter_dims.size());
  std::vector<int> ksize = vectorize<int>(filter_data_dims);
  UpdatePaddingAndDilation(
      &paddings_, &dilations_, padding_algorithm, in_data_dims, strides, ksize);

  // x_shape_vec: {n, c, h, w} or {n, c, d, h, w} for channel_first
  // x_shape_vec: {n, h, w, c} or {n, d, h, w, c} for channel_last
  std::vector<int64_t> x_shape_vec = vectorize(x.dims());
  // filter_shape_vec: {k_o, k_i, k_h, k_w} or {k_o, k_i, k_d, k_h, k_w}
  std::vector<int64_t> filter_shape_vec = vectorize(filter_.dims());

  // use col_shape in the im2col and col2im (or vol2col and col2vol)
  // calculation
  // col_shape_vec: {o_c/g, k_h, k_w, h, w} or {o_c/g, k_d, k_h, k_w, d, h, w}
  size_t data_dim = filter_shape_vec.size() - 2;
  std::vector<int64_t> col_shape_vec(1 + 2 * data_dim);
  if (data_layout != DataLayout::kNHWC) {
    col_shape_vec[0] = out_dims[1] / groups;
    for (size_t j = 0; j < data_dim; ++j) {
      col_shape_vec[j + 1] = filter_shape_vec[j + 2];
      col_shape_vec[j + 1 + data_dim] = x_shape_vec[j + 2];
    }
  } else {
    col_shape_vec[0] = out_dims[out_dims.size() - 1] / groups;
    for (size_t j = 0; j < data_dim; ++j) {
      col_shape_vec[j + 1] = filter_shape_vec[j + 2];
      col_shape_vec[j + 1 + data_dim] = x_shape_vec[j + 1];
    }
  }
  DDim col_shape(make_ddim(col_shape_vec));

  // use col_matrix_shape in the gemm calculation
  // size: (o_c/g * k_h * k_w, h * w) or (o_c/g * k_d * k_h * k_w, d * h * w)
  DDim col_matrix_shape = flatten_to_2d(col_shape, data_dim + 1);

  DenseTensor col;
  col.Resize(col_shape);
  ctx.template Alloc<T>(&col);
  // col_matrix shares the same piece of data with col,
  // but will be reshaped into a two-dimensional matrix shape
  // to call the matrix multiplication interface.
  DenseTensor col_matrix;
  col_matrix.ShareDataWith(col);
  col_matrix.Resize(col_matrix_shape);

  // out size: (o_c, o_h, o_w) or (o_c, o_d, o_h, o_w) for channel_first
  // out size: (o_h, o_w, o_c) or (o_d, o_h, o_w, o_c) for channel_last
  DDim out_shape = slice_ddim(out->dims(), 1, out->dims().size());

  // x matrix size: (i_c, h * w) or (i_c, d * h * w) for channel_first
  // x matrix size: (h * w, i_c) or (d * h * w, i_c) for channel_last
  DDim x_matrix_shape;
  if (data_layout != DataLayout::kNHWC) {
    x_matrix_shape = {x_dims[1], col_matrix_shape[1]};
  } else {
    x_matrix_shape = {col_matrix_shape[1], x_dims[x_dims.size() - 1]};
  }

  // filter size: (i_c, o_c/g * k_h * k_w) or (i_c, o_c/g * k_d * k_h * k_w)
  DDim filter_matrix_shape;
  if (data_layout != DataLayout::kNHWC) {
    filter_matrix_shape = {x_dims[1], col_matrix_shape[0]};
  } else {
    filter_matrix_shape = {x_dims[x_dims.size() - 1], col_matrix_shape[0]};
  }
  filter_.Resize(filter_matrix_shape);

  ctx.template Alloc<T>(out);

  funcs::SetConstant<Context, T> set_zero;

  auto blas = funcs::GetBlas<Context, T>(ctx);
  set_zero(ctx, out, static_cast<T>(0));

  int in_step = (data_layout != DataLayout::kNHWC
                     ? static_cast<int>(x_dims[1]) / groups
                     : static_cast<int>(x_dims[x_dims.size() - 1]) / groups);

  int out_step =
      (data_layout != DataLayout::kNHWC
           ? static_cast<int>(out_dims[1]) / groups
           : static_cast<int>(out_dims[out_dims.size() - 1]) / groups);
139
  phi::funcs::Col2ImFunctor<phi::funcs::ColFormat::kCFO, Context, T> col2im;
140
  phi::funcs::Col2VolFunctor<Context, T> col2vol;
F
From00 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  funcs::ConcatFunctor<Context, T> concat_functor;

  // convolution transpose: gemm + col2im or col2vol (similar to conv-backward
  // on x)
  size_t D = x.dims().size();
  for (int i = 0; i < batch_size; i++) {
    // batch with size (i_c, h * w) or (i_c, d * h * w) for channel_first
    // batch with size (h * w, i_c) or (d * h * w, i_c) for channel_last
    DenseTensor x_batch = x.Slice(i, i + 1).Resize(x_matrix_shape);

    // out size: (o_c, o_h, o_w) or (o_c, o_d, o_h, o_w) for channel_first
    // out size: (o_h, o_w, o_c) or (o_d, o_h, o_w, o_c) for channel_last
    DenseTensor out_batch = out->Slice(i, i + 1).Resize(out_shape);

    std::vector<DenseTensor> out_batch_vec;
    for (int g = 0; g < groups; g++) {
      int64_t start = g * in_step;
      int64_t end = (g + 1) * in_step;
      int axes = (data_layout != DataLayout::kNHWC ? 0 : 1);
      DenseTensor filter_slice = filter_.Slice(g * in_step, (g + 1) * in_step);
      DenseTensor in_slice, out_slice;

      // col_matrix = filter_slice * x_slice
      // of shape (o_c/g * k_h * k_w, h * w)
      // or (o_c/g * k_d * k_h * k_w, d * h * w)
      if (data_layout != DataLayout::kNHWC) {
        in_slice = x_batch.Slice(g * in_step, (g + 1) * in_step);
        out_slice = out_batch.Slice(g * out_step, (g + 1) * out_step);
        blas.MatMul(filter_slice,
                    true,
                    in_slice,
                    false,
                    static_cast<T>(1.0),
                    &col_matrix,
                    static_cast<T>(0.0));
      } else {
        funcs::Slice<Context, T, 2>(ctx, &x_batch, &in_slice, start, end, axes);
        start = g * out_step;
        end = (g + 1) * out_step;
        axes = D - 2;
        if (D == 4U) {
          funcs::Slice<Context, T, 3>(
              ctx, &out_batch, &out_slice, start, end, axes);
        } else if (D == 5U) {
          funcs::Slice<Context, T, 4>(
              ctx, &out_batch, &out_slice, start, end, axes);
        }
        blas.MatMul(filter_slice,
                    true,
                    in_slice,
                    true,
                    static_cast<T>(1.0),
                    &col_matrix,
                    static_cast<T>(0.0));
      }

      if (data_dim == 2U) {
        // col2im: col_matrix -> dy from (o_c/g * k_h * k_w, h * w) to (o_c/g,
        // o_h, o_w) or (o_h, o_w, o_c/g)
        col2im(ctx,
               col,
               dilations_,
               strides,
               std::vector<int>{
                   paddings_[0], paddings_[2], paddings_[1], paddings_[3]},
               &out_slice,
               data_layout);
      } else if (data_dim == 3U) {
        // col2vol: col_matrix -> dy from (o_c/g * k_d * k_h * k_w, d * h * w)
        // to (o_c/g, o_d, o_h, o_w) or (o_d, o_h, o_w, o_c/g)
        col2vol(
            ctx, col, dilations_, strides, paddings_, &out_slice, data_layout);
      }
      if (data_layout == DataLayout::kNHWC) {
        out_batch_vec.push_back(out_slice);
      }
    }
    if (data_layout == DataLayout::kNHWC) {
      concat_functor(ctx, out_batch_vec, static_cast<int>(D - 2), &out_batch);
    }
  }
}

template <typename T, typename Context>
void Conv2dTransposeKernel(const Context& ctx,
                           const DenseTensor& x,
                           const DenseTensor& filter,
                           const std::vector<int>& strides,
                           const std::vector<int>& paddings,
                           const std::vector<int>& output_padding,
231
                           const IntArray& output_size,
F
From00 已提交
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 264 265 266 267 268 269 270 271 272 273 274
                           const std::string& padding_algorithm,
                           int groups,
                           const std::vector<int>& dilations,
                           const std::string& data_format,
                           DenseTensor* out) {
  ConvTransposeRawKernel<T, Context>(ctx,
                                     x,
                                     filter,
                                     strides,
                                     paddings,
                                     padding_algorithm,
                                     groups,
                                     dilations,
                                     data_format,
                                     out);
}

template <typename T, typename Context>
void Conv3dTransposeKernel(const Context& ctx,
                           const DenseTensor& x,
                           const DenseTensor& filter,
                           const std::vector<int>& strides,
                           const std::vector<int>& paddings,
                           const std::vector<int>& output_padding,
                           const std::vector<int>& output_size,
                           const std::string& padding_algorithm,
                           int groups,
                           const std::vector<int>& dilations,
                           const std::string& data_format,
                           DenseTensor* out) {
  ConvTransposeRawKernel<T, Context>(ctx,
                                     x,
                                     filter,
                                     strides,
                                     paddings,
                                     padding_algorithm,
                                     groups,
                                     dilations,
                                     data_format,
                                     out);
}

}  // namespace phi