Could you try to update the parameters inplace:
p.sub_(lr* p.grad)

This would avoid creating a new tensor named p, which doesn’t have the .grad attribute yet (None by default before the first backward call).
Also, you don’t need to call p.requires_grad_() inside the loop.
Note that Variables are deprecated and you can just use tensors now.
PS: I’ve formatted your code. If you would like to post code snippets, you can wrap your code into three backticks ```