depthwise_conv_pe.hpp 5.4 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
namespace zynqmp {

class DepthwiseConvPE : public PE {
 public:
27 28 29 30 31 32 33 34 35 36 37
  inline int gcd_(int a, int b) {
    while (b) {
      int temp = a;
      a = b;
      b = temp % b;
    }
    return a;
  }

  inline int lcm_(int a, int b) { return a * b / gcd_(a, b); }

C
Chon 已提交
38
  bool init() {
Y
Yan Chunwei 已提交
39 40 41
    Tensor* output = param_.output;
    output->setAligned(true);
    output->setDataLocation(Device);
C
Chon 已提交
42 43 44 45 46 47 48 49 50
    return true;
  }

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

51 52 53 54 55 56 57 58 59 60 61
    int repeat = 1;
    int alignment = 16;
    int length = channel;

    if (channel % alignment != 0 || channel < alignment) {
      int c_lcm = lcm_(channel, alignment);
      repeat = c_lcm / (channel);
    }
    Shape shape(N, {channel * repeat});

    float16* b_data = bias_.mutableData<float16>(FP16, shape);
T
TianXiaogang 已提交
62 63 64
    if (param_.bias()->dataType() == FP32) {
      float* new_bias_data = param_.bias()->data<float>();
      // bias从float转换成float16
65 66 67 68 69 70 71 72 73
      // for (int i = 0; i < channel; i++) {
      //   b_data[i] = float_to_half(new_bias_data[i]);
      // }
      // bias 按16对齐填充hw
      for (int i = 0; i < repeat; i++) {
        for (int j = 0; j < length; j++) {
          float16 value = float_to_half(new_bias_data[j]);
          b_data[i * length + j] = value;
        }
T
TianXiaogang 已提交
74 75 76 77
      }
      bias_.flush();
    } else {
      float16* new_bias_data = param_.bias()->data<float16>();
78 79 80 81 82 83 84
      // memcpy(b_data, new_bias_data, channel * sizeof(float16));
      for (int i = 0; i < repeat; i++) {
        for (int j = 0; j < length; j++) {
          // float16 value = float_to_half(bias_data_float[j]);
          b_data[i * length + j] = new_bias_data[j];
        }
      }
T
TianXiaogang 已提交
85
      bias_.flush();
C
Chon 已提交
86 87
    }

T
TianXiaogang 已提交
88 89 90 91 92 93 94 95 96 97 98
    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);

    } else {
      // filter 全为1时,且channal为对齐时
      float16* scale_data = param_.scale()->data<float16>();
      float16* filter_data = param.quantizedFilter()->mutableData<float16>(
          FP16, param.filter->shape());
99 100

      // memcpy(filter_data, scale_data, channel * sizeof(float16));
T
TianXiaogang 已提交
101 102 103 104 105
      memcpy(filter_data,
             scale_data,
             param.filter->shape().numel() * sizeof(float16));
      param.quantizedFilter()->flush();
    }
C
Chon 已提交
106 107

    DWconvArgs args = {0};
Y
Yan Chunwei 已提交
108
    args.bias_address = b_data;
C
Chon 已提交
109
    args.filter_address = param.quantizedFilter()->data<void>();
Y
Yan Chunwei 已提交
110 111
    args.kernel.width = param.filter->shape().height();
    args.kernel.height = param.filter->shape().width();
C
Chon 已提交
112 113 114 115 116 117
    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();
T
TianXiaogang 已提交
118 119
    args.image.pad_width = param.paddings[0];
    args.image.pad_height = param.paddings[1];
C
Chon 已提交
120 121 122 123 124 125 126
    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 已提交
127 128 129

    inplace_.power_enable = false;
    inplace_.normalize_enable = false;
C
Chon 已提交
130 131
  }

Y
Yan Chunwei 已提交
132 133
  bool dispatch() {
    param_.input->syncToDevice();
134 135 136 137 138 139 140 141 142 143 144 145
    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) {
Y
Yan Chunwei 已提交
146 147 148
      config_inplace(inplace_);
    }
    bool ret = compute_fpga_dwconv(param_.args) == 0;
149 150
    if (inplace_.relu_enable || inplace_.leaky_relu_enable ||
        inplace_.relu6_enable || inplace_.sigmoid_enable) {
Y
Yan Chunwei 已提交
151
      inplace_.relu_enable = false;
152 153 154
      inplace_.leaky_relu_enable = false;
      inplace_.relu6_enable = false;
      inplace_.sigmoid_enable = false;
Y
Yan Chunwei 已提交
155 156 157 158
      config_inplace(inplace_);
    }
    return ret;
  }
C
Chon 已提交
159 160 161 162 163

  DepthwiseConvParam& param() { return param_; }

 private:
  DepthwiseConvParam param_;
Y
Yan Chunwei 已提交
164 165
  Tensor bias_;
  InplaceArgs inplace_ = {0};
C
Chon 已提交
166 167 168
};

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