Skip to content

Add support for qkeras v3#1473

Open
makoeppel wants to merge 41 commits into
fastmachinelearning:mainfrom
makoeppel:feature/add_qkeras_v3
Open

Add support for qkeras v3#1473
makoeppel wants to merge 41 commits into
fastmachinelearning:mainfrom
makoeppel:feature/add_qkeras_v3

Conversation

@makoeppel
Copy link
Copy Markdown
Contributor

These are the Handlers needed for qkerasV3 (https://pypi.org/project/qkeras-v3/). Updated tutorials: https://github.com/bo3z/hls4ml-tutorial/tree/tutorial-revamp-2026 were tested with the new qkerasV3.

@bo3z
Copy link
Copy Markdown
Contributor

bo3z commented May 14, 2026

Thank you for this!

We should update pyproject.toml to add an optional dependency qkeras-v3 which installs qkeras-v3 and a recent version of tensorflow (one matching keras-v3) from pip (the same way qkeras is now installed).

In my understanding this doesn't yet cover all the layers, which is fine, but we should probably add QConv1D/QConv2D layers, as these are used in the tutorials.

Once the above two are added, the PyTests could also migrate to QKeras v3 (should be as easy as updating the pyproject.toml for the testing dependencies)

@makoeppel
Copy link
Copy Markdown
Contributor Author

I did all the requested changes. For the qconv part the fallback to keras-v2 did the job. I added only a new test case in the test_qkeras.py to test for qconv layers. I also had to convert the 3layer model to "*.keras": fastmachinelearning/example-models#24.

Most tests will actually fail if only keras3 is used. Since the qkeras part is now mostly converted to keras-v3 I was wondering if there should not be also an update to keras3 for the rest of the project. Certainly not in this PR.

Copy link
Copy Markdown
Contributor

@bo3z bo3z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated in some of the comments, the biggest change is aiming to keep support for both QKeras v2 and QKeras v3 (the same way we support Keras v2 and v3). The rest of the comments are for consistency with the rest of the code base and better understanding

@calad0i since you worked on the Keras v3 parser, could you also provide some feedback on this PR when you get the chance?

Comment thread docs/intro/setup.rst Outdated
Comment thread hls4ml/converters/keras_v3/_base.py Outdated
Comment thread hls4ml/converters/keras/qkeras.py
Comment thread hls4ml/converters/keras_v3/qkeras/activation.py Outdated
Comment thread hls4ml/converters/keras_v3/qkeras/activation.py Outdated
Comment thread pyproject.toml Outdated
Comment thread pyproject.toml
Comment thread test/pytest/test_qkeras.py Outdated
Comment thread hls4ml/model/quantizers.py Outdated
Comment thread test/pytest/test_keras_v3_api.py Outdated
Comment thread test/pytest/test_keras_v3_api.py Outdated
Comment thread test/pytest/test_qkeras.py
Comment thread docs/intro/status.rst
+-----------------------+-----+-----+--------------+--------+--------+-----+
| QKeras | ✅ | ✅ | ✅ | ✅ | N/A | N/A |
+-----------------------+-----+-----+--------------+--------+--------+-----+
| QKeras-v3 | ✅ | ✅ | ✅ | ✅ | ✅ | N/A |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't garnet a specific example implemented in some version of qkeras v2?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also work in qkeras v3.

Comment thread hls4ml/converters/keras/qkeras.py
Comment thread hls4ml/converters/keras_v3/qkeras/activation.py Outdated
Comment thread hls4ml/converters/keras_v3/_base.py Outdated
Comment thread hls4ml/converters/keras_v3_to_hls.py Outdated
Comment thread hls4ml/converters/keras_v3/qkeras/batchnorm.py Outdated
Comment thread hls4ml/converters/keras_v3/qkeras/activation.py Outdated
Comment thread hls4ml/converters/keras_v3/qkeras/activation.py
Comment thread hls4ml/converters/keras_v3/qkeras/batchnorm.py Outdated
ret['input_shape'] = [list(t.shape[1:]) for t in in_tensors]
ret['output_keras_tensor_names'] = [t.name for t in out_tensors]

activation = config.get('activation')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

activation handling here is treated as a special case. The base class shall cover this already though...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have now this layer.py which is more or less a base class. I can call it also _base.py if you want.

@makoeppel makoeppel added the please test Trigger testing by creating local PR branch label May 21, 2026
@makoeppel makoeppel added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels May 21, 2026
@makoeppel makoeppel added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels May 21, 2026
@makoeppel makoeppel added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels May 21, 2026
@makoeppel makoeppel added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels May 21, 2026
@makoeppel makoeppel added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels May 21, 2026
@makoeppel makoeppel added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels May 21, 2026
@bo3z
Copy link
Copy Markdown
Contributor

bo3z commented May 22, 2026

There seems to be one unrelated failure in HGQ. @calad0i any ideas?

@calad0i
Copy link
Copy Markdown
Contributor

calad0i commented May 22, 2026

Looks like usual fluctuation in bit exactness tests, can run again and see what happens...

@makoeppel makoeppel added please test Trigger testing by creating local PR branch and removed please test Trigger testing by creating local PR branch labels May 23, 2026
@makoeppel
Copy link
Copy Markdown
Contributor Author

Looks like usual fluctuation in bit exactness tests, can run again and see what happens...

Now all tests passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New hls4ml feature please test Trigger testing by creating local PR branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants