Defining my_hook on the class will make it a class method. And so you need to specify it as self.my_hook.
Otherwise you can put it inline in the function:
import torch
import torch.nn as nn
class Net(torch.nn.Module):

    def __init__(self):       
        super(Net, self).__init__()
        self.w=torch.nn.Parameter(torch.ones(2))
       

    def forward(self, x):
        def my_hook(grad):
            grad = grad.clone()
            grad[0] = 50
            print("new grad: ", grad)
            return grad

        out1= 2 * x
        out1.register_hook(my_hook)
        out = torch.mul(out1,self.w)
        return out

   
learning_rate = 0.2
model=Net()
criterion=torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

model(torch.rand(2, requires_grad=True)).sum().backward()