reshape2_kernel.cpp 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* 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"
#include "framework/ddim.h"

namespace paddle_mobile {
namespace operators {

template <>
bool Reshape2Kernel<FPGA, float>::Init(Reshape2Param<FPGA> *param) {
  auto input = const_cast<LoDTensor *>(param->InputX());
  auto output = param->Out();
  auto shape = param->Shape();
28
  output->scale[0] = input->scale[0];
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  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));
  output->set_type(input->type());
  fpga::format_ofm(output);
  DLOG << "input: " << input;
  DLOG << "output: " << output;

  return true;
}

void reshape(LoDTensor *input, LoDTensor *output) {
  // Subscript r means after reshape

52 53
  auto input_ptr = input->data<int8_t>();
  auto output_ptr = output->data<int8_t>();
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  output->scale[0] = input->scale[0];
  output->scale[1] = input->scale[1];

  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;

71
  fpga::fpga_invalidate(input_ptr, H * WC_align * sizeof(int8_t));
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  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;
        offset_r = c * HW + h * W + w;
        cr = offset_r / HWr;
        hr = offset_r % HWr / Wr;
        wr = offset_r % Wr;
        offset_align_r = hr * WCr_align + wr * Cr + cr;
        output_ptr[offset_align_r] = input_ptr[offset_align];
      }
    }
  }

93
  fpga::fpga_flush(output_ptr, Hr * WCr_align * sizeof(int8_t));
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

template <>
void Reshape2Kernel<FPGA, float>::Compute(const Reshape2Param<FPGA> &param) {
  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));
113 114

  bool reshapeNeedFlg = 1;
qnqinan's avatar
qnqinan 已提交
115
  if (output->dims() == input->dims()) {
116 117 118 119 120
    reshapeNeedFlg = 0;
  } else if (output->dims().size() != input->dims().size()) {
    auto inputdimsize = input->dims().size();
    auto outputdimsize = output->dims().size();
    int smallersize =
121
        inputdimsize > outputdimsize ? outputdimsize : inputdimsize;
122 123
    int i = 0;
    for (i = 0; i < smallersize; i++) {
124
      if ((input->dims())[i] != (output->dims())[i]) break;
125 126 127 128 129 130 131 132
    }
    if (i == smallersize) {
      reshapeNeedFlg = 0;
    }
  }
  if (reshapeNeedFlg) {
    reshape(input, output);
  } else {
133 134 135 136
    DLOG << "No need to reshape";
    output->ShareDataWith(*input);
    framework::LoD lod = input->lod();
    output->set_lod(lod);
137
    output->scale[0] = input->scale[0];
138 139 140 141 142 143 144 145
    return;
  }
}

}  // namespace operators
}  // namespace paddle_mobile

#endif