Skip to content

Instantly share code, notes, and snippets.

@syed-ahmed
Created July 1, 2019 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syed-ahmed/16682e6b4cfe5fa7e07928c828772947 to your computer and use it in GitHub Desktop.
Save syed-ahmed/16682e6b4cfe5fa7e07928c828772947 to your computer and use it in GitHub Desktop.
diff --git a/aten/src/ATen/CPUGenerator.cpp b/aten/src/ATen/CPUGenerator.cpp
index c0b6e30..97cbb4d 100644
--- a/aten/src/ATen/CPUGenerator.cpp
+++ b/aten/src/ATen/CPUGenerator.cpp
@@ -50,6 +50,7 @@ inline uint64_t make64BitsFrom32Bits(uint32_t hi, uint32_t lo) {
CPUGenerator::CPUGenerator(uint64_t seed_in)
: Generator{Device(DeviceType::CPU)},
engine_{seed_in},
+ engine2{seed_in, 0, 0},
next_float_normal_sample_{c10::optional<float>()},
next_double_normal_sample_{c10::optional<double>()} { }
@@ -61,6 +62,7 @@ void CPUGenerator::set_current_seed(uint64_t seed) {
next_float_normal_sample_.reset();
next_double_normal_sample_.reset();
engine_ = mt19937(seed);
+ engine2 = Philox4_32_10(seed, 0, 0);
}
/**
@@ -83,6 +85,10 @@ uint64_t CPUGenerator::seed() {
return random;
}
+void CPUGenerator::increment_philox(uint64_t increment) {
+ engine2.incr_n(increment);
+}
+
/**
* Gets the DeviceType of CPUGenerator.
* Used for type checking during run time.
@@ -97,7 +103,7 @@ DeviceType CPUGenerator::device_type() {
* See Note [Acquire lock when using random generators]
*/
uint32_t CPUGenerator::random() {
- return engine_();
+ return engine2();
}
/**
@@ -106,8 +112,8 @@ uint32_t CPUGenerator::random() {
* See Note [Acquire lock when using random generators]
*/
uint64_t CPUGenerator::random64() {
- uint32_t random1 = engine_();
- uint32_t random2 = engine_();
+ uint32_t random1 = engine2();
+ uint32_t random2 = engine2();
return detail::make64BitsFrom32Bits(random1, random2);
}
diff --git a/aten/src/ATen/CPUGenerator.h b/aten/src/ATen/CPUGenerator.h
index f41142a..656a441 100644
--- a/aten/src/ATen/CPUGenerator.h
+++ b/aten/src/ATen/CPUGenerator.h
@@ -26,10 +26,12 @@ struct CAFFE2_API CPUGenerator : public Generator {
void set_next_double_normal_sample(c10::optional<double> randn);
at::mt19937 engine();
void set_engine(at::mt19937 engine);
+ void increment_philox(uint64_t increment);
private:
CPUGenerator* clone_impl() const override;
at::mt19937 engine_;
+ at::Philox4_32_10 engine2;
c10::optional<float> next_float_normal_sample_;
c10::optional<double> next_double_normal_sample_;
};
diff --git a/aten/src/ATen/core/PhiloxRNGEngine.h b/aten/src/ATen/core/PhiloxRNGEngine.h
index 9a283f1..6b69778 100644
--- a/aten/src/ATen/core/PhiloxRNGEngine.h
+++ b/aten/src/ATen/core/PhiloxRNGEngine.h
@@ -162,11 +162,16 @@ public:
++counter[3];
}
+ C10_HOST_DEVICE inline uint64_t seed() const {
+ return seed_;
+ }
+
private:
detail::UINT4 counter;
detail::UINT4 output;
detail::UINT2 key;
uint32_t STATE;
+ uint64_t seed_;
C10_HOST_DEVICE inline uint32_t mulhilo32(uint32_t a, uint32_t b,
uint32_t *result_high) {
diff --git a/aten/src/TH/generic/THTensorRandom.cpp b/aten/src/TH/generic/THTensorRandom.cpp
index 1b3dcf9..0c03c5b 100644
--- a/aten/src/TH/generic/THTensorRandom.cpp
+++ b/aten/src/TH/generic/THTensorRandom.cpp
@@ -78,14 +78,23 @@ void THTensor_(geometric)(THTensor *self, at::Generator *_generator, double p)
void THTensor_(uniform)(THTensor *self, at::Generator *_generator, double a, double b)
{
+ auto size = THTensor_(numel)(self);
auto gen = at::get_generator_or_default<at::CPUGenerator>(_generator, at::detail::getDefaultCPUGenerator());
- // See Note [Acquire lock when using random generators]
- std::lock_guard<std::mutex> lock(gen->mutex_);
#if defined(TH_REAL_IS_FLOAT)
+ {
+ // See Note [Acquire lock when using random generators]
+ std::lock_guard<std::mutex> lock(gen->mutex_);
+ gen->increment_philox(size + 3 / 4);
+ }
at::uniform_real_distribution<float> uniform((float)a, (float)b);
TH_TENSOR_APPLY(scalar_t, self, *self_data = (scalar_t)uniform(gen););
#else
+ {
+ // See Note [Acquire lock when using random generators]
+ std::lock_guard<std::mutex> lock(gen->mutex_);
+ gen->increment_philox(2*size + 3 / 4);
+ }
at::uniform_real_distribution<double> uniform(a, b);
TH_TENSOR_APPLY(scalar_t, self, *self_data = (scalar_t)uniform(gen););
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment