Skip to content
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

Incorrect hls4ml results for AveragePooling2D/MaxPooling2D Keras layer #995

Open
4 tasks done
clw5710 opened this issue Apr 11, 2024 · 0 comments
Open
4 tasks done
Labels

Comments

@clw5710
Copy link

clw5710 commented Apr 11, 2024

Prerequisites

Please make sure to check off these prerequisites before submitting a bug report.

  • Test that the bug appears on the current version of the master branch. Make sure to include the commit hash of the commit you checked out.
  • Check that the issue hasn't already been reported, by checking the currently open issues.
  • If there are steps to reproduce the problem, make sure to write them down below.
  • If relevant, please include the hls4ml project files, which were created directly before and/or after the bug.

Quick summary

The AveragePooling2D Keras Layer is producing incorrect data when compiled with hls4ml. Namely, when the pool_size=1 and strides=2 the data is incorrect. When setting the pool_size=2 the hls4ml data matches Keras. This issue was observed by turning on tracing and saving off the Keras and hls4ml data. The layer data matches between Keras and hls4ml until the AveragePooling2D layer is reached.

Details

I have created a simple 6 layer model to isolate the problem with AveragePooling2D. I've also ran profiling to confirm that the bit widths are set correctly. I do not see the values saturating in the trace results and the bit widths have been widened to 64,6 just to be safe. There does not seem to be some kind of pattern between hls4ml and Keras for the incorrect layer data, and it seems to occur when the pool_size is smaller than the strides.

Steps to Reproduce

The code can be found on this gist: https://gist.github.com/clw5710/cd3ba4dfd23db151d1a6fbf10db2ec30
The model can be trained and compiled with hls4ml very quickly. The file nn_pooling_test.py contains the model architecture, then there are two jupyter notebooks that train the model and build it using hls4ml. The training accuracy is poor but I'm just interested in the hls4ml AveragePooling2D layer results matching Keras.

I also supplied the csv output files where you can see the first convolutional layer matches but then the pooling layer does not. If the pool_size is set to 2 in nn_pooling_test.py the results match.

Expected behavior

I expected the hls4ml AveragePooling2D data to match the Keras AveragePooling2D data.

Actual behavior

The data is incorrect when the pool_size=1 and strides=2. When pool_size=2 and strides=2 the AveragePool2D data matches between Keras and hls4ml. This edge case seems to be causing an issue.

Possible fix

I believe the fix would be in nnet_pooling.h in the pooling2d_cl function here:

void pooling2d_cl(data_T data[CONFIG_T::in_height * CONFIG_T::in_width * CONFIG_T::n_filt],

@clw5710 clw5710 added the bug label Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant