scale_pe.hpp 5.7 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

T
TianXiaogang 已提交
17 18
#include <algorithm>

19 20
#include "lite/backends/fpga/KD/pe.hpp"
#include "lite/backends/fpga/KD/pe_params.hpp"
T
TianXiaogang 已提交
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 {
T
TianXiaogang 已提交
26

Y
Yan Chunwei 已提交
27 28 29 30 31 32 33 34 35 36 37 38
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); }
C
chonwhite 已提交
39

Y
Yan Chunwei 已提交
40 41 42 43 44 45 46 47 48 49 50
  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();
T
TianXiaogang 已提交
51 52
    DepthwiseConvParam& dw_param = dw_pe_.param();

Y
Yan Chunwei 已提交
53 54 55 56 57 58 59 60 61
    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);
    }
T
TianXiaogang 已提交
62 63

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

T
TianXiaogang 已提交
66 67
    float* filter_data = filter.mutableData<float>(FP32, shape);
    std::fill_n(filter_data, input->shape().channel(), 1.0f);
Y
Yan Chunwei 已提交
68

T
TianXiaogang 已提交
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
    Tensor* scale = dw_param.scale();
    float16* scale_data = scale->mutableData<float16>(FP16, shape);

    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) {
      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;
          }
        }
      }
      float* scale_data_float = param_.scale->data<float>();
Y
Yan Chunwei 已提交
94 95
      for (int i = 0; i < repeat; i++) {
        for (int j = 0; j < length; j++) {
T
TianXiaogang 已提交
96 97
          float16 value = float_to_half(scale_data_float[j]);
          scale_data[i * length + j] = value;
Y
Yan Chunwei 已提交
98 99 100
        }
      }
    } else {
T
TianXiaogang 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
      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 已提交
119 120
      for (int i = 0; i < repeat; i++) {
        for (int j = 0; j < length; j++) {
T
TianXiaogang 已提交
121 122
          float16 value = scale_data_float[j];
          scale_data[i * length + j] = value;
Y
Yan Chunwei 已提交
123 124 125 126
        }
      }
    }

T
TianXiaogang 已提交
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
    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 已提交
169 170
      }
    }
T
TianXiaogang 已提交
171 172 173
    output->flush();
    output->scale()[0] = max / 127.0f;
    output->scale()[1] = 127.0f / max;
Y
Yan Chunwei 已提交
174 175 176
  }

  bool dispatch() {
T
TianXiaogang 已提交
177 178 179 180 181 182 183 184 185 186
    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();
    }
Y
Yan Chunwei 已提交
187
    param_.input->syncToDevice();
T
TianXiaogang 已提交
188
    return dw_pe_.dispatch();
Y
Yan Chunwei 已提交
189 190 191 192 193 194
  }

  ScaleParam& param() { return param_; }

 private:
  ScaleParam param_;
T
TianXiaogang 已提交
195 196
  Tensor filter;
  DepthwiseConvPE dw_pe_;
Y
Yan Chunwei 已提交
197 198 199
};
}  // namespace zynqmp
}  // namespace paddle