|
| 1 | +/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <stdint.h> |
| 18 | +#include <atomic> |
| 19 | +#include <deque> |
| 20 | +#include <iostream> // temp for debug |
| 21 | +#include <memory> |
| 22 | +#include <mutex> // NOLINT |
| 23 | +#include <random> |
| 24 | +#include <typeinfo> |
| 25 | +#include <utility> |
| 26 | + |
| 27 | +namespace paddle { |
| 28 | +namespace framework { |
| 29 | + |
| 30 | +struct GeneratorState { |
| 31 | + int64_t device = -1; |
| 32 | + uint64_t current_seed = 34342423252; |
| 33 | + std::mt19937_64 cpu_engine; |
| 34 | +}; |
| 35 | + |
| 36 | +struct Generator { |
| 37 | + Generator() { |
| 38 | + GeneratorState default_gen_state_cpu; |
| 39 | + default_gen_state_cpu.device = -1; |
| 40 | + default_gen_state_cpu.current_seed = 34342423252; |
| 41 | + std::seed_seq seq({34342423252}); |
| 42 | + default_gen_state_cpu.cpu_engine = std::mt19937_64(seq); |
| 43 | + this->state_ = std::make_shared<GeneratorState>(default_gen_state_cpu); |
| 44 | + } |
| 45 | + explicit Generator(GeneratorState state_in) |
| 46 | + : state_{std::make_shared<GeneratorState>(state_in)} {} |
| 47 | + Generator(const Generator& other) |
| 48 | + : Generator(other, std::lock_guard<std::mutex>(other.mutex)) {} |
| 49 | + |
| 50 | + // get random state |
| 51 | + GeneratorState* GetState(); |
| 52 | + // set random state |
| 53 | + void SetState(GeneratorState* state_in); |
| 54 | + // get current seed |
| 55 | + uint64_t GetCurrentSeed(); |
| 56 | + // random a seed and get |
| 57 | + uint64_t Seed(); |
| 58 | + |
| 59 | + // set seed |
| 60 | + void SetCurrentSeed(uint64_t seed); |
| 61 | + // get cpu engine |
| 62 | + std::mt19937_64& GetCPUEngine(); |
| 63 | + // set cpu engine |
| 64 | + void SetCPUEngine(std::mt19937_64 engine); |
| 65 | + |
| 66 | + uint64_t Random64(); |
| 67 | + |
| 68 | + bool is_init_py = false; |
| 69 | + |
| 70 | + // CPU Generator singleton |
| 71 | + static std::shared_ptr<Generator> GetInstance() { |
| 72 | + if (NULL == gen_instance_) { |
| 73 | + gen_instance_.reset(new paddle::framework::Generator()); |
| 74 | + } |
| 75 | + return gen_instance_; |
| 76 | + } |
| 77 | + |
| 78 | + static std::shared_ptr<Generator> GetInstanceX() { |
| 79 | + if (NULL == gen_instance_) { |
| 80 | + gen_instance_.reset(new paddle::framework::Generator()); |
| 81 | + } |
| 82 | + gen_instance_->is_init_py = true; |
| 83 | + return gen_instance_; |
| 84 | + } |
| 85 | + |
| 86 | + private: |
| 87 | + static std::shared_ptr<Generator> gen_instance_; |
| 88 | + std::shared_ptr<GeneratorState> state_; |
| 89 | + mutable std::mutex mutex; |
| 90 | + |
| 91 | + Generator(const Generator& other, const std::lock_guard<std::mutex>&) |
| 92 | + : state_(std::make_shared<GeneratorState>(*(other.state_))) {} |
| 93 | +}; |
| 94 | + |
| 95 | +} // namespace framework |
| 96 | +} // namespace paddle |
0 commit comments