提交 b1401fb7 编写于 作者: Y Yiqun Liu 提交者: 石晓伟

Remove subgraph_detector from inference/analysis to the common framework/ir directory. (#22094)

test=develop
上级 50bee83f
...@@ -39,6 +39,7 @@ cc_library(graph_helper SRCS graph_helper.cc DEPS graph) ...@@ -39,6 +39,7 @@ cc_library(graph_helper SRCS graph_helper.cc DEPS graph)
cc_library(pass SRCS pass.cc DEPS graph node graph_helper) cc_library(pass SRCS pass.cc DEPS graph node graph_helper)
cc_library(graph_traits SRCS graph_traits.cc DEPS graph) cc_library(graph_traits SRCS graph_traits.cc DEPS graph)
cc_library(graph_pattern_detector SRCS graph_pattern_detector.cc DEPS graph graph_helper graph_traits) cc_library(graph_pattern_detector SRCS graph_pattern_detector.cc DEPS graph graph_helper graph_traits)
cc_library(subgraph_detector SRCS subgraph_detector.cc DEPS graph_pattern_detector executor)
cc_library(fuse_pass_base SRCS fuse_pass_base.cc DEPS pass) cc_library(fuse_pass_base SRCS fuse_pass_base.cc DEPS pass)
cc_library(placement_pass_base SRCS placement_pass_base.cc DEPS pass) cc_library(placement_pass_base SRCS placement_pass_base.cc DEPS pass)
...@@ -99,7 +100,7 @@ endif() ...@@ -99,7 +100,7 @@ endif()
if(WITH_NGRAPH) if(WITH_NGRAPH)
cc_library(ngraph_subgraph_pass SRCS ngraph_subgraph_pass.cc DEPS ngraph_bridge cc_library(ngraph_subgraph_pass SRCS ngraph_subgraph_pass.cc DEPS ngraph_bridge
analysis_helper subgraph_detector graph_pattern_detector pass fuse_pass_base ${op_library_DEPS}) subgraph_detector fuse_pass_base ${op_library_DEPS})
set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h) set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h)
file(APPEND ${pass_file} "USE_PASS(ngraph_subgraph_pass);\n") file(APPEND ${pass_file} "USE_PASS(ngraph_subgraph_pass);\n")
set(INFER_IR_PASSES ${INFER_IR_PASSES} ngraph_subgraph_pass CACHE INTERNAL "") set(INFER_IR_PASSES ${INFER_IR_PASSES} ngraph_subgraph_pass CACHE INTERNAL "")
......
...@@ -20,8 +20,7 @@ ...@@ -20,8 +20,7 @@
#include "paddle/fluid/framework/ir/graph_helper.h" #include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/framework/ir/graph_pattern_detector.h" #include "paddle/fluid/framework/ir/graph_pattern_detector.h"
#include "paddle/fluid/framework/ir/ngraph_subgraph_pass.h" #include "paddle/fluid/framework/ir/ngraph_subgraph_pass.h"
#include "paddle/fluid/inference/analysis/helper.h" #include "paddle/fluid/framework/ir/subgraph_detector.h"
#include "paddle/fluid/inference/analysis/ir_passes/subgraph_detector.h"
#include "paddle/fluid/operators/ngraph/ngraph_bridge.h" #include "paddle/fluid/operators/ngraph/ngraph_bridge.h"
#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/string/pretty_log.h" #include "paddle/fluid/string/pretty_log.h"
...@@ -30,8 +29,6 @@ namespace paddle { ...@@ -30,8 +29,6 @@ namespace paddle {
namespace framework { namespace framework {
namespace ir { namespace ir {
namespace ANAT = paddle::inference::analysis;
std::string GenerateEngineKey(const std::set<std::string> &engine_inputs, std::string GenerateEngineKey(const std::set<std::string> &engine_inputs,
const std::set<std::string> &engine_outputs, const std::set<std::string> &engine_outputs,
const std::string &size) { const std::string &size) {
...@@ -59,19 +56,18 @@ void NgraphSubgraphPass::ApplyImpl(Graph *graph) const { ...@@ -59,19 +56,18 @@ void NgraphSubgraphPass::ApplyImpl(Graph *graph) const {
return !paddle::operators::NgraphBridge::isRegister(op_type); return !paddle::operators::NgraphBridge::isRegister(op_type);
}; };
ANAT::SubGraphFuser fuser(graph, teller, 0, "ngraph_engine"); SubGraphFuser fuser(graph, teller, 0, "ngraph_engine");
fuser(); fuser();
for (auto *node : graph->Nodes()) { for (auto *node : graph->Nodes()) {
if (node->IsOp() && !ANAT::Agent(node).subgraph()->empty()) { if (node->IsOp() && !Agent(node).subgraph()->empty()) {
OpDesc *op_desc = node->Op(); OpDesc *op_desc = node->Op();
op_desc->SetType("ngraph_engine"); op_desc->SetType("ngraph_engine");
CreateNgraphEngineOp(node, graph); CreateNgraphEngineOp(node, graph);
std::unordered_set<const Node *> nodes2remove( std::unordered_set<const Node *> nodes2remove(
ANAT::Agent(node).subgraph()->begin(), Agent(node).subgraph()->begin(), Agent(node).subgraph()->end());
ANAT::Agent(node).subgraph()->end());
GraphSafeRemoveNodes(graph, nodes2remove); GraphSafeRemoveNodes(graph, nodes2remove);
} }
...@@ -79,7 +75,7 @@ void NgraphSubgraphPass::ApplyImpl(Graph *graph) const { ...@@ -79,7 +75,7 @@ void NgraphSubgraphPass::ApplyImpl(Graph *graph) const {
std::unordered_set<const Node *> nodes2remove; std::unordered_set<const Node *> nodes2remove;
for (auto *node : graph->Nodes()) { for (auto *node : graph->Nodes()) {
if (node->IsOp() && ANAT::Agent(node).deleted()) { if (node->IsOp() && Agent(node).deleted()) {
nodes2remove.insert(node); nodes2remove.insert(node);
} }
} }
...@@ -116,7 +112,7 @@ void UpdateNgraphIO(Node *node, Graph *graph, ...@@ -116,7 +112,7 @@ void UpdateNgraphIO(Node *node, Graph *graph,
return; return;
} }
auto &subgraph = *ANAT::Agent(node).subgraph(); auto &subgraph = *Agent(node).subgraph();
std::unordered_set<std::string> inputs; std::unordered_set<std::string> inputs;
std::unordered_set<std::string> outputs; std::unordered_set<std::string> outputs;
for (auto *node : subgraph) { for (auto *node : subgraph) {
...@@ -138,7 +134,7 @@ void UpdateNgraphIO(Node *node, Graph *graph, ...@@ -138,7 +134,7 @@ void UpdateNgraphIO(Node *node, Graph *graph,
} }
void NgraphSubgraphPass::CreateNgraphEngineOp(Node *node, Graph *graph) const { void NgraphSubgraphPass::CreateNgraphEngineOp(Node *node, Graph *graph) const {
auto &subgraph = *ANAT::Agent(node).subgraph(); auto &subgraph = *Agent(node).subgraph();
PADDLE_ENFORCE_NE(subgraph.empty(), true, "subgraph cannot be empty"); PADDLE_ENFORCE_NE(subgraph.empty(), true, "subgraph cannot be empty");
framework::proto::BlockDesc block_proto; framework::proto::BlockDesc block_proto;
......
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/fluid/inference/analysis/ir_passes/subgraph_detector.h" #include "paddle/fluid/framework/ir/subgraph_detector.h"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
#include "paddle/fluid/framework/ir/graph_helper.h" #include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/framework/ir/graph_pattern_detector.h" #include "paddle/fluid/framework/ir/graph_pattern_detector.h"
#include "paddle/fluid/framework/ir/node.h" #include "paddle/fluid/framework/ir/node.h"
DECLARE_bool(use_ngraph); DECLARE_bool(use_ngraph);
namespace paddle { namespace paddle {
namespace inference { namespace framework {
namespace analysis { namespace ir {
using framework::ir::Node; std::pair<std::vector<Node *>, std::vector<Node *>>
ExtractInputAndOutputOfSubGraph(std::vector<Node *> &graph) { // NOLINT
std::pair<std::vector<Node *>, std::vector<Node *>> std::unordered_set<Node *> nodes(graph.begin(), graph.end());
ExtractInputAndOutputOfSubGraph(std::vector<Node *> &graph) { // NOLINT std::unordered_set<Node *> inputs;
std::unordered_set<Node *> nodes(graph.begin(), graph.end()); std::unordered_set<Node *> outputs;
std::unordered_set<Node *> inputs; // Input a Value, check whether its inlink is in the subgraph.
std::unordered_set<Node *> outputs; auto inlink_in_subgraph = [&](Node *n) {
// Input a Value, check whether its inlink is in the subgraph. for (auto *in : n->inputs) {
auto inlink_in_subgraph = [&](Node *n) { if (nodes.count(in)) return true;
for (auto *in : n->inputs) { }
if (nodes.count(in)) return true; return false;
} };
return false;
}; for (auto &node : graph) {
for (auto *in : node->inputs) {
for (auto &node : graph) { // The Value that is written by nodes inside a sub-graph shouldn't be the
for (auto *in : node->inputs) { // input of the sub-graph.
// The Value that is written by nodes inside a sub-graph shouldn't be the if (!nodes.count(in) && in->IsVar() && !inlink_in_subgraph(in)) {
// input of the sub-graph. inputs.insert(in);
if (!nodes.count(in) && in->IsVar() && !inlink_in_subgraph(in)) { }
inputs.insert(in); }
} for (auto *out : node->outputs) {
} if (!nodes.count(out) && out->IsVar()) {
for (auto *out : node->outputs) { outputs.insert(out);
if (!nodes.count(out) && out->IsVar()) { }
outputs.insert(out); }
} }
} return std::make_pair(std::vector<Node *>(inputs.begin(), inputs.end()),
} std::vector<Node *>(outputs.begin(), outputs.end()));
return std::make_pair(std::vector<Node *>(inputs.begin(), inputs.end()), }
std::vector<Node *>(outputs.begin(), outputs.end()));
} // Filter the Intermediate results of the subgraph node.
void FilterRedundantOutputOfSubGraph(Graph *graph) {
// Filter the Intermediate results of the subgraph node. std::vector<Node *> op_nodes;
void FilterRedundantOutputOfSubGraph(Graph *graph) { for (auto &node : TopologicalSort(*graph)) {
std::vector<Node *> op_nodes; if (node.IsVar() || Agent(&node).deleted()) {
for (auto &node : TopologicalSort(*graph)) { continue;
if (node.IsVar() || Agent(&node).deleted()) { }
continue; op_nodes.push_back(&node);
} }
op_nodes.push_back(&node); size_t op_num = op_nodes.size();
} for (size_t i = 0; i < op_num; i++) {
size_t op_num = op_nodes.size(); if (op_nodes[i]->IsOp()) continue;
for (size_t i = 0; i < op_num; i++) { std::unordered_set<std::string> follow_up_input_names;
if (op_nodes[i]->IsOp()) continue; for (size_t j = i + 1; j < op_num; j++) {
std::unordered_set<std::string> follow_up_input_names; for (auto *in : op_nodes[j]->inputs) {
for (size_t j = i + 1; j < op_num; j++) { follow_up_input_names.insert(in->Name());
for (auto *in : op_nodes[j]->inputs) { }
follow_up_input_names.insert(in->Name()); }
} std::vector<Node *> filtered_subgraph_outlinks;
} for (auto *out : op_nodes[i]->outputs) {
std::vector<Node *> filtered_subgraph_outlinks; if (follow_up_input_names.count(out->Name())) {
for (auto *out : op_nodes[i]->outputs) { filtered_subgraph_outlinks.push_back(out);
if (follow_up_input_names.count(out->Name())) { } else {
filtered_subgraph_outlinks.push_back(out); Agent(out).set_deleted(true);
} else { }
Agent(out).set_deleted(true); }
} // The filtered_subgraph_outlinks may be empty.
} op_nodes[i]->outputs = filtered_subgraph_outlinks;
// The filtered_subgraph_outlinks may be empty. }
op_nodes[i]->outputs = filtered_subgraph_outlinks; }
}
} std::vector<std::vector<Node *>> SubgraphDetector::operator()() {
MarkNodesInsideSubGraph();
std::vector<std::vector<Node *>> SubgraphDetector::operator()() { return ExtractSubGraphs();
MarkNodesInsideSubGraph(); }
return ExtractSubGraphs();
} // Mark the output variables inside a subgraph with the func.
inline void MarkOutLinksInSubGraph(const Node *func) {
// Mark the output variables inside a subgraph with the func. for (auto *var : func->outputs) {
inline void MarkOutLinksInSubGraph(const Node *func) { Agent(var).set_marked(true);
for (auto *var : func->outputs) { }
Agent(var).set_marked(true); }
}
} void SubgraphDetector::MarkNodesInsideSubGraph() {
for (auto &node : framework::ir::GraphTraits::DFS(*graph_)) {
void SubgraphDetector::MarkNodesInsideSubGraph() { if (node_inside_subgraph_teller_(&node)) {
for (auto &node : framework::ir::GraphTraits::DFS(*graph_)) { Agent(&node).set_marked(true);
if (node_inside_subgraph_teller_(&node)) { if (node.IsOp()) {
Agent(&node).set_marked(true); // If a function is inside the sub-graph, mark all the output variables
if (node.IsOp()) { // to be inside too, so that two marked functions will be inside a same
// If a function is inside the sub-graph, mark all the output variables // sub-graph, lets take a example: A_function->var->B_function, if
// to be inside too, so that two marked functions will be inside a same // A_function is marked, var should also be marked, so that B_function
// sub-graph, lets take a example: A_function->var->B_function, if // will be in the same sub-graph with A_function if B_function is
// A_function is marked, var should also be marked, so that B_function // marked.
// will be in the same sub-graph with A_function if B_function is MarkOutLinksInSubGraph(&node);
// marked. }
MarkOutLinksInSubGraph(&node); }
} }
} }
}
} // Use the Union Find(UF) algorithm to find fully connected sub-graphs, if node
// a's output is node b, that is a and b is in the same sub-graph. The UF
// Use the Union Find(UF) algorithm to find fully connected sub-graphs, if node // algorithm will group them to the same cluster.
// a's output is node b, that is a and b is in the same sub-graph. The UF using node_map_t = std::unordered_map<int, Node *>;
// algorithm will group them to the same cluster. // Find the ancestor id of a node.
using node_map_t = std::unordered_map<int, Node *>; int UnionFindGetAncestor(const node_map_t &node_map, size_t id) {
// Find the ancestor id of a node. int tmp = id;
int UnionFindGetAncestor(const node_map_t &node_map, size_t id) { do {
int tmp = id; tmp = Agent(node_map.at(tmp)).union_find_parent();
do { } while (Agent(node_map.at(tmp)).union_find_parent() != tmp);
tmp = Agent(node_map.at(tmp)).union_find_parent(); return tmp;
} while (Agent(node_map.at(tmp)).union_find_parent() != tmp); }
return tmp; // Make this two node share the same ancestor.
} // TODO(Superjom) bad performance, make a balanced tree latter.
// Make this two node share the same ancestor. void UnionFindCombine(const node_map_t &node_map, size_t a, size_t b) {
// TODO(Superjom) bad performance, make a balanced tree latter. int a_ancestor = UnionFindGetAncestor(node_map, a);
void UnionFindCombine(const node_map_t &node_map, size_t a, size_t b) { int b_ancestor = UnionFindGetAncestor(node_map, b);
int a_ancestor = UnionFindGetAncestor(node_map, a); Agent(node_map.at(b_ancestor)).set_union_find_parent(a_ancestor);
int b_ancestor = UnionFindGetAncestor(node_map, b); Agent(node_map.at(a)).set_union_find_parent(a_ancestor);
Agent(node_map.at(b_ancestor)).set_union_find_parent(a_ancestor); Agent(node_map.at(b)).set_union_find_parent(a_ancestor);
Agent(node_map.at(a)).set_union_find_parent(a_ancestor); }
Agent(node_map.at(b)).set_union_find_parent(a_ancestor);
} // This is a simple representation of a graph.
// The BriefNode hold the pointer of the Node.
// This is a simple representation of a graph. // This is to avoid changing the original graph
// The BriefNode hold the pointer of the Node. // in the process of trt graph analysis.
// This is to avoid changing the original graph struct BriefNode {
// in the process of trt graph analysis. explicit BriefNode(Node *n) { node = n; }
struct BriefNode { Node *node;
explicit BriefNode(Node *n) { node = n; } std::vector<BriefNode *> inlinks;
Node *node; std::vector<BriefNode *> outlinks;
std::vector<BriefNode *> inlinks; };
std::vector<BriefNode *> outlinks;
}; // Union two adjacent BriefNode.
// Suppose we have two adjacent nodes src and dst.
// Union two adjacent BriefNode. // We will perform the following operations:
// Suppose we have two adjacent nodes src and dst. // 1. add all inputs(except src) of dst to src inlinks.
// We will perform the following operations: // 2. add all outputs of dst to src outlinks.
// 1. add all inputs(except src) of dst to src inlinks. // 3. change all the dst's inputs and outputs
// 2. add all outputs of dst to src outlinks. // corresponding inlinks and outlinks to src node.
// 3. change all the dst's inputs and outputs // 4. delete all dst's inlinks and outlinks.
// corresponding inlinks and outlinks to src node. void UnionContractedNodes(const std::unordered_map<int, BriefNode *> &node_map,
// 4. delete all dst's inlinks and outlinks. int src_id, int dst_id) {
void UnionContractedNodes(const std::unordered_map<int, BriefNode *> &node_map, // merge the two adjacent nodes into one node.
int src_id, int dst_id) { BriefNode *src_node = node_map.at(src_id);
// merge the two adjacent nodes into one node. BriefNode *dst_node = node_map.at(dst_id);
BriefNode *src_node = node_map.at(src_id);
BriefNode *dst_node = node_map.at(dst_id); std::unordered_set<BriefNode *> inputs(src_node->inlinks.begin(),
src_node->inlinks.end());
std::unordered_set<BriefNode *> inputs(src_node->inlinks.begin(), std::unordered_set<BriefNode *> outputs;
src_node->inlinks.end());
std::unordered_set<BriefNode *> outputs; for (auto *n : src_node->outlinks) {
if (n != dst_node) outputs.insert(n);
for (auto *n : src_node->outlinks) { }
if (n != dst_node) outputs.insert(n);
} // Add the inlinks and outlinks of dst node to src node.
std::vector<BriefNode *> dst_in_nodes = dst_node->inlinks;
// Add the inlinks and outlinks of dst node to src node. for (BriefNode *node : dst_in_nodes) {
std::vector<BriefNode *> dst_in_nodes = dst_node->inlinks; if (node != src_node) {
for (BriefNode *node : dst_in_nodes) { inputs.insert(node);
if (node != src_node) { }
inputs.insert(node); }
}
} std::vector<BriefNode *> dst_out_nodes = dst_node->outlinks;
for (BriefNode *node : dst_out_nodes) {
std::vector<BriefNode *> dst_out_nodes = dst_node->outlinks; outputs.insert(node);
for (BriefNode *node : dst_out_nodes) { }
outputs.insert(node);
} // update the dst and src node's inlinks and outlinks.
#ifdef __clang__
// update the dst and src node's inlinks and outlinks. src_node->inlinks = std::vector<BriefNode *>(inputs.begin(), inputs.end());
#ifdef __clang__ src_node->outlinks = std::vector<BriefNode *>(outputs.begin(), outputs.end());
src_node->inlinks = std::vector<BriefNode *>(inputs.begin(), inputs.end()); dst_node->inlinks.clear();
src_node->outlinks = std::vector<BriefNode *>(outputs.begin(), outputs.end()); dst_node->outlinks.clear();
dst_node->inlinks.clear(); #else
dst_node->outlinks.clear(); src_node->inlinks =
#else std::move(std::vector<BriefNode *>(inputs.begin(), inputs.end()));
src_node->inlinks = src_node->outlinks =
std::move(std::vector<BriefNode *>(inputs.begin(), inputs.end())); std::move(std::vector<BriefNode *>(outputs.begin(), outputs.end()));
src_node->outlinks = dst_node->inlinks.clear();
std::move(std::vector<BriefNode *>(outputs.begin(), outputs.end())); dst_node->outlinks.clear();
dst_node->inlinks.clear(); #endif
dst_node->outlinks.clear();
#endif auto inlink_or_outlink_cleaner = [&](std::vector<BriefNode *> &nodes) {
for (auto *&n : nodes) {
auto inlink_or_outlink_cleaner = [&](std::vector<BriefNode *> &nodes) { if (n == src_node || n == dst_node) {
for (auto *&n : nodes) { n = src_node;
if (n == src_node || n == dst_node) { }
n = src_node; }
} };
} // Change all the dst inputs and outputs corresponding inlink and
}; // outlink to the src node.
// Change all the dst inputs and outputs corresponding inlink and for (auto *node : src_node->inlinks) {
// outlink to the src node. inlink_or_outlink_cleaner(node->outlinks);
for (auto *node : src_node->inlinks) { }
inlink_or_outlink_cleaner(node->outlinks);
} for (auto *node : src_node->outlinks) {
inlink_or_outlink_cleaner(node->inlinks);
for (auto *node : src_node->outlinks) { }
inlink_or_outlink_cleaner(node->inlinks); }
}
} // FlexibleDFS
// If reverse is true, do reverse dfs.
// FlexibleDFS // If enter func is not nullptr, calls enter(node) before visiting any children
// If reverse is true, do reverse dfs. // of node.
// If enter func is not nullptr, calls enter(node) before visiting any children // If leave func not nullptr, calls leave(node) after visiting all parents of
// of node. // node.
// If leave func not nullptr, calls leave(node) after visiting all parents of void FlexibleDFS(const std::vector<BriefNode *> &source, bool reverse,
// node. const std::function<bool(const BriefNode *)> &enter,
void FlexibleDFS(const std::vector<BriefNode *> &source, bool reverse, const std::function<bool(const BriefNode *)> &leave) {
const std::function<bool(const BriefNode *)> &enter, typedef struct {
const std::function<bool(const BriefNode *)> &leave) { const BriefNode *node;
typedef struct { bool leave;
const BriefNode *node; } FNode;
bool leave;
} FNode; std::vector<FNode> stack;
for (auto &node : source) {
std::vector<FNode> stack; stack.push_back(FNode{node, false});
for (auto &node : source) { }
stack.push_back(FNode{node, false}); std::unordered_set<const BriefNode *> visited;
} while (!stack.empty()) {
std::unordered_set<const BriefNode *> visited; auto fnode = stack.back();
while (!stack.empty()) { stack.pop_back();
auto fnode = stack.back();
stack.pop_back(); if (fnode.leave) {
if (leave && !leave(fnode.node)) return;
if (fnode.leave) { }
if (leave && !leave(fnode.node)) return; if (visited.count(fnode.node)) continue;
} visited.insert(fnode.node);
if (visited.count(fnode.node)) continue;
visited.insert(fnode.node); if (enter && !enter(fnode.node)) return;
if (enter && !enter(fnode.node)) return; if (leave) stack.push_back(FNode{fnode.node, true});
const std::vector<BriefNode *> iter_nodes =
if (leave) stack.push_back(FNode{fnode.node, true}); reverse == true ? fnode.node->inlinks : fnode.node->outlinks;
const std::vector<BriefNode *> iter_nodes = for (const BriefNode *node : iter_nodes) {
reverse == true ? fnode.node->inlinks : fnode.node->outlinks; if (!visited.count(node)) {
for (const BriefNode *node : iter_nodes) { stack.push_back(FNode{node, false});
if (!visited.count(node)) { }
stack.push_back(FNode{node, false}); }
} }
} }
}
} std::vector<std::vector<Node *>> SubgraphDetector::ExtractSubGraphs() {
// Run the Extract algorithm to find all subgraphs.
std::vector<std::vector<Node *>> SubgraphDetector::ExtractSubGraphs() { std::vector<Node *> marked_nodes;
// Run the Extract algorithm to find all subgraphs. // We use brief_node_map to represent the original graph in order to avoid
std::vector<Node *> marked_nodes; // changing the original graph.
// We use brief_node_map to represent the original graph in order to avoid std::unordered_map<int, BriefNode *> brief_node_map;
// changing the original graph.
std::unordered_map<int, BriefNode *> brief_node_map; std::unordered_set<int32_t> valid_node_ids;
for (auto *node : graph_->Nodes()) {
std::unordered_set<int32_t> valid_node_ids; valid_node_ids.insert(node->id());
for (auto *node : graph_->Nodes()) { }
valid_node_ids.insert(node->id());
} for (auto &node : framework::ir::GraphTraits::TS(*graph_)) {
brief_node_map[node.id()] = new BriefNode(&node);
for (auto &node : framework::ir::GraphTraits::TS(*graph_)) { if (Agent(&node).marked()) {
brief_node_map[node.id()] = new BriefNode(&node); marked_nodes.push_back(&node);
if (Agent(&node).marked()) { }
marked_nodes.push_back(&node); }
}
} // extract sub-graphs in the marked node set, use Union Find algorithm.
node_map_t node_map; // id to ptr
// extract sub-graphs in the marked node set, use Union Find algorithm. for (auto *n : marked_nodes) {
node_map_t node_map; // id to ptr // n's parent == n.id means it is the ancestor
for (auto *n : marked_nodes) { Agent(n).set_union_find_parent(n->id());
// n's parent == n.id means it is the ancestor node_map[n->id()] = n;
Agent(n).set_union_find_parent(n->id()); }
node_map[n->id()] = n;
} // create breif node map
for (auto &itr : brief_node_map) {
// create breif node map for (Node *node : itr.second->node->inputs) {
for (auto &itr : brief_node_map) { if (!valid_node_ids.count(node->id())) {
for (Node *node : itr.second->node->inputs) { LOG(INFO) << "invalid node id " << node->id();
if (!valid_node_ids.count(node->id())) { continue;
LOG(INFO) << "invalid node id " << node->id(); }
continue; itr.second->inlinks.push_back(brief_node_map.at(node->id()));
} }
itr.second->inlinks.push_back(brief_node_map.at(node->id()));
} for (Node *node : itr.second->node->outputs) {
if (!valid_node_ids.count(node->id())) {
for (Node *node : itr.second->node->outputs) { LOG(INFO) << "invalid node id " << node->id();
if (!valid_node_ids.count(node->id())) { continue;
LOG(INFO) << "invalid node id " << node->id(); }
continue; itr.second->outlinks.push_back(brief_node_map.at(node->id()));
} }
itr.second->outlinks.push_back(brief_node_map.at(node->id())); }
}
} for (auto &itr : brief_node_map) {
BriefNode *brief_node = itr.second;
for (auto &itr : brief_node_map) {
BriefNode *brief_node = itr.second; if (!Agent(brief_node->node).marked()) {
VLOG(4) << brief_node->node->id() << " node not a trt candidate.";
if (!Agent(brief_node->node).marked()) { continue;
VLOG(4) << brief_node->node->id() << " node not a trt candidate."; }
continue;
} // Our algorithm must guarantee that:
// 1. The graph is always directed acyclic graph(DAG).
// Our algorithm must guarantee that: // 2. If there is a path in the subgraph from X to Y (X and Y are both
// 1. The graph is always directed acyclic graph(DAG). // nodes in the subgraph), then all paths from X to Y are in the
// 2. If there is a path in the subgraph from X to Y (X and Y are both // subgraph.
// nodes in the subgraph), then all paths from X to Y are in the //
// subgraph. // In order to achieve the above guarantee.
// // For adjacent nodes src -> dst.
// In order to achieve the above guarantee. // 1. Get all dst input nodes except src.
// For adjacent nodes src -> dst. // 2. Reverse DFS from those input nodes
// 1. Get all dst input nodes except src. // 3. If there is a path from input nodes to src,
// 2. Reverse DFS from those input nodes // then the src and dst nodes can not be fused into one node,
// 3. If there is a path from input nodes to src, // otherwise it can be done.
// then the src and dst nodes can not be fused into one node,
// otherwise it can be done. while (true) {
std::unordered_set<BriefNode *> contract_nodes;
while (true) { for (auto *out : brief_node->outlinks) {
std::unordered_set<BriefNode *> contract_nodes; // must be an trt candidate
for (auto *out : brief_node->outlinks) { if (!Agent(out->node).marked()) continue;
// must be an trt candidate // get all dst input nodes except src.
if (!Agent(out->node).marked()) continue; std::vector<BriefNode *> source_nodes;
// get all dst input nodes except src. for (auto *n : out->inlinks) {
std::vector<BriefNode *> source_nodes; if (n != brief_node) {
for (auto *n : out->inlinks) { source_nodes.push_back(n);
if (n != brief_node) { }
source_nodes.push_back(n); }
}
} // Reverse DFS from the source_nodes.
bool have_excess_path = false;
// Reverse DFS from the source_nodes. FlexibleDFS(source_nodes, true, nullptr,
bool have_excess_path = false; [&have_excess_path, brief_node](const BriefNode *n) {
FlexibleDFS(source_nodes, true, nullptr, if (n == brief_node) {
[&have_excess_path, brief_node](const BriefNode *n) { have_excess_path = true;
if (n == brief_node) { return false;
have_excess_path = true; }
return false; return true;
} });
return true; if (have_excess_path) continue;
}); contract_nodes.insert(out);
if (have_excess_path) continue; }
contract_nodes.insert(out); if (contract_nodes.empty()) break;
}
if (contract_nodes.empty()) break; for (auto dst_node : contract_nodes) {
UnionFindCombine(node_map, brief_node->node->id(),
for (auto dst_node : contract_nodes) { dst_node->node->id());
UnionFindCombine(node_map, brief_node->node->id(), UnionContractedNodes(brief_node_map, brief_node->node->id(),
dst_node->node->id()); dst_node->node->id());
UnionContractedNodes(brief_node_map, brief_node->node->id(), }
dst_node->node->id()); }
} }
}
} std::unordered_map<int /*ancestor*/, std::vector<Node *>> clusters;
for (auto *n : marked_nodes) {
std::unordered_map<int /*ancestor*/, std::vector<Node *>> clusters; if (n->IsOp()) {
for (auto *n : marked_nodes) { clusters[UnionFindGetAncestor(node_map, Agent(n).union_find_parent())]
if (n->IsOp()) { .push_back(n);
clusters[UnionFindGetAncestor(node_map, Agent(n).union_find_parent())] }
.push_back(n); }
} std::vector<std::vector<Node *>> result;
} std::for_each(clusters.begin(), clusters.end(),
std::vector<std::vector<Node *>> result; [&](const decltype(clusters)::value_type &it) {
std::for_each(clusters.begin(), clusters.end(), result.push_back(it.second);
[&](const decltype(clusters)::value_type &it) { });
result.push_back(it.second);
}); return result;
}
return result;
} void SubGraphFuser::operator()() { ReplaceNodesWithSubGraphs(); }
void SubGraphFuser::operator()() { ReplaceNodesWithSubGraphs(); } void RemoveIntermediateOutputInSubgraph(const std::vector<Node *> &subgraph,
Graph *graph,
void RemoveIntermediateOutputInSubgraph(const std::vector<Node *> &subgraph, std::vector<Node *> *outputs) {
Graph *graph, std::unordered_set<Node *> subgraph_set(subgraph.begin(), subgraph.end());
std::vector<Node *> *outputs) { std::unordered_set<Node *> valid_output;
std::unordered_set<Node *> subgraph_set(subgraph.begin(), subgraph.end());
std::unordered_set<Node *> valid_output; for (auto *output : *outputs) {
int num_used = 0;
for (auto *output : *outputs) { for (auto *node : output->outputs) {
int num_used = 0; if (!subgraph_set.count(node)) ++num_used;
for (auto *node : output->outputs) { if (num_used > 0) valid_output.insert(output);
if (!subgraph_set.count(node)) ++num_used; }
if (num_used > 0) valid_output.insert(output); }
}
} // In use for ngraph subgraph pass for parallel executor,
// this will remove all nodes, bypass this and let ngraph
// In use for ngraph subgraph pass for parallel executor, // subgraph pass to process outputs
// this will remove all nodes, bypass this and let ngraph if (FLAGS_use_ngraph && valid_output.size() == 0) return;
// subgraph pass to process outputs
if (FLAGS_use_ngraph && valid_output.size() == 0) return; outputs->assign(valid_output.begin(), valid_output.end());
}
outputs->assign(valid_output.begin(), valid_output.end());
} void DetachDeletedNodes(framework::ir::Graph *graph) {
std::unordered_set<const Node *> nodes;
void DetachDeletedNodes(framework::ir::Graph *graph) { for (auto *node : graph->Nodes()) {
std::unordered_set<const Node *> nodes; if (Agent(node).deleted()) {
for (auto *node : graph->Nodes()) { node->inputs.clear();
if (Agent(node).deleted()) { node->outputs.clear();
node->inputs.clear(); }
node->outputs.clear(); }
} }
}
} void SubGraphFuser::ReplaceNodesWithSubGraphs() {
auto subgraphs = SubgraphDetector(graph_, node_inside_subgraph_teller_)();
void SubGraphFuser::ReplaceNodesWithSubGraphs() { for (auto &subgraph : subgraphs) {
auto subgraphs = SubgraphDetector(graph_, node_inside_subgraph_teller_)(); if (subgraph.size() <= (size_t)min_subgraph_size_) continue;
for (auto &subgraph : subgraphs) { std::unordered_set<Node *> subgraph_uniq(subgraph.begin(), subgraph.end());
if (subgraph.size() <= (size_t)min_subgraph_size_) continue; // replace this sub-graph with the first node. Two steps: 1. Create a Block
std::unordered_set<Node *> subgraph_uniq(subgraph.begin(), subgraph.end()); // Node that contains this subgraph 2. Mark the nodes inside the sub-graph
// replace this sub-graph with the first node. Two steps: 1. Create a Block // as deleted. 3. Replace the deleted node with the new Block Node.
// Node that contains this subgraph 2. Mark the nodes inside the sub-graph framework::OpDesc empty_desc;
// as deleted. 3. Replace the deleted node with the new Block Node. empty_desc.SetType(name_);
framework::OpDesc empty_desc; auto *block_node = graph_->CreateOpNode(&empty_desc);
empty_desc.SetType(name_); Agent(block_node).set_subgraph({});
auto *block_node = graph_->CreateOpNode(&empty_desc); auto io = ExtractInputAndOutputOfSubGraph(subgraph);
Agent(block_node).set_subgraph({}); block_node->inputs = std::move(io.first);
auto io = ExtractInputAndOutputOfSubGraph(subgraph); block_node->outputs = std::move(io.second);
block_node->inputs = std::move(io.first);
block_node->outputs = std::move(io.second); RemoveIntermediateOutputInSubgraph(subgraph, graph_, &block_node->outputs);
RemoveIntermediateOutputInSubgraph(subgraph, graph_, &block_node->outputs); for (auto *node : subgraph) {
// TODO(Superjomn) need a unified mechanism to treat deleted node in each
for (auto *node : subgraph) { // pass.
// TODO(Superjomn) need a unified mechanism to treat deleted node in each Agent(node).set_deleted(true);
// pass. Agent(block_node).subgraph()->push_back(node);
Agent(node).set_deleted(true); }
Agent(block_node).subgraph()->push_back(node);
} // Change all the sub-graph's inputs and outputs corresponding inlink and
// outlink to this sub-graph node.
// Change all the sub-graph's inputs and outputs corresponding inlink and auto inlink_or_outlink_cleaner = [&](std::vector<Node *> &nodes) {
// outlink to this sub-graph node. for (auto *&n : nodes) {
auto inlink_or_outlink_cleaner = [&](std::vector<Node *> &nodes) { if (subgraph_uniq.count(n)) {
for (auto *&n : nodes) { n = block_node;
if (subgraph_uniq.count(n)) { }
n = block_node; }
} std::unordered_set<Node *> uniq(nodes.begin(), nodes.end());
} nodes.assign(uniq.begin(), uniq.end());
std::unordered_set<Node *> uniq(nodes.begin(), nodes.end()); };
nodes.assign(uniq.begin(), uniq.end()); for (auto *i : block_node->inputs) {
}; inlink_or_outlink_cleaner(i->outputs);
for (auto *i : block_node->inputs) { }
inlink_or_outlink_cleaner(i->outputs); for (auto *&o : block_node->outputs) {
} inlink_or_outlink_cleaner(o->inputs);
for (auto *&o : block_node->outputs) { }
inlink_or_outlink_cleaner(o->inputs); }
} // DetachDeletedNodes(graph_);
} FilterRedundantOutputOfSubGraph(graph_);
// DetachDeletedNodes(graph_); }
FilterRedundantOutputOfSubGraph(graph_);
} inline bool CheckNodeIndegreeEquals(const Node &node, size_t n) {
return node.inputs.size() == n;
inline bool CheckNodeIndegreeEquals(const Node &node, size_t n) { }
return node.inputs.size() == n;
} } // namespace ir
} // namespace framework
} // namespace analysis } // namespace paddle
} // namespace inference
} // namespace paddle
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. /* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
/* #pragma once
* This file defines the the class to partition a graph.
*/ #include <string>
#include <vector>
#pragma once #include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/graph_traits.h"
#include <string> #include "paddle/fluid/framework/ir/node.h"
#include <vector>
#include "paddle/fluid/framework/ir/graph.h" namespace paddle {
#include "paddle/fluid/framework/ir/graph_traits.h" namespace framework {
#include "paddle/fluid/framework/ir/node.h" namespace ir {
#include "paddle/fluid/inference/analysis/argument.h"
#include "paddle/fluid/inference/analysis/helper.h" const char kIsFunctionNode[] = "__is_function_node__";
const char kFunctionNodeSubGraph[] = "__function_node_sub_graph__";
namespace paddle { const char kSubgraphSplitterMarkerAttrName[] =
namespace inference { "_sub_graph_splitter_inside_sub_graph";
namespace analysis {
/*
using framework::ir::Graph; * Detect the nodes in a sub-graph that meet some conditions. This class doesn't
using framework::ir::NodesTSIterator; * modify the graph.
*/
const char kIsFunctionNode[] = "__is_function_node__"; class SubgraphDetector {
const char kFunctionNodeSubGraph[] = "__function_node_sub_graph__"; public:
const char kSubgraphSplitterMarkerAttrName[] = // Tell whether a node is inside a sub-graph.
"_sub_graph_splitter_inside_sub_graph"; using NodeInsideSubgraphTeller = std::function<bool(const Node *)>;
/* SubgraphDetector(Graph *graph, const NodeInsideSubgraphTeller &teller)
* Detect the nodes in a sub-graph that meet some conditions. This class doesn't : graph_(graph), node_inside_subgraph_teller_(teller) {}
* modify the graph.
*/ std::vector<std::vector<Node *>> operator()();
class SubgraphDetector {
public: protected:
// Tell whether a node is inside a sub-graph. // Mark the nodes inside the accepted sub-graph using
using NodeInsideSubgraphTeller = // node_inside_subgraph_teller.
std::function<bool(const framework::ir::Node *)>; void MarkNodesInsideSubGraph();
SubgraphDetector(Graph *graph, const NodeInsideSubgraphTeller &teller) // Merge the marked nodes into sub-graphs and return the sub-graphs.
: graph_(graph), node_inside_subgraph_teller_(teller) {} std::vector<std::vector<Node *>> ExtractSubGraphs();
std::vector<std::vector<framework::ir::Node *>> operator()(); private:
Graph *graph_;
protected: NodeInsideSubgraphTeller node_inside_subgraph_teller_;
// Mark the nodes inside the accepted sub-graph using };
// node_inside_subgraph_teller.
void MarkNodesInsideSubGraph(); /*
* SubGraphFuser - Replace some nodes with the sub-graph node they are inside.
// Merge the marked nodes into sub-graphs and return the sub-graphs. * To some extent, the TensorRT engine is just a fusion op for a model.
std::vector<std::vector<framework::ir::Node *>> ExtractSubGraphs(); */
class SubGraphFuser {
private: public:
Graph *graph_; using NodeInsideSubgraphTeller = SubgraphDetector::NodeInsideSubgraphTeller;
NodeInsideSubgraphTeller node_inside_subgraph_teller_;
}; SubGraphFuser(Graph *graph, const NodeInsideSubgraphTeller &teller,
int min_subgraph_size, std::string name = "anakin_engine")
/* : graph_(graph),
* SubGraphFuser - Replace some nodes with the sub-graph node they are inside. node_inside_subgraph_teller_(teller),
* To some extent, the TensorRT engine is just a fusion op for a model. min_subgraph_size_{min_subgraph_size},
*/ name_{name} {}
class SubGraphFuser {
public: // The main method which run all the logic.
using NodeInsideSubgraphTeller = SubgraphDetector::NodeInsideSubgraphTeller; void operator()();
SubGraphFuser(Graph *graph, const NodeInsideSubgraphTeller &teller, protected:
int min_subgraph_size, std::string name = "anakin_engine") // Remove the nodes inside sub-graphs and replace with the SubGraphNode.
: graph_(graph), void ReplaceNodesWithSubGraphs();
node_inside_subgraph_teller_(teller),
min_subgraph_size_{min_subgraph_size}, private:
name_{name} {} Graph *graph_;
NodeInsideSubgraphTeller node_inside_subgraph_teller_;
// The main method which run all the logic. int min_subgraph_size_;
void operator()(); const std::string name_;
};
protected:
// Remove the nodes inside sub-graphs and replace with the SubGraphNode. struct NodeWrapper {
void ReplaceNodesWithSubGraphs(); bool deleted{false};
bool marked{false};
private: int union_find_parent{-1};
Graph *graph_; std::vector<Node *> subgraph;
NodeInsideSubgraphTeller node_inside_subgraph_teller_; };
int min_subgraph_size_;
const std::string name_; /*
}; * ir::Node agent for subgraph detector.
*/
struct NodeWrapper { struct Agent {
bool deleted{false}; explicit Agent(Node *x) : x_(x) {}
bool marked{false};
int union_find_parent{-1}; NodeWrapper &wrapper() {
std::vector<framework::ir::Node *> subgraph; if (!x_->IsWrappedBy<NodeWrapper>()) {
}; x_->WrappedBy<NodeWrapper>(new NodeWrapper);
}
/* return x_->template Wrapper<NodeWrapper>();
* ir::Node agent for subgraph detector. }
*/
struct Agent { bool deleted() { return wrapper().deleted; }
explicit Agent(framework::ir::Node *x) : x_(x) {} void set_deleted(bool x) { wrapper().deleted = x; }
NodeWrapper &wrapper() { bool marked() { return wrapper().marked; }
if (!x_->IsWrappedBy<NodeWrapper>()) { void set_marked(bool x) { wrapper().marked = x; }
x_->WrappedBy<NodeWrapper>(new NodeWrapper);
} void set_subgraph(const std::vector<framework::ir::Node *> &x) {
return x_->template Wrapper<NodeWrapper>(); wrapper().subgraph = x;
} }
bool deleted() { return wrapper().deleted; } int union_find_parent() { return wrapper().union_find_parent; }
void set_deleted(bool x) { wrapper().deleted = x; } void set_union_find_parent(int v) { wrapper().union_find_parent = v; }
bool marked() { return wrapper().marked; } std::vector<Node *> *subgraph() { return &wrapper().subgraph; }
void set_marked(bool x) { wrapper().marked = x; } std::vector<Node *> &inputs() { return x_->inputs; }
std::vector<Node *> &outputs() { return x_->outputs; }
void set_subgraph(const std::vector<framework::ir::Node *> &x) {
wrapper().subgraph = x; private:
} Node *x_;
};
int union_find_parent() { return wrapper().union_find_parent; }
void set_union_find_parent(int v) { wrapper().union_find_parent = v; } // The nodes those have no input will be treated as start points.
static std::vector<Node *> ExtractStartPoints(const Graph &g) {
std::vector<framework::ir::Node *> *subgraph() { return &wrapper().subgraph; } std::vector<Node *> result;
std::vector<framework::ir::Node *> &inputs() { return x_->inputs; } for (auto *node : g.Nodes()) {
std::vector<framework::ir::Node *> &outputs() { return x_->outputs; } if (node->inputs.empty()) {
result.push_back(node);
private: }
framework::ir::Node *x_; }
}; return result;
}
// The nodes those have no input will be treated as start points.
static std::vector<framework::ir::Node *> ExtractStartPoints(const Graph &g) { static iterator_range<NodesTSIterator> TopologicalSort(const Graph &g) {
std::vector<framework::ir::Node *> result; auto start_points = ExtractStartPoints(g);
for (auto *node : g.Nodes()) { PADDLE_ENFORCE_GT(
if (node->inputs.empty()) { start_points.size(), 0U,
result.push_back(node); platform::errors::InvalidArgument(
} "Expected the number of graph's start points >= 1. Expected %d.",
} start_points.size()));
return result; NodesTSIterator x(start_points);
} return iterator_range<NodesTSIterator>(NodesTSIterator(start_points),
NodesTSIterator());
static iterator_range<NodesTSIterator> TopologicalSort(const Graph &g) { }
auto start_points = ExtractStartPoints(g);
PADDLE_ENFORCE(!start_points.empty()); } // namespace ir
NodesTSIterator x(start_points); } // namespace framework
return iterator_range<NodesTSIterator>(NodesTSIterator(start_points), } // namespace paddle
NodesTSIterator());
}
} // namespace analysis
} // namespace inference
} // namespace paddle
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
#include "paddle/fluid/framework/ir/graph.h" #include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/scope.h" #include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/inference/analysis/argument.h" #include "paddle/fluid/inference/analysis/argument.h"
#include "paddle/fluid/inference/analysis/ir_passes/subgraph_detector.h"
#include "paddle/fluid/string/pretty_log.h" #include "paddle/fluid/string/pretty_log.h"
namespace paddle { namespace paddle {
......
cc_library(subgraph_detector SRCS subgraph_detector.cc subgraph_util.cc DEPS proto_desc) cc_library(subgraph_util SRCS subgraph_util.cc DEPS subgraph_detector)
if(WITH_TESTING)
add_dependencies(subgraph_detector gtest)
endif()
if (WITH_GPU AND TENSORRT_FOUND) if (WITH_GPU AND TENSORRT_FOUND)
cc_library(tensorrt_subgraph_pass SRCS tensorrt_subgraph_pass.cc DEPS subgraph_detector tensorrt_op_teller) cc_library(tensorrt_subgraph_pass SRCS tensorrt_subgraph_pass.cc DEPS subgraph_util tensorrt_op_teller)
set(analysis_deps ${analysis_deps} set(analysis_deps ${analysis_deps}
subgraph_detector tensorrt_subgraph_pass subgraph_util tensorrt_subgraph_pass
CACHE INTERNAL "") CACHE INTERNAL "")
set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h) set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h)
...@@ -16,10 +13,10 @@ if (WITH_GPU AND TENSORRT_FOUND) ...@@ -16,10 +13,10 @@ if (WITH_GPU AND TENSORRT_FOUND)
endif() endif()
if (ANAKIN_SUBGRAPH) if (ANAKIN_SUBGRAPH)
cc_library(anakin_subgraph_pass SRCS anakin_subgraph_pass.cc DEPS subgraph_detector anakin_op_teller) cc_library(anakin_subgraph_pass SRCS anakin_subgraph_pass.cc DEPS subgraph_util anakin_op_teller)
set(analysis_deps ${analysis_deps} set(analysis_deps ${analysis_deps}
subgraph_detector anakin_subgraph_pass subgraph_util anakin_subgraph_pass
CACHE INTERNAL "") CACHE INTERNAL "")
set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h) set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h)
......
...@@ -22,11 +22,11 @@ ...@@ -22,11 +22,11 @@
#include <vector> #include <vector>
#include "paddle/fluid/framework/ir/graph_pattern_detector.h" #include "paddle/fluid/framework/ir/graph_pattern_detector.h"
#include "paddle/fluid/framework/ir/subgraph_detector.h"
#include "paddle/fluid/inference/anakin/convert/op_converter.h" #include "paddle/fluid/inference/anakin/convert/op_converter.h"
#include "paddle/fluid/inference/anakin/op_teller.h" #include "paddle/fluid/inference/anakin/op_teller.h"
#include "paddle/fluid/inference/analysis/helper.h" #include "paddle/fluid/inference/analysis/helper.h"
#include "paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.h" #include "paddle/fluid/inference/analysis/ir_passes/anakin_subgraph_pass.h"
#include "paddle/fluid/inference/analysis/ir_passes/subgraph_detector.h"
#include "paddle/fluid/string/pretty_log.h" #include "paddle/fluid/string/pretty_log.h"
namespace paddle { namespace paddle {
...@@ -50,7 +50,7 @@ void analysis::AnakinSubgraphPass::ApplyImpl( ...@@ -50,7 +50,7 @@ void analysis::AnakinSubgraphPass::ApplyImpl(
return anakin::OpTeller::Global().Tell(node->Op()->Type(), *node->Op()); return anakin::OpTeller::Global().Tell(node->Op()->Type(), *node->Op());
}; };
SubGraphFuser fuser(graph, teller, 6 /* min_subgraph_size */); framework::ir::SubGraphFuser fuser(graph, teller, 6 /* min_subgraph_size */);
fuser(); fuser();
std::vector<std::string> graph_param_names = std::vector<std::string> graph_param_names =
...@@ -61,17 +61,18 @@ void analysis::AnakinSubgraphPass::ApplyImpl( ...@@ -61,17 +61,18 @@ void analysis::AnakinSubgraphPass::ApplyImpl(
std::vector<std::string> repetitive_params; std::vector<std::string> repetitive_params;
for (auto *node : graph->Nodes()) { for (auto *node : graph->Nodes()) {
if (node->IsOp() && !Agent(node).subgraph()->empty()) { if (node->IsOp() && !framework::ir::Agent(node).subgraph()->empty()) {
CreateAnakinOp(node, graph, graph_param_names, &repetitive_params); CreateAnakinOp(node, graph, graph_param_names, &repetitive_params);
std::unordered_set<const Node *> nodes2remove( std::unordered_set<const Node *> nodes2remove(
Agent(node).subgraph()->begin(), Agent(node).subgraph()->end()); framework::ir::Agent(node).subgraph()->begin(),
framework::ir::Agent(node).subgraph()->end());
framework::ir::GraphSafeRemoveNodes(graph, nodes2remove); framework::ir::GraphSafeRemoveNodes(graph, nodes2remove);
} }
} }
std::unordered_set<const Node *> nodes2remove; std::unordered_set<const Node *> nodes2remove;
for (auto *node : graph->Nodes()) { for (auto *node : graph->Nodes()) {
if (node->IsOp() && Agent(node).deleted()) { if (node->IsOp() && framework::ir::Agent(node).deleted()) {
nodes2remove.insert(node); nodes2remove.insert(node);
} }
} }
...@@ -96,11 +97,11 @@ std::string GenerateAnakinEngineKey(const std::set<std::string> &engine_inputs, ...@@ -96,11 +97,11 @@ std::string GenerateAnakinEngineKey(const std::set<std::string> &engine_inputs,
} }
void AnakinSubgraphPass::CreateAnakinOp( void AnakinSubgraphPass::CreateAnakinOp(
framework::ir::Node *node, Graph *graph, framework::ir::Node *node, framework::ir::Graph *graph,
const std::vector<std::string> &graph_params, const std::vector<std::string> &graph_params,
std::vector<std::string> *repetitive_params) const { std::vector<std::string> *repetitive_params) const {
auto *op_desc = node->Op(); auto *op_desc = node->Op();
auto &subgraph = *Agent(node).subgraph(); auto &subgraph = *framework::ir::Agent(node).subgraph();
PADDLE_ENFORCE(!subgraph.empty()); PADDLE_ENFORCE(!subgraph.empty());
framework::ProgramDesc *program_desc = framework::ProgramDesc *program_desc =
...@@ -164,7 +165,7 @@ void AnakinSubgraphPass::CreateAnakinOp( ...@@ -164,7 +165,7 @@ void AnakinSubgraphPass::CreateAnakinOp(
graph_var_map[node->Name()] = node; graph_var_map[node->Name()] = node;
} }
} }
auto &subgraph_nodes = *Agent(node).subgraph(); auto &subgraph_nodes = *framework::ir::Agent(node).subgraph();
// The following procedure is used to rename all the intermediate // The following procedure is used to rename all the intermediate
// variables and the output variables of the subgraph. // variables and the output variables of the subgraph.
......
...@@ -17,8 +17,8 @@ ...@@ -17,8 +17,8 @@
#include <set> #include <set>
#include "paddle/fluid/framework/ir/graph_pattern_detector.h" #include "paddle/fluid/framework/ir/graph_pattern_detector.h"
#include "paddle/fluid/framework/ir/subgraph_detector.h"
#include "paddle/fluid/inference/analysis/helper.h" #include "paddle/fluid/inference/analysis/helper.h"
#include "paddle/fluid/inference/analysis/ir_passes/subgraph_detector.h"
#include "paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.h" #include "paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.h"
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h" #include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
#include "paddle/fluid/inference/tensorrt/engine.h" #include "paddle/fluid/inference/tensorrt/engine.h"
...@@ -40,9 +40,9 @@ void analysis::TensorRtSubgraphPass::ApplyImpl( ...@@ -40,9 +40,9 @@ void analysis::TensorRtSubgraphPass::ApplyImpl(
return tensorrt::OpTeller::Global().Tell(node->Op()->Type(), *node->Op()); return tensorrt::OpTeller::Global().Tell(node->Op()->Type(), *node->Op());
}; };
SubGraphFuser fuser(graph, teller, framework::ir::SubGraphFuser fuser(
Get<int>("min_subgraph_size") /*min subgraph size*/, graph, teller, Get<int>("min_subgraph_size") /*min subgraph size*/,
"tensorrt_engine"); "tensorrt_engine");
fuser(); fuser();
std::vector<std::string> graph_param_names = std::vector<std::string> graph_param_names =
...@@ -52,18 +52,19 @@ void analysis::TensorRtSubgraphPass::ApplyImpl( ...@@ -52,18 +52,19 @@ void analysis::TensorRtSubgraphPass::ApplyImpl(
std::vector<std::string> repetitive_params; std::vector<std::string> repetitive_params;
for (auto *node : graph->Nodes()) { for (auto *node : graph->Nodes()) {
if (node->IsOp() && !Agent(node).subgraph()->empty()) { if (node->IsOp() && !framework::ir::Agent(node).subgraph()->empty()) {
CreateTensorRTOp(node, graph, graph_param_names, &repetitive_params); CreateTensorRTOp(node, graph, graph_param_names, &repetitive_params);
std::unordered_set<const Node *> nodes2remove( std::unordered_set<const Node *> nodes2remove(
Agent(node).subgraph()->begin(), Agent(node).subgraph()->end()); framework::ir::Agent(node).subgraph()->begin(),
framework::ir::Agent(node).subgraph()->end());
framework::ir::GraphSafeRemoveNodes(graph, nodes2remove); framework::ir::GraphSafeRemoveNodes(graph, nodes2remove);
} }
} }
std::unordered_set<const Node *> nodes2remove; std::unordered_set<const Node *> nodes2remove;
for (auto *node : graph->Nodes()) { for (auto *node : graph->Nodes()) {
if (node->IsOp() && Agent(node).deleted()) { if (node->IsOp() && framework::ir::Agent(node).deleted()) {
nodes2remove.insert(node); nodes2remove.insert(node);
} }
} }
...@@ -88,11 +89,11 @@ std::string GenerateEngineKey(const std::set<std::string> &engine_inputs, ...@@ -88,11 +89,11 @@ std::string GenerateEngineKey(const std::set<std::string> &engine_inputs,
} }
void TensorRtSubgraphPass::CreateTensorRTOp( void TensorRtSubgraphPass::CreateTensorRTOp(
framework::ir::Node *node, Graph *graph, framework::ir::Node *node, framework::ir::Graph *graph,
const std::vector<std::string> &graph_params, const std::vector<std::string> &graph_params,
std::vector<std::string> *repetitive_params) const { std::vector<std::string> *repetitive_params) const {
auto *op_desc = node->Op(); auto *op_desc = node->Op();
auto &subgraph = *Agent(node).subgraph(); auto &subgraph = *framework::ir::Agent(node).subgraph();
PADDLE_ENFORCE(!subgraph.empty()); PADDLE_ENFORCE(!subgraph.empty());
framework::ProgramDesc *program_desc = framework::ProgramDesc *program_desc =
...@@ -161,7 +162,7 @@ void TensorRtSubgraphPass::CreateTensorRTOp( ...@@ -161,7 +162,7 @@ void TensorRtSubgraphPass::CreateTensorRTOp(
if (precision_mode == AnalysisConfig::Precision::kHalf) enable_fp16 = true; if (precision_mode == AnalysisConfig::Precision::kHalf) enable_fp16 = true;
auto enable_int8 = Get<bool>("enable_int8"); auto enable_int8 = Get<bool>("enable_int8");
auto use_calib_mode = Get<bool>("use_calib_mode"); auto use_calib_mode = Get<bool>("use_calib_mode");
auto &subgraph_nodes = *Agent(node).subgraph(); auto &subgraph_nodes = *framework::ir::Agent(node).subgraph();
// The following procedure is used to rename all the intermediate // The following procedure is used to rename all the intermediate
// variables and the output variables of the subgraph. // variables and the output variables of the subgraph.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册