conv_transpose_compute.cc 6.1 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2019 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.

#include "lite/kernels/arm/conv_transpose_compute.h"
#include <vector>
17
#include "lite/backends/arm/math/funcs.h"
Y
Yan Chunwei 已提交
18 19 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
#include "lite/core/op_registry.h"
#include "lite/core/type_system.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace arm {

void Conv2DTransposeCompute::PrepareForRun() {
  auto& param = this->Param<param_t>();
  auto x_dims = param.x->dims();
  auto w_dims = param.filter->dims();
  auto o_dims = param.output->dims();
  int win = x_dims[3];  // nchw
  int hin = x_dims[2];
  int chin = x_dims[1];
  int num = x_dims[0];
  int wout = o_dims[3];
  int hout = o_dims[2];
  int chout = o_dims[1];
  int kw = w_dims[3];  // oihw
  int kh = w_dims[2];
  int group = param.groups;

  // deconv weights layout: chin * chout * kh * kw
  int m = chout * kw * kh / group;
  int n = hin * win;
  int k = chin / group;

T
TianXiaogang 已提交
47
  workspace_size_ = group * m * n * sizeof(float);
Y
Yan Chunwei 已提交
48

T
TianXiaogang 已提交
49
  auto& ctx = this->ctx_->template As<ARMContext>();
Y
Yan Chunwei 已提交
50 51
  lite::Tensor tmp_weights;
  lite::arm::math::prepackA(
X
Xiaoyang LI 已提交
52
      &tmp_weights, *(param.filter), 1.f, m, k, group, true, &ctx);
Y
Yan Chunwei 已提交
53 54 55
  param.filter->Resize(tmp_weights.dims());
  param.filter->CopyDataFrom(tmp_weights);
  param.filter->Resize(w_dims);
X
Xiaoyang LI 已提交
56
  is_first_epoch_ = false;
Y
Yan Chunwei 已提交
57 58 59
}

void Conv2DTransposeCompute::Run() {
T
TianXiaogang 已提交
60 61
  auto& ctx = this->ctx_->template As<ARMContext>();
  ctx.ExtendWorkspace(workspace_size_);
Y
Yan Chunwei 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  auto& param = this->Param<param_t>();
  auto x_dims = param.x->dims();
  auto o_dims = param.output->dims();
  auto w_dims = param.filter->dims();
  int num = x_dims[0];
  int chin = x_dims[1];
  int hin = x_dims[2];
  int win = x_dims[3];
  int chout = o_dims[1];
  int hout = o_dims[2];
  int wout = o_dims[3];
  int kw = w_dims[3];  // oihw
  int kh = w_dims[2];
  int group = param.groups;
  bool fuse_relu = param.fuse_relu;
  bool flag_bias = (param.bias != nullptr);

H
HappyAngel 已提交
79 80 81
  auto paddings = *param.paddings;
  auto dilations = *param.dilations;

Y
Yan Chunwei 已提交
82 83 84
  int m = chout * kw * kh / group;
  int n = hin * win;
  int k = chin / group;
H
HappyAngel 已提交
85 86 87 88

  bool pads_equal =
      (paddings[0] == paddings[1]) && (paddings[2] == paddings[3]);

Y
Yan Chunwei 已提交
89 90 91
  int group_size_in = win * hin * chin / group;
  int group_size_out = wout * hout * chout / group;
  int group_size_coldata = m * n;
H
HappyAngel 已提交
92 93

  bool pads_all_qual = pads_equal && (paddings[0] == paddings[2]);
94
  int hblock = lite::arm::math::get_hblock(&ctx);
Y
Yan Chunwei 已提交
95 96 97
  int m_roundup = hblock * ((m + hblock - 1) / hblock);
  int group_size_weights = ((m_roundup * k + 15) / 16) * 16;
  bool flag_1x1s1p1 = (kw == 1) && (kh == 1) && (param.strides[0] == 1) &&
H
HappyAngel 已提交
98
                      (param.strides[1] == 1) && pads_all_qual &&
99 100
                      (paddings[0] == 0) && (dilations[0] == 1) &&
                      (dilations[1] == 1);
Y
Yan Chunwei 已提交
101 102 103 104 105 106 107 108 109
  ctx.ExtendWorkspace(sizeof(float) * group * m * n);

  auto din = param.x->data<float>();
  auto dout = param.output->mutable_data<float>();
  auto weights = param.filter->data<float>();
  for (int i = 0; i < num; i++) {
    const float* din_batch = din + i * chin * hin * win;
    float* dout_batch = dout + i * chout * hout * wout;
    float* col_data = static_cast<float*>(ctx.workspace_data<float>()) +
X
Xiaoyang LI 已提交
110
                      ctx.llc_size() / sizeof(float);
Y
Yan Chunwei 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    if (flag_1x1s1p1) {
      col_data = dout_batch;
    }
    for (int g = 0; g < group; g++) {
      const float* din_group = din_batch + g * group_size_in;
      const float* weights_group = weights + g * group_size_weights;
      float* coldata_group = col_data + g * group_size_coldata;

      lite::arm::math::sgemm_prepack(false,
                                     m,
                                     n,
                                     k,
                                     weights_group,
                                     din_group,
                                     n,
X
Xiaoyang LI 已提交
126
                                     0.f,
Y
Yan Chunwei 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
                                     coldata_group,
                                     n,
                                     nullptr,
                                     false,
                                     fuse_relu && (!flag_bias),
                                     &ctx);
    }
    if (!flag_1x1s1p1) {
      lite::arm::math::col2im<float>(col_data,
                                     chout,
                                     hout,
                                     wout,
                                     kh,
                                     kw,
H
HappyAngel 已提交
141
                                     paddings[0],
142
                                     paddings[1],
H
HappyAngel 已提交
143
                                     paddings[2],
144
                                     paddings[3],
Y
Yan Chunwei 已提交
145 146
                                     param.strides[0],
                                     param.strides[1],
H
HappyAngel 已提交
147 148
                                     dilations[0],
                                     dilations[1],
Y
Yan Chunwei 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                                     dout_batch);
    }
    if (flag_bias) {
      lite::arm::math::fill_bias_relu<float>(
          dout_batch,
          static_cast<const float*>(param.bias->data<float>()),
          chout,
          wout * hout,
          flag_bias,
          fuse_relu);
    }
  }
}
}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(conv2d_transpose,
                     kARM,
                     kFloat,
                     kNCHW,
                     paddle::lite::kernels::arm::Conv2DTransposeCompute,
                     def)
Y
Yan Chunwei 已提交
173 174 175 176
    .BindInput("Input", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Bias", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindInput("Filter", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Output", {LiteType::GetTensorTy(TARGET(kARM))})
Y
Yan Chunwei 已提交
177
    .Finalize();