fastmachinelearning / hls4ml

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

Softmax with less than 10bits input generates error with default config #885

Closed calad0i closed 10 months ago

calad0i commented 11 months ago

When the input bitwidth to a softmax layer is smaller than 10 bits, a warning message WARNING: Higher bound(something) and lower(something-10) bound cannot be negative. is thrown more than 1000 times, and random results are generated from the softmax layer.

This issue presents with both Stable and Latency strategies.

The corresponding code where the warning was raised is https://github.com/fastmachinelearning/hls4ml/blob/d36e2267c5330247453324aac3008c0f4807c891/hls4ml/templates/vivado/nnet_utils/nnet_activation.h#L144 When the input's precision to the new softmax is smaller than 10 bits (table_size=1024 by default and not configurable in an obvious way), the cut to generate indices goes sub-zero, and the significant bits go random and produce random outputs. Adjusting table_size manually will fix the issue.

PR to fix comes later.