未验证 提交 3c949ba9 编写于 作者: R RedContritio 提交者: GitHub

support auto generate static for gaussian (gaussian_random) (#52422)

* support auto generate static for gaussian (gaussian_random)

* move gaussian_random_batch_size_like Kernels from gaussian_random_op.* to gaussian_random_batch_size_like_op.*
上级 4b28f4ff
...@@ -19,12 +19,35 @@ limitations under the License. */ ...@@ -19,12 +19,35 @@ limitations under the License. */
namespace paddle { namespace paddle {
namespace operators { namespace operators {
template <typename T>
class CPUGaussianRandomBatchSizeLikeKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
float mean = context.Attr<float>("mean");
float std = context.Attr<float>("std");
auto* tensor = context.Output<phi::DenseTensor>("Out");
T* data = tensor->mutable_data<T>(context.GetPlace());
unsigned int seed = static_cast<unsigned int>(context.Attr<int>("seed"));
std::minstd_rand engine;
if (seed == 0) {
seed = std::random_device()();
}
engine.seed(seed);
std::normal_distribution<T> dist(mean, std);
int64_t size = tensor->numel();
for (int64_t i = 0; i < size; ++i) {
data[i] = dist(engine);
}
}
};
class GaussianRandomBatchSizeLikeOp : public BatchSizeLikeOp { class GaussianRandomBatchSizeLikeOp : public BatchSizeLikeOp {
protected: protected:
using BatchSizeLikeOp::BatchSizeLikeOp; using BatchSizeLikeOp::BatchSizeLikeOp;
phi::KernelKey GetExpectedKernelType( phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override { const framework::ExecutionContext& ctx) const override {
return phi::KernelKey( return phi::KernelKey(
static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype")), static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype")),
ctx.GetPlace()); ctx.GetPlace());
...@@ -76,4 +99,7 @@ REGISTER_OPERATOR( ...@@ -76,4 +99,7 @@ REGISTER_OPERATOR(
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>, paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
paddle::operators::BatchSizeLikeNoNeedBufferVarsInferer); paddle::operators::BatchSizeLikeNoNeedBufferVarsInferer);
// Kernels are registered in gaussian_random_op.cc and gaussian_random_op.cu REGISTER_OP_CPU_KERNEL(
gaussian_random_batch_size_like,
paddle::operators::CPUGaussianRandomBatchSizeLikeKernel<float>,
paddle::operators::CPUGaussianRandomBatchSizeLikeKernel<double>);
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
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 <random>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/generator.h"
#include "paddle/phi/infermeta/nullary.h"
namespace paddle {
namespace operators {
template <typename T>
class CPUGaussianRandomBatchSizeLikeKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& context) const override {
float mean = context.Attr<float>("mean");
float std = context.Attr<float>("std");
auto* tensor = context.Output<phi::DenseTensor>("Out");
T* data = tensor->mutable_data<T>(context.GetPlace());
unsigned int seed = static_cast<unsigned int>(context.Attr<int>("seed"));
std::minstd_rand engine;
if (seed == 0) {
seed = std::random_device()();
}
engine.seed(seed);
std::normal_distribution<T> dist(mean, std);
int64_t size = tensor->numel();
for (int64_t i = 0; i < size; ++i) {
data[i] = dist(engine);
}
}
};
class GaussianRandomOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
protected:
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
auto data_type =
static_cast<framework::proto::VarType::Type>(ctx.Attr<int>("dtype"));
return phi::KernelKey(data_type, ctx.device_context().GetPlace());
}
phi::KernelKey GetKernelTypeForVar(
const std::string& var_name,
const phi::DenseTensor& tensor,
const phi::KernelKey& expected_kernel_type) const override {
if (var_name == "ShapeTensor" || var_name == "ShapeTensorList") {
return phi::KernelKey(phi::Backend::ALL_BACKEND,
expected_kernel_type.layout(),
expected_kernel_type.dtype());
}
return phi::KernelKey(
tensor.place(), tensor.layout(), expected_kernel_type.dtype());
}
};
class GaussianRandomOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddOutput("Out", "Output matrix of gaussian random op");
AddAttr<std::vector<int64_t>>("shape",
"(vector<int64_t>) "
"The dimension of random tensor.")
.SetDefault({});
AddInput("ShapeTensor",
"(Tensor<int>), optional). The shape of the output."
"It has a higher priority than Attr(shape).")
.AsDispensable();
AddInput("ShapeTensorList",
"(vector<Tensor<int>>, optional). The shape of the output. "
"It has a higher priority than Attr(shape)."
"The shape of the element in vector must be [1].")
.AsDuplicable()
.AsDispensable();
AddAttr<float>("mean",
"(float, default 0.0) "
"mean of random tensor.")
.SetDefault(.0f);
AddAttr<float>("std",
"(float, default 1.0) "
"std of random tensor.")
.SetDefault(1.0f);
AddAttr<int>("seed",
"(int, default 0) "
"Random seed of generator."
"0 means use system wide seed."
"Note that if seed is not 0, this operator will always "
"generate the same random numbers every time.")
.SetDefault(0);
AddAttr<int>("dtype",
"(int, default 5(FP32)) "
"Output data type.")
.SetDefault(framework::proto::VarType::FP32);
AddAttr<bool>("use_mkldnn",
"(bool, default false) Only used in mkldnn kernel")
.SetDefault(false);
AddComment(R"DOC(
GaussianRandom Operator.
Used to initialize tensors with gaussian random generator.
)DOC");
}
};
} // namespace operators
} // namespace paddle
namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(gaussian_random,
GaussianRandomInferShapeFunctor,
PD_INFER_META(phi::GaussianInferMeta));
REGISTER_OPERATOR(
gaussian_random,
ops::GaussianRandomOp,
ops::GaussianRandomOpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
GaussianRandomInferShapeFunctor);
REGISTER_OP_CPU_KERNEL(gaussian_random_batch_size_like,
ops::CPUGaussianRandomBatchSizeLikeKernel<float>,
ops::CPUGaussianRandomBatchSizeLikeKernel<double>);
REGISTER_OP_VERSION(gaussian_random)
.AddCheckpoint(
R"ROC(
Upgrade gaussian_random add new inputs [ShapeTensor] and [ShapeTensorList]
and modify the attribute of [shape])ROC",
paddle::framework::compatible::OpVersionDesc()
.NewInput("ShapeTensor",
"The output shape supports Tensor type. ShapeTensor is "
"dispensable.")
.NewInput("ShapeTensorList",
"The output shape supports list filled with Tensor. "
"ShapeTensorList is dispensable.")
.ModifyAttr("shape",
"The arg 'default_value' of attr 'shape' is changed: "
"from 'None' to '{}'.",
std::vector<int64_t>{}));
...@@ -112,7 +112,6 @@ register_unity_group( ...@@ -112,7 +112,6 @@ register_unity_group(
gather_op.cc gather_op.cc
gather_tree_op.cc gather_tree_op.cc
gaussian_random_batch_size_like_op.cc gaussian_random_batch_size_like_op.cc
gaussian_random_op.cc
mkldnn/gaussian_random_mkldnn_op.cc mkldnn/gaussian_random_mkldnn_op.cc
group_norm_op.cc group_norm_op.cc
gru_op.cc) gru_op.cc)
...@@ -425,7 +424,7 @@ register_unity_group( ...@@ -425,7 +424,7 @@ register_unity_group(
gather_nd_op.cu gather_nd_op.cu
gather_op.cu gather_op.cu
gather_tree_op.cu gather_tree_op.cu
gaussian_random_op.cu gaussian_random_batch_size_like_op.cu
grid_sampler_op.cu grid_sampler_op.cu
group_norm_op.cu) group_norm_op.cu)
register_unity_group( register_unity_group(
......
...@@ -827,6 +827,18 @@ ...@@ -827,6 +827,18 @@
outputs : outputs :
out : Out out : Out
- op : gaussian (gaussian_random)
outputs :
out : Out
int_array:
shape :
data_type : int64_t
tensor_name : ShapeTensor
tensors_name : ShapeTensorList
extra :
attrs : [bool use_mkldnn = false]
manual_signature : [gaussian]
- op : gelu - op : gelu
backward : gelu_grad backward : gelu_grad
inputs : inputs :
......
...@@ -61,6 +61,19 @@ ...@@ -61,6 +61,19 @@
- delete_attr : dims - delete_attr : dims
comment : The attr 'dims' is deleted. comment : The attr 'dims' is deleted.
- op : gaussian_random
version :
- checkpoint : Upgrade gaussian_random add new inputs [ShapeTensor] and [ShapeTensorList]
and modify the attribute of [shape]
action :
- add_input : ShapeTensor
comment : The output shape supports Tensor type. ShapeTensor is dispensable.
- add_input : ShapeTensorList
comment : The output shape supports list filled with Tensor. ShapeTensorList is dispensable.
- modify_attr : shape
comment : "The arg 'default_value' of attr 'shape' is changed: from 'None' to '{}'."
default : std::vector<int64_t>{}
- op : greater_equal - op : greater_equal
version : version :
- checkpoint : Upgrade compare ops, add a new attribute [force_cpu] - checkpoint : Upgrade compare ops, add a new attribute [force_cpu]
......
...@@ -90,6 +90,17 @@ ...@@ -90,6 +90,17 @@
param : [x, axis, keepdim, reduce_all] param : [x, axis, keepdim, reduce_all]
backward : frobenius_norm_grad backward : frobenius_norm_grad
- op : gaussian
args : (IntArray shape = {}, float mean = .0f, float std = 1.0f, int seed = 0, DataType dtype = DataType::FLOAT32)
output: Tensor(out)
infer_meta :
func : GaussianInferMeta
param : [shape, mean, std, seed, dtype]
kernel :
func : gaussian
param : [shape, mean, std, seed, dtype]
data_type : dtype
- op : greater_equal - op : greater_equal
args : (Tensor x, Tensor y, int axis = -1, bool force_cpu=false) args : (Tensor x, Tensor y, int axis = -1, bool force_cpu=false)
output : Tensor(out) output : Tensor(out)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册