diff --git a/paddle/operators/random_op.cc b/paddle/operators/random_op.cc index c219a0b67d6d8e090ddb6dcea730d5cae3ec63ad..b85ff8422071a11bf0db68a0eba2918b70a8d256 100644 --- a/paddle/operators/random_op.cc +++ b/paddle/operators/random_op.cc @@ -3,6 +3,18 @@ namespace paddle { namespace operators { + +using paddle::platform::GPUPlace; +template +bool Gaussian( + Generator g, T* output, const int size, const T& mean, const T& std) { + std::normal_distribution distribution(mean, std); + for (int i = 0; i < size; ++i) { + output[i] = distribution(g()); + } + return true; +} + class RandomOp : public framework::OperatorWithKernel { protected: void InferShape( @@ -12,7 +24,7 @@ protected: PADDLE_ENFORCE(outputs.size() == 1, "Output size of RandomOp must be one."); PADDLE_ENFORCE(inputs[0] != nullptr && outputs[0] != nullptr, "Inputs/Outputs of RandomOp must all be set."); - outputs[0]->set_dims(inputs[0]->dims()); + outputs[0]->set_dims(context.op_.attrs_.at("shape")); } }; diff --git a/paddle/operators/random_op.cu b/paddle/operators/random_op.cu index 50985f66998cd146c0e6e0bbe97e02eb5a433a54..ea1096aeb97ad22a4925b5a5ec8069cfd5e15902 100644 --- a/paddle/operators/random_op.cu +++ b/paddle/operators/random_op.cu @@ -1,6 +1,19 @@ #include "paddle/operators/random_op.h" #include "paddle/framework/op_registry.h" +namespace paddle { +namespace operators { + +using paddle::platform::GPUPlace; +template +bool Gaussian(Generator g, T* output, const int size, const T& mean, const T& std) { + return curandGenerateNormal(g, output, size, mean, std); +} + +} // operators +} // paddle + + typedef paddle::operators::RandomOpKernel RandomOpKernel_GPU_float; REGISTER_OP_GPU_KERNEL(random_op, RandomOpKernel_GPU_float); \ No newline at end of file diff --git a/paddle/operators/random_op.h b/paddle/operators/random_op.h index 123d9d6ffab5f0c27e2c92c7de910be290885fbb..1b5fb16de1a6533e2f559c333a194225868917ce 100644 --- a/paddle/operators/random_op.h +++ b/paddle/operators/random_op.h @@ -6,24 +6,52 @@ namespace paddle { namespace operators { +template +bool Gaussian( + Generator g, T* output, const int size, const T& mean, const T& std); + template class RandomOpKernel : public framework::OpKernel { public: void Compute(const framework::KernelContext& context) const override { - auto* output = context.Output(0)->GetMutable(); - output->mutable_data(context.GetPlace()); - - auto shape = context.op_.attrs_.at("Shape"); auto mean = context.op_.attrs_.at("mean"); auto std = context.op_.attrs_.at("std"); auto seed = context.op_.attrs_.at("seed"); + auto* output = context.Output(0)->GetMutable(); + output->mutable_data(context.GetPlace()); + + Gaussian(, output, output->size(), mean, std) : // std::default_random_engine generator(seed); // std::normal_distribution distribution(mean, std); - framework::EigenMatrix::From(*output).device(*( - context.GetEigenDevice())) = framework::EigenMatrix::Random(); + // framework::EigenMatrix::From(*output).device(*( + // context.GetEigenDevice())) = + // framework::EigenMatrix::Random(); } }; +// using paddle::platform::CPUPlace; +// template +// class RandomOpKernel : public framework::OpKernel { +// public: +// void Compute(const framework::KernelContext& context) const override { + +// std::unique_ptr generator(seed); +// for(size_t i=0; i < output->size(); ++i) { +// output[i] = distribution(generator()); +// } +// } + +// }; + +// using paddle::platform::GPUPlace; +// template +// class RandomOpKernel : public framework::OpKernel { +// public: +// void Compute(const framework::KernelContext& context) const override { + +// } +// } + } // namespace operators } // namespace paddle