Allow AP_SAT, AP_RND for 'maximum' precision in HLS Config #1422
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
During the development of a gravnet model for hls4ml, I found that setting rounding and saturation for the maximum allowed precision can be beneficial for increasing accuracy. Below you can see histograms and mean difference with one standard deviation when the maximum precision is
ap_fixed<16,8,AP_RND,AP_SAT,0>and when rounding and saturation are not enabled (ap_fixed<16,8>).In the current upstream main branch of hls4ml, rounding and saturation modes set through the 'maximum' field in the HLS config are ignored during precision inference (see e.g. here). I thus propose a single function
_apply_max_precision_constraintsto be applied where necessary in theinfer_precision.pymodule, which adheres the following rules:(meaning the user likely set them explicitly).
We can of course discuss, what the preferred ruleset should be here.
No additional dependencies are required for this change.
Type of change
Tests
Pytest
Added a new pytest module,
test_max_precision.py, which tests the newly added_apply_max_precision_constraintsfunction isolated and within the_infer_precisionfunction, using mocks.Conversion to HLS
Ran the full jupyter notebook for gravnet keras conversion to hls at hls4ml-gravnet (Link) to generate the below listed plots, with the proposed change enabled and disabled. The profiling section was ran with this fix applied. We currently do not provide the fully trained model open-source, since it is not finalized. @bo3z please contact me directly, also regarding the dataset, if needed.
Checklist
pre-commiton the files I edited or added.GravNet plots showing accuracies and bias across layers
With rounding and saturation enabled for the maximum precision.