Skip to content

Commit c86257a

Browse files
committed
addressed comments
1 parent 29becc9 commit c86257a

File tree

1 file changed

+21
-59
lines changed

1 file changed

+21
-59
lines changed

prototype_source/pt2e_quantizer.rst

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ A Note on IR for PT2E Quantization Flow
306306
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
307307
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?
308308

309-
1. Matching ``aten`` IR directly
310-
------------------------------------
309+
Motivation: Problem of Matching ``aten`` IR directly
310+
--------------------------------------------------------
311311

312312
The most straightforward way might be matching ``aten`` IR directly.
313313

@@ -337,78 +337,40 @@ Example::
337337

338338
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.
339339

340-
2. Using ``SubgraphMatcher``
341-
--------------------------------
342-
Because of this, we recommend people to recognize the pattern through ``SubgraphMatcher``, 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.
340+
Recommendation: Use ``SubgraphMatcherWithNameNodeMap`` for pattern matching
341+
-----------------------------------------------------------------------------
342+
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.
343343

344344
Example::
345345

346-
def conv_relu_pattern(x, weight, bias):
347-
conv = torch.nn.functional.conv2d(x, weight, bias)
348-
relu = torch.nn.functional.relu(conv)
349-
return relu
350-
351-
matcher = SubgraphMatcher(conv_relu_pattern)
352-
matches = matcher.match(model)
353-
for match in matches:
354-
# find input and output of the pattern
355-
# annotate the nodes
356-
inputs, output = _find_input_and_output(match)
357-
inputs[0].users[0].meta["quantization_annotation"] = ...
358-
inputs[1].users[0].meta["quantization_annotation"] = ...
359-
output.meta["quantization_annotation"] = ...
360-
361-
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.
362-
363-
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.
364-
365-
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.
366-
367-
3. Using ``SubgraphMatcherWithNameNodeMap``
368-
----------------------------------------------
369-
We also introduced a different SubgraphMatcher util called ``SubgraphMatcherWithNameNodeMap`` to make it easier to query the nodes that people want to annotate.
370-
371-
Example::
372-
373-
def conv_relu_pattern(x, weight, bias):
374-
conv = torch.nn.functional.conv2d(x, weight, bias)
375-
relu = torch.nn.functional.relu(conv)
346+
def conv_relu_pattern(input, weight, bias):
347+
conv = torch.nn.functional.conv2d(input, weight, bias)
348+
output = torch.nn.functional.relu(conv)
376349
# returns an additional dict that includes a map from name to node that we want to annotate
377-
return relu, {"conv": conv, "relu": relu, "x": x, "weight": weight, "bias": bias}
350+
return relu, {"input": input, "weight": weight, "bias": bias, "output": output}
378351

379352
matcher = SubgraphMatcherWithNameNodeMap(conv_relu_pattern)
380353
matches = matcher.match(model)
381354
for match in matches:
382355
# find input and output of the pattern
383356
# annotate the nodes
384357
name_node_map = match.name_node_map
385-
name_node_map["conv"].meta["quantization_annotation"] = ...
386-
name_node_map["relu"].meta["quantization_annotation"] = ...
387-
388-
This should be easier to use than the original ``SubgraphMatcher`` around finding the node we want to annotate or use. However, it may not work if some of the operators are captured as multiple ops. For example, if ``torch.nn.functional.conv2d`` is captured as multiple ops, then the inputs ``x``, ``weight``, ``bias`` may not be the direct inputs to the final operator marked by ``conv``, in this case we'll need to revert to the same way of access mentioned in the previous example.
389-
390-
Example::
358+
input_node = name_node_map["input"]
359+
weight_node = name_node_map["weight"]
360+
bias_node = name_node_map["bias"]
361+
output_node = name_node_map["relu"]
362+
input_node.users[0].meta["quantization_annotation"] = ...
363+
weight_node.users[0].meta["quantization_annotation"] = ...
364+
bias_node.users[0].meta["quantization_annotation"] = ...
365+
output_node.meta["quantization_annotation"] = ...
391366

392-
for match in matches:
393-
# find input and output of the pattern
394-
# annotate the nodes
395-
name_node_map = match.name_node_map
396-
input_x = name_node_map["x"]
397-
# since input_x may not be the input of name_node_map["conv"]
398-
# we'll get the user nodes of this op and annotate that instead
399-
input_x.users[0].meta["quantization_annotation"] = ...
400-
...
401-
402-
403-
Similar to ``SubgraphMatcher``, we also need to make sure to capture the PyTorch pattern with different example inputs to cover all possible ``aten`` IR variants.
367+
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.
404368

405-
4. General Recommentation
406-
----------------------------------------------
407-
Given the above UX and constraints/caveats, we recommend people to start with option 3. and make sure the first operator in the pattern are functional ops that map to one aten op (e.g. F.conv2d, F.linear), and provide enough variants of example inputs. (We may provide some (pattern, list of example_inputs) so people can just use them directly in the future)
369+
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.
408370

409-
If people are uncertain if some functional operator is going to be traced into multiple aten operators then they can pick option 2.
371+
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.
410372

411-
We would not recommend option 1. since that's the least stable in all three options.
373+
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.
412374

413375
Conclusion
414376
^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)