Closed sei-rquartiano closed 1 week ago
Hi, I encountered this issue before. I added two lines below https://github.com/fastmachinelearning/hls4ml/blob/main/hls4ml/converters/pytorch/core.py#L66-L67
if hasattr(node, 'dim'):
layer['axis'] = class_object.dim
if hasattr(class_object, 'dim'): # temperal solution
layer['axis'] = class_object.dim
when parsing the pytorch's softmax, there is no dim
in node
. I found dim
is in class_object
. I hope this helps you:)
Hi! Thanks for reporting these issues. The softmax issues are indeed a bug that we overlooked because softmax activation was missing from our tests. This is fixed in https://github.com/fastmachinelearning/hls4ml/pull/1086.
For the issues with numerical correctness in sigmoid and softmax, this seems to be related to the io_type setting. If I run your setup with io_parallel, everything seems to work. The issues with io_stream are not reproduced in our tests, so I'm not quite sure yet what's going wrong there.
Thank you both for the input! I will switch over to that PR branch until it's merged and change my 'io_type' to 'io_parallel.' If I manage to figure out what's going on with 'io_stream' on my end I'll be sure to let you know. Thanks again!
Getting correct results after merged PR and recommended changes. Thank you!
Prerequisites
Please make sure to check off these prerequisites before submitting a bug report.
Quick summary
Unable to compile a PyTorch softmax or sigmoid layer. Attempted both module and functional calls; got different error messages described in next section
Details
For the nn.Softmax() call I get a
KeyError: 'axis'
error which stops config generation. For nn.Sigmoid() and functional softmax or sigmoid calls it gets through compilation, however anAssertionError
shows that the values are out of tolerance. More importantly than the tolerance, they appear to output the same incorrect values every time.Steps to Reproduce
This is the code I've used to investigate errors with pytorch softmax/sigmoid calls. To switch between them comment/uncomment the appropriate lines in the model definition towards the top of the file. I also added a linear layer as an example of something that would normally precede a softmax. Doesn't seem to affect the error.
Expected behavior
Successful model compilation
Actual behavior
Output and error message for softmax module call:
Error message for sigmoid module or any functional calls:
Note: the output values between nn.Sigmoid() and nn.functional.softmax/sigmoid are different, but they're the same for each function and are always outside of tolerance.
Optional
Possible Fix
Isn't a fix as much as it's something I noticed. When I originally encountered the module call error I wondered whether it had to do with the specific keyword 'axis' since it's that in Keras but 'dim' in PyTorch. However when I looked in
converters/core.py: parse_activation_layer()
I notice two things['layer_name']
if statements inside the function (it is in the globalactivation_layers
list outside it), however Sigmoid is there and adding softmax to the list doesn't seem to do anythingThanks for looking into this