From 2a4200363c0d64b91bb9e92cc5cd781315bc9e4c Mon Sep 17 00:00:00 2001 From: RedContritio Date: Tue, 11 Apr 2023 11:09:57 +0800 Subject: [PATCH] support auto generate for op merged_momentum optimizer (#52708) * fix error in generator/type_mapping.py * support auto generate for op merged_momentum optimizer --- .../fluid/operators/generator/type_mapping.py | 2 +- .../optimizers/merged_momentum_op.cc | 111 ------------------ paddle/phi/api/yaml/legacy_ops.yaml | 11 -- paddle/phi/api/yaml/op_compat.yaml | 6 + paddle/phi/api/yaml/ops.yaml | 11 ++ paddle/phi/ops/compat/merged_momentum_sig.cc | 40 ------- 6 files changed, 18 insertions(+), 163 deletions(-) delete mode 100644 paddle/fluid/operators/optimizers/merged_momentum_op.cc delete mode 100644 paddle/phi/ops/compat/merged_momentum_sig.cc diff --git a/paddle/fluid/operators/generator/type_mapping.py b/paddle/fluid/operators/generator/type_mapping.py index 8aec1bcc49a..e6b59b7823a 100644 --- a/paddle/fluid/operators/generator/type_mapping.py +++ b/paddle/fluid/operators/generator/type_mapping.py @@ -76,7 +76,7 @@ opmaker_attr_types_map = { 'int64_t[]': 'std::vector', 'float[]': 'std::vector', 'double[]': 'std::vector', - 'str[]': 'std::vector<', + 'str[]': 'std::vector', } output_type_map = {'Tensor': 'Tensor', 'Tensor[]': 'std::vector'} diff --git a/paddle/fluid/operators/optimizers/merged_momentum_op.cc b/paddle/fluid/operators/optimizers/merged_momentum_op.cc deleted file mode 100644 index 17d31e35fde..00000000000 --- a/paddle/fluid/operators/optimizers/merged_momentum_op.cc +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) 2021 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 "paddle/fluid/framework/infershape_utils.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/phi/core/infermeta_utils.h" -#include "paddle/phi/infermeta/multiary.h" - -namespace paddle { -namespace operators { - -class MergedMomentumOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext &ctx) const override { - auto param_dtype = - framework::OperatorWithKernel::IndicateVarDataType(ctx, "Param"); - return phi::KernelKey(param_dtype, ctx.GetPlace()); - } -}; - -class MergedMomentumOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("Param", - "(Tensor, default Tensor) " - "Input parameter that has to be updated") - .AsDuplicable(); - AddInput("Grad", - "(Tensor, default Tensor) " - "Input gradient of the parameter") - .AsDuplicable(); - AddInput("Velocity", - "(Tensor, default Tensor) " - "Input velocity (corresponding to the parameter) " - "that has to be updated") - .AsDuplicable(); - AddInput("LearningRate", - "(Tensor, default Tensor) " - "Input learning rate") - .AsDuplicable(); - AddInput("MasterParam", "FP32 master weight for AMP.") - .AsDispensable() - .AsDuplicable(); - AddOutput("ParamOut", - "(Tensor) This output is updated parameter. " - "It shared memory with Input(Param).") - .AsDuplicable(); - AddOutput("VelocityOut", - "(Tensor) This output is updated velocity. " - "It shared memory with Input(Velocity).") - .AsDuplicable(); - AddOutput("MasterParamOut", - "The updated FP32 master weight for AMP. " - "It shared memory with Input(MasterParam).") - .AsDispensable() - .AsDuplicable(); - AddAttr("mu", "(float) Momentum coefficient"); - AddAttr("use_nesterov", - "(bool, default false) " - "Use Nesterov Momentum or not.") - .SetDefault(false); - AddAttr>( - "regularization_method", - "(string) regularization_method, right now only " - "support l2decay or none") - .SetDefault({}); - AddAttr>("regularization_coeff", - "(float) regularization_coeff") - .SetDefault({}); - AddAttr("multi_precision", - "(bool, default false) " - "Whether to use multi-precision during weight updating.") - .SetDefault(false); - AddAttr( - "rescale_grad", - "(float, default 1.0) Multiply the gradient with `rescale_grad`" - "before updating. Often choose to be `1.0/batch_size`.") - .SetDefault(1.0f); - AddComment(R"DOC(Merged Momentum Optimizer.)DOC"); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -namespace plat = paddle::platform; - -DECLARE_INFER_SHAPE_FUNCTOR(merged_momentum, - MergedMomentumInferShapeFunctor, - PD_INFER_META(phi::MergedMomentumInferMeta)); - -REGISTER_OP_WITHOUT_GRADIENT(merged_momentum, - ops::MergedMomentumOp, - ops::MergedMomentumOpMaker, - MergedMomentumInferShapeFunctor); diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 53ae099e762..e44bbe7e6dd 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -884,17 +884,6 @@ data_type : param inplace : (param -> param_out), (moment1 -> moment1_out), (moment2 -> moment2_out), (beta1_pow -> beta1_pow_out), (beta2_pow -> beta2_pow_out), (master_param -> master_param_out) -- op : merged_momentum_ - args : (Tensor[] param, Tensor[] grad, Tensor[] velocity, Tensor[] learning_rate, Tensor[] master_param, float mu, bool use_nesterov = false, str[] regularization_method = {}, float[] regularization_coeff = {}, bool multi_precision = false, float rescale_grad = 1.0f) - output : Tensor[](param_out){param.size()}, Tensor[](velocity_out){param.size()}, Tensor[](master_param_out){param.size()} - infer_meta : - func : MergedMomentumInferMeta - optional: master_param - kernel : - func : merged_momentum - data_type : param - inplace : (param -> param_out), (velocity -> velocity_out), (master_param -> master_param_out) - - op : min args : (Tensor x, IntArray axis={}, bool keepdim=false) output : Tensor(out) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index f807a3d748b..98a00e6f5a9 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1421,6 +1421,12 @@ outputs : out : Out +- op : merged_momentum_ + inputs : + {param : Param, grad : Grad, velocity : Velocity, learning_rate : LearningRate, master_param : MasterParam} + outputs : + {param_out : ParamOut, velocity_out : VelocityOut, master_param_out : MasterParamOut} + - op : meshgrid backward : meshgrid_grad inputs : diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 3afbf00c049..31f4a114b71 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -1190,6 +1190,17 @@ kernel : func : merge_selected_rows {selected_rows -> selected_rows} +- op : merged_momentum_ + args : (Tensor[] param, Tensor[] grad, Tensor[] velocity, Tensor[] learning_rate, Tensor[] master_param, float mu, bool use_nesterov = false, str[] regularization_method = {}, float[] regularization_coeff = {}, bool multi_precision = false, float rescale_grad = 1.0f) + output : Tensor[](param_out){param.size()}, Tensor[](velocity_out){param.size()}, Tensor[](master_param_out){param.size()} + infer_meta : + func : MergedMomentumInferMeta + kernel : + func : merged_momentum + data_type : param + optional: master_param, master_param_out + inplace : (param -> param_out), (velocity -> velocity_out), (master_param -> master_param_out) + - op : meshgrid args : (Tensor[] inputs) output : Tensor[]{inputs.size()} diff --git a/paddle/phi/ops/compat/merged_momentum_sig.cc b/paddle/phi/ops/compat/merged_momentum_sig.cc deleted file mode 100644 index 3444d5e2d30..00000000000 --- a/paddle/phi/ops/compat/merged_momentum_sig.cc +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2022 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 "paddle/phi/core/compat/op_utils.h" - -namespace phi { - -KernelSignature MergedMomentumOpArgumentMapping( - const ArgumentMappingContext& ctx) { - return KernelSignature( - "merged_momentum", - {"Param", "Grad", "Velocity", "LearningRate", "MasterParam"}, - {"mu", - "use_nesterov", - "regularization_method", - "regularization_coeff", - "multi_precision", - "rescale_grad"}, - { - "ParamOut", - "VelocityOut", - "MasterParamOut", - }); -} - -} // namespace phi - -PD_REGISTER_ARG_MAPPING_FN(merged_momentum, - phi::MergedMomentumOpArgumentMapping); -- GitLab