From c536152a761b8d7ff1f48125e8ff1e1277af347e Mon Sep 17 00:00:00 2001 From: BLMartin99 Date: Tue, 27 Jun 2023 14:29:57 -0700 Subject: [PATCH] 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 +