-
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
refine random related operators #3596
Merged
QiJune
merged 8 commits into
PaddlePaddle:develop
from
QiJune:implement_random_function
Aug 22, 2017
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d525abe
refine random related ops
QiJune 7c274dc
use curand
QiJune 2f47f35
fix gpu build error
QiJune 08c987d
use dynload curand
QiJune b054392
fix gaussion op bug
QiJune 36e8e72
expose random seed to users
QiJune 1918ad8
fix gpu build error
QiJune aff90d8
fix gpu build error
QiJune 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,65 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
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. */ | ||
|
||
#include <memory> | ||
#include <random> | ||
#include "paddle/platform/dynload/curand.h" | ||
#include "paddle/platform/gpu_info.h" | ||
|
||
#include <thrust/device_ptr.h> | ||
#include <thrust/iterator/counting_iterator.h> | ||
#include <thrust/random.h> | ||
#include <thrust/transform.h> | ||
#include "paddle/framework/op_registry.h" | ||
#include "paddle/framework/operator.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
template <typename T> | ||
class GaussianRandomKernel : public framework::OpKernel { | ||
struct GaussianGenerator { | ||
T mean_, std_; | ||
unsigned int seed_; | ||
|
||
__host__ __device__ GaussianGenerator(T mean, T std, int seed) | ||
: mean_(mean), std_(std), seed_(seed) {} | ||
|
||
__host__ __device__ T operator()(const unsigned int n) const { | ||
thrust::minstd_rand rng; | ||
rng.seed(seed_); | ||
thrust::normal_distribution<T> dist(mean_, std_); | ||
rng.discard(n); | ||
return dist(rng); | ||
} | ||
}; | ||
|
||
template <typename T> | ||
class GPUGaussianRandomKernel : public framework::OpKernel { | ||
public: | ||
void Compute(const framework::ExecutionContext& context) const override { | ||
float mean = context.op_.GetAttr<float>("mean"); | ||
float std = context.op_.GetAttr<float>("std"); | ||
auto* tensor = context.Output<framework::Tensor>(0); | ||
auto* tensor = context.Output<framework::Tensor>("Out"); | ||
T* data = tensor->mutable_data<T>(context.GetPlace()); | ||
|
||
int seed = context.op_.GetAttr<int>("seed"); | ||
unsigned int seed = | ||
static_cast<unsigned int>(context.op_.GetAttr<int>("seed")); | ||
if (seed == 0) { | ||
std::random_device rd; | ||
seed = rd(); | ||
} | ||
curandGenerator_t g; | ||
PADDLE_ENFORCE(platform::dynload::curandCreateGenerator( | ||
&g, CURAND_RNG_PSEUDO_DEFAULT)); | ||
PADDLE_ENFORCE( | ||
platform::dynload::curandSetPseudoRandomGeneratorSeed(g, seed)); | ||
platform::dynload::curandGenerateNormal( | ||
g, data, framework::product(tensor->dims()), mean, std); | ||
T mean = static_cast<T>(context.op_.GetAttr<float>("mean")); | ||
T std = static_cast<T>(context.op_.GetAttr<float>("std")); | ||
thrust::counting_iterator<unsigned int> index_sequence_begin(0); | ||
ssize_t N = framework::product(tensor->dims()); | ||
thrust::transform(index_sequence_begin, index_sequence_begin + N, | ||
thrust::device_ptr<T>(data), | ||
GaussianGenerator<T>(mean, std, seed)); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OP_GPU_KERNEL(gaussian_random, ops::GaussianRandomKernel<float>); | ||
REGISTER_OP_GPU_KERNEL(gaussian_random, | ||
paddle::operators::GPUGaussianRandomKernel<float>); |
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
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
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
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.
it is useless to convert int to unsigned int.
Attribute Member
can not contain an unsigned int.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.
And maybe we can add unsigned to attribute map