From 5206504f7ff7b502721d147a916af4fc1cd82d57 Mon Sep 17 00:00:00 2001 From: Chen Lai Date: Thu, 11 Apr 2024 14:24:21 -0700 Subject: [PATCH 1/2] Skip annotate boolean input (#2957) Summary: Pull Request resolved: https://github.com/pytorch/executorch/pull/2957 ghstack-source-id: 222200589 exported-using-ghexport It only makes sense to quantize fp tensor, but not boolean. Add a check to make sure only fp tensor are annotated in quantizer Reviewed By: jerryzh168 Differential Revision: D55946526 fbshipit-source-id: d94bfee38ab2d29fc9672ab631b4d5d0c5239d25 --- backends/qualcomm/quantizer/utils.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/backends/qualcomm/quantizer/utils.py b/backends/qualcomm/quantizer/utils.py index 809b7298eba..6a2c8e205b4 100644 --- a/backends/qualcomm/quantizer/utils.py +++ b/backends/qualcomm/quantizer/utils.py @@ -9,6 +9,7 @@ import torch from torch._ops import OpOverload +from torch._subclasses import FakeTensor from torch.ao.quantization.quantizer import ( QuantizationAnnotation, @@ -41,6 +42,18 @@ def decorator(annotator: Callable): return decorator +def _is_input_float_tensor(node: Node): + """Check if the input is not a float tensor, so that we can skip quantization for the node + since observers only works with float Tensors + """ + if ( + not isinstance(node, Node) + or "val" not in node.meta + or not isinstance(node.meta["val"], FakeTensor) + ): + return False + return node.meta["val"].dtype == torch.float32 + def _is_annotated(nodes: List[Node]): """ @@ -123,11 +136,11 @@ def annotate_binary(node: Node, quantization_config: QuantizationConfig) -> None input_qspec_map = {} input_act0 = node.args[0] - if isinstance(input_act0, Node): + if _is_input_float_tensor(input_act0): input_qspec_map[input_act0] = input_act_qspec input_act1 = node.args[1] - if isinstance(input_act1, Node): + if _is_input_float_tensor(input_act1): input_qspec_map[input_act1] = input_act_qspec node.meta[QUANT_ANNOTATION_KEY] = QuantizationAnnotation( From d95850985ae3943fc9dc81447f5e71114556ce98 Mon Sep 17 00:00:00 2001 From: Chen Lai Date: Mon, 15 Apr 2024 20:56:31 -0700 Subject: [PATCH 2/2] fix lint --- backends/qualcomm/quantizer/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backends/qualcomm/quantizer/utils.py b/backends/qualcomm/quantizer/utils.py index 6a2c8e205b4..58be76aba11 100644 --- a/backends/qualcomm/quantizer/utils.py +++ b/backends/qualcomm/quantizer/utils.py @@ -42,6 +42,7 @@ def decorator(annotator: Callable): return decorator + def _is_input_float_tensor(node: Node): """Check if the input is not a float tensor, so that we can skip quantization for the node since observers only works with float Tensors