Skip to content

[quant] Fix fx quant ptq static tutorial #1457

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
merged 3 commits into from
Apr 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions prototype_source/fx_graph_mode_ptq_static.rst
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ The purpose for calibration is to run through some sample examples that is repre
the statistics of the Tensors and we can later use this information to calculate quantization parameters.

.. code:: python

def calibrate(model, data_loader):
model.eval()
with torch.no_grad():
Expand All @@ -320,17 +321,19 @@ the statistics of the Tensors and we can later use this information to calculate

7. Convert the Model to a Quantized Model
-----------------------------------------
``convert_fx`` takes a calibrated model and produces a quantized model.
``convert_fx`` takes a calibrated model and produces a quantized model.

.. code:: python
quantized_model = convert_fx(prepared_model)

quantized_model = convert_fx(prepared_model)
print(quantized_model)

8. Evaluation
-------------
We can now print the size and accuracy of the quantized model.

.. code:: python

print("Size of model before quantization")
print_size_of_model(float_model)
print("Size of model after quantization")
Expand Down Expand Up @@ -372,6 +375,7 @@ we'll first call fuse explicitly to fuse the conv and bn in the model:
Note that ``fuse_fx`` only works in eval mode.

.. code:: python

fused = fuse_fx(float_model)

conv1_weight_after_fuse = fused.conv1[0].weight[0]
Expand All @@ -383,6 +387,7 @@ Note that ``fuse_fx`` only works in eval mode.
--------------------------------------------------------------------

.. code:: python

scripted_float_model_file = "resnet18_scripted.pth"

print("Size of baseline model")
Expand All @@ -397,6 +402,7 @@ quantized in eager mode. FX graph mode and eager mode produce very similar quant
so the expectation is that the accuracy and speedup are similar as well.

.. code:: python

print("Size of Fx graph mode quantized model")
print_size_of_model(quantized_model)
top1, top5 = evaluate(quantized_model, criterion, data_loader_test)
Expand Down