Error with hls_model.predict() when using multiple inputs
Created by: thaarres
I am trying to call hls_model.predict()
for a model with multiple inputs. When passing <=2 samples, the function works. However, when passing >2 samples, an error is raised:
Traceback (most recent call last):
File "debugPredict.py", line 45, in <module>
y_hls = hls_model.predict(x_hls).reshape(y.shape)
File "/mnt/data/thaarres/miniconda3/envs/hls4ml-l1jets/lib/python3.7/site-packages/hls4ml/model/hls_model.py", line 621, in predict
top_function(*argtuple)
ctypes.ArgumentError: argument 4: <class 'TypeError'>: expected LP_c_ushort instance instead of numpy.ndarray`
A minimal working example can be found below, with GarNet as imported from here:
import numpy as np
from garnet import GarNet
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
import hls4ml
vmax = 16
feat = 3
x = Input(shape=(vmax,feat))
n = Input(shape=(1,), dtype='uint16')
inputs = [x, n]
outputs = GarNet(8, 8, 16, simplified=True, collapse='mean', input_format='xn',
output_activation=None, name='gar_1', quantize_transforms=False)(inputs)
model = Model(inputs=inputs, outputs=outputs)
model.summary()
config = hls4ml.utils.config_from_keras_model(model, granularity='name')
config['Model'] = {}
config['Model']['ReuseFactor'] = 1
config['Model']['Strategy'] = 'Latency'
config['Model']['Precision'] = 'ap_fixed<32,6>'
config['LayerName']['gar_1']['Precision'] = {'default': 'ap_fixed<32, 6, AP_RND, AP_SAT>', 'result': 'ap_fixed<32, 6>'}
cfg = hls4ml.converters.create_config('xc7z020clg400-1')
cfg['HLSConfig'] = config
cfg['KerasModel'] = model
hls_model = hls4ml.converters.keras_to_hls(cfg)
hls_model.compile()
# WORKS (1 input sample)
x = [np.random.rand(1,vmax,feat), np.random.randint(0,vmax,size=(1,1))]
y = model.predict(x)
x_hls = [x[0], x[1].astype(np.float64)]
y_hls = hls_model.predict(x_hls).reshape(y.shape)
print('1 sample keras predict: \n', np.squeeze(y))
print('1 sample hls4ml predict:\n', np.squeeze(y_hls))
# FAILS (>2 input samples)
x = [np.random.rand(3,vmax,feat), np.random.randint(0,vmax,size=(3,1))]
y = model.predict(x)
x_hls = [x[0], x[1].astype(np.float64)]
y_hls = hls_model.predict(x_hls).reshape(y.shape)
print('3 samples keras predict: \n', np.squeeze(y))
print('3 samples hls4ml predict:\n', np.squeeze(y_hls))`