Skip to content

Commit ce27577

Browse files
jerryzh168Svetlana Karslioglu
and
Svetlana Karslioglu
authored
[quant] Add IR related recommendations for Quantizer tutorial (#2608)
* [quant] Add IR related recommendations for Quantizer tutorial --------- Co-authored-by: Svetlana Karslioglu <svekars@fb.com>
1 parent 4e25f97 commit ce27577

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

prototype_source/pt2e_quantizer.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,76 @@ functions that are used in the example:
302302
`get_bias_qspec <https://github.com/pytorch/pytorch/blob/47cfcf566ab76573452787335f10c9ca185752dc/torch/ao/quantization/_pt2e/quantizer/utils.py#L53>`__
303303
can be used to get the ``QuantizationSpec`` from ``QuantizationConfig`` for a specific pattern.
304304

305+
A Note on IR for PT2E Quantization Flow
306+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
307+
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?
308+
309+
Motivation: Problem of Matching ``aten`` IR directly
310+
--------------------------------------------------------
311+
312+
The most straightforward way might be matching ``aten`` IR directly.
313+
314+
Example::
315+
316+
for n in gm.graph.nodes:
317+
if n.op != "call_function" or n.target not in [
318+
torch.ops.aten.relu.default,
319+
torch.ops.aten.relu_.default,
320+
]:
321+
continue
322+
relu_node = n
323+
maybe_conv_node = n.args[0]
324+
if (
325+
not isinstance(maybe_conv_node, Node)
326+
or maybe_conv_node.op != "call_function"
327+
or maybe_conv_node.target
328+
not in [
329+
torch.ops.aten.conv1d.default,
330+
torch.ops.aten.conv2d.default,
331+
]
332+
):
333+
continue
334+
335+
# annotate conv and relu nodes
336+
...
337+
338+
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.
339+
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.
343+
344+
Example::
345+
346+
def conv_relu_pattern(input, weight, bias):
347+
conv = torch.nn.functional.conv2d(input, weight, bias)
348+
output = torch.nn.functional.relu(conv)
349+
# returns an additional dict that includes a map from name to node that we want to annotate
350+
return relu, {"input": input, "weight": weight, "bias": bias, "output": output}
351+
352+
matcher = SubgraphMatcherWithNameNodeMap(conv_relu_pattern)
353+
matches = matcher.match(model)
354+
for match in matches:
355+
# find input and output of the pattern
356+
# annotate the nodes
357+
name_node_map = match.name_node_map
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"] = ...
366+
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.
368+
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.
370+
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.
372+
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.
374+
305375
Conclusion
306376
^^^^^^^^^^^^^^^^^^^
307377

0 commit comments

Comments
 (0)