-
Notifications
You must be signed in to change notification settings - Fork 749
Creating Paramaterized Test For Quantizers For Easier Testing #16098
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
RahulC7
wants to merge
3
commits into
pytorch:main
Choose a base branch
from
RahulC7:export-D88054917
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.
+131
−0
Open
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
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 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,16 +7,143 @@ | |||||||||||||||||||||||||||
| # pyre-strict | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import unittest | ||||||||||||||||||||||||||||
| from typing import Callable | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||
| from executorch.backends.cadence.aot.graph_builder import GraphBuilder | ||||||||||||||||||||||||||||
| from executorch.backends.cadence.aot.quantizer.patterns import AddmmPattern | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from executorch.backends.cadence.aot.quantizer.quantizer import ( | ||||||||||||||||||||||||||||
| CadenceAtenQuantizer, | ||||||||||||||||||||||||||||
| CadenceDefaultQuantizer, | ||||||||||||||||||||||||||||
| CadenceQuantizer, | ||||||||||||||||||||||||||||
| CadenceW8A32MixedQuantizer, | ||||||||||||||||||||||||||||
| CadenceWith16BitLinearActivationsQuantizer, | ||||||||||||||||||||||||||||
| CadenceWith16BitMatmulActivationsQuantizer, | ||||||||||||||||||||||||||||
| qconfig_A16, | ||||||||||||||||||||||||||||
| qconfig_A8W8, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| from executorch.exir.pass_base import NodeMetadata | ||||||||||||||||||||||||||||
| from parameterized import parameterized | ||||||||||||||||||||||||||||
| from torch._ops import OpOverload | ||||||||||||||||||||||||||||
| from torchao.quantization.pt2e.quantizer.quantizer import ( | ||||||||||||||||||||||||||||
| Q_ANNOTATION_KEY, | ||||||||||||||||||||||||||||
| QuantizationAnnotation, | ||||||||||||||||||||||||||||
| QuantizationSpec, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Type alias for graph builder functions | ||||||||||||||||||||||||||||
| GraphBuilderFn = Callable[ | ||||||||||||||||||||||||||||
| ["QuantizerAnnotationTest"], tuple[torch.fx.GraphModule, torch.fx.Node] | ||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| class QuantizerAnnotationTest(unittest.TestCase): | ||||||||||||||||||||||||||||
| """Unit tests for verifying quantizer annotations are correctly applied.""" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _build_matmul_graph(self) -> tuple[torch.fx.GraphModule, torch.fx.Node]: | ||||||||||||||||||||||||||||
| """Build a simple graph with a matmul operation.""" | ||||||||||||||||||||||||||||
| builder = GraphBuilder() | ||||||||||||||||||||||||||||
| x = builder.placeholder("x", torch.randn(4, 8)) | ||||||||||||||||||||||||||||
| y = builder.placeholder("y", torch.randn(8, 4)) | ||||||||||||||||||||||||||||
| matmul = builder.call_operator( | ||||||||||||||||||||||||||||
| op=torch.ops.aten.matmul.default, | ||||||||||||||||||||||||||||
| args=(x, y), | ||||||||||||||||||||||||||||
| meta=NodeMetadata( | ||||||||||||||||||||||||||||
| {"source_fn_stack": [("matmul", torch.ops.aten.matmul.default)]} | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| builder.output([matmul]) | ||||||||||||||||||||||||||||
| gm = builder.get_graph_module() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| matmul_nodes = gm.graph.find_nodes( | ||||||||||||||||||||||||||||
| op="call_function", | ||||||||||||||||||||||||||||
| target=torch.ops.aten.matmul.default, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| self.assertEqual(len(matmul_nodes), 1, "Should find exactly one matmul node") | ||||||||||||||||||||||||||||
| return gm, matmul_nodes[0] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _build_linear_graph(self) -> tuple[torch.fx.GraphModule, torch.fx.Node]: | ||||||||||||||||||||||||||||
| """Build a simple graph with a linear operation (no bias).""" | ||||||||||||||||||||||||||||
| builder = GraphBuilder() | ||||||||||||||||||||||||||||
| x = builder.placeholder("x", torch.randn(1, 10)) | ||||||||||||||||||||||||||||
| weight = builder.placeholder("weight", torch.randn(5, 10)) | ||||||||||||||||||||||||||||
| linear = builder.call_operator( | ||||||||||||||||||||||||||||
| op=torch.ops.aten.linear.default, | ||||||||||||||||||||||||||||
| args=(x, weight), | ||||||||||||||||||||||||||||
| meta=NodeMetadata( | ||||||||||||||||||||||||||||
| {"source_fn_stack": [("linear", torch.ops.aten.linear.default)]} | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| builder.output([linear]) | ||||||||||||||||||||||||||||
| gm = builder.get_graph_module() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| linear_nodes = gm.graph.find_nodes( | ||||||||||||||||||||||||||||
| op="call_function", | ||||||||||||||||||||||||||||
| target=torch.ops.aten.linear.default, | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| self.assertEqual(len(linear_nodes), 1, "Should find exactly one linear node") | ||||||||||||||||||||||||||||
| return gm, linear_nodes[0] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @parameterized.expand( | ||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||||
| "matmul_A16", | ||||||||||||||||||||||||||||
| lambda self: self._build_matmul_graph(), | ||||||||||||||||||||||||||||
| CadenceWith16BitMatmulActivationsQuantizer(), | ||||||||||||||||||||||||||||
| torch.ops.aten.matmul.default, | ||||||||||||||||||||||||||||
| qconfig_A16.output_activation, | ||||||||||||||||||||||||||||
| # For matmul, both inputs are activations | ||||||||||||||||||||||||||||
| [qconfig_A16.input_activation, qconfig_A16.input_activation], | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||||
| "linear_A16", | ||||||||||||||||||||||||||||
| lambda self: self._build_linear_graph(), | ||||||||||||||||||||||||||||
| CadenceWith16BitLinearActivationsQuantizer(), | ||||||||||||||||||||||||||||
| torch.ops.aten.linear.default, | ||||||||||||||||||||||||||||
| qconfig_A16.output_activation, | ||||||||||||||||||||||||||||
| # For linear: [input_activation, weight] | ||||||||||||||||||||||||||||
| [qconfig_A16.input_activation, qconfig_A16.weight], | ||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
| def test_quantizer_annotation( | ||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||
| name: str, | ||||||||||||||||||||||||||||
| graph_builder_fn: GraphBuilderFn, | ||||||||||||||||||||||||||||
| quantizer: CadenceQuantizer, | ||||||||||||||||||||||||||||
| target: OpOverload, | ||||||||||||||||||||||||||||
| expected_output_qspec: QuantizationSpec, | ||||||||||||||||||||||||||||
| expected_input_qspecs: list[QuantizationSpec], | ||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||
| """Parameterized test for quantizer annotations.""" | ||||||||||||||||||||||||||||
| gm, op_node = graph_builder_fn(self) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| quantizer.annotate(gm) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| annotation: QuantizationAnnotation = op_node.meta[Q_ANNOTATION_KEY] | ||||||||||||||||||||||||||||
| self.assertTrue(annotation._annotated) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Verify output annotation | ||||||||||||||||||||||||||||
| self.assertEqual(annotation.output_qspec, expected_output_qspec) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Verify input annotations | ||||||||||||||||||||||||||||
| # Build actual_specs in the fixed order defined by op_node.args | ||||||||||||||||||||||||||||
| self.assertEqual(len(annotation.input_qspec_map), len(expected_input_qspecs)) | ||||||||||||||||||||||||||||
| actual_specs = [ | ||||||||||||||||||||||||||||
| annotation.input_qspec_map[op_node.args[i]] | ||||||||||||||||||||||||||||
| for i in range(len(expected_input_qspecs)) | ||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||
|
Comment on lines
+133
to
+136
|
||||||||||||||||||||||||||||
| actual_specs = [ | |
| annotation.input_qspec_map[op_node.args[i]] | |
| for i in range(len(expected_input_qspecs)) | |
| ] | |
| actual_specs = [] | |
| for i in range(len(expected_input_qspecs)): | |
| key = op_node.args[i] | |
| if key not in annotation.input_qspec_map: | |
| raise KeyError( | |
| f"Key {key!r} not found in input_qspec_map. " | |
| f"Available keys: {list(annotation.input_qspec_map.keys())}" | |
| ) | |
| actual_specs.append(annotation.input_qspec_map[key]) |
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.
The
targetparameter is unused in the test function. Consider removing it from the parameter list (lines 94 and 103) if it's not needed, or use it to validate thatop_node.targetmatches the expected target operation for additional test robustness.