You should recalculate Q for each forward pass. Otherwise the computation graph for Q gets freed when you run loss.backward() and the second run can’t backpropagate all the way back to T.
class Net(nn.Module):
  def __init__(self):
    super(Net, self).__init__()
    self.T = nn.Parameter(torch.Tensor(5, 5))        
      
  def forward(self, x0):
    Q = torch.mm(self.T, self.T.t())
    rhat = x0.unsqueeze(1).matmul(Q).matmul(x0.unsqueeze(2)).squeeze(2)
    return rhat