Caffe2 - C++ API
A deep learning, cross platform ML framework
variable_version.h
1 #pragma once
2 
3 #include <atomic>
4 #include <cstdint>
5 #include <memory>
6 
7 // Every Variable has a version counter. Version counters are incremented
8 // whenever the data or shape of a tensor changes through Variable operations.
9 // These are typicallly in-place operations. Version counters are used to
10 // detect modifications to saved variables which would result in incorrect
11 // gradient calculations. Version counters may be shared between Variables:
12 //
13 // 1. A view shares the version counter of the base Variable,
14 // 2. Detached variables share the version counter of the source,
15 // 3. Unpacked saved variables share the version counter of the source.
16 
17 namespace torch { namespace autograd {
18 
20  public:
21  // NOTE: As of C++11 and 14, default-constructing a std::atomic variable
22  // leaves it in a persistently undefined state. See
23  // https://cplusplus.github.io/LWG/issue2334.
24  VariableVersion(uint32_t version = 0)
25  : version_block_(std::make_shared<std::atomic<uint32_t>>(version)) {}
26 
27  void bump() noexcept {
28  version_block_->fetch_add(1);
29  }
30 
31  uint32_t current_version() const noexcept {
32  return version_block_->load();
33  }
34 
35  private:
36  std::shared_ptr<std::atomic<uint32_t>> version_block_;
37 };
38 }} // namespace torch::autograd
Definition: jit_type.h:17