From 5839e3236b04a960df93e87161f708cc99f41593 Mon Sep 17 00:00:00 2001 From: Xin Pan Date: Fri, 26 Oct 2018 18:03:24 +0800 Subject: [PATCH] add program check test=develop --- paddle/fluid/framework/ir/graph.cc | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/paddle/fluid/framework/ir/graph.cc b/paddle/fluid/framework/ir/graph.cc index 265a128e9..bc54a259f 100644 --- a/paddle/fluid/framework/ir/graph.cc +++ b/paddle/fluid/framework/ir/graph.cc @@ -23,8 +23,59 @@ limitations under the License. */ namespace paddle { namespace framework { namespace ir { +namespace { +void CheckProgram(const ProgramDesc &program) { + std::map visit; +#define _INT(role) static_cast(role) + + for (size_t i = 0; i < program.Size(); ++i) { + for (OpDesc *op : program.Block(i).AllOps()) { + int role_id = boost::get( + op->GetAttr(OpProtoAndCheckerMaker::OpRoleAttrName())); + visit[role_id] = true; + switch (role_id) { + case _INT(OpRole::kForward): + PADDLE_ENFORCE( + visit.find(_INT(OpRole::kBackward)) == visit.end(), + "Cannot add forward operator before backward operator."); + break; + case _INT(OpRole::kBackward): + case _INT(OpRole::kBackward) | _INT(OpRole::kLoss): + PADDLE_ENFORCE( + visit.find(_INT(OpRole::kOptimize)) == visit.end(), + "Cannot add backward operator before optimize operator."); + break; + case _INT(OpRole::kForward) | _INT(OpRole::kLoss): + PADDLE_ENFORCE(visit.find(_INT(OpRole::kBackward) | + _INT(OpRole::kLoss)) == visit.end(), + "Cannot add backward|loss operator before " + "forward|loss operator."); + PADDLE_ENFORCE( + visit.find(_INT(OpRole::kOptimize)) == visit.end(), + "Cannot add backward operator before optimize operator."); + break; + case _INT(OpRole::kOptimize): + case _INT(OpRole::kOptimize) | _INT(OpRole::kLRSched): + PADDLE_ENFORCE(visit.find(_INT(OpRole::kBackward)) != visit.end(), + "Optimize operators must follow backward operator."); + break; + case _INT(OpRole::kLRSched): + case _INT(OpRole::kDist): + case _INT(OpRole::kRPC): + case _INT(OpRole::kNotSpecified): + break; + default: + LOG(FATAL) << "Unknown operator role. Don't add new role because " + "you don't know what you are doing."; + } + } + } +#undef _INT +} +} // namespace Graph::Graph(const ProgramDesc &program) : program_(program) { + CheckProgram(program_); // Make the nodes id start from 0. Node::ResetId(); auto var_nodes = InitFromProgram(program_); -- GitLab