resize_pe.hpp 3.6 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2019 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. */

#pragma once

17 18
#include "lite/backends/fpga/KD/pe.hpp"
#include "lite/backends/fpga/KD/pe_params.hpp"
Y
Yan Chunwei 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

namespace paddle {
namespace zynqmp {
class ResizePE : public PE {
 public:
  bool init() {
    Tensor* output = param_.output;
    output->setAligned(true);
    output->setDataLocation(Device);
    return true;
  }

  void apply() {
    Tensor* input = param_.input;
    Tensor* output = param_.output;
    ResizeArgs& args = args_;

    int input_width = input->shape().width();
    int input_height = input->shape().height();
    int input_channel = input->shape().channel();

    int output_width = output->shape().width();
    int output_height = output->shape().height();

    args.input_width = input_width;
    args.input_height = input_height;
    args.image_channel = input_channel;
    args.output_width = output_width;
    args.output_height = output_height;
    float height_ratio = static_cast<float>(input_height) /
                         static_cast<float>(args.output_height);
    float width_ratio =
        static_cast<float>(input_width) / static_cast<float>(args.output_width);
    args.height_ratio = *reinterpret_cast<uint32_t*>(&height_ratio);
    args.width_ratio = *reinterpret_cast<uint32_t*>(&width_ratio);

    args.input_image_address = input->mutableData<void>();
    args.output_image_address = output->mutableData<void>();
    args.output_scale_address = reinterpret_cast<uint32_t*>(output->scale());
  }

  void compute_scale(Tensor* src, float* scale) {
    float16* data = src->data<float16>();
    src->invalidate();
    float max = 0;
    for (int i = 0; i < src->shape().numel(); i++) {
      float value = half_to_float(data[i]);
      if (value < 0) {
        value = -value;
      }
      if (value > max) {
        max = value;
      }
    }
    scale[0] = max / 127.0;
    scale[1] = 127.0 / max;
  }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  void cpu_compute() {
    Shape& in_shape = param_.input->shape();
    Shape& out_shape = param_.output->shape();
    int channel = in_shape.channel();
    int in_height = in_shape.height();
    int in_width = in_shape.width();
    int out_width = out_shape.width();
    int factor = out_shape.width() / in_shape.width();

    param_.input->syncToCPU();

    for (int h = 0; h < in_height; h++) {
      for (int w = 0; w < in_width; w++) {
        int src_index = in_width * channel * h + w * channel;
        float16* src = param_.input->data<float16>() + src_index;
        for (int v = 0; v < factor; v++) {
          for (int i = 0; i < factor; i++) {
            int dst_index = out_width * channel * h * factor +
                            out_width * channel * v + w * channel * factor +
                            channel * i;
            float16* dst = param_.output->data<float16>() + dst_index;
            memcpy(dst, src, channel * sizeof(float16));
          }
        }
      }
    }
    param_.output->flush();
    param_.output->copyScaleFrom(param_.input);
  }
Y
Yan Chunwei 已提交
105 106

  bool dispatch() {
107
    cpu_compute();
Y
Yan Chunwei 已提交
108 109 110 111 112 113 114 115 116 117 118
    return true;
  }

  ResizeParam& param() { return param_; }

 private:
  ResizeParam param_;
  ResizeArgs args_;
};
}  // namespace zynqmp
}  // namespace paddle