mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/63117 Test Plan: Imported from OSS Reviewed By: ngimel Differential Revision: D30388097 Pulled By: albanD fbshipit-source-id: f1bc777064645db1ff848bdd64af95bffb530984
45 lines
1.2 KiB
C++
45 lines
1.2 KiB
C++
#pragma once
|
|
|
|
#include <c10/core/AutogradState.h>
|
|
#include <c10/macros/Macros.h>
|
|
|
|
namespace c10 {
|
|
|
|
struct TORCH_API GradMode {
|
|
static bool is_enabled();
|
|
static void set_enabled(bool enabled);
|
|
};
|
|
|
|
// A RAII, thread local (!) guard that enables or disables grad mode upon
|
|
// construction, and sets it back to the original value upon destruction.
|
|
struct TORCH_API AutoGradMode {
|
|
AutoGradMode(bool enabled) : prev_mode(GradMode::is_enabled()) {
|
|
GradMode::set_enabled(enabled);
|
|
}
|
|
~AutoGradMode() {
|
|
GradMode::set_enabled(prev_mode);
|
|
}
|
|
bool prev_mode;
|
|
};
|
|
|
|
// A RAII, thread local (!) guard that stops future operations from building
|
|
// gradients.
|
|
struct TORCH_API NoGradGuard : public AutoGradMode {
|
|
NoGradGuard() : AutoGradMode(/*enabled=*/false) {}
|
|
};
|
|
|
|
// A RAII, thread local (!) guard that enables or disables forward grad mode
|
|
// upon construction, and sets it back to the original value upon destruction.
|
|
struct TORCH_API AutoFwGradMode {
|
|
AutoFwGradMode(bool enabled)
|
|
: prev_mode(AutogradState::get_tls_state().get_fw_grad_mode()) {
|
|
AutogradState::get_tls_state().set_fw_grad_mode(enabled);
|
|
}
|
|
~AutoFwGradMode() {
|
|
AutogradState::get_tls_state().set_fw_grad_mode(prev_mode);
|
|
}
|
|
bool prev_mode;
|
|
};
|
|
|
|
} // namespace c10
|