Tensor::register_hook: Avoid wrapping hook in two levels of std::function (#53917)
Summary:
The void overload of `register_hook` puts the user's callable into a `std::function` which is used in a lambda, then `_register_hook` wraps that lambda in another `std::function`. This is bad because each call goes through two indirections and also it requires more heap allocations.
Instead, the lambda can capture the original callable without wrapping it in an `std::function` first.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53917
Reviewed By: gchanan
Differential Revision: D27513822
Pulled By: swolchok
fbshipit-source-id: 026d40d7e9fb718757b7203737b0662ba36bc021