/* 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::Init(Reshape2Param *param) { auto input = const_cast(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(-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 // TODO zhangyang verify this function float *input_ptr_f, *output_ptr_f; half *input_ptr_h, *output_ptr_h; bool is_float = false; if (input->type() == typeid(float)) { input_ptr_f = input->data(); output_ptr_f = output->data(); is_float = true; } else { input_ptr_h = input->data(); output_ptr_h = output->data(); } auto C = static_cast(input->dims()[1]); auto H = static_cast(input->dims()[2]); auto W = static_cast(input->dims()[3]); auto Cr = static_cast(output->dims()[1]); auto Hr = static_cast(output->dims()[2]); auto Wr = static_cast(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; 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 + c; cr = offset_r / HWr; hr = offset_r % HWr / Wr; wr = offset_r % Wr; offset_align_r = hr * WCr_align + wr * Cr + cr; // DLOG << "hwc"<< h<< " " << w << " " << c; // DLOG << "hrwrcr" << hr<< " " << wr << " " << cr; if (is_float) { output_ptr_f[offset_align_r] = input_ptr_f[offset_align]; } else { output_ptr_h[offset_align_r] = input_ptr_h[offset_align]; } } } } } template <> void Reshape2Kernel::Compute(const Reshape2Param ¶m) { auto input = const_cast(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(-num_in / num_shape); break; } } output->Resize(framework::make_ddim(shape)); if (output->dims() == input->dims()) { DLOG << "No need to reshape"; return; } reshape(input, output); // } } // namespace operators } // namespace paddle_mobile #endif