Fix over-allocation of bits for quantised po2
Created by: bo3z
Description
- When using power-of-2 multiplications, the current implementation assigns too many bits
- In Vivado, there is a limit of 65,536 bits, which will be allocated when the weights are quantised in QKeras with 17 or more bits and a power of 2
- The multiplication can be computed in far less bits - it only requires the width of the input and weights combined, not the power of that.
- This problem is very sneaky - it is very rare to train with QKeras with a bit-width of more than 16 - however, the
alpha
parameter in QKeras can often be more than 16 bits (when it is not specified by the user to be 1 - attached is a quantised jet classification model that initially found this bug - model)
Type of change
-
Bug fix (non-breaking change that fixes an issue)
Tests
- Extend test_qkeras.py to test power of 2 multiplication with a high number and ensure the outputs match.
Checklist
-
I have read the guidelines for contributing. -
I have commented my code, particularly in hard-to-understand areas. -
I have made corresponding changes to the documentation. -
My changes generate no new warnings. -
I have installed and run pre-commit
on the files I edited or added. -
I have added tests that prove my fix is effective or that my feature works.