opset_matching is used for operator set searching, it returns relevant op set instead of specific paths, should be used when results of path_matching overflow. opset_matching 用于图检索,匹配算子集合(无序) 一个最简单的例子: sp_expr = lamdba x: x.type == 'Conv'
(
self,
sp_expr: Callable,
rp_expr: Callable,
ep_expr: Callable,
direction: str = 'up'
)
| 562 | return _ret_collection |
| 563 | |
| 564 | def opset_matching( |
| 565 | self, |
| 566 | sp_expr: Callable, |
| 567 | rp_expr: Callable, |
| 568 | ep_expr: Callable, |
| 569 | direction: str = 'up' |
| 570 | ) -> OperationSet: |
| 571 | """opset_matching is used for operator set searching, it returns |
| 572 | relevant op set instead of specific paths, should be used when results |
| 573 | of path_matching overflow. |
| 574 | |
| 575 | opset_matching 用于图检索,匹配算子集合(无序) |
| 576 | 一个最简单的例子: |
| 577 | sp_expr = lamdba x: x.type == 'Conv' |
| 578 | rp_expr = lamdba x, y: y.type == 'Relu' |
| 579 | ep_expr = lamdba x: x.type == 'Conv' |
| 580 | limitation = None |
| 581 | direction = 'down' |
| 582 | 该指令检索出从任意 Conv 出发,经过任意多个Relu,到任意 Conv 的所有相关算子 |
| 583 | 注意返回结果是无序的,相比于 path matching,opset matching 的性能更高。 |
| 584 | |
| 585 | 你需要注意到 rp_expr 是一个二元表达式,其中 x 代表起点,y 代表终点 |
| 586 | |
| 587 | 在极端情况下,path matching 的结果是无法返回的(由于其结果数可能过多) |
| 588 | 此时应当使用 opset matching |
| 589 | Args: |
| 590 | sp_expr (Union[Pattern, Callable]): |
| 591 | start point expression, 用于匹配检索起点的表达式 |
| 592 | rp_expr (Union[Pattern, Callable]): |
| 593 | relay point expression, 用于匹配检索中继点的表达式 |
| 594 | ep_expr (Union[Pattern, Callable]): |
| 595 | end point expression, 用于匹配检索终点的表达式 |
| 596 | limitation (Union[Limitation, Callable]): |
| 597 | 用于过滤路径的表达式 |
| 598 | direction (str, optional): |
| 599 | 图检索方向,up, down. Defaults to 'down'. |
| 600 | greedy (bool, optional): |
| 601 | 是否执行贪心匹配,设置为True则尝试匹配直到最后一个end point. |
| 602 | """ |
| 603 | ret_collection, candidates = OperationSet(), OperationSet() |
| 604 | for operation in self.graph.operations.values(): |
| 605 | # filter out operation which can not be a start point. |
| 606 | if sp_expr(operation): candidates.add(operation) |
| 607 | |
| 608 | for operation in candidates: |
| 609 | # match patterns, add patterns towards result collection. |
| 610 | partial_matchings = self._opset_matching( |
| 611 | start_point=operation, rp_expr=rp_expr, |
| 612 | ep_expr=ep_expr, direction=direction) |
| 613 | |
| 614 | if len(partial_matchings) > 0: |
| 615 | ret_collection.update(partial_matchings) |
| 616 | |
| 617 | # clear cache |
| 618 | self._cache.clear() |
| 619 | return ret_collection |
| 620 | |
| 621 | def activation_matching( |
no test coverage detected