forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqnn_pass_manager.py
More file actions
303 lines (279 loc) · 11.2 KB
/
qnn_pass_manager.py
File metadata and controls
303 lines (279 loc) · 11.2 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# 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 inspect
from collections import OrderedDict
from typing import Dict
from executorch.backends.qualcomm._passes import (
AnnotateAvgPool1D,
AnnotateQuantAttrs,
AnnotateStack,
AnnotateUnbind,
CanonicalizeConv,
ConvertBmmToMatmul,
ConvertLinearToConv2d,
ConvertMhaToSha,
ConvertSquareToPow,
DecomposeAcos,
DecomposeAny,
DecomposeBinaryAlpha,
DecomposeCDist,
DecomposeColIm,
DecomposeEinsum,
DecomposeExpM1,
DecomposeFloorDivide,
DecomposeGlu,
DecomposeLinalgVectorNorm,
DecomposeLogVariants,
DecomposeMaxPool3d,
DecomposeMinMaxDim,
DecomposeReciprocal,
DecomposeRemainder,
DecomposeRoll,
DecomposeSilu,
DecomposeThreshold,
DecomposeTriu,
DecomposeTrunc,
DecomposeWrapWithAutocast,
ExpandBroadcastTensorShape,
FixedLinearKeepDim,
FoldQDQ,
FuseConsecutiveCast,
FuseConsecutiveTranspose,
I64toI32,
InsertIOQDQ,
InsertRequantize,
InsertReshapeForReduceOps,
LayoutTransform,
LiftConstantScalarOperands,
RecomposePadMaxPool2d,
RecomposePixelUnshuffle,
RecomposeRmsNorm,
ReduceDynamicRange,
Remove0DTensor,
RemoveRedundancy,
ReplaceArangeArgs,
ReplaceInfValues,
ResolveDebugHandle,
TagQuantIO,
)
from executorch.backends.qualcomm._passes.utils import (
get_passes_dependency_for_capture_program,
)
from executorch.backends.qualcomm.serialization.qc_schema import (
QnnExecuTorchBackendType,
)
from executorch.backends.qualcomm.utils.constants import (
QCOM_PASS_ACTIVATE_KEY,
QCOM_PASS_ARGS_KWARGS_DEFAULTS_KEY,
)
from executorch.backends.transforms.decompose_sdpa import (
DecomposeScaledDotProductAttention,
)
from executorch.exir import ExportedProgram
from executorch.exir.pass_manager import PassManager
from executorch.exir.program._program import (
_get_updated_graph_signature,
lift_constant_tensor_pass,
)
from torch.fx import GraphModule
from torch.fx.passes.infra.pass_manager import this_before_that_pass_constraint
def get_capture_program_passes():
"""
Defines and returns the default ordered passes for the capture program.
This function creates an OrderedDict containing a series of default passes.
Returns:
OrderedDict: An ordered dictionary containing all default passes along with their activation status and initialization parameters.
"""
# The second value in each tuple in `default_passes_and_setting` indicates whether the corresponding pass is activated by default.
# If a pass is activated, it will be executed by default.
default_passes_and_setting = [
(AnnotateAvgPool1D, True),
(AnnotateQuantAttrs, True),
(AnnotateStack, True),
(AnnotateUnbind, True),
(ConvertBmmToMatmul, False),
(DecomposeAcos, True),
(DecomposeAny, True),
(DecomposeColIm, True),
(DecomposeLogVariants, True),
(DecomposeMaxPool3d, True),
(DecomposeMinMaxDim, True),
(DecomposeRemainder, True),
(DecomposeTrunc, True),
(ExpandBroadcastTensorShape, True),
(FixedLinearKeepDim, True),
(FoldQDQ, True),
(I64toI32, True),
(LayoutTransform, True),
(RecomposePadMaxPool2d, True),
(RecomposePixelUnshuffle, True),
(RecomposeRmsNorm, True),
(Remove0DTensor, True),
(RemoveRedundancy, True),
(TagQuantIO, False),
(ResolveDebugHandle, True),
]
passes = OrderedDict()
for p, act in default_passes_and_setting:
init_signature = inspect.signature(p.__init__)
args_kwargs_defaults = {
k: v.default if v.default is not inspect.Parameter.empty else None
for k, v in init_signature.parameters.items()
if k != "self"
}
passes[p] = {
QCOM_PASS_ACTIVATE_KEY: act,
QCOM_PASS_ARGS_KWARGS_DEFAULTS_KEY: args_kwargs_defaults,
}
return passes
class QnnPassManager(PassManager):
def __init__(self) -> None:
super().__init__()
def _transform(self, graph_module: GraphModule):
return self(graph_module).graph_module
# TODO: Move these passes into qnn_partitioner and qnn_preprocess to
# prevent users from needing to call custom APIs like capture_program
def get_to_edge_transform_passes(
self,
exported_program: ExportedProgram,
passes_job: OrderedDict = None,
dep_table: Dict = None,
backend_type: QnnExecuTorchBackendType = QnnExecuTorchBackendType.kHtpBackend,
):
# TODO: remove this workaround when target could be correctly detected
from executorch.backends.qualcomm.builders import node_visitor
from executorch.exir.dialects._ops import ops as exir_ops
node_visitor.q_ops.add(exir_ops.edge.torchao.quantize_affine.default)
node_visitor.dq_ops.add(exir_ops.edge.torchao.dequantize_affine.default)
passes_job = (
passes_job if passes_job is not None else get_capture_program_passes()
)
dep_table = (
dep_table
if dep_table is not None
else get_passes_dependency_for_capture_program()
)
for that, these in dep_table.items():
for this in these:
self.add_constraint(this_before_that_pass_constraint(this, that))
for p in passes_job:
self.add_pass(p)
self.solve_constraints()
sorted_passes = self.passes
self.passes = []
for p in sorted_passes:
if not passes_job[p][QCOM_PASS_ACTIVATE_KEY]:
continue
kwargs = passes_job[p][QCOM_PASS_ARGS_KWARGS_DEFAULTS_KEY]
if "edge_program" in kwargs:
kwargs["edge_program"] = exported_program
if "backend_type" in kwargs:
kwargs["backend_type"] = backend_type
self.add_pass(p(**kwargs))
assert isinstance(
self.passes[-1], ResolveDebugHandle
), "Please ensure ResolveDebugHandle is the last executed edge pass."
return self.passes
def transform_for_to_edge_pipeline(
self,
exported_program: ExportedProgram,
passes_job: OrderedDict = None,
dep_table: Dict = None,
):
transform_passes = self.get_to_edge_transform_passes(
exported_program, passes_job=passes_job, dep_table=dep_table
)
for p in transform_passes:
p(exported_program.graph_module)
exported_program._graph_signature = _get_updated_graph_signature(
exported_program.graph_signature,
exported_program.graph_module,
)
exported_program._validate()
return exported_program
# Before quantizer
def transform_for_annotation_pipeline(self, graph_module: GraphModule):
self.add_pass(RemoveRedundancy(quantization_capture=True))
self.add_pass(ReduceDynamicRange())
self.add_pass(RecomposePixelUnshuffle(quantization_capture=True))
self.add_pass(RecomposeRmsNorm(quantization_capture=True))
self.add_pass(ReplaceArangeArgs())
self.add_pass(DecomposeAcos())
self.add_pass(DecomposeBinaryAlpha())
self.add_pass(DecomposeCDist())
self.add_pass(DecomposeMaxPool3d(quantization_capture=True))
self.add_pass(DecomposeScaledDotProductAttention())
self.add_pass(DecomposeRoll())
self.add_pass(DecomposeSilu())
self.add_pass(DecomposeThreshold())
self.add_pass(DecomposeTriu())
self.add_pass(DecomposeTrunc())
self.add_pass(DecomposeWrapWithAutocast())
self.add_pass(DecomposeEinsum())
self.add_pass(DecomposeExpM1())
self.add_pass(DecomposeGlu())
# HTP and GPU doesn't support ElementWiseUnary with operation=reciprocal
# Decompose Reciprocal into Div for these 2 backend
# TODO: Skip this pass for CPU backend (Dependency: Backend-aware passes manager)
self.add_pass(DecomposeReciprocal())
self.add_pass(DecomposeRemainder())
self.add_pass(DecomposeLinalgVectorNorm(quantization_capture=True))
self.add_pass(DecomposeLogVariants())
self.add_pass(ReplaceInfValues())
self.add_pass(LiftConstantScalarOperands())
self.add_pass(InsertReshapeForReduceOps())
return self._transform(graph_module)
def transform_for_export_pipeline(
self, exported_program: ExportedProgram, convert_linear_to_conv2d: bool = False
):
self.add_pass(DecomposeBinaryAlpha())
self.add_pass(DecomposeCDist())
self.add_pass(DecomposeScaledDotProductAttention())
self.add_pass(DecomposeRoll())
self.add_pass(DecomposeThreshold())
self.add_pass(DecomposeTriu())
self.add_pass(DecomposeLinalgVectorNorm(quantization_capture=True))
self.add_pass(DecomposeExpM1())
# DecomposeFloorDivide does not apply to the annotation pipeline,
# since the CPU QDQ model would reduce accuracy.
# We keep div and floor operations in floating-point to maintain precision.
# This pass is needed before to_edge pipeline to avoid mixed type for div operator with RemoveMixedTypeOperators pass.
self.add_pass(DecomposeFloorDivide())
self.add_pass(DecomposeWrapWithAutocast())
# HTP and GPU doesn't support ElementWiseUnary with operation=reciprocal
# Decompose Reciprocal into Div for these 2 backend
# TODO: Skip this pass for CPU backend (Dependency: Backend-aware passes manager)
self.add_pass(DecomposeReciprocal())
# this pass will rewrite state_dict, it needs to be accomplished before
# to_edge_transform_and_lower
self.add_pass(CanonicalizeConv(exported_program))
if convert_linear_to_conv2d:
self.add_pass(ConvertLinearToConv2d(exported_program))
self.add_pass(ConvertSquareToPow())
self.add_pass(LiftConstantScalarOperands())
self.add_pass(InsertReshapeForReduceOps())
self._transform(exported_program.graph_module)
ep = lift_constant_tensor_pass(exported_program)
return ep
def transform_for_preprocess_pipeline(
self, exported_program: ExportedProgram, use_mha2sha=False
):
self.add_pass(FoldQDQ(exported_program, force_fold=True))
if use_mha2sha:
self.add_pass(ConvertMhaToSha(exported_program))
self.add_pass(InsertRequantize())
self.add_pass(InsertIOQDQ(exported_program))
self.add_pass(LayoutTransform(exported_program, insert_permute=True))
self.add_pass(FuseConsecutiveCast())
self.add_pass(FuseConsecutiveTranspose())
self._transform(exported_program.graph_module)
# Update inputs_to_buffers and buffers_to_mutate in graph signature for mutable buffer
# Since I/O will be inserted Q/DQ, it results in failed to mapping output node names and buffer
exported_program._graph_signature = _get_updated_graph_signature(
exported_program.graph_signature,
exported_program.graph_module,
)
return exported_program.graph_module