hls4ml icon indicating copy to clipboard operation
hls4ml copied to clipboard

Add set_data_types_from_keras_model() and set_accum_from_keras_model() for automatic precision inference

Open maksgraczyk opened this issue 3 years ago • 3 comments

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:

  • Adding set_data_types_from_keras_model() doing that with a Keras model.
  • Adding 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 (unlike set_data_types_from_keras_model(), it doesn't use profiling information).
  • Adding extra arguments to config_from_keras_model() allowing for the functions above to be called automatically.

maksgraczyk avatar Apr 14 '21 10:04 maksgraczyk

The test failure is due to the new import from profiling that is introduced in utils. Specifically this line that imports matplotlib (but the pandas and seaborn import would fail too). You can see in setup.py that these packages are 'extras' for profiling and not automatically installed. We want to keep it this way so that users not using profiling can skip those packages.

For your PR, I think that just means we need to add some more try: import / except: ImportError guards around the packages only needed for making the plots (matplotlib, seaborn, pandas). The methods from profiling that you've called should work without those, and wrapping the imports that way should resolve the import error.

thesps avatar Apr 20 '21 07:04 thesps

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

maksgraczyk avatar Apr 20 '21 15:04 maksgraczyk

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 set_data_types_from_keras_model() to be called also from hls4ml.utils rather than only hls4ml.utils.config.

maksgraczyk avatar Apr 21 '21 12:04 maksgraczyk