fpga_quantilization.cpp 2.7 KB
Newer Older
H
hanbuhe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* Copyright (c) 2018 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. */

#include "fpga/fpga_quantilization.h"
#include <algorithm>

namespace paddle_mobile {
namespace fpga {

template <typename Dtype>
static void chw_to_hwc(Dtype* data_in, Dtype* data_out, int num, int channel,
                       int height, int width) {
  int offset_height = 0;

  for (int n = 0; n < num; n++) {
    int amount_per_row = width * channel;
    for (int c = 0; c < channel; c++) {
      for (int h = 0; h < height; h++) {
        int offset_height = h * amount_per_row;
        for (int w = 0; w < width; w++) {
          *(data_out + offset_height + w * channel + c) = *(data_in++);
        }
      }
    }
    data_out += num;
  }
}

H
hanbuhe 已提交
40 41 42 43 44 45 46 47 48
template <typename Dtype>
static Dtype find_max(Dtype* data, int num) {
  Dtype max = 0;
  for (int i = 0; i < num; ++i) {
    max = std::max(max, data[i]);
  }
  return max;
}

C
chonwhite 已提交
49

50
// template <typename Dtype>
C
chonwhite 已提交
51 52 53 54
void quantify_filter(framework::Tensor* filter) {

  DLOG << "quantilize_filter........";

H
hanbuhe 已提交
55
  float scale = 0;
H
hanbuhe 已提交
56
  float fix_range = static_cast<float>((1 << (8 - 1)) - 1);
H
hanbuhe 已提交
57 58 59 60 61 62

  const int batch_size = filter->dims()[0];
  const int channel = filter->dims()[1];
  const int height = filter->dims()[2];
  const int width = filter->dims()[3];

H
hanbuhe 已提交
63
  int8_t* int_data = nullptr;
64
  int8_t* tmp_data = new int8_t[filter->numel()];
H
hanbuhe 已提交
65

H
hanbuhe 已提交
66 67 68
  // 32bit filter -> 8bit filter;
  if (filter->type() == typeid(float)) {
    float* float_data = filter->data<float>();
C
chonwhite 已提交
69
    float max = find_max<float>(float_data, filter->numel());
H
hanbuhe 已提交
70

H
hanbuhe 已提交
71
    scale = (max / fix_range);
H
hanbuhe 已提交
72 73

    for (int i = 0; i < filter->numel(); ++i) {
H
hanbuhe 已提交
74
      tmp_data[i] = (int8_t)float_data[i] * scale;
H
hanbuhe 已提交
75
    }
C
chonwhite 已提交
76
    int_data = filter->mutable_data<int8_t>();
H
hanbuhe 已提交
77
  } else {
C
chonwhite 已提交
78
    int8_t max = find_max<int8_t>(filter->data<int8_t>(), filter->numel());
H
hanbuhe 已提交
79 80
    scale = (max / fix_range);

H
hanbuhe 已提交
81
    for (int i = 0; i < filter->numel(); ++i) {
C
chonwhite 已提交
82
      tmp_data[i] = filter->data<int8_t>()[i];
H
hanbuhe 已提交
83
    }
H
hanbuhe 已提交
84
    int_data = filter->mutable_data<int8_t>();
H
hanbuhe 已提交
85
  }
H
hanbuhe 已提交
86 87 88 89
  // NCHW -> NHWC;
  chw_to_hwc<int8_t>(tmp_data, int_data, batch_size, channel, height, width);
  delete tmp_data;
  *(filter->fpga_args().scale_pointer()) = scale;
C
chonwhite 已提交
90

H
hanbuhe 已提交
91 92 93 94
}

}  // namespace fpga
}  // namespace paddle_mobile