conv_depthwise.cc 13.0 KB
Newer Older
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
// 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_depthwise.h"
#include "lite/backends/arm/math/conv_block_utils.h"
#include "lite/backends/arm/math/conv_impl.h"

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

template <>
void DepthwiseConv<PRECISION(kFloat), PRECISION(kFloat)>::PrepareForRun() {
  auto& param = this->Param<param_t>();
  CHECK(this->ctx_);
  auto& ctx = this->ctx_->template As<ARMContext>();
  auto w_dims = param.filter->dims();
  auto kw = w_dims[3];
C
chenjiaoAngel 已提交
31 32 33
  auto channel = w_dims[0];
  auto hin = param.x->dims()[2];
  auto win = param.x->dims()[3];
34
  auto paddings = *param.paddings;
C
chenjiaoAngel 已提交
35
  bool ch_four = channel <= 4 * win;
36 37
  // select dw conv kernel
  if (kw == 3) {
38
    bool pads_less = ((paddings[1] < 2) && (paddings[3] < 2));
C
chenjiaoAngel 已提交
39
    if (ch_four && pads_less && paddings[0] == paddings[2] &&
H
HappyAngel 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        (paddings[0] == 0 || paddings[0] == 1)) {
      flag_trans_weights_ = false;
    } else {
      // trans weights
      constexpr int cblock = 4;
      auto oc = w_dims[0];
      auto kh = w_dims[2];
      auto cround = ROUNDUP(oc, cblock);
      weights_.Resize({cround, 1, kh, kw});
      auto w_data = weights_.mutable_data<float>();
      auto w_data_in = param.filter->data<float>();
      lite::arm::math::conv_trans_weights_numc(
          w_data_in, w_data, oc, 1, cblock, kh * kw);
      flag_trans_weights_ = true;
    }
55
    impl_ = lite::arm::math::conv_depthwise_3x3_fp32;
56 57 58
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_depthwise_3x3_fp32";
#endif
59
  } else if (kw == 5) {
C
chenjiaoAngel 已提交
60
    bool pads_five = (paddings[0] < 5) || (paddings[2] < 5);
61
    auto strides = param.strides;
C
chenjiaoAngel 已提交
62 63
    if (ch_four && pads_five && win >= kw && hin >= kw &&
        (strides[0] == 1 && strides[1] == 1)) {
C
chenjiaoAngel 已提交
64 65 66 67 68 69 70
      flag_trans_weights_ = false;
      impl_ = lite::arm::math::conv_depthwise_5x5_fp32;
#ifdef LITE_WITH_PROFILE
      kernel_func_name_ = "conv_depthwise_5x5_fp32";
#endif
    } else if ((strides[0] == 1 && strides[1] == 1) ||
               (strides[0] == 2 && strides[1] == 2)) {
71
      // trans weights
72 73 74 75 76 77 78 79 80 81
      constexpr int cblock = 4;
      auto oc = w_dims[0];
      auto kh = w_dims[2];
      auto cround = ROUNDUP(oc, cblock);
      weights_.Resize({cround, 1, kh, kw});
      auto w_data = weights_.mutable_data<float>();
      auto w_data_in = param.filter->data<float>();
      lite::arm::math::conv_trans_weights_numc(
          w_data_in, w_data, oc, 1, cblock, kh * kw);
      flag_trans_weights_ = true;
82
      impl_ = lite::arm::math::conv_depthwise_5x5_fp32;
83 84 85
#ifdef LITE_WITH_PROFILE
      kernel_func_name_ = "conv_depthwise_5x5_fp32";
#endif
86
    } else {
87 88
      LOG(FATAL)
          << "5x5 depthwise conv only support stride == 1 or stride == 2";
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
  } else {
    LOG(FATAL) << "this type dw conv not impl";
  }
}

template <>
void DepthwiseConv<PRECISION(kInt8), PRECISION(kFloat)>::PrepareForRun() {
  auto& param = this->Param<param_t>();
  CHECK(this->ctx_);
  auto& ctx = this->ctx_->template As<ARMContext>();
  auto w_dims = param.filter->dims();
  int kh = w_dims[2];
  int kw = w_dims[3];
  int oc = w_dims[0];
  /// update scale
  float in_scale = param.input_scale;
  auto& scale = param.weight_scale;
  CHECK(scale.size() == 1 || scale.size() == oc)
      << "weights scale size must = filter size or = 1";
  w_scale_.resize(oc);
  for (int i = 0; i < oc; ++i) {
    if (scale.size() == 1) {
      w_scale_[i] = scale[0] * in_scale;
    } else {
      w_scale_[i] = scale[i] * in_scale;
    }
  }
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

  auto paddings = *param.paddings;
  auto strides = param.strides;
  auto x_dims = param.x->dims();
  int iw = x_dims[3];
  int ih = x_dims[2];
  auto act_param = param.activation_param;
  bool has_act = act_param.has_active;
  lite_api::ActivationType act_type = act_param.active_type;
  // no activation and relu activation is supported now
  bool support_act_type =
      (has_act == false) ||
      (has_act == true && act_type == lite_api::ActivationType::kRelu);
  bool support_pad_type =
      (paddings[0] == paddings[1]) && (paddings[2] == paddings[3]) &&
      (paddings[0] == paddings[2]) && (paddings[0] == 0 || paddings[0] == 1);
  bool support_stride_type = (strides[0] == 1 && strides[1] == 1);
  bool support_width_type = iw > 9 ? true : false;
135 136
  /// select dw conv kernel
  if (kw == 3) {
Y
yiicy 已提交
137
    // trans weights
138
    impl_ = lite::arm::math::conv_depthwise_3x3_int8_fp32;
139 140 141
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_depthwise_3x3_int8_fp32";
#endif
142 143 144 145 146 147 148 149 150 151 152
    if (!support_act_type || !support_pad_type || !support_stride_type ||
        !support_width_type) {
      int cround = ROUNDUP(w_dims[0], 8);
      weights_.Resize({cround / 8, 1, kh * kw, 8});
      auto wptr = param.filter->data<int8_t>();
      auto wptr_new = weights_.mutable_data<int8_t>();
      lite::arm::math::conv_trans_weights_numc(wptr, wptr_new, oc, 1, 8, 9);
      flag_trans_weights_ = true;
    } else {
      flag_trans_weights_ = false;
    }
Y
yiicy 已提交
153 154 155
  } else if (kw == 5) {
    // trans weights
    impl_ = lite::arm::math::conv_depthwise_5x5_int8_fp32;
156 157 158
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_depthwise_5x5_int8_fp32";
#endif
Y
yiicy 已提交
159 160 161 162 163 164
    int cround = ROUNDUP(w_dims[0], 8);
    weights_.Resize({cround / 8, 1, kh * kw, 8});
    auto wptr = param.filter->data<int8_t>();
    auto wptr_new = weights_.mutable_data<int8_t>();
    lite::arm::math::conv_trans_weights_numc(wptr, wptr_new, oc, 1, 8, 25);
    flag_trans_weights_ = true;
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
  } else {
    LOG(FATAL) << "this type dw conv not impl";
  }
}

template <>
void DepthwiseConv<PRECISION(kInt8), PRECISION(kInt8)>::PrepareForRun() {
  auto& param = this->Param<param_t>();
  CHECK(this->ctx_);
  auto& ctx = this->ctx_->template As<ARMContext>();
  auto w_dims = param.filter->dims();
  int kw = w_dims[3];
  int kh = w_dims[2];
  int oc = w_dims[0];
  /// update scale
  float in_scale = param.input_scale;
  float out_scale = param.output_scale;
  auto& scale = param.weight_scale;
  CHECK(scale.size() == 1 || scale.size() == oc)
      << "weights scale size must = filter size or = 1";
  w_scale_.resize(oc);
  for (int i = 0; i < oc; ++i) {
    if (scale.size() == 1) {
      w_scale_[i] = scale[0] * in_scale / out_scale;
    } else {
      w_scale_[i] = scale[i] * in_scale / out_scale;
    }
  }
  /// update bias
  if (param.bias) {
    bias_.Resize(param.bias->dims());
    auto ptr = bias_.mutable_data<float>();
    auto ptr_in = param.bias->data<float>();
    for (int i = 0; i < bias_.numel(); ++i) {
      ptr[i] = ptr_in[i] / out_scale;
    }
    flag_trans_bias_ = true;
  }
203 204 205 206 207 208
  //! update relu6 parameter
  if (param.activation_param.has_active &&
      param.activation_param.active_type == lite_api::ActivationType::kRelu6) {
    param.activation_param.Relu_clipped_coef =
        param.activation_param.Relu_clipped_coef / param.output_scale;
  }
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

  auto paddings = *param.paddings;
  auto strides = param.strides;
  auto x_dims = param.x->dims();
  int iw = x_dims[3];
  int ih = x_dims[2];
  auto act_param = param.activation_param;

  bool has_act = act_param.has_active;
  lite_api::ActivationType act_type = act_param.active_type;
  // no activation and relu activation is supported now
  bool support_act_type =
      (has_act == false) ||
      (has_act == true && act_type == lite_api::ActivationType::kRelu);
  bool support_pad_type =
      (paddings[0] == paddings[1]) && (paddings[2] == paddings[3]) &&
      (paddings[0] == paddings[2]) && (paddings[0] == 0 || paddings[0] == 1);
  bool support_stride_type = (strides[0] == 1 && strides[1] == 1);
  bool support_width_type = iw > 9 ? true : false;
228 229
  /// select dw conv kernel
  if (kw == 3) {
Y
yiicy 已提交
230
    // trans weights
231
    impl_ = lite::arm::math::conv_depthwise_3x3_int8_int8;
232 233 234
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_depthwise_3x3_int8_int8";
#endif
235 236 237 238 239 240 241 242 243 244 245
    if (!support_act_type || !support_pad_type || !support_stride_type ||
        !support_width_type) {
      int cround = ROUNDUP(w_dims[0], 8);
      weights_.Resize({cround / 8, 1, kh * kw, 8});
      auto wptr = param.filter->data<int8_t>();
      auto wptr_new = weights_.mutable_data<int8_t>();
      lite::arm::math::conv_trans_weights_numc(wptr, wptr_new, oc, 1, 8, 9);
      flag_trans_weights_ = true;
    } else {
      flag_trans_weights_ = false;
    }
Y
yiicy 已提交
246 247 248
  } else if (kw == 5) {
    // trans weights
    impl_ = lite::arm::math::conv_depthwise_5x5_int8_int8;
249 250 251
#ifdef LITE_WITH_PROFILE
    kernel_func_name_ = "conv_depthwise_5x5_int8_int8";
#endif
Y
yiicy 已提交
252 253 254 255 256 257
    int cround = ROUNDUP(w_dims[0], 8);
    weights_.Resize({cround / 8, 1, kh * kw, 8});
    auto wptr = param.filter->data<int8_t>();
    auto wptr_new = weights_.mutable_data<int8_t>();
    lite::arm::math::conv_trans_weights_numc(wptr, wptr_new, oc, 1, 8, 25);
    flag_trans_weights_ = true;
258 259 260 261 262
  } else {
    LOG(FATAL) << "this type dw conv not impl";
  }
}

263 264 265 266 267 268 269 270
#ifdef LITE_WITH_PROFILE
template <>
void DepthwiseConv<PRECISION(kFloat), PRECISION(kFloat)>::
    SetProfileRuntimeKernelInfo(paddle::lite::profile::OpCharacter* ch) {
  ch->kernel_func_name = kernel_func_name_;
}
#endif

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
template <>
void DepthwiseConv<PRECISION(kFloat), PRECISION(kFloat)>::Run() {
  auto& param = this->Param<param_t>();
  CHECK(this->ctx_);
  auto& ctx = this->ctx_->template As<ARMContext>();
  const auto* i_data = param.x->data<float>();
  const auto* w_data = flag_trans_weights_ ? weights_.data<float>()
                                           : param.filter->data<float>();
  const auto* b_data = param.bias ? param.bias->data<float>() : nullptr;
  if (flag_trans_bias_) {
    b_data = bias_.data<float>();
  }
  auto* o_data = param.output->mutable_data<float>();

  auto x_dims = param.x->dims();
  auto w_dims = param.filter->dims();
  auto o_dims = param.output->dims();

  int iw = x_dims[3];  // nchw
  int ih = x_dims[2];
  int ic = x_dims[1];
  int bs = x_dims[0];
  int oh = o_dims[2];
  int ow = o_dims[3];
  int oc = o_dims[1];

  impl_(i_data,
        o_data,
        bs,
        oc,
        oh,
        ow,
        ic,
        ih,
        iw,
        w_data,
        b_data,
        param,
        &ctx,
        w_scale_.data());
}

313 314 315 316 317 318 319 320
#ifdef LITE_WITH_PROFILE
template <>
void DepthwiseConv<PRECISION(kInt8), PRECISION(kFloat)>::
    SetProfileRuntimeKernelInfo(paddle::lite::profile::OpCharacter* ch) {
  ch->kernel_func_name = kernel_func_name_;
}
#endif

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
template <>
void DepthwiseConv<PRECISION(kInt8), PRECISION(kFloat)>::Run() {
  auto& param = this->Param<param_t>();
  CHECK(this->ctx_);
  auto& ctx = this->ctx_->template As<ARMContext>();
  const auto* i_data = param.x->data<int8_t>();
  const auto* w_data = flag_trans_weights_ ? weights_.data<int8_t>()
                                           : param.filter->data<int8_t>();
  const auto* b_data = param.bias ? param.bias->data<float>() : nullptr;
  if (flag_trans_bias_) {
    b_data = bias_.data<float>();
  }
  auto* o_data = param.output->mutable_data<float>();

  auto x_dims = param.x->dims();
  auto w_dims = param.filter->dims();
  auto o_dims = param.output->dims();

339
  int iw = x_dims[3];
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  int ih = x_dims[2];
  int ic = x_dims[1];
  int bs = x_dims[0];
  int oh = o_dims[2];
  int ow = o_dims[3];
  int oc = o_dims[1];

  impl_(i_data,
        o_data,
        bs,
        oc,
        oh,
        ow,
        ic,
        ih,
        iw,
        w_data,
        b_data,
        param,
        &ctx,
        w_scale_.data());
}

363 364 365 366 367 368 369 370
#ifdef LITE_WITH_PROFILE
template <>
void DepthwiseConv<PRECISION(kInt8), PRECISION(kInt8)>::
    SetProfileRuntimeKernelInfo(paddle::lite::profile::OpCharacter* ch) {
  ch->kernel_func_name = kernel_func_name_;
}
#endif

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
template <>
void DepthwiseConv<PRECISION(kInt8), PRECISION(kInt8)>::Run() {
  auto& param = this->Param<param_t>();
  CHECK(this->ctx_);
  auto& ctx = this->ctx_->template As<ARMContext>();
  const auto* i_data = param.x->data<int8_t>();
  const auto* w_data = flag_trans_weights_ ? weights_.data<int8_t>()
                                           : param.filter->data<int8_t>();
  const auto* b_data = param.bias ? param.bias->data<float>() : nullptr;
  if (flag_trans_bias_) {
    b_data = bias_.data<float>();
  }
  auto* o_data = param.output->mutable_data<int8_t>();

  auto x_dims = param.x->dims();
  auto w_dims = param.filter->dims();
  auto o_dims = param.output->dims();

389
  int iw = x_dims[3];
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  int ih = x_dims[2];
  int ic = x_dims[1];
  int bs = x_dims[0];
  int oh = o_dims[2];
  int ow = o_dims[3];
  int oc = o_dims[1];

  impl_(i_data,
        o_data,
        bs,
        oc,
        oh,
        ow,
        ic,
        ih,
        iw,
        w_data,
        b_data,
        param,
        &ctx,
        w_scale_.data());
}

}  // namespace arm
}  // namespace kernels
}  // namespace lite
}  // namespace paddle