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

Method path_matching

ppq/IR/search.py:506–562  ·  view source on GitHub ↗

path_matching is used for path searching on the graph, and complete paths will be returned. Note that it's possible for results to overflow when there are numerous matchings. path_matching 用于图检索,匹配完整路径 一个最简单的例子: sp_expr = lamdba x: x.type == 'Conv

(
        self,
        sp_expr: Callable,
        rp_expr: Callable,
        ep_expr: Callable,
        direction: str
        )

Source from the content-addressed store, hash-verified

504 return ret_collection
505
506 def path_matching(
507 self,
508 sp_expr: Callable,
509 rp_expr: Callable,
510 ep_expr: Callable,
511 direction: str
512 ) -> List[Path]:
513 """path_matching is used for path searching on the graph, and complete
514 paths will be returned. Note that it's possible for results to overflow
515 when there are numerous matchings.
516
517 path_matching 用于图检索,匹配完整路径
518 一个最简单的例子:
519 sp_expr = lamdba x: x.type == 'Conv'
520 rp_expr = lamdba x, y: y.type == 'Relu'
521 ep_expr = lamdba x: x.type == 'Conv'
522 limitation = None
523 direction = 'down'
524 该指令检索出从任意 Conv 出发,到任意 Conv 的所有可行路径
525 其中路径上含有任意多个 Relu 节点,并且只能包含 Relu
526
527 你需要注意到 rp_expr 是一个二元表达式,其中 x 代表起点,y 代表终点
528 Args:
529 sp_expr (Union[Pattern, Callable]):
530 start point expression, 用于匹配检索起点的表达式
531 rp_expr (Union[Pattern, Callable]):
532 relay point expression, 用于匹配检索中继点的表达式
533 ep_expr (Union[Pattern, Callable]):
534 end point expression, 用于匹配检索终点的表达式
535 limitation (Union[Limitation, Callable]):
536 用于过滤路径的表达式
537 direction (str, optional):
538 图检索方向,search direction: up, down. Defaults to 'down'.
539 greedy (bool, optional):
540 是否执行贪心匹配,设置为True则尝试匹配直到最后一个end point.
541 whether to search greedily
542 """
543 if direction not in {'up', 'down'}:
544 raise KeyError("traversal_direction must be one of {'up', 'down'}")
545
546 _ret_collection = []
547 for operation in self.graph.operations.values():
548
549 # filter out operation which can not be a start point.
550 if not sp_expr(operation): continue
551
552 # match patterns, add patterns towards result collection.
553 matchings = self._path_matching(
554 start_point=operation, rp_expr=rp_expr,
555 ep_expr=ep_expr, direction=direction)
556 _ret_collection.extend(matchings)
557
558 # clear cache
559 self._cache.clear()
560
561 # use limitation to filter out invalid path.
562 return _ret_collection
563

Callers 5

fuse_bnMethod · 0.95
processMethod · 0.95
activation_matchingMethod · 0.95
concat_matchingMethod · 0.95
test_graph_api.pyFile · 0.80

Calls 2

_path_matchingMethod · 0.95
clearMethod · 0.45

Tested by

no test coverage detected