Skip to content

Commit 0055bb6

Browse files
committed
updates
1 parent 0793293 commit 0055bb6

File tree

9 files changed

+61
-28
lines changed

9 files changed

+61
-28
lines changed

docs/advanced/extension.rst

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ For concreteness, let's say our custom layer ``KReverse`` is implemented in Kera
2626
.. code-block:: Python
2727
2828
# Keras implementation of a custom layer
29-
class KReverse(tf.keras.layers.Layer):
29+
class KReverse(keras.layers.Layer):
3030
'''Keras implementation of a hypothetical custom layer'''
3131
3232
def __init__(self):
3333
super().__init__()
3434
3535
def call(self, inputs):
36-
return tf.reverse(inputs, axis=[-1])
36+
return inputs[..., ::-1]
3737
3838
def get_config(self):
3939
return super().get_config()
@@ -58,19 +58,42 @@ This parser reads the attributes of the Keras layer instance and populates a dic
5858
It also returns a list of output shapes (one sjape for each output).
5959
In this case, there a single output with the same shape as the input.
6060

61-
.. code-block:: Python
61+
.. tabs::
62+
.. tab:: Keras v2
63+
.. code-block:: Python
64+
65+
# Parser for converter
66+
def parse_reverse_layer(keras_layer, input_names, input_shapes, data_reader):
67+
layer = {}
68+
layer['class_name'] = 'KReverse'
69+
layer['name'] = keras_layer['config']['name']
70+
layer['n_in'] = input_shapes[0][1]
71+
72+
if input_names is not None:
73+
layer['inputs'] = input_names
74+
75+
return layer, [shape for shape in input_shapes[0]]
76+
77+
.. tab:: Keras v3
78+
.. code-block:: Python
6279
63-
# Parser for converter
64-
def parse_reverse_layer(keras_layer, input_names, input_shapes, data_reader):
65-
layer = {}
66-
layer['class_name'] = 'HReverse'
67-
layer['name'] = keras_layer['config']['name']
68-
layer['n_in'] = input_shapes[0][1]
80+
from hls4ml.converters.keras_v3._base import register, KerasV3LayerHandler
6981
70-
if input_names is not None:
71-
layer['inputs'] = input_names
82+
@register
83+
class KReverseHandler(KerasV3LayerHandler):
84+
'''Keras v3 layer handler for KReverse'''
7285
73-
return layer, [shape for shape in input_shapes[0]]
86+
handles = ('KReverse',)
87+
def handle(
88+
self,
89+
layer: 'keras.Layer',
90+
in_tensors: Sequence['KerasTensor'],
91+
out_tensors: Sequence['KerasTensor'],
92+
) -> dict[str, Any] | tuple[dict[str, Any], ...]:
93+
# Only layer-specific parameters are needed.
94+
# Common parameters are automatically added in the base class.
95+
assert len(in_tensors[0].shape) == 2, 'KReverse is only supported for 2D tensors'
96+
return {'n_in': in_tensors[0].shape[-1]}
7497
7598
Next, we need the actual HLS implementaton of the function, which can be written in a header file ``nnet_reverse.h``.
7699

@@ -144,30 +167,29 @@ In this case, the HLS code is valid for both the Vivado and Quartus backends.
144167
# For keras v3, use register on subclassed KerasV3LayerHandler from hls4ml.converters.keras_v3._base instead
145168
146169
# Register the hls4ml's IR layer
147-
hls4ml.model.layers.register_layer('HReverse', HReverse)
170+
hls4ml.model.layers.register_layer('KReverse', HReverse)
148171
149172
for backend_id in ['Vivado', 'Quartus']:
150173
# Register the optimization passes (if any)
151174
backend = hls4ml.backends.get_backend(backend_id)
152-
backend.register_pass('remove_duplicate_reverse', RemoveDuplicateReverse, flow=f'{backend_id.lower()}:optimize')
153175
154176
# Register template passes for the given backend
155177
backend.register_template(HReverseConfigTemplate)
156178
backend.register_template(HReverseFunctionTemplate)
157179
158180
# Register HLS implementation
159-
backend.register_source('nnet_reverse.h')
181+
backend.register_source('/path/to/your/nnet_reverse.h')
160182
161183
Finally, we can actually test the ``hls4ml`` custom layer compared to the Keras one.
162184

163185
.. code-block:: Python
164186
165187
# Test if it works
166-
kmodel = tf.keras.models.Sequential(
188+
kmodel = keras.models.Sequential(
167189
[
168-
tf.keras.layers.Input(shape=(8,)),
190+
keras.layers.Input(shape=(8,)),
169191
KReverse(),
170-
tf.keras.layers.ReLU(),
192+
keras.layers.ReLU(),
171193
]
172194
)
173195

docs/advanced/hgq.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ High Granularity Quantization (HGQ2)
33
======================================
44

55
.. note::
6-
HGQ2 is the successor of the original `HGQ <./hgq1.html>`__. framework, which was built on Keras v2. HGQ2 built on top of Keras v3, leveraging its new features and improvements.
6+
New projects are encouraged to use `HGQ2 <../hgq2.html>`_ instead of the original `HGQ <../hgq.html>`_.
7+
HGQ2 extends the original HGQ with more supported layers, more quantizer options, and is on top of Keras v3, which can be used natively with JAX, PyTorch, and TensorFlow backends.
78

89
.. image:: https://img.shields.io/badge/License-LGPLv3-blue.svg
910
:target: https://www.gnu.org/licenses/lgpl-3.0.en.html

docs/advanced/hgq1.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
High Granularity Quantization (HGQ)
33
===================================
44

5+
.. note::
6+
While still supported and maintained, HGQ is deprecated in favor of `HGQ2 <../hgq2.html>`_. New projects are strongly encouraged to use HGQ2 instead.
7+
58
.. image:: https://github.com/calad0i/HGQ/actions/workflows/sphinx-build.yml/badge.svg
69
:target: https://calad0i.github.io/HGQ/
710
.. image:: https://badge.fury.io/py/hgq.svg

docs/api/configuration.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ for automatic setting of precisions. The layer-level precisions with the ``'nam
7373
(see :ref:`Automatic precision inference`). Note that layer-level settings take precedence over model-level settings. A ``'name'`` granularity is required for QKeras
7474
and QONNX model parsing. Passing the backend to these functions is recommended because some configuration options depend on the backend. See :py:class:`~hls4ml.utils.config.config_from_keras_model`
7575
and similar for more information on the various options. Note specifically the documentation of :py:class:`~hls4ml.utils.config.config_from_pytorch_model` on how to handle differences in input data
76-
formats between pytorch and keras (hls4ml follows keras conventions internally). Note that passing precision configurations for HGQ/HGQ2 models is not needed in general, and **should not be done** without understanding the implications.
76+
formats between pytorch and keras (hls4ml follows keras conventions internally).
77+
78+
.. warning::
79+
Note that passing precision configurations when invoking the full model precision propagation (by default for HGQ/HGQ2 models, or when `bit_exact=True` is set for other frontends) is **not needed** and **should not be done** without understanding the implications.
7780

7881
One can override specific values before using the configuration:
7982

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def get_pypi_version(package, url_pattern=URL_PATTERN):
7070
'sphinx.ext.napoleon',
7171
'sphinx_contributors',
7272
'sphinx_github_changelog',
73+
'sphinx_tabs.tabs',
7374
]
7475

7576
# Note: to build locally, you will need to set the SPHINX_GITHUB_CHANGELOG_TOKEN
@@ -103,7 +104,7 @@ def get_pypi_version(package, url_pattern=URL_PATTERN):
103104

104105
html_theme_options = {
105106
'canonical_url': '',
106-
'analytics_id': '', # Provided by Google in your dashboard
107+
'analytics_id': '', # Provided by Google in your dashboard
107108
'logo_only': True,
108109
'display_version': True,
109110
'prev_next_buttons_location': 'bottom',

docs/frontend/keras.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ The ``data_format='channels_first'`` parameter of Keras layers is supported for
1818
* `QKeras <https://github.com/fastmachinelearning/qkeras>`_
1919
The equivalent QKeras API and its quantizers are also supported by ``hls4ml``. QKeras is not compatible with Keras v3.
2020
* `HGQ <https://github.com/calad0i/HGQ>`_
21-
The equivalent HGQ API is also supported.
21+
The equivalent HGQ API is also supported. Still maintained but deprecated in favor of `HGQ2 <../hgq2.html>`_.
2222
* `HGQ2 <https://github.com/calad0i/HGQ2>`_
2323
The equivalent HGQ2 API is also supported, plus some additional advanced operators.

docs/intro/setup.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ The following Python packages are all optional and are only required if you inte
5757
* `Brevitas <https://xilinx.github.io/brevitas/>`_: Based on PyTorch. See `frontend/pytorch <../frontend/pytorch.html>`_ for more details.
5858
* `QONNX <https://github.com/fastmachinelearning/qonnx>`_: Based on ONNX. See `frontend/onnx <../frontend/onnx.html>`_ for more details.
5959

60-
Running C simulation from Python requires a C++11-compatible compiler. On Linux, a GCC C++ compiler ``g++`` is required. Any version from a recent Linux should work. On MacOS, the *clang*-based ``g++`` is enough. For the oneAPI backend, one must have oneAPI installed, along with the FPGA compiler, to run C/SYCL simulations.
60+
Running C simulation from Python requires a C++11-compatible compiler. On Linux, a GCC C++ compiler ``g++`` is required. Any version from a recent Linux should work. On MacOS, the *clang*-based ``g++`` is enough. For the oneAPI backend, one must have `oneAPI=2025.0` (2025.1 is known **not to work**) installed, along with the FPGA compiler, to run C/SYCL simulations.
6161

6262
Specific functionalities may need additional Python packages. If any needed is missing, ``hls4ml`` will raise an error and prompt you to install the missing packages.
6363

6464
To run FPGA synthesis, installation of following tools is required:
6565

66-
* Xilinx Vivado HLS 2018.2 to 2020.1 for synthesis for Xilinx FPGAs using the ``Vivado`` backend.
66+
* Xilinx Vivado HLS 2020.1 for synthesis for Xilinx FPGAs using the ``Vivado`` backend. Older versions may work, but use at your own risk.
6767

6868
* Vitis HLS 2022.2 or newer is required for synthesis for Xilinx FPGAs using the ``Vitis`` backend.
6969

7070
* Intel Quartus 20.1 to 21.4 for the synthesis for Intel/Altera FPGAs using the ``Quartus`` backend.
7171

72-
* oneAPI 2024.1 to 2025.0 with the FPGA compiler and recent Intel/Altera Quartus for Intel/Altera FPGAs using the ``oneAPI`` backend.
72+
* oneAPI 2024.1 to 2025.0 with the FPGA compiler and recent Intel/Altera Quartus for Intel/Altera FPGAs using the ``oneAPI`` backend. Newer versions are known **not to work**.
7373

7474
Catapult HLS 2024.1_1 or 2024.2 can be used to synthesize both for ASICs and FPGAs.
7575

docs/intro/status.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ A summary of the on-going status of the ``hls4ml`` tool is in the table below.
5151
+-----------------------+-----+-----+--------------+--------+--------+-----+
5252
| Frontend/Backend | MLP | CNN | RNN/LSTM/GRU | GarNet | Einsum | MHA |
5353
+=======================+=====+=====+==============+========+========+=====+
54-
| Keras v2/QKeras ||||| N/A ||
54+
| Keras v2 |||||||
55+
+-----------------------+-----+-----+--------------+--------+--------+-----+
56+
| QKeras ||||| N/A | N/A |
5557
+-----------------------+-----+-----+--------------+--------+--------+-----+
5658
| HGQ ||| N/A | N/A | N/A | N/A |
5759
+-----------------------+-----+-----+--------------+--------+--------+-----+
@@ -63,7 +65,7 @@ A summary of the on-going status of the ``hls4ml`` tool is in the table below.
6365
+-----------------------+-----+-----+--------------+--------+--------+-----+
6466
| ONNX |||||||
6567
+-----------------------+-----+-----+--------------+--------+--------+-----+
66-
| QONNX ||| N/A | N/A | N/A | N/A |
68+
| QONNX ||| | N/A | N/A | N/A |
6769
+-----------------------+-----+-----+--------------+--------+--------+-----+
6870
| Vivado/Vitis HLS |||||||
6971
+-----------------------+-----+-----+--------------+--------+--------+-----+
@@ -82,7 +84,7 @@ Other feature notes:
8284
- Intel HLS versions 20.1 to 21.4, versions > 21.4 have not been tested.
8385
- Vitis HLS versions 2022.2 to 2024.1. Versions <= 2022.1 are known not to work.
8486
- Catapult HLS versions 2024.1_1 to 2024.2
85-
- oneAPI versions 2024.1 to 2025.0. 2025.1 is known to not work.
87+
- oneAPI versions 2024.1 to 2025.0. Any future versions are known to not work.
8688

8789
* ``hls4ml`` supports Linux [*]_ and requires python >=3.10. hls4ml does not require a specific Linux distribution version and we recommend following the requirements of the HLS tool you are using.
8890
* Windows and macOS are not supported. Setting up ``hls4ml`` on these platforms, for example using the Windows Subsystem for Linux (WSL), should be possible, but we do not provide support for such use cases.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ optional-dependencies.doc = [
3333
"sphinx-contributors",
3434
"sphinx-github-changelog",
3535
"sphinx-rtd-theme",
36+
"sphinx-tabs",
3637
]
3738
optional-dependencies.hgq = [ "hgq>=0.2.3" ]
3839
optional-dependencies.hgq2 = [ "hgq2>=0.0.1" ]

0 commit comments

Comments
 (0)