Merge branch 'sequence_enumerate_op' of https://github.com/chenwhql/Paddle into sequence_enumerate_op
	
		
	
				
					
				
			
						commit
						b081363bae
					
				
											
												
													File diff suppressed because one or more lines are too long
												
											
										
									
								
											
												
													File diff suppressed because it is too large
													Load Diff
												
											
										
									
								| @ -0,0 +1,126 @@ | |||||||
|  | // Copyright (c) 2018 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/ir/fc_lstm_fuse_pass.h" | ||||||
|  | 
 | ||||||
|  | namespace paddle { | ||||||
|  | namespace framework { | ||||||
|  | namespace ir { | ||||||
|  | 
 | ||||||
|  | std::unique_ptr<ir::Graph> FCLstmFusePass::ApplyImpl( | ||||||
|  |     std::unique_ptr<ir::Graph> graph) const { | ||||||
|  |   GraphPatternDetector gpd; | ||||||
|  |   auto* pattern = gpd.mutable_pattern(); | ||||||
|  | 
 | ||||||
|  |   std::unordered_set<int> fused_ops({// first lstm
 | ||||||
|  |                                      13, 15, 16, | ||||||
|  |                                      // second lstm
 | ||||||
|  |                                      23, 25, 26}); | ||||||
|  | 
 | ||||||
|  |   pattern->NewNode([&](Node* x) { return fused_ops.count(x->id()); }, | ||||||
|  |                    "any_node"); | ||||||
|  | 
 | ||||||
|  |   std::unordered_set<Node*> marked_nodes; | ||||||
|  | 
 | ||||||
|  |   auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, | ||||||
|  |                      Graph* g) { | ||||||
|  | 
 | ||||||
|  |     auto* id = subgraph.at(gpd.pattern().RetriveNode("any_node")); | ||||||
|  |     marked_nodes.insert(id); | ||||||
|  |   }; | ||||||
|  |   gpd(graph.get(), handler); | ||||||
|  | 
 | ||||||
|  |   // Create New OpDesc
 | ||||||
|  |   auto lstm_creator = [&](int lstm, int input, int weight_x, int weight_h, | ||||||
|  |                           int bias, int hidden, int cell, int xx) { | ||||||
|  | #define GET_NODE(x) auto* x##_n = graph->RetriveNode(x); | ||||||
|  |     GET_NODE(input); | ||||||
|  |     GET_NODE(weight_x); | ||||||
|  |     GET_NODE(weight_h); | ||||||
|  |     GET_NODE(bias); | ||||||
|  |     GET_NODE(hidden); | ||||||
|  |     GET_NODE(cell); | ||||||
|  |     GET_NODE(xx); | ||||||
|  |     GET_NODE(lstm); | ||||||
|  | 
 | ||||||
|  |     OpDesc op_desc; | ||||||
|  |     op_desc.SetType("fusion_lstm"); | ||||||
|  | #define SET_IN(Key, node__) op_desc.SetInput(#Key, {node__##_n->Name()}); | ||||||
|  |     SET_IN(X, input); | ||||||
|  |     SET_IN(WeightX, weight_x); | ||||||
|  |     SET_IN(WeightH, weight_h); | ||||||
|  |     SET_IN(Bias, bias); | ||||||
|  | #undef GET_NODE | ||||||
|  | #undef SET_IN | ||||||
|  | 
 | ||||||
|  |     LOG(INFO) << "hidden_n: " << hidden_n->Name(); | ||||||
|  |     LOG(INFO) << "cell: " << cell_n->Name(); | ||||||
|  |     LOG(INFO) << "xx: " << xx_n->Name(); | ||||||
|  | 
 | ||||||
|  |     op_desc.SetInput("H0", {}); | ||||||
|  |     op_desc.SetInput("C0", {}); | ||||||
|  |     op_desc.SetOutput("Hidden", {hidden_n->Name()}); | ||||||
|  |     op_desc.SetOutput("Cell", {cell_n->Name()}); | ||||||
|  |     op_desc.SetOutput("XX", {xx_n->Name()}); | ||||||
|  |     op_desc.SetOutput("BatchedGate", {"blstm_0.tmp_2"}); | ||||||
|  |     op_desc.SetOutput("BatchCellPreAct", {"blstm_1.tmp_2"}); | ||||||
|  |     op_desc.SetAttr("is_reverse", lstm_n->Op()->GetAttr("is_reverse")); | ||||||
|  |     op_desc.SetAttr("use_peepholes", false); | ||||||
|  |     auto* op = graph->CreateOpNode(&op_desc); | ||||||
|  | 
 | ||||||
|  | #define LINK_TO(a, b)      \ | ||||||
|  |   a->outputs.push_back(b); \ | ||||||
|  |   b->inputs.push_back(a); | ||||||
|  |     LINK_TO(input_n, op); | ||||||
|  |     LINK_TO(weight_x_n, op); | ||||||
|  |     LINK_TO(weight_h_n, op); | ||||||
|  |     LINK_TO(bias_n, op); | ||||||
|  |     LINK_TO(op, hidden_n); | ||||||
|  | #undef LINK_TO | ||||||
|  |     return op; | ||||||
|  | 
 | ||||||
|  |   }; | ||||||
|  | 
 | ||||||
|  |   lstm_creator(16, 12, 14, 18, 17, 22, 21, 19); | ||||||
|  |   lstm_creator(26, 12, 24, 28, 27, 32, 31, 29); | ||||||
|  | 
 | ||||||
|  |   // remove all the nodes
 | ||||||
|  | 
 | ||||||
|  |   for (auto* node : marked_nodes) { | ||||||
|  |     graph->RemoveNode(const_cast<Node*>(node)); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   for (auto* node : graph->Nodes()) { | ||||||
|  |     for (auto it = node->inputs.begin(); it != node->inputs.end();) { | ||||||
|  |       if (marked_nodes.count(*it)) { | ||||||
|  |         it = const_cast<Node*>(node)->inputs.erase(it); | ||||||
|  |       } else | ||||||
|  |         it++; | ||||||
|  |     } | ||||||
|  |     for (auto it = node->outputs.begin(); it != node->outputs.end();) { | ||||||
|  |       if (marked_nodes.count(*it)) { | ||||||
|  |         it = const_cast<Node*>(node)->outputs.erase(it); | ||||||
|  |       } else | ||||||
|  |         it++; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   return graph; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | }  // namespace ir
 | ||||||
|  | }  // namespace framework
 | ||||||
|  | }  // namespace paddle
 | ||||||
|  | 
 | ||||||
|  | REGISTER_PASS(fc_lstm_fuse_pass, paddle::framework::ir::FCLstmFusePass); | ||||||
| @ -0,0 +1,33 @@ | |||||||
|  | // Copyright (c) 2018 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/ir/graph.h" | ||||||
|  | #include "paddle/fluid/framework/ir/graph_pattern_detector.h" | ||||||
|  | #include "paddle/fluid/framework/ir/pass.h" | ||||||
|  | 
 | ||||||
|  | namespace paddle { | ||||||
|  | namespace framework { | ||||||
|  | namespace ir { | ||||||
|  | 
 | ||||||
|  | class FCLstmFusePass : public Pass { | ||||||
|  |  public: | ||||||
|  |   virtual ~FCLstmFusePass() {} | ||||||
|  | 
 | ||||||
|  |  protected: | ||||||
|  |   std::unique_ptr<ir::Graph> ApplyImpl(std::unique_ptr<ir::Graph> graph) const; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | }  // namespace ir
 | ||||||
|  | }  // namespace framework
 | ||||||
|  | }  // namespace paddle
 | ||||||
| @ -0,0 +1,44 @@ | |||||||
|  | // Copyright (c) 2018 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.
 | ||||||
|  | 
 | ||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #include "paddle/fluid/framework/ir/graph.h" | ||||||
|  | #include "paddle/fluid/framework/ir/pass.h" | ||||||
|  | #include "paddle/fluid/framework/scope.h" | ||||||
|  | 
 | ||||||
|  | namespace paddle { | ||||||
|  | namespace framework { | ||||||
|  | namespace ir { | ||||||
|  | 
 | ||||||
|  | static const char kParamScopeAttr[] = "param_scope"; | ||||||
|  | 
 | ||||||
|  | class FusePassBase : public Pass { | ||||||
|  |  public: | ||||||
|  |   void Init(Graph* graph) const { graph_ = graph; } | ||||||
|  | 
 | ||||||
|  |   Scope* param_scope() const { | ||||||
|  |     PADDLE_ENFORCE(graph_->Has(kParamScopeAttr)); | ||||||
|  |     return graph_->Get<framework::Scope*>(kParamScopeAttr); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   virtual ~FusePassBase() {} | ||||||
|  | 
 | ||||||
|  |  protected: | ||||||
|  |   mutable Graph* graph_; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | }  // namespace ir
 | ||||||
|  | }  // namespace framework
 | ||||||
|  | }  // namespace paddle
 | ||||||
| @ -0,0 +1,65 @@ | |||||||
|  | /* Copyright (c) 2018 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/ir/graph_to_program_pass.h" | ||||||
|  | 
 | ||||||
|  | #include <map> | ||||||
|  | #include <string> | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | #include "paddle/fluid/framework/ir/graph.h" | ||||||
|  | #include "paddle/fluid/framework/ir/graph_helper.h" | ||||||
|  | 
 | ||||||
|  | #include "paddle/fluid/framework/program_desc.h" | ||||||
|  | 
 | ||||||
|  | namespace paddle { | ||||||
|  | namespace framework { | ||||||
|  | namespace ir { | ||||||
|  | 
 | ||||||
|  | std::unique_ptr<Graph> GraphToProgramPass::ApplyImpl( | ||||||
|  |     std::unique_ptr<Graph> graph) const { | ||||||
|  |   ProgramDesc& program = Get<ProgramDesc>("program"); | ||||||
|  | 
 | ||||||
|  |   std::unique_ptr<proto::ProgramDesc> program_pb( | ||||||
|  |       new proto::ProgramDesc(*program.Proto())); | ||||||
|  | 
 | ||||||
|  |   auto block = program_pb->mutable_blocks(kRootBlockIndex); | ||||||
|  |   block->clear_vars(); | ||||||
|  |   std::unordered_set<std::string> visited_vars; | ||||||
|  |   for (ir::Node* n : graph->Nodes()) { | ||||||
|  |     if (n->NodeType() == ir::Node::Type::kVariable) { | ||||||
|  |       if (n->Var() && visited_vars.count(n->Var()->Name()) == 0) { | ||||||
|  |         visited_vars.insert(n->Var()->Name()); | ||||||
|  |         block->add_vars()->MergeFrom(*n->Var()->Proto()); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   block->clear_ops(); | ||||||
|  |   std::vector<ir::Node*> nodes = TopologySortOperations(*graph); | ||||||
|  |   for (ir::Node* n : nodes) { | ||||||
|  |     if (!n->Op()) { | ||||||
|  |       continue; | ||||||
|  |     } | ||||||
|  |     block->add_ops()->MergeFrom(*n->Op()->Proto()); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   program.CopyFrom(*program_pb); | ||||||
|  |   return graph; | ||||||
|  | } | ||||||
|  | }  // namespace ir
 | ||||||
|  | }  // namespace framework
 | ||||||
|  | }  // namespace paddle
 | ||||||
|  | 
 | ||||||
|  | REGISTER_PASS(graph_to_program_pass, paddle::framework::ir::GraphToProgramPass); | ||||||
| @ -0,0 +1,30 @@ | |||||||
|  | /* Copyright (c) 2018 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. */ | ||||||
|  | 
 | ||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #include "paddle/fluid/framework/ir/pass.h" | ||||||
|  | 
 | ||||||
|  | namespace paddle { | ||||||
|  | namespace framework { | ||||||
|  | namespace ir { | ||||||
|  | 
 | ||||||
|  | class GraphToProgramPass : public Pass { | ||||||
|  |  protected: | ||||||
|  |   std::unique_ptr<Graph> ApplyImpl(std::unique_ptr<Graph> graph) const override; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | }  // namespace ir
 | ||||||
|  | }  // namespace framework
 | ||||||
|  | }  // namespace paddle
 | ||||||
| @ -0,0 +1,110 @@ | |||||||
|  | /* Copyright (c) 2018 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/ir/graph_to_program_pass.h" | ||||||
|  | 
 | ||||||
|  | #include <string> | ||||||
|  | #include <vector> | ||||||
|  | #include "gtest/gtest.h" | ||||||
|  | #include "paddle/fluid/framework/program_desc.h" | ||||||
|  | 
 | ||||||
|  | namespace paddle { | ||||||
|  | namespace framework { | ||||||
|  | namespace ir { | ||||||
|  | 
 | ||||||
|  | void BuildNoCircleGraph(Graph* g) { | ||||||
|  |   OpDesc op1; | ||||||
|  |   op1.SetType("op1"); | ||||||
|  |   OpDesc op2; | ||||||
|  |   op2.SetType("op2"); | ||||||
|  |   OpDesc op3; | ||||||
|  |   op3.SetType("op3"); | ||||||
|  |   OpDesc op4; | ||||||
|  |   op4.SetType("op4"); | ||||||
|  |   OpDesc op5; | ||||||
|  |   op5.SetType("op5"); | ||||||
|  |   VarDesc var1("var1"); | ||||||
|  |   VarDesc var2("var2"); | ||||||
|  |   VarDesc var3("var3"); | ||||||
|  |   VarDesc var4("var4"); | ||||||
|  | 
 | ||||||
|  |   ir::Node* o1 = g->CreateOpNode(&op1); | ||||||
|  |   ir::Node* o2 = g->CreateOpNode(&op2); | ||||||
|  |   ir::Node* o3 = g->CreateOpNode(&op3); | ||||||
|  |   ir::Node* o4 = g->CreateOpNode(&op4); | ||||||
|  |   ir::Node* o5 = g->CreateOpNode(&op5); | ||||||
|  |   ir::Node* v1 = g->CreateVarNode(&var1); | ||||||
|  |   ir::Node* v2 = g->CreateVarNode(&var2); | ||||||
|  |   ir::Node* v3 = g->CreateVarNode(&var3); | ||||||
|  |   ir::Node* v4 = g->CreateVarNode(&var4); | ||||||
|  | 
 | ||||||
|  |   // o1->v1->o2
 | ||||||
|  |   o1->outputs.push_back(v1); | ||||||
|  |   o2->inputs.push_back(v1); | ||||||
|  |   v1->inputs.push_back(o1); | ||||||
|  |   v1->outputs.push_back(o2); | ||||||
|  |   // o2->v2->o3
 | ||||||
|  |   // o2->v2->o4
 | ||||||
|  |   o2->outputs.push_back(v2); | ||||||
|  |   o3->inputs.push_back(v2); | ||||||
|  |   o4->inputs.push_back(v2); | ||||||
|  |   v2->outputs.push_back(o3); | ||||||
|  |   v2->outputs.push_back(o4); | ||||||
|  |   v2->inputs.push_back(o2); | ||||||
|  |   // o2->v3->o5
 | ||||||
|  |   o2->outputs.push_back(v3); | ||||||
|  |   o5->inputs.push_back(v3); | ||||||
|  |   v3->inputs.push_back(o2); | ||||||
|  |   v3->outputs.push_back(o5); | ||||||
|  |   // o3-v4->o5
 | ||||||
|  |   o3->outputs.push_back(v4); | ||||||
|  |   o5->inputs.push_back(v4); | ||||||
|  |   v4->inputs.push_back(o3); | ||||||
|  |   v4->outputs.push_back(o5); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | TEST(GraphToProgramPass, Basic) { | ||||||
|  |   ProgramDesc prog; | ||||||
|  |   std::unique_ptr<Graph> g(new Graph(prog)); | ||||||
|  |   BuildNoCircleGraph(g.get()); | ||||||
|  | 
 | ||||||
|  |   auto pass = paddle::framework::ir::PassRegistry::Instance().Get( | ||||||
|  |       "graph_to_program_pass"); | ||||||
|  | 
 | ||||||
|  |   ProgramDesc compiled_prog; | ||||||
|  |   pass->SetNotOwned<paddle::framework::ProgramDesc>("program", &compiled_prog); | ||||||
|  |   pass->Apply(std::move(g)); | ||||||
|  |   std::vector<OpDesc*> ops = compiled_prog.Block(0).AllOps(); | ||||||
|  |   EXPECT_EQ(ops[0]->Type(), "op1"); | ||||||
|  |   EXPECT_EQ(ops[1]->Type(), "op2"); | ||||||
|  |   if (ops[2]->Type() == "op3") { | ||||||
|  |     EXPECT_EQ(ops[3]->Type(), "op4"); | ||||||
|  |   } else if (ops[2]->Type() == "op4") { | ||||||
|  |     EXPECT_EQ(ops[3]->Type(), "op3"); | ||||||
|  |   } | ||||||
|  |   EXPECT_EQ(ops[4]->Type(), "op5"); | ||||||
|  | 
 | ||||||
|  |   std::unordered_set<std::string> vars; | ||||||
|  |   for (VarDesc* v : compiled_prog.Block(0).AllVars()) { | ||||||
|  |     vars.insert(v->Name()); | ||||||
|  |   } | ||||||
|  |   EXPECT_TRUE(vars.find("var1") != vars.end()); | ||||||
|  |   EXPECT_TRUE(vars.find("var2") != vars.end()); | ||||||
|  |   EXPECT_TRUE(vars.find("var3") != vars.end()); | ||||||
|  | } | ||||||
|  | }  // namespace ir
 | ||||||
|  | }  // namespace framework
 | ||||||
|  | }  // namespace paddle
 | ||||||
|  | 
 | ||||||
|  | USE_PASS(graph_to_program_pass); | ||||||
Some files were not shown because too many files have changed in this diff Show More
					Loading…
					
					
				
		Reference in new issue