-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add cuda generator #26786
Merged
Merged
add cuda generator #26786
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
869c885
add cuda generator
yaoxuefeng6 3f74c1e
support initializer with cuda generator
yaoxuefeng6 5ce3939
fix mac buid fail
yaoxuefeng6 d8c58a3
fix build error
yaoxuefeng6 b7df9fc
fix ut fail
yaoxuefeng6 dfa5d03
fix ut fail
yaoxuefeng6 9a8e89b
fix ut
yaoxuefeng6 c8e3dca
fix ut fail
yaoxuefeng6 09e31a8
fix ut fail
yaoxuefeng6 95aa055
fix ut fail
yaoxuefeng6 f4946e3
fix ut fail
yaoxuefeng6 8f8bf9c
fix ut fail
yaoxuefeng6 288eaef
polish code and fix ut fail
yaoxuefeng6 a412135
fix ut fail and polish codes
yaoxuefeng6 158c5fc
fix ci fail
yaoxuefeng6 766799e
fix device id and add get/set_cuda_rng_state api
yaoxuefeng6 c8360b6
fix style
yaoxuefeng6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ static uint64_t GetRandomSeed() { | |
struct GeneratorState { | ||
int64_t device = -1; | ||
uint64_t current_seed = 34342423252; | ||
uint64_t thread_offset = 0; | ||
std::mt19937_64 cpu_engine; | ||
}; | ||
|
||
|
@@ -49,6 +50,7 @@ struct Generator { | |
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; | ||
|
@@ -59,11 +61,25 @@ struct Generator { | |
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; | ||
this->is_init_py_ = true; // TODO(zhiqiu): remove it in future | ||
} | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the default is false? |
||
} | ||
|
||
Generator(const Generator& other) = delete; | ||
|
||
// get random state | ||
|
@@ -83,8 +99,11 @@ struct Generator { | |
|
||
uint64_t Random64(); | ||
|
||
std::pair<uint64_t, uint64_t> IncrementOffset(uint64_t increament_offset); | ||
|
||
void SetIsInitPy(bool); | ||
bool GetIsInitPy() const; | ||
uint64_t get_device_id() { return this->state_.device; } | ||
|
||
private: | ||
GeneratorState state_; | ||
|
@@ -105,5 +124,8 @@ std::shared_ptr<std::mt19937_64> OpDefaultCPUEngine(); | |
|
||
std::shared_ptr<std::mt19937_64> GetCPURandomEngine(uint64_t); | ||
|
||
const std::shared_ptr<Generator>& GetDefaultCUDAGenerator( | ||
int64_t device_id = -1); | ||
|
||
} // namespace framework | ||
} // namespace paddle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,6 +96,42 @@ __global__ void RandomGeneratorWithSeed(const size_t n, const int* seed, | |
} | ||
} | ||
|
||
template <typename T, typename MaskType> | ||
__global__ void RandomGeneratorWithGenerator(const size_t n, uint64_t seed, | ||
const float dropout_prob, | ||
const T* src, MaskType* mask_data, | ||
T* dst, bool is_upscale_in_train, | ||
uint64_t increment) { | ||
curandStatePhilox4_32_10_t state; | ||
int idx = blockDim.x * blockIdx.x + threadIdx.x; | ||
int step_size = 0; | ||
|
||
MaskType mask; | ||
T dest; | ||
for (; idx < n; idx += blockDim.x * gridDim.x) { | ||
T s = src[idx]; | ||
if (step_size == 0) { | ||
curand_init(seed, idx, increment, &state); | ||
step_size = blockDim.x * gridDim.x; | ||
} else { | ||
curand_init(seed, idx, increment, &state); | ||
} | ||
if (curand_uniform(&state) < dropout_prob) { | ||
mask = 0; | ||
dest = 0; | ||
} else { | ||
mask = 1; | ||
if (is_upscale_in_train) { | ||
dest = s / static_cast<T>(1.0f - dropout_prob); | ||
} else { | ||
dest = s; | ||
} | ||
} | ||
mask_data[idx] = mask; | ||
dst[idx] = dest; | ||
} | ||
} | ||
|
||
// It seems that Eigen::Tensor::setRandom in GPU will SEGFAULT. | ||
// Use std::random and thrust::random(thrust is a std library in CUDA) to | ||
// implement uniform random. | ||
|
@@ -150,6 +186,15 @@ class GPUDropoutKernel : public framework::OpKernel<T> { | |
context.Attr<bool>("fix_seed") ? context.Attr<int>("seed") : rnd(); | ||
} | ||
|
||
auto gen_cuda = framework::GetDefaultCUDAGenerator(-1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the device_id is -1 here? I think it should be got by the context. |
||
if (gen_cuda->GetIsInitPy() && (!context.Attr<bool>("fix_seed"))) { | ||
auto seed_offset = gen_cuda->IncrementOffset(1); | ||
RandomGeneratorWithGenerator<T, uint8_t><<<grid, threads, 0, stream>>>( | ||
size, seed_offset.first, dropout_prob, x_data, mask_data, y_data, | ||
upscale_in_train, seed_offset.second); | ||
return; | ||
} | ||
|
||
RandomGenerator<T, uint8_t><<<grid, threads, 0, stream>>>( | ||
size, seed_data, dropout_prob, x_data, mask_data, y_data, | ||
upscale_in_train); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case, the originally given device_id is -1, what the number after this?