Refactor hotpatch_vars and apply it to libtorch (#14976)
Summary:
Fixes #14801.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14976
Differential Revision: D13485381
Pulled By: soumith
fbshipit-source-id: 0af3c2e1b90988d56f6f85632328d1e4b788ffd2