/* 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 #include #include #include #include #include // temp for debug #include #include // NOLINT #include #include #include #include "paddle/phi/core/generator.h" namespace paddle { namespace framework { 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; } struct Generator : public phi::Generator { Generator() { auto seed = GetRandomSeed(); std::seed_seq seq({seed}); auto engine = std::make_shared(seq); this->state_.cpu_engine = *engine; this->state_.device = -1; 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; } explicit Generator(uint64_t seed) { std::seed_seq seq({seed}); auto engine = std::make_shared(seq); this->state_.cpu_engine = *engine; this->state_.device = -1; 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; } Generator(uint64_t seed, uint64_t device_id) { std::seed_seq seq({seed}); auto engine = std::make_shared(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; } Generator(const Generator& other) = delete; // get random state phi::Generator::GeneratorState GetState(); // set random state void SetState(const phi::Generator::GeneratorState&); // get current seed uint64_t GetCurrentSeed(); // random a seed and get uint64_t Seed(); // set seed void SetCurrentSeed(uint64_t seed); // get cpu engine std::shared_ptr GetCPUEngine(); // set cpu engine void SetCPUEngine(std::shared_ptr); uint64_t Random64(); std::pair IncrementOffset(uint64_t increament_offset); uint64_t get_device_id() { return this->state_.device; } private: phi::Generator::GeneratorState state_; std::shared_ptr engine_; mutable std::mutex mu_; }; // The DefaultCPUGenerator is used in manual_seed() const std::shared_ptr& DefaultCPUGenerator(); const std::shared_ptr& DefaultCUDAGenerator(int64_t device_id = -1); const std::shared_ptr& DefaultXPUGenerator(int64_t device_id = -1); std::shared_ptr GetCPURandomEngine(uint64_t); const std::shared_ptr& SetRandomSeedGenerator( const std::string& name, uint64_t seed); const std::shared_ptr& GetRandomSeedGenerator( const std::string& name); } // namespace framework } // namespace paddle