Maybe the title is confusing. Here is the small example:
w = torch.tensor([3.2], requires_grad=True)
p = torch.tensor([2.0, 1, 7], requires_grad=True)
g = torch.sum(p ** 2)
e = w * g
e.backward(retain_graph=True)
f = p.grad

l = e + 0.5 * f.mean()
l.backward()

here f is the funciton of w, however we loss gradient graph of f ( only tensor), we know f=2pw actually. when we do backward() for l, we want f contributes to gradient of w (df/dw = 2p). How to make it here?
Thanks.