reshape2_kernel.cpp 3.8 KB
Newer Older
Z
zhangyang0701 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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 RESHAPE2_OP

#include "operators/kernel/reshape2_kernel.h"
18
#include "framework/ddim.h"
Z
zhangyang0701 已提交
19 20 21 22 23 24

namespace paddle_mobile {
namespace operators {

template <>
bool Reshape2Kernel<FPGA, float>::Init(Reshape2Param<FPGA> *param) {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  auto input = const_cast<LoDTensor *>(param->InputX());
  auto output = param->Out();
  auto shape = param->Shape();

  auto num_in = framework::product(input->dims());
  auto num_shape = framework::product(framework::make_ddim(shape));
  PADDLE_MOBILE_ENFORCE(num_shape != 0, "0 index is not supported");

  for (int i = 0; i < shape.size(); i++) {
    if (shape[i] == -1) {
      shape[i] = static_cast<int>(-num_in / num_shape);
      break;
    }
  }
  output->Resize(framework::make_ddim(shape));
Z
zhangyang0701 已提交
40 41
  output->set_type(input->type());
  fpga::format_ofm(output);
42 43 44
  DLOG << "input: " << input;
  DLOG << "output: " << output;

Z
zhangyang0701 已提交
45 46 47
  return true;
}

Z
zhangyang0701 已提交
48 49 50
void reshape(LoDTensor *input, LoDTensor *output) {
  // Subscript r means after reshape

J
jameswu2014 已提交
51 52 53 54
  auto input_ptr = input->data<half>();
  auto output_ptr = output->data<half>();
  output->scale[0] = input->scale[0];
  output->scale[1] = input->scale[1];
Z
zhangyang0701 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

  auto C = static_cast<int>(input->dims()[1]);
  auto H = static_cast<int>(input->dims()[2]);
  auto W = static_cast<int>(input->dims()[3]);
  auto Cr = static_cast<int>(output->dims()[1]);
  auto Hr = static_cast<int>(output->dims()[2]);
  auto Wr = static_cast<int>(output->dims()[3]);
  PADDLE_MOBILE_ENFORCE(C * H * W == Cr * Hr * Wr, "Dims don't match");
  auto WC = W * C;
  auto WC_align = fpga::align_to_x(WC, IMAGE_ALIGNMENT);
  auto HW = H * W;
  auto WCr = Wr * Cr;
  auto WCr_align = fpga::align_to_x(WCr, IMAGE_ALIGNMENT);
  auto HWr = Hr * Wr;

J
jameswu2014 已提交
70 71
  fpga::fpga_invalidate(input_ptr, H * WC_align * sizeof(half));

Z
zhangyang0701 已提交
72 73 74 75 76 77 78 79 80 81
  int offset_align = 0;
  int offset_r = 0, offset_align_r = 0;
  int cr = 0, hr = 0, wr = 0;

  for (int h = 0; h < H; h++) {
    int offset0 = h * WC_align;
    for (int w = 0; w < W; w++) {
      int offset1 = w * C + offset0;
      for (int c = 0; c < C; c++) {
        offset_align = offset1 + c;
J
jameswu2014 已提交
82
        offset_r = c * HW + h * W + w;
Z
zhangyang0701 已提交
83 84 85 86
        cr = offset_r / HWr;
        hr = offset_r % HWr / Wr;
        wr = offset_r % Wr;
        offset_align_r = hr * WCr_align + wr * Cr + cr;
J
jameswu2014 已提交
87
        output_ptr[offset_align_r] = input_ptr[offset_align];
Z
zhangyang0701 已提交
88 89 90
      }
    }
  }
J
jameswu2014 已提交
91 92

  fpga::fpga_flush(output_ptr, Hr * WCr_align * sizeof(half));
Z
zhangyang0701 已提交
93 94
}

Z
zhangyang0701 已提交
95 96
template <>
void Reshape2Kernel<FPGA, float>::Compute(const Reshape2Param<FPGA> &param) {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  auto input = const_cast<LoDTensor *>(param.InputX());
  auto output = param.Out();
  auto shape = param.Shape();

  auto num_in = framework::product(input->dims());
  auto num_shape = framework::product(framework::make_ddim(shape));
  PADDLE_MOBILE_ENFORCE(num_shape != 0, "0 index is not supported");

  for (int i = 0; i < shape.size(); i++) {
    if (shape[i] == -1) {
      shape[i] = static_cast<int>(-num_in / num_shape);
      break;
    }
  }
  output->Resize(framework::make_ddim(shape));
Z
zhangyang0701 已提交
112 113
  if (output->dims() == input->dims()) {
    DLOG << "No need to reshape";
J
jameswu2014 已提交
114 115 116
    output->ShareDataWith(*input);
    framework::LoD lod = input->lod();
    output->set_lod(lod);
Z
zhangyang0701 已提交
117
    return;
118
  }
Z
zhangyang0701 已提交
119 120

  reshape(input, output);
121
  //
Z
zhangyang0701 已提交
122 123 124 125 126 127
}

}  // namespace operators
}  // namespace paddle_mobile

#endif