pool_2x2.cpp 10.4 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
16 17 18
#include "operators/math/pool_2x2.h"
#include <algorithm>
#include <vector>
W
wangliu 已提交
19 20 21 22

namespace paddle_mobile {
namespace operators {
namespace math {
23
#define FLT_MAX __FLT_MAX__
W
wangliu 已提交
24

25 26
void Pool2x2Maxs2p0(vector<int> strides, vector<int> paddings,
                    const Tensor *input, Tensor *output) {
W
wangliu 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  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];
  int output_height = output->dims()[2];
  const int output_width = output->dims()[3];
  const int ksize_height = 2;
  const int ksize_width = 2;
  const int stride_height = strides[0];
  const int stride_width = strides[1];
  const int padding_height = paddings[0];
  const int padding_width = paddings[1];

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

44 45 46
  const int input_batch_stride = output_channels * input_channel_stride;
  const int output_batch_stride = output_channels * output_channel_stride;

W
wangliu 已提交
47 48 49
  const float *input_data = input->data<float>();
  float *output_data = output->mutable_data<float>();

50 51 52 53 54
  int w1 = input_width / 16;
  int _w1 = input_width % 16;
  int w2 = _w1 / 4;
  int _w2 = _w1 % 4;

W
wangliu 已提交
55 56
  for (int i = 0; i < batch_size; ++i) {
    for (int c = 0; c < output_channels; ++c) {
57 58 59 60 61 62 63 64 65
      for (int ph = 0; ph < input_height; ph += 2) {
        const float *in_ptr1 = input_data + i * input_batch_stride +
                               c * input_channel_stride + ph * input_width;
        const float *in_ptr2 = in_ptr1 + input_width;
        if (ph + 1 >= input_height) {
          in_ptr2 = static_cast<float *>(
              paddle_mobile::memory::Alloc(sizeof(float) * input_width));
          memset(static_cast<void *>(const_cast<float *>(in_ptr2)), -FLT_MAX,
                 sizeof(float) * input_width);
W
wangliu 已提交
66
        }
67 68
        float *out_ptr = output_data + i * output_batch_stride +
                         c * output_channel_stride + ph / 2 * output_width;
L
liuruilong 已提交
69 70 71
#if __ARM_NEON
#if __aarch64__
#else
L
liuruilong 已提交
72
        asm volatile(
73 74 75 76 77 78 79 80 81 82 83
            "subs       %[w1], %[w1], #1        \n\t"
            "blt        end_w1_%=               \n\t"
            "loop_w1_%=:                        \n\t"

            "pld        [%[in_ptr1], #64]       \n\t"
            "pld        [%[in_ptr2], #64]       \n\t"

            "vld1.f32   {q0, q1},   [%[in_ptr1]]!   \n\t"
            "vld1.f32   {q2, q3},   [%[in_ptr2]]!   \n\t"
            "vld1.f32   {q6, q7},   [%[in_ptr1]]!   \n\t"
            "vld1.f32   {q8, q9},   [%[in_ptr2]]!   \n\t"
W
wangliu 已提交
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
            "vmax.f32   q0,     q0,   q2        \n\t"
            "vmax.f32   q1,     q1,   q3        \n\t"

            "vmax.f32   q6,     q6,   q8        \n\t"
            "vmax.f32   q7,     q7,   q9        \n\t"

            "vpmax.f32  d8,     d0,   d1        \n\t"
            "vpmax.f32  d9,     d2,   d3        \n\t"

            "vpmax.f32  d10,    d12,  d13       \n\t"
            "vpmax.f32  d11,    d14,  d15       \n\t"

            "vst1.32  {q4, q5},  [%[out_ptr]]!  \n\t"

            "subs       %[w1], %[w1], #1        \n\t"
            "bge        loop_w1_%=              \n\t"
            "end_w1_%=:                         \n\t"

            "subs       %[w2], %[w2], #1        \n\t"
            "blt        end_w2_%=               \n\t"
            "loop_w2_%=:                        \n\t"

            "vld1.f32   {q0},   [%[in_ptr1]]!   \n\t"
            "vld1.f32   {q1},   [%[in_ptr2]]!   \n\t"
            "vmax.f32   q0,     q0,   q1        \n\t"
            "vpmax.f32  d4,     d0,   d1        \n\t"
            "vst1.32    {d4},   [%[out_ptr]]!   \n\t"

            "subs       %[w2], %[w2], #1        \n\t"
            "bge        loop_w2_%=              \n\t"
            "end_w2_%=:                         \n\t"
            :
            : [w1] "r"(w1), [w2] "r"(w2), [in_ptr1] "r"(in_ptr1),
              [in_ptr2] "r"(in_ptr2), [out_ptr] "r"(out_ptr)
            : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8",
              "q9");
L
liuruilong 已提交
121 122
#endif
#endif
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

        if (_w2 != 0) {
          in_ptr1 += 16 * w1 + 4 * w2;
          in_ptr2 += 16 * w1 + 4 * w2;
          out_ptr += 8 * w1 + 2 * w2;
          if (_w2 == 1) {
            *out_ptr = (*in_ptr1 > *in_ptr2) ? *in_ptr1 : *in_ptr2;
          } else if (_w2 == 2) {
            float temp = (*in_ptr1++ > *in_ptr2++) ? *in_ptr1++ : *in_ptr2++;
            float temp1 = (*in_ptr1 > *in_ptr2) ? *in_ptr1 : *in_ptr2;
            *out_ptr = (temp > temp1) ? temp : temp1;
          } else if (_w2 == 3) {
            float temp = (*in_ptr1++ > *in_ptr2++) ? *in_ptr1++ : *in_ptr2++;
            float temp1 = (*in_ptr1++ > *in_ptr2++) ? *in_ptr1++ : *in_ptr2++;
            *out_ptr++ = (temp > temp1) ? temp : temp1;
            *out_ptr = (*in_ptr1 > *in_ptr2) ? *in_ptr1 : *in_ptr2;
          }
W
wangliu 已提交
140 141 142 143 144 145
        }
      }
    }
  }
}

146 147
void Pool2x2Avgs2p0(vector<int> strides, vector<int> paddings,
                    const Tensor *input, Tensor *output) {
L
liuruilong 已提交
148
  const int batch_size = input->dims()[0];
W
wangliu 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  const int input_height = input->dims()[2];
  const int input_width = input->dims()[3];

  const int output_channels = output->dims()[1];
  int output_height = output->dims()[2];
  const int output_width = output->dims()[3];
  const int ksize_height = 2;
  const int ksize_width = 2;
  const int stride_height = strides[0];
  const int stride_width = strides[1];
  const int padding_height = paddings[0];
  const int padding_width = paddings[1];

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

165 166 167
  const int input_batch_stride = output_channels * input_channel_stride;
  const int output_batch_stride = output_channels * output_channel_stride;

W
wangliu 已提交
168 169 170
  const float *input_data = input->data<float>();
  float *output_data = output->mutable_data<float>();

171 172 173 174 175 176
  int w1 = input_width / 16;
  int _w1 = input_width % 16;
  int w2 = _w1 / 4;
  int _w2 = _w1 % 4;

  float quarter = 1 / 4;
W
wangliu 已提交
177 178
  for (int i = 0; i < batch_size; ++i) {
    for (int c = 0; c < output_channels; ++c) {
179 180 181 182 183 184 185 186 187
      for (int ph = 0; ph < input_height; ph += 2) {
        const float *in_ptr1 = input_data + i * input_batch_stride +
                               c * input_channel_stride + ph * input_width;
        const float *in_ptr2 = in_ptr1 + input_width;
        if (ph + 1 >= input_height) {
          in_ptr2 = static_cast<float *>(
              paddle_mobile::memory::Alloc(sizeof(float) * input_width));
          memset(static_cast<void *>(const_cast<float *>(in_ptr2)), 0,
                 sizeof(float) * input_width);
W
wangliu 已提交
188
        }
189 190
        float *out_ptr = output_data + i * output_batch_stride +
                         c * output_channel_stride + ph / 2 * output_width;
L
liuruilong 已提交
191 192 193
#if __ARM_NEON
#if __aarch64__
#else
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        asm volatile(
            "subs       %[w1], %[w1], #1        \n\t"
            "blt        end_w1_%=               \n\t"
            "loop_w1_%=:                        \n\t"

            "pld        [%[in_ptr1], #64]       \n\t"
            "pld        [%[in_ptr2], #64]       \n\t"

            "vmov.f32   d0[0],      %[quarter]      \n\t"
            "vld1.f32   {q1, q2},   [%[in_ptr1]]!   \n\t"
            "vld1.f32   {q3, q4},   [%[in_ptr2]]!   \n\t"
            "vld1.f32   {q7, q8},   [%[in_ptr1]]!   \n\t"
            "vld1.f32   {q9, q10},  [%[in_ptr2]]!   \n\t"

            "vadd.f32   q1,     q1,   q3        \n\t"
            "vadd.f32   q2,     q2,   q4        \n\t"
W
wangliu 已提交
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
            "vadd.f32   q7,     q7,   q9        \n\t"
            "vadd.f32   q8,     q8,   q10       \n\t"

            "vpadd.f32  d10,    d2,   d3        \n\t"
            "vpadd.f32  d11,    d4,   d5        \n\t"

            "vpadd.f32  d12,    d14,  d15       \n\t"
            "vpadd.f32  d13,    d16,  d17       \n\t"

            "vmul.f32   q5,     q5,   d0[0]     \n\t"
            "vmul.f32   q6,     q6,   d0[0]     \n\t"

            "vst1.32  {q5, q6},  [%[out_ptr]]!  \n\t"

            "subs       %[w1], %[w1], #1        \n\t"
            "bge        loop_w1_%=              \n\t"
            "end_w1_%=:                         \n\t"

            "subs       %[w2], %[w2], #1        \n\t"
            "blt        end_w2_%=               \n\t"
            "loop_w2_%=:                        \n\t"

            "vld1.f32   {q1},   [%[in_ptr1]]!   \n\t"
            "vld1.f32   {q2},   [%[in_ptr2]]!   \n\t"
            "vadd.f32   q1,     q1,   q2        \n\t"
            "vpadd.f32  d4,     d2,   d3        \n\t"
            "vmul.f32   d4,     d4,   d0[0]     \n\t"
            "vst1.32    {d4},   [%[out_ptr]]!   \n\t"

            "subs       %[w2], %[w2], #1        \n\t"
            "bge        loop_w2_%=              \n\t"
            "end_w2_%=:                         \n\t"
            :
            : [w1] "r"(w1), [w2] "r"(w2), [in_ptr1] "r"(in_ptr1),
              [in_ptr2] "r"(in_ptr2), [out_ptr] "r"(out_ptr),
              [quarter] "r"(quarter)
            : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8",
              "q9", "q10");
L
liuruilong 已提交
249 250
#endif
#endif
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

        if (_w2 != 0) {
          in_ptr1 += 16 * w1 + 4 * w2;
          in_ptr2 += 16 * w1 + 4 * w2;
          out_ptr += 8 * w1 + 2 * w2;
          if (_w2 == 1) {
            *out_ptr = 0.5 * (*in_ptr1 + *in_ptr2);
          } else if (_w2 == 2) {
            float temp = 0;
            temp += *in_ptr1++;
            temp += *in_ptr2++;
            temp += *in_ptr1;
            temp += *in_ptr2;
            *out_ptr = 0.5 * temp;
          } else if (_w2 == 3) {
            float temp = 0;
            temp += *in_ptr1++;
            temp += *in_ptr2++;
            temp += *in_ptr1++;
            temp += *in_ptr2++;
            *out_ptr++ = 0.5 * temp;
            *out_ptr = 0.5 * (*in_ptr1 + *in_ptr2);
          }
W
wangliu 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286
        }
      }
    }
  }
}

//}
}  // namespace math

}  // namespace operators
}  // namespace paddle_mobile

#endif