matcher¶
Subgraph Isomorphic Matcher¶
With uTensorGraphMatcher, performing isomorphic subgraph matching
along with replacing or manipulating the matched subgraph(s) takes just a
few line of code:
from utensor_cgen.matcher import uTensorGraphMatcher
# `pattrn_ugraph` is the pattern to match with
pattrn_ugraph = ...
matcher = uTensorGraphMatcher(pattrn_ugraph)
# a larget graph to perform subgraph match
subject_ugraph = ...
# matches is a list of `uTensorGraphMatch` objects
matches = matcher.match_all(subject_ugraph)
if matches:
# do stuff with the matches
Use Case: Node Fusion¶
Note: we’ll use operation/node/layer interchangeably in the documentation
It’s commonly seen pattern in convolution neural network (
CNN),conv -> relu -> pooling. That is, a 2D convolution followed by a relu layer and then a pooling down sampling layer.With our
uTensorGraphMatcher, you can locate such pattern in yourCNNmodel and fuse/replace matched nodes into one optimizedQuantizedFusedConv2DMaxpoolnode.
Left: original graph
Middle: matched convolution layer
Right: replace the matched layer with specialized
QuantizedFusedConv2DMaxpoolnode
Use Case: Dropout Layer Removal¶
Though
dropoutis an effective technique to improve training performance of your model, it’s not necessary during inference phrase.In the mainstream frameworks such as Tensorflow or `PyTorch`_, an
dropoutlayer is typically implemented with other elementary operations/nodes. As a result, finding and removing those nodes for inference optimization (both in model size and prediciton time) is not trivial and error prone.With our
uTensorGraphMatcher, you can find and remove the dropout nodes as illustrated in the following picture.Left: original graph with dropout Layers
Middle: matched dropout layers
Right: graph with dropout layers removed
We use mainly Tensorflow for declaring the pattern graph for matcher now.
High-level graph builder is on its way, see Future Works for detail.
Module members¶
-
class
utensor_cgen.matcher.uTensorGraphMatch¶ A isomorphic subgraph match
See also
uTensorGraphMatcher- Parameters
pattern_ugraph (
uTensorGraph) – the parttern graphsubject_ugraph (
uTensorGraph) – the subjective graphpatrn2subj_op_map (dict) – a dict with key as op name in the
pattern_ugraphand value as the matched op in thesubject_ugraphsubj2patrn_op_map (dict) – a dict with key as op name in the
subject_ugraphand value as the matched op in thepattern_ugraphpatrn2subj_tensor_map (dict) – a dict with key as the tensor object in the
pattern_ugraphand value as the tensor object in thesubject_ugraphsubj2patrn_tensor_map (dict) – a dict with key as the tensor object in the
subject_ugraphand value as the tensor object in thepattern_ugraph
-
replace_with(self, callback, suffix=None)¶ Replace matched subgraph with a given ugraph given by the callback, not in-place
- Parameters
callback (callable) – a callable object which takes a
uTensorGraphMatchand reutrn three values – auTensorGraphobject to replaced the matched subgraph with (thereplacing graph), aninput_map(dict) maps input tensors in pattern graph to the input tensors in replacing graph and anoutput_map(dict) which maps the output tensorssuffix (str) – (optional) the suffix to add to the name of ops/tensors in the replacing graph returned by
callback. If not given, it will be a random string
- Return type
uTensorGraph, a new graph with matched subgraph replaced
-
class
utensor_cgen.matcher.uTensorGraphMatcher¶ Isomorphic Subgraph Matcher
Perform isomorphic subgraph match against given graph
A minimal example
# Example: match and replace patrn_ugraph = ... # load the pattern uTensorGraph # create a matcher matcher = uTensorGraphMatcher( pattern_ugraph=patrn_ugraph, op_equality_delegate=delegate, # a subclass of :py:class:`OpEqualityDelegateBase` ) # define a callback def callback(match): # inspect the match object .... # return the replacing graph, input and output map return repl_ugraph, input_map, output_map # load the subject uTensorGraph subj_ugraph = ... # search for 1 match matches = matcher.match(subj_ugraph, n=1) # return a list of uTensorGraphMatch objects if matches: match = matches[0] match.replace_with(callback)
See also
uTensorGraphMatch- Parameters
pattern_ugraph (
uTensorGraph) – a graph serve as pattern to look for
-
match(self, other_ugraph, n=1)¶ Match the pattern against the graph
- Parameters
other_ugraph (
uTensorGraph) – the graph where to search the patternn (int) – the maximum matches to return
- Return type
List[
uTensorGraphMatch]

