Compute quantization properties for a node. Determine which inputs and/or outputs should be annotated for quantization based on the node's operator and surrounding pattern. Args: node (Node): Node to analyze. gm (torch.fx.GraphModule): Owning graph module. quant
( # noqa: C901
node: Node, gm: torch.fx.GraphModule, quantization_config
)
| 599 | |
| 600 | |
| 601 | def get_quant_properties( # noqa: C901 |
| 602 | node: Node, gm: torch.fx.GraphModule, quantization_config |
| 603 | ) -> _OpQuantProperties | None: |
| 604 | """Compute quantization properties for a node. |
| 605 | |
| 606 | Determine which inputs and/or outputs should be annotated for quantization |
| 607 | based on the node's operator and surrounding pattern. |
| 608 | |
| 609 | Args: |
| 610 | node (Node): Node to analyze. |
| 611 | gm (torch.fx.GraphModule): Owning graph module. |
| 612 | quantization_config: Source for activation/weight/bias qspecs. |
| 613 | |
| 614 | Returns: |
| 615 | _OpQuantProperties | None: Properties to apply, or ``None`` if the |
| 616 | node is unsupported or not suitable for quantization. |
| 617 | |
| 618 | """ |
| 619 | if node.target == torch.ops.aten.conv_transpose2d.input: |
| 620 | weight_qspec = _adjust_weight_qspec_for_conv_transpose( |
| 621 | node, quantization_config.get_weight_qspec() |
| 622 | ) |
| 623 | quantization_config = replace(quantization_config, weight=weight_qspec) |
| 624 | |
| 625 | input_act_qspec = quantization_config.get_input_act_qspec() |
| 626 | weight_qspec = quantization_config.get_weight_qspec() |
| 627 | output_act_qspec = quantization_config.get_output_act_qspec() |
| 628 | bias_qspec = quantization_config.get_bias_qspec(node) |
| 629 | if output_act_qspec is not None: |
| 630 | # Check if output activation qspec is symmetric. In that case |
| 631 | # we avoid conv + relu fusion for quantization annotation. |
| 632 | is_symmetric = output_act_qspec.qscheme == torch.per_tensor_symmetric |
| 633 | else: |
| 634 | is_symmetric = False |
| 635 | |
| 636 | quant_properties = _OpQuantProperties() |
| 637 | |
| 638 | def any_or_hardtanh_min_zero(n: Node): |
| 639 | """Return True for any op or hardtanh with ``min_val == 0``.""" |
| 640 | # Check that if the node is a hardtanh, its min_val is zero |
| 641 | return ( |
| 642 | n.target |
| 643 | not in (torch.ops.aten.hardtanh.default, torch.ops.aten.hardtanh_.default) |
| 644 | or n.args[1] == 0 |
| 645 | ) |
| 646 | |
| 647 | if not is_symmetric and _match_pattern( |
| 648 | node, |
| 649 | [ |
| 650 | _conv_ops, |
| 651 | {torch.ops.aten.batch_norm.default}, |
| 652 | { |
| 653 | torch.ops.aten.relu.default, |
| 654 | torch.ops.aten.relu_.default, |
| 655 | torch.ops.aten.hardtanh.default, |
| 656 | torch.ops.aten.hardtanh_.default, |
| 657 | }, |
| 658 | ], |
no test coverage detected