parent
fcd6f64b98
commit
e2c08d286f
@ -1,30 +1,39 @@
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#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 <typename T>
|
||||
class GaussianRandomOpKernel<platform::GPUPlace, T>
|
||||
: public framework::OpKernel {
|
||||
class GaussianRandomKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::KernelContext& context) const override {
|
||||
auto mean = context.op_.GetAttr<T>("mean");
|
||||
auto std = context.op_.GetAttr<T>("std");
|
||||
auto* output = context.Output(0)->GetMutable<framework::Tensor>();
|
||||
T* r = output->mutable_data<T>(context.GetPlace());
|
||||
auto ctx =
|
||||
static_cast<const platform::GPUDeviceContext*>(context.device_context_);
|
||||
// generator need to modify context
|
||||
auto g = const_cast<platform::GPUDeviceContext*>(ctx)->RandGenerator();
|
||||
curandGenerateNormal(g, r, framework::product(output->dims()), mean, std);
|
||||
void Compute(const framework::ExecutionContext& context) const override {
|
||||
T mean = static_cast<T>(context.op_.GetAttr<T>("mean"));
|
||||
T std = static_cast<T>(context.op_.GetAttr<T>("std"));
|
||||
auto* tensor = context.Output<framework::Tensor>(0);
|
||||
T* data = tensor->mutable_data<T>(context.GetPlace());
|
||||
|
||||
int seed = context.op_.GetAttr<int>("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<platform::GPUDeviceContext*>(ctx)->RandGenerator();
|
||||
curandGenerateNormal(g, data, framework::product(tensor->dims()), mean,
|
||||
std);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
||||
|
||||
typedef paddle::operators::GaussianRandomOpKernel<paddle::platform::GPUPlace,
|
||||
float>
|
||||
RandomOpKernel_GPU_float;
|
||||
REGISTER_OP_GPU_KERNEL(gaussian_random, GaussianRandomOpKernel_GPU_float);
|
||||
namespace ops = paddle::operators;
|
||||
REGISTER_OP_GPU_KERNEL(gaussian_random, ops::GaussianRandomKernel<float>);
|
@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
#include <random>
|
||||
#include "glog/logging.h"
|
||||
#include "paddle/framework/eigen.h"
|
||||
#include "paddle/framework/operator.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace operators {
|
||||
|
||||
template <typename Place, typename T>
|
||||
class GaussianRandomOpKernel : public framework::OpKernel {
|
||||
public:
|
||||
void Compute(const framework::KernelContext& context) const override {}
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace paddle
|
Loading…
Reference in new issue