I think the issue you are seeing might be created by torch.jit.trace which is used by the ONNX exporter as the default. Could you try to trace the model without ONNX and see if it’s still failing?
If so, try to use torch.jit.script instead and see if this would be working. If so, use it in the ONNX export as well.