depthwise_conv_pe.hpp 4.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 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
    return true;
  }

  void apply() {
    DepthwiseConvParam& param = param_;
    Tensor* input = param.input;
    Tensor* output = param.output;
    int channel = output->shape().channel();
C
chonwhite 已提交
39

Y
Yan Chunwei 已提交
40
    float16* b_data = bias_.mutableData<float16>(FP16, param_.bias()->shape());
41 42
    if (param_.bias()->dataType() == FP32) {
      float* new_bias_data = param_.bias()->data<float>();
C
chonwhite 已提交
43
      // bias从float转换成float16
44 45 46 47 48 49 50 51
      for (int i = 0; i < channel; i++) {
        b_data[i] = float_to_half(new_bias_data[i]);
      }
      bias_.flush();
    } else {
      float16* new_bias_data = param_.bias()->data<float16>();
      memcpy(b_data, new_bias_data, channel * sizeof(float16));
      bias_.flush();
C
Chon 已提交
52 53
    }

54 55 56 57 58
    if (param_.scale()->dataType() == FP32) {
      float* new_scale_data = param_.scale()->data<float>();
      Tensor* quantized_filter = param.quantizedFilter();
      quantized_filter->mutableData<float16>(FP16, param.filter->shape());
      format_dw_filter(param.filter, param.quantizedFilter(), new_scale_data);
C
chonwhite 已提交
59

60
    } else {
C
chonwhite 已提交
61
      // filter 全为1时,且channal为对齐时
62
      float16* scale_data = param_.scale()->data<float16>();
C
chonwhite 已提交
63 64
      float16* filter_data = param.quantizedFilter()->mutableData<float16>(
          FP16, param.filter->shape());
65
      // memcpy(filter_data, scale_data, channel * sizeof(float16));
C
chonwhite 已提交
66 67 68
      memcpy(filter_data,
             scale_data,
             param.filter->shape().numel() * sizeof(float16));
69 70
      param.quantizedFilter()->flush();
    }
C
Chon 已提交
71 72

    DWconvArgs args = {0};
Y
Yan Chunwei 已提交
73
    args.bias_address = b_data;
C
Chon 已提交
74
    args.filter_address = param.quantizedFilter()->data<void>();
Y
Yan Chunwei 已提交
75 76
    args.kernel.width = param.filter->shape().height();
    args.kernel.height = param.filter->shape().width();
C
Chon 已提交
77 78 79 80 81 82
    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();
83 84
    args.image.pad_width = param.paddings[0];
    args.image.pad_height = param.paddings[1];
C
Chon 已提交
85 86 87 88 89 90 91
    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;
    param.args = args;
Y
Yan Chunwei 已提交
92 93 94 95

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

Y
Yan Chunwei 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
  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 已提交
111 112 113 114 115

  DepthwiseConvParam& param() { return param_; }

 private:
  DepthwiseConvParam param_;
Y
Yan Chunwei 已提交
116 117
  Tensor bias_;
  InplaceArgs inplace_ = {0};
C
Chon 已提交
118 119 120
};

}  // namespace zynqmp
C
chonwhite 已提交
121
}  // namespace paddle