pooling_pe.hpp 6.8 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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. */

#pragma once

#include <algorithm>

19 20
#include "lite/backends/fpga/KD/pe.hpp"
#include "lite/backends/fpga/KD/pe_params.hpp"
Y
Yan Chunwei 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

namespace paddle {
namespace zynqmp {

class PoolingPE : public PE {
 public:
  bool init() {
    Tensor* output = param_.output;
    output->setAligned(true);
    output->setDataLocation(Device);
    return true;
  }

  void apply() {
    Tensor* input = param_.input;
    Tensor* output = param_.output;

38 39
    uint32_t k_height = 1;
    uint32_t k_width = 1;
Y
Yan Chunwei 已提交
40 41 42 43

    if (param_.globalPooling) {
      k_width = input->shape().width();
      k_height = input->shape().height();
T
TianXiaogang 已提交
44 45
      param_.kernelSize[0] = k_height;
      param_.kernelSize[1] = k_width;
46 47 48
    } else {
      k_height = param_.kernelSize[0];
      k_width = param_.kernelSize[1];
Y
Yan Chunwei 已提交
49 50 51 52
    }

    PoolingArgs args = {0};
    args.mode = param_.type;
H
HappyAngel 已提交
53
    auto paddings = *param_.paddings;
Y
Yan Chunwei 已提交
54 55 56 57 58
    args.kernel_reciprocal = fp32_2_fp16(1.0f / (k_width * k_height));
    args.image.address = input->data<float16>();
    args.image.channels = input->shape().channel();
    args.image.height = input->shape().height();
    args.image.width = input->shape().width();
H
HappyAngel 已提交
59 60
    args.image.pad_height = paddings[0];
    args.image.pad_width = paddings[2];
Y
Yan Chunwei 已提交
61 62 63 64 65 66 67 68 69 70 71
    args.image.scale_address = input->scale();
    args.output.address = output->mutableData<float16>();
    args.output.scale_address = output->scale();
    args.kernel.height = k_height;
    args.kernel.width = k_width;
    args.kernel.stride_h = param_.strides[0];
    args.kernel.stride_w = param_.strides[1];
    args.out_height = output->shape().height();
    args.out_width = output->shape().width();
    param_.poolingArgs = args;

72
    // use_cpu_ = output->shape().width() == 1 && output->shape().height() == 1
H
HappyAngel 已提交
73
    // && (k_width > 7 || k_height > 7);
Y
Yan Chunwei 已提交
74
    use_cpu_ = output->shape().width() == 1 && output->shape().height() == 1 &&
75 76
               (k_width > 255 || k_height > 255);
    // use_cpu_ = param_.type == AVERAGE;
Y
Yan Chunwei 已提交
77 78 79 80 81 82 83 84
  }

  void compute() {
    Tensor* input = param_.input;
    Tensor* output = param_.output;
    input->syncToCPU();

    Tensor float_input;
85
    // Tensor float_output;
Y
Yan Chunwei 已提交
86 87 88
    float* image_addr = float_input.mutableData<float>(FP32, input->shape());
    float_input.copyFrom(input);
    float16* data_out = output->data<float16>();
H
HappyAngel 已提交
89
    auto paddings = *param_.paddings;
Y
Yan Chunwei 已提交
90 91 92 93

    int image_height = input->shape().height();
    int image_width = input->shape().width();
    int image_channels = input->shape().channel();
H
HappyAngel 已提交
94 95
    int image_pad_h = paddings[0];
    int image_pad_w = paddings[2];
Y
Yan Chunwei 已提交
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
    int kernel_height = param_.kernelSize[1];
    int kernel_width = param_.kernelSize[0];
    int kernel_step_h = param_.strides[0];
    int kernel_step_w = param_.strides[1];

    int pooled_height_ = output->shape().height();
    int pooled_width_ = output->shape().width();

    int kernel = kernel_height * kernel_width;

    float max = 0;

    for (int ph = 0; ph < pooled_height_; ++ph) {
      for (int pw = 0; pw < pooled_width_; ++pw) {
        int hstart = ph * kernel_step_h - image_pad_h;
        int wstart = pw * kernel_step_w - image_pad_w;
        int hend = std::min(hstart + kernel_height, image_height);
        int wend = std::min(wstart + kernel_width, image_width);
        hstart = std::max(hstart, 0);
        wstart = std::max(wstart, 0);

        kernel = (hend - hstart) * (wend - wstart);
        for (int c = 0; c < image_channels; ++c) {
          const int pool_index = (ph * pooled_width_ + pw) * image_channels + c;
          float sum = 0;
121 122
          // const int index =
          //     (hstart * image_width + wstart) * image_channels + c;
Y
Yan Chunwei 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
          for (int h = hstart; h < hend; ++h) {
            for (int w = wstart; w < wend; ++w) {
              const int index = (h * image_width + w) * image_channels + c;
              float value = image_addr[index];
              sum += value;
            }
          }
          float value = sum / kernel;
          if (value > max) {
            max = value;
          }
          data_out[pool_index] = float_to_half(value);
        }
      }
    }
    output->scale()[0] = max / 127.0f;
    output->scale()[1] = 127.0f / max;
    output->flush();
  }

T
TianXiaogang 已提交
143
  void cpu_compute1() {
Y
Yan Chunwei 已提交
144 145 146 147 148 149 150
    Tensor* input = param_.input;
    Tensor* output = param_.output;
    input->syncToCPU();

    Tensor float_input;
    float_input.mutableData<float>(FP32, input->shape());
    float_input.copyFrom(input);
151
    // float_input.saveToFile("pool_float.txt");
Y
Yan Chunwei 已提交
152
    float16* data_out = output->data<float16>();
153

Y
Yan Chunwei 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    int kernel_hw = param_.kernelSize[0] * param_.kernelSize[1];

    float scale_max = 0;
    for (int i = 0; i < output->shape().channel(); i++) {
      float sum = 0;
      for (int j = 0; j < kernel_hw; j++) {
        float value = half_to_float(input->data<float16>()[i * kernel_hw + j]);
        sum += value;
      }
      float value = sum / kernel_hw;
      data_out[i] = float_to_half(value);
      scale_max = std::max(scale_max, std::abs(value));
    }
    output->scale()[0] = scale_max / 127.0f;
    output->scale()[1] = 127.0f / scale_max;
T
TianXiaogang 已提交
169
    output->flush();
170
    // exit(-1);
T
TianXiaogang 已提交
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
  }

  void cpu_compute() {
    Tensor* input = param_.input;
    Tensor* output = param_.output;
    input->syncToCPU();

    Tensor float_input;
    float* float_input_data =
        float_input.mutableData<float>(FP32, input->shape());
    float_input.copyFrom(input);

    float16* data_out = output->data<float16>();

    int kernel_hw = param_.kernelSize[0] * param_.kernelSize[1];

    float scale_max = 0;
    for (int i = 0; i < output->shape().channel(); i++) {
      float sum = 0;
      for (int j = 0; j < kernel_hw; j++) {
        sum += float_input_data[i * kernel_hw + j];
      }
      float value = sum / kernel_hw;
      data_out[i] = float_to_half(value);
      scale_max = std::max(scale_max, std::abs(value));
    }
    output->scale()[0] = scale_max / 127.0f;
    output->scale()[1] = 127.0f / scale_max;
Y
Yan Chunwei 已提交
199
    output->flush();
200
    // exit(-1);
Y
Yan Chunwei 已提交
201 202 203 204
  }

  bool dispatch() {
    if (use_cpu_) {
205
      // cpu_compute();
Y
Yan Chunwei 已提交
206
      compute();
207
      // exit(-1);
Y
Yan Chunwei 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      return true;
    }
    param_.input->syncToDevice();
    return compute_fpga_pool(param_.poolingArgs) == 0;
  }

  PoolingParam& param() { return param_; }

 private:
  PoolingParam param_;
  bool use_cpu_;
};

}  // namespace zynqmp
}  // namespace paddle