The grad attribute will be retained for leaf variables by default.
If you want to print them for the loss, you would need to call loss.retain_grad() before calling loss.baclward().
However, note that this gradient will be 1. by default, if you didn’t pass any manual gradient argument to loss.backward(gradient=).
Here is a small example:
model = models.resnet18()
x = torch.randn(1, 3, 224, 224)
target = torch.zeros(1).long()

criterion = nn.CrossEntropyLoss()

out = model(x)
loss = criterion(out, target)
loss.retain_grad() # use this to print the grad
loss.backward()

print(loss.grad)
> tensor(1.)

To print the gradient of all parameters, you could use this code snippet after calling backward:
# print grads of all parameters
for name, param in model.named_parameters():
    print(name, param.grad.abs().max())