diff --git a/.gitignore b/.gitignore index f88ab47d9bd2e825f6cb3c5569e7f283e302b73a..8048d2ad8a91d182f75dc97bb388100d62177315 100644 --- a/.gitignore +++ b/.gitignore @@ -93,3 +93,5 @@ paddle/fluid/pybind/eager_op_function_impl.h paddle/fluid/pybind/op_function_impl.h paddle/fluid/pybind/*final_state_op_function_impl.h paddle/fluid/prim/api/generated/prim_api/* +paddle/fluid/framework/__init__.py +python/paddle/incubate/fleet/parameter_server/pslib/ps_pb2.py diff --git a/paddle/fluid/distributed/ps/table/common_graph_table.cc b/paddle/fluid/distributed/ps/table/common_graph_table.cc index 69a912e191c435043bedaf448cdc8aae6c4d5655..f2b54def608afabfcf2b1c150d26b50705f6201c 100644 --- a/paddle/fluid/distributed/ps/table/common_graph_table.cc +++ b/paddle/fluid/distributed/ps/table/common_graph_table.cc @@ -26,11 +26,11 @@ #include "paddle/fluid/distributed/ps/table/graph/graph_node.h" #include "paddle/fluid/framework/fleet/fleet_wrapper.h" #include "paddle/fluid/framework/fleet/heter_ps/graph_gpu_wrapper.h" -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/io/fs.h" #include "paddle/fluid/platform/timer.h" #include "paddle/fluid/string/printf.h" #include "paddle/fluid/string/string_helper.h" +#include "paddle/phi/core/generator.h" DECLARE_bool(graph_load_in_parallel); DECLARE_bool(graph_get_neighbor_id); @@ -2574,7 +2574,7 @@ int32_t GraphTable::Initialize(const GraphParameter &graph) { _shards_task_pool.resize(task_pool_size_); for (size_t i = 0; i < _shards_task_pool.size(); ++i) { _shards_task_pool[i].reset(new ::ThreadPool(1)); - _shards_task_rng_pool.push_back(paddle::framework::GetCPURandomEngine(0)); + _shards_task_rng_pool.push_back(phi::GetCPURandomEngine(0)); } load_node_edge_task_pool.reset(new ::ThreadPool(load_thread_num)); diff --git a/paddle/fluid/distributed/ps/table/depends/initializers.h b/paddle/fluid/distributed/ps/table/depends/initializers.h index 7c707feacecc5ed2fde65fc9a764c0e7280be332..467227097674b1d3694ec6ba69f17ccca5f83d03 100644 --- a/paddle/fluid/distributed/ps/table/depends/initializers.h +++ b/paddle/fluid/distributed/ps/table/depends/initializers.h @@ -22,8 +22,8 @@ #include #include "gflags/gflags.h" -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/operators/truncated_gaussian_random_op.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace distributed { @@ -64,7 +64,7 @@ class UniformInitializer : public Initializer { max_ = std::stof(attrs[3]); dist_ = std::uniform_real_distribution(min_, max_); - random_engine_ = framework::GetCPURandomEngine(seed_); + random_engine_ = phi::GetCPURandomEngine(seed_); } float GetValue() override { return dist_(*random_engine_); } @@ -90,7 +90,7 @@ class GaussianInitializer : public Initializer { mean_ = std::stof(attrs[2]); std_ = std::stof(attrs[3]); - random_engine_ = framework::GetCPURandomEngine(seed_); + random_engine_ = phi::GetCPURandomEngine(seed_); dist_ = std::normal_distribution(mean_, std_); } @@ -120,7 +120,7 @@ class TruncatedGaussianInitializer : public Initializer { std::uniform_real_distribution dist_( std::numeric_limits::min(), 1.0); - random_engine_ = framework::GetCPURandomEngine(seed_); + random_engine_ = phi::GetCPURandomEngine(seed_); } float GetValue() override { diff --git a/paddle/fluid/distributed/ps/table/graph/graph_weighted_sampler.cc b/paddle/fluid/distributed/ps/table/graph/graph_weighted_sampler.cc index 8f2494477475d96b45493524e106620532fb4c5c..70942df5f24c49c603b2bc85b85de956c042bb28 100644 --- a/paddle/fluid/distributed/ps/table/graph/graph_weighted_sampler.cc +++ b/paddle/fluid/distributed/ps/table/graph/graph_weighted_sampler.cc @@ -18,7 +18,7 @@ #include #include -#include "paddle/fluid/framework/generator.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace distributed { diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 41e7ea1abbff60e4cf8341e103f3587a1f44d3b2..1f310222e013b6896f44cd4bacff8708b665b2ae 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -1136,11 +1136,6 @@ cc_test_old( string_helper glog) -cc_library( - generator - SRCS generator.cc - DEPS enforce place) - cc_library( infershape_utils SRCS infershape_utils.cc diff --git a/paddle/fluid/framework/generator.h b/paddle/fluid/framework/generator.h deleted file mode 100644 index 54096bf5c442e5d0847c3ae639121cbefb5f09f7..0000000000000000000000000000000000000000 --- a/paddle/fluid/framework/generator.h +++ /dev/null @@ -1,121 +0,0 @@ -/* 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 diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index f0cc04aa05b8d1b7933834397230fa4b862ee717..e8888940a99ac939cae45757ebe26c961b21949b 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -28,7 +28,6 @@ #include "paddle/fluid//platform/device/gpu/gpu_types.h" #include "paddle/fluid/framework/feed_fetch_method.h" #include "paddle/fluid/framework/feed_fetch_type.h" -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/ir/fuse_pass_base.h" #include "paddle/fluid/framework/ir/pass.h" #include "paddle/fluid/framework/naive_executor.h" @@ -62,6 +61,7 @@ #include "paddle/phi/common/data_type.h" #include "paddle/phi/common/place.h" #include "paddle/phi/core/enforce.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/kernels/funcs/data_type_transform.h" #include "paddle/utils/string/split.h" @@ -441,8 +441,8 @@ void AnalysisPredictor::InitDeviceContexts() { .GetZeroAllocator(platform::CPUPlace()) .get()); gpu_context->SetGenerator( - framework::DefaultCUDAGenerator(place_.GetDeviceId()).get()); - gpu_context->SetHostGenerator(framework::DefaultCPUGenerator().get()); + phi::DefaultCUDAGenerator(place_.GetDeviceId()).get()); + gpu_context->SetHostGenerator(phi::DefaultCPUGenerator().get()); gpu_context->SetStream(gpu_resource->GetStream()); gpu_context->SetBlasHandle(gpu_resource->GetBlasHandleCreator()); diff --git a/paddle/fluid/operators/cudnn_lstm_op.cu.cc b/paddle/fluid/operators/cudnn_lstm_op.cu.cc index 97e5eae62ab3bcd246f806b5f82e1d19e4a66600..2bdf1044d3cfa572b4efbd0c58497f070ea0e8b3 100644 --- a/paddle/fluid/operators/cudnn_lstm_op.cu.cc +++ b/paddle/fluid/operators/cudnn_lstm_op.cu.cc @@ -12,9 +12,9 @@ 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. */ -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/utils.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/kernels/funcs/math_function.h" #ifdef PADDLE_WITH_CUDA #include "paddle/fluid/operators/cudnn_lstm_cache.h" @@ -232,7 +232,7 @@ class CudnnLSTMGPUKernel : public framework::OpKernel { if (seed == 0) { // If not specify seed, use global Generator to generate seed. int device_id = ctx.GetPlace().GetDeviceId(); - auto gen_cuda = paddle::framework::DefaultCUDAGenerator(device_id); + auto gen_cuda = phi::DefaultCUDAGenerator(device_id); seed = static_cast(gen_cuda->Random64()); } // else use `ctx.Attr("seed")` specified seed diff --git a/paddle/fluid/operators/decode_jpeg_op.cc b/paddle/fluid/operators/decode_jpeg_op.cc index 521798e8ddce8194143e53cc195b7c4df5bd292b..43fefd4dd17343325f32af4c7a171f43e0b510a1 100644 --- a/paddle/fluid/operators/decode_jpeg_op.cc +++ b/paddle/fluid/operators/decode_jpeg_op.cc @@ -16,11 +16,11 @@ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/core/infermeta_utils.h" #include "paddle/phi/infermeta/unary.h" diff --git a/paddle/fluid/operators/fused/fused_dropout_helper.h b/paddle/fluid/operators/fused/fused_dropout_helper.h index e530ae6b40ac5039e752d45052acd970540d3c4d..8025ba97ac00471b0f41a35b071eccd43ff030a4 100644 --- a/paddle/fluid/operators/fused/fused_dropout_helper.h +++ b/paddle/fluid/operators/fused/fused_dropout_helper.h @@ -14,10 +14,10 @@ limitations under the License. */ #pragma once -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/operators/fused/fused_dropout_act_bias.h" #include "paddle/fluid/operators/fused/fused_layernorm_residual_dropout_bias.h" #include "paddle/fluid/operators/fused/fused_residual_dropout_bias.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/kernels/funcs/dropout_impl_util.h" #include "paddle/phi/kernels/funcs/functors.h" #include "paddle/phi/kernels/layer_norm_kernel.h" diff --git a/paddle/fluid/operators/fused_softmax_mask_op.cc b/paddle/fluid/operators/fused_softmax_mask_op.cc index 3c8db22f52617e3228548343697b9dbfdd12ecd4..cb5b247bad03ebea56206e792b974bbc9586140d 100644 --- a/paddle/fluid/operators/fused_softmax_mask_op.cc +++ b/paddle/fluid/operators/fused_softmax_mask_op.cc @@ -12,9 +12,9 @@ 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. */ -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/core/infermeta_utils.h" #include "paddle/phi/infermeta/backward.h" #include "paddle/phi/infermeta/binary.h" diff --git a/paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.cc b/paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.cc index 5d1e4089a753dad1c2d27eb56b95a7203b4d1837..e47024e5fc62405989b1a5eb6789aac254e9b396 100644 --- a/paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.cc +++ b/paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.cc @@ -12,8 +12,8 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.h" -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.cu b/paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.cu index 41c42e6134eefdbfbd4c54d2c56a3e50bdc69566..6c4c5a54a657a91c1e2960ef88208888a2600c50 100644 --- a/paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.cu +++ b/paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.cu @@ -43,11 +43,11 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/operators/fused_softmax_mask_upper_triangle_op.h" #include "paddle/fluid/platform/float16.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/gaussian_random_op.cc b/paddle/fluid/operators/gaussian_random_op.cc index 03c1c4dd64c4c8d354861842c02fca6739b9d427..26a81340505bc7d53b78b269842e000064d67d32 100644 --- a/paddle/fluid/operators/gaussian_random_op.cc +++ b/paddle/fluid/operators/gaussian_random_op.cc @@ -14,10 +14,10 @@ limitations under the License. */ #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/infermeta/nullary.h" namespace paddle { diff --git a/paddle/fluid/operators/gaussian_random_op.cu b/paddle/fluid/operators/gaussian_random_op.cu index 41d2547cc9ba058237840f3ebb3f00e83f94a5eb..b91a93b7cb1cfb09f30f93f87faf2c7ae6196407 100644 --- a/paddle/fluid/operators/gaussian_random_op.cu +++ b/paddle/fluid/operators/gaussian_random_op.cu @@ -13,10 +13,10 @@ See the License for the specific language governing permissions and limitations under the License. */ #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/amp/fp16_type_traits.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/kernels/funcs/index_impl.cu.h" namespace paddle { @@ -59,7 +59,7 @@ class GPUGaussianRandomBatchSizeLikeKernel : public framework::OpKernel { int64_t size = tensor->numel(); int device_id = context.GetPlace().GetDeviceId(); - auto gen_cuda = framework::DefaultCUDAGenerator(device_id); + auto gen_cuda = phi::DefaultCUDAGenerator(device_id); auto& dev_cxt = context.template device_context(); if (seed == 0) { diff --git a/paddle/fluid/operators/gaussian_random_op_mlu.cc b/paddle/fluid/operators/gaussian_random_op_mlu.cc index 5128cc95025816fd48bdb37a11148d1c628c2782..dad21a23661d93cc8af55b8fbba1a23622f5a57a 100644 --- a/paddle/fluid/operators/gaussian_random_op_mlu.cc +++ b/paddle/fluid/operators/gaussian_random_op_mlu.cc @@ -14,8 +14,8 @@ limitations under the License. */ #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -37,7 +37,7 @@ class MLUGaussianRandomKernel : public framework::OpKernel { int64_t size = tensor->numel(); unsigned int seed = static_cast(context.Attr("seed")); - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { cpu_data[i] = dist(*engine); } diff --git a/paddle/fluid/operators/gaussian_random_op_npu.cc b/paddle/fluid/operators/gaussian_random_op_npu.cc index 5e3fa3dbef5e643e5d9c020dad42dbacb3c5955a..9b3c23ad2b9c19dab0ca6465b549f76e1b5d3907 100644 --- a/paddle/fluid/operators/gaussian_random_op_npu.cc +++ b/paddle/fluid/operators/gaussian_random_op_npu.cc @@ -15,9 +15,9 @@ limitations under the License. */ #include #include "paddle/fluid/framework/convert_utils.h" -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -39,7 +39,7 @@ class NPUGaussianRandomKernel : public framework::OpKernel { int64_t size = tensor->numel(); unsigned int seed = static_cast(context.Attr("seed")); - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { cpu_data[i] = dist(*engine); } diff --git a/paddle/fluid/operators/index_impl.cu.h b/paddle/fluid/operators/index_impl.cu.h index ab411ad8ac625abf38306815787976b34b404711..9d8f22661e7c4f7c99997af9cf98f114a59f2c05 100644 --- a/paddle/fluid/operators/index_impl.cu.h +++ b/paddle/fluid/operators/index_impl.cu.h @@ -16,11 +16,11 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/phi/backends/gpu/gpu_launch_config.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/core/hostdevice.h" #include "paddle/phi/kernels/funcs/aligned_vector.h" #include "paddle/phi/kernels/funcs/distribution_helper.h" diff --git a/paddle/fluid/operators/math/sampler.cc b/paddle/fluid/operators/math/sampler.cc index 8aeba61a15ab93362e769aceb45b83fa50dbe49d..a9d31ac7837801cec72ba40f0572b6fb51e3fddf 100644 --- a/paddle/fluid/operators/math/sampler.cc +++ b/paddle/fluid/operators/math/sampler.cc @@ -16,7 +16,7 @@ limitations under the License. */ #include -#include "paddle/fluid/framework/generator.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -26,7 +26,7 @@ Sampler::~Sampler() {} UniformSampler::UniformSampler(int64_t range, unsigned int seed) : Sampler(range, seed), inv_range_(1.0 / (range + 1)) { - random_engine_ = framework::GetCPURandomEngine(seed_); + random_engine_ = phi::GetCPURandomEngine(seed_); dist_ = std::make_shared>(0, range); } @@ -36,7 +36,7 @@ float UniformSampler::Probability(int64_t value) const { return inv_range_; } LogUniformSampler::LogUniformSampler(int64_t range, unsigned int seed) : Sampler(range, seed), log_range_(log(range + 1)) { - random_engine_ = framework::GetCPURandomEngine(seed_); + random_engine_ = phi::GetCPURandomEngine(seed_); dist_ = std::make_shared>(0, 1); } @@ -66,7 +66,7 @@ CustomSampler::CustomSampler(int64_t range, const float *alias_probabilities, unsigned int seed) : Sampler(range, seed) { - random_engine_ = framework::GetCPURandomEngine(seed_); + random_engine_ = phi::GetCPURandomEngine(seed_); real_dist_ = std::make_shared>(0, 1); int_dist_ = std::make_shared>(0, range); diff --git a/paddle/fluid/operators/randint_op.cc b/paddle/fluid/operators/randint_op.cc index 810680ea5d4233ae8f9c63fca600e4cb6574fe00..ddfcf64107c8c57dd4c28fe47e9d220db1fa6f9c 100644 --- a/paddle/fluid/operators/randint_op.cc +++ b/paddle/fluid/operators/randint_op.cc @@ -15,11 +15,11 @@ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/uniform_random_op.h" #include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/randperm_op.h b/paddle/fluid/operators/randperm_op.h index 5512471fc2cdf8f49cc50102326dafc27e98563a..988b5d475ee31e960e11ffd6a6bd2bc75e75417a 100644 --- a/paddle/fluid/operators/randperm_op.h +++ b/paddle/fluid/operators/randperm_op.h @@ -20,17 +20,17 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/platform/place.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { template static inline void random_permate(T* data_ptr, int num, unsigned int seed) { - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int i = 0; i < num; ++i) { data_ptr[i] = static_cast(i); } diff --git a/paddle/fluid/operators/read_file_op.cc b/paddle/fluid/operators/read_file_op.cc index 9b42a895a927ce62e4a51381dbe760dcaab1aafb..1d109da045a5a822c914a174819899c60df62d47 100644 --- a/paddle/fluid/operators/read_file_op.cc +++ b/paddle/fluid/operators/read_file_op.cc @@ -16,10 +16,10 @@ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/sampling_id_op.h b/paddle/fluid/operators/sampling_id_op.h index e5c4f744db4a5f8ebfcba3374b81e1ccb64291de..6894746ee467b0dc1adce957ee115f66f1a6c997 100644 --- a/paddle/fluid/operators/sampling_id_op.h +++ b/paddle/fluid/operators/sampling_id_op.h @@ -21,8 +21,8 @@ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -56,7 +56,7 @@ class SamplingIdKernel : public framework::OpKernel { static_cast(context.Attr("min")), static_cast(context.Attr("max"))); - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); std::vector ids(batch_size); for (int i = 0; i < batch_size; ++i) { T r = dist(*engine); diff --git a/paddle/fluid/operators/seed_op.h b/paddle/fluid/operators/seed_op.h index c3cbc16fb4884c25fdadd63cb5225cac3b9c5775..0d8f06c01d2260dc3fe24949292a20dbc5226487 100644 --- a/paddle/fluid/operators/seed_op.h +++ b/paddle/fluid/operators/seed_op.h @@ -13,9 +13,9 @@ // limitations under the License. #pragma once -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -36,7 +36,7 @@ static int get_seed(const framework::ExecutionContext& context) { } } else { std::string name = context.Attr("rng_name"); - auto rng = framework::GetRandomSeedGenerator(name); + auto rng = phi::GetRandomSeedGenerator(name); do { // NOTE(wangxi): cpu dropout will use random seed if seed == 0 seed = static_cast(rng->Random64()); } while (seed == 0); diff --git a/paddle/fluid/operators/truncated_gaussian_random_op.cc b/paddle/fluid/operators/truncated_gaussian_random_op.cc index c5a4a1268fd7dedcd16935b997839b3edef133c8..921f6f0e3163901abe47c3052c6a5d673481dbbb 100644 --- a/paddle/fluid/operators/truncated_gaussian_random_op.cc +++ b/paddle/fluid/operators/truncated_gaussian_random_op.cc @@ -18,9 +18,9 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/infermeta/nullary.h" namespace paddle { diff --git a/paddle/fluid/operators/truncated_gaussian_random_op.h b/paddle/fluid/operators/truncated_gaussian_random_op.h index a6ff2f686cb76bb03de8074014f82d6ff9e57bd3..12947343ea1064872e9ecd20f2c2436d69b57466 100644 --- a/paddle/fluid/operators/truncated_gaussian_random_op.h +++ b/paddle/fluid/operators/truncated_gaussian_random_op.h @@ -17,8 +17,8 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { diff --git a/paddle/fluid/operators/truncated_gaussian_random_op_mlu.cc b/paddle/fluid/operators/truncated_gaussian_random_op_mlu.cc index d2d51c29371f88d29511187a26d4c3d4341bb5c2..960c26961b99c5c1b417299bf01ccc9e7f2f963c 100644 --- a/paddle/fluid/operators/truncated_gaussian_random_op_mlu.cc +++ b/paddle/fluid/operators/truncated_gaussian_random_op_mlu.cc @@ -15,9 +15,9 @@ limitations under the License. */ #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/truncated_gaussian_random_op.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -41,7 +41,7 @@ class TruncatedGaussianRandomMLUKernel : public framework::OpKernel { int64_t size = tensor->numel(); unsigned int seed = static_cast(context.Attr("seed")); - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { data_cpu[i] = truncated_normal(dist(*engine)); diff --git a/paddle/fluid/operators/truncated_gaussian_random_op_npu.cc b/paddle/fluid/operators/truncated_gaussian_random_op_npu.cc index c3b2e24892e402e66030d91e433afcace7c8b186..15362f294053b17631e7dbeb4f5ffc282d35b5a4 100644 --- a/paddle/fluid/operators/truncated_gaussian_random_op_npu.cc +++ b/paddle/fluid/operators/truncated_gaussian_random_op_npu.cc @@ -90,7 +90,7 @@ class NPUTruncatedGaussianRandomKernel : public framework::OpKernel { int64_t size = tensor->numel(); unsigned int seed = static_cast(context.Attr("seed")); - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { cpu_data[i] = truncated_normal(dist(*engine)); } diff --git a/paddle/fluid/operators/uniform_random_inplace_op_xpu.cc b/paddle/fluid/operators/uniform_random_inplace_op_xpu.cc index fcbbc625be38306a3bb4fb2adb354ea9471399fa..bf0360ace0b87f845936a3ddda3b482e475aae3f 100644 --- a/paddle/fluid/operators/uniform_random_inplace_op_xpu.cc +++ b/paddle/fluid/operators/uniform_random_inplace_op_xpu.cc @@ -14,10 +14,10 @@ limitations under the License. */ #ifdef PADDLE_WITH_XPU -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/uniform_random_op.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -36,7 +36,7 @@ class XPUUniformRandomInplaceKernel : public framework::OpKernel { static_cast(ctx.Attr("min")), static_cast(ctx.Attr("max"))); unsigned int seed = static_cast(ctx.Attr("seed")); - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { data_cpu[i] = dist(*engine); } diff --git a/paddle/fluid/operators/uniform_random_op.cc b/paddle/fluid/operators/uniform_random_op.cc index e2605332ccef92baa29a28b8f9ecaf3ffbc740ef..b687dc50ab0d7ead3be70634e70f2d338bf942a0 100644 --- a/paddle/fluid/operators/uniform_random_op.cc +++ b/paddle/fluid/operators/uniform_random_op.cc @@ -15,11 +15,11 @@ limitations under the License. */ #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/infershape_utils.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/platform/bfloat16.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/infermeta/nullary.h" namespace paddle { @@ -35,7 +35,7 @@ inline void UniformRealDistribution(T *data, VLOG(4) << "[CPU] UniformRandomKernel"; std::uniform_real_distribution dist(static_cast(min), static_cast(max)); - auto engine = paddle::framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { data[i] = dist(*engine); @@ -50,7 +50,7 @@ inline void UniformRealDistribution(paddle::platform::bfloat16 *data, const unsigned int seed) { VLOG(4) << "[CPU] UniformRandomKernel"; std::uniform_real_distribution dist(min, max); - auto engine = paddle::framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { data[i] = static_cast(dist(*engine)); diff --git a/paddle/fluid/operators/uniform_random_op.h b/paddle/fluid/operators/uniform_random_op.h index 05a643b33b215dc3ecd3ba3026b2651d3bb88f72..16bce515f2a7fd31ea0485c4ddfa7352959023f7 100644 --- a/paddle/fluid/operators/uniform_random_op.h +++ b/paddle/fluid/operators/uniform_random_op.h @@ -22,7 +22,7 @@ #if defined(__NVCC__) || defined(__HIPCC__) #include -#include "paddle/fluid/framework/generator.h" +#include "paddle/phi/core/generator.h" #include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/distribution_helper.h" #include "paddle/phi/kernels/funcs/index_impl.cu.h" diff --git a/paddle/fluid/operators/uniform_random_op_mlu.cc b/paddle/fluid/operators/uniform_random_op_mlu.cc index 8e5f61c83108834addb94caeff951e8c04ffbd52..bcf51c522d34600dd9b74fd5797b13d14e02a741 100644 --- a/paddle/fluid/operators/uniform_random_op_mlu.cc +++ b/paddle/fluid/operators/uniform_random_op_mlu.cc @@ -12,9 +12,9 @@ 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. */ -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/operators/mlu/mlu_baseop.h" #include "paddle/fluid/operators/uniform_random_op.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -68,7 +68,7 @@ class MLUUniformRandomKernel : public framework::OpKernel { static_cast(ctx.Attr("min")), static_cast(ctx.Attr("max"))); unsigned int seed = static_cast(ctx.Attr("seed")); - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { data_cpu[i] = dist(*engine); diff --git a/paddle/fluid/operators/uniform_random_op_npu.cc b/paddle/fluid/operators/uniform_random_op_npu.cc index e82c6e1f2a91a15b1fe07f333a1116387e03f282..5958a7751b8beb2b6f3c57311904a11dcc5c8a2a 100644 --- a/paddle/fluid/operators/uniform_random_op_npu.cc +++ b/paddle/fluid/operators/uniform_random_op_npu.cc @@ -14,10 +14,10 @@ limitations under the License. */ #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/uniform_random_op.h" +#include "paddle/phi/core/generator.h" namespace paddle { namespace operators { @@ -69,7 +69,7 @@ class NPUUniformRandomKernel : public framework::OpKernel { static_cast(ctx.Attr("min")), static_cast(ctx.Attr("max"))); unsigned int seed = static_cast(ctx.Attr("seed")); - auto engine = framework::GetCPURandomEngine(seed); + auto engine = phi::GetCPURandomEngine(seed); for (int64_t i = 0; i < size; ++i) { data_cpu[i] = dist(*engine); diff --git a/paddle/fluid/platform/device_context.cc b/paddle/fluid/platform/device_context.cc index 4aecc6e0412b030fe2a7998a3fb499468886ac8e..a917b6a00e9d133a551c8fbec0b52333a63fd070 100644 --- a/paddle/fluid/platform/device_context.cc +++ b/paddle/fluid/platform/device_context.cc @@ -19,7 +19,6 @@ limitations under the License. */ #include #include "glog/logging.h" -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/memory/allocation/allocator_facade.h" #include "paddle/fluid/platform/device/device_wrapper.h" #include "paddle/fluid/platform/place.h" @@ -27,6 +26,7 @@ limitations under the License. */ #include "paddle/fluid/platform/profiler/event_tracing.h" #include "paddle/phi/core/allocator.h" #include "paddle/phi/core/expect.h" +#include "paddle/phi/core/generator.h" #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) #include "paddle/fluid/memory/allocation/cuda_device_context_allocator.h" @@ -193,22 +193,20 @@ std::unique_ptr CreateDeviceContext( instance.GetAllocator(paddle::platform::CUDAPinnedPlace()).get()); cuda_ctx->PartialInitWithAllocator(); - dev_ctx->SetGenerator( - framework::DefaultCUDAGenerator(p.GetDeviceId()).get()); + dev_ctx->SetGenerator(phi::DefaultCUDAGenerator(p.GetDeviceId()).get()); #endif } else if (is_xpu_place(p)) { #if defined(PADDLE_WITH_XPU) dev_ctx->SetAllocator( memory::allocation::AllocatorFacade::Instance().GetAllocator(p).get()); - dev_ctx->SetGenerator( - framework::DefaultXPUGenerator(p.GetDeviceId()).get()); + dev_ctx->SetGenerator(phi::DefaultXPUGenerator(p.GetDeviceId()).get()); #endif } else { dev_ctx->SetAllocator( memory::allocation::AllocatorFacade::Instance().GetAllocator(p).get()); - dev_ctx->SetGenerator(framework::DefaultCPUGenerator().get()); + dev_ctx->SetGenerator(phi::DefaultCPUGenerator().get()); } - dev_ctx->SetHostGenerator(framework::DefaultCPUGenerator().get()); + dev_ctx->SetHostGenerator(phi::DefaultCPUGenerator().get()); dev_ctx->SetHostAllocator(memory::allocation::AllocatorFacade::Instance() .GetAllocator(platform::CPUPlace()) .get()); diff --git a/paddle/fluid/pybind/generator_py.cc b/paddle/fluid/pybind/generator_py.cc index b144c79aabbdf1bdb390640c2b6d544f192b981b..32765145731c6909de42513b24ba2d1b90d92c8a 100644 --- a/paddle/fluid/pybind/generator_py.cc +++ b/paddle/fluid/pybind/generator_py.cc @@ -21,10 +21,10 @@ limitations under the License. */ #endif #include +#include #include #include -#include "paddle/fluid/framework/generator.h" #include "paddle/fluid/pybind/generator_py.h" namespace py = pybind11; @@ -72,27 +72,24 @@ void BindGenerator(py::module* m_ptr) { }); py::class_(m, "mt19937_64", ""); - py::class_>( - m, "Generator") + py::class_>(m, "Generator") .def("__init__", - [](framework::Generator& self) { - new (&self) framework::Generator(); - }) - .def("get_state", &framework::Generator::GetState) - .def("set_state", &framework::Generator::SetState) + [](phi::Generator& self) { new (&self) phi::Generator(); }) + .def("get_state", &phi::Generator::GetState) + .def("set_state", &phi::Generator::SetState) .def("manual_seed", - [](std::shared_ptr& self, uint64_t seed) { + [](std::shared_ptr& self, uint64_t seed) { self->SetCurrentSeed(seed); return self; }) - .def("seed", &framework::Generator::Seed) - .def("initial_seed", &framework::Generator::GetCurrentSeed) - .def("random", &framework::Generator::Random64); - m.def("default_cpu_generator", &framework::DefaultCPUGenerator); - m.def("default_cuda_generator", &framework::DefaultCUDAGenerator); - m.def("default_xpu_generator", &framework::DefaultXPUGenerator); - m.def("set_random_seed_generator", &framework::SetRandomSeedGenerator); - m.def("get_random_seed_generator", &framework::GetRandomSeedGenerator); + .def("seed", &phi::Generator::Seed) + .def("initial_seed", &phi::Generator::GetCurrentSeed) + .def("random", &phi::Generator::Random64); + m.def("default_cpu_generator", &phi::DefaultCPUGenerator); + m.def("default_cuda_generator", &phi::DefaultCUDAGenerator); + m.def("default_xpu_generator", &phi::DefaultXPUGenerator); + m.def("set_random_seed_generator", &phi::SetRandomSeedGenerator); + m.def("get_random_seed_generator", &phi::GetRandomSeedGenerator); } } // namespace pybind } // namespace paddle diff --git a/paddle/phi/core/CMakeLists.txt b/paddle/phi/core/CMakeLists.txt index 8e85b737d8adfcfbadc59a5823975ec3bf469ea0..e413ee4ef2ac6821e453018fa30fdb3445213642 100644 --- a/paddle/phi/core/CMakeLists.txt +++ b/paddle/phi/core/CMakeLists.txt @@ -120,6 +120,11 @@ cc_library( SRCS mixed_vector.cc DEPS device_context place memory) +cc_library( + generator + SRCS generator.cc + DEPS enforce place) + # Will remove once we implemented MKLDNN_Tensor if(WITH_MKLDNN) add_dependencies(dense_tensor mkldnn) diff --git a/paddle/fluid/framework/generator.cc b/paddle/phi/core/generator.cc similarity index 75% rename from paddle/fluid/framework/generator.cc rename to paddle/phi/core/generator.cc index ce516ca4f5516b585851e88d062a45b7ce70cf0c..16a72117a2a383ee99d64a9656b762add4f4e17f 100644 --- a/paddle/fluid/framework/generator.cc +++ b/paddle/phi/core/generator.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. +/* Copyright (c) 2023 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. @@ -12,19 +12,18 @@ 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. */ -#include "paddle/fluid/framework/generator.h" +#include "paddle/phi/core/generator.h" #include #include #include -#include "paddle/fluid/platform/device/gpu/gpu_info.h" -#include "paddle/fluid/platform/device/xpu/xpu_info.h" -#include "paddle/fluid/platform/enforce.h" +#include "paddle/phi/backends/gpu/gpu_info.h" +#include "paddle/phi/backends/xpu/xpu_info.h" +#include "paddle/phi/core/enforce.h" -namespace paddle { -namespace framework { +namespace phi { const std::shared_ptr& DefaultXPUGenerator(int64_t device_id) { #if defined(PADDLE_WITH_XPU) @@ -35,13 +34,13 @@ const std::shared_ptr& DefaultXPUGenerator(int64_t device_id) { static std::vector> default_xpu_generators; std::call_once(num_devices_init_flag, []() { - num_xpu_devices = paddle::platform::GetXPUDeviceCount(); + num_xpu_devices = phi::backends::xpu::GetXPUDeviceCount(); xpu_device_flags.resize(num_xpu_devices); default_xpu_generators.resize(num_xpu_devices); }); if (device_id < 0) { - PADDLE_THROW(platform::errors::InvalidArgument( - "xpu device id shoule be greater than 0")); + PADDLE_THROW( + phi::errors::InvalidArgument("xpu device id shoule be greater than 0")); } std::call_once(xpu_device_flags[device_id], [device_id]() { @@ -52,7 +51,7 @@ const std::shared_ptr& DefaultXPUGenerator(int64_t device_id) { }); return default_xpu_generators[device_id]; #else - PADDLE_THROW(platform::errors::PermissionDenied( + PADDLE_THROW(phi::errors::PermissionDenied( "getDefaultXPUGenerator only support in XPU place")); #endif } @@ -66,12 +65,12 @@ const std::shared_ptr& DefaultCUDAGenerator(int64_t device_id) { static std::vector> default_cuda_generators; std::call_once(num_devices_init_flag, []() { - num_cuda_devices = paddle::platform::GetGPUDeviceCount(); + num_cuda_devices = phi::backends::gpu::GetGPUDeviceCount(); cuda_device_flags.resize(num_cuda_devices); default_cuda_generators.resize(num_cuda_devices); }); if (device_id < 0) { - PADDLE_THROW(platform::errors::InvalidArgument( + PADDLE_THROW(phi::errors::InvalidArgument( "cuda device id shoule be greater than 0")); } @@ -83,7 +82,7 @@ const std::shared_ptr& DefaultCUDAGenerator(int64_t device_id) { }); return default_cuda_generators[device_id]; #else - PADDLE_THROW(platform::errors::PermissionDenied( + PADDLE_THROW(phi::errors::PermissionDenied( "getDefaultCUDAGenerator only support in CUDA place")); #endif } @@ -107,7 +106,7 @@ const std::shared_ptr& SetRandomSeedGenerator( auto iter = rng_map.find(name); PADDLE_ENFORCE_EQ(iter == rng_map.end(), true, - platform::errors::AlreadyExists( + phi::errors::AlreadyExists( "%s RandomSeedGenerator is already exist", name)); auto generator = std::make_shared(seed); @@ -115,7 +114,7 @@ const std::shared_ptr& SetRandomSeedGenerator( PADDLE_ENFORCE_EQ( emplace_success, true, - platform::errors::PermissionDenied( + phi::errors::PermissionDenied( "SetRandomSeedGenerator cannot emplace %s RandomSeedGenerator", name)); return rng_map[name]; @@ -125,12 +124,12 @@ const std::shared_ptr& GetRandomSeedGenerator( const std::string& name) { auto& rng_map = GetRandomSeedGeneratorMap(); auto iter = rng_map.find(name); - PADDLE_ENFORCE_EQ(iter != rng_map.end(), - true, - platform::errors::NotFound( - "%s RandomSeedGenerator is not found, please " - "use `set_random_seed_generator` to set rng first", - name)); + PADDLE_ENFORCE_EQ( + iter != rng_map.end(), + true, + phi::errors::NotFound("%s RandomSeedGenerator is not found, please " + "use `set_random_seed_generator` to set rng first", + name)); return iter->second; } @@ -160,6 +159,43 @@ std::shared_ptr GetCPURandomEngine(uint64_t seed) { } } +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; +} + +Generator::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::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; +} + phi::Generator::GeneratorState Generator::GetState() { std::lock_guard lock(this->mu_); state_.cpu_engine = *engine_; @@ -231,10 +267,9 @@ std::pair Generator::IncrementOffset( this->state_.thread_offset += increament_offset; return std::make_pair(this->state_.current_seed, cur_offset); #else - PADDLE_THROW(platform::errors::PermissionDenied( + PADDLE_THROW(phi::errors::PermissionDenied( "Increment Offset only support in CUDA place")); #endif } -} // namespace framework -} // namespace paddle +} // namespace phi diff --git a/paddle/phi/core/generator.h b/paddle/phi/core/generator.h index 3263b2a52573271357d2e5f8751a654e06629a87..aaf6990d929634c7d1455d3431590dd8219c2562 100644 --- a/paddle/phi/core/generator.h +++ b/paddle/phi/core/generator.h @@ -14,12 +14,25 @@ limitations under the License. */ #pragma once -#include +#include + +#include +#include +#include // temp for debug #include +#include // NOLINT #include +#include +#include namespace phi { +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; +} + class Generator { public: struct GeneratorState { @@ -29,27 +42,56 @@ class Generator { std::mt19937_64 cpu_engine; }; - virtual ~Generator() = default; + Generator(); + + explicit Generator(uint64_t seed); + + Generator(uint64_t seed, uint64_t device_id); + + Generator(const Generator& other) = delete; + + ~Generator() = default; // get random state - virtual GeneratorState GetState() = 0; + GeneratorState GetState(); // set random state - virtual void SetState(const GeneratorState&) = 0; + void SetState(const GeneratorState&); // get current seed - virtual uint64_t GetCurrentSeed() = 0; + uint64_t GetCurrentSeed(); // random a seed and get - virtual uint64_t Seed() = 0; + uint64_t Seed(); // set seed - virtual void SetCurrentSeed(uint64_t seed) = 0; + void SetCurrentSeed(uint64_t seed); // get cpu engine - virtual std::shared_ptr GetCPUEngine() = 0; + std::shared_ptr GetCPUEngine(); // set cpu engine - virtual void SetCPUEngine(std::shared_ptr) = 0; - virtual uint64_t Random64() = 0; - virtual std::pair IncrementOffset( - uint64_t increament_offset) = 0; + void SetCPUEngine(std::shared_ptr); + + uint64_t Random64(); + + std::pair IncrementOffset(uint64_t increament_offset); - virtual uint64_t get_device_id() = 0; + uint64_t get_device_id() { return this->state_.device; } + + private: + 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 phi diff --git a/paddle/phi/kernels/gpu/class_center_sample_kernel.cu b/paddle/phi/kernels/gpu/class_center_sample_kernel.cu index 698ec44e6123bd357c8606486da750d98a1aca18..0348c096e41c9a05804f795966b515f3fcebd786 100644 --- a/paddle/phi/kernels/gpu/class_center_sample_kernel.cu +++ b/paddle/phi/kernels/gpu/class_center_sample_kernel.cu @@ -458,7 +458,7 @@ void ClassCenterSampleKernel(const Context& dev_ctx, (NumBlocks(num_classes) * kNumCUDAThreads * vec_size) + 1) * vec_size; - // auto gen_cuda = paddle::framework::DefaultCUDAGenerator(device_id); + // auto gen_cuda = phi::DefaultCUDAGenerator(device_id); auto gen_cuda = dev_ctx.GetGenerator(); if (!fix_seed) { auto seed_offset = gen_cuda->IncrementOffset(offset);