Both
w = torch.randn(2,5)
w.requires_grad_()

and
w = torch.randn(2, 5, requires_grad=True)

will work in PyTorch without any compilation errors. I tried hard to make this example to get the gradient of a jump from uninitialized to random value, but I am missing some important tip to check this.