-
Notifications
You must be signed in to change notification settings - Fork 700
Arm backend: Propagate node info from quantizer to backend #15300
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
Draft
oscarandersson8218
wants to merge
3
commits into
pytorch:main
Choose a base branch
from
oscarandersson8218:node_quant_metadata
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # 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. | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ArmAnnotationInfo: | ||
| """ | ||
| Data class to carry Arm-specific annotation information through the pipeline. | ||
| This is intended to be attached to node.meta['custom'] and propagated | ||
| through partitioning and backend stages. As it's propagated through the pipeline, | ||
| it's intentionally minimal and only carries whether the node is quantized or not. | ||
| """ | ||
|
|
||
| quantized: bool | ||
| CUSTOM_META_KEY: str = "_arm_annotation_info" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # 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.quantizer import ( | ||
| get_symmetric_quantization_config, | ||
| TOSAQuantizer, | ||
| ) | ||
| from executorch.backends.arm.test.tester.test_pipeline import TosaPipelineINT | ||
| from executorch.backends.arm.tosa import TosaSpecification | ||
| from executorch.backends.xnnpack.test.tester import Quantize | ||
|
|
||
|
|
||
| class AddSigmoidMul(torch.nn.Module): | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.sigmoid = torch.nn.Sigmoid() | ||
|
|
||
| def forward(self, x, y): | ||
| return self.sigmoid(x + y) * x | ||
|
|
||
|
|
||
| def get_selective_quantizer(modules): | ||
| quantizer = TOSAQuantizer(TosaSpecification.create_from_string("TOSA-1.0+INT")) | ||
| quantizer.set_global(get_symmetric_quantization_config()) | ||
| for module in modules: | ||
| quantizer.set_module_type(module, None) | ||
|
|
||
| return Quantize(quantizer, get_symmetric_quantization_config()) | ||
|
|
||
|
|
||
| def test_qdq_squeezed_fp_op(): | ||
| """Test that a float operation surrounded by quantize-dequantize pairs | ||
| is correctly handled by the partitioner and the TOSA backend. | ||
| Pattern: | ||
| q -> dq -> add -> q -> dq -> sigmoid -> q -> dq -> mul -> dq -> q | ||
| |_____Non-delegated____| | ||
| """ | ||
| aten_op = "torch.ops.aten.add.Tensor" | ||
| exir_op = "executorch_exir_dialects_edge__ops_aten_add_Tensor" | ||
| module = AddSigmoidMul() | ||
| x = torch.randn(2, 3, 4) | ||
| y = torch.randn(2, 3, 4) | ||
| pipeline = TosaPipelineINT( | ||
| module=module, test_data=(x, y), aten_op=aten_op, exir_op=exir_op | ||
| ) | ||
| pipeline.change_args("quantize", get_selective_quantizer([torch.nn.Sigmoid])) | ||
| pipeline.change_args( | ||
| "check_count.exir", | ||
| { | ||
| "torch.ops.higher_order.executorch_call_delegate": 2, | ||
| "executorch_exir_dialects_edge__ops_aten_sigmoid_default": 1, | ||
| "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 2, | ||
| "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 3, | ||
| }, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| class MulAddSigmoidConv(torch.nn.Module): | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.sigmoid = torch.nn.Sigmoid() | ||
| self.conv = torch.nn.Conv1d(3, 3, 1) | ||
|
|
||
| def forward(self, x, y): | ||
| return self.conv(self.sigmoid(x + y * x)) | ||
|
|
||
|
|
||
| def test_quantized_to_float_transition(): | ||
| """Test that a model executing quantized ops followed by float ops | ||
| is correctly handled by the partitioner and the TOSA backend. | ||
| Pattern: | ||
| q -> dq -> mul -> q -> dq -> add -> q -> dq -> sigmoid -> conv | ||
| |____Non-delegated___| | ||
| """ | ||
| aten_op = "torch.ops.aten.add.Tensor" | ||
| exir_op = "executorch_exir_dialects_edge__ops_aten_add_Tensor" | ||
| module = MulAddSigmoidConv() | ||
| x = torch.randn(2, 3, 4) | ||
| y = torch.randn(2, 3, 4) | ||
| pipeline = TosaPipelineINT( | ||
| module=module, test_data=(x, y), aten_op=aten_op, exir_op=exir_op | ||
| ) | ||
| pipeline.change_args( | ||
| "quantize", get_selective_quantizer([torch.nn.Sigmoid, torch.nn.Conv1d]) | ||
| ) | ||
| pipeline.change_args( | ||
| "check_count.exir", | ||
| { | ||
| "torch.ops.higher_order.executorch_call_delegate": 1, | ||
| "executorch_exir_dialects_edge__ops_aten_sigmoid_default": 1, | ||
| "executorch_exir_dialects_edge__ops_aten_convolution_default": 1, | ||
| "executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor_default": 1, | ||
| "executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor_default": 2, | ||
| }, | ||
| ) | ||
| pipeline.run() |
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.
@digantdesai What are your thoughts on this conceptually?