concat_kernel.cpp 2.7 KB
Newer Older
H
hanbuhe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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 CONCAT_OP

#include "operators/kernel/concat_kernel.h"

namespace paddle_mobile {
namespace operators {

template <>
N
nhzlx 已提交
23
bool ConcatKernel<FPGA, float>::Init(ConcatParam<FPGA> *param) {
Z
zhangyang 已提交
24 25 26
  auto inputs = param->Inputs();
  auto out = param->Out();
  auto image_num = inputs.size();
27
  auto images_in =
28
      (int8_t **)fpga::fpga_malloc(image_num * sizeof(int8_t *));  // NOLINT
29 30
  auto scales_in =
      (float **)fpga::fpga_malloc(image_num * sizeof(float *));  // NOLINT
Z
zhangyang 已提交
31
  auto channel_num =
32
      (uint32_t *)fpga::fpga_malloc(image_num * sizeof(uint32_t));  // NOLINT
Z
zhangyang 已提交
33 34 35 36 37 38 39 40

  auto height = inputs[0]->dims()[2];
  auto width = inputs[0]->dims()[3];
  for (int i = 0; i < image_num; i++) {
    auto input = inputs[i];
    PADDLE_MOBILE_ENFORCE(
        input->dims()[2] == height && input->dims()[3] == width,
        "Image height & width should be unified");
41
    images_in[i] = input->data<int8_t>();
42
    channel_num[i] = (uint32_t)inputs[i]->dims()[1];  // NOLINT
Z
zhangyang 已提交
43 44
    scales_in[i] = input->scale;
  }
45
  fpga::format_concat_output(out, height, width, image_num, channel_num);
Z
zhangyang 已提交
46

47
  fpga::ConcatArgs concatArgs = {0};
48
  concatArgs.image_num = image_num;
Z
zhangyang 已提交
49 50
  concatArgs.images_in = images_in;
  concatArgs.scales_in = scales_in;
51
  concatArgs.image_out = out->data<int8_t>();
Z
zhangyang 已提交
52 53
  concatArgs.scale_out = out->scale;
  concatArgs.channel_num = channel_num;
54 55
  concatArgs.height = height;
  concatArgs.width = width;
56 57 58 59 60 61 62 63 64

  auto deleter = [](void *p) { fpga::fpga_free(p); };
  concatArgs.vector_concat_space.push_back(std::shared_ptr<char>(
      reinterpret_cast<char *>(concatArgs.images_in), deleter));
  concatArgs.vector_concat_space.push_back(std::shared_ptr<char>(
      reinterpret_cast<char *>(concatArgs.scales_in), deleter));
  concatArgs.vector_concat_space.push_back(std::shared_ptr<char>(
      reinterpret_cast<char *>(concatArgs.channel_num), deleter));

Z
zhangyang 已提交
65
  param->SetFpgaArgs(concatArgs);
H
hanbuhe 已提交
66 67 68 69
  return true;
}

template <>
L
liuruilong 已提交
70
void ConcatKernel<FPGA, float>::Compute(const ConcatParam<FPGA> &param) {
71
  ComputeFPGAConcat(param.FpgaArgs());
H
hanbuhe 已提交
72
}
H
hanbuhe 已提交
73
template class ConcatKernel<FPGA, float>;
H
hanbuhe 已提交
74 75 76 77 78

}  // namespace operators
}  // namespace paddle_mobile

#endif