Yes, if inputs are torch.nn.Parameter 's it is clear that they will be True in ctx.needs_input_grad.

Note that nn.Parameter is only a tool in nn. From the autograd point of view, it’s only a Tensor with requires_grad=True.
The first linear’s output requires gradients. You can check print(x.requires_grad).
So the second linear knows that it needs to compute gradients for its input x. (because x.requires_grad == True).
The first linear knows that it does not need to compute gradients for it’s first input because inp.requires_grad==False.
If you question is how is the backpropagation implemented. The implementation detail here is that during the forward, a graph is built with everything that needs to be done during the backward.
You can see this very nicely using the torchviz 4 package.