fastmachinelearning / hls4ml

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

Multiple stream clones with different numbers of outputs crash Keras converter. #905

Closed calad0i closed 10 months ago

calad0i commented 11 months ago

Prerequisites

Please make sure to check off these prerequisites before submitting a bug report.

Quick summary

Keras converter crashes when multiple clone operations are required with different precision. PR comes later.

Steps to Reproduce

(Sort of) minimal reproduce example:

from keras.layers import Dense, Add

inp = keras.Input(shape=(10,))
x = Dense(10)(inp)
y = Dense(10)(inp)
z = Dense(10)(inp)
xy = Add()([x,y]) # 5
xy = Add()([xy,y]) # 5
model = keras.Model(inp, [xy, z])

model_hls = convert_from_keras_model(model, io_type='io_stream', output_dir='/tmp/stream_clone_crash', hls_config = {'Model':{'Precision':'fixed<32,10>', 'ReuseFactor':1}})

Cause: optimization at https://github.com/fastmachinelearning/hls4ml/blob/0d48affe108f9df470950fe08a73bc3b5c3f2338/hls4ml/backends/fpga/passes/clone.py#L30C15-L30C15: When two clone happens with different #outputs, the template generated last time will crash.