Skip to content

Commit aa12743

Browse files
Merge PTQ/QAT tutorial for x86Inductor
1 parent 9bdfcac commit aa12743

File tree

3 files changed

+35
-83
lines changed

3 files changed

+35
-83
lines changed

prototype_source/prototype_index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ Prototype features are not available as part of binary distributions like PyPI o
8989
:link: ../prototype/pt2e_quant_qat.html
9090
:tags: Quantization
9191

92+
.. customcarditem::
93+
:header: PyTorch 2 Export Quantization with X86 Backend through Inductor
94+
:card_description: Learn how to use PT2 Export Quantization with X86 Backend through Inductor.
95+
:image: ../_static/img/thumbnails/cropped/generic-pytorch-logo.png
96+
:link: ../prototype/pt2e_quant_x86_inductor.html
97+
:tags: Quantization
9298

9399
.. Sparsity
94100

prototype_source/pt2e_quant_qat_x86_inductor.rst

Lines changed: 0 additions & 77 deletions
This file was deleted.

prototype_source/pt2e_quant_ptq_x86_inductor.rst renamed to prototype_source/pt2e_quant_x86_inductor.rst

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PyTorch 2 Export Post Training Quantization with X86 Backend through Inductor
1+
PyTorch 2 Export Quantization with X86 Backend through Inductor
22
========================================================================================
33

44
**Author**: `Leslie Fang <https://github.com/leslie-fang-intel>`_, `Weiwen Xia <https://github.com/Xia-Weiwen>`_, `Jiong Gong <https://github.com/jgong5>`_, `Jerry Zhang <https://github.com/jerryzh168>`_
@@ -7,6 +7,7 @@ Prerequisites
77
^^^^^^^^^^^^^^^
88

99
- `PyTorch 2 Export Post Training Quantization <https://pytorch.org/tutorials/prototype/pt2e_quant_ptq.html>`_
10+
- `PyTorch 2 Export Quantization-Aware Training tutorial <https://pytorch.org/tutorials/prototype/pt2e_quant_qat.html>`_
1011
- `TorchInductor and torch.compile concepts in PyTorch <https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html>`_
1112

1213
Introduction
@@ -15,14 +16,15 @@ Introduction
1516
This tutorial introduces the steps for utilizing the PyTorch 2 Export Quantization flow to generate a quantized model customized
1617
for the x86 inductor backend and explains how to lower the quantized model into the inductor.
1718

18-
The new quantization 2 flow uses the PT2 Export to capture the model into a graph and perform quantization transformations on top of the ATen graph. This approach is expected to have significantly higher model coverage, better programmability, and a simplified UX.
19+
The new quantization 2 flow uses the PT2 Export to capture the model into a graph and perform quantization transformations on top of the ATen graph.
20+
This approach is expected to have significantly higher model coverage, better programmability, and a simplified UX.
1921
TorchInductor is the new compiler backend that compiles the FX Graphs generated by TorchDynamo into optimized C++/Triton kernels.
2022

2123
This flow of quantization 2 with Inductor mainly includes three steps:
2224

2325
- Step 1: Capture the FX Graph from the eager Model based on the `torch export mechanism <https://pytorch.org/docs/main/export.html>`_.
2426
- Step 2: Apply the Quantization flow based on the captured FX Graph, including defining the backend-specific quantizer, generating the prepared model with observers,
25-
performing the prepared model's calibration, and converting the prepared model into the quantized model.
27+
performing the prepared model's calibration or quantization-aware training, and converting the prepared model into the quantized model.
2628
- Step 3: Lower the quantized model into inductor with the API ``torch.compile``.
2729

2830
The high-level architecture of this flow could look like this:
@@ -82,6 +84,8 @@ We will start by performing the necessary imports, capturing the FX Graph from t
8284
model = models.__dict__[model_name](pretrained=True)
8385

8486
# Set the model to eval mode
87+
# Only apply it for post-training static quantization
88+
# Skip this step for quantization-aware training
8589
model = model.eval()
8690

8791
# Create the data, using the dummy data here as an example
@@ -115,26 +119,44 @@ Next, we will have the FX Module to be quantized.
115119
After we capture the FX Module to be quantized, we will import the Backend Quantizer for X86 CPU and configure how to
116120
quantize the model.
117121

122+
For post-training static quantization:
123+
118124
::
119125

120126
quantizer = X86InductorQuantizer()
121127
quantizer.set_global(xiq.get_default_x86_inductor_quantization_config())
122128

129+
For quantization-aware training:
130+
131+
::
132+
133+
quantizer = X86InductorQuantizer()
134+
quantizer.set_global(xiq.get_default_x86_inductor_quantization_config(is_qat=True))
135+
123136
.. note::
124137

125138
The default quantization configuration in ``X86InductorQuantizer`` uses 8-bits for both activations and weights.
126139
When Vector Neural Network Instruction is not available, the oneDNN backend silently chooses kernels that assume
127140
`multiplications are 7-bit x 8-bit <https://oneapi-src.github.io/oneDNN/dev_guide_int8_computations.html#inputs-of-mixed-type-u8-and-s8>`_. In other words, potential
128141
numeric saturation and accuracy issue may happen when running on CPU without Vector Neural Network Instruction.
129142

130-
After we import the backend-specific Quantizer, we will prepare the model for post-training quantization.
131-
``prepare_pt2e`` folds BatchNorm operators into preceding Conv2d operators, and inserts observers in appropriate places in the model.
143+
After we import the backend-specific Quantizer, we will prepare the model for post-training quantization or quantization-aware training.
144+
145+
For post-training static quantization, ``prepare_pt2e`` folds BatchNorm operators into preceding Conv2d operators, and inserts observers in appropriate places in the model.
132146

133147
::
134148

135149
prepared_model = prepare_pt2e(exported_model, quantizer)
136150

137-
Now, we will calibrate the ``prepared_model`` after the observers are inserted in the model.
151+
For quantization-aware training:
152+
153+
::
154+
155+
prepared_model = prepare_qat_pt2e(exported_model, quantizer)
156+
157+
158+
Now, we will do calibration for post-training static quantization or quantization-aware training. Here is the example code
159+
for post-training static quantization. The example code omits quantization-aware training for simplicity.
138160

139161
::
140162

@@ -154,6 +176,7 @@ Finally, we will convert the calibrated Model to a quantized Model. ``convert_pt
154176
::
155177

156178
converted_model = convert_pt2e(prepared_model)
179+
torch.ao.quantization.move_exported_model_to_eval(converted_model)
157180

158181
After these steps, we finished running the quantization flow and we will get the quantized model.
159182

0 commit comments

Comments
 (0)