pe.cpp 10.2 KB
Newer Older
qnqinan's avatar
qnqinan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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/driver/pe.h"
#include "fpga/V2/config.h"
#include "fpga/V2/driver/driver.h"
#include "fpga/V2/filter.h"
#include "fpga/V2/image.h"

namespace paddle_mobile {
namespace fpga {
qnqinan's avatar
qnqinan 已提交
23
<<<<<<< HEAD
qnqinan's avatar
qnqinan 已提交
24
#define MUL8(x) (x * 8)
qnqinan's avatar
qnqinan 已提交
25
=======
Z
zhangyang 已提交
26
#define MUL8(x) ((x)*8)
qnqinan's avatar
qnqinan 已提交
27
>>>>>>> upstream/develop
qnqinan's avatar
qnqinan 已提交
28 29 30 31 32 33
#define BYPASS_DONE 1

float Findfp16Max() {
  uint16_t abs_vals[16];
  uint64_t max_fp16;

qnqinan's avatar
qnqinan 已提交
34
<<<<<<< HEAD
qnqinan's avatar
qnqinan 已提交
35
  max_fp16 = reg_readq(MUL8(49));
qnqinan's avatar
qnqinan 已提交
36
=======
Z
zhangyang 已提交
37
  max_fp16 = driver::reg_readq(MUL8(49));
qnqinan's avatar
qnqinan 已提交
38
>>>>>>> upstream/develop
qnqinan's avatar
qnqinan 已提交
39 40 41 42
  abs_vals[0] = (uint16_t)(0x0000007f & (max_fp16));        // NOLINT
  abs_vals[1] = (uint16_t)(0x0000007f & (max_fp16 >> 16));  // NOLINT
  abs_vals[2] = (uint16_t)(0x0000007f & (max_fp16 >> 32));  // NOLINT
  abs_vals[3] = (uint16_t)(0x0000007f & (max_fp16 >> 48));  // NOLINT
qnqinan's avatar
qnqinan 已提交
43
<<<<<<< HEAD
qnqinan's avatar
qnqinan 已提交
44
  max_fp16 = reg_readq(MUL8(50));
qnqinan's avatar
qnqinan 已提交
45
=======
Z
zhangyang 已提交
46
  max_fp16 = driver::reg_readq(MUL8(50));
qnqinan's avatar
qnqinan 已提交
47
>>>>>>> upstream/develop
qnqinan's avatar
qnqinan 已提交
48 49 50 51
  abs_vals[4] = (uint16_t)(0x0000007f & (max_fp16));        // NOLINT
  abs_vals[5] = (uint16_t)(0x0000007f & (max_fp16 >> 16));  // NOLINT
  abs_vals[6] = (uint16_t)(0x0000007f & (max_fp16 >> 32));  // NOLINT
  abs_vals[7] = (uint16_t)(0x0000007f & (max_fp16 >> 48));  // NOLINT
qnqinan's avatar
qnqinan 已提交
52
<<<<<<< HEAD
qnqinan's avatar
qnqinan 已提交
53
  max_fp16 = reg_readq(MUL8(51));
qnqinan's avatar
qnqinan 已提交
54
=======
Z
zhangyang 已提交
55
  max_fp16 = driver::reg_readq(MUL8(51));
qnqinan's avatar
qnqinan 已提交
56
>>>>>>> upstream/develop
qnqinan's avatar
qnqinan 已提交
57 58 59 60
  abs_vals[8] = (uint16_t)(0x0000007f & (max_fp16));         // NOLINT
  abs_vals[9] = (uint16_t)(0x0000007f & (max_fp16 >> 16));   // NOLINT
  abs_vals[10] = (uint16_t)(0x0000007f & (max_fp16 >> 32));  // NOLINT
  abs_vals[11] = (uint16_t)(0x0000007f & (max_fp16 >> 48));  // NOLINT
qnqinan's avatar
qnqinan 已提交
61
<<<<<<< HEAD
qnqinan's avatar
qnqinan 已提交
62
  max_fp16 = reg_readq(MUL8(52));
qnqinan's avatar
qnqinan 已提交
63
=======
Z
zhangyang 已提交
64
  max_fp16 = driver::reg_readq(MUL8(52));
qnqinan's avatar
qnqinan 已提交
65
>>>>>>> upstream/develop
qnqinan's avatar
qnqinan 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  abs_vals[12] = (uint16_t)(0x0000007f & (max_fp16));
  abs_vals[13] = (uint16_t)(0x0000007f & (max_fp16 >> 16));  // NOLINT
  abs_vals[14] = (uint16_t)(0x0000007f & (max_fp16 >> 32));  // NOLINT
  abs_vals[15] = (uint16_t)(0x0000007f & (max_fp16 >> 48));  // NOLINT

  uint16_t tmp = 0;
  for (int i = 0; i < 16; i++) {
    if (tmp < abs_vals[i]) {
      tmp = abs_vals[i];
    }
  }
  return fp16_2_fp32(tmp) / 127.0f;
}

int ComputeFpgaConv(const struct SplitConvArgs &args) {
qnqinan's avatar
qnqinan 已提交
81
<<<<<<< HEAD
qnqinan's avatar
qnqinan 已提交
82
  ComputeBasicConv(args.conv_args[0]);
qnqinan's avatar
qnqinan 已提交
83
=======
Z
zhangyang 已提交
84
  ComputeBasicConv(args.conv_arg[0]);
qnqinan's avatar
qnqinan 已提交
85
>>>>>>> upstream/develop
qnqinan's avatar
qnqinan 已提交
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 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
}

int ComputeBasicConv(const struct ConvArgs &args) {
#ifdef FPGA_PRINT_MODE
  DLOG << "======Compute Basic Conv======";
  DLOG << "   relu_enabled:" << args.relu_enabled
       << "   sb_address:" << args.sb_address
       << "   filter_address:" << args.filter_address
       << "   filter_num:" << args.filter_num
       << "   group_num:" << args.group_num;
  DLOG << "   image_address:" << args.image.address
       << "   image_scale_address:" << args.image.scale_address
       << "   image_channels:" << args.image.channels
       << "   image_height:" << args.image.height
       << "   image_width:" << args.image.width
       << "   pad_height:" << args.image.pad_height
       << "   pad_width:" << args.image.pad_width;
  DLOG << "   kernel_height:" << args.kernel.height
       << "   kernel_width:" << args.kernel.width
       << "   stride_h:" << args.kernel.stride_h
       << "   stride_w:" << args.kernel.stride_w;
  DLOG << "   out_address:" << args.output.address
       << "   out_scale_address:" << args.output.scale_address;
#endif

#ifndef PADDLE_MOBILE_ZU5
  return 0;
#endif

  return 0;
}

int ComputeFpgaPool(const struct PoolingArgs &args) {
#ifdef FPGA_PRINT_MODE
  DLOG << "=============ComputeFpgaPool===========";
  DLOG << "   mode:" << args.mode
       << "   kernel_reciprocal:" << fp16_2_fp32(args.kernel_reciprocal);
  DLOG << "   image_address:" << args.image.address
       << "   image_scale_address:" << args.image.scale_address
       << "   image_channels:" << args.image.channels
       << "   image_height:" << args.image.height
       << "   image_width:" << args.image.width
       << "   pad_height:" << args.image.pad_height
       << "   pad_width:" << args.image.pad_width;
  DLOG << "   kernel_height:" << args.kernel.height
       << "   kernel_width:" << args.kernel.width
       << "   stride_h:" << args.kernel.stride_h
       << "   stride_w:" << args.kernel.stride_w;
  DLOG << "   out_address:" << args.output.address
       << "   out_scale_address:" << args.output.scale_address;
#endif
#ifndef PADDLE_MOBILE_ZU5
  return 0;
#endif
  return 0;
}

int ComputeFpgaEWAdd(const struct EWAddArgs &args) {
#ifdef FPGA_PRINT_MODE
  DLOG << "=============ComputeFpgaEWAdd===========";
  DLOG << "   relu_enabled:" << args.relu_enabled
       << "   const0:" << fp16_2_fp32(int16_t(args.const0))
       << "   const1:" << fp16_2_fp32(int16_t(args.const1));
  DLOG << "   image0_address:" << args.image0.address
       << "   image0_scale_address:" << args.image0.scale_address
       << "   image0_channels:" << args.image0.channels
       << "   image0_height:" << args.image0.height
       << "   image0_width:" << args.image0.width
       << "   pad0_height:" << args.image0.pad_height
       << "   pad0_width:" << args.image0.pad_width;
  DLOG << "   image1_address:" << args.image1.address
       << "   image1_scale_address:" << args.image1.scale_address
       << "   image1_channels:" << args.image1.channels
       << "   image1_height:" << args.image1.height
       << "   image1_width:" << args.image1.width
       << "   pad1_height:" << args.image1.pad_height
       << "   pad_width:" << args.image1.pad_width;
  DLOG << "   out_address:" << args.output.address
       << "   out_scale_address:" << args.output.scale_address;
#endif
#ifndef PADDLE_MOBILE_ZU5
  return 0;
#endif
  return 0;
}

int PerformBypass(const struct BypassArgs &args) {
#ifdef FPGA_PRINT_MODE
  DLOG << "=============ComputeFpgaBypass===========";
  DLOG << "   input_type:" << args.input_data_type
       << "   output_type:" << args.output_data_type
       << "   input_layout_type:" << args.input_layout_type
       << "   output_layout_type:" << args.output_layout_type;
  DLOG << "   image_address:" << args.image.address
       << "   image_scale_address:" << args.image.scale_address
       << "   image_channels:" << args.image.channels
       << "   image_height:" << args.image.height
       << "   image_width:" << args.image.width
       << "   pad_height:" << args.image.pad_height
       << "   pad_width:" << args.image.pad_width;
  DLOG << "   out_address:" << args.output.address
       << "   out_scale_address:" << args.output.scale_address;
#endif
#ifndef PADDLE_MOBILE_ZU5
  return 0;
#endif

qnqinan's avatar
qnqinan 已提交
193
<<<<<<< HEAD
qnqinan's avatar
qnqinan 已提交
194 195
  uint64_t ifm_src_paddr = vaddr_to_paddr(args.image.address);
  uint64_t ifm_dst_paddr = vaddr_to_paddr(args.output.address);
qnqinan's avatar
qnqinan 已提交
196
=======
Z
zhangyang 已提交
197 198
  uint64_t ifm_src_paddr = driver::vaddr_to_paddr(args.image.address);
  uint64_t ifm_dst_paddr = driver::vaddr_to_paddr(args.output.address);
qnqinan's avatar
qnqinan 已提交
199
>>>>>>> upstream/develop
qnqinan's avatar
qnqinan 已提交
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
  uint64_t bp_enable;
  int64_t length;
  uint64_t pixels;

  // fp32->fp16
  if ((args.input_data_type) && (!args.output_data_type)) {
    pixels = (args.image.channels) * (args.image.width) * (args.image.height);
    length = pixels * sizeof(float);
    bp_enable = 0x8800000000000000 + length;
  }
  // fp16->fp32
  else if ((!args.input_data_type) && (args.output_data_type)) {
    pixels = filter::calc_aligned_channel((args.image.channels)) *
             (args.image.width) * (args.image.height);
    length = pixels * sizeof(short);
    length = align_to_x((int)length, 64);  // NOLINT
    bp_enable = 0x8a00000000000000 + length;
  }
  // fp16->fp16 findmax
  else if ((!args.input_data_type) && (!args.output_data_type)) {
    pixels = (args.image.channels) * (args.image.width) * (args.image.height);
    length = pixels * sizeof(short);
    bp_enable = 0x8900000000000000 + length;
  } else {
    return -1;
  }

  // start bypass
qnqinan's avatar
qnqinan 已提交
228
<<<<<<< HEAD
qnqinan's avatar
qnqinan 已提交
229 230 231 232 233 234 235 236 237 238
  reg_writeq(ifm_src_paddr, MUL8(27));
  reg_writeq(ifm_dst_paddr, MUL8(28));
  reg_writeq(0, MUL8(0));
  reg_writeq(bp_enable, MUL8(0));
  // poll
  int ret = -1;
  ret = fpga_regpoll(MUL8(48), BYPASS_DONE, 0xffffffff);
  if (ret != -1) {
    // clear "irq"
    reg_readq(MUL8(63));
qnqinan's avatar
qnqinan 已提交
239
=======
Z
zhangyang 已提交
240 241 242 243
  driver::reg_writeq(ifm_src_paddr, MUL8(27));
  driver::reg_writeq(ifm_dst_paddr, MUL8(28));
  driver::reg_writeq(0, MUL8(0));
  driver::reg_writeq(bp_enable, MUL8(0));
Z
zhangyang 已提交
244
  // poll
Z
zhangyang 已提交
245
  int ret = -1;
Z
zhangyang 已提交
246
  ret = driver::fpga_regpoll(MUL8(48), BYPASS_DONE, 0xffffffff);
Z
zhangyang 已提交
247 248
  if (ret != -1) {
    // clear "irq"
Z
zhangyang 已提交
249
    driver::reg_readq(MUL8(63));
qnqinan's avatar
qnqinan 已提交
250
>>>>>>> upstream/develop
qnqinan's avatar
qnqinan 已提交
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
  }
  // get max value
  if ((!args.input_data_type) && (!args.output_data_type)) {
    float scale = Findfp16Max();
    args.output.scale_address[0] = (float)(1.0 / scale);  // NOLINT
    args.output.scale_address[1] = scale;
  }
  return ret;
}

int ComputeFPGAConcat(const struct ConcatArgs &args) {
#ifdef FPGA_PRINT_MODE
  DLOG << "=============ComputeFpgaConcat===========";
  DLOG << "   Image_num: " << args.image_num
       << "   out_address:" << args.image_out
       << "   out_scale_address:" << args.scale_out
       << "   out_channel:" << args.out_channel;
  DLOG << "   image_height:" << args.height << "   image_width:" << args.width;
  for (int i = 0; i < args.image_num; i++) {
    DLOG << "   " << i << "th:        ";
    DLOG << "   channel_num:" << args.channel_num[i]
         << "   aligned_channel_num:" << args.aligned_channel_num[i]
         << "   image_address:" << args.images_in[i]
         << "   image_scale_address:" << args.scales_in[i];
  }
#endif

  image::concat_images(args.images_in, args.scales_in, args.image_out,
                       args.scale_out, args.image_num, args.channel_num,
                       args.height, args.width, args.aligned_channel_num,
                       args.out_channel);
  return 0;
}

}  // namespace fpga
}  // namespace paddle_mobile