You can get dout/da by callling a.retain_grad()
input = torch.ones(1)
f = torch.nn.Linear(1, 1, bias=False)
f.weight.data.copy_(torch.ones(1))
a = 2*f(input)
out = a*a/2
a.retain_grad()
out.backward()
assert a.grad[0] == 2.
assert f.weight.grad[0] == 4.