fetch_kernel.cpp 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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. */
#include "operators/kernel/fetch_kernel.h"
namespace paddle_mobile {
namespace operators {

template <>
19
bool FetchKernel<FPGA, float>::Init(FetchParam<FPGA> *param) {
20 21 22 23
  auto input = const_cast<LoDTensor *>(param->InputX());
  int col = param->Col();
  DLOG << "col = " << col;
  auto output = &(param->Out()->at(col));
24
  output->init(type_id<float>().hash_code());
25
  output->mutable_data<float>(input->dims());
26

27
  auto aligned_output = param->aligned_out;
28 29 30 31 32 33 34 35 36 37 38
  int outC = 1;
  int outW = 1;
  if (output->dims().size() == 4) {
    outC = output->dims()[1];
    outW = output->dims()[3];
  } else {  // 2
    outC = output->dims()[1];
  }
  int unalignedCW = outC * outW;
  int alignedCW = fpga::align_to_x(unalignedCW, IMAGE_ALIGNMENT);
  if (alignedCW != unalignedCW) {
39 40 41 42
    param->aligned_out = std::make_shared<Tensor>();
    param->aligned_out->Resize(input->dims());
    param->aligned_out->init(type_id<float>().hash_code());
    fpga::format_ofm(param->aligned_out.get());
43
  }
44 45
  return true;
}
46
void dealign(float *src, float *dst, int input_c, int input_h, int input_w) {
47 48
  int alignCW =
      paddle_mobile::fpga::align_to_x(input_c * input_w, IMAGE_ALIGNMENT);
49 50 51 52 53 54 55 56
  int dealignCW = input_c * input_w;
  for (int h = 0; h < input_h; ++h) {
    auto input_offset = h * alignCW;
    auto output_offset = h * dealignCW;
    memcpy((dst + output_offset), (src + input_offset),
           dealignCW * sizeof(float));
  }
}
57
template <>
58
void FetchKernel<FPGA, float>::Compute(const FetchParam<FPGA> &param) {
59 60 61
  auto input = const_cast<LoDTensor *>(param.InputX());
  int col = param.Col();
  auto output = &param.Out()->at(col);
62 63 64 65 66 67 68 69 70 71 72 73 74
  auto outdata_ptr = const_cast<float *>(output->data<float>());
  int outC = 1;
  int outH = 1;
  int outW = 1;
  if (output->dims().size() == 4) {
    outC = output->dims()[1];
    outH = output->dims()[2];
    outW = output->dims()[3];
  } else {  // 2
    outC = output->dims()[1];
  }
  int unalignedCW = outC * outW;
  int alignedCW = fpga::align_to_x(unalignedCW, IMAGE_ALIGNMENT);
75
  if (input->type() == type_id<float>()) {
76
    if ((output->dims().size() != 4) || (unalignedCW == alignedCW)) {
77 78 79 80 81 82 83
      output->ShareDataWith(*input);
    } else {
      auto input_address = input->data<float>();
      dealign(input_address, outdata_ptr, outC, outH, outW);
      fpga::fpga_flush(outdata_ptr, outC * outH * outW * sizeof(float));
    }

84 85
    return;
  }
86 87
  auto input_address = input->data<int8_t>();
  float Si = input->scale[0];
88
  float scale = Si / 127.0f;
89

90
  const int num_th = 32;
91 92
  fpga::fpga_invalidate(input_address, (input->fpga_data_num) * sizeof(int8_t));
  if (input->fpga_data_num < num_th) {
93
    for (int idx = 0; idx < product(input->dims()); ++idx) {
94
      outdata_ptr[idx] = input_address[idx] * scale;
95
    }
96
    fpga::fpga_flush(outdata_ptr, product(input->dims()) * sizeof(float));
97 98
    return;
  }
99

100
  auto aligned_out = param.aligned_out.get();
101
  if (unalignedCW != alignedCW) {
102
    auto aligned_ptr = aligned_out->data<float>();
103
    fpga::fpga_invalidate(aligned_ptr, (input->fpga_data_num) * sizeof(float));
104
    for (int idx = 0; idx < input->fpga_data_num; ++idx) {
105
      aligned_ptr[idx] = input_address[idx] * scale;
106
    }
107
    dealign(aligned_ptr, outdata_ptr, outC, outH, outW);
108
    fpga::fpga_flush(outdata_ptr, outC * outH * outW * sizeof(float));
109
    return;
110
  }
111
  for (int idx = 0; idx < input->fpga_data_num; ++idx) {
112
    outdata_ptr[idx] = input_address[idx] * scale;
113
  }
114
  fpga::fpga_flush(outdata_ptr, outC * outH * outW * sizeof(float));
115
}
116
template class FetchKernel<FPGA, float>;
117 118 119

}  // namespace operators
}  // namespace paddle_mobile