Why do we need to provide inputs in torch.autograd.grad

How would the grad() function otherwise know for what you want the derivative?  E.g., say you have something like g(f(w*x+b)). Say g() is your output. Now, do you want to compute the derivative of g with respect to f, w, x, or b? How would the grad function know?