hls4ml icon indicating copy to clipboard operation
hls4ml copied to clipboard

Average pooling result_t

Open jmitrevs opened this issue 7 months ago • 4 comments

Quick summary

Currently all poolings set the result_t to the input precision if auto. While this makes sense for MaxPooling, I think it would be preferrable if result_t is made to match accum_t for AvgPooling, which is suitably widened. Currently, average pooling can introduce an unexpected truncation. (I would at least use rounding by default if you wanted to keep the same precision.)

jmitrevs avatar May 12 '25 20:05 jmitrevs

I am hitting this issue with avgppool. Which rounding mode would you suggest? ap_fixed<16,6,AP_RND_ZERO>? Is there a change I can make locally to achieve result_tmatching accum_t?

sei-jgwohlbier avatar Jun 11 '25 16:06 sei-jgwohlbier

config = hls4ml.utils.config_from_keras_model(model, granularity='name')
config['LayerName']['watever_name_of_your_avg_pool_layer_is']['Precision']['accum'] = 'ap_whatever<...>'
config['LayerName']['watever_name_of_your_avg_pool_layer_is']['Precision']['result'] = 'ap_whatever<...>'

vloncar avatar Jun 12 '25 14:06 vloncar

@jmitrevs Will such result_t have an impact on the subsequent layers having a bitwidth potentially too wide? accum_t should be wide enough to to store the sum of elements in the pool window, but after averaging the integer component does not need to be as wide, though fractional part probably does.

vloncar avatar Jun 12 '25 14:06 vloncar

Ideally one would just increase the fractional component but not the integer component, with rounding left to the user if they want to reduce the width.

jmitrevs avatar Jun 12 '25 16:06 jmitrevs