scale_pe.hpp 6.1 KB
Newer Older
Y
Yan Chunwei 已提交
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

M
MyPandaShaoxiang 已提交
17 18
#include <algorithm>

19 20
#include "lite/backends/fpga/KD/pe.hpp"
#include "lite/backends/fpga/KD/pe_params.hpp"
M
MyPandaShaoxiang 已提交
21 22
#include "lite/backends/fpga/KD/pes/depthwise_conv_pe.hpp"
#include "lite/backends/fpga/KD/tensor.hpp"
Y
Yan Chunwei 已提交
23 24 25

namespace paddle {
namespace zynqmp {
M
MyPandaShaoxiang 已提交
26

Y
Yan Chunwei 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
class ScalePE : public PE {
 public:
  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); }
  bool init() {
    Tensor* output = param_.output;
    output->setAligned(true);
    output->setDataLocation(Device);
    return true;
  }

  void apply() {
    Tensor* input = param_.input;
    Tensor* output = param_.output;
    Shape& input_shape = input->shape();
M
MyPandaShaoxiang 已提交
50 51
    DepthwiseConvParam& dw_param = dw_pe_.param();

Y
Yan Chunwei 已提交
52 53 54 55 56 57 58 59 60
    int channel = input_shape.channel();
    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);
    }
M
MyPandaShaoxiang 已提交
61 62

    // FPGA限制 H >2047, W >1023 , WC> 65536 ,需要使用CPU实现
Y
Yan Chunwei 已提交
63 64
    Shape shape(N, {channel * repeat});

M
MyPandaShaoxiang 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    float* filter_data = filter.mutableData<float>(FP32, shape);
    std::fill_n(filter_data, input->shape().channel(), 1.0f);

    Tensor* scale = dw_param.scale();
    float16* scale_data = scale->mutableData<float16>(FP16, shape);
    // memcpy(scale_data, param_.scale->data<float>(), input->shape().channel()
    // * sizeof(float));

    Tensor* bias = dw_param.bias();
    float16* bias_data = bias->mutableData<float16>(FP16, shape);
    std::fill_n(bias_data, input->shape().channel(), 0);

    if (param_.scale->dataType() == FP32) {
      // std::cout << "scale dataType FP32:" << std::endl;
      if (param_.bias != nullptr) {
        float* bias_data_float = param_.bias->data<float>();
        for (int i = 0; i < repeat; i++) {
          for (int j = 0; j < length; j++) {
            float16 value = float_to_half(bias_data_float[j]);
            bias_data[i * length + j] = value;
          }
        }
      } else {
        float16 zero = float_to_half(0.0f);
        for (int i = 0; i < repeat; i++) {
          for (int j = 0; j < length; j++) {
            bias_data[i * length + j] = zero;
          }
        }
      }
Y
Yan Chunwei 已提交
95

M
MyPandaShaoxiang 已提交
96
      float* scale_data_float = param_.scale->data<float>();
Y
Yan Chunwei 已提交
97 98
      for (int i = 0; i < repeat; i++) {
        for (int j = 0; j < length; j++) {
M
MyPandaShaoxiang 已提交
99 100
          float16 value = float_to_half(scale_data_float[j]);
          scale_data[i * length + j] = value;
Y
Yan Chunwei 已提交
101 102 103
        }
      }
    } else {
M
MyPandaShaoxiang 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      if (param_.bias != nullptr) {
        float16* bias_data_float = param_.bias->data<float16>();
        for (int i = 0; i < repeat; i++) {
          for (int j = 0; j < length; j++) {
            float16 value = bias_data_float[j];
            bias_data[i * length + j] = value;
          }
        }
      } else {
        float16 zero = float_to_half(0.0f);
        for (int i = 0; i < repeat; i++) {
          for (int j = 0; j < length; j++) {
            bias_data[i * length + j] = zero;
          }
        }
      }

      float16* scale_data_float = param_.scale->data<float16>();
Y
Yan Chunwei 已提交
122 123
      for (int i = 0; i < repeat; i++) {
        for (int j = 0; j < length; j++) {
M
MyPandaShaoxiang 已提交
124 125
          float16 value = scale_data_float[j];
          scale_data[i * length + j] = value;
Y
Yan Chunwei 已提交
126 127 128 129
        }
      }
    }

M
MyPandaShaoxiang 已提交
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
    // if (param_.bias != nullptr) {
    //   memcpy(bias_data, param_.bias->data<float>(), input->shape().channel()
    //   * sizeof(float));
    // }

    dw_param.input = param_.input;
    dw_param.output = param_.output;
    dw_param.filter = &filter;

    dw_param.strides = {1, 1};
    dw_param.paddings = {0, 0};
    dw_param.kernelSize = {1, 1};
    dw_param.dilations = {1, 1};

    dw_pe_.init();
    dw_pe_.apply();
  }

  void cpu_compute() {
    Tensor* input = param_.input;
    Tensor* output = param_.output;
    Tensor float_input;
    float* image_addr = float_input.mutableData<float>(FP32, input->shape());
    input->syncToCPU();
    float_input.copyFrom(input);
    float16* data_out = output->data<float16>();

    float* scale_data = param_.scale->data<float>();

    int wh = input->shape().width() * input->shape().height();

    float16* in_data = input->data<float16>();

    float max = 0;

    for (int i = 0; i < wh; i++) {
      for (int c = 0; c < input->shape().channel(); c++) {
        int index = i * input->shape().channel() + c;
        float value = half_to_float(in_data[index]) * scale_data[c];
        data_out[index] = float_to_half(value);

        if (value < 0) {
          value = -value;
        }
        if (value > max) {
          max = value;
        }
Y
Yan Chunwei 已提交
177 178
      }
    }
M
MyPandaShaoxiang 已提交
179 180 181
    output->flush();
    output->scale()[0] = max / 127.0f;
    output->scale()[1] = 127.0f / max;
Y
Yan Chunwei 已提交
182 183 184
  }

  bool dispatch() {
M
MyPandaShaoxiang 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
    // cpu_compute();
    // return true;

    if (param_.scale->dataType() == FP16) {
      DepthwiseConvParam& dw_param = dw_pe_.param();
      memcpy(dw_param.quantizedFilter()->mutableData<float16>(),
             param_.scale->data<float16>(),
             param_.scale->shape().numel() * sizeof(float16));
      dw_param.quantizedFilter()->scale()[0] = param_.scale->scale()[0];
      dw_param.quantizedFilter()->scale()[1] = param_.scale->scale()[1];

      dw_param.quantizedFilter()->flush();
      // apply();
    }
Y
Yan Chunwei 已提交
199
    param_.input->syncToDevice();
M
MyPandaShaoxiang 已提交
200
    return dw_pe_.dispatch();
Y
Yan Chunwei 已提交
201 202 203 204 205 206
  }

  ScaleParam& param() { return param_; }

 private:
  ScaleParam param_;
M
MyPandaShaoxiang 已提交
207 208
  Tensor filter;
  DepthwiseConvPE dw_pe_;
Y
Yan Chunwei 已提交
209 210 211
};
}  // namespace zynqmp
}  // namespace paddle