MCPcopy
hub / github.com/OpenPPL/ppq / pattern_matching

Method pattern_matching

ppq/IR/search.py:655–690  ·  view source on GitHub ↗

暴力子图模式匹配 这是 PPQ 0.6.6 更新的内容 在 0.6.6 之前,我们使用具有不确定性的贪心匹配算法,但是考虑到实际应用中的问题 在 0.6.6 版本之后,我们将其修改为枚举匹配。 子图匹配问题是一个 NP-Hard 的问题,不存在多项式时间复杂度的解法。 你需要给出一个模式子图,match_burte_force 方法将在 graph 中对模式子图进行匹配。 PPQ 使用了非递归的算法完成上述匹配,其最坏时间和空间复杂度大概都是 O(NM^k) 其中 N 是母图节点个数,M 是子图

(self, patterns: List[Callable], 
                         edges: List[List[int]], 
                         exclusive: bool = True)

Source from the content-addressed store, hash-verified

653 return dict(concat_matchings)
654
655 def pattern_matching(self, patterns: List[Callable],
656 edges: List[List[int]],
657 exclusive: bool = True) -> List[List[Operation]]:
658 """暴力子图模式匹配 这是 PPQ 0.6.6 更新的内容
659 在 0.6.6 之前,我们使用具有不确定性的贪心匹配算法,但是考虑到实际应用中的问题
660 在 0.6.6 版本之后,我们将其修改为枚举匹配。
661
662 子图匹配问题是一个 NP-Hard 的问题,不存在多项式时间复杂度的解法。
663 你需要给出一个模式子图,match_burte_force 方法将在 graph 中对模式子图进行匹配。
664
665 PPQ 使用了非递归的算法完成上述匹配,其最坏时间和空间复杂度大概都是 O(NM^k)
666 其中 N 是母图节点个数,M 是子图节点个数,k 是母图的最大出度
667
668 对于存在二义性子图模式,匹配复杂度将指数级增长;为了限制算法执行时间,当匹配到多于
669 max_candidates 个模式子图时,算法强制停机,并报错返回。
670
671 实际使用中的时间复杂度更加接近于 O(NM)
672
673 参数 exclusive 指定了是否需要进行精确匹配。在精确匹配模式下:
674 1. 不允许模式子图中除根节点外的其他节点有来自模式子图以外节点的输入
675 2. 不允许模式子图中除叶节点外的其他节点有朝向模式子图以外节点的输出
676
677 Example:
678 pt = PatternTree(
679 patterns = [lambda x: x.is_computing_op, 'Softplus', 'Tanh', 'Mul']
680 edges = [[0, 1], [1, 2], [2, 3], [0, 3]])
681
682 pt create an abstract tree pattern of:
683 --- 'Softplus' --- 'Tanh' --
684 lambda x: x.is_computing_op --- + + --- 'Mul'
685 --- --- --- --- --
686
687 """
688 return PatternMatchHelper().match_burte_force(
689 graph=self.graph, pattern=GraphPattern(node_patterns=patterns, edges=edges),
690 exclusive=exclusive)

Callers 9

optimizeMethod · 0.95
optimizeMethod · 0.95
optimizeMethod · 0.95
optimizeMethod · 0.95
fuse_gemmMethod · 0.95
fuse_layernormMethod · 0.95
fuse_skiplayernormMethod · 0.95
fuse_geluMethod · 0.95
fuse_selfattentionMethod · 0.95

Calls 3

PatternMatchHelperClass · 0.85
GraphPatternClass · 0.85
match_burte_forceMethod · 0.80

Tested by

no test coverage detected