Integration of PQuantML with hls4ml#1400
Conversation
|
What's the plan for installing PQuant together with hls4ml? In principle, we should have I expect the pytests to fail currently because of this. We are preparing a dedicated testing container with keras 3 that should be able to be used for this going forward, but that would then need the proper installation instructions for pquant to work. Also, would be nice to have the documentation update included in this PR ;) |
I have now included it.
This is a known issue for the PQuantML team and they are trying to solve it. As far as I have understood, the problem is due to pip trying to install the wrong version of tensorflow, and the only solution at the moment is "manually" installing it with conda.
Added :) |
7a04955 to
a8b3c1f
Compare
|
To make the tests work, We probably also need to run the torch version of the tests in this environment, then. Locally I still have a lot of issues running the tests. I can install pquant through pip now, but I'm getting tons of errors coming from PQuant itself.
So I assume there is still something wrong with my environment. |
|
I finally got the tests to work locally, so here is some feedback that should hopefully make it all work:
Even with that I still had some issues with pquant imports failing when running pytest, despite the same inputs working fine in a simple python shell in the same environment. But it worked when running it as Then, running the actual tests, I get the following to types of errors:
|
|
Thanks! I think you need to catch up to change in the keras3 parser where the |
| layer_list.append(lay) | ||
|
|
||
| if idx < len(layer) - 1: | ||
| inputs_map[lay['name']] = inputs_map.get(layer[idx + 1]['name'], layer[idx + 1]['name']) |
There was a problem hiding this comment.
I don't quite get the logic in this mapping, can you please explain it?
There was a problem hiding this comment.
Let's assume we have two consecutive layers A, who has an input and output quantizer, and B.
Layer A will be parsed as 3 layers: FixedPointQuantizer A_iq, A proper, and FixedPointQuantizer A_oq. The input argument for the node B, though, will still be A, as A_oq did not exist at the moment of tracing the model. This means that the correct flow of data in the model will not be reconstructed, and both A_oq and B will be seen as outputs of A (A_oq will also be one of the outputs of the global model, as it is not used as input by anyone).
What these lines do is ensuring that if a layer sees A as input, it gets redirected to the next parsed layer, i.e. A_oq, and the correct flow can be reconstructed.
Admittedly, this has the useless consequence of redirecting the layers with A_iq as node argument to A, which should never happen as A_iq did not exist at tracing time.
Perhaps it could be reworked to just work on the A equivalent with something along the lines of:
if lay['name'] == layer_name and idx != len(layer) - 1:
inputs_map[lay['name']] = inputs_map.get(layer[-1]['name'], layer[-1]['name'])
Alternatively, we could do an explicit check for PQuantML layers and set the input map before all of this, as it is done in line 209 for skip layers.
There was a problem hiding this comment.
Thanks for the explanation. I think the suggestion with the explicit check to only work on A might be a good idea to not have unintended consequences some time in the future, even if the current implementation is save now.
There was a problem hiding this comment.
Got it.
I changed it so that I explicitly check if it is a PQuantML layer and if it has an output quantizer; only in this case, the inputs_map is changed.
| hls_model.compile() | ||
|
|
||
| .. note:: | ||
| Do not pass any precision configuration from ``hls4ml.converters.convert_from_<frontend>_model`` in general. PQuantML-defined models will invoke model-wise precision propagation automatically to ensure bit-exactness between the PQuantML model and the generated HLS code (See `here <./precision.html>`__ for more details). |
There was a problem hiding this comment.
Can we document also what happens if this note is ignored? Will things break, will results be silently incorrect?
There was a problem hiding this comment.
I was following HGQ2's note, as the underlying bit exact pass that is invoked is the same. From my understanding, explicit layer precision is simply ignored (as per the Model-wise Precision Inference doc), while if you have both auto precision in the config and invoke the bit exact pass it will lead to undefined behavior.
|
Looks good to me now, thanks! Lets run the test one last time and merge once they are done. |

Description
This PR introduces the possibility of directly parsing PQuantML layers into hls4ml, both using the PyTorch and Keras V3 frontend. It uses HGQ2 conventions and relies on the bit_exact optimization pass to ensure correct precision is enforced and bit exactness.
Type of change
Tests
Tests are present both for PyTorch and Keras V3 frontend:
test_pquant_pytoch.pyandtest_pquant_keras.py.Checklist
pre-commiton the files I edited or added.