forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saved_variable.h
77 lines (64 loc) · 2.69 KB
/
saved_variable.h
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include <torch/csrc/WindowsTorchApiMacro.h>
#include <torch/csrc/autograd/forward_grad.h>
#include <ATen/ATen.h>
#include <cstdint>
#include <memory>
namespace torch { namespace autograd {
using Variable = at::Tensor;
struct Node;
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
TORCH_API extern const char* ERR_BACKWARD_TWICE;
/// A snapshot of a variable at a certain version. A `SavedVariable` stores
/// enough information to reconstruct a variable from a certain point in time.
class TORCH_API SavedVariable {
public:
SavedVariable() = default;
SavedVariable(const Variable& variable, bool is_output, bool is_inplace_on_view=false);
SavedVariable(const c10::optional<Variable>& variable, bool is_output, bool is_inplace_on_view=false);
SavedVariable(SavedVariable&&) = default;
SavedVariable& operator=(SavedVariable&&) = default;
~SavedVariable() {
if (fw_grad_) {
// See note [ Using ForwardGrad ]
fw_grad_->clear();
}
}
/// Reconstructs the saved variable. Pass `saved_for` as the gradient
/// function if constructing the `SavedVariable` with it would have caused a
/// circular reference.
Variable unpack(std::shared_ptr<Node> saved_for = nullptr) const;
void reset_data() {
return data_.reset();
}
private:
// This field contains either:
// 1. the variable to save
// 2. or its tensor_data.
// If storing the variable itself would create a circular reference,
// we fall into the second case and its metadata is also saved separately.
// In that case, the grad_fn must be passed in to the unpack function when
// reconstructing the Variable (except when we are doing an inplace operation on
// a view, see below).
// The field saved_orignal_ below reflects the two cases: its value is true
// in the first case and false in the second case.
at::Tensor data_;
// This field is used to store the forward AD gradients associated with
// the saved Tensor. Note that this shared_ptr must never be shared with
// either the saved Tensor or the unpacked Tensor. See note [ Using ForwardGrad ]
std::shared_ptr<ForwardGrad> fw_grad_;
// Weak version of grad_fn_ that prevents leaks in rebase_history() for
// inplace views.
// This variable is used when the user chooses to create a SavedVariable with
// is_inplace_on_view = true.
// In that case, the grad_fn passed in to the unpack function at unwrapping
// time is unused.
std::weak_ptr<Node> weak_grad_fn_;
c10::VariableVersion version_counter_;
uint32_t saved_version_ = 0;
uint32_t output_nr_ = 0;
bool was_default_constructed_ = true;
bool is_inplace_on_view_ = false;
bool saved_original_ = false;
};
}} // namespace torch::autograd