conv_pe.hpp 6.3 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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

#include <arm_neon.h>
T
TianXiaogang 已提交
18
#include <algorithm>
Y
Yan Chunwei 已提交
19 20
#include <vector>

21 22 23 24 25 26 27
#include "lite/backends/fpga/KD/pe.hpp"
#include "lite/backends/fpga/KD/pe_params.hpp"
#include "lite/backends/fpga/KD/pes/concat_pe.hpp"
#include "lite/backends/fpga/KD/pes/conv_pe.hpp"
#include "lite/backends/fpga/KD/pes/conv_process.hpp"
#include "lite/backends/fpga/KD/pes/elementwise_add_pe.hpp"
#include "lite/backends/fpga/KD/pes/scale_pe.hpp"
C
chonwhite 已提交
28
#include "lite/backends/fpga/KD/pes/split_pe.hpp"
Y
Yan Chunwei 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

namespace paddle {
namespace zynqmp {

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

  void apply() {
    split_axis = fill_split_arg(param_);

C
chonwhite 已提交
45 46
    split_channel = param_.groups != 1 && param_.splitParams().size() > 1;

Y
Yan Chunwei 已提交
47 48 49 50 51 52 53 54 55
    if (split_axis == 0 && param_.splitParams().size() > 1) {
      ConcatParam& concat_param = concatPE_.param();
      for (auto conv_param : param_.splitParams()) {
        concat_param.inputs.push_back(&conv_param->output);
      }
      concat_param.output = param_.output;
      concatPE_.init();
      concatPE_.apply();
    }
T
TianXiaogang 已提交
56

C
chonwhite 已提交
57 58 59 60 61 62 63 64 65 66
    if (split_channel) {
      SplitParam& split_param = splitPE_.param();
      split_param.input = param_.input;
      for (auto conv_param : param_.splitParams()) {
        split_param.outputs.push_back(&conv_param->input);
      }
      splitPE_.init();
      splitPE_.apply();
    }

T
TianXiaogang 已提交
67 68 69
    if (DLEngine::get_instance().isZU3() &&
        param_.input->shape().dimSize() == 4 &&
        param_.input->shape().width() == 1 &&
C
chonwhite 已提交
70
        param_.input->shape().channel() >= 2048) {
T
TianXiaogang 已提交
71 72
      use_cpu_ = true;
    }
C
chonwhite 已提交
73
    if (!use_cpu_) {
74
      // param_.filter->releaseData();
T
TianXiaogang 已提交
75 76
    }

C
chonwhite 已提交
77
    // exit(-1);
T
TianXiaogang 已提交
78
  }
Y
Yan Chunwei 已提交
79 80 81 82 83 84 85 86 87
  void cpu_compute() {
    Tensor* input = param_.input;
    Tensor* output = param_.output;
    input->syncToCPU();

    Tensor float_input;
    Tensor float_output;
    float* image_addr = float_input.mutableData<float>(FP32, input->shape());
    float_input.copyFrom(input);
C
chonwhite 已提交
88
    // float16* data_out = output->data<float16>();
Y
Yan Chunwei 已提交
89 90 91 92 93 94 95 96 97 98 99
    float* out = float_output.mutableData<float>(FP32, output->shape());

    int out_channel = output->shape().channel();
    int in_channel = input->shape().channel();

    float* filter_data = param_.filter->data<float>();
    float* mi = new float[in_channel];
    for (int i = 0; i < out_channel; i++) {
      float* image = image_addr;
      float* filter_ptr = filter_data + i * in_channel;
      float* out_ptr = mi;
C
chonwhite 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113
#pragma omp parallel for
      for (int j = 0; j < in_channel; j++) {
        // float32x4_t x0 = vld1q_f32(image);
        // float32x4_t x1 = vld1q_f32(filter_ptr);

        // float32x4_t r = vmulq_f32(x0, x1);

        // vst1q_f32(out_ptr, r);
        // image += 4;
        // filter_ptr += 4;
        // out_ptr += 4;
        float value = image_addr[j] * filter_ptr[j];
        mi[j] = value;
      }
Y
Yan Chunwei 已提交
114

C
chonwhite 已提交
115 116 117
      float sum = 0;
      for (int j = 0; j < in_channel; j++) {
        sum += mi[j];
Y
Yan Chunwei 已提交
118
      }
C
chonwhite 已提交
119
      out[i] = sum;
Y
Yan Chunwei 已提交
120 121 122 123 124 125 126
    }
    delete[] mi;
    float_output.flush();
    output->copyFrom(&float_output);
  }

  bool dispatch() {
C
chonwhite 已提交
127
    fpga_reset();
T
TianXiaogang 已提交
128 129 130 131 132
    if (use_cpu_) {
      cpu_compute();
      return true;
    }

C
chonwhite 已提交
133 134 135 136 137 138 139 140 141
    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;
    }
T
TianXiaogang 已提交
142

C
chonwhite 已提交
143 144
    if (inplace_.relu_enable || inplace_.leaky_relu_enable ||
        inplace_.relu6_enable || inplace_.sigmoid_enable) {
Y
Yan Chunwei 已提交
145
      config_inplace(inplace_);
T
TianXiaogang 已提交
146
      if (inplace_.leaky_relu_enable) {
C
chonwhite 已提交
147
        activeParamterArgs.type = TYPE_LEAKY_RELU;
T
TianXiaogang 已提交
148
        activeParamterArgs.leaky_relu_factor =
C
chonwhite 已提交
149
            fp32_2_fp16(param_.activeParam.leaky_relu_factor);
T
TianXiaogang 已提交
150 151
        config_activation(activeParamterArgs);
      }
Y
Yan Chunwei 已提交
152 153 154
    }

    std::vector<BasicConvParam*>& params = param_.splitParams();
C
chonwhite 已提交
155 156 157 158 159 160

    if (split_channel) {
      // splitPE_.param().input->saveToFile("input_image",true);
      splitPE_.dispatch();
    }

Y
Yan Chunwei 已提交
161 162
    int ret = 0;
    for (auto conv_param : params) {
C
chonwhite 已提交
163 164 165 166
      // conv_param->input.printScale();
      // if (split_channel) {
      //   conv_param->input.saveToFile("pack_image",true);
      // }
Y
Yan Chunwei 已提交
167 168 169
      ret |= compute_fpga_conv_basic(conv_param->args);
    }

C
chonwhite 已提交
170 171
    if (inplace_.relu_enable || inplace_.leaky_relu_enable ||
        inplace_.relu6_enable || inplace_.sigmoid_enable) {
Y
Yan Chunwei 已提交
172
      inplace_.relu_enable = false;
T
TianXiaogang 已提交
173
      inplace_.leaky_relu_enable = false;
C
chonwhite 已提交
174 175
      inplace_.relu6_enable = false;
      inplace_.sigmoid_enable = false;
Y
Yan Chunwei 已提交
176
      config_inplace(inplace_);
T
TianXiaogang 已提交
177 178

      if (inplace_.leaky_relu_enable) {
C
chonwhite 已提交
179
        activeParamterArgs.type = TYPE_LEAKY_RELU;
T
TianXiaogang 已提交
180 181 182
        activeParamterArgs.leaky_relu_factor = fp32_2_fp16(0);
        config_activation(activeParamterArgs);
      }
Y
Yan Chunwei 已提交
183 184 185 186
    }

    size_t size = params.size();
    if (split_axis == 0 && ret == 0 && size > 1) {
C
chonwhite 已提交
187
      // std::cout << "concat size:" << size << std::endl;
Y
Yan Chunwei 已提交
188 189 190
      concatPE_.dispatch();
    }
    if (split_axis == 1 && ret == 0 && size > 1) {
C
chonwhite 已提交
191
      // for (int n = 0; n < size - 1; n++) {
Y
Yan Chunwei 已提交
192 193 194 195 196 197 198
      ElementwiseAddParam& add_param = addPE_.param();
      add_param.inputs = {&params[0]->output, &params[1]->output};
      add_param.output = param_.output;
      addPE_.init();
      addPE_.apply();
      addPE_.dispatch();
    }
C
chonwhite 已提交
199

Y
Yan Chunwei 已提交
200 201 202 203 204
    return ret == 0;
  }

  ConvParam& param() { return param_; }

C
chonwhite 已提交
205 206
  ~ConvPE() {}

Y
Yan Chunwei 已提交
207
 private:
T
TianXiaogang 已提交
208
  bool use_cpu_ = false;
C
chonwhite 已提交
209
  bool split_channel = false;
Y
Yan Chunwei 已提交
210 211
  ConvParam param_;
  ConcatPE concatPE_;
C
chonwhite 已提交
212
  SplitPE splitPE_;
Y
Yan Chunwei 已提交
213 214 215
  ElementwiseAddPE addPE_;
  int split_axis = 0;
  InplaceArgs inplace_ = {0};
T
TianXiaogang 已提交
216
  ActiveParamterArgs activeParamterArgs;
Y
Yan Chunwei 已提交
217 218 219 220
};

}  // namespace zynqmp
}  // namespace paddle