pool_3x3.cpp 33.3 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2018 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. */

#ifdef POOL_OP
W
wangliu 已提交
16 17 18
#ifdef _OPENMP
#include <omp.h>
#endif
W
wangliu 已提交
19
#include "framework/tensor.h"
20
#include "operators/math/pool_3x3.h"
Z
zhaojiaying01 已提交
21
#if __ARM_NEON
22 23 24
#include <arm_neon.h>
#endif  // __ARM_NEON
#include <climits>
W
wangliu 已提交
25 26 27 28 29 30 31
namespace paddle_mobile {
namespace operators {
namespace math {
using framework::Tensor;
using std::max;
using std::min;
using std::vector;
W
wangliu 已提交
32
void Pool3x3Avgs1p1(const Tensor *input, Tensor *output) {
Z
zhaojiaying01 已提交
33
#if __ARM_NEON
34 35
  const int batch_size = static_cast<int>(input->dims()[0]);
  const int input_channel = static_cast<int>(input->dims()[1]);
W
wangliu 已提交
36

37 38 39 40
  const int input_height = static_cast<int>(input->dims()[2]);
  const int input_width = static_cast<int>(input->dims()[3]);
  const int output_height = static_cast<int>(output->dims()[2]);
  const int output_width = static_cast<int>(output->dims()[3]);
Z
ZhenWang 已提交
41
  output->mutable_data<float>();
W
wangliu 已提交
42

43
  const int hxw = input_height * input_width;
W
wangliu 已提交
44

45
  const int l = input_height;
W
wangliu 已提交
46

47
  const float coef = 1.0 / 9.0;
48 49
  const float coef1 = 1.0 / 6.0;
  const float coef2 = 1.0 / 4.0;
50

51 52
  float32x4_t v_coef = vdupq_n_f32(coef);
  float32x4_t v_coef1 = vdupq_n_f32(coef1);
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  for (int b = 0; b < batch_size; b++) {
#pragma omp parallel for
    for (int c = 0; c < input_channel; c++) {
      const float *input_data = input->data<float>() + c * hxw;
      float *output_data = output->data<float>() + c * hxw;

      for (int i = 1; i < output_height - 1; i++) {
        float *output_ptr;
        float32x4_t in0, in1, in2, in3, in4, in5, tmp0, tmp1, tmp2, tmp3, tmp4,
            tmp5, out0;
        for (int m = 1; m < output_width - 4; m += 4) {
          output_ptr = output_data + i * output_width + m;
          in0 = vld1q_f32(input_data + (i - 1) * input_width + m - 1);
          in1 = vld1q_f32(input_data + (i - 1) * input_width + m + 3);
          in2 = vld1q_f32(input_data + i * input_width + m - 1);
          in3 = vld1q_f32(input_data + i * input_width + m + 3);
          in4 = vld1q_f32(input_data + (i + 1) * input_width + m - 1);
          in5 = vld1q_f32(input_data + (i + 1) * input_width + m + 3);
72 73 74 75 76 77 78 79

          tmp0 = vextq_f32(in0, in1, 1);
          tmp1 = vextq_f32(in0, in1, 2);
          tmp2 = vextq_f32(in2, in3, 1);
          tmp3 = vextq_f32(in2, in3, 2);
          tmp4 = vextq_f32(in4, in5, 1);
          tmp5 = vextq_f32(in4, in5, 2);

80 81 82 83 84 85 86 87 88 89 90 91 92 93
          out0 = in0;
          out0 = vaddq_f32(out0, tmp0);
          out0 = vaddq_f32(out0, tmp1);
          out0 = vaddq_f32(out0, in2);
          out0 = vaddq_f32(out0, tmp2);
          out0 = vaddq_f32(out0, tmp3);
          out0 = vaddq_f32(out0, in4);
          out0 = vaddq_f32(out0, tmp4);
          out0 = vaddq_f32(out0, tmp5);

          vst1q_f32(output_ptr, vmulq_f32(out0, v_coef));
        }
        int m;
        for (m = 1; (m + 3) < output_width - 1; m = m + 4) {
94 95
        }

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        for (int j = m; j < output_width - 1; j++) {
          output_data[i * output_width + j] =
              input_data[(i - 1) * input_width + j - 1] +
              input_data[(i - 1) * input_width + j] +
              input_data[(i - 1) * input_width + j + 1] +
              input_data[(i)*input_width + j - 1] +
              input_data[(i)*input_width + j] +
              input_data[(i)*input_width + j + 1] +
              input_data[(i + 1) * input_width + j - 1] +
              input_data[(i + 1) * input_width + j] +
              input_data[(i + 1) * input_width + j + 1];
          output_data[i * output_width + j] =
              output_data[i * output_width + j] * coef;
        }
      }
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
      output_data[0] =
          input_data[0] + input_data[1] + input_data[l] + input_data[l + 1];
      output_data[l - 1] = input_data[l - 2] + input_data[l - 1] +
                           input_data[2 * l - 2] + input_data[2 * l - 1];
      output_data[(l - 1) * l] =
          input_data[(l - 2) * l] + input_data[(l - 2) * l + 1] +
          input_data[(l - 1) * l] + input_data[(l - 1) * l + 1];
      output_data[l * l - 1] = input_data[(l - 2) * (l + 1)] +
                               input_data[(l - 2) * (l + 1) + 1] +
                               input_data[l * l - 2] + input_data[l * l - 1];
      output_data[0] = output_data[0] * coef2;
      output_data[l - 1] = output_data[l - 1] * coef2;
      output_data[(l - 1) * l] = output_data[(l - 1) * l] * coef2;
      output_data[l * l - 1] = output_data[l * l - 1] * coef2;

      for (int i = 1; i < l - 1; ++i) {
        output_data[i * l] = input_data[i * l - l] + input_data[i * l - l + 1] +
                             input_data[i * l] + input_data[i * l + 1] +
                             input_data[i * l + l] + input_data[i * l + l + 1];

        output_data[i * l + l - 1] =
            input_data[i * l + l - 1 - l - 1] + input_data[i * l + l - 1 - l] +
            input_data[i * l + l - 1 - 1] + input_data[i * l + l - 1] +
            input_data[i * l + l - 1 + l - 1] + input_data[i * l + l - 1 + l];
        output_data[i * l] = output_data[i * l] * coef1;
        output_data[i * l + l - 1] = output_data[i * l + l - 1] * coef1;
      }
139

140 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
      int m;
      for (m = 1; m < output_width - 4; m += 4) {
        float *output_ptr = output_data + m;
        float32x4_t in0, in1, in2, in3, tmp0, tmp1, tmp2, tmp3, out0;
        in0 = vld1q_f32(input_data + m - 1);
        in1 = vld1q_f32(input_data + m + 3);
        in2 = vld1q_f32(input_data + input_width + m - 1);
        in3 = vld1q_f32(input_data + input_width + m + 3);
        tmp0 = vextq_f32(in0, in1, 1);
        tmp1 = vextq_f32(in0, in1, 2);
        tmp2 = vextq_f32(in2, in3, 1);
        tmp3 = vextq_f32(in2, in3, 2);
        out0 = in0;
        out0 = vaddq_f32(out0, tmp0);
        out0 = vaddq_f32(out0, tmp1);
        out0 = vaddq_f32(out0, in2);
        out0 = vaddq_f32(out0, tmp2);
        out0 = vaddq_f32(out0, tmp3);

        vst1q_f32(output_ptr, vmulq_f32(out0, v_coef1));
      }

      for (m = 1; (m + 3) < output_width - 1; m += 4) {
      }
      for (int j = m; j < output_width - 1; j++) {
        output_data[j] = input_data[j - 1] + input_data[j] + input_data[j + 1] +
                         input_data[input_width + j - 1] +
                         input_data[input_width + j] +
                         input_data[input_width + j + 1];
        output_data[j] = output_data[j] * coef1;
      }

      for (m = 1; m < output_width - 4; m += 4) {
        float *output_ptr =
            output_data + (output_height - 1) * output_width + m;

        float32x4_t in0, in1, in2, in3, tmp0, tmp1, tmp2, tmp3, out0;
        in0 = vld1q_f32(input_data + (output_height - 2) * input_width + m - 1);
        in1 = vld1q_f32(input_data + (output_height - 2) * input_width + m + 3);
        in2 = vld1q_f32(input_data + (output_height - 1) * input_width + m - 1);
        in3 = vld1q_f32(input_data + (output_height - 1) * input_width + m + 3);
        tmp0 = vextq_f32(in0, in1, 1);
        tmp1 = vextq_f32(in0, in1, 2);
        tmp2 = vextq_f32(in2, in3, 1);
        tmp3 = vextq_f32(in2, in3, 2);
        out0 = in0;
        out0 = vaddq_f32(out0, tmp0);
        out0 = vaddq_f32(out0, tmp1);
        out0 = vaddq_f32(out0, in2);
        out0 = vaddq_f32(out0, tmp2);
        out0 = vaddq_f32(out0, tmp3);

        vst1q_f32(output_ptr, vmulq_f32(out0, v_coef1));
      }
      for (m = 1; (m + 3) < output_width - 1; m = m + 4) {
      }
      for (int j = m; j < output_width - 1; j++) {
        output_data[(output_height - 1) * input_width + j] =
            input_data[(output_height - 2) * input_width + j - 1] +
            input_data[(output_height - 2) * input_width + j] +
            input_data[(output_height - 2) * input_width + j + 1] +
            input_data[(output_height - 1) * input_width + j - 1] +
            input_data[(output_height - 1) * input_width + j] +
            input_data[(output_height - 1) * input_width + j + 1];
        output_data[(output_height - 1) * output_width + j] =
            output_data[(output_height - 1) * output_width + j] * coef1;
W
wangliu 已提交
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 231 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 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

//  const int batch_size = input->dims()[0];
//
//  const int h_in = input->dims()[2];
//
//  const int w_in = input->dims()[3];
//
//  const int output_channels = output->dims()[1];
//
//  const int h_out = output->dims()[2];
//  const int w_out = output->dims()[3];
//  const int outputdata_channel_stride = h_out * w_out;
//  const int inputdata_channel_stride = h_in * w_in;
//  const int input_batch_stride = output_channels * inputdata_channel_stride;
//  const int output_batch_stride = output_channels *
//  outputdata_channel_stride; float *out_data = output->data<float>(); const
//  float *input_data = input->data<float>();
//
//  const float coef = 1.0 / 9.0;
//  for (int k = 0; k < batch_size; ++k) {
// #pragma omp parallel for
//    for (int c = 0; c < output_channels; ++c) {
//      const float *input_seg = input_data + c * inputdata_channel_stride;
//      float *output_seg = out_data + c * outputdata_channel_stride;
//      // four corner point
//      output_seg[0] = (input_seg[0] + input_seg[1] + input_seg[w_in] +
//                       input_seg[w_in + 1]) *
//                      coef;
//      output_seg[w_out - 1] =
//          (input_seg[w_in - 2] + input_seg[w_in - 1] + input_seg[w_in * 2 -
//          2] +
//           input_seg[2 * w_in - 1]) *
//          coef;
//      output_seg[(h_out - 1) * w_out] =
//          (input_seg[(h_in - 2) * w_in] + input_seg[(h_in - 2) * w_in + 1] +
//           input_seg[(h_in - 1) * w_in] + input_seg[(h_in - 1) * w_in + 1])
//           *
//          coef;
//      output_seg[h_out * w_out - 1] =
//          (input_seg[h_in * w_in - 1] + input_seg[h_in * w_in - 2] +
//           input_seg[(h_in - 1) * w_in - 1] +
//           input_seg[(h_in - 1) * w_in - 2]) *
//          coef;
//      // left side & right side
//      for (int i = 1; i < h_in - 1; ++i) {
//        output_seg[i * w_out] =
//            (input_seg[i * w_in - w_in] + input_seg[i * w_in - w_in + 1] +
//             input_seg[i * w_in] + input_seg[i * w_in + 1] +
//             input_seg[i * w_in + w_in] + input_seg[i * w_in + w_in + 1]) *
//            coef;
//        output_seg[i * w_out + w_out - 1] =
//            (input_seg[i * w_in - w_in + w_in - 2] +
//             input_seg[i * w_in - w_in + 1 + w_in - 2] +
//             input_seg[i * w_in + w_in - 2] +
//             input_seg[i * w_in + 1 + w_in - 2] +
//             input_seg[i * w_in + w_in + w_in - 2] +
//             input_seg[i * w_in + w_in + 1 + w_in - 2]) *
//            coef;
//      }
//      // top 1 row & bottom 1 row
//      const float *input_tmp = input_seg;
//
//      float32x4_t in0, in1, in2, in3, in4, in5, in6, in7, tmp0, tmp1, tmp2,
//          tmp3, tmp4, tmp5, sum, out0;
//      float32x4_t v_coef = vdupq_n_f32(coef);
//      in0 = vld1q_f32(input_tmp);
//      in2 = vld1q_f32(input_tmp + w_in);
//      const float *input_tmp_end = input_tmp + (h_in - 2) * w_in;
//      in4 = vld1q_f32(input_tmp_end);
//      in6 = vld1q_f32(input_tmp_end + w_in);
//      int c_mid = w_out - 2;
//      auto output_ptr = output_seg + 1;
//      for (; c_mid > 3; c_mid -= 4) {
//        in1 = vld1q_f32(input_tmp + 4);
//        in3 = vld1q_f32(input_tmp + w_in + 4);
//
//        tmp0 = vextq_f32(in0, in1, 1);
//        tmp1 = vextq_f32(in0, in1, 2);
//
//        tmp2 = vextq_f32(in2, in3, 1);
//        tmp3 = vextq_f32(in2, in3, 2);
//
//        sum = vaddq_f32(in0, tmp0);
//        sum = vaddq_f32(sum, tmp1);
//        sum = vaddq_f32(sum, in2);
//        sum = vaddq_f32(sum, tmp2);
//        sum = vaddq_f32(sum, tmp3);
//
//        vst1q_f32(output_ptr, vmulq_f32(sum, v_coef));
//
//        in5 = vld1q_f32(input_tmp_end + 4);
//        in7 = vld1q_f32(input_tmp_end + w_in + 4);
//
//        tmp0 = vextq_f32(in4, in5, 1);
//        tmp1 = vextq_f32(in4, in5, 2);
//        tmp2 = vextq_f32(in6, in7, 1);
//        tmp3 = vextq_f32(in6, in7, 2);
//
//        sum = vaddq_f32(in0, tmp0);
//        sum = vaddq_f32(sum, tmp1);
//        sum = vaddq_f32(sum, in2);
//        sum = vaddq_f32(sum, tmp2);
//        sum = vaddq_f32(sum, tmp3);
//
//        vst1q_f32(output_ptr + (h_out - 1) * w_out, vmulq_f32(sum, v_coef));
//
//        // can optimize to each 8 stride.
//        input_tmp += 4;
//        input_tmp_end += 4;
//        output_ptr += 4;
//        in0 = in1;
//        in2 = in3;
//        in4 = in5;
//        in6 = in7;
//      }
//      // top right remain
//      float32x4_t pad0 = vdupq_n_f32(input_seg[w_in - 1]);
//      float32x4_t pad1 = vdupq_n_f32(input_seg[2 * w_in - 1]);
//
//      tmp0 = vextq_f32(in0, pad0, 1);
//      tmp1 = vextq_f32(in0, pad0, 2);
//      tmp2 = vextq_f32(in2, pad1, 2);
//      tmp3 = vextq_f32(in2, pad1, 2);
//
//      sum = vaddq_f32(in0, tmp0);
//      sum = vaddq_f32(sum, tmp1);
//      sum = vaddq_f32(sum, in2);
//      sum = vaddq_f32(sum, tmp2);
//      sum = vaddq_f32(sum, tmp3);
//      out0 = vmulq_f32(sum, v_coef);
//
//      for (int i = 0; i < c_mid; ++i) {
//        if (i == 0) {
//          vst1q_lane_f32(output_ptr + i, out0, 0);
//        }
//        if (i == 1) {
//          vst1q_lane_f32(output_ptr + i, out0, 1);
//        }
//        if (i == 2) {
//          vst1q_lane_f32(output_ptr + i, out0, 2);
//        }
//      }
//
//      // bottom_right remain
//      float32x4_t pad2 = vdupq_n_f32(input_seg[(h_in - 1) * w_in - 1]);
//      float32x4_t pad3 = vdupq_n_f32(input_seg[h_in * w_in - 1]);
//
//      tmp0 = vextq_f32(in4, pad2, 1);
//      tmp1 = vextq_f32(in4, pad2, 2);
//      tmp2 = vextq_f32(in6, pad3, 2);
//      tmp3 = vextq_f32(in6, pad3, 2);
//
//      sum = vaddq_f32(in4, tmp0);
//      sum = vaddq_f32(sum, tmp1);
//      sum = vaddq_f32(sum, in6);
//      sum = vaddq_f32(sum, tmp2);
//      sum = vaddq_f32(sum, tmp3);
//      out0 = vmulq_f32(sum, v_coef);
//
//      for (int i = 0; i < c_mid; ++i) {
//        if (i == 0) {
//          vst1q_lane_f32(output_ptr + (h_out - 1) * w_out + i, out0, 0);
//        }
//        if (i == 1) {
//          vst1q_lane_f32(output_ptr + (h_out - 1) * w_out + i, out0, 1);
//        }
//        if (i == 2) {
//          vst1q_lane_f32(output_ptr + (h_out - 1) * w_out + i, out0, 2);
//        }
//      }
//      // mid
//      for (int j = 0; j < h_out - 2; ++j) {
//        output_ptr = output_seg + w_out * (j + 1) + 1;
//        input_tmp = input_seg + j * w_in;
//
//        in0 = vld1q_f32(input_tmp);
//        in2 = vld1q_f32(input_tmp + w_in);
//        in4 = vld1q_f32(input_tmp + 2 * w_in);
//        c_mid = w_out - 2;
//        for (; c_mid > 3; c_mid -= 4) {
//          in1 = vld1q_f32(input_tmp + 4);
//          in3 = vld1q_f32(input_tmp + w_in + 4);
//          in5 = vld1q_f32(input_tmp + 2 * w_in + 4);
//
//          tmp0 = vextq_f32(in0, in1, 1);
//          tmp1 = vextq_f32(in0, in1, 2);
//          tmp2 = vextq_f32(in2, in3, 1);
//          tmp3 = vextq_f32(in2, in3, 2);
//          tmp4 = vextq_f32(in4, in5, 1);
//          tmp5 = vextq_f32(in4, in5, 2);
//
//          sum = vaddq_f32(in0, tmp0);
//          sum = vaddq_f32(sum, tmp1);
//          sum = vaddq_f32(sum, in2);
//          sum = vaddq_f32(sum, tmp2);
//          sum = vaddq_f32(sum, tmp3);
//          sum = vaddq_f32(sum, in4);
//          sum = vaddq_f32(sum, tmp4);
//          sum = vaddq_f32(sum, tmp5);
//
//          out0 = vmulq_f32(sum, v_coef);
//          vst1q_f32(output_ptr, out0);
//          output_ptr += 4;
//          input_tmp += 4;
//          in0 = in1;
//          in2 = in3;
//          in4 = in5;
//        }
//        // mid remain
//        float32x4_t pad0 = vdupq_n_f32(input_seg[(j + 1) * w_in - 1]);
//        float32x4_t pad1 = vdupq_n_f32(input_seg[(j + 2) * w_in - 1]);
//        float32x4_t pad2 = vdupq_n_f32(input_seg[(j + 2) * w_in - 1]);
//
//        tmp0 = vextq_f32(in0, pad0, 1);
//        tmp1 = vextq_f32(in0, pad0, 2);
//        tmp2 = vextq_f32(in2, pad1, 1);
//        tmp3 = vextq_f32(in2, pad1, 2);
//        tmp4 = vextq_f32(in4, pad2, 1);
//        tmp5 = vextq_f32(in4, pad2, 2);
//
//        sum = vaddq_f32(in0, tmp0);
//        sum = vaddq_f32(sum, tmp1);
//        sum = vaddq_f32(sum, in2);
//        sum = vaddq_f32(sum, tmp2);
//        sum = vaddq_f32(sum, tmp3);
//        sum = vaddq_f32(sum, in4);
//        sum = vaddq_f32(sum, tmp4);
//        sum = vaddq_f32(sum, tmp5);
//        out0 = vmulq_f32(sum, v_coef);
//
//        for (int i = 0; i < c_mid; ++i) {
//          if (i == 0) {
//            vst1q_lane_f32(output_ptr + i, out0, 0);
//          }
//          if (i == 1) {
//            vst1q_lane_f32(output_ptr + i, out0, 1);
//          }
//          if (i == 2) {
//            vst1q_lane_f32(output_ptr + i, out0, 2);
//          }
//        }
//      }
//      //      input_data += inputdata_channel_stride;
//      //      out_data += outputdata_channel_stride;
//    }
//    input_data += input_batch_stride;
//    out_data += output_batch_stride;
//  }
W
wangliu 已提交
457 458 459 460
#endif
}

void Pool3x3Maxs1p1(const Tensor *input, Tensor *output) {
Z
zhaojiaying01 已提交
461
#if __ARM_NEON
W
wangliu 已提交
462 463 464 465 466 467 468 469 470 471 472 473
  const int batch_size = input->dims()[0];

  const int h_in = input->dims()[2];

  const int w_in = input->dims()[3];

  const int output_channels = output->dims()[1];

  const int h_out = output->dims()[2];
  const int w_out = output->dims()[3];
  const int outputdata_channel_stride = h_out * w_out;
  const int inputdata_channel_stride = h_in * w_in;
W
wangliu 已提交
474 475
  const int input_batch_stride = output_channels * inputdata_channel_stride;
  const int output_batch_stride = output_channels * outputdata_channel_stride;
Z
ZhenWang 已提交
476
  float *out_data = output->mutable_data<float>();
W
wangliu 已提交
477 478
  const float *input_data = input->data<float>();
  for (int k = 0; k < batch_size; ++k) {
W
wangliu 已提交
479
#pragma omp parallel for
W
wangliu 已提交
480
    for (int c = 0; c < output_channels; ++c) {
W
wangliu 已提交
481 482
      const float *input_seg = input_data + c * inputdata_channel_stride;
      float *output_seg = out_data + c * outputdata_channel_stride;
W
wangliu 已提交
483
      // four corner point
W
wangliu 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497
      output_seg[0] = std::max(std::max(input_seg[0], input_seg[1]),
                               std::max(input_seg[w_in], input_seg[w_in + 1]));
      output_seg[w_out - 1] =
          std::max(std::max(input_seg[w_in - 2], input_seg[w_in - 1]),
                   std::max(input_seg[w_in * 2 - 2], input_seg[2 * w_in - 1]));
      output_seg[(h_out - 1) * w_out] =
          std::max(std::max(input_seg[(h_in - 2) * w_in],
                            input_seg[(h_in - 2) * w_in + 1]),
                   std::max(input_seg[(h_in - 1) * w_in],
                            input_seg[(h_in - 1) * w_in + 1]));
      output_seg[h_out * w_out - 1] = std::max(
          std::max(input_seg[(h_in - 1) * w_in - 1],
                   input_seg[(h_in - 1) * w_in - 2]),
          std::max(input_seg[h_in * w_in - 1], input_seg[h_in * w_in - 2]));
W
wangliu 已提交
498 499
      // left side & right side
      for (int i = 1; i < h_in - 1; ++i) {
W
wangliu 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
        float max1 = std::max(input_seg[i * w_in - w_in],
                              input_seg[i * w_in - w_in + 1]);
        float max2 = std::max(input_seg[i * w_in], input_seg[i * w_in + 1]);
        float max3 = std::max(input_seg[i * w_in + w_in],
                              input_seg[i * w_in + w_in + 1]);
        output_seg[i * w_out] = std::max(std::max(max1, max2), max3);

        max1 = std::max(input_seg[i * w_in - w_in + w_in - 2],
                        input_seg[i * w_in - w_in + 1 + w_in - 2]);
        max2 = std::max(input_seg[i * w_in + w_in - 2],
                        input_seg[i * w_in + 1 + w_in - 2]);
        max3 = std::max(input_seg[i * w_in + w_in + w_in - 2],
                        input_seg[i * w_in + w_in + 1 + w_in - 2]);
        output_seg[i * w_out + w_out - 1] =
            std::max(std::max(max1, max2), max3);
W
wangliu 已提交
515 516
      }
      // top 1 row & bottom 1 row
W
wangliu 已提交
517
      const float *input_tmp = input_seg;
W
wangliu 已提交
518 519 520 521 522 523 524 525 526

      float32x4_t in0, in1, in2, in3, in4, in5, in6, in7, tmp0, tmp1, tmp2,
          tmp3, tmp4, tmp5, max;
      in0 = vld1q_f32(input_tmp);
      in2 = vld1q_f32(input_tmp + w_in);
      const float *input_tmp_end = input_tmp + (h_in - 2) * w_in;
      in4 = vld1q_f32(input_tmp_end);
      in6 = vld1q_f32(input_tmp_end + w_in);
      int c_mid = w_out - 2;
W
wangliu 已提交
527
      auto output_ptr = output_seg + 1;
W
wangliu 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
      for (; c_mid > 3; c_mid -= 4) {
        in1 = vld1q_f32(input_tmp + 4);
        in3 = vld1q_f32(input_tmp + w_in + 4);

        tmp0 = vextq_f32(in0, in1, 1);
        tmp1 = vextq_f32(in0, in1, 2);

        tmp2 = vextq_f32(in2, in3, 1);
        tmp3 = vextq_f32(in2, in3, 2);

        max = vmaxq_f32(in0, tmp0);
        max = vmaxq_f32(max, tmp1);
        max = vmaxq_f32(max, in2);
        max = vmaxq_f32(max, tmp2);
        max = vmaxq_f32(max, tmp3);

        vst1q_f32(output_ptr, max);

        in5 = vld1q_f32(input_tmp_end + 4);
        in7 = vld1q_f32(input_tmp_end + w_in + 4);

        tmp0 = vextq_f32(in4, in5, 1);
        tmp1 = vextq_f32(in4, in5, 2);
        tmp2 = vextq_f32(in6, in7, 1);
        tmp3 = vextq_f32(in6, in7, 2);

        max = vmaxq_f32(in4, tmp0);
        max = vmaxq_f32(max, tmp1);
        max = vmaxq_f32(max, in6);
        max = vmaxq_f32(max, tmp2);
        max = vmaxq_f32(max, tmp3);

        vst1q_f32(output_ptr + (h_out - 1) * w_out, max);

        input_tmp += 4;
        input_tmp_end += 4;
        output_ptr += 4;
        in0 = in1;
        in2 = in3;
        in4 = in5;
        in6 = in7;
      }
      // top right remain
W
wangliu 已提交
571 572
      float32x4_t pad0 = vdupq_n_f32(input_seg[w_in - 1]);
      float32x4_t pad1 = vdupq_n_f32(input_seg[2 * w_in - 1]);
W
wangliu 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

      tmp0 = vextq_f32(in0, pad0, 1);
      tmp1 = vextq_f32(in0, pad0, 2);
      tmp2 = vextq_f32(in2, pad1, 1);
      tmp3 = vextq_f32(in2, pad1, 2);

      max = vmaxq_f32(in0, tmp0);
      max = vmaxq_f32(max, tmp1);
      max = vmaxq_f32(max, in2);
      max = vmaxq_f32(max, tmp2);
      max = vmaxq_f32(max, tmp3);

      for (int i = 0; i < c_mid; ++i) {
        if (i == 0) {
          vst1q_lane_f32(output_ptr + i, max, 0);
        }
        if (i == 1) {
          vst1q_lane_f32(output_ptr + i, max, 1);
        }
        if (i == 2) {
          vst1q_lane_f32(output_ptr + i, max, 2);
        }
      }

      // bottom_right remain
W
wangliu 已提交
598 599
      float32x4_t pad2 = vdupq_n_f32(input_seg[(h_in - 1) * w_in - 1]);
      float32x4_t pad3 = vdupq_n_f32(input_seg[h_in * w_in - 1]);
W
wangliu 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

      tmp0 = vextq_f32(in4, pad2, 1);
      tmp1 = vextq_f32(in4, pad2, 2);
      tmp2 = vextq_f32(in6, pad3, 1);
      tmp3 = vextq_f32(in6, pad3, 2);

      max = vmaxq_f32(in4, tmp0);
      max = vmaxq_f32(max, tmp1);
      max = vmaxq_f32(max, in6);
      max = vmaxq_f32(max, tmp2);
      max = vmaxq_f32(max, tmp3);

      for (int i = 0; i < c_mid; ++i) {
        if (i == 0) {
          vst1q_lane_f32(output_ptr + (h_out - 1) * w_out + i, max, 0);
        }
        if (i == 1) {
          vst1q_lane_f32(output_ptr + (h_out - 1) * w_out + i, max, 1);
        }
        if (i == 2) {
          vst1q_lane_f32(output_ptr + (h_out - 1) * w_out + i, max, 2);
        }
      }
      // mid
      for (int j = 0; j < h_out - 2; ++j) {
W
wangliu 已提交
625 626
        output_ptr = output_seg + (j + 1) * w_out + 1;
        input_tmp = input_seg + j * w_in;
W
wangliu 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

        in0 = vld1q_f32(input_tmp);
        in2 = vld1q_f32(input_tmp + w_in);
        in4 = vld1q_f32(input_tmp + 2 * w_in);
        c_mid = w_out - 2;
        for (; c_mid > 3; c_mid -= 4) {
          in1 = vld1q_f32(input_tmp + 4);
          in3 = vld1q_f32(input_tmp + w_in + 4);
          in5 = vld1q_f32(input_tmp + 2 * w_in + 4);

          tmp0 = vextq_f32(in0, in1, 1);
          tmp1 = vextq_f32(in0, in1, 2);
          tmp2 = vextq_f32(in2, in3, 1);
          tmp3 = vextq_f32(in2, in3, 2);
          tmp4 = vextq_f32(in4, in5, 1);
          tmp5 = vextq_f32(in4, in5, 2);

          max = vmaxq_f32(in0, tmp0);
          max = vmaxq_f32(max, tmp1);
          max = vmaxq_f32(max, in2);
          max = vmaxq_f32(max, tmp2);
          max = vmaxq_f32(max, tmp3);
          max = vmaxq_f32(max, in4);
          max = vmaxq_f32(max, tmp4);
          max = vmaxq_f32(max, tmp5);

          vst1q_f32(output_ptr, max);
          output_ptr += 4;
          input_tmp += 4;
          in0 = in1;
          in2 = in3;
          in4 = in5;
        }
        // mid remain
W
wangliu 已提交
661 662 663
        float32x4_t pad0 = vdupq_n_f32(input_seg[(j + 1) * w_in - 1]);
        float32x4_t pad1 = vdupq_n_f32(input_seg[(j + 2) * w_in - 1]);
        float32x4_t pad2 = vdupq_n_f32(input_seg[(j + 3) * w_in - 1]);
W
wangliu 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692

        tmp0 = vextq_f32(in0, pad0, 1);
        tmp1 = vextq_f32(in0, pad0, 2);
        tmp2 = vextq_f32(in2, pad1, 1);
        tmp3 = vextq_f32(in2, pad1, 2);
        tmp4 = vextq_f32(in4, pad2, 1);
        tmp5 = vextq_f32(in4, pad2, 2);

        max = vmaxq_f32(in0, tmp0);
        max = vmaxq_f32(max, tmp1);
        max = vmaxq_f32(max, in2);
        max = vmaxq_f32(max, tmp2);
        max = vmaxq_f32(max, tmp3);
        max = vmaxq_f32(max, in4);
        max = vmaxq_f32(max, tmp4);
        max = vmaxq_f32(max, tmp5);

        for (int i = 0; i < c_mid; ++i) {
          if (i == 0) {
            vst1q_lane_f32(output_ptr + i, max, 0);
          }
          if (i == 1) {
            vst1q_lane_f32(output_ptr + i, max, 1);
          }
          if (i == 2) {
            vst1q_lane_f32(output_ptr + i, max, 2);
          }
        }
      }
W
wangliu 已提交
693 694
      //      input_data += inputdata_channel_stride;
      //      out_data += outputdata_channel_stride;
W
wangliu 已提交
695
    }
W
wangliu 已提交
696 697
    input_data += input_batch_stride;
    out_data += output_batch_stride;
W
wangliu 已提交
698
  }
699 700
#else

W
wangliu 已提交
701 702
#endif
}
W
wangliu 已提交
703 704 705

void Pool3x3Max(vector<int> strides, vector<int> paddings, const Tensor *input,
                Tensor *output) {
Z
zhaojiaying01 已提交
706
#if __ARM_NEON
W
wangliu 已提交
707 708 709 710 711 712 713 714 715 716
  const int batch_size = input->dims()[0];

  const int input_height = input->dims()[2];

  const int input_width = input->dims()[3];

  const int output_channels = output->dims()[1];

  const int output_height = output->dims()[2];
  const int output_width = output->dims()[3];
W
wangliu 已提交
717 718 719 720 721
  //  const int _kernel_size = 3;
  const int stride = strides[0];
  //  const int stride_width = strides[1];
  const int padding = paddings[0];
  //  const int padding_width = paddings[1];
W
wangliu 已提交
722 723 724 725 726 727 728 729 730
  const float negative_max = -INT_MAX;
  const int input_channel_stride = input_height * input_width;
  const int output_channel_stride = output_height * output_width;

  const float *input_data = input->data<float>();
  float *output_data = output->mutable_data<float>();

  const int input_batch_stride = output_channels * input_channel_stride;
  const int output_batch_stride = output_channels * output_channel_stride;
W
wangliu 已提交
731
  const float *pos1, *output_ptr;
W
wangliu 已提交
732 733
  int hstart, wstart, hend, wend;
  for (int i = 0; i < batch_size; ++i) {
W
wangliu 已提交
734
#pragma omp parallel for
W
wangliu 已提交
735
    for (int c = 0; c < output_channels; ++c) {
W
wangliu 已提交
736 737
      const float *input_seg = input_data + c * input_channel_stride;
      float *output_seg = output_data + c * output_channel_stride;
W
wangliu 已提交
738
      for (int ph = 0; ph < output_height; ph++) {
739 740 741
        int hstart = ph * stride - padding;
        int hend = min(hstart + 3, input_height);
        hstart = max(hstart, 0);
W
wangliu 已提交
742
        for (int pw = 0; pw < output_width; pw++) {
W
wangliu 已提交
743
          int wstart = pw * stride - padding;
744
          int wend = min(wstart + 3, input_width);
W
wangliu 已提交
745
          wstart = max(wstart, 0);
W
wangliu 已提交
746 747 748 749
          const float *pos1 = input_seg + hstart * input_width + wstart;
          const float *pos2 = input_seg + (hstart + 1) * input_width + wstart;
          const float *pos3 = input_seg + (hstart + 2) * input_width + wstart;
          output_ptr = output_seg + ph * output_width + pw;
W
wangliu 已提交
750 751 752 753 754

          if (hend - hstart != 3 || wend - wstart != 3) {
            float max_value = -INT_MAX;
            for (int h = hstart; h < hend; h++) {
              for (int w = wstart; w < wend; w++) {
W
wangliu 已提交
755
                float value = input_seg[h * input_width + w];
W
wangliu 已提交
756 757 758 759 760
                if (value > max_value) {
                  max_value = value;
                }
              }
            }
W
wangliu 已提交
761
            output_seg[ph * output_width + pw] = max_value;
W
wangliu 已提交
762
          } else {
763 764 765 766 767 768 769 770 771 772 773 774
#if __aarch64__
            const float32x4_t data1 = vld1q_f32(pos1);
            const float32x4_t data2 = vld1q_f32(pos1 + input_width);
            const float32x4_t data3 = vld1q_f32(pos1 + 2 * input_width);
            const float32x4_t max_data =
                vmaxq_f32(vmaxq_f32(data1, data2), data3);
            float32x2_t res =
                vpmax_f32(vget_high_f32(vsetq_lane_f32(-INT_MAX, max_data, 3)),
                          vget_low_f32(max_data));
            res = vpmax_f32(res, res);
            output_seg[ph * output_width + pw] = vget_lane_f32(res, 0);
#else
W
wangliu 已提交
775 776 777 778 779 780 781 782 783 784 785
            asm volatile(
                "vld1.32  {q1}, [%[pos1]]        \n\t"
                "vld1.32  {q2}, [%[pos2]]        \n\t"
                "vld1.32  {q3}, [%[pos3]]        \n\t"
                "vmax.f32 q1, q1, q2            \n\t"
                "vmax.f32 q2, q1, q3            \n\t"
                "vmov.f32 d5[1],  %[negative_max]         \n\t"
                "vpmax.f32  d6, d4, d5            \n\t"
                "vpmax.f32  d7, d6, d6             \n\t"
                "vst1.32 {d7[0]},[%[output_ptr]]    \n\t"
                :
W
wangliu 已提交
786
                : [input_seg] "r"(input_seg), [pos1] "r"(pos1),
W
wangliu 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
                  [pos2] "r"(pos2), [pos3] "r"(pos3),
                  [output_ptr] "r"(output_ptr), [negative_max] "r"(negative_max)
                : "memory", "q1", "q2", "q3", "q4");
#endif
          }
        }
      }
    }
    input_data += input_batch_stride;
    output_data += output_batch_stride;
  }
#endif
}

void Pool3x3Avg(vector<int> strides, vector<int> paddings, const Tensor *input,
                Tensor *output) {
Z
zhaojiaying01 已提交
803
#if __ARM_NEON
W
wangliu 已提交
804 805 806 807 808 809 810 811 812 813
  const int batch_size = input->dims()[0];

  const int input_height = input->dims()[2];

  const int input_width = input->dims()[3];

  const int output_channels = output->dims()[1];

  const int output_height = output->dims()[2];
  const int output_width = output->dims()[3];
W
wangliu 已提交
814 815
  const int stride = strides[0];
  const int padding = paddings[0];
W
wangliu 已提交
816 817 818 819 820 821 822 823 824 825 826 827 828

  const int input_channel_stride = input_height * input_width;
  const int output_channel_stride = output_height * output_width;

  const float *input_data = input->data<float>();
  float *output_data = output->mutable_data<float>();
  const float zero = 0;
  const float nine = 1.0 / 9.0;
  const float nine_ptr[] = {nine, nine};

  const int input_batch_stride = output_channels * input_channel_stride;
  const int output_batch_stride = output_channels * output_channel_stride;
  for (int i = 0; i < batch_size; ++i) {
W
wangliu 已提交
829
#pragma omp parallel for
W
wangliu 已提交
830
    for (int c = 0; c < output_channels; ++c) {
W
wangliu 已提交
831 832
      const float *input_seg = input_data + c * input_channel_stride;
      float *output_seg = output_data + c * output_channel_stride;
W
wangliu 已提交
833 834
      for (int ph = 0; ph < output_height; ph++) {
        for (int pw = 0; pw < output_width; pw++) {
W
wangliu 已提交
835 836 837 838
          int hstart = ph * stride - padding;
          int wstart = pw * stride - padding;
          int hend = min(hstart + 3, input_height + padding);
          int wend = min(wstart + 3, input_width + padding);
W
wangliu 已提交
839 840 841 842
          hstart = max(hstart, 0);
          wstart = max(wstart, 0);
          hend = min(hend, input_height);
          wend = min(wend, input_width);
843

W
wangliu 已提交
844 845 846 847
          const float *pos1 = input_seg + hstart * input_width + wstart;
          const float *pos2 = input_seg + (hstart + 1) * input_width + wstart;
          const float *pos3 = input_seg + (hstart + 2) * input_width + wstart;
          float *output_ptr = output_seg + ph * output_width + pw;
W
wangliu 已提交
848 849 850 851 852

          if (hend - hstart != 3 || wend - wstart != 3) {
            float sum = 0;
            for (int h = hstart; h < hend; h++) {
              for (int w = wstart; w < wend; w++) {
W
wangliu 已提交
853
                sum += input_seg[h * input_width + w];
W
wangliu 已提交
854 855
              }
            }
856 857
            output_seg[ph * output_width + pw] =
                sum / ((hend - hstart) * (wend - wstart) * 1.0);
W
wangliu 已提交
858
          } else {
859 860
#if __aarch64__
#else
W
wangliu 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873
            asm volatile(
                "vld1.32  {q1}, [%[pos1]]        \n\t"
                "vld1.32  {q2}, [%[pos2]]        \n\t"
                "vld1.32  {q3}, [%[pos3]]        \n\t"
                "vadd.f32 q1, q1, q2            \n\t"
                "vadd.f32 q2, q1, q3            \n\t"
                "vmov.f32 d5[1],  %[zero]         \n\t"
                "vpadd.f32  d6, d4, d5            \n\t"
                "vpadd.f32  d6, d6, d6             \n\t"
                "vld1.f32 d7, [%[nine_ptr]]!        \n\t"
                "vmul.f32 d6,d7                     \n\t"
                "vst1.32 {d6[0]},[%[output_ptr]]    \n\t"
                :
W
wangliu 已提交
874
                : [input_seg] "r"(input_seg), [pos1] "r"(pos1),
W
wangliu 已提交
875 876 877 878
                  [pos2] "r"(pos2), [pos3] "r"(pos3),
                  [output_ptr] "r"(output_ptr), [zero] "r"(zero),
                  [nine_ptr] "r"(nine_ptr)
                : "memory", "r6", "q1", "q2", "q3", "q4");
879
#endif
W
wangliu 已提交
880 881 882 883 884 885 886 887 888
            const float32x4_t data1 = vld1q_f32(pos1);
            const float32x4_t data2 = vld1q_f32(pos2);
            const float32x4_t data3 = vld1q_f32(pos3);
            const float32x4_t sum_data =
                vaddq_f32(vaddq_f32(data1, data3), data2);
            float32x2_t res =
                vpadd_f32(vget_high_f32(vsetq_lane_f32(0, sum_data, 3)),
                          vget_low_f32(sum_data));
            res = vpadd_f32(res, res);
W
wangliu 已提交
889
            output_seg[ph * output_width + pw] = vget_lane_f32(res, 0) / 9.0;
W
wangliu 已提交
890 891 892 893 894 895 896
          }
        }
      }
    }
    input_data += input_batch_stride;
    output_data += output_batch_stride;
  }
897
#else
W
wangliu 已提交
898 899 900 901 902 903 904
#endif
}
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile

#endif