-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[quant] Add IR related recommendations for Quantizer tutorial #2608
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
Merged
Changes from all commits
Commits
Show all changes
5 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 |
---|---|---|
|
@@ -302,6 +302,76 @@ functions that are used in the example: | |
`get_bias_qspec <https://github.com/pytorch/pytorch/blob/47cfcf566ab76573452787335f10c9ca185752dc/torch/ao/quantization/_pt2e/quantizer/utils.py#L53>`__ | ||
can be used to get the ``QuantizationSpec`` from ``QuantizationConfig`` for a specific pattern. | ||
|
||
A Note on IR for PT2E Quantization Flow | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
IR means the intermediate representation of the model, for example, ``torch`` IR (``torch.nn`` modules, ``torch.nn.functional`` ops) or ``aten`` IR (``torch.ops.aten.linear``, ...). PT2E Quantization Flow is using pre autograd aten IR (the output of `torch.export` API) so that we support training. As is shown before, we need to match the operator or operator patterns before we can attach annotations on them, So the question is how do we match the pattern? | ||
|
||
Motivation: Problem of Matching ``aten`` IR directly | ||
-------------------------------------------------------- | ||
|
||
The most straightforward way might be matching ``aten`` IR directly. | ||
|
||
Example:: | ||
|
||
for n in gm.graph.nodes: | ||
if n.op != "call_function" or n.target not in [ | ||
torch.ops.aten.relu.default, | ||
torch.ops.aten.relu_.default, | ||
]: | ||
continue | ||
relu_node = n | ||
maybe_conv_node = n.args[0] | ||
if ( | ||
not isinstance(maybe_conv_node, Node) | ||
or maybe_conv_node.op != "call_function" | ||
or maybe_conv_node.target | ||
not in [ | ||
torch.ops.aten.conv1d.default, | ||
torch.ops.aten.conv2d.default, | ||
] | ||
): | ||
continue | ||
|
||
# annotate conv and relu nodes | ||
... | ||
|
||
However one problem for using this IR is that the representation might change if the PyTorch implementation for modules or functional ops changed. But this could be unexpected since modeling users typically assume that when the eager mode model code doesn't change, they should get the same model representation after program capture as well. One concrete effect for this problem is that if a ``Quantizer`` do annotations based on recognizing ``aten`` IR patterns, then it may fail to recognzing the pattern after PyTorch version update, and the same eager mode floating point may be left unquantized. | ||
jerryzh168 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Recommendation: Use ``SubgraphMatcherWithNameNodeMap`` for pattern matching | ||
----------------------------------------------------------------------------- | ||
Because of this, we recommend people to recognize the pattern through ``SubgraphMatcherWithNameNodeMap`` (an improved version of ``SubgraphMatcher`` that makes it easier to query the nodes that people want to annotate), through capturing a ``torch`` IR pattern (with the same program capture used for capturing the floating point model), instead of using the ``aten`` IR pattern directly. | ||
|
||
Example:: | ||
jerryzh168 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def conv_relu_pattern(input, weight, bias): | ||
conv = torch.nn.functional.conv2d(input, weight, bias) | ||
output = torch.nn.functional.relu(conv) | ||
# returns an additional dict that includes a map from name to node that we want to annotate | ||
return relu, {"input": input, "weight": weight, "bias": bias, "output": output} | ||
|
||
matcher = SubgraphMatcherWithNameNodeMap(conv_relu_pattern) | ||
jerryzh168 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
matches = matcher.match(model) | ||
for match in matches: | ||
# find input and output of the pattern | ||
# annotate the nodes | ||
name_node_map = match.name_node_map | ||
input_node = name_node_map["input"] | ||
weight_node = name_node_map["weight"] | ||
bias_node = name_node_map["bias"] | ||
output_node = name_node_map["relu"] | ||
input_node.users[0].meta["quantization_annotation"] = ... | ||
weight_node.users[0].meta["quantization_annotation"] = ... | ||
bias_node.users[0].meta["quantization_annotation"] = ... | ||
output_node.meta["quantization_annotation"] = ... | ||
|
||
With this, the ``Quantizer`` will still be valid even when the implementation for nn modules and functionals changes, the ``aten`` IR for floating point model will change, but since we capture the pattern again instead of hardcoding the ``aten`` IR for the pattern, we'll get the updated ``aten`` IR as well and will still be able to match the pattern. | ||
|
||
One caveat is that if inputs of the pattern has multiple users, we don't have a good way to identify which user node we want to annotate except for checking the aten op target. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if I understand this issue clearly?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes that's correct |
||
|
||
Another caveat is that we need to make sure we have an exhaustive list of examples (e.g. 2D, 3D, 4D inputs, real v.s. symbolic inputs, training=True v.s. training=False etc.) for the pattern to make sure cover different possible ``aten`` IR outcomes captured from the ``torch`` IR pattern. | ||
|
||
Note: We may provide some (pattern, list of example_inputs) or some pre-generated matcher object so people can just use them directly in the future. | ||
|
||
Conclusion | ||
^^^^^^^^^^^^^^^^^^^ | ||
|
||
|
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.
Uh oh!
There was an error while loading. Please reload this page.