This is really weird. I played with your code a bit and I got it to not error out by doing either of two different things:

Firstly, by doing the final transpose in-place:

output = weight.mm(input.t()).t_()


Secondly, by putting a clone() after the end of the final transpose:

output = weight.mm(input.t()).t().clone()

I do not know why either of these things work. Perhaps someone with more experience with autograd could chime in here?