split_kernel.cpp 2.6 KB
Newer Older
Z
zhangyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

Z
zhangyang 已提交
15
#ifdef SPLIT_OP
Z
zhangyang 已提交
16

Z
zhangyang 已提交
17
#include "operators/kernel/split_kernel.h"
Z
zhangyang 已提交
18 19 20 21

namespace paddle_mobile {
namespace operators {
template <>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
bool SplitKernel<FPGA, float>::Init(SplitParam<FPGA> *param) {
  auto *in = const_cast<Tensor *>(param->InputX());
  auto outs = param->Outs();
  auto sections = param->Sections();
  int axis = param->Axis();
  PADDLE_MOBILE_ENFORCE(axis == 1, "Only support split in channel dimension");
  PADDLE_MOBILE_ENFORCE(outs.size() == sections.size(),
                        "Output number should be equal to section number");
  auto image_num = (uint32_t)outs.size();
  auto images_out =
      reinterpret_cast<void **>(fpga::fpga_malloc(image_num * sizeof(void *)));
  auto scales_out = reinterpret_cast<float **>(
      fpga::fpga_malloc(image_num * sizeof(float *)));
  auto out_channels = reinterpret_cast<uint32_t *>(
      fpga::fpga_malloc(image_num * sizeof(uint32_t)));
37
  DLOG << "input: " << in;
38 39
  for (int i = 0; i < image_num; i++) {
    fpga::format_fp16_ofm(outs[i]);
40 41
    DLOG << "output: " << outs[i];
    images_out[i] = outs[i]->mutable_data<half>();
42 43 44 45
    scales_out[i] = outs[i]->scale;
    out_channels[i] = (uint32_t)sections[i];
  }

46 47
  auto deleter = [](void *p) { fpga::fpga_free(p); };

48 49
  fpga::SplitArgs arg = {0};
  arg.image_num = image_num;
50
  arg.image_in = in->data<half>();
51 52 53 54 55 56
  arg.scale_in = in->scale;
  arg.images_out = images_out;
  arg.scales_out = scales_out;
  arg.out_channel_nums = out_channels;
  arg.height = (uint32_t)in->dims()[2];
  arg.width = (uint32_t)in->dims()[3];
57 58 59 60 61 62
  arg.vector_split_space.push_back(
      std::shared_ptr<char>(reinterpret_cast<char *>(images_out), deleter));
  arg.vector_split_space.push_back(
      std::shared_ptr<char>(reinterpret_cast<char *>(scales_out), deleter));
  arg.vector_split_space.push_back(
      std::shared_ptr<char>(reinterpret_cast<char *>(out_channels), deleter));
63 64

  param->SetFpgaArgs(arg);
Z
zhangyang 已提交
65 66 67
  return true;
}
template <>
68 69 70
void SplitKernel<FPGA, float>::Compute(const SplitParam<FPGA> &param) {
  fpga::ComputeFPGASplit(param.FpgaArgs());
}
Z
zhangyang 已提交
71 72 73 74

}  // namespace operators
}  // namespace paddle_mobile
#endif