-
Notifications
You must be signed in to change notification settings - Fork 971
Improve accuracy for models using shuffle, unshuffle, cat ops (#19159) #19159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhinaykukkadapu
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
abhinaykukkadapu:export-D102626539
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+573
−193
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,97 @@ | ||
| # 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. | ||
|
|
||
| from typing import Any, Dict | ||
|
|
||
| import torch | ||
| from executorch.backends.qualcomm.builders.node_visitor import dq_ops, q_ops | ||
| from executorch.backends.qualcomm.utils.constants import ( | ||
| QCOM_DTYPE, | ||
| QCOM_ENCODING, | ||
| QCOM_QUANT_MAX, | ||
| QCOM_QUANT_MIN, | ||
| QCOM_REQUANTIZE, | ||
| QCOM_SCALE, | ||
| QCOM_ZERO_POINT, | ||
| ) | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass, PassResult | ||
|
|
||
| from .utils import get_quant_attrs | ||
|
|
||
|
|
||
| EDGE_CAT_OPS = { | ||
| exir_ops.edge.aten.cat.default, | ||
| exir_ops.edge.aten.concat.default, | ||
| } | ||
|
|
||
|
|
||
| class AnnotateConcatRequant(ExportPass): | ||
| """ | ||
| Record explicit requantization needs for concat inputs whose concrete | ||
| post-calibration qparams do not match concat's output domain. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| edge_program: torch.export.ExportedProgram, | ||
| skip_advanced_requant: bool = False, | ||
| ): | ||
| super(AnnotateConcatRequant, self).__init__() | ||
| self.edge_program = edge_program | ||
| self.skip_advanced_requant = skip_advanced_requant | ||
|
|
||
| def _is_requant_needed(self, src_attrs: Dict[str, Any], dst_attrs: Dict[str, Any]): | ||
| if self.skip_advanced_requant: | ||
| return src_attrs[QCOM_DTYPE] != dst_attrs[QCOM_DTYPE] | ||
|
|
||
| return any( | ||
| src_attrs[attr] != dst_attrs[attr] | ||
| for attr in [ | ||
| QCOM_SCALE, | ||
| QCOM_ZERO_POINT, | ||
| QCOM_QUANT_MIN, | ||
| QCOM_QUANT_MAX, | ||
| QCOM_DTYPE, | ||
| ] | ||
| ) | ||
|
|
||
| def _annotate_concat_input_requant(self, quant_node: torch.fx.Node) -> None: | ||
| cat_node = quant_node.args[0] | ||
| if cat_node.target not in EDGE_CAT_OPS: | ||
| return | ||
|
|
||
| output_q_attrs = get_quant_attrs(self.edge_program, quant_node) | ||
| for input_node in cat_node.args[0]: | ||
| if input_node.target not in dq_ops: | ||
| continue | ||
|
|
||
| source_q_node = input_node.args[0] | ||
| if source_q_node.target not in q_ops: | ||
| continue | ||
|
|
||
| source_q_attrs = get_quant_attrs(self.edge_program, source_q_node) | ||
| if not self._is_requant_needed(source_q_attrs, output_q_attrs): | ||
| continue | ||
|
|
||
| source_node = source_q_node.args[0] | ||
| if not isinstance(source_node, torch.fx.Node): | ||
| continue | ||
|
|
||
| requant_attrs = output_q_attrs.copy() | ||
| requant_attrs[QCOM_ENCODING] = source_q_attrs[QCOM_ENCODING] | ||
| source_node.meta.setdefault(QCOM_REQUANTIZE, {}) | ||
| source_node.meta[QCOM_REQUANTIZE][cat_node.name] = requant_attrs | ||
|
|
||
| def call(self, graph_module: torch.fx.GraphModule): | ||
| for node in graph_module.graph.nodes: | ||
| if ( | ||
| node.target in q_ops | ||
| and isinstance(node.args[0], torch.fx.Node) | ||
| and node.args[0].target in EDGE_CAT_OPS | ||
| ): | ||
| self._annotate_concat_input_requant(node) | ||
| return PassResult(graph_module, True) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this change it reverting what this PR is doing: #15162.
The reason #15162 is introduced is because the input[0] could not cover the entire range of values for concat output, so a lot of output values were clipped.
If you have 2 input tensors like:
sample_input = ( torch.tensor([[[[-10.0, 2.0], [3.0, 4.0]]]]), torch.tensor([[[[1.0, 3.0], [8.0, 10.0]]]]), )and after it goes through cat operation, you will be getting the wrong value with this PR.
[tensor([[[[-9.9798, 1.9849], [ 2.9774, 4.0250], [ 1.0476, 3.0325], [ 4.0802, 4.0802]]]])]I have a demo PR to reproduce this error, please have a look:
#19182
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @winskuo-quic for the detailed review, i agree that it might've worked for the model but might not work when the ranges skewed like in your example. Let me revert the cat to concatobserver and test the accuracy.