forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSavedTensorHooks.cpp
39 lines (32 loc) · 1.11 KB
/
SavedTensorHooks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <ATen/SavedTensorHooks.h>
#include <c10/util/Exception.h>
namespace at {
namespace {
// PyObject is defined in c10/util/python_stub.h
// Reference counting is handled by the caller of `set_hooks`.
thread_local PyObject* pack_hook_(nullptr);
thread_local PyObject* unpack_hook_(nullptr);
// This flag is set to true the first time default hooks are registered
// and left at true for the rest of the execution.
// It's an optimization so that users who never use default hooks don't need to
// read the thread_local variables pack_hook_ and unpack_hook_.
static bool is_enabled(false);
}
void SavedTensorDefaultHooks::enable() {
is_enabled = true;
}
void SavedTensorDefaultHooks::set_hooks(PyObject* pack_hook, PyObject* unpack_hook) {
if (!is_enabled) {
TORCH_INTERNAL_ASSERT(pack_hook == nullptr && unpack_hook == nullptr);
return;
}
pack_hook_ = pack_hook;
unpack_hook_ = unpack_hook;
}
std::pair<PyObject*, PyObject*> SavedTensorDefaultHooks::get_hooks() {
if (!is_enabled) {
return std::make_pair(nullptr, nullptr);
}
return std::make_pair(pack_hook_, unpack_hook_);
}
}