You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please make sure to check off these prerequisites before submitting a bug report.
Test that the bug appears on the current version of the master branch. Make sure to include the commit hash of the commit you checked out.
Check that the issue hasn't already been reported, by checking the currently open issues.
If there are steps to reproduce the problem, make sure to write them down below.
If relevant, please include the hls4ml project files, which were created directly before and/or after the bug.
Quick summary
hls4ml fails on a PyTorch model with multiple return values.
Details
hls4ml fails on code below that has two linear layers and returns output from both layers.
Steps to Reproduce
Add what needs to be done to reproduce the bug. Add commented code examples and make sure to include the original model files / code, and the commit hash you are working on.
Clone the hls4ml repository
Checkout the master branch, with commit hash: [cc4fbf9]
Run conversion for code below.
from pathlib import Path
import numpy as np
import os
import shutil
import torch
import torch.nn as nn
from torchinfo import summary
from hls4ml.converters import convert_from_pytorch_model
from hls4ml.utils.config import config_from_pytorch_model
test_root_path = Path(__file__).parent
class test(nn.Module):
def __init__(self, n_in, n1, n2):
super().__init__()
self.lin1 = nn.Linear(n_in, n1, bias=True)
self.lin2 = nn.Linear(n_in, n2, bias=True)
def forward(self, x):
y = self.lin1(x)
z = self.lin2(x)
return y, z
if __name__ == "__main__":
n_batch = 16
n_in = 16
n1 = 32
n2 = 64
X_input_shape = (n_batch, n_in)
model = test(n_in, n1, n2)
io_type='io_stream'
backend='Vitis'
output_dir = str(test_root_path / f'hls4mlprj_2lin_{backend}_{io_type}')
if os.path.exists(output_dir):
print("delete project dir")
shutil.rmtree(output_dir)
model.eval()
summary(model, input_size=X_input_shape)
X_input = np.random.rand(*X_input_shape)
#X_input = np.ones(X_input_shape)
with torch.no_grad():
pytorch_prediction = [p.detach().numpy()
for p in model(torch.Tensor(X_input))]
# transform X_input to channels last
X_input_hls = np.ascontiguousarray(X_input)
# write tb data
ipf = "./tb_input_features.dat"
if os.path.isfile(ipf):
os.remove(ipf)
with open(ipf, "ab") as f:
for x in X_input_hls:
np.savetxt(f, x.flatten(), newline=" ")
opf = "./tb_output_predictions.dat"
if os.path.isfile(opf):
os.remove(opf)
with open(opf, "ab") as f:
for p0,p1 in zip(pytorch_prediction[0],
pytorch_prediction[1]):
np.savetxt(f, p0.flatten(), newline=" ")
np.savetxt(f, p1.flatten(), newline=" ")
default_precision='ap_fixed<16,6>'
default_precision='ap_fixed<32,12>'
#default_precision='ap_fixed<64,24>'
config = config_from_pytorch_model(model,
input_shape=X_input_shape[-1:],
backend=backend,
default_precision=default_precision,
default_reuse_factor=1,
channels_last_conversion='internal',
transpose_outputs=False)
config['Model']['Strategy'] = 'Resource'
print(config)
print(output_dir)
hls_model = convert_from_pytorch_model(
model,
output_dir=output_dir,
input_data_tb=ipf,
output_data_tb=opf,
backend=backend,
hls_config=config,
io_type=io_type,
part='xcvu9p-flga2104-2-e'
)
hls_model.compile()
print("pytorch_prediction")
print(pytorch_prediction)
# reshape hls prediction to channels last, then transpose
hls_prediction = hls_model.predict(X_input_hls)
print("hls_prediction")
print(hls_prediction)
rtol = 1.0e-2
atol = 1.0e-2
assert len(pytorch_prediction) == len(hls_prediction), "length mismatch"
for p0, h0 in zip(pytorch_prediction[0], hls_prediction[0]):
np.testing.assert_allclose(p0,
h0,
rtol=rtol, atol=atol)
for p1, h1 in zip(pytorch_prediction[1], hls_prediction[1]):
np.testing.assert_allclose(p1,
h1,
rtol=rtol, atol=atol)
# synthesize
hls_model.build(csim=True, synth=True, cosim=True, validation=True)
Hi @sei-jgwohlbier Thanks for reporting this issue, and thanks for always having very clear and complete bug reports, makes it very easy to fix the issues.
I had indeed overlooked this case when implementing the pytorch parser, fixed in this PR: #1151
Prerequisites
Please make sure to check off these prerequisites before submitting a bug report.
Quick summary
hls4ml fails on a PyTorch model with multiple return values.
Details
hls4ml fails on code below that has two linear layers and returns output from both layers.
Steps to Reproduce
Add what needs to be done to reproduce the bug. Add commented code examples and make sure to include the original model files / code, and the commit hash you are working on.
Expected behavior
Sucessful synthesis.
Actual behavior
The text was updated successfully, but these errors were encountered: