fully_connected_pe.hpp 9.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 44
    convParam_.relu = param_.relu;
    // convParam_.activeParam.type = param_.activeParam.type;
C
Chon 已提交
45 46 47 48 49 50 51 52
    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 已提交
53 54 55 56 57 58 59 60 61 62 63
    int align = 32;
    int chw_aligned = ((chw + align - 1) / align) * align;

    int infer_num = 1;
    Shape in_shape(NCHW, {infer_num, chw_aligned, 1, 1});
    aligned_input_.mutableData<float16>(FP16, in_shape);
    convParam_.input = &aligned_input_;

    Shape out_shape(NCHW, {infer_num, num, 1, 1});
    aligned_output_.mutableData<float16>(FP16, out_shape);
    convParam_.output = &aligned_output_;
C
Chon 已提交
64 65 66

    int height = param_.input->shape().height();
    int width = param_.input->shape().width();
C
chonwhite 已提交
67
    // int filter_channel = chw / height / width;
C
Chon 已提交
68 69

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

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

C
Chon 已提交
76 77 78
    for (int i = 0; i < num; i++) {
      for (int j = 0; j < chw; j++) {
        float scale = filter_data[j * num + i];
C
chonwhite 已提交
79
        new_filter_data[i * chw_aligned + j] = scale;
C
Chon 已提交
80 81 82
      }
    }

C
chonwhite 已提交
83 84 85 86 87 88 89
    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 已提交
90 91 92
    float* scale_data = convParam_.scale()->mutableData<float>(FP32, sb_shape);
    float* bias_data = convParam_.bias()->mutableData<float>(FP32, sb_shape);

C
chonwhite 已提交
93
    for (int i = 0; i < num; i++) {
C
Chon 已提交
94 95 96
      scale_data[i] = 1.0f;
      bias_data[i] = param_.bias->data<float>()[i];
    }
C
chonwhite 已提交
97 98 99 100
    // for (int i = 0; i < num; i++) {
    //   scale_data[i] = 1.0f;
    //   bias_data[i] = param_.bias->data<float>()[i];
    // }
Y
Yan Chunwei 已提交
101 102
    convParam_.scale()->flush();
    convParam_.bias()->flush();
C
Chon 已提交
103

Y
Yan Chunwei 已提交
104 105
    convPE_.init();
    convPE_.apply();
C
Chon 已提交
106 107
  }

C
chonwhite 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  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 n = 0; n < input->shape().num(); n++) {
      float16* input_data = input->data<float16>() + n * chw;
      float16* output_data =
          output->data<float16>() + n * output->shape().channel();
      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;
  }

  void cpu_compute1() {
    int num = conv_filter_.shape().num();
    int chw = conv_filter_.shape().channel();
    // chw = 336;

    float* filter_data = conv_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 n = 0; n < input->shape().num(); n++) {
      float16* input_data = input->data<float16>() + n * chw;
      float16* output_data =
          output->data<float16>() + n * output->shape().channel();
      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[i * chw + j];
          float data = half_to_float(input_data[j]);
          sum += scale * data;
        }
        float value = sum + bias;
        if (std::isinf(value) || i > 321) {
          std::cout << "i:" << i << " sum:" << sum << " bias:" << bias
                    << std::endl;
          // exit(-1);
        }
        if (i > 321) {
          std::cout << "i:" << i << " sum:" << sum << " bias:" << bias
                    << std::endl;
          // exit(-1);
        }

        output_data[i] = float_to_half(value);
        if (max < value) {
          max = value;
        }
      }
    }

    output->flush();
    output->scale()[0] = max / 127.0f;
    output->scale()[1] = 127.0f / max;

    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();
  }

  bool dispatch() {
    // batch_to_w();
    // return 1;
    // cpu_compute1();
    // return 1;

    // int num = param_.filter->shape().channel();
    // if (num == 2) {
    //   cpu_compute();
    //   return 1;
    // } else {
    // return convPE_.dispatch();
    // }
    ConvParam& convParam_ = convPE_.param();

    if (param_.input->shape().channel() == 321 &&
        param_.output->shape().channel() == 384) {
      // conv_filter_.saveToFile("conv_filter", true);
      // cpu_compute1();
      // return 1;
    }

    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>(),
             param_.input->data<float16>() + n * channel,
             channel * sizeof(float16));
      aligned_input_.copyScaleFrom(param_.input);
      aligned_input_.flush();

      if (param_.input->shape().channel() == 321 &&
          param_.output->shape().channel() == 384) {
        // aligned_input_.saveToFile("aligned_input_", true);
        // convParam_.filter->saveToFile("conv_filter", true);
      }

      convPE_.dispatch();
      aligned_output_.invalidate();
      if (param_.input->shape().num() == 230) {
        // aligned_output_.saveToFile("ao", true);
      }
      //
      float16* src = aligned_output_.data<float16>();
      float16* dst =
          param_.output->data<float16>() + n * param_.output->shape().channel();
      memcpy(dst, src, param_.output->shape().channel() * sizeof(float16));
      if (aligned_output_.scale()[0] > max) {
        max = aligned_output_.scale()[0];
      }
    }
    param_.output->flush();
    param_.output->scale()[0] = max / 127.0f;
    param_.output->scale()[1] = 127.0f / max;
    // param_.output->saveToFile("out", true);

    // exit(-1);
    // cpu_compute();

    // ConvParam& convParam_ = convPE_.param();
    // convParam_.scale()->saveToFile("scale", true);
    return true;
  }
C
Chon 已提交
290 291 292 293 294

  FullyConnectedParam& param() { return param_; }

 private:
  FullyConnectedParam param_;
C
chonwhite 已提交
295 296
  Tensor aligned_input_;
  Tensor aligned_output_;
Y
Yan Chunwei 已提交
297
  ConvPE convPE_;
C
chonwhite 已提交
298
  Tensor conv_filter_;
C
Chon 已提交
299 300
};
}  // namespace zynqmp
Y
Yan Chunwei 已提交
301
}  // namespace paddle