Skip to content

Commit 01348dc

Browse files
committed
Fixing quant range hang during qnn finalize
1 parent 124b677 commit 01348dc

3 files changed

Lines changed: 21 additions & 20 deletions

File tree

.github/workflows/pull.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,7 @@ jobs:
852852
strategy:
853853
matrix:
854854
dtype: [fp32]
855-
# TODO(T12345): re-enable qnn_16a16w once OOM on linux.2xlarge is resolved
856-
pt2e_quantize: [qnn_8a8w]
855+
pt2e_quantize: [qnn_16a16w, qnn_8a8w]
857856
mode: [qnn]
858857
fail-fast: false
859858
with:

.github/workflows/trunk.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,7 @@ jobs:
951951
strategy:
952952
matrix:
953953
dtype: [fp32]
954-
# TODO(T12345): re-enable qnn_16a16w once OOM on linux.2xlarge is resolved
955-
pt2e_quantize: [qnn_8a8w]
954+
pt2e_quantize: [qnn_16a16w, qnn_8a8w]
956955
mode: [qnn]
957956
fail-fast: false
958957
with:

backends/qualcomm/quantizer/quantizer.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66
import itertools
7+
import logging
78
import operator
89
from dataclasses import dataclass
910
from enum import IntEnum, unique
10-
from functools import partial, reduce
11+
from functools import partial
1112
from operator import attrgetter
1213
from typing import Callable, Dict, List, Optional, Sequence, Set, Tuple
1314

1415
# To support quantize op lowering in AOT
1516
try:
1617
import executorch.kernels.quantized # noqa[F401]
1718
except:
18-
import logging
19-
2019
logging.info(
2120
"Failed to load quantized_aot_lib. To run on LPAI backend, please make sure that quantized_aot_lib is accessible."
2221
)
23-
del logging
2422
import torch
2523
from executorch.backends.qualcomm._passes.qnn_pass_manager import (
2624
get_qnn_pass_manager_cls,
@@ -47,7 +45,7 @@
4745
from torch.fx import GraphModule
4846
from torch.fx.passes.utils.source_matcher_utils import get_source_partitions
4947
from torchao.quantization.pt2e import UniformQuantizationObserverBase
50-
from torchao.quantization.pt2e.quantizer import Quantizer
48+
from torchao.quantization.pt2e.quantizer import Quantizer, SharedQuantizationSpec
5149

5250
from .qconfig import (
5351
get_16a16w_qnn_ptq_config,
@@ -401,21 +399,26 @@ def recipe(self):
401399
def _get_quant_range(self, node):
402400
if quant_info := node.meta.get(QCOM_QUANT_ANNOTATION_KEY, None):
403401
try:
404-
dtype_info = torch.iinfo(
405-
reduce(getattr, ["output_qspec", "dtype"], quant_info)
406-
)
402+
# SharedQuantizationSpec has not quant info, so we need to find source node
403+
# that this node is sharing quant info with and use source node's quant info.
404+
qspec = quant_info.output_qspec
405+
while isinstance(qspec, SharedQuantizationSpec):
406+
edge_or_node = qspec.edge_or_node
407+
if isinstance(edge_or_node, tuple):
408+
input_node, user_node = edge_or_node
409+
shared_info = user_node.meta[QCOM_QUANT_ANNOTATION_KEY]
410+
qspec = shared_info.input_qspec_map[input_node]
411+
else:
412+
shared_info = edge_or_node.meta[QCOM_QUANT_ANNOTATION_KEY]
413+
qspec = shared_info.output_qspec
414+
dtype_info = torch.iinfo(qspec.dtype)
407415
except:
416+
logging.warning(f"Could not resolve quant range for node: {node.name}")
408417
return
409418

410419
quant_range = (
411-
dtype_info.max
412-
if quant_info.output_qspec.quant_max is None
413-
else quant_info.output_qspec.quant_max
414-
) - (
415-
dtype_info.min
416-
if quant_info.output_qspec.quant_min is None
417-
else quant_info.output_qspec.quant_min
418-
)
420+
dtype_info.max if qspec.quant_max is None else qspec.quant_max
421+
) - (dtype_info.min if qspec.quant_min is None else qspec.quant_min)
419422
return quant_range
420423

421424
def _get_candidates_with_infinity_args(self, graph_module: GraphModule):

0 commit comments

Comments
 (0)