From 305bd25be7689503c2d034303029fb613c25774c Mon Sep 17 00:00:00 2001 From: chengduo <30176695+chengduoZH@users.noreply.github.com> Date: Tue, 20 Aug 2019 16:03:57 +0800 Subject: [PATCH] [Cherry pick] Fix register op without gradient (#19272) * fix REGISTER_OP_WITHOUT_GRADIENT test=develop --- paddle/fluid/framework/op_registry.h | 9 +++-- .../fluid/tests/unittests/test_backward.py | 34 ++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/framework/op_registry.h b/paddle/fluid/framework/op_registry.h index a53a81c270a..3f14f47f0dd 100644 --- a/paddle/fluid/framework/op_registry.h +++ b/paddle/fluid/framework/op_registry.h @@ -16,6 +16,7 @@ limitations under the License. */ #include #include +#include #include #include #include @@ -53,8 +54,9 @@ class Registrar { template struct OperatorRegistrar : public Registrar { explicit OperatorRegistrar(const char* op_type) { - PADDLE_ENFORCE(!OpInfoMap::Instance().Has(op_type), - "'%s' is registered more than once.", op_type); + if (OpInfoMap::Instance().Has(op_type)) { + PADDLE_THROW("'%s' is registered more than once.", op_type); + } static_assert(sizeof...(ARGS) != 0, "OperatorRegistrar should be invoked at least by OpClass"); OpInfo info; @@ -206,7 +208,8 @@ struct OpKernelRegistrarFunctorEx