If I understand the issue correctly, you are concerned why:
loss.register_hook(lambda grad: print(grad))
loss.backward()

gives tensor(1.)?
If no gradient is passed to the backward function, it’ll be automatically set as torch.ones(1) for you, as the initial gradient is defined as dLoss/dLoss = 1.