I don’t know, how the function to modify the forward should work, but you could register a hook on the parameters, which create NaNs, check for invalid values during the backward pass, and set a flag which can be checked to modify your model.
After the faulty backward pass, you should zero out the gradients, check for the flag, and modify your model.
Would this workflow generally work?