From 926f5a4687684cebba5645e61858365d90e11eff Mon Sep 17 00:00:00 2001 From: Serge Panev Date: Thu, 25 Jun 2020 11:04:48 -0700 Subject: [PATCH 1/2] Remove check for subgraph with cycles Signed-off-by: Serge Panev --- src/operator/subgraph/build_subgraph.cc | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/operator/subgraph/build_subgraph.cc b/src/operator/subgraph/build_subgraph.cc index 413395c3b74f..53c4d73a3537 100644 --- a/src/operator/subgraph/build_subgraph.cc +++ b/src/operator/subgraph/build_subgraph.cc @@ -226,9 +226,7 @@ bool LabelSubgraph(const nnvm::Graph& g, SubgraphSelectorV2Ptr subgraph_selector std::stack s; s.push(descendant); size_t count = 0; - while (!s.empty()) { - CHECK_LT(count, indexed_graph.num_nodes()) << "Finding ancestor failed. There is probably" - " a loop in the graph"; + while (!s.empty() && count < indexed_graph.num_nodes()) { ++count; const nnvm::Node* top = s.top(); s.pop(); @@ -276,10 +274,6 @@ bool LabelSubgraph(const nnvm::Graph& g, SubgraphSelectorV2Ptr subgraph_selector if (excluded_node_id != -1) { CHECK_LT(excluded_node_id, static_cast(simple_nodes.size())); - CHECK_NE(excluded_node_id, static_cast(snid)) - << "A cycle is found in the computational graph between nodes " - << simple_nodes[excluded_node_id]->node->attrs.name << " and " - << simple_nodes[snid]->node->attrs.name; excluded_nodes->insert(simple_nodes[excluded_node_id].get()); ResetNodeLabels(g, simple_nodes, subgraph_nodes); return false; @@ -306,6 +300,7 @@ void PreSelectSubgraphNodes(const nnvm::Graph& g, SubgraphSelectorV2Ptr subgraph const std::vector& simple_nodes, std::vector* subgraph_nodes) { std::unordered_set excluded_nodes; + size_t n_excluded_nodes = 0; const size_t max_num_retry = simple_nodes.size() * simple_nodes.size(); size_t count = 0; bool success = false; @@ -313,7 +308,11 @@ void PreSelectSubgraphNodes(const nnvm::Graph& g, SubgraphSelectorV2Ptr subgraph success = LabelSubgraph(g, subgraph_selector, label, snid, simple_nodes, subgraph_nodes, &excluded_nodes); if (!success) { - CHECK(!excluded_nodes.empty()); + if (excluded_nodes.size() == n_excluded_nodes) { + // no possible subgraph for the current node snid + break; + } + n_excluded_nodes = excluded_nodes.size(); std::string excluded_node_names; for (auto node : excluded_nodes) { excluded_node_names += node->node->attrs.name + ", "; From 262ea86571933dd16b5f345553cc2af25f184d9d Mon Sep 17 00:00:00 2001 From: Serge Panev Date: Mon, 3 Aug 2020 15:48:06 -0700 Subject: [PATCH 2/2] Add comments Signed-off-by: Serge Panev --- src/operator/subgraph/build_subgraph.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/operator/subgraph/build_subgraph.cc b/src/operator/subgraph/build_subgraph.cc index 53c4d73a3537..93cb17464f1b 100644 --- a/src/operator/subgraph/build_subgraph.cc +++ b/src/operator/subgraph/build_subgraph.cc @@ -308,8 +308,11 @@ void PreSelectSubgraphNodes(const nnvm::Graph& g, SubgraphSelectorV2Ptr subgraph success = LabelSubgraph(g, subgraph_selector, label, snid, simple_nodes, subgraph_nodes, &excluded_nodes); if (!success) { + // Failed to label subgraph due to a cycle + // If the number of excluded_nodes didn't change since the last iteration, + // this means that there is no possible subgraph for the current node snid, we break + // Otherwise, we keep trying (with the excluded nodes tagged) if (excluded_nodes.size() == n_excluded_nodes) { - // no possible subgraph for the current node snid break; } n_excluded_nodes = excluded_nodes.size();