MCPcopy Create free account
hub / github.com/pytorch/executorch / _match_pattern

Function _match_pattern

backends/arm/quantizer/quantization_annotator.py:400–441  ·  view source on GitHub ↗

Check whether a node chain matches a pattern. Verify a chain of ancestors -> node -> descendants matches the provided ``pattern``. If ``filter_fn`` is provided, require all nodes in the chain to pass the filter. Each pattern element is an iterable of disjunctive node targets.

(
    node: Node,
    pattern: Sequence[Iterable[object]],
    filter_fn: Optional[Callable[[Node], bool]] = None,
)

Source from the content-addressed store, hash-verified

398
399
400def _match_pattern(
401 node: Node,
402 pattern: Sequence[Iterable[object]],
403 filter_fn: Optional[Callable[[Node], bool]] = None,
404) -> bool:
405 """Check whether a node chain matches a pattern.
406
407 Verify a chain of ancestors -> node -> descendants matches the provided
408 ``pattern``. If ``filter_fn`` is provided, require all nodes in the chain
409 to pass the filter. Each pattern element is an iterable of disjunctive
410 node targets.
411
412 """
413 if len(pattern) < 1:
414 raise ValueError("No pattern provided")
415
416 if filter_fn is not None:
417 if not filter_fn(node):
418 return False
419 if len(pattern) == 1:
420 # Base case where it has passed the filter_fn. Simply look if node.target is in pattern.
421 return node.target in pattern[0]
422 if node.target not in [op for sub_pattern in pattern for op in sub_pattern]:
423 # node.target not in pattern. No need to look at the rest of the pattern.
424 return False
425 # Find the index of this node's target in pattern
426 idx = [node.target in sub_pattern for sub_pattern in pattern].index(True)
427 left_pattern = pattern[:idx]
428 # Exclude idx as this contains node.target which we have already matched
429 right_pattern = pattern[idx + 1 :]
430 left_condition = True
431 right_condition = True
432 # Recursively look at the rest of the pattern by calling this function for
433 # node's input and user node with updated patterns.
434 if len(left_pattern) > 0:
435 parent = node.all_input_nodes[0]
436 if len(parent.users) != 1:
437 return False
438 left_condition = _match_pattern(parent, left_pattern, filter_fn)
439 if len(right_pattern) > 0:
440 right_condition = _match_pattern(list(node.users)[0], right_pattern, filter_fn)
441 return left_condition and right_condition
442
443
444_conv_ops = {

Callers 1

get_quant_propertiesFunction · 0.85

Calls 2

filter_fnFunction · 0.50
indexMethod · 0.45

Tested by

no test coverage detected