From e2c08d286fe8f230cb20ab5b6de6601d93feb82c Mon Sep 17 00:00:00 2001 From: dongzhihong Date: Tue, 8 Aug 2017 16:21:21 +0800 Subject: [PATCH] "keep style same with uniform operators" --- paddle/operators/gaussian_random_op.cc | 69 +++++++++++++------------- paddle/operators/gaussian_random_op.cu | 43 +++++++++------- paddle/operators/gaussian_random_op.h | 17 ------- 3 files changed, 60 insertions(+), 69 deletions(-) delete mode 100644 paddle/operators/gaussian_random_op.h diff --git a/paddle/operators/gaussian_random_op.cc b/paddle/operators/gaussian_random_op.cc index d7ced6b526b..b0b68ff36d3 100644 --- a/paddle/operators/gaussian_random_op.cc +++ b/paddle/operators/gaussian_random_op.cc @@ -12,42 +12,42 @@ See the License for the specific language governing permissions and limitations under the License. */ -#include "paddle/operators/gaussian_random_op.h" -#include "glog/logging.h" +#include #include "paddle/framework/op_registry.h" namespace paddle { namespace operators { template -class GaussianRandomOpKernel - : public framework::OpKernel { +class GaussianRandomKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { - auto mean = context.op_.GetAttr("mean"); - auto std = context.op_.GetAttr("std"); - auto* output = context.Output(0)->GetMutable(); - T* r = output->mutable_data(context.GetPlace()); - auto ctx = - static_cast(context.device_context_); - // generator need to modify context - auto g = const_cast(ctx)->RandGenerator(); + T mean = static_cast(context.op_.GetAttr("mean")); + T std = static_cast(context.op_.GetAttr("std")); + auto* tensor = context.Output(0); + T* data = tensor->mutable_data(context.GetPlace()); + + // TODO(dzh): attribute does not support unsigned int. + // And we need a global random seed configuration. + int seed = context.op_.GetAttr("seed"); + if (seed == 0) { + seed = std::random_device()(); + } + std::mt19937 g(seed); std::normal_distribution distribution(mean, std); - for (int i = 0; i < framework::product(output->dims()); ++i) { - r[i] = distribution(g); + for (int i = 0; i < framework::product(tensor->dims()); ++i) { + data[i] = distribution(g); } } }; class GaussianRandomOp : public framework::OperatorWithKernel { protected: - void InferShape(const framework::InferShapeContext& ctx) const override { - PADDLE_ENFORCE(inputs.size() == 0, "Input size of RandomOp must be zero."); - PADDLE_ENFORCE(outputs.size() == 1, "Output size of RandomOp must be one."); - PADDLE_ENFORCE(outputs[0] != nullptr, - "Outputs of RandomOp must all be set."); - auto* tensor = ctx.Output(0); - auto dims = GetAttr(std::vector("shape")); + void InferShape(const framework::InferShapeContext& context) const override { + auto* tensor = context.Output(0); + auto dims = GetAttr>("dims"); + PADDLE_ENFORCE(dims.size() > 0UL, + "dims can be one int or array. dims must be set."); tensor->Resize(framework::make_ddim(dims)); } }; @@ -57,26 +57,25 @@ class GaussianRandomOpMaker : public framework::OpProtoAndCheckerMaker { GaussianRandomOpMaker(framework::OpProto* proto, framework::OpAttrChecker* op_checker) : framework::OpProtoAndCheckerMaker(proto, op_checker) { - AddAttr>("shape", "The shape of matrix to be randomized"); - AddAttr("mean", "mean value of random.").SetDefault(.0); - AddAttr("std", "minimum value of random value") - .SetDefault(1.0) - .LargerThan(.0); AddOutput("Out", "output matrix of random op"); AddComment(R"DOC( -GaussianRandom Operator fill a matrix in normal distribution. -The eqution : Out = GaussianRandom(Shape=(d0, d1, ...), Dtype, mean, std) +GaussianRandom operator. +Use to initialize tensor with gaussian random generator. )DOC"); + + AddAttr>("dims", "The dimension of random tensor."); + AddAttr("mean", "mean value of random.").SetDefault(.0f); + AddAttr("std", "minimum value of random value.").SetDefault(1.0f); + AddAttr("seed", + "Random seed of generator." + "0 means use system wide seed") + .SetDefault(0); } }; } // namespace operators } // namespace paddle -REGISTER_OP(gaussian_random, paddle::operators::GaussianRandomOp, - paddle::operators::GaussianRandomOpMaker); - -typedef paddle::operators::GaussianRandomOpKernel - GaussianRandomOpKernel_CPU_float; -REGISTER_OP_CPU_KERNEL(gaussian_random, GaussianRandomOpKernel_CPU_float); +namespace ops = paddle::operators; +REGISTER_OP(gaussian_random, ops::GaussianRandomOp, ops::GaussianRandomOpMaker); +REGISTER_OP_CPU_KERNEL(gaussian_random, ops::GaussianRandomKernel); diff --git a/paddle/operators/gaussian_random_op.cu b/paddle/operators/gaussian_random_op.cu index afede378f6e..a408d2aa794 100644 --- a/paddle/operators/gaussian_random_op.cu +++ b/paddle/operators/gaussian_random_op.cu @@ -1,30 +1,39 @@ +#include +#include +#include "paddle/platform/dynload/curand.h" +#include "paddle/platform/gpu_info.h" + #include "paddle/framework/op_registry.h" -#include "paddle/operators/guassian_random_op.h" namespace paddle { namespace operators { template -class GaussianRandomOpKernel - : public framework::OpKernel { +class GaussianRandomKernel : public framework::OpKernel { public: - void Compute(const framework::KernelContext& context) const override { - auto mean = context.op_.GetAttr("mean"); - auto std = context.op_.GetAttr("std"); - auto* output = context.Output(0)->GetMutable(); - T* r = output->mutable_data(context.GetPlace()); - auto ctx = - static_cast(context.device_context_); - // generator need to modify context - auto g = const_cast(ctx)->RandGenerator(); - curandGenerateNormal(g, r, framework::product(output->dims()), mean, std); + void Compute(const framework::ExecutionContext& context) const override { + T mean = static_cast(context.op_.GetAttr("mean")); + T std = static_cast(context.op_.GetAttr("std")); + auto* tensor = context.Output(0); + T* data = tensor->mutable_data(context.GetPlace()); + + int seed = context.op_.GetAttr("seed"); + if (seed == 0) { + seed = std::random_device()(); + } + curandGenerator_t g; + PADDLE_ENFORCE(platform::dynload::curandCreateGenerator( + &g, CURAND_RNG_PSEUDO_DEFAULT)); + PADDLE_ENFORCE( + platform::dynload::curandSetPseudoRandomGeneratorSeed(g, seed)); + // auto g = const_cast(ctx)->RandGenerator(); + curandGenerateNormal(g, data, framework::product(tensor->dims()), mean, + std); } }; } // namespace operators } // namespace paddle -typedef paddle::operators::GaussianRandomOpKernel - RandomOpKernel_GPU_float; -REGISTER_OP_GPU_KERNEL(gaussian_random, GaussianRandomOpKernel_GPU_float); \ No newline at end of file +namespace ops = paddle::operators; +REGISTER_OP_GPU_KERNEL(gaussian_random, ops::GaussianRandomKernel); \ No newline at end of file diff --git a/paddle/operators/gaussian_random_op.h b/paddle/operators/gaussian_random_op.h deleted file mode 100644 index b463a171d9c..00000000000 --- a/paddle/operators/gaussian_random_op.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include -#include "glog/logging.h" -#include "paddle/framework/eigen.h" -#include "paddle/framework/operator.h" - -namespace paddle { -namespace operators { - -template -class GaussianRandomOpKernel : public framework::OpKernel { -public: - void Compute(const framework::KernelContext& context) const override {} -}; - -} // namespace operators -} // namespace paddle -- GitLab