Skip to content

Commit

Permalink
Merge pull request fastmachinelearning#86 from hls-fpga-machine-learn…
Browse files Browse the repository at this point in the history
…ing/jmgd/sublayer-fix

Fix sublayer to use layer code (+ pruning support + default unroll limit of 4096)
  • Loading branch information
nhanvtran authored Sep 23, 2018
2 parents 54294a7 + ea1cde2 commit efc6ea8
Show file tree
Hide file tree
Showing 27 changed files with 175 additions and 310 deletions.
3 changes: 2 additions & 1 deletion example-prjs/sublayer/build_prj.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ add_files -tb myproject_test.cpp -cflags "-I[file normalize ../../nnet_utils]"
add_files -tb firmware/weights
#add_files -tb tb_data
open_solution -reset "solution1"
set_part {xcku115-flvf1924-2-i}
config_array_partition -maximum_size 4096
set_part {xcku115-flvb2104-2-i}
create_clock -period 5 -name default
csim_design
csynth_design
Expand Down
31 changes: 18 additions & 13 deletions example-prjs/sublayer/firmware/myproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@
#include "myproject.h"

#include "nnet_layer.h"
#include "nnet_sublayer.h"
#include "nnet_conv.h"
#include "nnet_conv2d.h"
#include "nnet_activation.h"

//hls-fpga-machine-learning insert weights
#include "weights/w1.h"
#include "weights/b1.h"
#include "weights/w2.h"
#include "weights/b2.h"
#include "weights/w2_0.h"
#include "weights/b2_0.h"
#include "weights/w2_1.h"
#include "weights/b2_1.h"
#include "weights/w3.h"
#include "weights/b3.h"
#include "weights/w4.h"
Expand Down Expand Up @@ -63,22 +65,14 @@ void myproject(
#pragma HLS ARRAY_PARTITION variable=layer1_out complete dim=0
layer1_t logits1[N_LAYER_1];
#pragma HLS ARRAY_PARTITION variable=logits1 complete dim=0
layer1_t logits1_0[N_LAYER_1/2];
#pragma HLS ARRAY_PARTITION variable=logits1_0 complete dim=0
layer1_t logits1_1[N_LAYER_1/2];
#pragma HLS ARRAY_PARTITION variable=logits1_1 complete dim=0
nnet::compute_sublayer<input_t, layer1_t, config1_0>(data, logits1_0, w1, b1);
nnet::compute_sublayer<input_t, layer1_t, config1_1>(data, logits1_1, w1, b1);
nnet::merge<layer1_t, N_LAYER_1/2, N_LAYER_1/2>(logits1_0, logits1_1, logits1);
//nnet::compute_layer<input_t, layer1_t, config1>(data, logits1, w1, b1);
nnet::compute_layer<input_t, layer1_t, config1>(data, logits1, w1, b1);
nnet::relu<layer1_t, layer1_t, relu_config1>(logits1, layer1_out);


layer2_t layer2_out[N_LAYER_2];
#pragma HLS ARRAY_PARTITION variable=layer2_out complete dim=0
layer2_t logits2[N_LAYER_2];
#pragma HLS ARRAY_PARTITION variable=logits2 complete dim=0
nnet::compute_layer<layer1_t, layer2_t, config2>(layer1_out, logits2, w2, b2);
compute_layer2(layer1_out, logits2);
nnet::relu<layer2_t, layer2_t, relu_config2>(logits2, layer2_out);

layer3_t layer3_out[N_LAYER_3];
Expand All @@ -95,3 +89,14 @@ void myproject(


}

void compute_layer2(layer1_t layer1_out[N_LAYER_1], layer2_t logits2[N_LAYER_2]) {
layer2_t logits2_0[16];
#pragma HLS ARRAY_PARTITION variable=logits2_0 complete dim=0
layer2_t logits2_1[16];
#pragma HLS ARRAY_PARTITION variable=logits2_1 complete dim=0
nnet::compute_layer<layer1_t, layer2_t, config2_0>(layer1_out, logits2_0, w2_0, b2_0);
nnet::compute_layer<layer1_t, layer2_t, config2_1>(layer1_out, logits2_1, w2_1, b2_1);
nnet::merge<layer2_t, 16, 16>(logits2_0, logits2_1, logits2);
}

3 changes: 3 additions & 0 deletions example-prjs/sublayer/firmware/myproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ void myproject(
unsigned short &const_size_in,
unsigned short &const_size_out);

void compute_layer2(layer1_t layer1_out[N_LAYER_1], layer2_t logits2[N_LAYER_2]);

#endif

46 changes: 12 additions & 34 deletions example-prjs/sublayer/firmware/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "ap_int.h"
#include "ap_fixed.h"
#include "nnet_layer.h"
#include "nnet_sublayer.h"
#include "nnet_conv.h"
#include "nnet_conv2d.h"
#include "nnet_activation.h"
#include "nnet_common.h"

Expand All @@ -31,58 +31,36 @@ typedef ap_fixed<16,6> layer3_t;
struct config1 : nnet::layer_config {
static const unsigned n_in = N_INPUTS;
static const unsigned n_out = N_LAYER_1;

static const unsigned io_type = nnet::io_parallel;
static const unsigned reuse_factor = 1;
static const unsigned n_zeros = 0;
static const unsigned n_zeros = 593;
static const bool store_weights_in_bram = false;
typedef accum_default_t accum_t;
typedef bias_default_t bias_t;
typedef weight_default_t weight_t;
};
struct config1_0 : nnet::sublayer_config {
static const unsigned n_in = N_INPUTS;
static const unsigned n_out = N_LAYER_1;
static const unsigned n_part = 1;
static const unsigned i_part = 0;
static const unsigned n_sub_out = N_LAYER_1/2;
static const unsigned i_sub_out = 0;

struct relu_config1 : nnet::activ_config {
static const unsigned n_in = N_LAYER_1;
static const unsigned table_size = 1024;
static const unsigned io_type = nnet::io_parallel;
static const unsigned reuse_factor = 1;
static const unsigned n_zeros = 0;
static const bool store_weights_in_bram = false;
typedef accum_default_t accum_t;
typedef bias_default_t bias_t;
typedef weight_default_t weight_t;
};
struct config1_1 : nnet::sublayer_config {
static const unsigned n_in = N_INPUTS;
static const unsigned n_out = N_LAYER_1;
static const unsigned n_part = 1;
static const unsigned i_part = 0;
static const unsigned n_sub_out = N_LAYER_1/2;
static const unsigned i_sub_out = N_LAYER_1/2;

struct config2_0 : nnet::layer_config {
static const unsigned n_in = N_LAYER_1;
static const unsigned n_out = 16;
static const unsigned io_type = nnet::io_parallel;
static const unsigned reuse_factor = 1;
static const unsigned n_zeros = 0;
static const unsigned n_zeros = 774;
static const bool store_weights_in_bram = false;
typedef accum_default_t accum_t;
typedef bias_default_t bias_t;
typedef weight_default_t weight_t;
};
struct relu_config1 : nnet::activ_config {
static const unsigned n_in = N_LAYER_1;
static const unsigned table_size = 1024;
static const unsigned io_type = nnet::io_parallel;
};
struct config2 : nnet::layer_config {
struct config2_1 : nnet::layer_config {
static const unsigned n_in = N_LAYER_1;
static const unsigned n_out = N_LAYER_2;
static const unsigned n_out = 16;
static const unsigned io_type = nnet::io_parallel;
static const unsigned reuse_factor = 1;
static const unsigned n_zeros = 1579;
static const unsigned n_zeros = 805;
static const bool store_weights_in_bram = false;
typedef accum_default_t accum_t;
typedef bias_default_t bias_t;
Expand Down
6 changes: 3 additions & 3 deletions example-prjs/sublayer/firmware/weights/b1.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//Numpy array shape (64,)
//Min -0.449101030827
//Max 0.666767776012
//Min -0.4491010308265686
//Max 0.6667677760124207
//Number of zeros 0

bias_default_t b1[64] = {0.40715688467, -0.152693673968, 0.0247167721391, 0.00855304021388, 0.0373381376266, 0.050812792033, 0.405522465706, 0.287921875715, -0.449101030827, 0.0863167047501, 0.0802906751633, 0.0533042959869, 0.0377268269658, -0.104714356363, 0.161454766989, 0.0043580099009, 0.543596565723, 0.0671059116721, 0.0300234947354, -0.0791025981307, 0.0687709525228, 0.246556162834, 0.307227045298, 0.015126154758, 0.269042164087, 0.159081891179, 0.359984785318, 0.0347696691751, -0.11019410938, 0.290954172611, -0.383461236954, 0.212079584599, 0.267755180597, 0.323163509369, 0.238599523902, 0.666767776012, -0.210283786058, 0.467060565948, 0.0360072515905, 0.447051584721, 0.543918013573, 0.0631941556931, 0.219371959567, 0.343893527985, 0.0888087227941, 0.3759329319, 0.328853011131, 0.0872760862112, 0.0198150593787, 0.100737020373, -0.114139311016, -0.0335930995643, 0.498003512621, 0.0944355949759, 0.33665907383, 0.277953624725, 0.322312951088, 0.328217118979, -0.182733371854, 0.355605363846, 0.242487534881, 0.336844176054, 0.497421890497, 0.153069153428};
bias_default_t b1[64] = {0.40715688467025757, -0.15269367396831512, 0.024716772139072418, 0.008553040213882923, 0.03733813762664795, 0.05081279203295708, 0.4055224657058716, 0.28792187571525574, -0.4491010308265686, 0.08631670475006104, 0.08029067516326904, 0.05330429598689079, 0.03772682696580887, -0.10471435636281967, 0.16145476698875427, 0.004358009900897741, 0.5435965657234192, 0.06710591167211533, 0.030023494735360146, -0.07910259813070297, 0.06877095252275467, 0.24655616283416748, 0.3072270452976227, 0.015126154758036137, 0.26904216408729553, 0.15908189117908478, 0.35998478531837463, 0.03476966917514801, -0.11019410938024521, 0.2909541726112366, -0.38346123695373535, 0.21207958459854126, 0.2677551805973053, 0.3231635093688965, 0.2385995239019394, 0.6667677760124207, -0.2102837860584259, 0.46706056594848633, 0.03600725159049034, 0.4470515847206116, 0.5439180135726929, 0.0631941556930542, 0.21937195956707, 0.34389352798461914, 0.08880872279405594, 0.3759329319000244, 0.3288530111312866, 0.08727608621120453, 0.01981505937874317, 0.10073702037334442, -0.11413931101560593, -0.03359309956431389, 0.4980035126209259, 0.09443559497594833, 0.3366590738296509, 0.2779536247253418, 0.32231295108795166, 0.32821711897850037, -0.18273337185382843, 0.3556053638458252, 0.24248753488063812, 0.33684417605400085, 0.49742189049720764, 0.1530691534280777};
6 changes: 3 additions & 3 deletions example-prjs/sublayer/firmware/weights/b2.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//Numpy array shape (32,)
//Min -0.233829811215
//Max 0.638602077961
//Min -0.2338298112154007
//Max 0.638602077960968
//Number of zeros 0

bias_default_t b2[32] = {0.385666370392, 0.238704577088, 0.309233009815, 0.239236935973, -0.100519768894, 0.390451163054, 0.0123554319143, 0.00878310669214, -0.226692482829, 0.551124632359, 0.387682288885, -0.233829811215, 0.638602077961, -0.0973265096545, 0.0515359714627, 0.0117165837437, 0.291351050138, 0.0050294934772, 0.42868027091, 0.179656729102, 0.377021223307, 0.0864382982254, 0.0744026079774, 0.192403048277, 0.374691933393, -0.0706490576267, 0.00352985248901, -0.00252335402183, -0.000259724474745, -0.0192233566195, -0.000169069055119, 0.195322647691};
bias_default_t b2[32] = {0.3856663703918457, 0.23870457708835602, 0.30923300981521606, 0.23923693597316742, -0.10051976889371872, 0.3904511630535126, 0.012355431914329529, 0.008783106692135334, -0.22669248282909393, 0.551124632358551, 0.3876822888851166, -0.2338298112154007, 0.638602077960968, -0.09732650965452194, 0.05153597146272659, 0.011716583743691444, 0.2913510501384735, 0.005029493477195501, 0.42868027091026306, 0.1796567291021347, 0.3770212233066559, 0.08643829822540283, 0.07440260797739029, 0.19240304827690125, 0.3746919333934784, -0.07064905762672424, 0.0035298524890094995, -0.002523354021832347, -0.00025972447474487126, -0.019223356619477272, -0.00016906905511859804, 0.195322647690773};
6 changes: 6 additions & 0 deletions example-prjs/sublayer/firmware/weights/b2_0.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//Numpy array shape (16,)
//Min -0.2338298112154007
//Max 0.638602077960968
//Number of zeros 0

bias_default_t b2_0[16] = {0.3856663703918457, 0.23870457708835602, 0.30923300981521606, 0.23923693597316742, -0.10051976889371872, 0.3904511630535126, 0.012355431914329529, 0.008783106692135334, -0.22669248282909393, 0.551124632358551, 0.3876822888851166, -0.2338298112154007, 0.638602077960968, -0.09732650965452194, 0.05153597146272659, 0.011716583743691444};
6 changes: 6 additions & 0 deletions example-prjs/sublayer/firmware/weights/b2_1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//Numpy array shape (16,)
//Min -0.07064905762672424
//Max 0.42868027091026306
//Number of zeros 0

bias_default_t b2_1[16] = {0.2913510501384735, 0.005029493477195501, 0.42868027091026306, 0.1796567291021347, 0.3770212233066559, 0.08643829822540283, 0.07440260797739029, 0.19240304827690125, 0.3746919333934784, -0.07064905762672424, 0.0035298524890094995, -0.002523354021832347, -0.00025972447474487126, -0.019223356619477272, -0.00016906905511859804, 0.195322647690773};
6 changes: 3 additions & 3 deletions example-prjs/sublayer/firmware/weights/b3.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//Numpy array shape (32,)
//Min -0.192495539784
//Max 0.727849543095
//Min -0.19249553978443146
//Max 0.727849543094635
//Number of zeros 0

bias_default_t b3[32] = {0.130974292755, -0.177440658212, -0.0381854325533, 0.0132606215775, 0.278921306133, 0.0119517724961, 0.206687957048, -0.00634318916127, 0.323955714703, 0.24621617794, 0.0515957064927, 0.121288783848, -0.108800083399, 0.272505134344, 0.0230016112328, 0.179796844721, 0.30160176754, 0.017023300752, -0.0593090243638, -0.0195552054793, 0.096253298223, -0.0279509462416, 0.208594635129, -0.192495539784, 0.2576790452, 0.346903651953, -0.0277135614306, 0.727849543095, 0.509101092815, 0.116215974092, 0.0136004369706, 0.256516158581};
bias_default_t b3[32] = {0.13097429275512695, -0.17744065821170807, -0.03818543255329132, 0.013260621577501297, 0.27892130613327026, 0.01195177249610424, 0.20668795704841614, -0.006343189161270857, 0.3239557147026062, 0.24621617794036865, 0.05159570649266243, 0.12128878384828568, -0.10880008339881897, 0.27250513434410095, 0.02300161123275757, 0.17979684472084045, 0.301601767539978, 0.017023300752043724, -0.05930902436375618, -0.01955520547926426, 0.09625329822301865, -0.027950946241617203, 0.20859463512897491, -0.19249553978443146, 0.2576790452003479, 0.34690365195274353, -0.027713561430573463, 0.727849543094635, 0.5091010928153992, 0.11621597409248352, 0.013600436970591545, 0.25651615858078003};
6 changes: 3 additions & 3 deletions example-prjs/sublayer/firmware/weights/b4.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//Numpy array shape (5,)
//Min -0.260938197374
//Max 0.304645419121
//Min -0.26093819737434387
//Max 0.3046454191207886
//Number of zeros 0

bias_default_t b4[5] = {-0.0388891100883, -0.0420672409236, -0.260938197374, 0.0625408738852, 0.304645419121};
bias_default_t b4[5] = {-0.03888911008834839, -0.04206724092364311, -0.26093819737434387, 0.06254087388515472, 0.3046454191207886};
Loading

0 comments on commit efc6ea8

Please sign in to comment.