depthwise_conv_pe.hpp 3.5 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 19 20
#include "lite/backends/fpga/KD/float16.hpp"
#include "lite/backends/fpga/KD/pe.hpp"
#include "lite/backends/fpga/KD/pe_params.hpp"
#include "lite/backends/fpga/KD/pes/conv_process.hpp"
C
Chon 已提交
21

Y
Yan Chunwei 已提交
22
namespace paddle {
C
Chon 已提交
23 24 25 26 27
namespace zynqmp {

class DepthwiseConvPE : public PE {
 public:
  bool init() {
Y
Yan Chunwei 已提交
28 29 30
    Tensor* output = param_.output;
    output->setAligned(true);
    output->setDataLocation(Device);
C
Chon 已提交
31 32 33 34 35 36 37 38 39
    return true;
  }

  void apply() {
    DepthwiseConvParam& param = param_;
    Tensor* input = param.input;
    Tensor* output = param.output;
    int channel = output->shape().channel();

Y
Yan Chunwei 已提交
40 41 42 43 44 45
    float* new_scale_data = param_.scale()->data<float>();
    float* new_bias_data = param_.bias()->data<float>();

    float16* b_data = bias_.mutableData<float16>(FP16, param_.bias()->shape());
    for (int i = 0; i < channel; i++) {
      b_data[i] = float_to_half(new_bias_data[i]);
C
Chon 已提交
46
    }
Y
Yan Chunwei 已提交
47
    bias_.flush();
C
Chon 已提交
48 49 50 51 52 53

    Tensor* quantized_filter = param.quantizedFilter();
    quantized_filter->mutableData<float16>(FP16, param.filter->shape());
    format_dw_filter(param.filter, param.quantizedFilter(), new_scale_data);

    DWconvArgs args = {0};
Y
Yan Chunwei 已提交
54
    args.bias_address = b_data;
C
Chon 已提交
55
    args.filter_address = param.quantizedFilter()->data<void>();
Y
Yan Chunwei 已提交
56 57
    args.kernel.width = param.filter->shape().height();
    args.kernel.height = param.filter->shape().width();
C
Chon 已提交
58 59 60 61 62 63
    args.kernel.stride_w = param.strides[0];
    args.kernel.stride_h = param.strides[1];
    args.image.address = input->data<void>();
    args.image.channels = input->shape().channel();
    args.image.height = input->shape().height();
    args.image.width = input->shape().width();
H
HappyAngel 已提交
64 65 66
    auto paddings = *param.paddings;
    args.image.pad_width = param.paddings[2];
    args.image.pad_height = param.paddings[0];
C
Chon 已提交
67 68 69 70 71 72
    args.image.scale_address = input->scale();
    args.output.address = output->data<void>();
    args.output.scale_address = output->scale();
    args.out_width = param.output->shape().width();
    args.out_height = param.output->shape().height();
    args.sub_conv_num = 1;
H
HappyAngel 已提交
73 74 75 76 77 78
    bool pad_equal =
        ((paddings[0] == paddings[1]) && (paddings[2] == paddings[3]));
    if (!pad_equal) {
      LOG(FATA) << "This pad not support ! " << paddings[0] << ", "
                << paddings[1] << ", " << paddings[2] << ", " << paddings[3];
    }
C
Chon 已提交
79
    param.args = args;
Y
Yan Chunwei 已提交
80 81 82 83

    inplace_.relu_enable = param_.relu.enabled;
    inplace_.power_enable = false;
    inplace_.normalize_enable = false;
C
Chon 已提交
84 85
  }

Y
Yan Chunwei 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
  bool dispatch() {
    param_.input->syncToDevice();
    if (param_.relu.enabled) {
      inplace_.relu_enable = param_.relu.enabled;
      config_inplace(inplace_);
    }
    bool ret = compute_fpga_dwconv(param_.args) == 0;
    if (param_.relu.enabled) {
      inplace_.relu_enable = false;
      config_inplace(inplace_);
    }
    return ret;
  }
C
Chon 已提交
99 100 101 102 103

  DepthwiseConvParam& param() { return param_; }

 private:
  DepthwiseConvParam param_;
Y
Yan Chunwei 已提交
104 105
  Tensor bias_;
  InplaceArgs inplace_ = {0};
C
Chon 已提交
106 107 108
};

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