Brevitas
Getting Started
Installation
A. Pip
pip install brevitas
B. FINN Associated
BNN-PYNQ Experiments
Training
## Preparing an experiment directory mkdir -p /tmp/finn_dev_`whoami`/brevitas
## Run Training #BREVITAS_JIT=1 brevitas_bnn_pynq_train --network LFC_1W1A --experiments /tmp/finn_dev_`whoami`/brevitas #BREVITAS_JIT=1 brevitas_bnn_pynq_train --network TFC_1W1A --experiments /tmp/finn_dev_`whoami`/brevitas BREVITAS_JIT=1 brevitas_bnn_pynq_train --network TFC_1W1A --experiments /tmp/finn_dev_`whoami`/brevitas --gpus None
2023-07-25 07:10:50,735 Epoch: [1][0/600] Time 1.072 (1.072) Data 0.407 (0.407) Loss 1.9433 (1.9433) Prec@1 13.000 (13.000) Prec@5 46.000 (46.000) 2023-07-25 07:10:50,964 Epoch: [1][10/600] Time 0.015 (0.112) Data 0.007 (0.043) Loss 1.1267 (1.5350) Prec@1 31.000 (22.000) Prec@5 76.000 (61.000) 2023-07-25 07:10:51,196 Epoch: [1][20/600] Time 0.017 (0.066) Data 0.005 (0.026) Loss 0.6909 (1.2536) Prec@1 30.000 (24.667) Prec@5 80.000 (67.333) ... 2023-07-25 07:11:04,176 Epoch: [1][580/600] Time 0.015 (0.017) Data 0.007 (0.008) Loss 0.1151 (0.2348) Prec@1 84.000 (68.831) Prec@5 99.000 (94.373) 2023-07-25 07:11:04,400 Epoch: [1][590/600] Time 0.015 (0.017) Data 0.007 (0.008) Loss 0.1125 (0.2328) Prec@1 85.000 (69.100) Prec@5 99.000 (94.450) 2023-07-25 07:11:04,520 Epoch: [1][599/600] Time 0.009 (0.017) Data 0.002 (0.008) Loss 0.1027 (0.2307) Prec@1 86.000 (69.377) Prec@5 98.000 (94.508) 2023-07-25 07:11:04,777 Test: [0/100] Model Time 0.043 (0.043) Loss Time 0.000 (0.000) Loss 0.0595 (0.0595) Prec@1 91.000 (91.000) Prec@5 100.000 (100.000) 2023-07-25 07:11:04,798 Test: [1/100] Model Time 0.013 (0.028) Loss Time 0.000 (0.000) Loss 0.0794 (0.0695) Prec@1 86.000 (88.500) Prec@5 99.000 (99.500) 2023-07-25 07:11:04,817 Test: [2/100] Model Time 0.014 (0.023) Loss Time 0.000 (0.000) Loss 0.1139 (0.0843) Prec@1 85.000 (87.333) Prec@5 97.000 (98.667) ... 2023-07-25 07:11:06,772 Test: [97/100] Model Time 0.004 (0.013) Loss Time 0.000 (0.000) Loss 0.1443 (0.0818) Prec@1 80.000 (87.714) Prec@5 99.000 (98.602) 2023-07-25 07:11:06,783 Test: [98/100] Model Time 0.004 (0.013) Loss Time 0.000 (0.000) Loss 0.1537 (0.0825) Prec@1 75.000 (87.586) Prec@5 98.000 (98.596) 2023-07-25 07:11:06,792 Test: [99/100] Model Time 0.004 (0.013) Loss Time 0.000 (0.000) Loss 0.1126 (0.0828) Prec@1 81.000 (87.520) Prec@5 95.000 (98.560) 2023-07-25 07:11:06,837 Saving checkpoint model to /tmp/finn_dev_user/brevitas/TFC_1W1A_1W1A_20230725_071042/checkpoints/best.tar
Information - How to Customize Network
cd ~/finn/deps/brevitas/src/brevitas_examples/bnn_pynq
## Update the configuration file: e.g., from 3 layers of 64 neurons with MNIST to 4 layers of 32 neurons with a LOCAL dataset vi cfg/tfc_1w1a.ini ====================================================== [MODEL] ARCH: FC #DATASET: MNIST DATASET: LOCAL IN_CHANNELS: 1 #OUT_FEATURES = [64, 64, 64] OUT_FEATURES = [32, 32, 32, 32] NUM_CLASSES: 10 ====================================================== [QUANT] WEIGHT_BIT_WIDTH: 1 ACT_BIT_WIDTH: 1 IN_BIT_WIDTH: 1 ======================================================
## Add a logic of the LOCAL dataset to trainer.py (as needed) vi trainer.py ====================================================== elif dataset == 'LOCAL' : transform_train = transform_to_tensor builder = ... ======================================================
## Update the model class in FC.py for major modifications (as needed) vi models/FC.py #vi models/CNV.py
Troubleshooting - No NVIDIA Driver
## RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx
## Check if the gpus argument is None BREVITAS_JIT=1 brevitas_bnn_pynq_train --network TFC_1W1A --experiments /tmp/finn_dev_`whoami`/brevitas --gpus None
Evaluating
#BREVITAS_JIT=1 brevitas_bnn_pynq_train --evaluate --network LFC_1W1A --resume /tmp/finn_dev_user/brevitas/LFC_1W1A_1W1A_20230725_071042/checkpoints/best.tar #BREVITAS_JIT=1 brevitas_bnn_pynq_train --evaluate --network TFC_1W1A --resume /tmp/finn_dev_user/brevitas/TFC_1W1A_1W1A_20230725_071042/checkpoints/best.tar BREVITAS_JIT=1 brevitas_bnn_pynq_train --evaluate --network TFC_1W1A --resume /tmp/finn_dev_user/brevitas/TFC_1W1A_1W1A_20230725_071042/checkpoints/best.tar --gpus None
2023-07-25 08:02:21,792 Test: [0/100] Model Time 0.207 (0.207) Loss Time 0.142 (0.142) Loss 0.0595 (0.0595) Prec@1 91.000 (91.000) Prec@5 100.000 (100.000) 2023-07-25 08:02:21,814 Test: [1/100] Model Time 0.016 (0.112) Loss Time 0.000 (0.071) Loss 0.0794 (0.0695) Prec@1 86.000 (88.500) Prec@5 99.000 (99.500) 2023-07-25 08:02:21,834 Test: [2/100] Model Time 0.012 (0.078) Loss Time 0.000 (0.047) Loss 0.1139 (0.0843) Prec@1 85.000 (87.333) Prec@5 97.000 (98.667) ... 2023-07-25 08:02:23,707 Test: [97/100] Model Time 0.004 (0.013) Loss Time 0.000 (0.002) Loss 0.1443 (0.0818) Prec@1 80.000 (87.714) Prec@5 99.000 (98.602) 2023-07-25 08:02:23,717 Test: [98/100] Model Time 0.005 (0.013) Loss Time 0.000 (0.002) Loss 0.1537 (0.0825) Prec@1 75.000 (87.586) Prec@5 98.000 (98.596) 2023-07-25 08:02:23,727 Test: [99/100] Model Time 0.004 (0.013) Loss Time 0.000 (0.002) Loss 0.1126 (0.0828) Prec@1 81.000 (87.520) Prec@5 95.000 (98.560)
Convert to FINN-ONNX
python
import torch import brevitas.onnx from brevitas_examples.bnn_pynq.models import model_with_cfg model, cfg = model_with_cfg('TFC_1W1A', False) model.load_state_dict(torch.load('/tmp/finn_dev_user/brevitas/TFC_1W1A_1W1A_20230725_071042/checkpoints/best.tar')['state_dict']) model.eval() brevitas.onnx.export_finn_onnx(model, (1, 1, 28, 28), 'TFC_1W1A.onnx')
References
Acknowledgments
Daiphys is a professional-service company for research and development of leading-edge technologies in science and engineering.
Get started accelerating your business through our deep expertise in R&D with AI, quantum computing, and space development; please get in touch with Daiphys today!
Daiphys Technologies LLC - https://www.daiphys.com/