-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arm backend: qdq folding support for remaining operators (#7340)
* Add TOSA table as custom edge op Edge operators that are lowered to TOSA TABLEs are convereted to a custom edge IR table-op. Signed-off-by: Oscar Andersson <[email protected]> Change-Id: I147008c30b9b46c7b8ae1a1c15bc540fea614a69 * Add support for concat q/dq folding This is a special case where node.args can be lists with many incoming dq-nodes. Signed-off-by: Oscar Andersson <[email protected]> Change-Id: Icf511a8bdeaaffb597b18455ab7f1fbd947ce3ca * Increase q/dq folding coverage Add support for q/dq folding of more operators such as hardtanh, maxpool2d, mul, relu, select, sub, to_copy. Signed-off-by: Oscar Andersson <[email protected]> Change-Id: Ifdabda4c927dade41c000859054696844c546f7b * Add support for sum q/dq folding sum is retraced to an int64 dtype of operator after q/dq folding. This patch adds a pass to manually force the dtype to be int8. Signed-off-by: Oscar Andersson <[email protected]> Change-Id: Ifa737a398c5a878d52cd76a2392499905da085ce * Complete q/dq folding coverage Add support for q/dq folding for the remaining supported ops in Arm backend. Signed-off-by: Oscar Andersson <[email protected]> Change-Id: I9012b4a501ce018c9771c729706be3b031a5c7ae * Remove is_quant_node from NodeVisitor.define_node Signed-off-by: Oscar Andersson <[email protected]> Change-Id: Ibb17add461dc79e022a7f4accde29f9f9d61b16d * Fix pyre issues Address issues from pyre and add similar # pyre-ignores as in #7362. Signed-off-by: Oscar Andersson <[email protected]> Change-Id: I6feaa611dcd539b3b0d21a6a7dd696ef7db691ef --------- Signed-off-by: Oscar Andersson <[email protected]>
- Loading branch information
1 parent
62c6346
commit a29b208
Showing
53 changed files
with
822 additions
and
749 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2024 Arm Limited and/or its affiliates. | ||
# 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 itertools | ||
|
||
import torch | ||
from executorch.backends.arm._passes.arm_pass_utils import create_node | ||
from executorch.backends.arm.tosa_quant_utils import dq_op, q_op | ||
from executorch.exir.dialects._ops import ops as exir_ops | ||
from executorch.exir.pass_base import ExportPass, PassResult | ||
from torch.fx import GraphModule | ||
from torch.fx.passes.utils.source_matcher_utils import get_source_partitions | ||
|
||
|
||
class AnnotateDecomposedMatmulPass(ExportPass): | ||
""" | ||
torch.matmul can be decomposed in many ways, for instance: | ||
dq -> matmul -> q can become | ||
dq -> repeat -> view -> bmm -> view -> dq which makes quantization folding | ||
difficult. This helper function find all matmul partitions and annotate its | ||
matmul-op (can be mm or bmm). | ||
""" | ||
|
||
def call(self, graph_module: GraphModule) -> PassResult: | ||
matmul_partitions = get_source_partitions( | ||
graph_module.graph, | ||
[ | ||
torch.matmul, | ||
], | ||
None, | ||
) | ||
matmul_partitions = list( | ||
itertools.chain.from_iterable(matmul_partitions.values()) | ||
) | ||
matmul_targets = { | ||
exir_ops.edge.aten.mm.default, | ||
exir_ops.edge.aten.bmm.default, | ||
} | ||
for partition in matmul_partitions: | ||
quantized_input = all( | ||
input_node.target == dq_op for input_node in partition.input_nodes | ||
) | ||
matmul_node = [ | ||
node for node in partition.nodes if node.target in matmul_targets | ||
][0] | ||
if quantized_input: | ||
matmul_args = matmul_node.all_input_nodes | ||
for i in range(len(matmul_args)): | ||
input_node = partition.input_nodes[i] | ||
matmul_input_node = matmul_args[i] | ||
# Remove partition input dq-node | ||
input_node.replace_all_uses_with(input_node.all_input_nodes[0]) | ||
graph_module.graph.erase_node(input_node) | ||
input_node_qargs = input_node.args[1:] | ||
with graph_module.graph.inserting_before(matmul_node): | ||
# Create new dq-node before matmul | ||
dq_node = create_node( | ||
graph=graph_module.graph, | ||
op_target=dq_op, | ||
) | ||
dq_node.args = (matmul_input_node, *input_node_qargs) | ||
matmul_node.replace_input_with(matmul_input_node, dq_node) | ||
|
||
partition_output = list(partition.output_nodes[0].users)[0] | ||
quantized_output = partition_output.target == q_op | ||
if quantized_output: | ||
output_node_qargs = partition_output.args[1:] | ||
with graph_module.graph.inserting_after(matmul_node): | ||
# Create q-node after matmul | ||
q_node = create_node( | ||
graph=graph_module.graph, | ||
op_target=q_op, | ||
) | ||
matmul_node.replace_all_uses_with(q_node) | ||
q_node.args = (matmul_node, *output_node_qargs) | ||
# Remove partition output q-node | ||
partition_output.replace_all_uses_with( | ||
partition_output.all_input_nodes[0] | ||
) | ||
graph_module.graph.erase_node(partition_output) | ||
|
||
# retrace the graph to update the fake tensor types | ||
graph_module = super().call(graph_module).graph_module | ||
|
||
graph_module.recompile() | ||
return PassResult(graph_module, True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.