提交 0d554f1d 编写于 作者: D dongzhihong

"add template fill function"

上级 c110f565
7 合并请求!11636[IMPORTANT] MKLDNN layout: Support for sum operator,!8482Release/0.11.0,!8190Release/0.11.0,!8189Release/0.11.0,!6633给线性回归的get-started代码加上了预测的示例~~,!4615Feature/tensor array add python binding,!3060Random op
......@@ -3,6 +3,18 @@
namespace paddle {
namespace operators {
using paddle::platform::GPUPlace;
template <GPUPlace, typename T, typename Generator>
bool Gaussian(
Generator g, T* output, const int size, const T& mean, const T& std) {
std::normal_distribution<double> 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"));
}
};
......
#include "paddle/operators/random_op.h"
#include "paddle/framework/op_registry.h"
namespace paddle {
namespace operators {
using paddle::platform::GPUPlace;
template<GPUPlace, typename T, typename Generator>
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<paddle::platform::GPUPlace, float>
RandomOpKernel_GPU_float;
REGISTER_OP_GPU_KERNEL(random_op, RandomOpKernel_GPU_float);
\ No newline at end of file
......@@ -6,24 +6,52 @@
namespace paddle {
namespace operators {
template <typename Place, typename T, typename Generator>
bool Gaussian(
Generator g, T* output, const int size, const T& mean, const T& std);
template <typename Place, typename T>
class RandomOpKernel : public framework::OpKernel {
public:
void Compute(const framework::KernelContext& context) const override {
auto* output = context.Output(0)->GetMutable<framework::Tensor>();
output->mutable_data<T>(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<framework::Tensor>();
output->mutable_data<T>(context.GetPlace());
Gaussian<Place, T, >(, output, output->size(), mean, std) :
// std::default_random_engine generator(seed);
// std::normal_distribution<double> distribution(mean, std);
framework::EigenMatrix<T>::From(*output).device(*(
context.GetEigenDevice<Place>())) = framework::EigenMatrix<T>::Random();
// framework::EigenMatrix<T>::From(*output).device(*(
// context.GetEigenDevice<Place>())) =
// framework::EigenMatrix<T>::Random();
}
};
// using paddle::platform::CPUPlace;
// template<CPUPlace, typename T>
// class RandomOpKernel : public framework::OpKernel {
// public:
// void Compute(const framework::KernelContext& context) const override {
// std::unique_ptr<default_random_engine> generator(seed);
// for(size_t i=0; i < output->size(); ++i) {
// output[i] = distribution(generator());
// }
// }
// };
// using paddle::platform::GPUPlace;
// template<GPUPlace, typename T>
// class RandomOpKernel : public framework::OpKernel {
// public:
// void Compute(const framework::KernelContext& context) const override {
// }
// }
} // namespace operators
} // namespace paddle
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册
反馈
建议
客服 返回
顶部