I cannot do save_for_backward(weights, Mpre) because Mpre is not a tensor, and I get the error:

TypeError: save_for_backward can only save variables, but argument 1 is of type SuperLU

That is why I used ctx.intermediate.
When you say “for any input or output”, is it I/O of the forward function ?