From 3c949ba94722fd8fbd07ef08deff340f5c7ef5c2 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Mon, 3 Apr 2023 14:59:57 +0800 Subject: [PATCH] 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.* --- .../gaussian_random_batch_size_like_op.cc | 30 +++- ... => gaussian_random_batch_size_like_op.cu} | 0 paddle/fluid/operators/gaussian_random_op.cc | 161 ------------------ paddle/fluid/operators/unity_build_rule.cmake | 3 +- paddle/phi/api/yaml/op_compat.yaml | 12 ++ paddle/phi/api/yaml/op_version.yaml | 13 ++ paddle/phi/api/yaml/static_ops.yaml | 11 ++ 7 files changed, 65 insertions(+), 165 deletions(-) rename paddle/fluid/operators/{gaussian_random_op.cu => gaussian_random_batch_size_like_op.cu} (100%) delete mode 100644 paddle/fluid/operators/gaussian_random_op.cc diff --git a/paddle/fluid/operators/gaussian_random_batch_size_like_op.cc b/paddle/fluid/operators/gaussian_random_batch_size_like_op.cc index 84f5479a61d..c7c3af2b4d7 100644 --- a/paddle/fluid/operators/gaussian_random_batch_size_like_op.cc +++ b/paddle/fluid/operators/gaussian_random_batch_size_like_op.cc @@ -19,12 +19,35 @@ limitations under the License. */ namespace paddle { namespace operators { +template +class CPUGaussianRandomBatchSizeLikeKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& context) const override { + float mean = context.Attr("mean"); + float std = context.Attr("std"); + auto* tensor = context.Output("Out"); + T* data = tensor->mutable_data(context.GetPlace()); + + unsigned int seed = static_cast(context.Attr("seed")); + std::minstd_rand engine; + if (seed == 0) { + seed = std::random_device()(); + } + engine.seed(seed); + std::normal_distribution dist(mean, std); + int64_t size = tensor->numel(); + for (int64_t i = 0; i < size; ++i) { + data[i] = dist(engine); + } + } +}; + class GaussianRandomBatchSizeLikeOp : public BatchSizeLikeOp { protected: using BatchSizeLikeOp::BatchSizeLikeOp; phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { + const framework::ExecutionContext& ctx) const override { return phi::KernelKey( static_cast(ctx.Attr("dtype")), ctx.GetPlace()); @@ -76,4 +99,7 @@ REGISTER_OPERATOR( paddle::framework::EmptyGradOpMaker, 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, + paddle::operators::CPUGaussianRandomBatchSizeLikeKernel); diff --git a/paddle/fluid/operators/gaussian_random_op.cu b/paddle/fluid/operators/gaussian_random_batch_size_like_op.cu similarity index 100% rename from paddle/fluid/operators/gaussian_random_op.cu rename to paddle/fluid/operators/gaussian_random_batch_size_like_op.cu diff --git a/paddle/fluid/operators/gaussian_random_op.cc b/paddle/fluid/operators/gaussian_random_op.cc deleted file mode 100644 index 26a81340505..00000000000 --- a/paddle/fluid/operators/gaussian_random_op.cc +++ /dev/null @@ -1,161 +0,0 @@ -/* 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 - -#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 -class CPUGaussianRandomBatchSizeLikeKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& context) const override { - float mean = context.Attr("mean"); - float std = context.Attr("std"); - auto* tensor = context.Output("Out"); - T* data = tensor->mutable_data(context.GetPlace()); - - unsigned int seed = static_cast(context.Attr("seed")); - std::minstd_rand engine; - if (seed == 0) { - seed = std::random_device()(); - } - engine.seed(seed); - std::normal_distribution 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(ctx.Attr("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>("shape", - "(vector) " - "The dimension of random tensor.") - .SetDefault({}); - AddInput("ShapeTensor", - "(Tensor), optional). The shape of the output." - "It has a higher priority than Attr(shape).") - .AsDispensable(); - AddInput("ShapeTensorList", - "(vector>, 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("mean", - "(float, default 0.0) " - "mean of random tensor.") - .SetDefault(.0f); - AddAttr("std", - "(float, default 1.0) " - "std of random tensor.") - .SetDefault(1.0f); - AddAttr("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("dtype", - "(int, default 5(FP32)) " - "Output data type.") - .SetDefault(framework::proto::VarType::FP32); - AddAttr("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::EmptyGradOpMaker, - GaussianRandomInferShapeFunctor); - -REGISTER_OP_CPU_KERNEL(gaussian_random_batch_size_like, - ops::CPUGaussianRandomBatchSizeLikeKernel, - ops::CPUGaussianRandomBatchSizeLikeKernel); - -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{})); diff --git a/paddle/fluid/operators/unity_build_rule.cmake b/paddle/fluid/operators/unity_build_rule.cmake index d713696d65b..51fc042c10e 100644 --- a/paddle/fluid/operators/unity_build_rule.cmake +++ b/paddle/fluid/operators/unity_build_rule.cmake @@ -112,7 +112,6 @@ register_unity_group( gather_op.cc gather_tree_op.cc gaussian_random_batch_size_like_op.cc - gaussian_random_op.cc mkldnn/gaussian_random_mkldnn_op.cc group_norm_op.cc gru_op.cc) @@ -425,7 +424,7 @@ register_unity_group( gather_nd_op.cu gather_op.cu gather_tree_op.cu - gaussian_random_op.cu + gaussian_random_batch_size_like_op.cu grid_sampler_op.cu group_norm_op.cu) register_unity_group( diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 45c87aaca57..303edec5af7 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -827,6 +827,18 @@ outputs : 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 backward : gelu_grad inputs : diff --git a/paddle/phi/api/yaml/op_version.yaml b/paddle/phi/api/yaml/op_version.yaml index 101d895500f..0b4fd9226e0 100644 --- a/paddle/phi/api/yaml/op_version.yaml +++ b/paddle/phi/api/yaml/op_version.yaml @@ -61,6 +61,19 @@ - delete_attr : dims 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{} + - op : greater_equal version : - checkpoint : Upgrade compare ops, add a new attribute [force_cpu] diff --git a/paddle/phi/api/yaml/static_ops.yaml b/paddle/phi/api/yaml/static_ops.yaml index 235a1abde00..95c7fd2d948 100644 --- a/paddle/phi/api/yaml/static_ops.yaml +++ b/paddle/phi/api/yaml/static_ops.yaml @@ -90,6 +90,17 @@ param : [x, axis, keepdim, reduce_all] 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 args : (Tensor x, Tensor y, int axis = -1, bool force_cpu=false) output : Tensor(out) -- GitLab