Closed sei-jgwohlbier closed 2 weeks ago
Hi @sei-jgwohlbier,
thanks for reporting this and the detailed studies you did. I was able to pinpoint the issue to a problem with the conversion to channels_last
for the case of streamed inputs. Basically, before reshape layers, we insert a transpose to undo the conversion of the input data to channels_last
so that the reshape operation can be done correctly. In this case, this messes up the dimensions of the output of the squeeze layer because there actually wasn't any transpose to do undo.
From what I can tell, this can be fixed by changing this line https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/model/optimizer/passes/convert_to_channels_last.py#L97
from if isinstance(node, Reshape) and len(node.attributes['target_shape']) == 1:
to if isinstance(node, Reshape) and len(node.attributes['target_shape']) == 1 and not model.config.config['HLSConfig']['Model']['ChannelsLastConversion'] == "internal":
.
The one thing I can't quite understand is why there aren't any problems with the io_parallel
case. There the result is identical with and without this additional transpose. So before making a PR with a fix, I would like to hear what @vloncar thinks about this.
Awesome, thanks very much. Indeed my test passes with your fix. Let me know if you need me to test anything.
@JanFSchulte do you know if anyone else has looked at this? I continue to use your patch in my version. Thanks. jgw
Hi! Ah, no, this hasn't been fixed in the main branch yet. I'll get around to making a PR in the next few days. Thanks for reminding me!
I ended up including the fix in this PR that also deals with fixes to the pytorch parser: https://github.com/fastmachinelearning/hls4ml/pull/1086
Sorry, forgot to close. Thanks!
Prerequisites
Please make sure to check off these prerequisites before submitting a bug report.
Quick summary
For a simple PyTorch model with a pool1d followed by a squeeze the generated code is incorrect. The correct values are present in the execution, but they are not stored correctly.
Details
Steps to Reproduce
test_pool_squeeze.py
provided below.Expected behavior
Expected passing test.
Actual behavior
The test fails. Details of generated source code behavior is provided below.
Optional
This is the test file
test_pool_squeeze.py
There is a problem with the generated
myproject.cpp
file. I have added some print statements to try to assess what is going on. The print statements are included below.Below is the output of running the compiled
myproject_test.cpp
.Note the WARNING about
layer5_out
containing leftover data. More below.The first line is the input, two channels of length 4 in channels last format.
layer2
is the output of thennet::pooling1d_cl
. I have confirmed the two values shown inlayer2
are the correct averages accounting for channels last. I.e.,Indeed, these are the two values that should appear as "predictions," but the data is not being correctly extracted.
Other notes:
layer5_t
indefines.h
is not correct. Based on the expected output I think it should betypedef nnet::array<ap_fixed<64,24>, 2*1> layer5_t;
, (2*1
not1*1
), but when I make that change the results are not correct. Thelayer5_out
warning does go away in this case.hls4ml
source.