pool_kernel.cpp 3.6 KB
Newer Older
qnqinan's avatar
qnqinan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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 "operators/kernel/pool_kernel.h"

class PoolingArgs;
namespace paddle_mobile {
20 21 22
namespace operators {

template <>
N
nhzlx 已提交
23
bool PoolKernel<FPGA, float>::Init(PoolParam<FPGA> *param) {
24 25
  auto *input = const_cast<LoDTensor *>(param->Input());
  auto *output = param->Output();
26 27 28
  vector<int> ksize = param->Ksize();
  vector<int> strides = param->Strides();
  vector<int> paddings = param->Paddings();
Z
zhangyang 已提交
29
  std::string pooling_type = param->PoolingType();
qnqinan's avatar
qnqinan 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  if (input->type() == type_id<float>()) {
    int channels = input->dims()[1];
    int height = input->dims()[2];
    int width = input->dims()[3];
    int num = input->dims()[0];
    int out_width = (width + 2 * paddings[1] - ksize[1]) / strides[1] + 1;
    int out_height = (height + 2 * paddings[0] - ksize[0]) / strides[0] + 1;
    framework::DDim dim =
        framework::make_ddim({num, channels, out_height, out_width});
    output->mutable_data<float>(dim);
    return true;
  }

  auto input_ptr = input->data<half>();
  fpga::format_fp16_ofm(output);
  auto output_ptr = output->mutable_data<half>();

Z
zhangyang 已提交
48
  fpga::PoolingArgs poolArgs = {0};
Z
zhangyang 已提交
49 50
  poolArgs.mode = pooling_type == "max" ? 0 : 1;  // max:0, avg:1
  poolArgs.kernel_reciprocal =
Z
zhangyang 已提交
51
      fpga::fp32_2_fp16(float(1.0 / (ksize[0] * ksize[1])));  // NOLINT
Z
zhangyang 已提交
52 53 54 55 56 57
  poolArgs.image.address = input_ptr;
  poolArgs.image.channels = (uint32_t)input->dims()[1];
  poolArgs.image.height = (uint32_t)input->dims()[2];
  poolArgs.image.width = (uint32_t)input->dims()[3];
  poolArgs.image.pad_height = (uint32_t)paddings[0];
  poolArgs.image.pad_width = (uint32_t)paddings[1];
Z
zhangyang 已提交
58
  poolArgs.image.scale_address = input->scale;
59
  poolArgs.output.address = output_ptr;
60
  poolArgs.output.scale_address = output->scale;
Z
zhangyang 已提交
61 62 63 64
  poolArgs.kernel.height = (uint32_t)ksize[0];
  poolArgs.kernel.width = (uint32_t)ksize[1];
  poolArgs.kernel.stride_h = (uint32_t)strides[0];
  poolArgs.kernel.stride_w = (uint32_t)strides[1];
65 66 67 68 69
  param->SetFpgaArgs(poolArgs);
  return true;
}

template <>
L
liuruilong 已提交
70
void PoolKernel<FPGA, float>::Compute(const PoolParam<FPGA> &param) {
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 96 97 98
  auto *input = const_cast<LoDTensor *>(param.Input());

  if (input->type() == type_id<float>()) {
    auto *output = param.Output();
    auto in = input->data<float>();
    auto N = input->dims()[0];
    output->Resize(
        {N, output->dims()[1], output->dims()[2], output->dims()[3]});
    auto len = output->numel();
    auto out = output->mutable_data<float>();
    int C = input->dims()[1], H = input->dims()[2],  // N = input->dims()[0],
        W = input->dims()[3];
    int HW = H * W, CHW = C * H * W, WC = W * C;

    for (int n = 0; n < N; n++) {
      for (int c = 0; c < C; c++) {
        out[n * C + c] = 0;
        for (int h = 0; h < H; h++) {
          for (int w = 0; w < W; w++) {
            out[n * C + c] += in[n * CHW + h * WC + w * C +
                                 c];  // in[n * CHW + c * HW + h * W + w]; //
          }
        }
        out[n * C + c] /= HW;
      }
    }
    return;
  }
99 100 101
  fpga::ComputeFpgaPool(param.FpgaArgs());
}
}  // namespace operators
qnqinan's avatar
qnqinan 已提交
102 103
}  // namespace paddle_mobile

104
#endif