generator.cc 4.2 KB
Newer Older
Y
yaoxuefeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

L
Leo Chen 已提交
15 16 17 18
#include "paddle/fluid/framework/generator.h"

#include <glog/logging.h>

Y
yaoxuefeng 已提交
19 20 21 22 23 24 25 26 27
#include <deque>
#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <utility>

namespace paddle {
namespace framework {

L
Leo Chen 已提交
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
const std::shared_ptr<Generator>& DefaultCPUGenerator() {
  static auto default_cpu_generator =
      std::make_shared<Generator>(GetRandomSeed());
  VLOG(4) << "initial seed: " << default_cpu_generator->GetCurrentSeed()
          << ", cpu engine: " << default_cpu_generator->GetCPUEngine().get();
  return default_cpu_generator;
}

std::shared_ptr<std::mt19937_64> OpDefaultCPUEngine() {
  static auto op_default_cpu_engine = std::make_shared<std::mt19937_64>();
  return op_default_cpu_engine;
}

// NOTE(zhiqiu): there are 3 conditions:
// (1) op seed is not set and DefaultCPUGenerator is inited, use
// DefaultCPUGenerator
// (2) op seed is not set and DefaultCPUGenerator is not inited, use se
// OpDefaultCPUEngine() and set a radnom seed
// (3) op seed is set, use OpDefaultCPUEngine() and set the seed
std::shared_ptr<std::mt19937_64> GetCPURandomEngine(uint64_t seed) {
  if (DefaultCPUGenerator()->GetIsInitPy() && seed == 0) {
    VLOG(4) << "Use random engine from generator";
    return DefaultCPUGenerator()->GetCPUEngine();
  } else {
    // NOTE(zhiqiu): creating an engine instance everytime instead of using
    // OpDefaultCPUEngine(), this is the legacy behavior of random operators.
    // The benefit is that when runing PE with fixed-seed in multiple thrads,
    // each thread has their own engine, and doesn't affect each other.
    //
    // And we need to measure the determinacy of Generator in PE.
    auto engine = std::make_shared<std::mt19937_64>();
    if (seed == 0) {
      seed = GetRandomSeed();
      VLOG(4) << "Use default random engine with random seed = " << seed;
    } else {
      VLOG(4) << "Use default random engine with fixed random seed = " << seed;
    }
    static std::mutex mu_;
    {
      std::lock_guard<std::mutex> lock(mu_);
      engine->seed(seed);
    }
    return engine;
  }
}
Y
yaoxuefeng 已提交
73

L
Leo Chen 已提交
74 75 76 77
GeneratorState Generator::GetState() {
  std::lock_guard<std::mutex> lock(this->mu_);
  state_.cpu_engine = *engine_;
  return this->state_;
Y
yaoxuefeng 已提交
78 79
}

L
Leo Chen 已提交
80 81 82 83
void Generator::SetState(const GeneratorState& state) {
  std::lock_guard<std::mutex> lock(this->mu_);
  this->state_ = state;
  this->engine_ = std::make_shared<std::mt19937_64>(state.cpu_engine);
Y
yaoxuefeng 已提交
84 85 86
}

uint64_t Generator::GetCurrentSeed() {
L
Leo Chen 已提交
87 88
  std::lock_guard<std::mutex> lock(this->mu_);
  return this->state_.current_seed;
Y
yaoxuefeng 已提交
89 90 91
}

uint64_t Generator::Seed() {
L
Leo Chen 已提交
92
  std::lock_guard<std::mutex> lock(this->mu_);
Y
yaoxuefeng 已提交
93 94 95
  uint64_t seed;
  std::random_device de;
  seed = ((((uint64_t)de()) << 32) + de()) & 0x1FFFFFFFFFFFFF;
L
Leo Chen 已提交
96
  this->state_.current_seed = seed;
Y
yaoxuefeng 已提交
97
  std::seed_seq seq({seed});
L
Leo Chen 已提交
98
  this->engine_->seed(seq);
Y
yaoxuefeng 已提交
99

L
Leo Chen 已提交
100
  return this->state_.current_seed;
Y
yaoxuefeng 已提交
101 102 103
}

void Generator::SetCurrentSeed(uint64_t seed) {
L
Leo Chen 已提交
104 105
  std::lock_guard<std::mutex> lock(this->mu_);
  this->state_.current_seed = seed;
Y
yaoxuefeng 已提交
106
  std::seed_seq seq({seed});
L
Leo Chen 已提交
107
  this->engine_->seed(seq);
Y
yaoxuefeng 已提交
108 109
}

L
Leo Chen 已提交
110 111 112
std::shared_ptr<std::mt19937_64> Generator::GetCPUEngine() {
  std::lock_guard<std::mutex> lock(this->mu_);
  return this->engine_;
Y
yaoxuefeng 已提交
113 114
}

L
Leo Chen 已提交
115 116 117
void Generator::SetCPUEngine(std::shared_ptr<std::mt19937_64> engine) {
  std::lock_guard<std::mutex> lock(this->mu_);
  this->engine_ = engine;
Y
yaoxuefeng 已提交
118 119 120
}

uint64_t Generator::Random64() {
L
Leo Chen 已提交
121 122 123 124 125 126 127 128
  std::lock_guard<std::mutex> lock(this->mu_);
  auto engine = this->engine_;
  return (*engine)();
}

void Generator::SetIsInitPy(bool is_init_py) {
  this->is_init_py_ = is_init_py;
  VLOG(4) << "SetIsInitPy:" << this->is_init_py_;
Y
yaoxuefeng 已提交
129
}
L
Leo Chen 已提交
130
bool Generator::GetIsInitPy() const { return this->is_init_py_; }
Y
yaoxuefeng 已提交
131 132 133

}  // namespace framework
}  // namespace paddle