-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add set_data_types_from_keras_model() and set_accum_from_keras_model() for automatic precision inference #321
base: main
Are you sure you want to change the base?
Add set_data_types_from_keras_model() and set_accum_from_keras_model() for automatic precision inference #321
Conversation
find_optimal_a_b() inside the method has to be implemented
…from_keras_model()
The test failure is due to the new import from For your PR, I think that just means we need to add some more |
If you want to familiarise yourself with the new method, you can have a look at this Jupyter notebook: https://gist.github.com/maksgraczyk/031ad1ec8191e237c2f5008adade2257 |
I've made 2 extra commits after getting feedback from one of the users: the first one makes sure that only layers actually present in an HLSModel config are processed (it turns out that some Keras layers may not be actually present in an HLSModel config) and the second one allows |
hls4ml/utils/config.py
Outdated
min_b = 2 ** (type_b.integer - type_b.width) | ||
|
||
max_val = n * max_w * max_i + max_b | ||
min_val = min_b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in the general case the minimum value could come either from the product of weight * input or the bias, depending on their precisions, not always from the bias.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bias may not exist (e.g., Dense(..., use_bias=False)
), in that case we insert zeros and set precision to uint(1), which may throw this function off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback! I've just pushed the commits addressing both issues and adding Conv-type layer support.
Applicable to set_accum_from_keras_model(). Fixes: make min_val inference consider the presence of bias and the comparison between min_b and min_w * min_i.
…m_from_keras_model()
…_type_mode is not 'default'
…d ignore_optimal_type_errors
Profiling information gathered e.g. by
hls4ml.model.profiling.numerical()
and other information can be used for setting data types in an HLSModel config heuristically so that the optimal precision configuration is achieved faster.This PR does the following:
set_data_types_from_keras_model()
doing that with a Keras model.set_accum_from_keras_model()
which sets accum_t of relevant layers based on a Keras model and data types set in an HLSModel config (unlikeset_data_types_from_keras_model()
, it doesn't use profiling information).config_from_keras_model()
allowing for the functions above to be called automatically.