Skip to content

Commit

Permalink
quantization: autogenerate quantization backend configs for documenta…
Browse files Browse the repository at this point in the history
…tion (pytorch#75126)

Summary:
Pull Request resolved: pytorch#75126

Quantization has a high volume of configurations of how to quantize an
op for a reference model representation which is useful for a lowering
step for a backend.  An example of this is

```
 {'dtype_configs': [{'input_dtype': torch.quint8,
										 'output_dtype': torch.quint8}],
	'observation_type': <ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT: 0>,
	'pattern': <class 'torch.nn.modules.conv.ConvTranspose1d'>},
```

These configs are checked into master, and they are created with Python functions.
Therefore, there is no easy way for the user to see what the configs actually
are without running some Python code.

This PR is one approach to document these configs. Here is what this is doing:
1. during documentation build, write a text file of the configs
2. render that text file on a quantization page, with some additional context

In the future, this could be extended to autogenerate better looking tables
such as: op support per backend and dtype, op support per valid quantization settings per backend,
etc.

Test Plan:
```
cd docs
make html
cd html
python -m http.server 8000
// render http://[::]:8000/quantization-backend-configuration.html
// it renders correctly
```

Reviewed By: ejguan

Differential Revision: D35365461

Pulled By: vkuzo

fbshipit-source-id: d60f776ccb57da9db3d09550e4b27bd5e725635a
(cherry picked from commit 14865c0)
  • Loading branch information
vkuzo authored and pytorchmergebot committed Apr 4, 2022
1 parent 83400e8 commit 74b23b2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ scripts/release_notes/*.json
compile_commands.json
*.egg-info/
docs/source/scripts/activation_images/
docs/source/scripts/quantization_backend_configs/

## General

Expand Down
1 change: 1 addition & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ help:

figures:
@$(PYCMD) source/scripts/build_activation_images.py
@$(PYCMD) source/scripts/build_quantization_configs.py

docset: html
doc2dash --name $(SPHINXPROJ) --icon $(SOURCEDIR)/_static/img/pytorch-logo-flame.png --enable-js --online-redirect-url https://pytorch.org/docs/ --force $(BUILDDIR)/html/
Expand Down
20 changes: 20 additions & 0 deletions docs/source/quantization-backend-configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Quantization Backend Configuration
----------------------------------

FX Graph Mode Quantization allows the user to configure various
quantization behaviors of an op in order to match the expectation
of their backend.

In the future, this document will contain a detailed spec of
these configurations.


Default values for native configurations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Below is the output of the configuration for quantization of ops
in fbgemm and qnnpack (PyTorch's default quantized backends).

Results:

.. literalinclude:: scripts/quantization_backend_configs/default_backend_config.txt
11 changes: 11 additions & 0 deletions docs/source/quantization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,17 @@ and supported quantized modules and functions.
torch.ao.ns._numeric_suite
torch.ao.ns._numeric_suite_fx

Quantization Backend Configuration
----------------------------------

The :doc:`Quantization Backend Configuration <quantization-backend-configuration>` contains documentation
on how to configure the quantization workflows for various backends.

.. toctree::
:hidden:

quantization-backend-configuration

Quantized Tensors
---------------------------------------

Expand Down
23 changes: 23 additions & 0 deletions docs/source/scripts/build_quantization_configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
This script will generate default values of quantization configs.
These are for use in the documentation.
"""

from torch.ao.quantization.fx.backend_config import get_native_backend_config_dict
import os.path
from pprint import pprint


# Create a directory for the images, if it doesn't exist
QUANTIZATION_BACKEND_CONFIG_IMAGE_PATH = os.path.join(
os.path.realpath(os.path.join(__file__, "..")),
"quantization_backend_configs"
)

if not os.path.exists(QUANTIZATION_BACKEND_CONFIG_IMAGE_PATH):
os.mkdir(QUANTIZATION_BACKEND_CONFIG_IMAGE_PATH)

output_path = os.path.join(QUANTIZATION_BACKEND_CONFIG_IMAGE_PATH, "default_backend_config.txt")

with open(output_path, "w") as f:
pprint(get_native_backend_config_dict(), stream=f)

0 comments on commit 74b23b2

Please sign in to comment.