nlesc-dirac / pytorch

Improved LBFGS and LBFGS-B optimizers in PyTorch.
Apache License 2.0
51 stars 4 forks source link

"view size is not compatible with input tensor's size and stride" #1

Closed cw-tan closed 2 years ago

cw-tan commented 2 years ago

Hi, thanks for the great LBFGS optimizer. This issue is to bring to your attention an error that appeared for my use case. I was using the LBFGS optimizer as a general purpose optimizer, with it being initialized as optimizer = LBFGSNew([tensor_1, tensor_2], lr=lr, history_size=6, max_iter=4, line_search_fn=True) for example.

I got the following error during an optimization step: RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

This error did not appear when I did optimizations with respect to single tensors, for example: optimizer = LBFGSNew([tensor_1], lr=lr, history_size=6, max_iter=4, line_search_fn=True)

Based on this Pytorch issue, I'm guessing that the way that the separate tensors are (combined?) and stored in memory is not contiguous.

I managed to avoid the error by changing all the lines that used .view(...) to .reshape(...) or .contiguous().view(...), with these lines being lines 87, 89 and 108: https://github.com/nlesc-dirac/pytorch/blob/6c229b9b22ad133e145283a086cec5f13dfb7d10/lbfgsnew.py#L87 https://github.com/nlesc-dirac/pytorch/blob/6c229b9b22ad133e145283a086cec5f13dfb7d10/lbfgsnew.py#L89 https://github.com/nlesc-dirac/pytorch/blob/6c229b9b22ad133e145283a086cec5f13dfb7d10/lbfgsnew.py#L108

My optimization seemed to work out well after this change (I opted for the .contiguous().view(...) change), but I just wanted to check if these changes won't create other problems as I'm not sure about the motivation for using .view(...) over .reshape(...) in the first place. Thank you!

SarodYatawatta commented 2 years ago

Thanks, I will check if the fix is OK, and merge it.

SarodYatawatta commented 2 years ago

Fixed. thanks

cw-tan commented 2 years ago

Thanks!