From 3fa302b86538f7f075da7d35b3d0d5166cc8f78e Mon Sep 17 00:00:00 2001 From: Guoxia Wang Date: Wed, 29 Dec 2021 16:24:02 +0800 Subject: [PATCH] fix seed for class_center_sample using paddle.seed (#38248) (#38498) --- .../fluid/operators/class_center_sample_op.cu | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/paddle/fluid/operators/class_center_sample_op.cu b/paddle/fluid/operators/class_center_sample_op.cu index cfcfd04e6fc..c3f6460e018 100644 --- a/paddle/fluid/operators/class_center_sample_op.cu +++ b/paddle/fluid/operators/class_center_sample_op.cu @@ -390,18 +390,26 @@ class ClassCenterSampleCUDAKernel : public framework::OpKernel { ctx.cuda_device_context().stream()))); // step 5: random sample negative class center + uint64_t seed_data; + uint64_t increment; int vec_size = VectorizedSize(cub_sort_keys_ptr); - int increment = ((num_classes - 1) / - (NumBlocks(num_classes) * kNumCUDAThreads * vec_size) + - 1) * - vec_size; - if (!fix_seed) { + auto offset = ((num_classes - 1) / + (NumBlocks(num_classes) * kNumCUDAThreads * vec_size) + + 1) * + vec_size; + auto gen_cuda = framework::GetDefaultCUDAGenerator(rank); + if (gen_cuda->GetIsInitPy() && (!fix_seed)) { + auto seed_offset = gen_cuda->IncrementOffset(offset); + seed_data = seed_offset.first; + increment = seed_offset.second; + } else { std::random_device rnd; - seed = rnd(); + seed_data = fix_seed ? seed + rank : rnd(); + increment = offset; } RandomSampleClassCenter<<>>( - num_classes, seed + rank, increment, num_classes, cub_sort_keys_ptr); + num_classes, seed_data, increment, num_classes, cub_sort_keys_ptr); // step 6: mark positive class center as negative value // fill the sort values to index 0, 1, ..., batch_size-1 -- GitLab