Hi,
Some precisions to Alexey’s message:


Indeed, the problem is not with only expand, but with expand + the inplace. And in particular because expand is a view of a.  This is why you don’t see any issue if you remove the inplace or if you replace the expand with a repeat that is not a view (it allocates new memory).


"it is about “unreachable” I don’t think that is. And the error in an internal bug on our end I think 
The reason for .data to “fix” the issue is because it hides the inplace op from the autograd (in a bad way) and so it looks like you do only the view for the autograd.
In general, you should never use .data! and the with torch.no_grad() is the right way to do this!


The temporary fix here is to remove the print of b between the no_grad block and the line where you use it for the sum.
I opened an issue here that explains the exact issue and discusses how to solve it.: Printing should not have (bad) autograd side effects · Issue #49756 · pytorch/pytorch · GitHub 2