fastmachinelearning / hls4ml

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

Leaky Quantized ReLU fix #961

Closed DrWatt closed 3 months ago

DrWatt commented 5 months ago

Description

The quantized_relu activation layer included in the QKeras library lets the user set the negative_slope option which practically change the activation function from the usual ReLU to a LeakyReLU. This change was not perceived by hls4ml, meaning that that information was lost when the HLS_model was created by the library. In order to fix this behaviour I have added the QLeakyReLU in the list of supported layers in model/layers.py using the ParametrizedActivation implementation mimicking the LeakyReLU already present and so following a similar path of implementation to the "non-leaky" quantized_relu. Other changes were made to model/profiling.py and utils/config.py to make them compatible with the new layer.

A couple of other fixes were made in backends/vivado_accelerator/vivado_accelerator_config.py (a missing "casting" function when comparing complex objects with strings) and in model/types.py (adding the ap_ to the hls fixed precision type due to errors raised by the vivado compiler)

Type of change

Tests

The quantized_relu with a negative_slope different from the default one was added in the pytest routine covering qkeras' layers. A specific test script has been also added to the pytest directory. The results from the new implementation are asserted to be equal to the QKeras layer with a relative tolerance of 0.00001.

Checklist

jmitrevs commented 3 months ago

Sorry for the delay, but what is the status of this? It looks to me like maybe I can approve and merge it.

vloncar commented 3 months ago

I was testing this and the changes work, however the PR includes unnecessary changes and the test doesn't do anything (data is all positive, so it passes even without the changes in the converter). I distilled the changes and made a proper test in https://github.com/vloncar/hls4ml/tree/qrelu_negative_slope

vloncar commented 3 months ago

I made #987 as a continuation of this PR