bias_scale.cpp 4.2 KB
Newer Older
Z
zhangyang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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/V2/bias_scale.h"
#include <memory.h>
17
#include <math.h>
Z
zhangyang 已提交
18
#include "fpga/common/fpga_common.h"
Z
zhangyang 已提交
19 20 21 22 23

namespace paddle_mobile {
namespace fpga {
namespace bias_scale {

24 25
void align_element(float **data_in, int num_per_div_before_alignment, int num) {
  int copynum = 0;
Z
zhangyang 已提交
26
  float *ptr_unaligned = *data_in;
27 28 29 30 31 32
  int div_num =
      (num + num_per_div_before_alignment - 1) / num_per_div_before_alignment;
  int num_per_div_after_alignment =
      align_to_x(num_per_div_before_alignment, BS_NUM_ALIGNMENT);
  int num_element =
      2 * div_num * num_per_div_after_alignment;  // including bias & scale
Z
zhangyang 已提交
33
  float *ptr_aligned =
34
      (float *)fpga_malloc(num_element * sizeof(float));  // NOLINT
Z
zhangyang 已提交
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  memset(ptr_aligned, 0, num_element * sizeof(float));

  for (int i = 0; i < div_num; i++) {
    if (i == div_num - 1) {
      copynum = (num_per_div_after_alignment * div_num > num)
                    ? (num % num_per_div_after_alignment)
                    : (num_per_div_before_alignment);
    } else {
      copynum = num_per_div_before_alignment;
    }

    memcpy(ptr_aligned + i * num_per_div_after_alignment,
           ptr_unaligned + num_per_div_before_alignment * i,
           copynum * sizeof(float));
    memcpy(ptr_aligned + (div_num + i) * num_per_div_after_alignment,
           ptr_unaligned + num_per_div_before_alignment * i + num,
           copynum * sizeof(float));
Z
zhangyang 已提交
53 54 55 56 57 58
  }

  fpga_free(ptr_unaligned);
  *data_in = ptr_aligned;
}

59 60 61 62 63 64 65 66 67 68 69
void fixed_scale_bias_new(void*data_in, int data_len) {
    int* data_tmp = static_cast<int*>(data_in);
    for (int idx = 0; idx < data_len/2; ++idx) {
        float tmp = (static_cast<float*>(data_in))[idx];
        data_tmp[idx] = static_cast<int>(round(tmp*pow(2.0, 23.0)));
        tmp = (static_cast<float*>(data_in))[idx+data_len/2];
        data_tmp[idx+data_len/2] = static_cast<int>(round(tmp*pow(2.0, 30.0)));
    }
    return;
}

70 71 72 73
void interleave(float **data_in, int num_after_alignment) {
  // num_after_alignment: number of bias after alignment

  float *ptr_uninterleaved = *data_in;
74
  // fixed_scale_bias_new(ptr_uninterleaved, 2 * num_after_alignment);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  float *ptr_interleaved =
      (float *)fpga_malloc(2 * num_after_alignment * sizeof(float));  // NOLINT
  int num = num_after_alignment / 4;
  for (int i = 0; i < num; i++) {
    memcpy(ptr_interleaved + 8 * i, ptr_uninterleaved + 4 * i,
           4 * sizeof(float));
    memcpy(ptr_interleaved + 8 * i + 4,
           ptr_uninterleaved + num_after_alignment + 4 * i, 4 * sizeof(float));
  }

  fpga_free(ptr_uninterleaved);
  *data_in = ptr_interleaved;
}

void format_bias_scale_array(float **bias_scale_array,
                             int element_num_per_division, int num) {
  align_element(bias_scale_array, element_num_per_division, num);
  int div_num = (num + element_num_per_division - 1) / element_num_per_division;
  int element_num_after_division =
      align_to_x(element_num_per_division, BS_NUM_ALIGNMENT);
  interleave(bias_scale_array, div_num * element_num_after_division);
  fpga_flush(*bias_scale_array, 2 * element_num_after_division * sizeof(float));
}
void format_bias_array(float **bias_array, int num) {
  float *ptr_unaligned = *bias_array;
  int num_before_align = num;
  int num_after_align = align_to_x(num_before_align, BIAS_NUM_ALIGNMENT);
  int16_t *ptr_aligned =
      (int16_t *)fpga_malloc(num_after_align * sizeof(int16_t));  // NOLINT

  memset(ptr_aligned, 0, num_after_align * sizeof(int16_t));
  for (int i = 0; i < num_before_align; i++) {
    ptr_aligned[i] = fp32_2_fp16(ptr_unaligned[i]);
  }
  *bias_array = (float *)ptr_aligned;  // NOLINT
  fpga_free(ptr_unaligned);
Z
zhangyang 已提交
111 112 113 114 115
}

}  // namespace bias_scale
}  // namespace fpga
}  // namespace paddle_mobile