generator.h 4.2 KB
Newer Older
Y
yaoxuefeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

L
Leo Chen 已提交
17
#include <glog/logging.h>
Y
yaoxuefeng 已提交
18 19 20 21 22 23 24 25 26 27
#include <stdint.h>
#include <atomic>
#include <deque>
#include <iostream>  // temp for debug
#include <memory>
#include <mutex>  // NOLINT
#include <random>
#include <typeinfo>
#include <utility>

W
Wilber 已提交
28 29
#include "paddle/pten/core/generator.h"

Y
yaoxuefeng 已提交
30 31 32
namespace paddle {
namespace framework {

L
Leo Chen 已提交
33 34 35 36 37 38
static uint64_t GetRandomSeed() {
  std::random_device rd;
  // double has 53 bit significant, so limit uint64 to 53 bits
  return ((((uint64_t)rd()) << 32) + rd()) & 0x1FFFFFFFFFFFFF;
}

W
Wilber 已提交
39
struct Generator : public pten::Generator {
Y
yaoxuefeng 已提交
40
  Generator() {
L
Leo Chen 已提交
41 42 43 44 45 46
    auto seed = GetRandomSeed();
    std::seed_seq seq({seed});
    auto engine = std::make_shared<std::mt19937_64>(seq);
    this->state_.cpu_engine = *engine;
    this->state_.device = -1;
    this->state_.current_seed = seed;
Y
yaoxuefeng 已提交
47
    this->state_.thread_offset = 0;
L
Leo Chen 已提交
48 49 50 51 52 53 54 55 56 57
    this->engine_ = engine;
    VLOG(4) << "initial seed: " << this->state_.current_seed
            << ", cpu engine: " << &this->state_.cpu_engine;
  }
  explicit Generator(uint64_t seed) {
    std::seed_seq seq({seed});
    auto engine = std::make_shared<std::mt19937_64>(seq);
    this->state_.cpu_engine = *engine;
    this->state_.device = -1;
    this->state_.current_seed = seed;
Y
yaoxuefeng 已提交
58
    this->state_.thread_offset = 0;
L
Leo Chen 已提交
59 60 61 62
    this->engine_ = engine;
    VLOG(4) << "initial seed: " << this->state_.current_seed
            << ", cpu engine: " << &this->state_.cpu_engine;
    this->is_init_py_ = true;  // TODO(zhiqiu): remove it in future
Y
yaoxuefeng 已提交
63
  }
Y
yaoxuefeng 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76
  Generator(uint64_t seed, uint64_t device_id) {
    std::seed_seq seq({seed});
    auto engine = std::make_shared<std::mt19937_64>(seq);
    this->state_.cpu_engine = *engine;
    this->state_.device = device_id;
    this->state_.current_seed = seed;
    this->state_.thread_offset = 0;
    this->engine_ = engine;
    VLOG(4) << "initial seed: " << this->state_.current_seed
            << ", cpu engine: " << &this->state_.cpu_engine;
    this->is_init_py_ = false;  // TODO(zhiqiu): remove it in future
  }

L
Leo Chen 已提交
77
  Generator(const Generator& other) = delete;
Y
yaoxuefeng 已提交
78 79

  // get random state
W
Wilber 已提交
80
  pten::Generator::GeneratorState GetState();
Y
yaoxuefeng 已提交
81
  // set random state
W
Wilber 已提交
82
  void SetState(const pten::Generator::GeneratorState&);
Y
yaoxuefeng 已提交
83 84 85 86 87 88 89
  // get current seed
  uint64_t GetCurrentSeed();
  // random a seed and get
  uint64_t Seed();
  // set seed
  void SetCurrentSeed(uint64_t seed);
  // get cpu engine
L
Leo Chen 已提交
90
  std::shared_ptr<std::mt19937_64> GetCPUEngine();
Y
yaoxuefeng 已提交
91
  // set cpu engine
L
Leo Chen 已提交
92
  void SetCPUEngine(std::shared_ptr<std::mt19937_64>);
Y
yaoxuefeng 已提交
93 94 95

  uint64_t Random64();

Y
yaoxuefeng 已提交
96 97
  std::pair<uint64_t, uint64_t> IncrementOffset(uint64_t increament_offset);

L
Leo Chen 已提交
98 99
  void SetIsInitPy(bool);
  bool GetIsInitPy() const;
Y
yaoxuefeng 已提交
100
  uint64_t get_device_id() { return this->state_.device; }
Y
yaoxuefeng 已提交
101

L
Leo Chen 已提交
102
 private:
W
Wilber 已提交
103
  pten::Generator::GeneratorState state_;
L
Leo Chen 已提交
104 105 106 107 108 109 110 111
  std::shared_ptr<std::mt19937_64> engine_;
  mutable std::mutex mu_;

  // NOTE(zhiqiu): is_init_py_ is used to make generator be compatible with
  // old seed, and it should be removed after all random-related operators
  // and unittests upgrades to use generator.
  bool is_init_py_ = false;
};
Y
yaoxuefeng 已提交
112

L
Leo Chen 已提交
113 114
// The DefaultCPUGenerator is used in manual_seed()
const std::shared_ptr<Generator>& DefaultCPUGenerator();
Y
yaoxuefeng 已提交
115

L
Leo Chen 已提交
116 117
// If op seed is set or global is not set, the OpDefaultCPUEngine is used.
std::shared_ptr<std::mt19937_64> OpDefaultCPUEngine();
Y
yaoxuefeng 已提交
118

L
Leo Chen 已提交
119
std::shared_ptr<std::mt19937_64> GetCPURandomEngine(uint64_t);
Y
yaoxuefeng 已提交
120

Y
yaoxuefeng 已提交
121 122 123
const std::shared_ptr<Generator>& GetDefaultCUDAGenerator(
    int64_t device_id = -1);

124 125 126 127 128 129
const std::shared_ptr<Generator>& SetRandomSeedGenerator(
    const std::string& name, uint64_t seed);

const std::shared_ptr<Generator>& GetRandomSeedGenerator(
    const std::string& name);

Y
yaoxuefeng 已提交
130 131
}  // namespace framework
}  // namespace paddle