pool_compute.cc 14.1 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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/pool_compute.h"
#include <string>
#include <vector>
18
#include "lite/backends/arm/math/funcs.h"
Y
Yan Chunwei 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "lite/core/op_registry.h"
#include "lite/core/type_system.h"

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

void PoolCompute::PrepareForRun() {
  auto& ctx = this->ctx_->template As<ARMContext>();
}

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;
41
  std::vector<int>& paddings = *param.paddings;
Y
Yan Chunwei 已提交
42 43 44 45 46 47 48 49

  std::string& pooling_type = param.pooling_type;
  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;

50 51 52 53 54 55
  bool pads_less =
      (paddings[0] == paddings[2]) && (paddings[1] < 2) && (paddings[3] < 2);

  bool pads_equal = (paddings[0] == paddings[2]) &&
                    (paddings[0] == paddings[1]) &&
                    (paddings[2] == paddings[3]);
56
  bool kps_equal =
57
      (ksize[0] == ksize[1]) && (strides[0] == strides[1]) && pads_less;
58
  bool global_pooling = (paddings[0] == 0) && (ksize[0] == in_dims[2]) &&
59
                        (ksize[1] == in_dims[3]) && kps_equal && pads_equal;
60 61
  bool win_ksize = (in_dims[2] > ksize[0]) && (in_dims[3] > ksize[1]);
  kps_equal = kps_equal && win_ksize;
62
  global_pooling = param.global_pooling || global_pooling;
63

Y
Yan Chunwei 已提交
64 65
  if (global_pooling) {
    for (size_t i = 0; i < ksize.size(); ++i) {
66 67
      paddings[2 * i] = 0;
      paddings[2 * i + 1] = 0;
Y
Yan Chunwei 已提交
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
      ksize[i] = static_cast<int>(in_dims[i + 2]);
    }
    if (pooling_type == "max") {
      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]);
      return;
    } else if (pooling_type == "avg") {
      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]);
      return;
    }
  } else {
94 95 96 97 98 99 100 101 102 103 104
    if (ksize[0] == 1 && strides[0] == 2 && paddings[0] == 0 && kps_equal) {
      auto& ctx = this->ctx_->template As<ARMContext>();
      if (pooling_type == "max") {
        lite::arm::math::pooling1x1s2p0_max(din,
                                            dout,
                                            out_dims[0],
                                            out_dims[1],
                                            out_dims[2],
                                            out_dims[3],
                                            in_dims[1],
                                            in_dims[2],
105 106 107
                                            in_dims[3],
                                            paddings[1],
                                            paddings[3]);
108 109 110 111
        return;
      }
    } else if (ksize[0] == 2 && strides[0] == 2 && paddings[0] == 0 &&
               kps_equal) {
Y
Yan Chunwei 已提交
112
      if (pooling_type == "max") {
113 114 115 116 117 118 119 120 121 122 123
        lite::arm::math::pooling2x2s2p0_max(din,
                                            dout,
                                            out_dims[0],
                                            out_dims[1],
                                            out_dims[2],
                                            out_dims[3],
                                            in_dims[1],
                                            in_dims[2],
                                            in_dims[3],
                                            paddings[1],
                                            paddings[3]);
Y
Yan Chunwei 已提交
124 125
        return;
      } else if (pooling_type == "avg") {
126 127 128 129 130 131 132 133 134 135 136 137
        lite::arm::math::pooling2x2s2p0_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,
                                            paddings[1],
                                            paddings[3]);
Y
Yan Chunwei 已提交
138 139
        return;
      }
140
    } else if (ksize[0] == 2 && strides[0] == 2 && paddings[0] == 1 &&
141
               kps_equal) {
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
      if (pooling_type == "max") {
        lite::arm::math::pooling2x2s2p1_max(din,
                                            dout,
                                            out_dims[0],
                                            out_dims[1],
                                            out_dims[2],
                                            out_dims[3],
                                            in_dims[1],
                                            in_dims[2],
                                            in_dims[3],
                                            paddings[1],
                                            paddings[3]);
        return;
      } else if (pooling_type == "avg") {
        lite::arm::math::pooling2x2s2p1_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,
                                            paddings[1],
                                            paddings[3]);
        return;
      }
    } else if (ksize[0] == 3 && strides[0] == 1 && paddings[0] == 1 &&
               pads_equal && kps_equal) {
Y
Yan Chunwei 已提交
172 173 174 175 176 177 178 179 180
      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],
181 182 183
                                            in_dims[3],
                                            paddings[1],
                                            paddings[3]);
Y
Yan Chunwei 已提交
184 185 186 187 188 189 190 191 192 193 194
        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],
195 196 197
                                            exclusive,
                                            paddings[1],
                                            paddings[3]);
198 199 200
        return;
      }
    } else if (ksize[0] == 3 && strides[0] == 1 && paddings[0] == 0 &&
201
               pads_equal && kps_equal) {
202 203 204 205 206 207 208 209 210
      if (pooling_type == "max") {
        lite::arm::math::pooling3x3s1p0_max(din,
                                            dout,
                                            out_dims[0],
                                            out_dims[1],
                                            out_dims[2],
                                            out_dims[3],
                                            in_dims[1],
                                            in_dims[2],
211 212 213
                                            in_dims[3],
                                            paddings[1],
                                            paddings[3]);
214 215 216 217 218 219 220 221 222 223 224
        return;
      } else if (pooling_type == "avg") {
        lite::arm::math::pooling3x3s1p0_avg(din,
                                            dout,
                                            out_dims[0],
                                            out_dims[1],
                                            out_dims[2],
                                            out_dims[3],
                                            in_dims[1],
                                            in_dims[2],
                                            in_dims[3],
225 226 227
                                            exclusive,
                                            paddings[1],
                                            paddings[3]);
Y
Yan Chunwei 已提交
228 229 230
        return;
      }
    } else if (ksize[0] == 3 && strides[0] == 2 && paddings[0] == 0 &&
231
               pads_equal && kps_equal) {
Y
Yan Chunwei 已提交
232 233 234 235 236 237 238 239 240
      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],
241 242 243
                                            in_dims[3],
                                            paddings[1],
                                            paddings[3]);
Y
Yan Chunwei 已提交
244 245 246 247 248 249 250 251 252 253 254
        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],
255 256 257
                                            exclusive,
                                            paddings[1],
                                            paddings[3]);
Y
Yan Chunwei 已提交
258 259 260
        return;
      }
    } else if (ksize[0] == 3 && strides[0] == 2 && paddings[0] == 1 &&
261
               pads_equal && kps_equal) {
Y
Yan Chunwei 已提交
262 263 264 265 266 267 268 269 270
      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],
271 272 273
                                            in_dims[3],
                                            paddings[1],
                                            paddings[3]);
Y
Yan Chunwei 已提交
274 275 276 277 278 279 280 281 282 283 284
        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],
285 286 287
                                            exclusive,
                                            paddings[1],
                                            paddings[3]);
Y
Yan Chunwei 已提交
288 289 290 291
        return;
      }
    }
  }
292

Y
Yan Chunwei 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  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);
}

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

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