pool_2x2.cpp 6.6 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* 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
#include "pool_2x2.h"

namespace paddle_mobile {
namespace operators {
namespace math {

void Pool2x2Max(vector<int> strides, vector<int> paddings, const Tensor *input,
                Tensor *output) {
L
liuruilong 已提交
24
#ifdef __ARM_NEON
25 26 27

#ifdef ARMV7

W
wangliu 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 94 95
  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;

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

  int out_w_num = output_width >> 2;
  const int in_h_num = output_height >> 1;
  const int input_batch_stride = output_channels * input_channel_stride;
  const int output_batch_stride = output_channels * output_channel_stride;
  int remain = output_width - out_w_num << 2;
  for (int i = 0; i < batch_size; ++i) {
    for (int c = 0; c < output_channels; ++c) {
      const float *input_data_chanel_row_next = input_data + input_width;
      for (; output_height > 0; output_height--) {
        if (out_w_num > 0) {
          asm volatile(
              "max_loop:                            \n\t"
              "vld1.f32  {q0,q1},  [%[in_ptr1]]!         \n\t"
              "vld1.f32  {q2,q3},  [%[in_ptr2]]!         \n\t"
              "vmax.f32  q0,  q0,  q2                 \n\t"
              "vmax.f32  q1,  q1,  q3                 \n\t"
              "vpmax.f32  d4,  d0, d1                  \n\t"
              "vpmax.f32  d5,  d2, d3                  \n\t"
              "subs %[out_w_num],  #1                  \n\t"
              "vst1.32  {q2},  [%[out_ptr]]!                 \n\t"
              "bne  max_loop                            \n\t"
              : [in_ptr1] "+r"(input_data),
                [in_ptr2] "+r"(input_data_chanel_row_next),
                [out_ptr] "+r"(output_data), [out_w_num] "+r"(out_w_num)
              :
              : "memory", "q0", "q1", "q2", "q3");
        }

        for (; remain > 0; remain--) {
          float max_row1 = std::max(input_data[0], input_data[1]);
          float max_row2 = std::max(input_data_chanel_row_next[0],
                                    input_data_chanel_row_next[1]);
          *output_data = std::max(max_row1, max_row2);
          input_data += 2;
          input_data_chanel_row_next += 2;
          output_data++;
        }
      }
      input_data += input_channel_stride;
      output_data += output_channel_stride;
    }
    input_data += input_batch_stride;
    output_data += output_batch_stride;
  }
#endif
96 97

#endif
W
wangliu 已提交
98 99 100 101
}

void Pool2x2Avg(vector<int> strides, vector<int> paddings, const Tensor *input,
                Tensor *output) {
L
liuruilong 已提交
102
#ifdef __ARM_NEON
103 104

#ifdef ARMV7
L
liuruilong 已提交
105
  const int batch_size = input->dims()[0];
W
wangliu 已提交
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 133 134 135 136 137 138 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

  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;

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

  int out_w_num = output_width >> 2;
  const int input_batch_stride = output_channels * input_channel_stride;
  const int output_batch_stride = output_channels * output_channel_stride;
  float vqua[] = {0.25f, 0.25f, 0.25f, 0.25f};
  int remain = output_width - out_w_num << 2;
  for (int i = 0; i < batch_size; ++i) {
    for (int c = 0; c < output_channels; ++c) {
      const float *input_data_chanel_row_next = input_data + input_width;
      for (; output_height > 0; output_height--) {
        if (out_w_num > 0) {
          asm volatile(
              "avg_loop:                            \n\t"
              "vld1.32  {q0,q1},  [%[in_ptr1]]!         \n\t"
              "vld1.32  {q2,q3},  [%[in_ptr2]]!         \n\t"
              "vadd.f32  q0,  q0,  q2                 \n\t"
              "vadd.f32  q1,  q1,  q3                 \n\t"
              "vpadd.f32  d4,  d0, d1                  \n\t"
              "vpadd.f32  d5,  d2, d3                  \n\t"
              "vld1.32  {q4}, [%[vqua]]!                  \n\t"
              "vmul.f32  q2,  q2,  q4                          \n\t"
              "subs %[out_w_num],  #1                  \n\t"
              "vst1.32  {q2},  [%[out_ptr]]!                 \n\t"
              "bne  avg_loop                            \n\t"
              : [in_ptr1] "+r"(input_data),
                [in_ptr2] "+r"(input_data_chanel_row_next),
                [out_ptr] "+r"(output_data), [out_w_num] "+r"(out_w_num)
              : [vqua] "r"(vqua)
              : "memory", "q0", "q1", "q2", "q3", "q4");
        }

        for (; remain > 0; remain--) {
          float max_row1 = std::max(input_data[0], input_data[1]);
          float max_row2 = std::max(input_data_chanel_row_next[0],
                                    input_data_chanel_row_next[1]);
          *output_data = std::max(max_row1, max_row2);
          input_data += 2;
          input_data_chanel_row_next += 2;
          output_data++;
        }
      }
      input_data += input_channel_stride;
      output_data += output_channel_stride;
    }
    input_data += input_batch_stride;
    output_data += output_batch_stride;
  }
174 175
#else

L
liuruilong 已提交
176
// TODO(): to imp other asm
177 178 179

#endif

W
wangliu 已提交
180 181 182 183 184 185 186 187 188 189
#endif
}

//}
}  // namespace math

}  // namespace operators
}  // namespace paddle_mobile

#endif