Package: samo
SAMO: Streaming Architecture Mapping OptimiserThe SAMO framework provides a method of optimising the mapping of a Convolutional Neural Network model onto an FPGA platform for Streaming Architecture frameworks. Both a Simulated Annealing and Brute Force optimiser are implemented. We currently support the following frameworks:FINNHLS4MLfpgaconvnet-hlsandfpgaconvnet-modelInstallationYou can install this package using:python -m pip install samoUsageThe general usage of the SAMO tool can be seen by runningpython -m samo --help.Example platform configurations are given in theplatformsdirectory and example CNN models can be generated by runningpython scripts/generate_networks.py.fpgaConvNetFirst off, thefpgaconvnet-modelandfpgaconvnet-hlsneed to be installed.python -m pip install fpgaconvnet-model fpgaconvnet-hlsThe command-line interface can be used to optimise the model for a given platform. For example:python -m samo --optimiser annealing --model models/model.onnx \
    --backend fpgaconvnet --platform platforms/platform.json \
    --output-path outputs/The optimised configuration is saved underoutputs/same.json. This can be used to generate hardware withfpgaconvnet-hls.fromfpgaconvnet.hls.generate.networkimportGenerateNetwork# create instance of the networkgen_net=GenerateNetwork("model-name","outputs/same.json","models/model.onnx")# iterate over partitions in networkforiinrange(len(gen_net.partitions_generator)):# generate hardware and create HLS projectgen_net.create_partition_project(i)# run HLS synthesisgen_net.generate_partition_hardware(i)FINNIn order to run the optimiser with the FINN toolflow, the first step is to download the following forkgit clone https://github.com/Yu-Zhewen/finn.git
cd finn
git checkout 4cc0b6fdae2f5c06f0b5bcc6fa45fba4d8b69111As FINN requires docker, set SAMO_DIR to the path of SAMO in run_docker.sh, before entering the docker.bash run_docker.shWithin the docker, generate the FINN-ONNX through the following steps.cd ../samo
cp models/${network}.onnx outputs/saved/finn/${network}.onnx
cp ../finn/notebooks/samo/config/${network}.json ../finn/notebooks/samo/config.json
jupyter nbconvert --to notebook --execute ../finn/notebooks/samo/pre_optimiser_steps.ipynb
mv ../finn/notebooks/samo/pre_optimiser_steps.nbconvert.ipynb outputs/saved/finn/${network}_pre_optimiser_steps.nbconvert.ipynbTo optimise the CNN model in the FINN-ONNX format, you need to do:python -m samo --optimiser annealing --model outputs/saved/finn/${network}_pre_optimiser.onnx  \
    --backend finn --platform platforms/zedboard.json \
    --output-path outputs/saved/finn/${network}_post_optimiser.onnxFinally, the following command is used to generate the hardware.jupyter nbconvert --to notebook --execute ../finn/notebooks/samo/post_optimiser_steps.ipynbHLS4MLThis tool can be used to generate optimised designs for theHLS4MLframework. SAMO tunes thereuse-factorfor layers of the CNN model, and generates aResourcedriven design.To optimise a keras model for a given platform, run the following:python -m samo --optimiser annealing --model models/model.keras \
    --backend hls4ml --platform platforms/zedboard.json \
    --output-path outputs/model_hls4ml.jsonThe previous command generates a configuration file (outputs/model_hls4ml.json), which can be used by the HLS4ML to generate hardware. To do this, you will need to use the HLS4ML API to convert this configuration file into a HLS project.importhls4mlfromtensorflowimportkeras# load the configurationwithopen("outputs/model_hls4ml.json","r")asf:config=json.load(f)# load the platformwithopen("platforms/zedboard.json","r")asf:platform=json.load(f)# load the keras modelmodel=keras.models.load_model("models/model.keras")# create the hls modelhls_model=hls4ml.converters.convert_from_keras_model(model,hls_config=config,output_dir="outputs/hls4ml_prj",io_type="io_stream",fpga_part=platform["part"])# build the HLS projecthls_model.build(csim=True,cosim=True)Feel free to post an issue if you have any questions or problems!