Deeploy.CommonExtensions.OptimizationPasses.Matchers.NonBranchingMatcher
- class Deeploy.CommonExtensions.OptimizationPasses.Matchers.NonBranchingMatcher(regex_op: bool = False)
Bases:
SubgraphMatcherPattern matcher for sequential computational graphs without branching.
This matcher is optimized for patterns that form a simple chain of operations without splits or merges in the computational flow. It uses a recursive algorithm to follow the linear path of operations.
The matching algorithm follows edges from the anchor node to build a complete mapping between pattern nodes and graph nodes, verifying operation types and attributes at each step. .. rubric:: Notes
This matcher is efficient for linear operation sequences such as: - Conv -> BatchNorm -> ReLU chains - Linear -> Dropout -> Activation sequences - Simple preprocessing pipelines
The algorithm assumes that each node in the pattern has at most one output connection to the next node in the sequence.
Methods
- __init__(regex_op: bool = False)
Initialize the non-branching pattern matcher.
- Parameters:
regex_op (bool, optional) – Enable regex-based operation type matching. Default is False.
__init__([regex_op])Initialize the non-branching pattern matcher.
is_op_match(patternNode, graphNode)Check if a pattern node operation matches a graph node operation.
match(graph, pattern)Find all non-overlapping matches of a pattern in a target graph.
- is_op_match(patternNode: Node, graphNode: Node)
Check if a pattern node operation matches a graph node operation.
Compares the operation types of two nodes according to the configured matching policy (regex or exact match).
- Parameters:
patternNode (gs.Node) – The pattern node whose operation type serves as the match criterion.
graphNode (gs.Node) – The target graph node to check for a match.
- Returns:
True if the operations match according to the configured policy, False otherwise.
- Return type:
bool
Notes
When regex_op is True, the pattern node’s operation is treated as a regular expression pattern and matched against the graph node’s operation using re.fullmatch. When False, exact string equality is used.
- match(graph: Graph, pattern: Graph)
Find all non-overlapping matches of a pattern in a target graph.
Systematically searches the target graph for instances of the pattern, ensuring that each node participates in at most one match to avoid conflicts during transformations.
- Parameters:
graph (gs.Graph) – The target graph to search for pattern matches.
pattern (gs.Graph) – The pattern graph to find instances of.
- Returns:
A list of Match objects representing all non-overlapping instances of the pattern found in the target graph.
- Return type:
List[Match]
Notes
The algorithm: 1. Validates the pattern using the subclass-specific validation 2. Iterates through all nodes in the target graph as potential anchors 3. Attempts to match the pattern from each anchor 4. Collects only non-overlapping matches to avoid conflicts
Non-overlapping means that if a node is part of one match, it cannot be part of any other match in the returned list.