fusion_fc_kernel.cpp 2.6 KB
Newer Older
qnqinan's avatar
qnqinan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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 FUSION_FC_OP

#include "operators/kernel/fusion_fc_kernel.h"
qnqinan's avatar
qnqinan 已提交
17
#include "fpga/fpga_quantilization.h"
qnqinan's avatar
qnqinan 已提交
18 19

namespace paddle_mobile {
20
namespace operators {
qnqinan's avatar
qnqinan 已提交
21

22 23 24 25
template <>
bool FusionFcKernel<FPGA, float>::Init(FusionFcParam *param) {
  bool relu_enabled = false;
  const Tensor *input_x = param->InputX();
26
  auto input_x_ptr = input_x->data<half>();
qnqinan's avatar
qnqinan 已提交
27
  Tensor *input_y = param->InputY();
28 29 30
  const Tensor *input_z = param->InputZ();
  auto input_z_ptr = input_z->data<float>();
  Tensor *out = param->Out();
31
  auto out_ptr = out->mutable_data<half>();
qnqinan's avatar
qnqinan 已提交
32

33 34
  PADDLE_MOBILE_ENFORCE(input_x->dims()[1] == input_y->dims()[0],
                        "Image channel should be equal to weight number");
qnqinan's avatar
qnqinan 已提交
35 36 37 38 39 40 41
  int channel = input_x->dims()[1];
  float *bs_ptr = (float *)fpga::fpga_malloc(2 * channel * sizeof(float));
  for (int i = 0; i < channel; i++) {
    bs_ptr[i * 2] = 1;
    bs_ptr[i * 2 + 1] = input_z_ptr[i];
  }

qnqinan's avatar
qnqinan 已提交
42 43 44
  fpga::quantify_filter(input_y);
  auto input_y_ptr = input_y->data<int8_t>();

45 46 47 48 49
  fpga::ConvArgs convArgs;
  convArgs.relu_enabled = relu_enabled;
  convArgs.filter_address = (void *)input_y_ptr;
  convArgs.filter_num = out->dims()[1];
  convArgs.group_num = 1;
qnqinan's avatar
qnqinan 已提交
50
  convArgs.sb_address = (void *)bs_ptr;
51 52 53 54 55 56 57 58
  convArgs.kernel.stride_w = 1;
  convArgs.kernel.stride_h = 1;
  convArgs.kernel.height = input_x->dims()[2];
  convArgs.kernel.width = input_x->dims()[3];
  convArgs.image.address = (void *)input_x_ptr;
  convArgs.image.channels = input_x->dims()[1];
  convArgs.image.height = input_x->dims()[2];
  convArgs.image.width = input_x->dims()[3];
59 60
  convArgs.image.pad_height = 0;
  convArgs.image.pad_width = 0;
qnqinan's avatar
update  
qnqinan 已提交
61
  convArgs.image.scale_address = input_x->fpga_args().scale_pointer();
62
  convArgs.output.address = (void *)out_ptr;
qnqinan's avatar
update  
qnqinan 已提交
63
  convArgs.output.scale_address = out->fpga_args().scale_pointer();
64 65 66
  param->SetFpgaArgs(convArgs);
  return true;
}
qnqinan's avatar
qnqinan 已提交
67

68 69 70 71 72
template <>
void FusionFcKernel<FPGA, float>::Compute(const FusionFcParam &param) const {
  fpga::ComputeFpgaConv(param.FpgaArgs());
}
}  // namespace operators
qnqinan's avatar
qnqinan 已提交
73 74
}  // namespace paddle_mobile

75
#endif