forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPUGeneratorImpl.cpp
176 lines (154 loc) · 4.46 KB
/
CPUGeneratorImpl.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <ATen/CPUGeneratorImpl.h>
#include <c10/util/C++17.h>
#include <algorithm>
namespace at {
namespace detail {
/**
* PyTorch maintains a collection of default generators that get
* initialized once. The purpose of these default generators is to
* maintain a global running state of the pseudo random number generation,
* when a user does not explicitly mention any generator.
* getDefaultCPUGenerator gets the default generator for a particular
* device.
*/
const Generator& getDefaultCPUGenerator() {
static auto default_gen_cpu = createCPUGenerator(c10::detail::getNonDeterministicRandom());
return default_gen_cpu;
}
/**
* Utility to create a CPUGeneratorImpl. Returns a shared_ptr
*/
Generator createCPUGenerator(uint64_t seed_val) {
return make_generator<CPUGeneratorImpl>(seed_val);
}
/**
* Helper function to concatenate two 32 bit unsigned int
* and return them as a 64 bit unsigned int
*/
inline uint64_t make64BitsFrom32Bits(uint32_t hi, uint32_t lo) {
return (static_cast<uint64_t>(hi) << 32) | lo;
}
} // namespace detail
/**
* CPUGeneratorImpl class implementation
*/
CPUGeneratorImpl::CPUGeneratorImpl(uint64_t seed_in)
: c10::GeneratorImpl{Device(DeviceType::CPU), DispatchKeySet(c10::DispatchKey::CPU)},
engine_{seed_in},
next_float_normal_sample_{c10::optional<float>()},
next_double_normal_sample_{c10::optional<double>()} { }
/**
* Manually seeds the engine with the seed input
* See Note [Acquire lock when using random generators]
*/
void CPUGeneratorImpl::set_current_seed(uint64_t seed) {
next_float_normal_sample_.reset();
next_double_normal_sample_.reset();
engine_ = mt19937(seed);
}
/**
* Gets the current seed of CPUGeneratorImpl.
*/
uint64_t CPUGeneratorImpl::current_seed() const {
return engine_.seed();
}
/**
* Gets a nondeterministic random number from /dev/urandom or time,
* seeds the CPUGeneratorImpl with it and then returns that number.
*
* FIXME: You can move this function to Generator.cpp if the algorithm
* in getNonDeterministicRandom is unified for both CPU and CUDA
*/
uint64_t CPUGeneratorImpl::seed() {
auto random = c10::detail::getNonDeterministicRandom();
this->set_current_seed(random);
return random;
}
/**
* Gets the DeviceType of CPUGeneratorImpl.
* Used for type checking during run time.
*/
DeviceType CPUGeneratorImpl::device_type() {
return DeviceType::CPU;
}
/**
* Gets a random 32 bit unsigned integer from the engine
*
* See Note [Acquire lock when using random generators]
*/
uint32_t CPUGeneratorImpl::random() {
return engine_();
}
/**
* Gets a random 64 bit unsigned integer from the engine
*
* See Note [Acquire lock when using random generators]
*/
uint64_t CPUGeneratorImpl::random64() {
uint32_t random1 = engine_();
uint32_t random2 = engine_();
return detail::make64BitsFrom32Bits(random1, random2);
}
/**
* Get the cached normal random in float
*/
c10::optional<float> CPUGeneratorImpl::next_float_normal_sample() {
return next_float_normal_sample_;
}
/**
* Get the cached normal random in double
*/
c10::optional<double> CPUGeneratorImpl::next_double_normal_sample() {
return next_double_normal_sample_;
}
/**
* Cache normal random in float
*
* See Note [Acquire lock when using random generators]
*/
void CPUGeneratorImpl::set_next_float_normal_sample(c10::optional<float> randn) {
next_float_normal_sample_ = randn;
}
/**
* Cache normal random in double
*
* See Note [Acquire lock when using random generators]
*/
void CPUGeneratorImpl::set_next_double_normal_sample(c10::optional<double> randn) {
next_double_normal_sample_ = randn;
}
/**
* Get the engine of the CPUGeneratorImpl
*/
at::mt19937 CPUGeneratorImpl::engine() {
return engine_;
}
/**
* Set the engine of the CPUGeneratorImpl
*
* See Note [Acquire lock when using random generators]
*/
void CPUGeneratorImpl::set_engine(at::mt19937 engine) {
engine_ = engine;
}
/**
* Public clone method implementation
*
* See Note [Acquire lock when using random generators]
*/
std::shared_ptr<CPUGeneratorImpl> CPUGeneratorImpl::clone() const {
return std::shared_ptr<CPUGeneratorImpl>(this->clone_impl());
}
/**
* Private clone method implementation
*
* See Note [Acquire lock when using random generators]
*/
CPUGeneratorImpl* CPUGeneratorImpl::clone_impl() const {
auto gen = new CPUGeneratorImpl();
gen->set_engine(engine_);
gen->set_next_float_normal_sample(next_float_normal_sample_);
gen->set_next_double_normal_sample(next_double_normal_sample_);
return gen;
}
} // namespace at