Use out.retain_grad().
PyTorch by default only saves the gradients for the initial variables x and w (the “leaf” variables) that have requires_grad=True set – not for intermediate outputs like out.
To save the gradient for out, use the retain_grad method
out = torch.matmul(x, w)
out.retain_grad()