Skip to content

Commit 3ec6c5a

Browse files
committed
update qonnx documentation
1 parent 9cbf0f1 commit 3ec6c5a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

docs/advanced/qonnx.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
==============
2+
ONNX and QONNX
3+
==============
4+
5+
Parsing of ONNX and QONNX models is made in conjunction with the `qonnx <https://github.com/fastmachinelearning/qonnx>`_ package, even if it no quantization is used. This is a common initial parser shared with the AMD/Xilinx FINN project. The first step is to do constant folding, shape inference, etc., on the ONNX graph, commonly known as `cleaning`. If a model has convolution layers, the model also needs to be converted to a channels-last format, since that is what hls4ml mainly supports. The ``qonnx`` package also provides a number of additional transforms that may need to be used. For example, ``Gemm`` nodes need to converted to ``MatMul`` and ``Add`` nodes.
6+
7+
There are command-line based versions of cleaning and channels-last conversion:
8+
9+
.. code-block:: bash
10+
11+
$ qonnx_clean filename.onnx
12+
$ qonnx_to_channels_last filename_clean.onnx
13+
$ qonnx_clean filename_clean_channels_last.onnx # good to do a clean again as a last step
14+
15+
Things can similarly be done in python. This method is usually easier if you additionally need to call other transforms. An example is given below which also calls the ``GemmToMatMul`` converter:
16+
17+
.. code-block:: python
18+
19+
model = ModelWrapper('filename.onnx')
20+
model = qonnx.util.cleanup.cleanup_model(model)
21+
model = model.transform(ConvertToChannelsLastAndClean())
22+
model = model.transform(GemmToMatMul())
23+
model = qonnx.util.cleanup.cleanup_model(model)
24+
25+
``ModelWrapper`` is defined in ``qonnx.core.modelwrapper``. More information on the ``qonnx`` package can be found at the `QONNX documentation page <https://qonnx.readthedocs.io/en/latest/index.html>`_.
26+
27+
28+
The next steps are very similar to if you are using a Keras model:
29+
30+
.. code-block:: python
31+
32+
config = hls4ml.utils.config.config_from_onnx_model(
33+
model, granularity='name', backend='Vitis', default_precision='fixed<16,6>'
34+
)
35+
# modify the config as desired
36+
hls_model = hls4ml.converters.convert_from_onnx_model(
37+
model,
38+
output_dir='my-hls-test',
39+
io_type='io_stream',
40+
backend='Vitis',
41+
hls_config=config,
42+
)
43+
hls_model.compile()
44+
45+
Note, unlike the Keras version, "name" granularity is the default for ``config_from_onnx_model``, and it must be used for QONNX models. Unquantized ONNX models can use "model" if so desired, but generally there is no benefit.
46+
47+
One can subsequently call the ``predict`` function to check the performance or build the project.
48+
49+
Note that ``execute_onnx`` in ``qonnx.core.onnx_exec`` can be use to run the QONNX graphs directly, and it also provides the values at intermediate layers for validating the model (tracing).
50+
51+
Quant nodes
52+
===========
53+
54+
Documentation for quant nodes is provided in the `qonnx package <https://github.com/fastmachinelearning/qonnx/tree/main/docs/qonnx-custom-ops>`_. Note that currently hls4ml only supports the `Quant operator <https://github.com/fastmachinelearning/qonnx/tree/main/docs/qonnx-custom-ops/quant_op.md>`_. Also, not all legal ``Quant`` configurations are parsable by hls4ml or synthesizable. The ``scale``, ``zeropt``, and ``bitwidth`` values must be constant (though not necessarily scalar for the ``scale`` and ``zeropt``).
55+
56+
Generally if the ``zeropt`` is 0 and the ``scale`` is a scalar power of 2, hls4ml uses ``ap_fixed`` or ``ac_fixed`` types (depending on the backend) to represent the quantizations. In other cases, the ``scale`` and ``zeropt`` need to be explicitly handled by hls4ml, and there is more of a chance of hls4ml not being able to process the input. (Please report any issues that you find.)

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
:hidden:
2323
:caption: Advanced Features
2424

25+
advanced/qonnx
2526
advanced/fifo_depth
2627
advanced/extension
2728
advanced/accelerator

0 commit comments

Comments
 (0)