elementwise_add_pe.hpp 3.0 KB
Newer Older
C
Chon 已提交
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"
C
Chon 已提交
19

Y
Yan Chunwei 已提交
20
namespace paddle {
C
Chon 已提交
21 22 23 24
namespace zynqmp {

class ElementwiseAddPE : public PE {
 public:
Y
Yan Chunwei 已提交
25 26 27 28 29 30
  bool init() {
    Tensor* output = param_.output;
    output->setAligned(true);
    output->setDataLocation(Device);
    return true;
  }
C
Chon 已提交
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

  void apply() {
    Tensor* input0 = param_.inputs[0];
    Tensor* input1 = param_.inputs[1];
    Tensor* output = param_.output;
    EWAddArgs args = {0};
    args.const0 = 0x3c00;
    args.const1 = 0x3c00;  // =1
    args.image0.address = input0->data<float16>();
    args.image0.channels = input0->shape().channel();
    args.image0.scale_address = input0->scale();
    args.image0.height = input0->shape().height();
    args.image0.width = input0->shape().width();
    args.image0.pad_height = 0;
    args.image0.pad_width = 0;
    args.image1.address = input1->data<float16>();
    args.image1.channels = input1->shape().channel();
    args.image1.scale_address = input1->scale();
    args.image1.height = input1->shape().height();
    args.image1.width = input1->shape().width();
    args.image1.pad_height = 0;
    args.image1.pad_width = 0;
    args.output.scale_address = output->scale();
    args.output.address = output->data<float16>();
    param_.ewargs = args;
  }

  bool dispatch() {
Y
Yan Chunwei 已提交
59 60
    param_.inputs[0]->syncToDevice();
    param_.inputs[1]->syncToDevice();
C
chonwhite 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    // InplaceArgs inplace_ = {0};

    if (param_.activeParam.type == TYPE_RELU) {
      inplace_.relu_enable = true;
    } else if (param_.activeParam.type == TYPE_RELU6) {
      inplace_.relu6_enable = true;
    } else if (param_.activeParam.type == TYPE_SIGMOID) {
      inplace_.sigmoid_enable = true;
    } else if (param_.activeParam.type == TYPE_LEAKY_RELU) {
      inplace_.leaky_relu_enable = true;
    }
    if (inplace_.relu_enable || inplace_.leaky_relu_enable ||
        inplace_.relu6_enable || inplace_.sigmoid_enable) {
      config_inplace(inplace_);
C
Chon 已提交
75 76
    }
    compute_fpga_ewadd(param_.ewargs);
C
chonwhite 已提交
77 78 79 80 81 82 83
    if (inplace_.relu_enable || inplace_.leaky_relu_enable ||
        inplace_.relu6_enable || inplace_.sigmoid_enable) {
      inplace_.relu_enable = false;
      inplace_.relu6_enable = false;
      inplace_.sigmoid_enable = false;
      inplace_.leaky_relu_enable = false;
      config_inplace(inplace_);
C
Chon 已提交
84 85 86 87 88 89 90 91
    }
    return true;
  }

  ElementwiseAddParam& param() { return param_; }

 private:
  ElementwiseAddParam param_;
C
chonwhite 已提交
92
  InplaceArgs inplace_ = {0};
C
Chon 已提交
93 94 95
};

}  // namespace zynqmp
Y
Yan Chunwei 已提交
96
}  // namespace paddle