Add inference mode python bindings and tests (#58045)

Summary:
Fixes https://github.com/pytorch/pytorch/issues/56608

 - Adds binding to the `c10::InferenceMode` RAII class in `torch._C._autograd.InferenceMode` through pybind. Also binds the `torch.is_inference_mode` function.
 - Adds context manager `torch.inference_mode` to manage an instance of `c10::InferenceMode` (global).  Implemented in `torch.autograd.grad_mode.py` to reuse the `_DecoratorContextManager` class.
 - Adds some tests based on those linked in the issue + several more for just the context manager

Issues/todos (not necessarily for this PR):
- Improve short inference mode description
- Small example
- Improved testing since there is no direct way of checking TLS/dispatch keys
-

Pull Request resolved: https://github.com/pytorch/pytorch/pull/58045

Reviewed By: agolynski

Differential Revision: D28390595

Pulled By: soulitzer

fbshipit-source-id: ae98fa036c6a2cf7f56e0fd4c352ff804904752c
This commit is contained in:
Jeffrey Wan
2021-05-13 08:54:28 -07:00
committed by Facebook GitHub Bot
parent 002ce5c1df
commit e71b526e7e
12 changed files with 430 additions and 6 deletions
+2 -2
View File
@@ -515,11 +515,11 @@ TEST(InferenceModeTest, TestAccessVersionCounter) {
InferenceMode guard;
t = torch::ones({1, 2, 3});
ASSERT_THROWS_WITH(t.unsafeGetTensorImpl()->version_counter().current_version(),
"Inference tensor do not track version counter.");
"Inference tensors do not track version counter.");
t.unsafeGetTensorImpl()->bump_version();
}
ASSERT_THROWS_WITH(t.unsafeGetTensorImpl()->version_counter().current_version(),
"Inference tensor do not track version counter.");
"Inference tensors do not track version counter.");
ASSERT_THROWS_WITH(t.unsafeGetTensorImpl()->bump_version(),
"Inplace update to inference tensor outside InferenceMode is not allowed.");
// Suggested workaround