albanD:

Now if you print during the backward, the backward of the second one is called first, then the backward of the first one.


Yes, I modified autograd.Function.
Yes, if inputs are torch.nn.Parameter 's it is clear that they will be True in ctx.needs_input_grad.
What about first True which  wx+b w.r.t. x? It is figuring out two things:

More work underway. There are layers in the back process.
Select correct var which is x and take derivative w.r.t. to it to take part in a chain rule.

ctx.needs_input_grad (True, True, True) - more work to do, first True, which is grad_input in other words x.
ctx.needs_input_grad (False, True, True) - no more work to do, first False. No more backprop.

x requires_grad = False by default.