-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Quantize compatible node + activation patterns as one block
Annotate conv1d/conv2d/linear followed by relu/relu6 patterns as one block and fuse the activation into its parent. The activation will then be implicitly done in the tosa.rescale node that will have a -128 zero-point. Change-Id: I5bf1e2c91be21ab842012fbc20d159af7fe2222d
- Loading branch information
Showing
4 changed files
with
133 additions
and
4 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
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,60 @@ | ||
# Copyright 2025 Arm Limited and/or its affiliates. | ||
# | ||
# 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.arm.tosa_quant_utils import q_op | ||
from executorch.exir.dialects._ops import ops as exir_ops | ||
from executorch.exir.pass_base import ExportPass, PassResult | ||
from torch.fx import Node | ||
|
||
|
||
class FuseQuantizedActivationPass(ExportPass): | ||
def _is_fuseable_quantized_activation(self, node: Node): | ||
"""Fuse activations that have a 0 lower bound and quantized with a qmin zero-point""" | ||
is_fuseable = node.target == exir_ops.edge.aten.relu.default | ||
if node.target == exir_ops.edge.aten.hardtanh.default: | ||
min_val = node.args[1] | ||
is_fuseable = min_val == 0 | ||
|
||
is_quantized = len(node.users) == 1 and next(iter(node.users)).target == q_op | ||
if is_quantized: | ||
quant_node = next(iter(node.users)) | ||
zp = quant_node.args[2] | ||
qmin = quant_node.args[3] | ||
|
||
return is_fuseable and is_quantized and zp == qmin | ||
|
||
def _is_fuseable_input(self, node: Node): | ||
return ( | ||
node.target | ||
in ( | ||
exir_ops.edge.aten.convolution.default, | ||
exir_ops.edge.aten.linear.default, | ||
) | ||
and len(node.users) == 1 | ||
) | ||
|
||
def call(self, graph_module: torch.fx.GraphModule): | ||
modified = False | ||
for node in graph_module.graph.nodes: | ||
if node.op != "call_function": | ||
continue | ||
|
||
if not self._is_fuseable_quantized_activation(node): | ||
continue | ||
|
||
input_node = node.args[0] | ||
if not self._is_fuseable_input(input_node): | ||
continue | ||
|
||
node.replace_all_uses_with(input_node) | ||
graph_module.graph.erase_node(node) | ||
modified = True | ||
|
||
if modified: | ||
graph_module.recompile() | ||
graph_module = super().call(graph_module).graph_module | ||
|
||
return PassResult(graph_module, modified) |
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