Skip to content

Commit

Permalink
Refactor (linter): add support for isort (#505)
Browse files Browse the repository at this point in the history
* chore: pre-commit is now a requirement

* chore: support for isort for import sorting

* chore: isort linted files

* CI: pre-commit checks in github actions

* Pre-commit fix, workflow works
  • Loading branch information
Giuseppe5 authored Jan 17, 2023
1 parent e4b845f commit aede0dc
Show file tree
Hide file tree
Showing 345 changed files with 2,245 additions and 1,872 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/develop_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ jobs:
if: startsWith(runner.os, 'macOS') == true

- name: Run Nox session for testing brevitas develop install and imports
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
shell: bash
run: nox -v -s tests_brevitas_install_dev-${{ matrix.python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)

- name: Run Nox session for testing brevitas_examples develop install and imports
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
shell: bash
run: nox -v -s tests_brevitas_examples_install_dev-${{ matrix.python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)


4 changes: 1 addition & 3 deletions .github/workflows/end_to_end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ jobs:
if: startsWith(runner.os, 'macOS') == true

- name: Run Nox session for end-to-end flows
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
shell: bash
run: nox -v -s tests_brevitas_end_to_end-${{ matrix.python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)


4 changes: 1 addition & 3 deletions .github/workflows/examples_pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ jobs:
if: startsWith(runner.os, 'macOS') == true

- name: Run Nox session for brevitas_examples pytest
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
shell: bash
run: nox -v -s tests_brevitas_examples_cpu-${{ matrix.python_version }}\(${{ matrix.jit_status }}\,\ pytorch_${{ matrix.pytorch_version }}\)


4 changes: 1 addition & 3 deletions .github/workflows/finn_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ jobs:
run: sudo apt-get install protobuf-compiler libprotoc-dev

- name: Run Nox session for Brevitas-FINN integration
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
shell: bash
run: nox -v -s tests_brevitas_finn_integration-${{ matrix.python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)


5 changes: 2 additions & 3 deletions .github/workflows/gen_github_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import OrderedDict as od

from utils import Action, combine_od_list

from utils import Action
from utils import combine_od_list

BASE_YML_TEMPLATE = 'base.yml.template'
PYTEST_YML = 'pytest.yml'
Expand Down Expand Up @@ -208,4 +208,3 @@ def gen_test_brevitas_end_to_end():
gen_test_brevitas_ort_integration()
gen_test_brevitas_notebook()
gen_test_brevitas_end_to_end()

4 changes: 1 addition & 3 deletions .github/workflows/notebook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ jobs:
if: startsWith(runner.os, 'macOS') == true

- name: Run Nox session for Notebook execution
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
shell: bash
run: nox -v -s tests_brevitas_notebook-${{ matrix.python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)


4 changes: 1 addition & 3 deletions .github/workflows/ort_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ jobs:
if: startsWith(runner.os, 'macOS') == true

- name: Run Nox session for Brevitas-ORT integration
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
shell: bash
run: nox -v -s tests_brevitas_ort_integration-${{ matrix.python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)


32 changes: 32 additions & 0 deletions .github/workflows/pre_commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Pre-Commit Workflow

on:
push:
branches: [ master, dev ]
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review
- review_requested

jobs:
pre_commit:
name: pre-commit
runs-on: ubuntu-latest

if: ${{ !github.event.pull_request.draft }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.7'

- name: Run pre-commit
uses: pre-commit/[email protected]
with:
extra_args: --verbose --all-files
4 changes: 1 addition & 3 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ jobs:
if: startsWith(runner.os, 'macOS') == true

- name: Run Nox session for brevitas pytest
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
if: ${{ (contains(fromJson('["1.5.1", "1.10.1", "1.13.0"]'), matrix.pytorch_version ) && github.event_name == 'pull_request' && matrix.platform == 'ubuntu-latest' ) || (github.event_name != 'pull_request') || (github.event_name == 'pull_request' && github.event.action == 'review_requested')}}
shell: bash
run: nox -v -s tests_brevitas_cpu-${{ matrix.python_version }}\(${{ matrix.jit_status }}\,\ pytorch_${{ matrix.pytorch_version }}\)


1 change: 0 additions & 1 deletion .github/workflows/rebase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ jobs:
steps:
- name: Always run
run: echo "This job is used to prevent the workflow to fail when all other jobs are skipped."

7 changes: 3 additions & 4 deletions .github/workflows/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from collections import OrderedDict as od
from functools import reduce
from string import Template
from textwrap import indent
from collections import OrderedDict as od

import yaml


NIX_NEWLINE = '\n'
# whitespaces to indent generated portions of output yaml
STEP_INDENT = 4
Expand Down Expand Up @@ -66,7 +65,7 @@ def gen_yaml(self, base_template_path, output_path):
Action.list_of_dicts_str(self.exclude_list, False, True, True), EXCLUDE_INDENT * ' ')
else:
d['exclude'] = ''

d['strategy'] = indent(Action.dict_str(self.strategy_params, False, False), 6 * ' ')
template = CustomTemplate(open(base_template_path).read())
generated_file = template.substitute(d)
Expand All @@ -76,4 +75,4 @@ def gen_yaml(self, base_template_path, output_path):


def combine_od_list(od_list):
return od(reduce(lambda l1, l2: l1 + l2, list(map(lambda d: list(d.items()), od_list))))
return od(reduce(lambda l1, l2: l1 + l2, list(map(lambda d: list(d.items()), od_list))))
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data
*.onnx

# Commented out files
*.bak
*.bak

# Installer logs
pip-log.txt
Expand Down
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

default_language_version:
python: python3

default_stages: [commit]

exclude: 'docs'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: check-added-large-files
args: [--maxkb=5000]
- id: check-ast
- id: check-json
- id: check-merge-conflict
- id: check-xml
- id: check-yaml
args: [--allow-multiple-documents]
- id: debug-statements
- id: end-of-file-fixer
- id: requirements-txt-fixer
- id: mixed-line-ending

- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
name: isort (python)
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ mechanisms:
`LICENSE` file at the top containing the specific license and restrictions
which apply to that software, or
2) It will contain specific license and restriction terms at the top of every
file.
file.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Brevitas is a PyTorch library for neural network quantization, with a focus on *

**Please note that Brevitas is a research project and not an official Xilinx product.**

If you like this project please consider ⭐ this repo, as it is the simplest and best way to support it.
If you like this project please consider ⭐ this repo, as it is the simplest and best way to support it.

If you have issues, comments, or are just looking for advices on training quantized neural networks, open an issue or a discussion.

Expand All @@ -37,7 +37,7 @@ If you adopt Brevitas in your work, please cite it as:
- *2021/10/29* - Release version 0.7.0, see the [release notes](https://github.com/Xilinx/brevitas/releases/tag/v0.7.0).
- *2021/06/04* - Release version 0.6.0, see the [release notes](https://github.com/Xilinx/brevitas/releases/tag/v0.6.0).
- *2021/05/24* - Release version 0.5.1, fix a bunch of minor issues. See [release notes](https://github.com/Xilinx/brevitas/releases/tag/v0.5.1).
- *2021/05/06* - Release version 0.5.0, see the [release notes](https://github.com/Xilinx/brevitas/releases/tag/v0.5.0).
- *2021/05/06* - Release version 0.5.0, see the [release notes](https://github.com/Xilinx/brevitas/releases/tag/v0.5.0).
- *2021/03/15* - Release version 0.4.0, add support for \_\_torch_function\_\_ to QuantTensor.
- *2021/03/04* - Release version 0.3.1, fix bug w/ act initialization from statistics w/ IGNORE_MISSING_KEYS=1.
- *2021/03/01* - Release version 0.3.0, implements enum and shape solvers within extended dependency injectors. This allows declarative quantizers to be self-contained.
Expand All @@ -61,9 +61,3 @@ pip install brevitas
## Getting started

Check out available info at https://xilinx.github.io/brevitas/getting_started .






2 changes: 1 addition & 1 deletion docsrc/source/about.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ Cite as
publisher = {Zenodo},
doi = {10.5281/zenodo.3333552},
url = {https://doi.org/10.5281/zenodo.3333552}
}
}
2 changes: 0 additions & 2 deletions docsrc/source/api_reference/brevitas.function.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ brevitas.function.shape module
:members:
:undoc-members:
:show-inheritance:


2 changes: 1 addition & 1 deletion docsrc/source/architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,4 @@ TODO
Losses
------

TODO
TODO
3 changes: 2 additions & 1 deletion docsrc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import os
import sys

import brevitas

sys.path.insert(0, os.path.abspath(brevitas.__file__))
Expand Down Expand Up @@ -93,4 +94,4 @@
"numpy": ("http://docs.scipy.org/doc/numpy/", None),
"python": ("https://docs.python.org/", None),
"torch": ("https://pytorch.org/docs/master/", None),
}
}
10 changes: 5 additions & 5 deletions docsrc/source/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ F.A.Q.
**Q: Pytorch supports quantization-aware training. Why should I use
Brevitas?**

**A:** Quantization in Pytorch is designed to target specific backends
supported by PyTorch itself (currently FBGEMM and qnnpack).
**A:** Quantization in Pytorch is designed to target specific backends
supported by PyTorch itself (currently FBGEMM and qnnpack).

Brevitas is designed as a platform to implement novel quantization
algorithms to target a variety of hardware backends adhering to a loose
set of assumptions (i.e. uniform affine quantization).
set of assumptions (i.e. uniform affine quantization).

**Q: How can I train X/Y and run it on hardware W/Z? I can't find any
documentation.**
Expand Down Expand Up @@ -41,11 +41,11 @@ make my model faster at inference time. What I am doing wrong?**
**A:** Brevitas is concerned with modelling a reduced precision
data-path, it does not provide inference-time acceleration on its own.
To achieve acceleration, you should export your Brevitas model to a
downstream toolchain / backend. See the
downstream toolchain / backend. See the

**Q: My (C/G/T)PU supports float16 / bfloat16 / bfloat19 training. Can I
use it to train with Brevitas?**

**A:** Datatypes outside of float32 at training time have not been tested. That includes training on TPU / Pytorch-XLA.
Do the math in terms of which reduced-precision integers can reasonably fit in a reduced-precision
floating-point format at training time, and use at your own risk.
floating-point format at training time, and use at your own risk.
20 changes: 10 additions & 10 deletions docsrc/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ The result is the following:
# ... training ...
A neural network with 4 bits weights and floating-point
activations can provide an advantage in terms of model storage,
but it doesn't provide any advantage in terms of compute,
as the weights need to be converted to float at runtime first.
In order to make more practical, we want to quantize activations too.
activations can provide an advantage in terms of model storage,
but it doesn't provide any advantage in terms of compute,
as the weights need to be converted to float at runtime first.
In order to make more practical, we want to quantize activations too.

Weights and activations quantization, float biases
--------------------------------------------------

We now quantize both weights and activations to 4 bits, while keeping biases in floating-point.
We now quantize both weights and activations to 4 bits, while keeping biases in floating-point.
In order to do so, we replace ``torch.nn.ReLU`` with
``brevitas.nn.QuantReLU``, specifying ``bit_width=4``.
``brevitas.nn.QuantReLU``, specifying ``bit_width=4``.
Additionally, in order to quantize the very first input, we introduce a
``brevitas.nn.QuantIdentity`` at the beginning of the network. The end
result is the following:
Expand Down Expand Up @@ -166,7 +166,7 @@ Weights, activations, biases quantization
Compared to the previous scenario:
- We now set ``return_quant_tensor=True`` in every quantized activations to propagate a ``QuantTensor`` to the next layer. This informs each ``QuantLinear`` or ``QuantConv2d`` of how the input passed in has been quantized.
- We now set ``return_quant_tensor=True`` in every quantized activations to propagate a ``QuantTensor`` to the next layer. This informs each ``QuantLinear`` or ``QuantConv2d`` of how the input passed in has been quantized.
- A ``QuantTensor`` is just a tensor-like data structure providing metadata about how a tensor has been quantized, similar to a `torch.qint` dtype, but training friendly. Setting ``return_quant_tensor=True`` does not affect the way quantization is performed, it only changes the way the output is represented.
- We enable bias quantization by setting the `Int32Bias` quantizer. What it does is to perform bias quantization with ```bias_scale = input_scale * weight_scale``, as it commonly done across inference toolchains. This is why we have to set ``return_quant_tensor=True``: each layer with ``Int32Bias`` can read the input scale from the ``QuantTensor`` passed in and use for bias quantization.
- ``torch`` operations that are algorithmically invariant to quantization, such as `F.max_pool2d`, can propagate QuantTensor through them without extra changes.
Expand All @@ -175,8 +175,8 @@ Export to ONNX
--------------

Brevitas does not perform any low-precision acceleration on its own. For that to happen, the model need to be exported first to an inference toolchain through some intermediate representation like ONNX.
One popular way to represent 8-bit quantization within ONNX is through the `QDQ format <https://onnxruntime.ai/docs/performance/quantization.html#onnx-quantization-representation-format>`_.
Brevitas extends *QDQ* to **QCDQ**, inserting a `Clip` node to represent quantization to *<= 8 bits*. We can then export all previous defined model to *QCDQ*.
One popular way to represent 8-bit quantization within ONNX is through the `QDQ format <https://onnxruntime.ai/docs/performance/quantization.html#onnx-quantization-representation-format>`_.
Brevitas extends *QDQ* to **QCDQ**, inserting a `Clip` node to represent quantization to *<= 8 bits*. We can then export all previous defined model to *QCDQ*.
The interface of the export function matches the `torch.onnx.export` function, and accepts all its kwargs:

.. code-block:: python
Expand All @@ -196,4 +196,4 @@ The interface of the export function matches the `torch.onnx.export` function, a
Where to go from here
---------------------

Check out the :doc:`Tutorials section </tutorials/index>` for more information on things like ONNX export, quantized recurrent layers, quantizers, or a more detailed overview of the library in the TVMCon tutorial.
Check out the :doc:`Tutorials section </tutorials/index>` for more information on things like ONNX export, quantized recurrent layers, quantizers, or a more detailed overview of the library in the TVMCon tutorial.
2 changes: 1 addition & 1 deletion docsrc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ It provides a platform both for researchers interested in implementing new quant
Brevitas supports a super-set of quantization schemes implemented across various frameworks and compilers under a single unified API.
For certain combinations of layers and types of of quantization inference acceleration is supported by exporting to *FINN*, *onnxruntime* or *Pytorch*'s own quantized operators.

Brevitas has been successfully adopted both in various research projects as well as in large-scale commercial deployments targeting CPUs, GPUs, and custom accelerators running on Xilinx FPGAs. The general quantization style implemented is affine quantization, with a focus on uniform quantization. Non-uniform quantization is currently not supported out-of-the-box.
Brevitas has been successfully adopted both in various research projects as well as in large-scale commercial deployments targeting CPUs, GPUs, and custom accelerators running on Xilinx FPGAs. The general quantization style implemented is affine quantization, with a focus on uniform quantization. Non-uniform quantization is currently not supported out-of-the-box.
Loading

0 comments on commit aede0dc

Please sign in to comment.