forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrad_mode.h
More file actions
30 lines (24 loc) · 740 Bytes
/
Copy pathgrad_mode.h
File metadata and controls
30 lines (24 loc) · 740 Bytes
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
#pragma once
#include <c10/macros/Macros.h>
namespace at {
struct CAFFE2_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 CAFFE2_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 CAFFE2_API NoGradGuard : public AutoGradMode {
NoGradGuard() : AutoGradMode(/*enabled=*/false) {}
};
}