/
usr
/
local
/
lib64
/
python3.6
/
site-packages
/
torch
/
include
/
c10
/
core
/
/usr/local/lib64/python3.6/site-packages/torch/include/c10/core
mkdir
upload
Name
Size
Mode
Actions
impl/
-
0755
rm
Allocator.h
8725
0644
edit
dl
rm
AutogradState.h
994
0644
edit
dl
rm
Backend.h
8487
0644
edit
dl
rm
CompileTimeFunctionPointer.h
1677
0644
edit
dl
rm
CopyBytes.h
1229
0644
edit
dl
rm
CPUAllocator.h
2267
0644
edit
dl
rm
DefaultDtype.h
394
0644
edit
dl
rm
DefaultTensorOptions.h
1032
0644
edit
dl
rm
Device.h
5396
0644
edit
dl
rm
DeviceGuard.h
7555
0644
edit
dl
rm
DeviceType.h
2994
0644
edit
dl
rm
DispatchKey.h
18069
0644
edit
dl
rm
DispatchKeySet.h
12951
0644
edit
dl
rm
Event.h
4169
0644
edit
dl
rm
GeneratorImpl.h
3713
0644
edit
dl
rm
GradMode.h
1261
0644
edit
dl
rm
InferenceMode.h
3471
0644
edit
dl
rm
Layout.h
1225
0644
edit
dl
rm
MemoryFormat.h
8571
0644
edit
dl
rm
OptionalRef.h
521
0644
edit
dl
rm
QEngine.h
861
0644
edit
dl
rm
QScheme.h
1562
0644
edit
dl
rm
Scalar.h
6019
0644
edit
dl
rm
ScalarType.h
17073
0644
edit
dl
rm
ScalarTypeToTypeMeta.h
1365
0644
edit
dl
rm
Storage.h
4369
0644
edit
dl
rm
StorageImpl.h
5610
0644
edit
dl
rm
Stream.h
7373
0644
edit
dl
rm
StreamGuard.h
6315
0644
edit
dl
rm
TensorImpl.h
96152
0644
edit
dl
rm
TensorOptions.h
27593
0644
edit
dl
rm
thread_pool.h
2992
0644
edit
dl
rm
UndefinedTensorImpl.h
911
0644
edit
dl
rm
WrapDimMinimal.h
805
0644
edit
dl
rm
Edit:
/usr/local/lib64/python3.6/site-packages/torch/include/c10/core/InferenceMode.h
(3471B)
#pragma once #include <c10/core/AutogradState.h> #include <c10/core/GradMode.h> #include <c10/core/impl/LocalDispatchKeySet.h> #include <c10/macros/Macros.h> namespace c10 { // A RAII, thread local (!) guard that enables or disables inference mode upon // construction, and sets it back to the original value upon destruction. struct TORCH_API InferenceMode { // Note [Expected TLS state in InferenceMode]: // InferenceMode: ADInplaceOrView not in // raw_local_dispatch_key_set.included(), // Autograd in raw_local_dispatch_key_set.excluded() // GradMode is disabled. // NormalMode: ADInplaceOrView in raw_local_dispatch_key_set.included(), // Autograd not in raw_local_dispatch_key_set.excluded() // GradMode is enabled by default unless toggled manually // through other APIs, e.g. NoGradGuard. // // Invariant: // - ADInplaceOrView is never in the excluded set // - Autograd is never in the included set // - Setting InferenceMode will set GradMode accordingly, but not vice versa. // // 1. Why do we put ADInplaceOrView in included set outside InferenceMode? // // Inplace update to inference tensor outside InferenceMode is not // allowed. See Note [Inplace update inference tensor] for more details. // Without going through ADInplaceOrView kernel, we cannot throw error // for `inference_tensor.add_(1)` case. // // 2. Why not put ADInplaceOrView in the excluded set inside InferenceMode? // // For example: // torch::Tensor a = torch::ones({1, 2, 3}).set_requires_grad(true); // torch::Tensor k = a + 2; // { // c10::InferenceMode guard(true); // k.add_(2); // } // `k.add_(2)` still need to go through ADInplaceOrView kernel so that it's // prepared for future autograd. // // 3. Why does setting InferenceMode also set GradMode? // // This is required since InferenceMode is a faster and more restricive // version of NoGradGuard. All runtime checks using GradMode::is_enabled() // are applicable to InferenceMode as well, e.g. // `tensorTypeInCurrentExecutionContext` in interpreter.cpp. InferenceMode(bool enabled = true) : prev_mode(AutogradState::get_tls_state()), prev_keyset(c10::impl::tls_local_dispatch_key_set()) { // Enabling inference mode means disabling grad modes // And disabling inference mode means enabling grad modes AutogradState::set_tls_state(AutogradState( /* grad_mode */ !enabled, /* inference_mode */ enabled, /* fw_grad_mode */ !enabled)); DispatchKeySet included = enabled ? prev_keyset.included_.remove(c10::DispatchKey::ADInplaceOrView) : prev_keyset.included_.add(c10::DispatchKey::ADInplaceOrView); DispatchKeySet excluded = enabled ? (prev_keyset.excluded_ | c10::autograd_dispatch_keyset) : (prev_keyset.excluded_ - c10::autograd_dispatch_keyset); c10::impl::PODLocalDispatchKeySet cur_keyset; cur_keyset.set_included(included); cur_keyset.set_excluded(excluded); c10::impl::_force_tls_local_dispatch_key_set(cur_keyset); } ~InferenceMode() { AutogradState::set_tls_state(prev_mode); c10::impl::_force_tls_local_dispatch_key_set(prev_keyset); } static bool is_enabled(); private: AutogradState prev_mode; c10::impl::LocalDispatchKeySet prev_keyset; }; } // namespace c10
Save
cmd:
run