From 2c836ff914900b5cf0cdf1c24b69d700b08e8868 Mon Sep 17 00:00:00 2001 From: sneaxiy Date: Thu, 28 Mar 2019 11:41:16 +0000 Subject: [PATCH] check default grad maker test=develop --- paddle/fluid/framework/details/op_registry.h | 5 + paddle/fluid/framework/grad_op_desc_maker.h | 18 +-- paddle/fluid/framework/op_info.cc | 15 +++ paddle/fluid/framework/op_info.h | 7 ++ .../fluid/op_use_default_grad_op_maker.spec | 110 ++++++++++++++++++ .../fused/fused_embedding_seq_pool_op.cc | 13 +-- paddle/fluid/operators/lookup_table_op.cc | 12 +- paddle/fluid/operators/nce_op.cc | 12 +- paddle/fluid/pybind/pybind.cc | 4 + paddle/scripts/paddle_build.sh | 8 ++ tools/diff_use_default_grad_op_maker.py | 66 +++++++++++ ...generate_op_use_grad_op_desc_maker_spec.py | 29 +++++ 12 files changed, 256 insertions(+), 43 deletions(-) create mode 100644 paddle/fluid/op_use_default_grad_op_maker.spec create mode 100644 tools/diff_use_default_grad_op_maker.py create mode 100644 tools/generate_op_use_grad_op_desc_maker_spec.py diff --git a/paddle/fluid/framework/details/op_registry.h b/paddle/fluid/framework/details/op_registry.h index e5b58ec6876..a9a4fb08a2c 100644 --- a/paddle/fluid/framework/details/op_registry.h +++ b/paddle/fluid/framework/details/op_registry.h @@ -16,6 +16,7 @@ limitations under the License. */ #include #include +#include #include #include #include @@ -183,6 +184,10 @@ struct OpInfoFiller { T maker(fwd_op, no_grad_set, grad_to_var, grad_block); return maker(); }; + + info->use_default_grad_op_desc_maker_ = + std::is_base_of, T>::value || + std::is_base_of, T>::value; } }; diff --git a/paddle/fluid/framework/grad_op_desc_maker.h b/paddle/fluid/framework/grad_op_desc_maker.h index f2f4c53eea2..25a64b69ae8 100644 --- a/paddle/fluid/framework/grad_op_desc_maker.h +++ b/paddle/fluid/framework/grad_op_desc_maker.h @@ -147,7 +147,7 @@ class SingleGradOpDescMaker : public GradOpDescMakerBase { public: using GradOpDescMakerBase::GradOpDescMakerBase; - std::vector> operator()() const { + std::vector> operator()() const final { std::vector> retv; retv.emplace_back(this->Apply()); return retv; @@ -158,14 +158,14 @@ class SingleGradOpDescMaker : public GradOpDescMakerBase { }; template -class DefaultGradOpDescMaker : public SingleGradOpDescMaker { +class DefaultGradOpDescMaker final : public SingleGradOpDescMaker { public: using SingleGradOpDescMaker::SingleGradOpDescMaker; protected: - virtual std::unique_ptr Apply() const { + std::unique_ptr Apply() const final { auto* grad = new OpDesc(); - grad->SetType(this->GradOpType()); + grad->SetType(this->ForwardOpType() + "_grad"); for (auto& input_param : this->InputNames()) { grad->SetInput(input_param, this->Input(input_param)); @@ -182,18 +182,12 @@ class DefaultGradOpDescMaker : public SingleGradOpDescMaker { return std::unique_ptr(grad); } - - virtual std::string GradOpType() const { - return this->ForwardOpType() + "_grad"; - } }; -class EmptyGradOpMaker : public GradOpDescMakerBase { +class EmptyGradOpMaker final : public GradOpDescMakerBase { public: using GradOpDescMakerBase::GradOpDescMakerBase; - std::vector> operator()() const override { - return {}; - } + std::vector> operator()() const final { return {}; } }; } // namespace framework diff --git a/paddle/fluid/framework/op_info.cc b/paddle/fluid/framework/op_info.cc index af75baa5c4b..c815e194d43 100644 --- a/paddle/fluid/framework/op_info.cc +++ b/paddle/fluid/framework/op_info.cc @@ -13,6 +13,9 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/framework/op_info.h" +#include +#include +#include namespace paddle { namespace framework { @@ -24,5 +27,17 @@ OpInfoMap& OpInfoMap::Instance() { static OpInfoMap g_op_info_map; return g_op_info_map; } + +std::vector OpInfoMap::GetUseDefaultGradOpDescMakerOps() const { + // Use set to sort op names + std::set result_ops; + for (auto& pair : map_) { + if (pair.second.use_default_grad_op_desc_maker_) { + result_ops.insert(pair.first); + } + } + return std::vector(result_ops.begin(), result_ops.end()); +} + } // namespace framework } // namespace paddle diff --git a/paddle/fluid/framework/op_info.h b/paddle/fluid/framework/op_info.h index e200d188b3f..daa72769c49 100644 --- a/paddle/fluid/framework/op_info.h +++ b/paddle/fluid/framework/op_info.h @@ -17,6 +17,7 @@ limitations under the License. */ #include #include #include +#include #include "paddle/fluid/framework/attribute.h" #include "paddle/fluid/framework/no_need_buffer_vars_inference.h" @@ -42,6 +43,10 @@ struct OpInfo { InferInplaceOpFN infer_inplace_; InferNoNeedBufferVarsFN infer_no_need_buffer_vars_; + // NOTE(zjl): this flag is added to check whether + // the grad maker is the default one. + bool use_default_grad_op_desc_maker_{false}; + bool HasOpProtoAndChecker() const { return proto_ != nullptr && checker_ != nullptr; } @@ -105,6 +110,8 @@ class OpInfoMap { std::unordered_map* mutable_map() { return &map_; } + std::vector GetUseDefaultGradOpDescMakerOps() const; + private: OpInfoMap() = default; std::unordered_map map_; diff --git a/paddle/fluid/op_use_default_grad_op_maker.spec b/paddle/fluid/op_use_default_grad_op_maker.spec new file mode 100644 index 00000000000..ef46cb8a303 --- /dev/null +++ b/paddle/fluid/op_use_default_grad_op_maker.spec @@ -0,0 +1,110 @@ +abs +acos +asin +atan +attention_lstm +bilinear_interp +bilinear_tensor_product +bpr_loss +brelu +conv_shift +cos +cos_sim +dequantize +elementwise_div +elementwise_max +elementwise_min +elu +fc +flatten +fsp +fused_embedding_fc_lstm +fused_embedding_seq_pool +fusion_gru +fusion_lstm +fusion_repeated_fc_relu +fusion_seqconv_eltadd_relu +fusion_seqexpand_concat_fc +fusion_seqpool_concat +fusion_squared_mat_sub +gelu +gru +hard_shrink +hierarchical_sigmoid +hinge_loss +huber_loss +im2sequence +l1_norm +label_smooth +leaky_relu +linear_chain_crf +log +log_loss +logsigmoid +lookup_table +lrn +lstm +lstm_unit +lstmp +margin_rank_loss +max_pool2d_with_index +max_pool3d_with_index +maxout +modified_huber_loss +multiplex +nce +nearest_interp +norm +pool2d +pool3d +pow +prelu +psroi_pool +quantize +rank_loss +reduce_max +reduce_mean +reduce_min +reduce_prod +reduce_sum +requantize +reshape +rnn_memory_helper +roi_align +roi_perspective_transform +roi_pool +round +row_conv +scatter +sequence_concat +sequence_conv +sequence_expand +sequence_expand_as +sequence_pad +sequence_scatter +sequence_slice +sequence_softmax +sequence_unpad +shuffle_channel +sigmoid_cross_entropy_with_logits +sin +softplus +softshrink +softsign +space_to_depth +spp +square +squared_l2_distance +squared_l2_norm +squeeze +stanh +swish +tanh_shrink +teacher_student_sigmoid_loss +tensor_array_to_tensor +thresholded_relu +transpose +tree_conv +unpool +unsqueeze +warpctc diff --git a/paddle/fluid/operators/fused/fused_embedding_seq_pool_op.cc b/paddle/fluid/operators/fused/fused_embedding_seq_pool_op.cc index 9cc94ab88d5..3ee962d37b1 100644 --- a/paddle/fluid/operators/fused/fused_embedding_seq_pool_op.cc +++ b/paddle/fluid/operators/fused/fused_embedding_seq_pool_op.cc @@ -107,17 +107,6 @@ And the output will change the LoD information with input Ids. } }; -class FusedEmbeddingSeqPoolOpGradDescMaker - : public framework::DefaultGradOpDescMaker { - using ::paddle::framework::DefaultGradOpDescMaker< - true>::DefaultGradOpDescMaker; - - protected: - virtual std::string GradOpType() const { - return "fused_embedding_seq_pool_grad"; - } -}; - class FusedEmbeddingSeqPoolOpGrad : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; @@ -160,7 +149,7 @@ class FusedEmbeddingSeqPoolOpGradVarTypeInference namespace ops = paddle::operators; REGISTER_OPERATOR(fused_embedding_seq_pool, ops::FusedEmbeddingSeqPoolOp, - ops::FusedEmbeddingSeqPoolOpGradDescMaker, + paddle::framework::DefaultGradOpDescMaker, ops::FusedEmbeddingSeqPoolOpMaker); REGISTER_OPERATOR(fused_embedding_seq_pool_grad, ops::FusedEmbeddingSeqPoolOpGrad, diff --git a/paddle/fluid/operators/lookup_table_op.cc b/paddle/fluid/operators/lookup_table_op.cc index d635fc617bc..de1baaf5100 100644 --- a/paddle/fluid/operators/lookup_table_op.cc +++ b/paddle/fluid/operators/lookup_table_op.cc @@ -119,15 +119,6 @@ or not. And the output only shares the LoD information with input Ids. } }; -class LookupTableOpGradDescMaker - : public framework::DefaultGradOpDescMaker { - using ::paddle::framework::DefaultGradOpDescMaker< - true>::DefaultGradOpDescMaker; - - protected: - virtual std::string GradOpType() const { return "lookup_table_grad"; } -}; - class LookupTableOpGrad : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; @@ -169,7 +160,8 @@ class LookupTableOpGradVarTypeInference : public framework::VarTypeInference { namespace ops = paddle::operators; REGISTER_OPERATOR(lookup_table, ops::LookupTableOp, - ops::LookupTableOpGradDescMaker, ops::LookupTableOpMaker); + paddle::framework::DefaultGradOpDescMaker, + ops::LookupTableOpMaker); REGISTER_OPERATOR(lookup_table_grad, ops::LookupTableOpGrad, ops::LookupTableOpGradVarTypeInference); diff --git a/paddle/fluid/operators/nce_op.cc b/paddle/fluid/operators/nce_op.cc index fa7cc58c084..3caa4e60656 100644 --- a/paddle/fluid/operators/nce_op.cc +++ b/paddle/fluid/operators/nce_op.cc @@ -187,14 +187,6 @@ By default this operator uses a uniform distribution for sampling. } }; -class NCEOpGradDescMaker : public framework::DefaultGradOpDescMaker { - using ::paddle::framework::DefaultGradOpDescMaker< - true>::DefaultGradOpDescMaker; - - protected: - virtual std::string GradOpType() const { return "nce_grad"; } -}; - class NCEOpGrad : public framework::OperatorWithKernel { public: using framework::OperatorWithKernel::OperatorWithKernel; @@ -259,7 +251,9 @@ class NCEOpGradVarTypeInference : public framework::VarTypeInference { } // namespace paddle namespace ops = paddle::operators; -REGISTER_OPERATOR(nce, ops::NCEOp, ops::NCEOpGradDescMaker, ops::NCEOpMaker); +REGISTER_OPERATOR(nce, ops::NCEOp, + paddle::framework::DefaultGradOpDescMaker, + ops::NCEOpMaker); REGISTER_OPERATOR(nce_grad, ops::NCEOpGrad, ops::NCEOpGradVarTypeInference); REGISTER_OP_CPU_KERNEL(nce, ops::NCEKernel, ops::NCEKernel); diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index d4c85fd0c6b..93e75543bfb 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -29,6 +29,7 @@ limitations under the License. */ #include "paddle/fluid/framework/lod_rank_table.h" #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/lod_tensor_array.h" +#include "paddle/fluid/framework/op_info.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/parallel_executor.h" #include "paddle/fluid/framework/prune.h" @@ -155,6 +156,9 @@ PYBIND11_MODULE(core, m) { return paddle::operators::AppendPythonCallableObjectAndReturnId(py_obj); }); + m.def("_get_use_default_grad_op_desc_maker_ops", + [] { return OpInfoMap::Instance().GetUseDefaultGradOpDescMakerOps(); }); + // NOTE(zjl): ctest would load environment variables at the beginning even // though we have not `import paddle.fluid as fluid`. So we add this API // to enable eager deletion mode in unittest. diff --git a/paddle/scripts/paddle_build.sh b/paddle/scripts/paddle_build.sh index 025528e85c4..24a77d09b1a 100755 --- a/paddle/scripts/paddle_build.sh +++ b/paddle/scripts/paddle_build.sh @@ -425,6 +425,11 @@ function assert_api_not_changed() { sed -i '/.*ComposeNotAligned.*/d' new.spec python ${PADDLE_ROOT}/tools/diff_api.py ${PADDLE_ROOT}/paddle/fluid/API.spec new.spec + + # Currently, we only check in PR_CI python 2.7 + if [ "$1" == "cp27-cp27m" ]; then + python ${PADDLE_ROOT}/tools/diff_use_default_grad_op_maker.py ${PADDLE_ROOT}/paddle/fluid/op_use_default_grad_op_maker.spec + fi deactivate } @@ -434,9 +439,12 @@ function assert_api_spec_approvals() { fi API_FILES=("paddle/fluid/API.spec" + "paddle/fluid/op_use_default_grad_op_maker.spec" "python/paddle/fluid/parallel_executor.py" "paddle/fluid/framework/operator.h" "paddle/fluid/framework/tensor.h" + "paddle/fluid/framework/details/op_registry.h" + "paddle/fluid/framework/grad_op_desc_maker.h" "paddle/fluid/framework/lod_tensor.h" "paddle/fluid/framework/selected_rows.h" "paddle/fluid/framework/op_desc.h" diff --git a/tools/diff_use_default_grad_op_maker.py b/tools/diff_use_default_grad_op_maker.py new file mode 100644 index 00000000000..9e362f611bb --- /dev/null +++ b/tools/diff_use_default_grad_op_maker.py @@ -0,0 +1,66 @@ +# Copyright (c) 2019 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. + +import os + +os.environ['CUDA_VISIBLE_DEVICES'] = '' + +import paddle.fluid as fluid +import sys + + +def get_op_diff(filename): + ops_created_by_py_func = set( + fluid.core._get_use_default_grad_op_desc_maker_ops()) + + with open(filename, 'r') as f: + ops_read_from_file = set([line.strip() for line in f.readlines()]) + + diff_ops = [] + + for op in ops_read_from_file: + if op not in ops_created_by_py_func: + diff_ops.append(op) + else: + ops_created_by_py_func.remove(op) + + err_msg = [] + diff_ops = list(diff_ops) + if len(diff_ops) > 0: + err_msg.append('Added grad op with DefaultGradOpDescMaker: ' + str( + diff_ops)) + + ops_created_by_py_func = list(ops_created_by_py_func) + if len(ops_created_by_py_func) > 0: + err_msg.append('Remove grad op with DefaultGradOpDescMaker: ' + str( + ops_created_by_py_func)) + + return err_msg + + +if len(sys.argv) != 2: + print('Usage: python diff_use_default_grad_op_maker.py [filepath]') + sys.exit(1) + +file_path = str(sys.argv[1]) +err_msg = get_op_diff(file_path) + +if len(err_msg) > 0: + _, filename = os.path.split(file_path) + print('File `{}` is wrong compared to your PR revision!'.format(filename)) + print( + 'Please use `python generate_op_use_grad_op_desc_maker_spec.py [filepath]` to generate new `{}` file'. + format(filename)) + print('Error message is: ' + '; '.join(err_msg)) + sys.exit(1) diff --git a/tools/generate_op_use_grad_op_desc_maker_spec.py b/tools/generate_op_use_grad_op_desc_maker_spec.py new file mode 100644 index 00000000000..69b062a8716 --- /dev/null +++ b/tools/generate_op_use_grad_op_desc_maker_spec.py @@ -0,0 +1,29 @@ +# Copyright (c) 2019 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. + +import os + +os.environ['CUDA_VISIBLE_DEVICES'] = '' + +import paddle.fluid as fluid +import sys + +if len(sys.argv) != 2: + print('Usage: python generate_op_use_grad_op_desc_maker_spec.py [filepath]') + sys.exit(1) + +with open(sys.argv[1], 'w') as f: + ops = fluid.core._get_use_default_grad_op_desc_maker_ops() + for op in ops: + f.write(op + '\n') -- GitLab