Reset checkpoint_valid flag when error happens during function execution (#51746)
Summary:
Fixes https://github.com/pytorch/pytorch/issues/37874, https://github.com/pytorch/pytorch/issues/51743
Uses RAII to manage the flag so that it gets reset properly on exception
Pull Request resolved: https://github.com/pytorch/pytorch/pull/51746
Reviewed By: izdeby
Differential Revision: D26319619
Pulled By: soulitzer
fbshipit-source-id: ea1235438ba516f99195c83fa23d5880f9977c93