Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extractor dfs performance #1655

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions src/deepsparse/utils/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"""

import os
from typing import Any, List, Optional, Sequence, Tuple
from typing import Any, List, Optional, Sequence, Set, Tuple

import onnx.helper
import onnx.shape_inference
Expand Down Expand Up @@ -84,33 +84,56 @@ def _collect_new_outputs(self, names: List[str]) -> List[ValueInfoProto]:
def _dfs_search_reachable_nodes(
self,
node_output_name: str,
graph_input_names: List[str],
reachable_nodes: List[NodeProto],
graph_input_names: Set[str],
nodes: List[NodeProto],
reachable: Set[int],
unreachable: Set[int],
) -> None:
"""
Helper function to find nodes which are connected to an output

:param node_output_name: The name of the output
:param graph_input_names: The names of all inputs of the graph
:param nodes: The list of all nodes of the graph
:param reachable: The set of indexes to reachable nodes in `nodes`
:param unreachable: The set of indexes to unreachable nodes in `nodes`
"""
# finish search at inputs
if node_output_name in graph_input_names:
return
for node in self.graph.node:
# check output_name first to reduce run time
if node_output_name not in node.output:
continue
if node in reachable_nodes:
continue
reachable_nodes.append(node)
for name in node.input:

# find nodes connected to this output
nodes_to_search = [
index for index in unreachable if node_output_name in nodes[index].output
]

# add nodes connected to this output to sets
for node_index in nodes_to_search:
reachable.add(node_index)
unreachable.remove(node_index)

# recurse on inputs
for node_index in nodes_to_search:
for name in nodes[node_index].input:
self._dfs_search_reachable_nodes(
name, graph_input_names, reachable_nodes
name, graph_input_names, nodes, reachable, unreachable
)

def _collect_reachable_nodes(
self,
input_names: List[str],
output_names: List[str],
) -> List[NodeProto]:
reachable_nodes = list() # type: ignore
) -> list[NodeProto]:
_input_names = set(input_names)
nodes = list(self.graph.node)
reachable: Set[int] = set()
unreachable: Set[int] = set(range(len(nodes)))
for name in output_names:
self._dfs_search_reachable_nodes(name, input_names, reachable_nodes)
# needs to be topology sorted.
nodes = [n for n in self.graph.node if n in reachable_nodes]
self._dfs_search_reachable_nodes(
name, _input_names, nodes, reachable, unreachable
)
# needs to be topologically sorted
nodes = [nodes[node_index] for node_index in sorted(reachable)]
return nodes

def _collect_referred_local_functions(
Expand Down