fully_connected_pe.hpp 5.1 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

C
chonwhite 已提交
17 18
#include <math.h>
#include <cmath>
C
Chon 已提交
19 20
#include <vector>

21 22 23 24
#include "lite/backends/fpga/KD/pe.hpp"
#include "lite/backends/fpga/KD/pe_params.hpp"
#include "lite/backends/fpga/KD/pes/conv_pe.hpp"
#include "lite/backends/fpga/KD/pes/conv_process.hpp"
C
Chon 已提交
25

Y
Yan Chunwei 已提交
26
namespace paddle {
C
Chon 已提交
27 28 29 30
namespace zynqmp {

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

  void apply() {
Y
Yan Chunwei 已提交
39
    ConvParam& convParam_ = convPE_.param();
C
Chon 已提交
40 41 42
    Tensor* input = param_.input;
    convParam_.input = param_.input;
    convParam_.output = param_.output;
C
chonwhite 已提交
43
    convParam_.activeParam.type = param_.activeParam.type;
C
Chon 已提交
44 45 46 47 48 49 50 51
    convParam_.groups = 1;
    convParam_.strides = {1, 1};
    convParam_.paddings = {0, 0};
    convParam_.kernelSize = {input->shape().width(), input->shape().height()};
    convParam_.dilations = {1, 1};

    int num = param_.filter->shape().channel();
    int chw = param_.filter->shape().num();
C
chonwhite 已提交
52 53 54
    // if (num == 2) {
    //   return;
    // }
C
Chon 已提交
55 56 57

    int height = param_.input->shape().height();
    int width = param_.input->shape().width();
C
chonwhite 已提交
58
    // int filter_channel = chw / height / width;
C
Chon 已提交
59 60

    int channel = param_.output->shape().channel();
C
chonwhite 已提交
61 62
    Shape shape(NCHW, {num, chw_aligned, 1, 1});
    float* new_filter_data = conv_filter_.mutableData<float>(FP32, shape);
C
Chon 已提交
63 64
    float* filter_data = param_.filter->data<float>();

C
chonwhite 已提交
65 66
    memset(new_filter_data, 0, num * chw_aligned * sizeof(float));

C
Chon 已提交
67 68 69
    for (int i = 0; i < num; i++) {
      for (int j = 0; j < chw; j++) {
        float scale = filter_data[j * num + i];
C
chonwhite 已提交
70
        new_filter_data[i * chw_aligned + j] = scale;
C
Chon 已提交
71 72 73 74 75
      }
    }
    conv_filter->flush();
    convParam_.filter = conv_filter;

C
chonwhite 已提交
76 77 78 79 80 81 82
    conv_filter_.flush();
    convParam_.filter = &conv_filter_;
    // param_.filter->saveToFile("param_filter", true);
    // conv_filter->saveToFile("conv_filter", true);
    // exit(-1);

    Shape sb_shape(N, {num});
C
Chon 已提交
83 84 85
    float* scale_data = convParam_.scale()->mutableData<float>(FP32, sb_shape);
    float* bias_data = convParam_.bias()->mutableData<float>(FP32, sb_shape);

C
chonwhite 已提交
86
    for (int i = 0; i < num; i++) {
C
Chon 已提交
87 88 89
      scale_data[i] = 1.0f;
      bias_data[i] = param_.bias->data<float>()[i];
    }
C
chonwhite 已提交
90 91 92 93
    // for (int i = 0; i < num; i++) {
    //   scale_data[i] = 1.0f;
    //   bias_data[i] = param_.bias->data<float>()[i];
    // }
Y
Yan Chunwei 已提交
94 95
    convParam_.scale()->flush();
    convParam_.bias()->flush();
C
Chon 已提交
96

Y
Yan Chunwei 已提交
97 98
    convPE_.init();
    convPE_.apply();
C
Chon 已提交
99 100
  }

C
chonwhite 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  void cpu_compute() {
    int num = param_.filter->shape().channel();
    int chw = param_.filter->shape().num();

    float* filter_data = param_.filter->data<float>();
    float max = 0.0f;
    Tensor* input = param_.input;
    Tensor* output = param_.output;
    float16* input_data = input->data<float16>();
    float16* output_data = output->data<float16>();

    for (int i = 0; i < num; i++) {
      float sum = 0;
      float bias = param_.bias->data<float>()[i];
      for (int j = 0; j < chw; j++) {
        float scale = filter_data[j * num + i];
        float data = half_to_float(input_data[j]);
        sum += scale * data;
      }
      output_data[i] = float_to_half(sum + bias);
      if (max < output_data[i]) {
        max = output_data[i];
      }
    }

    output->flush();
    output->scale()[0] = max / 127.0f;
    output->scale()[1] = 127.0f / max;
C
chonwhite 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    output->saveToFile("cpu_compute", true);
    // exit(-1);
  }

  void batch_to_w() {
    ConvParam& convParam_ = convPE_.param();

    int channel = param_.input->shape().channel();
    param_.input->invalidate();

    int remainder =
        aligned_input_.shape().channel() - param_.input->shape().channel();

    float max = 0;
    for (int n = 0; n < param_.input->shape().num(); n++) {
      memset(aligned_input_.data<float16>(),
             0,
             aligned_input_.shape().channel() * sizeof(float16));
      memcpy(
          aligned_input_.data<float16>() + n * aligned_input_.shape().channel(),
          param_.input->data<float16>() + n * channel,
          channel * sizeof(float16));
      aligned_input_.copyScaleFrom(param_.input);
      aligned_input_.flush();
    }

    convPE_.dispatch();
C
chonwhite 已提交
156 157 158
  }

  bool dispatch() {
C
chonwhite 已提交
159 160 161 162 163
    // batch_to_w();
    // return 1;
    // cpu_compute1();
    // return 1;

C
chonwhite 已提交
164 165 166
    return convPE_.dispatch();
    // }
  }
C
Chon 已提交
167 168 169 170 171

  FullyConnectedParam& param() { return param_; }

 private:
  FullyConnectedParam param_;
C
chonwhite 已提交
172 173
  Tensor aligned_input_;
  Tensor aligned_output_;
Y
Yan Chunwei 已提交
174
  ConvPE convPE_;
C
chonwhite 已提交
175
  Tensor conv_filter_;
C
Chon 已提交
176 177
};
}  // namespace zynqmp
Y
Yan Chunwei 已提交
178
}  // namespace paddle