@albanD Here is the code after your suggestion
class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        dim=100
        
        self.hidden1 = nn.Linear(dim, dim, bias=False)
        self.output = nn.Linear(dim, 10)

        self.hidden1.weight.requires_grad = False
        self.u = torch.nn.Parameter(torch.randn(dim), requires_grad=True)
        
    def forward(self, x, W):
        U = torch.ger(self.u,self.u)
        self.hidden1.weight.copy_(U*W)

        x = self.hidden1(x)
        x = self.output(x)
    return F.log_softmax(x,dim=1)

model = Net()
model = model.cuda()
output = model.forward(data, W)
loss = F.nll_loss(output, target)
loss.backward() 

Now the error is
Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.