From c536152a761b8d7ff1f48125e8ff1e1277af347e Mon Sep 17 00:00:00 2001 From: BLMartin99 Date: Tue, 27 Jun 2023 14:29:57 -0700 Subject: [PATCH 1/4] Working Leaky Relu example --- example-prjs/leaky_relu/catapult.py | 43 +++++++++++++++ example-prjs/leaky_relu/config.yml | 15 ++++++ example-prjs/leaky_relu/leaky_relu.h5 | Bin 0 -> 8416 bytes example-prjs/leaky_relu/leaky_relu.json | 1 + example-prjs/leaky_relu/leaky_relu.py | 51 ++++++++++++++++++ example-prjs/leaky_relu/leaky_relu_weights.h5 | Bin 0 -> 7120 bytes example-prjs/leaky_relu/run_catapult.sh | 44 +++++++++++++++ example-prjs/leaky_relu/run_vivado.sh | 39 ++++++++++++++ example-prjs/leaky_relu/tb_input_features.dat | 2 + .../leaky_relu/tb_output_predictions.dat | 2 + example-prjs/leaky_relu/vivado.py | 43 +++++++++++++++ 11 files changed, 240 insertions(+) create mode 100644 example-prjs/leaky_relu/catapult.py create mode 100644 example-prjs/leaky_relu/config.yml create mode 100644 example-prjs/leaky_relu/leaky_relu.h5 create mode 100644 example-prjs/leaky_relu/leaky_relu.json create mode 100644 example-prjs/leaky_relu/leaky_relu.py create mode 100644 example-prjs/leaky_relu/leaky_relu_weights.h5 create mode 100755 example-prjs/leaky_relu/run_catapult.sh create mode 100755 example-prjs/leaky_relu/run_vivado.sh create mode 100644 example-prjs/leaky_relu/tb_input_features.dat create mode 100644 example-prjs/leaky_relu/tb_output_predictions.dat create mode 100644 example-prjs/leaky_relu/vivado.py diff --git a/example-prjs/leaky_relu/catapult.py b/example-prjs/leaky_relu/catapult.py new file mode 100644 index 0000000000..79db00adbf --- /dev/null +++ b/example-prjs/leaky_relu/catapult.py @@ -0,0 +1,43 @@ + +import hls4ml +# import pprint +import yaml +import numpy as np + +print(hls4ml.__version__) + +with open('config.yml', 'r') as ymlfile: + config = yaml.safe_load(ymlfile) + +# try tweaking the reuse_factor on one layer to get different pipelining +# config['HLSConfig']['LayerName']['fc1']['ReuseFactor'] = 4 + +print('NETWORK') +print(config) + +config['OutputDir'] = 'my-Catapult-test' +config['Backend'] = 'Catapult' +config['IOType'] = 'io_stream' + +config['HLSConfig']['Model']['Strategy'] = 'Latency' +#config['HLSConfig']['Model']['Strategy'] = 'Resource' + +# default threshold is infinity +config['HLSConfig']['Model']['BramFactor'] = np.inf +# set to zero to force all weights onto (external function) interface +config['HLSConfig']['Model']['BramFactor'] = 0 + +print('CURRENT CONFIGURATION') +print('Backend='+config['Backend']) +print('IOType='+config['IOType']) +print('BramFactor={bf}'.format(bf=config['HLSConfig']['Model']['BramFactor'])) + +# pprint.pprint(config) + +#Convert it to a hls project +hls_model = hls4ml.converters.keras_to_hls(config) + +hls_model.build(vsynth=False) + +# URL for this info: https://fastmachinelearning.org/hls4ml/setup/QUICKSTART.html + diff --git a/example-prjs/leaky_relu/config.yml b/example-prjs/leaky_relu/config.yml new file mode 100644 index 0000000000..3c02b585aa --- /dev/null +++ b/example-prjs/leaky_relu/config.yml @@ -0,0 +1,15 @@ +Backend: Catapult +KerasJson: leaky_relu.json +KerasH5: leaky_relu_weights.h5 +OutputDir: my-Catapult-test +ProjectName: leaky_relu +XilinxPart: xcku115-flvb2104-2-i +Part: xcku115-flvb2104-2-i +ClockPeriod: 5 + +IOType : io_parallel +HLSConfig: + Model: + Precision: ap_fixed<16, 6> + ReuseFactor: 1 + Strategy: Latency diff --git a/example-prjs/leaky_relu/leaky_relu.h5 b/example-prjs/leaky_relu/leaky_relu.h5 new file mode 100644 index 0000000000000000000000000000000000000000..83dff7176e6202d95c586bae078a9bcc1e97898f GIT binary patch literal 8416 zcmeHM%}yIJ5ccv@ML;T$I6!}-)t-7lC<(MGy_Bk?O{J(HXr)RBZB63*2(w9dH_!;> zPH%mT9((K~^f5U0EvUzyfn>!76@gTQeTePZ@i*hoGn2&ot+e(gJMmzGLCa*=7@O8Z z`Z&dQ;u;Cf>G@|IIM^R!|6{}q95&A2*&NEJ5#Jp}0WSQoSu8T8N%+*G))_$NOkf9h ziGfn_)duYM6Kag9>~WMwa{&J4%vuQLg%HSXKj z>t($MC&iy&%+HQ72e>*(uT*AFE+@+y)?k!I9;;d)!* zOGgB8lS^;G^-Mid@*@u@2Zd-rK_5q?@lzmkO{xYV$Ax%^H`HP=b)x>!R8xApZ<--s|-il-GlsD?`w3 zit@*VOSHBxs6itRc{AX<670rd2h3jN8<*_{S1P&1rJQd0{7NCWQh2`dtk>J=^)&Gq z)=bBrlJuF<&mo4OrJWm{Gxc=dW))+9vNW>2Wxz6E8L$jk1}p=X0n318z%pPNungR0 z2DaX>uQ}>pKVDrh7ynSwJwWo_;Ps-BYbKInp z7W=_gu+BpA?eo&nM!uTe=6Pz>m%C6k% zg>>JX`q3Ym$bCQ&_dh-=|xAi8;n4Mq5Kw75caH@ma5-|c-j-yWH#+Sc~W^x`y_mQKSs)|oaA5+I6 z`$+a>%yt}@05dmF{v6>KQ`V8Ag+~X~D(FbYXG_Zl0E{#6OXpuOP^)h2n+I*s)0WA; zQl8NeeC4(7-B7vyHM+XNmFue;<-6_ROkZ!+_MXYeUeNg>@URodx|n@lIQ+a|VgH z7!80zCLBXe7HWhui#RP6dXamh!ss&S=$WtZCe3QXGm>>6h5KV2vYy{D5TI$|b+dYg zWIfw3x&BiI;(DeOgC6A()51DI@w4+Ami62iw;*?PzoAZMP|x<97z#RHTq7B*LnQ;1 zK7GGf&+#oK>7+^DQPxQk56@Gd5r3KoM&H@2?;^H2sC47|;CZCS@$;0ATlCFxM(h~-R0bxKG5C((+VL%uV2801&Ko}4Pgn{2- z;Bc?L<-i!1_@GR65v~Ko>m*$FYfycH<7x|bzYnpx#qT`D=catapult.py +# to only run catapult +# sed -e 's/Vivado/Catapult/g' vivado.py | sed -e 's/vsynth=True/vsynth=False/g' >catapult.py + +# actually run HLS4ML + Catapult (+ optional vivado RTL) +python3 catapult.py + +# run just the C++ execution +echo "" +echo "=====================================================" +echo "=====================================================" +echo "C++ EXECUTION" +pushd my-Catapult-test; rm -f a.out; $MGC_HOME/bin/g++ -std=c++17 -I. -DWEIGHTS_DIR=\"firmware/weights\" -Ifirmware -I$MGC_HOME/shared/include firmware/leaky_relu.cpp leaky_relu_test.cpp; a.out; popd + +# Using VSCode setup generated by Catapult +echo "" +echo "=====================================================" +echo "=====================================================" +echo "To launch VSCode on the C++ generated by hls4ml:" +echo "setenv LD_LIBRARY_PATH $MGC_HOME/lib:$MGC_HOME/shared/lib" +echo "pushd my-Catapult-test; /wv/hlstools/vscode/LATEST/code Catapult.code-workspace" diff --git a/example-prjs/leaky_relu/run_vivado.sh b/example-prjs/leaky_relu/run_vivado.sh new file mode 100755 index 0000000000..0315ed4697 --- /dev/null +++ b/example-prjs/leaky_relu/run_vivado.sh @@ -0,0 +1,39 @@ +#! /bin/bash + +# This script runs the Vivado flows to generate the HLS. + +VENV=$HOME/venv + +MGC_HOME=/wv/hlsb/CATAPULT/TOT/CURRENT/aol/Mgc_home +export MGC_HOME + +export PATH=/wv/hlstools/python/python37/bin:$PATH:$XILINX_VIVADO/bin:$MGC_HOME/bin +export LD_LIBRARY_PATH=/wv/hlstools/python/python37/lib:$XILINX_VIVADO/lib/lnx64.o:$MGC_HOME/lib +export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python + +# needed for pytest +export OSTYPE=linux-gnu + +echo "Activating Virtual Environment..." +# bash +source $VENV/bin/activate + +rm -rf ./my-Vivado-test* + +mkdir -p tb_data + +# to run catapult+vivado_rtl +sed -e 's/Vivado/Catapult/g' vivado.py >catapult.py +# to only run catapult +# sed -e 's/Vivado/Catapult/g' vivado.py | sed -e 's/vsynth=True/vsynth=False/g' >catapult.py + +# actually run HLS4ML + Vivado HLS +python3 vivado.py + +# run just the C++ execution +echo "" +echo "=====================================================" +echo "=====================================================" +echo "C++ EXECUTION" +pushd my-Vivado-test; rm -f a.out; $MGC_HOME/bin/g++ -g -std=c++11 -I. -DWEIGHTS_DIR=\"firmware/weights\" -Ifirmware -Ifirmware/ap_types -I$MGC_HOME/shared/include firmware/leaky_relu.cpp leaky_relu_test.cpp; a.out; popd + diff --git a/example-prjs/leaky_relu/tb_input_features.dat b/example-prjs/leaky_relu/tb_input_features.dat new file mode 100644 index 0000000000..b1fcd4b3e1 --- /dev/null +++ b/example-prjs/leaky_relu/tb_input_features.dat @@ -0,0 +1,2 @@ +1 -2 3 -4 5 -5 1 -2 3 -4 4 -5 1 -2 3 -3 4 -5 1 -2 2 -3 4 -5 1 +-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 diff --git a/example-prjs/leaky_relu/tb_output_predictions.dat b/example-prjs/leaky_relu/tb_output_predictions.dat new file mode 100644 index 0000000000..7a7afb1664 --- /dev/null +++ b/example-prjs/leaky_relu/tb_output_predictions.dat @@ -0,0 +1,2 @@ +1 -0.6 3 -1.2 5 -1.5 1 -0.6 3 -1.2 4 -1.5 1 -0.6 3 -0.90000004 4 -1.5 1 -0.6 2 -0.90000004 4 -1.5 1 +-0.3 2 -0.90000004 4 -1.5 -1.8000001 7 -2.4 9 -3 -3.3000002 12 -3.9 14 -4.5 -4.8 17 -5.4 19 -6 -6.3 22 -6.9 24 -7.5000005 diff --git a/example-prjs/leaky_relu/vivado.py b/example-prjs/leaky_relu/vivado.py new file mode 100644 index 0000000000..2665211b27 --- /dev/null +++ b/example-prjs/leaky_relu/vivado.py @@ -0,0 +1,43 @@ + +import hls4ml +# import pprint +import yaml +import numpy as np + +print(hls4ml.__version__) + +with open('config.yml', 'r') as ymlfile: + config = yaml.safe_load(ymlfile) + +# try tweaking the reuse_factor on one layer to get different pipelining +# config['HLSConfig']['LayerName']['fc1']['ReuseFactor'] = 4 + +print('NETWORK') +print(config) + +config['OutputDir'] = 'my-Vivado-test' +config['Backend'] = 'Vivado' +config['IOType'] = 'io_stream' + +config['HLSConfig']['Model']['Strategy'] = 'Latency' +#config['HLSConfig']['Model']['Strategy'] = 'Resource' + +# default threshold is infinity +config['HLSConfig']['Model']['BramFactor'] = np.inf +# set to zero to force all weights onto (external function) interface +config['HLSConfig']['Model']['BramFactor'] = 0 + +print('CURRENT CONFIGURATION') +print('Backend='+config['Backend']) +print('IOType='+config['IOType']) +print('BramFactor={bf}'.format(bf=config['HLSConfig']['Model']['BramFactor'])) + +# pprint.pprint(config) + +#Convert it to a hls project +hls_model = hls4ml.converters.keras_to_hls(config) + +hls_model.build(vsynth=False) + +# URL for this info: https://fastmachinelearning.org/hls4ml/setup/QUICKSTART.html + From 009b79a64ca81feef2262ff08288e0cb74a76465 Mon Sep 17 00:00:00 2001 From: BLMartin99 Date: Tue, 27 Jun 2023 14:45:26 -0700 Subject: [PATCH 2/4] Made self-checking testbench --- example-prjs/leaky_relu/leaky_relu_test.cpp | 134 ++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 example-prjs/leaky_relu/leaky_relu_test.cpp diff --git a/example-prjs/leaky_relu/leaky_relu_test.cpp b/example-prjs/leaky_relu/leaky_relu_test.cpp new file mode 100644 index 0000000000..baf746e503 --- /dev/null +++ b/example-prjs/leaky_relu/leaky_relu_test.cpp @@ -0,0 +1,134 @@ +// +// rfnoc-hls-neuralnet: Vivado HLS code for neural-net building blocks +// +// Copyright (C) 2017 EJ Kreinar +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "firmware/leaky_relu.h" +#include "firmware/nnet_utils/nnet_helpers.h" +// #include "firmware/parameters.h" + +#include + +//hls-fpga-machine-learning insert bram + +//hls-fpga-machine-learning insert declare weights + +namespace nnet { + bool trace_enabled = true; + std::map *trace_outputs = NULL; + size_t trace_type_size = sizeof(double); +} + +CCS_MAIN(int argc, char *argv[]) +{ + //load input data from text file + std::ifstream fin("tb_data/tb_input_features.dat"); + //load predictions from text file + std::ifstream fpr("tb_data/tb_output_predictions.dat"); + +#ifdef RTL_SIM + std::string RESULTS_LOG = "tb_data/rtl_cosim_results.log"; +#else + std::string RESULTS_LOG = "tb_data/csim_results.log"; +#endif + std::ofstream fout(RESULTS_LOG); + +#ifndef __SYNTHESIS__ + static bool loaded_weights = false; + if (!loaded_weights) { + //hls-fpga-machine-learning insert load weights + loaded_weights = true; + } +#endif + std::string iline; + std::string pline; + + if (fin.is_open() && fpr.is_open()) { + while ( std::getline(fin,iline) && std::getline (fpr,pline) ) { + char* cstr=const_cast(iline.c_str()); + char* current; + std::vector in; + current=strtok(cstr," "); + while(current!=NULL) { + in.push_back(atof(current)); + current=strtok(NULL," "); + } + cstr=const_cast(pline.c_str()); + std::vector pr; + current=strtok(cstr," "); + while(current!=NULL) { + pr.push_back(atof(current)); + current=strtok(NULL," "); + } +// std::cout << " Input feature map size = " << in.size() << " Output predictions size = " << pr.size() << std::endl; + + //hls-fpga-machine-learning insert data + ac_channel input_1/*("input_1")*/; + nnet::copy_data(in, input_1); + ac_channel layer2_out/*("layer2_out")*/; + + //hls-fpga-machine-learning insert top-level-function + leaky_relu(input_1,layer2_out); + + result_t tmp = layer2_out[0]; + for(int i = 0; i < N_INPUT_1_1; i++) + { + if(fabs(pr[i]-tmp[i].to_double() >= 0.001)) + { + std::cout << "FAILURE" << std::endl; + std::cout << "Expected: " << pr[i] << " Actual: " << tmp[i].to_double() << std::endl; + return 1; + } + } + + //hls-fpga-machine-learning insert tb-output + nnet::print_result(layer2_out, fout); + } + fin.close(); + fpr.close(); + } else { + std::cout << "INFO: Unable to open input/predictions file, using default input." << std::endl; + + //hls-fpga-machine-learning insert zero + ac_channel input_1/*("input_1")*/; + nnet::fill_zero(input_1); + ac_channel layer2_out/*("layer2_out")*/; + + //hls-fpga-machine-learning insert top-level-function + leaky_relu(input_1,layer2_out); + + //hls-fpga-machine-learning insert output + + //hls-fpga-machine-learning insert tb-output + nnet::print_result(layer2_out, fout); + + } + + fout.close(); + std::cout << "INFO: Saved inference results to file: " << RESULTS_LOG << std::endl; + + return 0; +} From 1d58ad1e8aa9d8e58e0f2fa6ffb3648e1de7210c Mon Sep 17 00:00:00 2001 From: BLMartin99 Date: Tue, 27 Jun 2023 15:30:22 -0700 Subject: [PATCH 3/4] Added README document --- example-prjs/leaky_relu/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 example-prjs/leaky_relu/README.md diff --git a/example-prjs/leaky_relu/README.md b/example-prjs/leaky_relu/README.md new file mode 100644 index 0000000000..9abe30e92b --- /dev/null +++ b/example-prjs/leaky_relu/README.md @@ -0,0 +1,6 @@ +This is small conv2d 1 layer example. To run it: +1. Run the run_catapult.sh script. +2. Replace the leaky_relu_test.cpp in the my-Catapult-test with the leaky_relu_test.cpp a level up (if you would like the testbench to be self-checking). +3. Move tb_input_features.dat and tb_output_predictions.dat to my-Catapult-test/tb_data (if you want two pre-computed examples). + +Note: You can create your own image matrix or filter and get the predictions by editing then running leaky_relu.py. From ea7c6f7546d0ffbb00dd67688106beb3a0e9fe65 Mon Sep 17 00:00:00 2001 From: BLMartin99 Date: Thu, 29 Jun 2023 14:44:25 -0700 Subject: [PATCH 4/4] Updated vivado files --- example-prjs/leaky_relu/catapult.py | 1 - example-prjs/leaky_relu/run_vivado.sh | 4 ++-- example-prjs/leaky_relu/vivado.py | 1 - 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/example-prjs/leaky_relu/catapult.py b/example-prjs/leaky_relu/catapult.py index 79db00adbf..52a22e93b7 100644 --- a/example-prjs/leaky_relu/catapult.py +++ b/example-prjs/leaky_relu/catapult.py @@ -1,4 +1,3 @@ - import hls4ml # import pprint import yaml diff --git a/example-prjs/leaky_relu/run_vivado.sh b/example-prjs/leaky_relu/run_vivado.sh index 0315ed4697..0aeeaa3a3a 100755 --- a/example-prjs/leaky_relu/run_vivado.sh +++ b/example-prjs/leaky_relu/run_vivado.sh @@ -7,7 +7,7 @@ VENV=$HOME/venv MGC_HOME=/wv/hlsb/CATAPULT/TOT/CURRENT/aol/Mgc_home export MGC_HOME -export PATH=/wv/hlstools/python/python37/bin:$PATH:$XILINX_VIVADO/bin:$MGC_HOME/bin +export PATH=/wv/hlstools/python/python37/bin:$PATH:$XILINX_VIVADO/bin:$MGC_HOME/bin:/wv/hlstools/vivado/ixl/Vivado_HLS/2017.1/bin export LD_LIBRARY_PATH=/wv/hlstools/python/python37/lib:$XILINX_VIVADO/lib/lnx64.o:$MGC_HOME/lib export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python @@ -28,7 +28,7 @@ sed -e 's/Vivado/Catapult/g' vivado.py >catapult.py # sed -e 's/Vivado/Catapult/g' vivado.py | sed -e 's/vsynth=True/vsynth=False/g' >catapult.py # actually run HLS4ML + Vivado HLS -python3 vivado.py +python vivado.py # run just the C++ execution echo "" diff --git a/example-prjs/leaky_relu/vivado.py b/example-prjs/leaky_relu/vivado.py index 2665211b27..94da126843 100644 --- a/example-prjs/leaky_relu/vivado.py +++ b/example-prjs/leaky_relu/vivado.py @@ -1,4 +1,3 @@ - import hls4ml # import pprint import yaml