提交 6f80b5f1 编写于 作者: D dongzhihong

"move to template function"

上级 0d554f1d
/* 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 "paddle/operators/random_op.h" #include "paddle/operators/random_op.h"
#include "paddle/framework/op_registry.h" #include "paddle/framework/op_registry.h"
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using paddle::platform::GPUPlace; // using paddle::platform::CPUPlace;
template <GPUPlace, typename T, typename Generator> // template <paddle::platform::CPUPlace, typename T, typename DeviceContext>
bool Gaussian( template <typename T>
Generator g, T* output, const int size, const T& mean, const T& std) { bool Gaussian(platform::CPUDeviceContext& ctx,
framework::Tensor* output,
const int size,
const T& mean,
const T& std,
const T& seed) {
auto g = ctx.RandGenerator(seed);
std::normal_distribution<double> distribution(mean, std); std::normal_distribution<double> distribution(mean, std);
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
output[i] = distribution(g()); output[i] = distribution(g());
...@@ -24,7 +44,9 @@ protected: ...@@ -24,7 +44,9 @@ protected:
PADDLE_ENFORCE(outputs.size() == 1, "Output size of RandomOp must be one."); PADDLE_ENFORCE(outputs.size() == 1, "Output size of RandomOp must be one.");
PADDLE_ENFORCE(inputs[0] != nullptr && outputs[0] != nullptr, PADDLE_ENFORCE(inputs[0] != nullptr && outputs[0] != nullptr,
"Inputs/Outputs of RandomOp must all be set."); "Inputs/Outputs of RandomOp must all be set.");
outputs[0]->set_dims(context.op_.attrs_.at("shape")); outputs[0]->Resize(
framework::make_ddim(this->GetAttr<std::vector<int>>("shape")));
// outputs[0]->set_dims(context.op_.attrs_.at("shape"));
} }
}; };
...@@ -32,7 +54,7 @@ class RandomOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -32,7 +54,7 @@ class RandomOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
RandomOpMaker(framework::OpProto* proto, framework::OpAttrChecker* op_checker) RandomOpMaker(framework::OpProto* proto, framework::OpAttrChecker* op_checker)
: framework::OpProtoAndCheckerMaker(proto, op_checker) { : framework::OpProtoAndCheckerMaker(proto, op_checker) {
AddAttr<std::vector<int>>("Shape", "The shape of matrix to be randomized"); AddAttr<std::vector<int>>("shape", "The shape of matrix to be randomized");
AddAttr<float>("seed", "random seed generator.").SetDefault(1337); AddAttr<float>("seed", "random seed generator.").SetDefault(1337);
AddAttr<float>("mean", "mean value of random.").SetDefault(.0); AddAttr<float>("mean", "mean value of random.").SetDefault(.0);
AddAttr<float>("std", "minimum value of random value") AddAttr<float>("std", "minimum value of random value")
......
...@@ -4,9 +4,10 @@ ...@@ -4,9 +4,10 @@
namespace paddle { namespace paddle {
namespace operators { namespace operators {
using paddle::platform::GPUPlace; template <typename T>
template<GPUPlace, typename T, typename Generator> bool Gaussian(platform::CUDADeviceContext &ctx, framework::Tensor* output,
bool Gaussian(Generator g, T* output, const int size, const T& mean, const T& std) { const int size, const T& mean, const T& std, const T& seed) {
auto g = RandGenerator(seed);
return curandGenerateNormal(g, output, size, mean, std); return curandGenerateNormal(g, output, size, mean, std);
} }
......
...@@ -6,21 +6,33 @@ ...@@ -6,21 +6,33 @@
namespace paddle { namespace paddle {
namespace operators { namespace operators {
template <typename Place, typename T, typename Generator> template <typename T, typename DeviceContext>
bool Gaussian( bool Gaussian(DeviceContext& ctx,
Generator g, T* output, const int size, const T& mean, const T& std); framework::Tensor* output,
const int size,
const T& mean,
const T& std,
const T& seed);
template <typename Place, typename T> template <typename Place, typename T>
class RandomOpKernel : public framework::OpKernel { class RandomOpKernel : public framework::OpKernel {
public: public:
void Compute(const framework::KernelContext& context) const override { void Compute(const framework::KernelContext& context) const override {
auto mean = context.op_.attrs_.at("mean"); auto mean = context.op_.GetAttr<T>("mean");
auto std = context.op_.attrs_.at("std"); auto std = context.op_.GetAttr<T>("std");
auto seed = context.op_.attrs_.at("seed"); auto seed = context.op_.GetAttr<T>("seed");
auto* output = context.Output(0)->GetMutable<framework::Tensor>(); auto* output = context.Output(0)->GetMutable<framework::Tensor>();
output->mutable_data<T>(context.GetPlace()); output->mutable_data<T>(context.GetPlace());
Gaussian(context.device_context_,
Gaussian<Place, T, >(, output, output->size(), mean, std) : output,
framework::product(output->dims()),
mean,
std,
seed);
// Gaussian<T, const platform::DeviceContext>(context.device_context_,
// output,
// framework::product(output->dims()),
// mean, std, seed);
// std::default_random_engine generator(seed); // std::default_random_engine generator(seed);
// std::normal_distribution<double> distribution(mean, std); // std::normal_distribution<double> distribution(mean, std);
......
...@@ -39,6 +39,7 @@ class DeviceContext { ...@@ -39,6 +39,7 @@ class DeviceContext {
class CPUDeviceContext : public DeviceContext { class CPUDeviceContext : public DeviceContext {
public: public:
typedef std::mt19937 random_generator_type;
CPUDeviceContext() { eigen_device_.reset(new Eigen::DefaultDevice()); } CPUDeviceContext() { eigen_device_.reset(new Eigen::DefaultDevice()); }
Eigen::DefaultDevice* eigen_device() const { return eigen_device_.get(); } Eigen::DefaultDevice* eigen_device() const { return eigen_device_.get(); }
...@@ -48,7 +49,17 @@ class CPUDeviceContext : public DeviceContext { ...@@ -48,7 +49,17 @@ class CPUDeviceContext : public DeviceContext {
return retv; return retv;
} }
const random_generator_type& RandGenerator(const int seed) {
if (!rand_generator_) {
random_seed_ = seed;
rand_generator_.reset(new random_generator_type(random_seed_));
}
return *rand_generator_.get();
}
private: private:
int random_seed_;
std::unique_ptr<random_generator_type> rand_generator_;
std::unique_ptr<Eigen::DefaultDevice> eigen_device_; std::unique_ptr<Eigen::DefaultDevice> eigen_device_;
}; };
...@@ -87,6 +98,24 @@ class CUDADeviceContext : public DeviceContext { ...@@ -87,6 +98,24 @@ class CUDADeviceContext : public DeviceContext {
"cudaStreamSynchronize failed"); "cudaStreamSynchronize failed");
} }
const curandGenerator_t RandGenerator(const int seed) {
if (!rand_generator_) {
random_seed_ = seed;
GPUPlaceGuard guard(gpu_place_);
PADDLE_ENFORCE(paddle::platform::dynload::curandCreateGenerator(
&rand_generator_, CURAND_RNG_PSEUDO_DEFAULT),
"curandCreateGenerator failed");
PADDLE_ENFORCE(
paddle::platform::dynload::curandSetPseudoRandomGeneratorSeed(
rand_generator_, random_seed_),
"curandSetPseudoRandomGeneratorSeed failed");
PADDLE_ENFORCE(
paddle::platform::dynload::curandSetStream(rand_generator_, stream_),
"curandSetStream failed");
}
return rand_generator_;
}
cudaStream_t stream() { return stream_; } cudaStream_t stream() { return stream_; }
Eigen::GpuDevice* eigen_device() const { return eigen_device_.get(); } Eigen::GpuDevice* eigen_device() const { return eigen_device_.get(); }
...@@ -115,23 +144,6 @@ class CUDADeviceContext : public DeviceContext { ...@@ -115,23 +144,6 @@ class CUDADeviceContext : public DeviceContext {
return dnn_handle_; return dnn_handle_;
} }
curandGenerator_t curand_generator() {
if (!rand_generator_) {
GPUPlaceGuard guard(gpu_place_);
PADDLE_ENFORCE(paddle::platform::dynload::curandCreateGenerator(
&rand_generator_, CURAND_RNG_PSEUDO_DEFAULT),
"curandCreateGenerator failed");
PADDLE_ENFORCE(
paddle::platform::dynload::curandSetPseudoRandomGeneratorSeed(
rand_generator_, random_seed_),
"curandSetPseudoRandomGeneratorSeed failed");
PADDLE_ENFORCE(
paddle::platform::dynload::curandSetStream(rand_generator_, stream_),
"curandSetStream failed");
}
return rand_generator_;
}
~CUDADeviceContext() { ~CUDADeviceContext() {
Wait(); Wait();
if (blas_handle_) { if (blas_handle_) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册