pool_compute.cc 6.1 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
// 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 "paddle/fluid/lite/kernels/arm/pool_compute.h"
#include <string>
#include <vector>
#include "paddle/fluid/lite/arm/math/funcs.h"
#include "paddle/fluid/lite/core/op_registry.h"
#include "paddle/fluid/lite/core/type_system.h"

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

T
tensor-tang 已提交
27 28 29 30
void PoolCompute::PrepareForRun() {
  auto& ctx = this->ctx_->template As<ARMContext>();
}

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
void PoolCompute::Run() {
  auto& param = Param<operators::PoolParam>();
  auto& in_dims = param.x->dims();
  auto& out_dims = param.output->dims();

  const float* din = param.x->data<float>();
  float* dout = param.output->mutable_data<float>();

  std::vector<int>& ksize = param.ksize;
  std::vector<int>& strides = param.strides;
  std::vector<int>& paddings = param.paddings;

  std::string& pooling_type = param.pooling_type;
  bool global_pooling = param.global_pooling;
  bool exclusive = param.exclusive;
  bool adaptive = param.adaptive;
  bool ceil_mode = param.ceil_mode;
  bool use_quantizer = param.use_quantizer;
  std::string& data_format = param.data_format;

H
hong19860320 已提交
51 52 53 54
  bool kps_equal = (ksize[0] == ksize[1]) && (strides[0] == strides[1]) &&
                   (paddings[0] == paddings[1]);

  if (global_pooling) {
55 56 57 58 59
    for (size_t i = 0; i < ksize.size(); ++i) {
      paddings[i] = 0;
      ksize[i] = static_cast<int>(in_dims[i + 2]);
    }
    if (pooling_type == "max") {
H
hong19860320 已提交
60 61 62 63 64
      lite::arm::math::pooling_global_max(din, dout, out_dims[0], out_dims[1],
                                          out_dims[2], out_dims[3], in_dims[1],
                                          in_dims[2], in_dims[3]);
      VLOG(3) << "invoking pooling_global_max";
      return;
65
    } else if (pooling_type == "avg") {
H
hong19860320 已提交
66 67 68 69 70
      lite::arm::math::pooling_global_avg(din, dout, out_dims[0], out_dims[1],
                                          out_dims[2], out_dims[3], in_dims[1],
                                          in_dims[2], in_dims[3]);
      VLOG(3) << "invoking pooling_global_ave";
      return;
71 72
    }
  } else {
H
hong19860320 已提交
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
    if (ksize[0] == 2 && strides[0] == 2 && paddings[0] == 0 && kps_equal) {
      if (pooling_type == "max") {
        lite::arm::math::pooling2x2s2_max(din, dout, out_dims[0], out_dims[1],
                                          out_dims[2], out_dims[3], in_dims[1],
                                          in_dims[2], in_dims[3]);
        VLOG(3) << "invoking pooling2x2s2_max";
        return;
      } else if (pooling_type == "avg") {
        lite::arm::math::pooling2x2s2_avg(din, dout, out_dims[0], out_dims[1],
                                          out_dims[2], out_dims[3], in_dims[1],
                                          in_dims[2], in_dims[3], exclusive);
        VLOG(3) << "invoking pooling2x2s2_avg";
        return;
      }
    } else if (ksize[0] == 3 && strides[0] == 1 && paddings[0] == 1 &&
               kps_equal) {
      if (pooling_type == "max") {
        lite::arm::math::pooling3x3s1p1_max(din, dout, out_dims[0], out_dims[1],
                                            out_dims[2], out_dims[3],
                                            in_dims[1], in_dims[2], in_dims[3]);
        VLOG(3) << "invokingpooling3x3s1p1_max";
        return;
      } else if (pooling_type == "avg") {
        lite::arm::math::pooling3x3s1p1_avg(
            din, dout, out_dims[0], out_dims[1], out_dims[2], out_dims[3],
            in_dims[1], in_dims[2], in_dims[3], exclusive);
        VLOG(3) << "invoking pooling3x3s1p1_avg";
        return;
      }
    } else if (ksize[0] == 3 && strides[0] == 2 && paddings[0] == 0 &&
               kps_equal) {
      if (pooling_type == "max") {
        lite::arm::math::pooling3x3s2p0_max(din, dout, out_dims[0], out_dims[1],
                                            out_dims[2], out_dims[3],
                                            in_dims[1], in_dims[2], in_dims[3]);
        VLOG(3) << "pooling3x3s2p0_max";
        return;
      } else if (pooling_type == "avg") {
        lite::arm::math::pooling3x3s2p0_avg(
            din, dout, out_dims[0], out_dims[1], out_dims[2], out_dims[3],
            in_dims[1], in_dims[2], in_dims[3], exclusive);
        VLOG(3) << "invoking pooling3x3s2p0_avg";
        return;
      }
    } else if (ksize[0] == 3 && strides[0] == 2 && paddings[0] == 1 &&
               kps_equal) {
      if (pooling_type == "max") {
        lite::arm::math::pooling3x3s2p1_max(din, dout, out_dims[0], out_dims[1],
                                            out_dims[2], out_dims[3],
                                            in_dims[1], in_dims[2], in_dims[3]);
        VLOG(3) << "invoking pooling3x3s2p1_max";
        return;
      } else if (pooling_type == "avg") {
        lite::arm::math::pooling3x3s2p1_avg(
            din, dout, out_dims[0], out_dims[1], out_dims[2], out_dims[3],
            in_dims[1], in_dims[2], in_dims[3], exclusive);
        VLOG(3) << "invoking pooling3x3s2p1_avg";
        return;
      }
    }
133
  }
H
hong19860320 已提交
134 135 136 137 138
  lite::arm::math::pooling_basic(
      din, dout, out_dims[0], out_dims[1], out_dims[2], out_dims[3], in_dims[1],
      in_dims[2], in_dims[3], ksize, strides, paddings, global_pooling,
      exclusive, adaptive, ceil_mode, use_quantizer, pooling_type);
  VLOG(3) << "invoking pooling_basic";
139 140 141 142 143 144 145
}

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

146
REGISTER_LITE_KERNEL(pool2d, kARM, kFloat, kNCHW,
147 148 149 150
                     paddle::lite::kernels::arm::PoolCompute, def)
    .BindInput("X", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("Out", {LiteType::GetTensorTy(TARGET(kARM))})
    .Finalize();