fastmachinelearning / hls4ml

Machine learning on FPGAs using HLS
https://fastmachinelearning.org/hls4ml
Apache License 2.0
1.18k stars 388 forks source link

Make binary CNN match between Keras and hls4ml #804

Closed jmitrevs closed 10 months ago

jmitrevs commented 1 year ago

Description

The check on the pytest for binary CNNs had to be disabled because it failed. This enables it, and attempts to fix issues that it discovers

Type of change

Tests

The test checking has been enabled. It still fails in most cases.

Checklist

jmitrevs commented 1 year ago

This now matches for io_parallel implementations with the Latency strategy. Resource still fails (potentially for unrelated reasons also on Quartus) and for io_stream. @vloncar , I am curious if you think there's a better way to preserve the XnorPrecision designation when doing MaxPooling than I am doing here. It is a bit related to precision propagation, but I think in this case you really need to preserve the type for the network to make sense.

jmitrevs commented 1 year ago

With the latest push, this works for Vitis and Vivado (but not Quartus). Note that #817 was triggered with the test previously, which has been modified to not trigger the problem. However, a better fix for #817 would be preferred. It is somewhat beyond the scope of this PR, though.

jmitrevs commented 1 year ago

The way Winograd is written it doesn't support the xnor types, so I have forced the Quartus io_parallel tests to use im2col. If we want to update Winograd to do binary, I'll leave that as a separate development.

jmitrevs commented 1 year ago

Don't fully understand why the pytests fail here but succeeded on my laptop. I thought I was using the same random number, but maybe not.

jmitrevs commented 1 year ago

I think the issue was just accumulator precision in the test (big number - almost equal big number) and then quantization based on > or < 0. Let's see if increasing the bitwidth makes the tests succeed.

bo3z commented 11 months ago

The way Winograd is written it doesn't support the xnor types, so I have forced the Quartus io_parallel tests to use im2col. If we want to update Winograd to do binary, I'll leave that as a separate development.

Enabling binary support for Winograd needs to change (i) the explicit multiplication for binary weights, to use the product function from nnet_mult and (ii) for binary inputs, see how the addition of input elements behaves. But Winograd's algorithm is really meant to reduce the number of multiplications/DSP utilisation and with binary CNNs, that's not really a concern, so I don't think it is necessarily important in this case.

jmitrevs commented 11 months ago

I see there are some errors. Will make it draft till I fix them.

jmitrevs commented 11 months ago

The failures seem to be due to precision of the quantization, so I am removing the draft designation

bo3z commented 11 months ago

Looks good to me.

jmitrevs commented 11 months ago

The test passed, so I think we can maybe merge this.