-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathannotate_stack.py
More file actions
42 lines (35 loc) · 1.68 KB
/
Copy pathannotate_stack.py
File metadata and controls
42 lines (35 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Copyright (c) Qualcomm Innovation Center, Inc.
# All rights reserved
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import torch
from executorch.backends.qualcomm.builders.node_visitor import q_ops
from executorch.backends.qualcomm.utils.constants import QCOM_QUANT_ATTRS
from executorch.exir.pass_base import ExportPass, PassResult
from torch.fx.passes.utils.source_matcher_utils import get_source_partitions
from .utils import get_quant_attrs
class AnnotateStack(ExportPass):
"""
Add "quant_attrs" to graph nodes' meta from the QDQ information
generated after quantization process.
"""
_SOURCE_OPS = [torch.stack, torch.ops.aten.stack.default, "stack"]
def __init__(self, edge_program: torch.export.ExportedProgram):
super(AnnotateStack, self).__init__()
self.edge_program = edge_program
def _annotate_stack(self, graph_module: torch.fx.GraphModule):
partitions = get_source_partitions(graph_module.graph, self._SOURCE_OPS)
for src_partitions in partitions.values():
for src_partition in src_partitions:
output = src_partition.output_nodes[0]
if (list(output.users)[0].target) in q_ops:
quant_attrs = get_quant_attrs(
self.edge_program, list(output.users)[0]
)
for n in src_partition.nodes:
n.meta[QCOM_QUANT_ATTRS] = quant_attrs.copy()
def call(self, graph_module: torch.fx.GraphModule):
self._annotate_stack(graph_module)
graph_module.recompile()
return PassResult(graph_module, True)