这个函数对计算图进行预处理工作,其主要内容是将计算图的格式进行统一 这个函数将会统一 cast, slice, parameter, constant 算子的格式,并且执行有关 batchnorm 的合并工作. 在 PPQ 中,我们不希望出现 Constant 算子,所有 Constant 输入将被当作 parameter variable 连接到下游算子上 在 PPQ 中,我们不希望出现 Batchnorm 算子,所有 Batchnorm 将被合并 在 PPQ 中,我们不希望出现权重共享的算子,所有被共享的权重将被复制分裂成多份 在 PPQ 中,我们
(graph: BaseGraph)
| 591 | |
| 592 | |
| 593 | def format_graph(graph: BaseGraph) -> BaseGraph: |
| 594 | """这个函数对计算图进行预处理工作,其主要内容是将计算图的格式进行统一 这个函数将会统一 cast, slice, parameter, |
| 595 | constant 算子的格式,并且执行有关 batchnorm 的合并工作. |
| 596 | |
| 597 | 在 PPQ 中,我们不希望出现 Constant 算子,所有 Constant 输入将被当作 parameter variable 连接到下游算子上 |
| 598 | 在 PPQ 中,我们不希望出现 Batchnorm 算子,所有 Batchnorm 将被合并 |
| 599 | 在 PPQ 中,我们不希望出现权重共享的算子,所有被共享的权重将被复制分裂成多份 |
| 600 | 在 PPQ 中,我们不希望出现孤立算子,所有孤立算子将被移除 |
| 601 | |
| 602 | This function takes pre-processing procedure with your graph. |
| 603 | This function will convert operations like cast, slice, parameter, constant to the format that supported by ppq. |
| 604 | This function will merge batchnorm when possible. |
| 605 | |
| 606 | During quantization logic, we do not expect there is any constant operation in your network, so |
| 607 | all of them will be converted as parameter input variable. |
| 608 | |
| 609 | We do not expect there is any shared parameter in your network, all of them will be copied and spilted. |
| 610 | We do not expect any isolated operation in your network, all of them will be removed. |
| 611 | """ |
| 612 | |
| 613 | # do graph level optimization |
| 614 | formatter = GraphReplacer(GraphFormatter(GraphMerger(graph))) |
| 615 | if FORMATTER_FORMAT_CONSTANT_INPUT: |
| 616 | formatter(GraphCommand(GraphCommandType.FORMAT_CONSTANT_INPUT)) |
| 617 | formatter(GraphCommand(GraphCommandType.CONVERT_TO_TENSOR)) |
| 618 | formatter(GraphCommand(GraphCommandType.FORMAT_PARAMETERS)) |
| 619 | |
| 620 | if FORMATTER_FUSE_BIAS_ADD: |
| 621 | formatter(GraphCommand(GraphCommandType.FUSE_BIAS_ADD)) |
| 622 | |
| 623 | if FORMATTER_FUSE_BN: |
| 624 | formatter(GraphCommand(GraphCommandType.FUSE_BN)) |
| 625 | |
| 626 | if FORMATTER_REPLACE_BN_TO_CONV: |
| 627 | formatter(GraphCommand(GraphCommandType.REPLACE_BATCHNORM_TO_CONV)) |
| 628 | |
| 629 | formatter(GraphCommand(GraphCommandType.FORMAT_CAST)) |
| 630 | formatter(GraphCommand(GraphCommandType.FORMAT_SLICE)) |
| 631 | formatter(GraphCommand(GraphCommandType.FORMAT_CLIP)) |
| 632 | formatter(GraphCommand(GraphCommandType.FORMAT_PAD)) |
| 633 | formatter(GraphCommand(GraphCommandType.FORMAT_RESIZE)) |
| 634 | |
| 635 | if FORMATTER_REMOVE_IDENTITY: |
| 636 | formatter(GraphCommand(GraphCommandType.REMOVE_IDENTITY)) |
| 637 | |
| 638 | if FORMATTER_REMOVE_ISOLATED: |
| 639 | formatter(GraphCommand(GraphCommandType.DELETE_ISOLATED)) |
| 640 | |
| 641 | return graph |
| 642 | |
| 643 | |
| 644 | def dispatch_graph( |
no test coverage detected