From 3a7caf481ce66b1984f2e1d5d52e717178047a30 Mon Sep 17 00:00:00 2001 From: Zeng Jinle <32832641+sneaxiy@users.noreply.github.com> Date: Thu, 5 Dec 2019 07:03:11 -0600 Subject: [PATCH] add grad maker assert, test=develop (#21564) --- paddle/fluid/pybind/pybind.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 2d2438e7cdb..b80544e9989 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -282,6 +282,30 @@ static void inline CreateVariableIfNotExit( return; } +static void AssertStaticGraphAndDygraphGradMakerNoDiff() { + std::set ops; + for (auto &pair : framework::OpInfoMap::Instance().map()) { + bool has_static_grad_maker = (pair.second.grad_op_maker_ != nullptr); + bool has_dygraph_grad_maker = + (pair.second.dygraph_grad_op_maker_ != nullptr); + if (has_static_grad_maker ^ has_dygraph_grad_maker) { + bool has_kernel = + (framework::OperatorWithKernel::AllOpKernels().count(pair.first) > 0); + if (has_kernel) { + ops.insert(pair.first); + } else { + VLOG(5) << pair.first << " has no kernels, skip"; + } + } + } + PADDLE_ENFORCE_EQ(ops.empty(), true, + platform::errors::Unimplemented( + "OperatorWithKernel [%s] have only static graph grad " + "maker or have only dygraph grad maker, which is not " + "allowed", + string::join_strings(ops, ','))); +} + #ifdef PADDLE_WITH_AVX PYBIND11_MODULE(core_avx, m) { #else @@ -293,6 +317,8 @@ PYBIND11_MODULE(core_noavx, m) { paddle::memory::allocation::UseAllocatorStrategyGFlag(); + AssertStaticGraphAndDygraphGradMakerNoDiff(); + m.doc() = "C++ core of PaddlePaddle"; // using framework in this function. Since it is inside a function, it will -- GitLab