MCPcopy Create free account
hub / github.com/pytorch/executorch / get_anchors

Method get_anchors

backends/nxp/quantizer/patterns.py:758–811  ·  view source on GitHub ↗
(
        self, gm: fx.GraphModule, fused_partition: list[fx.GraphModule]
    )

Source from the content-addressed store, hash-verified

756 return [torch.ops.aten.linear.default]
757
758 def get_anchors(
759 self, gm: fx.GraphModule, fused_partition: list[fx.GraphModule]
760 ) -> PartitionAnchors:
761 linear_node = fused_partition[0].nodes[-1]
762
763 bias_qspec = DerivedQuantizationSpec(
764 derived_from=[
765 (linear_node.args[0], linear_node),
766 (linear_node.args[1], linear_node),
767 ],
768 derive_qparams_fn=get_bias_qparams,
769 dtype=torch.int32,
770 quant_min=-(2**31),
771 quant_max=2**31 - 1,
772 qscheme=torch.per_tensor_affine,
773 )
774
775 # Keep bias empty if not supplied
776 bias = []
777 if len(linear_node.args) > 2:
778 bias = [(linear_node, NodeArgsIdx(2), bias_qspec)]
779
780 # If the following node is a fusable activation, quantize together with activation
781 output = [(linear_node,)]
782 if (
783 len(linear_node.users) == 1
784 and len(linear_node.meta["val"].shape) <= 2
785 and self.neutron_target_info.is_supported_fused_activation__aten(
786 activation := next(iter(linear_node.users))
787 )
788 ):
789 activation_quantizer = self.neutron_quantizer.op_to_quantizer[
790 activation.target
791 ]
792 activation_quantizer.annotate(gm)
793 output = []
794 activation.meta["quantization_annotation"].input_qspec_map = {}
795
796 # In order for QAT to be numerically correct, there should be no quantization between
797 # linear node and batch norm node.
798 if self.is_qat:
799 linear_users = linear_node.users
800 possibly_bn = (
801 list(linear_users.keys())[0] if len(linear_users) == 1 else None
802 )
803 if possibly_bn and _is_batch_norm(possibly_bn):
804 output = []
805
806 return PartitionAnchors(
807 inputs=[(linear_node, NodeArgsIdx(0))],
808 weights=[(linear_node, NodeArgsIdx(1))],
809 biases=bias,
810 output=output,
811 )
812
813
814class MaxPool1DPattern(SharedSpecPattern):

Callers

nothing calls this directly

Calls 6

NodeArgsIdxClass · 0.85
_is_batch_normFunction · 0.85
keysMethod · 0.80
PartitionAnchorsClass · 0.70
annotateMethod · 0.45

Tested by

no test coverage detected