Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/validation steps #100

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors
============

* Mirza Mrahorovic (@mmrahorovic)
* Mirza Mrahorovic (@mmrahorovic) (maintainer)
* Jakoba Petri-Koenig (@auphelia)
* Yaman Umuroglu (@maltanar)
* Lucian Petrica (@quetric)
Expand All @@ -16,4 +16,3 @@ Contributors
* Matthias Gehre (@mgehre-amd)
* NaelF (@NaelF)
* Tim Paine (@timkpaine)
* Hannah Yan (@hannahxy13)
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<a>
<img src="https://img.shields.io/github/v/release/Xilinx/finn-examples?color=%09%23228B22&display_name=tag&label=Release" />
</a>
<a href="https://github.com/Xilinx/finn/tree/v0.10.1">
<img src="https://img.shields.io/badge/FINN-v0.10.1-blue" />
<a href="https://github.com/Xilinx/finn/tree/v0.9">
<img src="https://img.shields.io/badge/FINN-v0.9.0-blue" />
</a>
<a href="https://github.com/Xilinx/PYNQ/tree/v3.0.1">
<img src="https://img.shields.io/badge/PYNQ-v3.0.1-blue" />
</a>
<a href="https://www.xilinx.com/support/download/index.html/content/xilinx/en/downloadNav/vivado-design-tools/2022-1.html">
<img src="https://img.shields.io/badge/Vivado%2FVitis-v2022.2-blue" />
<img src="https://img.shields.io/badge/Vivado%2FVitis-v2022.1-blue" />
</a>
</p>

Expand Down Expand Up @@ -79,7 +79,7 @@ jupyter-notebook --no-browser --allow-root --port=8888

First, create & source a virtual environment:
```shell
conda create -n <virtual-env> python=3.8
conda create -n <virtual-env> python=3.10
conda activate <virtual-env>
```

Expand Down Expand Up @@ -128,14 +128,14 @@ dummy_out = accel.execute(dummy_in)
| CIFAR-10 | CNV (VGG-11-like) | several variants:<br>1/2-bit weights/activations | Pynq-Z1<br>ZCU104<br>Ultra96<br>U250 | Pynq-Z1<br>ZCU104<br>Ultra96<br>U250 |
| MNIST | 3-layer fully-connected | several variants:<br>1/2-bit weights/activations | Pynq-Z1<br>ZCU104<br>Ultra96<br>U250 | Pynq-Z1<br>ZCU104<br>Ultra96<br>U250 |
| ImageNet | MobileNet-v1 | 4-bit weights & activations<br>8-bit first layer weights | Alveo U250 | Alveo U250 |
| ImageNet | ResNet-50 | 1-bit weights 2-bit activations<br>4-bit residuals<br>8-bit first/last layer weights | Alveo U250 | Alveo U250 |
| ImageNet | ResNet-50 | 1-bit weights 2-bit activations<br>4-bit residuals<br>8-bit first/last layer weights | Alveo U250 | - |
| RadioML 2018 | 1D CNN (VGG10) | 4-bit weights & activations | ZCU104 | ZCU104 |
| MaskedFace-Net | [BinaryCoP](https://arxiv.org/pdf/2102.03456)<br/>*Contributed by TU Munich+BMW* | 1-bit weights & activations | Pynq-Z1 | Pynq-Z1 |
| Google Speech Commands v2 | 3-layer fully-connected | 3-bit weights & activations | Pynq-Z1 | Pynq-Z1 |
| UNSW-NB15 | 4-layer fully-connected | 2-bit weights & activations | Pynq-Z1 <br> ZCU104 <br> Ultra96 | Pynq-Z1 <br> ZCU104 <br> Ultra96 |
| GTSRB | CNV (VGG-11-like) | 1-bit weights & activations | Pynq-Z1 | Pynq-Z1 |

*Please note that you can target other boards (such as the Pynq-Z2 or ZCU102) by changing the build script manually, but these accelerators have not been tested.*
*Please note that the build flow for ResNet-50 for the Alveo U250 has known issues and we're currently working on resolving them. However, you can still execute the associated notebook, as we provide a pre-built FPGA bitfile generated with an older Vivado (/FINN) version targeting the [xilinx_u250_xdma_201830_2](https://www.xilinx.com/products/boards-and-kits/alveo/package-files-archive/u250-2018-3-1.html) platform.* <br>
*Furthermore, please note that you can target other boards (such as the Pynq-Z2 or ZCU102) by changing the build script manually, but these accelerators have not been tested.*

We welcome community contributions to add more examples to this repo!

Expand Down
26 changes: 23 additions & 3 deletions build/bnn-pynq/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"cnv-w2a2",
]

verif_en = os.getenv("VERIFICATION_EN", "0")

# which platforms to build the networks for
zynq_platforms = ["Pynq-Z1", "Ultra96", "ZCU104"]
alveo_platforms = ["U250"]
Expand Down Expand Up @@ -86,15 +88,33 @@ def platform_to_shell(platform):
board=platform_name,
shell_flow_type=shell_flow_type,
vitis_platform=vitis_platform,
generate_outputs=[build_cfg.DataflowOutputType.BITFILE],
generate_outputs=[
build_cfg.DataflowOutputType.BITFILE,
build_cfg.DataflowOutputType.STITCHED_IP,
],
save_intermediate_models=True,
default_swg_exception=True,
specialize_layers_config_file="specialize_layers_config/%s_specialize_layers.json"
% model_name,
)
model_file = "models/%s.onnx" % model_name
# launch FINN compiler to build
build.build_dataflow_cfg(model_file, cfg)

if verif_en == "1":
# Build the model with verification
import sys

sys.path.append(os.path.abspath(os.getenv("FINN_EXAMPLES_ROOT") + "/ci/"))
from verification_funcs import init_verif, verify_build_output

cfg.verify_steps, cfg.verify_input_npy, cfg.verify_expected_output_npy = init_verif(
model_name
)
build.build_dataflow_cfg(model_file, cfg)
verify_build_output(cfg, model_name)
else:
# Build the model without verification
build.build_dataflow_cfg(model_file, cfg)

# copy bitfiles into release dir if found
bitfile_gen_dir = cfg.output_dir + "/bitfile"
files_to_check_and_copy = [
Expand Down
31 changes: 25 additions & 6 deletions build/cybersecurity-mlp/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
from finn.util.basic import alveo_default_platform
import os
import shutil
from custom_steps import custom_step_mlp_export

# Define model name
model_name = "unsw_nb15-mlp-w2a2"

verif_en = os.getenv("VERIFICATION_EN", "0")

# Which platforms to build the networks for
zynq_platforms = ["Pynq-Z1", "Ultra96", "ZCU104"]
Expand All @@ -50,9 +56,6 @@ def platform_to_shell(platform):
raise Exception("Unknown platform, can't determine ShellFlowType")


# Define model name
model_name = "unsw_nb15-mlp-w2a2"

# Create a release dir, used for finn-examples release packaging
os.makedirs("release", exist_ok=True)

Expand Down Expand Up @@ -85,13 +88,29 @@ def platform_to_shell(platform):
build_cfg.DataflowOutputType.ESTIMATE_REPORTS,
build_cfg.DataflowOutputType.BITFILE,
build_cfg.DataflowOutputType.DEPLOYMENT_PACKAGE,
build_cfg.DataflowOutputType.STITCHED_IP,
jmonks-amd marked this conversation as resolved.
Show resolved Hide resolved
],
save_intermediate_models=True,
jmonks-amd marked this conversation as resolved.
Show resolved Hide resolved
)

model = "models/%s.onnx" % model_name
# Export MLP model to FINN-ONNX
model = custom_step_mlp_export(model_name)
# Launch FINN compiler to generate bitfile
build.build_dataflow_cfg(model, cfg)
if verif_en == "1":
# Build the model with verification
import sys

sys.path.append(os.path.abspath(os.getenv("FINN_EXAMPLES_ROOT") + "/ci/"))
from verification_funcs import init_verif, verify_build_output

cfg.verify_steps, cfg.verify_input_npy, cfg.verify_expected_output_npy = init_verif(
model_name
)
build.build_dataflow_cfg(model, cfg)
verify_build_output(cfg, model_name)
else:
# Build the model without verification
build.build_dataflow_cfg(model, cfg)

# Copy bitfiles into release dir if found
bitfile_gen_dir = cfg.output_dir + "/bitfile"
filtes_to_check_and_copy = ["finn-accel.bit", "finn-accel.hwh", "finn-accel.xclbin"]
Expand Down
118 changes: 118 additions & 0 deletions build/cybersecurity-mlp/custom_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright (C) 2023, Advanced Micro Devices, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of FINN nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import numpy as np
import pkg_resources as pk
import os

from brevitas.nn import QuantLinear, QuantReLU, QuantIdentity
import torch
import torch.nn as nn
import brevitas.onnx as bo
from qonnx.core.modelwrapper import ModelWrapper
from qonnx.core.datatype import DataType


# Define export wrapper
class CybSecMLPForExport(nn.Module):
def __init__(self, my_pretrained_model):
super(CybSecMLPForExport, self).__init__()
self.pretrained = my_pretrained_model
self.qnt_output = QuantIdentity(
quant_type="binary", scaling_impl_type="const", bit_width=1, min_val=-1.0, max_val=1.0
)

def forward(self, x):
# assume x contains bipolar {-1,1} elems
# shift from {-1,1} -> {0,1} since that is the
# input range for the trained network
x = (x + torch.tensor([1.0]).to("cpu")) / 2.0
out_original = self.pretrained(x)
out_final = self.qnt_output(out_original) # output as {-1, 1}
return out_final


def custom_step_mlp_export(model_name):
# Define model parameters
input_size = 593
hidden1 = 64
hidden2 = 64
hidden3 = 64
weight_bit_width = 2
act_bit_width = 2
num_classes = 1

# Create model definition from Brevitas library
model = nn.Sequential(
QuantLinear(input_size, hidden1, bias=True, weight_bit_width=weight_bit_width),
nn.BatchNorm1d(hidden1),
nn.Dropout(0.5),
QuantReLU(act_bit_width=act_bit_width),
QuantLinear(hidden1, hidden2, bias=True, weight_bit_width=weight_bit_width),
nn.BatchNorm1d(hidden2),
nn.Dropout(0.5),
QuantReLU(bit_width=act_bit_width),
QuantLinear(hidden2, hidden3, bias=True, weight_bit_width=weight_bit_width),
nn.BatchNorm1d(hidden3),
nn.Dropout(0.5),
QuantReLU(bit_width=act_bit_width),
QuantLinear(hidden3, num_classes, bias=True, weight_bit_width=weight_bit_width),
)

# Load pre-trained weights
assets_dir = pk.resource_filename("finn.qnn-data", "cybsec-mlp/")
trained_state_dict = torch.load(assets_dir + "/state_dict.pth")["models_state_dict"][0]
model.load_state_dict(trained_state_dict, strict=False)

# Network surgery: pad input size from 593 to 600 and convert bipolar to binary
W_orig = model[0].weight.data.detach().numpy()
W_new = np.pad(W_orig, [(0, 0), (0, 7)])
model[0].weight.data = torch.from_numpy(W_new)

model_for_export = CybSecMLPForExport(model)

# Create directory to save model
os.makedirs("models", exist_ok=True)
ready_model_filename = "models/%s.onnx" % model_name
input_shape = (1, 600)

# create a QuantTensor instance to mark input as bipolar during export
input_a = np.random.randint(0, 1, size=input_shape).astype(np.float32)
input_a = 2 * input_a - 1
scale = 1.0
input_t = torch.from_numpy(input_a * scale)

# Export to ONNX
bo.export_qonnx(model_for_export, export_path=ready_model_filename, input_t=input_t)

# Set input datatype for FINN's InferDataType transformation to infer the right datatypes
model = ModelWrapper(ready_model_filename)
model.set_tensor_datatype(model.graph.input[0].name, DataType["BIPOLAR"])
model.save(ready_model_filename)

return ready_model_filename
4 changes: 0 additions & 4 deletions build/cybersecurity-mlp/models/download-model.sh

This file was deleted.

Loading
Loading