Wei_Chen:

input_var = Variable(torch.from_numpy(X), requires_grad=True).type(torch.FloatTensor)


Your input_var is an intermediate result, nor a leaf because you created a variable that requires_grad, and then .type creates a new one that is an intermediate result.
Also, your code has some other issues:

You don’t need Variable wrappers anymore. Just write input_var = torch.as_tensor(X, dtype=torch.float).
You should use model_2(input_var), rather than directly the forward function. It skips some hooks and can cause incorrect results in certain cases.

retain_graph isn’t needed in your case.