Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/pruning/sparsifier/__pycache__/weight_norm_sparsifier.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/__init__.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/_correct_bias.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/_equalize.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/_learnable_fake_quantize.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/fake_quantize.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/fuse_modules.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/fuser_method_mappings.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/observer.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/qconfig.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/qconfig_mapping.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quant_type.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantization_mappings.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantize.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantize_fx.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantize_jit.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantize_pt2e.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/stubs.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/utils.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__init__.py +30 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/__init__.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/_common_operator_config_utils.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/_qnnpack_pt2e.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/backend_config.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/executorch.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/fbgemm.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/native.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/observation_type.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/onednn.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/qnnpack.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/tensorrt.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/utils.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/x86.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/_common_operator_config_utils.py +794 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/_qnnpack_pt2e.py +181 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/backend_config.py +749 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/executorch.py +498 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/fbgemm.py +129 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/native.py +231 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/observation_type.py +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/onednn.py +648 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/qnnpack.py +171 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/tensorrt.py +98 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/utils.py +314 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/x86.py +126 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__init__.py +3 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__pycache__/__init__.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__pycache__/_decomposed.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__pycache__/_equalize.cpython-312.pyc +0 -0
- tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__pycache__/_lower_to_native_backend.cpython-312.pyc +0 -0
tool_server/.venv/lib/python3.12/site-packages/torch/ao/pruning/sparsifier/__pycache__/weight_norm_sparsifier.cpython-312.pyc
ADDED
|
Binary file (10.5 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (6.79 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/_correct_bias.cpython-312.pyc
ADDED
|
Binary file (6.82 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/_equalize.cpython-312.pyc
ADDED
|
Binary file (11.9 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/_learnable_fake_quantize.cpython-312.pyc
ADDED
|
Binary file (11.8 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/fake_quantize.cpython-312.pyc
ADDED
|
Binary file (25.6 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/fuse_modules.cpython-312.pyc
ADDED
|
Binary file (7.13 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/fuser_method_mappings.cpython-312.pyc
ADDED
|
Binary file (13.5 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/observer.cpython-312.pyc
ADDED
|
Binary file (84 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/qconfig.cpython-312.pyc
ADDED
|
Binary file (20.8 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/qconfig_mapping.cpython-312.pyc
ADDED
|
Binary file (15.3 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quant_type.cpython-312.pyc
ADDED
|
Binary file (1.33 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantization_mappings.cpython-312.pyc
ADDED
|
Binary file (18.1 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantize.cpython-312.pyc
ADDED
|
Binary file (30.3 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantize_fx.cpython-312.pyc
ADDED
|
Binary file (32.9 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantize_jit.cpython-312.pyc
ADDED
|
Binary file (17.8 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/quantize_pt2e.cpython-312.pyc
ADDED
|
Binary file (10.3 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/stubs.cpython-312.pyc
ADDED
|
Binary file (3.54 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/__pycache__/utils.cpython-312.pyc
ADDED
|
Binary file (34.5 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__init__.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .backend_config import (
|
| 2 |
+
BackendConfig,
|
| 3 |
+
BackendPatternConfig,
|
| 4 |
+
DTypeConfig,
|
| 5 |
+
DTypeWithConstraints,
|
| 6 |
+
ObservationType,
|
| 7 |
+
)
|
| 8 |
+
from .executorch import get_executorch_backend_config
|
| 9 |
+
from .fbgemm import get_fbgemm_backend_config
|
| 10 |
+
from .native import get_native_backend_config, get_native_backend_config_dict
|
| 11 |
+
from .onednn import get_onednn_backend_config
|
| 12 |
+
from .qnnpack import get_qnnpack_backend_config
|
| 13 |
+
from .tensorrt import get_tensorrt_backend_config, get_tensorrt_backend_config_dict
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
__all__ = [
|
| 17 |
+
"get_fbgemm_backend_config",
|
| 18 |
+
"get_native_backend_config",
|
| 19 |
+
"get_native_backend_config_dict",
|
| 20 |
+
"get_qnnpack_backend_config",
|
| 21 |
+
"get_tensorrt_backend_config",
|
| 22 |
+
"get_tensorrt_backend_config_dict",
|
| 23 |
+
"get_executorch_backend_config",
|
| 24 |
+
"BackendConfig",
|
| 25 |
+
"BackendPatternConfig",
|
| 26 |
+
"DTypeConfig",
|
| 27 |
+
"DTypeWithConstraints",
|
| 28 |
+
"ObservationType",
|
| 29 |
+
"get_onednn_backend_config",
|
| 30 |
+
]
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (911 Bytes). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/_common_operator_config_utils.cpython-312.pyc
ADDED
|
Binary file (32.7 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/_qnnpack_pt2e.cpython-312.pyc
ADDED
|
Binary file (6.86 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/backend_config.cpython-312.pyc
ADDED
|
Binary file (32.5 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/executorch.cpython-312.pyc
ADDED
|
Binary file (18.3 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/fbgemm.cpython-312.pyc
ADDED
|
Binary file (3.77 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/native.cpython-312.pyc
ADDED
|
Binary file (6.37 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/observation_type.cpython-312.pyc
ADDED
|
Binary file (214 Bytes). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/onednn.cpython-312.pyc
ADDED
|
Binary file (19.3 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/qnnpack.cpython-312.pyc
ADDED
|
Binary file (4.3 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/tensorrt.cpython-312.pyc
ADDED
|
Binary file (3.19 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/utils.cpython-312.pyc
ADDED
|
Binary file (13.5 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/__pycache__/x86.cpython-312.pyc
ADDED
|
Binary file (3.73 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/_common_operator_config_utils.py
ADDED
|
@@ -0,0 +1,794 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: allow-untyped-defs
|
| 2 |
+
import copy
|
| 3 |
+
import operator
|
| 4 |
+
from collections import namedtuple
|
| 5 |
+
from typing import Callable, Union
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import torch.ao.nn.intrinsic as nni
|
| 9 |
+
import torch.ao.nn.intrinsic.qat as nniqat
|
| 10 |
+
import torch.ao.nn.qat as nnqat
|
| 11 |
+
import torch.ao.nn.quantized.reference as nnqr
|
| 12 |
+
import torch.nn as nn
|
| 13 |
+
import torch.nn.functional as F
|
| 14 |
+
from torch.ao.quantization.fuser_method_mappings import (
|
| 15 |
+
_sequential_wrapper2,
|
| 16 |
+
fuse_conv_bn,
|
| 17 |
+
fuse_conv_bn_relu,
|
| 18 |
+
fuse_convtranspose_bn,
|
| 19 |
+
fuse_linear_bn,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
from .backend_config import (
|
| 23 |
+
BackendPatternConfig,
|
| 24 |
+
DTypeConfig,
|
| 25 |
+
DTypeWithConstraints,
|
| 26 |
+
ObservationType,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
__all__: list[str] = []
|
| 31 |
+
|
| 32 |
+
# TODO: rename to be more explicit, e.g. qat_conv_relu
|
| 33 |
+
_ConvMetadata = namedtuple(
|
| 34 |
+
"_ConvMetadata",
|
| 35 |
+
[
|
| 36 |
+
"root",
|
| 37 |
+
"transpose",
|
| 38 |
+
"bn",
|
| 39 |
+
"reference",
|
| 40 |
+
"transpose_reference",
|
| 41 |
+
"fused_conv_relu",
|
| 42 |
+
"fused_conv_bn",
|
| 43 |
+
"fused_conv_bn_relu",
|
| 44 |
+
"qat",
|
| 45 |
+
"relu_qat",
|
| 46 |
+
"bn_qat",
|
| 47 |
+
"bn_relu_qat",
|
| 48 |
+
"func",
|
| 49 |
+
"func_transpose",
|
| 50 |
+
],
|
| 51 |
+
)
|
| 52 |
+
_Conv1dMetadata = _ConvMetadata(
|
| 53 |
+
nn.Conv1d,
|
| 54 |
+
nn.ConvTranspose1d,
|
| 55 |
+
nn.BatchNorm1d,
|
| 56 |
+
nnqr.Conv1d,
|
| 57 |
+
nnqr.ConvTranspose1d,
|
| 58 |
+
nni.ConvReLU1d,
|
| 59 |
+
nni.ConvBn1d,
|
| 60 |
+
nni.ConvBnReLU1d,
|
| 61 |
+
nnqat.Conv1d,
|
| 62 |
+
nniqat.ConvReLU1d,
|
| 63 |
+
nniqat.ConvBn1d,
|
| 64 |
+
nniqat.ConvBnReLU1d,
|
| 65 |
+
F.conv1d,
|
| 66 |
+
F.conv_transpose1d,
|
| 67 |
+
)
|
| 68 |
+
_Conv2dMetadata = _ConvMetadata(
|
| 69 |
+
nn.Conv2d,
|
| 70 |
+
nn.ConvTranspose2d,
|
| 71 |
+
nn.BatchNorm2d,
|
| 72 |
+
nnqr.Conv2d,
|
| 73 |
+
nnqr.ConvTranspose2d,
|
| 74 |
+
nni.ConvReLU2d,
|
| 75 |
+
nni.ConvBn2d,
|
| 76 |
+
nni.ConvBnReLU2d,
|
| 77 |
+
nnqat.Conv2d,
|
| 78 |
+
nniqat.ConvReLU2d,
|
| 79 |
+
nniqat.ConvBn2d,
|
| 80 |
+
nniqat.ConvBnReLU2d,
|
| 81 |
+
F.conv2d,
|
| 82 |
+
F.conv_transpose2d,
|
| 83 |
+
)
|
| 84 |
+
_Conv3dMetadata = _ConvMetadata(
|
| 85 |
+
nn.Conv3d,
|
| 86 |
+
nn.ConvTranspose3d,
|
| 87 |
+
nn.BatchNorm3d,
|
| 88 |
+
nnqr.Conv3d,
|
| 89 |
+
nnqr.ConvTranspose3d,
|
| 90 |
+
nni.ConvReLU3d,
|
| 91 |
+
nni.ConvBn3d,
|
| 92 |
+
nni.ConvBnReLU3d,
|
| 93 |
+
nnqat.Conv3d,
|
| 94 |
+
nniqat.ConvReLU3d,
|
| 95 |
+
nniqat.ConvBn3d,
|
| 96 |
+
nniqat.ConvBnReLU3d,
|
| 97 |
+
F.conv3d,
|
| 98 |
+
F.conv_transpose3d,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# Add constraints for fixed qparams ops like sigmoid and tanh to ensure values
|
| 102 |
+
# fall within the proper ranges, e.g. [0, 1] for sigmoid, [-1, 1] for tanh
|
| 103 |
+
_FIXED_QPARAM_OP_0TO1_CONSTRAINTS = DTypeWithConstraints(
|
| 104 |
+
dtype=torch.quint8,
|
| 105 |
+
quant_min_lower_bound=0,
|
| 106 |
+
quant_max_upper_bound=255,
|
| 107 |
+
scale_exact_match=1.0 / 256.0,
|
| 108 |
+
zero_point_exact_match=0,
|
| 109 |
+
)
|
| 110 |
+
_FIXED_QPARAM_OP_NEG1TO1_CONSTRAINTS = DTypeWithConstraints(
|
| 111 |
+
dtype=torch.quint8,
|
| 112 |
+
quant_min_lower_bound=0,
|
| 113 |
+
quant_max_upper_bound=255,
|
| 114 |
+
scale_exact_match=2.0 / 256.0,
|
| 115 |
+
zero_point_exact_match=128,
|
| 116 |
+
)
|
| 117 |
+
_FIXED_QPARAMS_OP_TO_CONSTRAINTS: dict[Union[Callable, str], DTypeWithConstraints] = {
|
| 118 |
+
torch.nn.Hardsigmoid: _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 119 |
+
torch.nn.functional.hardsigmoid: _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 120 |
+
"hardsigmoid": _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 121 |
+
"hardsigmoid_": _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 122 |
+
torch.nn.Sigmoid: _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 123 |
+
torch.sigmoid: _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 124 |
+
"sigmoid": _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 125 |
+
"sigmoid_": _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 126 |
+
torch.nn.Softmax: _FIXED_QPARAM_OP_0TO1_CONSTRAINTS,
|
| 127 |
+
torch.nn.Tanh: _FIXED_QPARAM_OP_NEG1TO1_CONSTRAINTS,
|
| 128 |
+
torch.tanh: _FIXED_QPARAM_OP_NEG1TO1_CONSTRAINTS,
|
| 129 |
+
"tanh": _FIXED_QPARAM_OP_NEG1TO1_CONSTRAINTS,
|
| 130 |
+
"tanh_": _FIXED_QPARAM_OP_NEG1TO1_CONSTRAINTS,
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def _get_binary_op_configs(
|
| 135 |
+
dtype_configs: list[DTypeConfig],
|
| 136 |
+
) -> list[BackendPatternConfig]:
|
| 137 |
+
binary_op_configs: list[BackendPatternConfig] = []
|
| 138 |
+
num_tensor_args_to_observation_type_mapping = {
|
| 139 |
+
# TODO: this is not used right now since we have extra check in prepare
|
| 140 |
+
# will need to change this to NO_OBSERVER later after we implemented
|
| 141 |
+
# Tensor dtype inference properly
|
| 142 |
+
0: ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT,
|
| 143 |
+
1: ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT,
|
| 144 |
+
2: ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT,
|
| 145 |
+
}
|
| 146 |
+
for op_with_quantized_bop_scalar_variant in [
|
| 147 |
+
operator.add,
|
| 148 |
+
torch.add,
|
| 149 |
+
operator.mul,
|
| 150 |
+
torch.mul,
|
| 151 |
+
]:
|
| 152 |
+
bop_patterns = [
|
| 153 |
+
(op_with_quantized_bop_scalar_variant, nn.ReLU),
|
| 154 |
+
(op_with_quantized_bop_scalar_variant, F.relu),
|
| 155 |
+
(op_with_quantized_bop_scalar_variant, torch.relu),
|
| 156 |
+
op_with_quantized_bop_scalar_variant,
|
| 157 |
+
]
|
| 158 |
+
binary_op_configs.extend(
|
| 159 |
+
BackendPatternConfig(bop_pattern)
|
| 160 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 161 |
+
._set_num_tensor_args_to_observation_type(
|
| 162 |
+
num_tensor_args_to_observation_type_mapping
|
| 163 |
+
)
|
| 164 |
+
for bop_pattern in bop_patterns
|
| 165 |
+
)
|
| 166 |
+
# matmul
|
| 167 |
+
binary_op_configs.append(
|
| 168 |
+
BackendPatternConfig(torch.matmul).set_dtype_configs(
|
| 169 |
+
dtype_configs
|
| 170 |
+
) # noqa: E131
|
| 171 |
+
)
|
| 172 |
+
return binary_op_configs
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def _get_linear_configs(dtype_configs: list[DTypeConfig]) -> list[BackendPatternConfig]:
|
| 176 |
+
"""
|
| 177 |
+
Return all configs related to linear modules and ops.
|
| 178 |
+
"""
|
| 179 |
+
observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 180 |
+
linear_configs: list[BackendPatternConfig] = []
|
| 181 |
+
|
| 182 |
+
# (1) Single linear modules/functions
|
| 183 |
+
# -------------------------------------
|
| 184 |
+
# linear module
|
| 185 |
+
linear_configs.append(
|
| 186 |
+
BackendPatternConfig(torch.nn.Linear)
|
| 187 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 188 |
+
.set_dtype_configs(dtype_configs)
|
| 189 |
+
.set_root_module(torch.nn.Linear)
|
| 190 |
+
.set_reference_quantized_module(nnqr.Linear)
|
| 191 |
+
.set_qat_module(nnqat.Linear)
|
| 192 |
+
)
|
| 193 |
+
# linear qat module
|
| 194 |
+
linear_configs.append(
|
| 195 |
+
BackendPatternConfig(nnqat.Linear)
|
| 196 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 197 |
+
.set_dtype_configs(dtype_configs)
|
| 198 |
+
.set_root_module(torch.nn.Linear)
|
| 199 |
+
.set_reference_quantized_module(nnqr.Linear)
|
| 200 |
+
)
|
| 201 |
+
# functional linear
|
| 202 |
+
linear_configs.append(
|
| 203 |
+
BackendPatternConfig(torch.nn.functional.linear)
|
| 204 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 205 |
+
.set_dtype_configs(dtype_configs)
|
| 206 |
+
._set_input_type_to_index({"weight": 1, "bias": 2})
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
# (2) Linear + relu
|
| 210 |
+
# -------------------
|
| 211 |
+
# 2.1 linear module + relu fusion config
|
| 212 |
+
# linear relu, linear module + relu module
|
| 213 |
+
linear_configs.append(
|
| 214 |
+
BackendPatternConfig((torch.nn.Linear, torch.nn.ReLU))
|
| 215 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 216 |
+
.set_fuser_method(_sequential_wrapper2(nni.LinearReLU))
|
| 217 |
+
.set_fused_module(nni.LinearReLU)
|
| 218 |
+
)
|
| 219 |
+
# linear relu, linear module + functional relu
|
| 220 |
+
linear_configs.append(
|
| 221 |
+
BackendPatternConfig((torch.nn.Linear, torch.nn.functional.relu))
|
| 222 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 223 |
+
.set_fuser_method(_sequential_wrapper2(nni.LinearReLU))
|
| 224 |
+
.set_fused_module(nni.LinearReLU)
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
# 2.2 linear module + relu, fused module configs
|
| 228 |
+
# linear relu, fused module
|
| 229 |
+
linear_configs.append(
|
| 230 |
+
BackendPatternConfig(nni.LinearReLU)
|
| 231 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 232 |
+
.set_dtype_configs(dtype_configs)
|
| 233 |
+
.set_root_module(torch.nn.Linear)
|
| 234 |
+
.set_reference_quantized_module(nnqr.Linear)
|
| 235 |
+
.set_qat_module(nniqat.LinearReLU)
|
| 236 |
+
)
|
| 237 |
+
# linear relu, qat fused module
|
| 238 |
+
linear_configs.append(
|
| 239 |
+
BackendPatternConfig(nniqat.LinearReLU)
|
| 240 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 241 |
+
.set_dtype_configs(dtype_configs)
|
| 242 |
+
.set_root_module(torch.nn.Linear)
|
| 243 |
+
.set_reference_quantized_module(nnqr.Linear)
|
| 244 |
+
)
|
| 245 |
+
# 2.3 functional linear + relu configs
|
| 246 |
+
# linear relu, functional linear + relu module
|
| 247 |
+
linear_configs.append(
|
| 248 |
+
BackendPatternConfig((F.linear, torch.nn.ReLU))
|
| 249 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 250 |
+
.set_dtype_configs(dtype_configs)
|
| 251 |
+
)
|
| 252 |
+
# linear relu, functional linear + functional relu
|
| 253 |
+
linear_configs.append(
|
| 254 |
+
BackendPatternConfig((F.linear, F.relu))
|
| 255 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 256 |
+
.set_dtype_configs(dtype_configs)
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
# (3) Linear + batchnorm
|
| 260 |
+
# ------------------------
|
| 261 |
+
# 3.1 linear bn fusion
|
| 262 |
+
linear_configs.append(
|
| 263 |
+
BackendPatternConfig((nn.Linear, nn.BatchNorm1d))
|
| 264 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 265 |
+
.set_fuser_method(fuse_linear_bn)
|
| 266 |
+
.set_fused_module(nni.LinearBn1d)
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
# 3.2 linear bn fused
|
| 270 |
+
# linear bn, fused module
|
| 271 |
+
linear_configs.append(
|
| 272 |
+
BackendPatternConfig(nni.LinearBn1d)
|
| 273 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 274 |
+
.set_dtype_configs(dtype_configs)
|
| 275 |
+
.set_root_module(torch.nn.Linear)
|
| 276 |
+
.set_reference_quantized_module(nnqr.Linear)
|
| 277 |
+
.set_qat_module(nniqat.LinearBn1d)
|
| 278 |
+
)
|
| 279 |
+
# linear bn, qat fused module
|
| 280 |
+
linear_configs.append(
|
| 281 |
+
BackendPatternConfig(nniqat.LinearBn1d)
|
| 282 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 283 |
+
.set_dtype_configs(dtype_configs)
|
| 284 |
+
.set_root_module(torch.nn.Linear)
|
| 285 |
+
.set_reference_quantized_module(nnqr.Linear)
|
| 286 |
+
)
|
| 287 |
+
return linear_configs
|
| 288 |
+
|
| 289 |
+
|
| 290 |
+
def _get_conv_configs(dtype_configs):
|
| 291 |
+
"""
|
| 292 |
+
Return all configs related to conv modules and ops.
|
| 293 |
+
"""
|
| 294 |
+
conv_configs = []
|
| 295 |
+
observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 296 |
+
for convs in [_Conv1dMetadata, _Conv2dMetadata, _Conv3dMetadata]:
|
| 297 |
+
# (1) Single conv modules/functions
|
| 298 |
+
# -----------------------------------
|
| 299 |
+
# conv module
|
| 300 |
+
conv_configs.append(
|
| 301 |
+
BackendPatternConfig(convs.root)
|
| 302 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 303 |
+
.set_dtype_configs(dtype_configs)
|
| 304 |
+
.set_root_module(convs.root)
|
| 305 |
+
.set_reference_quantized_module(convs.reference)
|
| 306 |
+
.set_qat_module(convs.qat)
|
| 307 |
+
)
|
| 308 |
+
# conv qat module
|
| 309 |
+
conv_configs.append(
|
| 310 |
+
BackendPatternConfig(convs.qat)
|
| 311 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 312 |
+
.set_dtype_configs(dtype_configs)
|
| 313 |
+
.set_root_module(convs.root)
|
| 314 |
+
.set_reference_quantized_module(convs.reference)
|
| 315 |
+
)
|
| 316 |
+
# functional conv
|
| 317 |
+
conv_configs.append(
|
| 318 |
+
BackendPatternConfig(convs.func)
|
| 319 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 320 |
+
.set_dtype_configs(dtype_configs)
|
| 321 |
+
._set_input_type_to_index({"weight": 1, "bias": 2})
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
# (2) Conv + relu
|
| 325 |
+
# -----------------
|
| 326 |
+
# 2.1 conv module + relu fusion configs
|
| 327 |
+
# conv relu fusion, conv module + relu module
|
| 328 |
+
conv_configs.append(
|
| 329 |
+
BackendPatternConfig((convs.root, torch.nn.ReLU))
|
| 330 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 331 |
+
.set_fuser_method(_sequential_wrapper2(convs.fused_conv_relu))
|
| 332 |
+
.set_fused_module(convs.fused_conv_relu)
|
| 333 |
+
)
|
| 334 |
+
# conv relu fusion, conv module + functional relu
|
| 335 |
+
conv_configs.append(
|
| 336 |
+
BackendPatternConfig((convs.root, F.relu))
|
| 337 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 338 |
+
.set_fuser_method(_sequential_wrapper2(convs.fused_conv_relu))
|
| 339 |
+
.set_fused_module(convs.fused_conv_relu)
|
| 340 |
+
)
|
| 341 |
+
# 2.2 conv module + relu fused module configs
|
| 342 |
+
# conv relu, fused module
|
| 343 |
+
conv_configs.append(
|
| 344 |
+
BackendPatternConfig(convs.fused_conv_relu)
|
| 345 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 346 |
+
.set_dtype_configs(dtype_configs)
|
| 347 |
+
.set_root_module(convs.root)
|
| 348 |
+
.set_reference_quantized_module(convs.reference)
|
| 349 |
+
.set_qat_module(convs.relu_qat)
|
| 350 |
+
)
|
| 351 |
+
# conv relu, qat fused module
|
| 352 |
+
conv_configs.append(
|
| 353 |
+
BackendPatternConfig(convs.relu_qat)
|
| 354 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 355 |
+
.set_dtype_configs(dtype_configs)
|
| 356 |
+
.set_root_module(convs.root)
|
| 357 |
+
.set_reference_quantized_module(convs.reference)
|
| 358 |
+
)
|
| 359 |
+
# 2.3 functional conv + relu configs
|
| 360 |
+
# conv relu, functional conv + relu module
|
| 361 |
+
conv_configs.append(
|
| 362 |
+
BackendPatternConfig((convs.func, torch.nn.ReLU))
|
| 363 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 364 |
+
.set_dtype_configs(dtype_configs)
|
| 365 |
+
)
|
| 366 |
+
# conv relu, functional conv + functional relu
|
| 367 |
+
conv_configs.append(
|
| 368 |
+
BackendPatternConfig((convs.func, F.relu))
|
| 369 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 370 |
+
.set_dtype_configs(dtype_configs)
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
# fused conv relu
|
| 374 |
+
conv_configs.append(
|
| 375 |
+
BackendPatternConfig(convs.fused_conv_relu)
|
| 376 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 377 |
+
.set_qat_module(convs.relu_qat)
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
conv_configs.append(
|
| 381 |
+
BackendPatternConfig(convs.relu_qat)
|
| 382 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 383 |
+
.set_root_module(convs.root)
|
| 384 |
+
.set_reference_quantized_module(convs.reference)
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
# (3) Conv + batchnorm (+ relu)
|
| 388 |
+
# -------------------------------
|
| 389 |
+
# 3.1 conv bn fusion configs
|
| 390 |
+
# conv + bn fusion
|
| 391 |
+
conv_configs.append(
|
| 392 |
+
BackendPatternConfig((convs.root, convs.bn))
|
| 393 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 394 |
+
.set_fuser_method(fuse_conv_bn)
|
| 395 |
+
.set_fused_module(convs.fused_conv_bn)
|
| 396 |
+
)
|
| 397 |
+
# conv + bn + relu module fusion
|
| 398 |
+
conv_configs.append(
|
| 399 |
+
BackendPatternConfig((convs.root, convs.bn, nn.ReLU))
|
| 400 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 401 |
+
.set_fuser_method(fuse_conv_bn_relu)
|
| 402 |
+
.set_fused_module(convs.fused_conv_bn_relu)
|
| 403 |
+
)
|
| 404 |
+
# conv + bn + relu functional fusion
|
| 405 |
+
conv_configs.append(
|
| 406 |
+
BackendPatternConfig((convs.root, convs.bn, F.relu))
|
| 407 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 408 |
+
.set_root_module(convs.root)
|
| 409 |
+
.set_fuser_method(fuse_conv_bn_relu)
|
| 410 |
+
.set_fused_module(convs.fused_conv_bn_relu)
|
| 411 |
+
)
|
| 412 |
+
# TODO: we can add fusion for torch.relu as well
|
| 413 |
+
|
| 414 |
+
# 3.2 conv + bn (+ relu) fused module configs
|
| 415 |
+
# fused conv bn
|
| 416 |
+
conv_configs.append(
|
| 417 |
+
BackendPatternConfig(convs.fused_conv_bn)
|
| 418 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 419 |
+
.set_qat_module(convs.bn_qat)
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
# fused conv bn relu
|
| 423 |
+
conv_configs.append(
|
| 424 |
+
BackendPatternConfig(convs.fused_conv_bn_relu)
|
| 425 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 426 |
+
.set_qat_module(convs.bn_relu_qat)
|
| 427 |
+
)
|
| 428 |
+
|
| 429 |
+
# conv bn, qat fused module
|
| 430 |
+
conv_configs.append(
|
| 431 |
+
BackendPatternConfig(convs.bn_qat)
|
| 432 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 433 |
+
.set_dtype_configs(dtype_configs)
|
| 434 |
+
.set_root_module(convs.root)
|
| 435 |
+
.set_reference_quantized_module(convs.reference)
|
| 436 |
+
)
|
| 437 |
+
# conv bn relu, qat fused module
|
| 438 |
+
conv_configs.append(
|
| 439 |
+
BackendPatternConfig(convs.bn_relu_qat)
|
| 440 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 441 |
+
.set_dtype_configs(dtype_configs)
|
| 442 |
+
.set_root_module(convs.root)
|
| 443 |
+
.set_reference_quantized_module(convs.reference)
|
| 444 |
+
)
|
| 445 |
+
|
| 446 |
+
# (4) conv transpose and its fusion
|
| 447 |
+
# 4.1 conv transpose config
|
| 448 |
+
conv_configs.append(
|
| 449 |
+
BackendPatternConfig(convs.transpose)
|
| 450 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 451 |
+
.set_root_module(convs.transpose)
|
| 452 |
+
.set_reference_quantized_module(convs.transpose_reference)
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
# 4.2 conv transpose + bn fusion
|
| 456 |
+
conv_configs.append(
|
| 457 |
+
BackendPatternConfig((convs.transpose, convs.bn))
|
| 458 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 459 |
+
.set_fuser_method(fuse_convtranspose_bn)
|
| 460 |
+
.set_root_module(convs.transpose)
|
| 461 |
+
.set_reference_quantized_module(convs.transpose_reference)
|
| 462 |
+
)
|
| 463 |
+
|
| 464 |
+
# 4.3 functional conv transpose
|
| 465 |
+
conv_configs.append(
|
| 466 |
+
BackendPatternConfig(convs.func_transpose)
|
| 467 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 468 |
+
._set_input_type_to_index({"weight": 1, "bias": 2})
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
return conv_configs
|
| 472 |
+
|
| 473 |
+
|
| 474 |
+
def _get_cat_config(dtype_configs: list[DTypeConfig]) -> BackendPatternConfig:
|
| 475 |
+
return (
|
| 476 |
+
BackendPatternConfig(torch.cat)
|
| 477 |
+
.set_observation_type(ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT)
|
| 478 |
+
.set_dtype_configs(dtype_configs)
|
| 479 |
+
)
|
| 480 |
+
|
| 481 |
+
|
| 482 |
+
def _get_ln_configs(dtype_configs: list[DTypeConfig]) -> list[BackendPatternConfig]:
|
| 483 |
+
ln_configs = []
|
| 484 |
+
ln_configs.append(
|
| 485 |
+
BackendPatternConfig(torch.nn.LayerNorm)
|
| 486 |
+
.set_observation_type(
|
| 487 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 488 |
+
) # noqa: E131
|
| 489 |
+
.set_dtype_configs(dtype_configs)
|
| 490 |
+
)
|
| 491 |
+
ln_configs.append(
|
| 492 |
+
BackendPatternConfig(torch.nn.functional.layer_norm)
|
| 493 |
+
.set_observation_type(
|
| 494 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 495 |
+
) # noqa: E131
|
| 496 |
+
.set_dtype_configs(dtype_configs)
|
| 497 |
+
._set_input_type_to_index({"weight": 2, "bias": 3})
|
| 498 |
+
)
|
| 499 |
+
return ln_configs
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
def _get_default_op_configs(
|
| 503 |
+
dtype_configs: list[DTypeConfig],
|
| 504 |
+
) -> list[BackendPatternConfig]:
|
| 505 |
+
default_ops = [
|
| 506 |
+
torch.nn.ELU,
|
| 507 |
+
torch.nn.LeakyReLU,
|
| 508 |
+
torch.nn.Hardswish,
|
| 509 |
+
torch.nn.InstanceNorm1d,
|
| 510 |
+
torch.nn.InstanceNorm2d,
|
| 511 |
+
torch.nn.InstanceNorm3d,
|
| 512 |
+
torch.nn.Dropout,
|
| 513 |
+
torch.nn.PReLU,
|
| 514 |
+
torch.nn.functional.elu,
|
| 515 |
+
torch.nn.functional.hardswish,
|
| 516 |
+
torch.nn.functional.leaky_relu,
|
| 517 |
+
torch.nn.functional.dropout,
|
| 518 |
+
]
|
| 519 |
+
configs = [
|
| 520 |
+
BackendPatternConfig(op)
|
| 521 |
+
.set_observation_type(
|
| 522 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 523 |
+
) # noqa: E131
|
| 524 |
+
.set_dtype_configs(dtype_configs)
|
| 525 |
+
for op in default_ops
|
| 526 |
+
]
|
| 527 |
+
|
| 528 |
+
configs.append(
|
| 529 |
+
BackendPatternConfig(torch.nn.functional.group_norm)
|
| 530 |
+
.set_observation_type(
|
| 531 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 532 |
+
) # noqa: E131
|
| 533 |
+
.set_dtype_configs(dtype_configs)
|
| 534 |
+
._set_input_type_to_index({"weight": 2, "bias": 3})
|
| 535 |
+
)
|
| 536 |
+
|
| 537 |
+
configs.append(
|
| 538 |
+
BackendPatternConfig(torch.nn.functional.instance_norm)
|
| 539 |
+
.set_observation_type(
|
| 540 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 541 |
+
) # noqa: E131
|
| 542 |
+
.set_dtype_configs(dtype_configs)
|
| 543 |
+
._set_input_type_to_index({"weight": 3, "bias": 4})
|
| 544 |
+
)
|
| 545 |
+
return configs
|
| 546 |
+
|
| 547 |
+
|
| 548 |
+
def _add_fixed_qparams_to_dtype_configs(
|
| 549 |
+
dtype_configs: list[DTypeConfig],
|
| 550 |
+
constraints: DTypeWithConstraints,
|
| 551 |
+
) -> list[DTypeConfig]:
|
| 552 |
+
"""
|
| 553 |
+
Return a copy of the list of DTypeConfigs where activations are subject to the specified
|
| 554 |
+
constraints required for fixed qparams ops.
|
| 555 |
+
|
| 556 |
+
If the data type doesn't match the one in the constraints, simply leave the corresponding
|
| 557 |
+
DTypeConfig unchanged.
|
| 558 |
+
|
| 559 |
+
If `scale_min_lower_bound` or `scale_max_upper_bound` is specified in the activations,
|
| 560 |
+
throw an exception since these settings are incompatible with fixed qparams ops.
|
| 561 |
+
"""
|
| 562 |
+
new_dtype_configs = []
|
| 563 |
+
for dtype_config in dtype_configs:
|
| 564 |
+
dc = copy.deepcopy(dtype_config)
|
| 565 |
+
for orig_constraints in [
|
| 566 |
+
dc.input_dtype_with_constraints,
|
| 567 |
+
dc.output_dtype_with_constraints,
|
| 568 |
+
]:
|
| 569 |
+
if orig_constraints.dtype != constraints.dtype:
|
| 570 |
+
continue
|
| 571 |
+
if orig_constraints.scale_min_lower_bound is not None:
|
| 572 |
+
raise ValueError(
|
| 573 |
+
f"scale_min_lower_bound is invalid for fixed qparams ops: {dtype_config}"
|
| 574 |
+
)
|
| 575 |
+
if orig_constraints.scale_max_upper_bound is not None:
|
| 576 |
+
raise ValueError(
|
| 577 |
+
f"scale_max_upper_bound is invalid for fixed qparams ops: {dtype_config}"
|
| 578 |
+
)
|
| 579 |
+
orig_constraints.quant_min_lower_bound = constraints.quant_min_lower_bound
|
| 580 |
+
orig_constraints.quant_max_upper_bound = constraints.quant_max_upper_bound
|
| 581 |
+
orig_constraints.scale_exact_match = constraints.scale_exact_match
|
| 582 |
+
orig_constraints.zero_point_exact_match = constraints.zero_point_exact_match
|
| 583 |
+
new_dtype_configs.append(dc)
|
| 584 |
+
return new_dtype_configs
|
| 585 |
+
|
| 586 |
+
|
| 587 |
+
def _get_fixed_qparams_op_configs(
|
| 588 |
+
dtype_configs: list[DTypeConfig],
|
| 589 |
+
) -> list[BackendPatternConfig]:
|
| 590 |
+
fixed_qparams_op_configs = []
|
| 591 |
+
for fixed_qparam_op, constraints in _FIXED_QPARAMS_OP_TO_CONSTRAINTS.items():
|
| 592 |
+
new_dtype_configs = _add_fixed_qparams_to_dtype_configs(
|
| 593 |
+
dtype_configs, constraints
|
| 594 |
+
)
|
| 595 |
+
fixed_qparams_op_configs.append(
|
| 596 |
+
BackendPatternConfig(fixed_qparam_op)
|
| 597 |
+
.set_observation_type(
|
| 598 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 599 |
+
) # noqa: E131
|
| 600 |
+
.set_dtype_configs(new_dtype_configs)
|
| 601 |
+
)
|
| 602 |
+
return fixed_qparams_op_configs
|
| 603 |
+
|
| 604 |
+
|
| 605 |
+
def _get_share_qparams_op_configs(dtype_configs):
|
| 606 |
+
"""Get the operator config for the operators that works for both float and quantized input
|
| 607 |
+
if input is quantized, the output Tensor shares the same quantization parameter
|
| 608 |
+
with input.
|
| 609 |
+
Example operator: avgpool2d, reshape, transpose, maxpool2d
|
| 610 |
+
Example observed operator:
|
| 611 |
+
observer_0 - avgpool2d - observer_0 (same observer instance as input)
|
| 612 |
+
"""
|
| 613 |
+
|
| 614 |
+
def _get_share_qprams_op_backend_config(op):
|
| 615 |
+
return (
|
| 616 |
+
BackendPatternConfig(op)
|
| 617 |
+
.set_observation_type(ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT)
|
| 618 |
+
.set_dtype_configs(dtype_configs)
|
| 619 |
+
)
|
| 620 |
+
|
| 621 |
+
share_qparams_ops = [
|
| 622 |
+
torch.nn.AdaptiveAvgPool1d,
|
| 623 |
+
torch.nn.AdaptiveAvgPool2d,
|
| 624 |
+
torch.nn.AdaptiveAvgPool3d,
|
| 625 |
+
torch.nn.AvgPool1d,
|
| 626 |
+
torch.nn.AvgPool2d,
|
| 627 |
+
torch.nn.AvgPool3d,
|
| 628 |
+
torch.nn.Hardtanh,
|
| 629 |
+
torch.nn.Identity,
|
| 630 |
+
torch.nn.MaxPool1d,
|
| 631 |
+
torch.nn.MaxPool2d,
|
| 632 |
+
torch.nn.MaxPool3d,
|
| 633 |
+
torch.nn.PixelShuffle,
|
| 634 |
+
torch.nn.PixelUnshuffle,
|
| 635 |
+
torch.nn.ReLU,
|
| 636 |
+
torch.nn.ReLU6,
|
| 637 |
+
torch.adaptive_avg_pool1d,
|
| 638 |
+
torch.nn.functional.adaptive_avg_pool2d,
|
| 639 |
+
torch.nn.functional.adaptive_avg_pool3d,
|
| 640 |
+
torch.nn.functional.hardtanh,
|
| 641 |
+
torch.nn.functional.hardtanh_,
|
| 642 |
+
torch.nn.functional.interpolate,
|
| 643 |
+
torch.nn.functional.max_pool1d,
|
| 644 |
+
torch.nn.functional.max_pool2d,
|
| 645 |
+
torch.nn.functional.max_pool3d,
|
| 646 |
+
torch.nn.functional.pixel_shuffle,
|
| 647 |
+
torch.nn.functional.pixel_unshuffle,
|
| 648 |
+
torch.nn.functional.relu,
|
| 649 |
+
torch.nn.functional.relu6,
|
| 650 |
+
torch.avg_pool1d,
|
| 651 |
+
torch._C._nn.avg_pool2d,
|
| 652 |
+
torch._C._nn.avg_pool3d,
|
| 653 |
+
torch.clamp,
|
| 654 |
+
torch.flatten,
|
| 655 |
+
torch.mean,
|
| 656 |
+
torch.narrow,
|
| 657 |
+
torch.repeat_interleave,
|
| 658 |
+
torch.transpose,
|
| 659 |
+
torch.squeeze,
|
| 660 |
+
torch.stack,
|
| 661 |
+
torch.unsqueeze,
|
| 662 |
+
operator.floordiv,
|
| 663 |
+
"contiguous",
|
| 664 |
+
"clamp",
|
| 665 |
+
"detach",
|
| 666 |
+
"detach_",
|
| 667 |
+
"mean",
|
| 668 |
+
"permute",
|
| 669 |
+
"repeat",
|
| 670 |
+
"repeat_interleave",
|
| 671 |
+
"reshape",
|
| 672 |
+
"resize_",
|
| 673 |
+
"relu",
|
| 674 |
+
"relu_",
|
| 675 |
+
"squeeze",
|
| 676 |
+
"squeeze_",
|
| 677 |
+
"transpose",
|
| 678 |
+
"unsqueeze",
|
| 679 |
+
"unsqueeze_",
|
| 680 |
+
"view",
|
| 681 |
+
]
|
| 682 |
+
return [_get_share_qprams_op_backend_config(op) for op in share_qparams_ops]
|
| 683 |
+
|
| 684 |
+
|
| 685 |
+
def _get_bn_configs(dtype_configs: list[DTypeConfig]) -> list[BackendPatternConfig]:
|
| 686 |
+
"""Get configs related to batchnorm."""
|
| 687 |
+
bn_configs = []
|
| 688 |
+
bn_to_fused_bn = {
|
| 689 |
+
torch.nn.BatchNorm2d: nni.BNReLU2d,
|
| 690 |
+
torch.nn.BatchNorm3d: nni.BNReLU3d,
|
| 691 |
+
}
|
| 692 |
+
for bn in bn_to_fused_bn.keys():
|
| 693 |
+
fused_bn = bn_to_fused_bn[bn]
|
| 694 |
+
# bn module + relu module fusion config
|
| 695 |
+
bn_configs.append(
|
| 696 |
+
BackendPatternConfig((bn, nn.ReLU))
|
| 697 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 698 |
+
.set_fuser_method(_sequential_wrapper2(fused_bn))
|
| 699 |
+
.set_fused_module(fused_bn)
|
| 700 |
+
)
|
| 701 |
+
# bn module + F.relu fusion config
|
| 702 |
+
bn_configs.append(
|
| 703 |
+
BackendPatternConfig((bn, F.relu))
|
| 704 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 705 |
+
.set_fuser_method(_sequential_wrapper2(fused_bn))
|
| 706 |
+
.set_fused_module(fused_bn)
|
| 707 |
+
)
|
| 708 |
+
bn_configs.append(
|
| 709 |
+
BackendPatternConfig(bn)
|
| 710 |
+
.set_observation_type(
|
| 711 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 712 |
+
) # noqa: E131
|
| 713 |
+
.set_dtype_configs(dtype_configs)
|
| 714 |
+
)
|
| 715 |
+
|
| 716 |
+
# fused bn configs
|
| 717 |
+
for fused_bn in bn_to_fused_bn.values():
|
| 718 |
+
bn_configs.append(
|
| 719 |
+
BackendPatternConfig(fused_bn)
|
| 720 |
+
.set_observation_type(
|
| 721 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 722 |
+
) # noqa: E131
|
| 723 |
+
.set_dtype_configs(dtype_configs)
|
| 724 |
+
)
|
| 725 |
+
return bn_configs
|
| 726 |
+
|
| 727 |
+
|
| 728 |
+
def _get_rnn_op_configs(dtype_configs: list[DTypeConfig]) -> list[BackendPatternConfig]:
|
| 729 |
+
rnn_op_configs = []
|
| 730 |
+
for rnn_op, ref_rnn_op in [
|
| 731 |
+
(nn.GRUCell, nnqr.GRUCell),
|
| 732 |
+
(nn.LSTMCell, nnqr.LSTMCell),
|
| 733 |
+
(nn.RNNCell, nnqr.RNNCell),
|
| 734 |
+
(nn.LSTM, nnqr.LSTM),
|
| 735 |
+
(nn.GRU, nnqr.GRU),
|
| 736 |
+
]:
|
| 737 |
+
rnn_op_configs.append(
|
| 738 |
+
BackendPatternConfig(rnn_op)
|
| 739 |
+
.set_observation_type(
|
| 740 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 741 |
+
) # noqa: E131
|
| 742 |
+
.set_dtype_configs(dtype_configs)
|
| 743 |
+
.set_root_module(rnn_op)
|
| 744 |
+
.set_reference_quantized_module(ref_rnn_op)
|
| 745 |
+
)
|
| 746 |
+
return rnn_op_configs
|
| 747 |
+
|
| 748 |
+
|
| 749 |
+
def _get_embedding_op_configs(
|
| 750 |
+
dtype_configs: list[DTypeConfig],
|
| 751 |
+
) -> list[BackendPatternConfig]:
|
| 752 |
+
embedding_op_configs = []
|
| 753 |
+
for embedding_op, qat_embedding_op, ref_embedding_op in [
|
| 754 |
+
(nn.Embedding, nnqat.Embedding, nnqr.Embedding),
|
| 755 |
+
(nn.EmbeddingBag, nnqat.EmbeddingBag, nnqr.EmbeddingBag),
|
| 756 |
+
]:
|
| 757 |
+
embedding_op_configs.append(
|
| 758 |
+
BackendPatternConfig(embedding_op)
|
| 759 |
+
.set_observation_type(
|
| 760 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 761 |
+
) # noqa: E131
|
| 762 |
+
.set_dtype_configs(dtype_configs)
|
| 763 |
+
.set_qat_module(qat_embedding_op)
|
| 764 |
+
.set_root_module(embedding_op)
|
| 765 |
+
.set_reference_quantized_module(ref_embedding_op)
|
| 766 |
+
)
|
| 767 |
+
|
| 768 |
+
# config for qat op
|
| 769 |
+
embedding_op_configs.append(
|
| 770 |
+
BackendPatternConfig(qat_embedding_op)
|
| 771 |
+
.set_observation_type(
|
| 772 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 773 |
+
) # noqa: E131
|
| 774 |
+
.set_dtype_configs(dtype_configs)
|
| 775 |
+
.set_root_module(embedding_op)
|
| 776 |
+
.set_reference_quantized_module(ref_embedding_op)
|
| 777 |
+
)
|
| 778 |
+
return embedding_op_configs
|
| 779 |
+
|
| 780 |
+
|
| 781 |
+
def _get_tensor_info_op_configs(dtype_configs):
|
| 782 |
+
"""
|
| 783 |
+
These ops work on tensors of different dtypes but return non-tensors
|
| 784 |
+
containing information about the input tensor.
|
| 785 |
+
"""
|
| 786 |
+
|
| 787 |
+
def _get_config(op):
|
| 788 |
+
return (
|
| 789 |
+
BackendPatternConfig(op)
|
| 790 |
+
.set_observation_type(ObservationType.INPUT_OUTPUT_NOT_OBSERVED)
|
| 791 |
+
.set_dtype_configs(dtype_configs)
|
| 792 |
+
)
|
| 793 |
+
|
| 794 |
+
return [_get_config(op) for op in ("shape", "size")]
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/_qnnpack_pt2e.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: allow-untyped-defs
|
| 2 |
+
import operator
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from torch.ao.quantization.backend_config import (
|
| 6 |
+
BackendConfig,
|
| 7 |
+
BackendPatternConfig,
|
| 8 |
+
DTypeConfig,
|
| 9 |
+
ObservationType,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
weighted_op_quint8_dtype_config = DTypeConfig(
|
| 14 |
+
input_dtype=torch.quint8,
|
| 15 |
+
output_dtype=torch.quint8,
|
| 16 |
+
weight_dtype=torch.qint8,
|
| 17 |
+
bias_dtype=torch.float,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_linear_configs():
|
| 22 |
+
linear_configs = []
|
| 23 |
+
observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 24 |
+
dtype_configs = [weighted_op_quint8_dtype_config]
|
| 25 |
+
|
| 26 |
+
# TODO: need to fix the way we insert observers for this pattern
|
| 27 |
+
# should be solved in the new fusion API
|
| 28 |
+
# reason that this doesn't work: the pattern is a bit complicated and we don't
|
| 29 |
+
# have a way to specify which input of the pattern we would like to observe
|
| 30 |
+
# pattern:
|
| 31 |
+
# bias input weight
|
| 32 |
+
# \ | /
|
| 33 |
+
# \ | t
|
| 34 |
+
# \ | /
|
| 35 |
+
# addmm
|
| 36 |
+
# we want to observe "weight" as weight, but there is not way to convey this
|
| 37 |
+
# information with current pattern language
|
| 38 |
+
#
|
| 39 |
+
# right now:
|
| 40 |
+
# original:
|
| 41 |
+
# weight - t \
|
| 42 |
+
# input - addmm
|
| 43 |
+
# observed (no hack):
|
| 44 |
+
# weight - t - observer \
|
| 45 |
+
# input - observer - addmm
|
| 46 |
+
# target:
|
| 47 |
+
# weight - observer - t \
|
| 48 |
+
# input - observer - addmm
|
| 49 |
+
|
| 50 |
+
# def root_node_getter(node_pattern):
|
| 51 |
+
# addmm, bias, act, weight = node_pattern
|
| 52 |
+
# return addmm
|
| 53 |
+
|
| 54 |
+
# linear_configs.append(
|
| 55 |
+
# BackendPatternConfig((torch.ops.aten.addmm.default, MatchAllNode, MatchAllNode, torch.ops.aten.t.default))
|
| 56 |
+
# .set_observation_type(observation_type) # noqa: E131
|
| 57 |
+
# .set_dtype_configs(dtype_configs)
|
| 58 |
+
# ._set_root_node_getter(root_node_getter))
|
| 59 |
+
|
| 60 |
+
linear_configs.append(
|
| 61 |
+
BackendPatternConfig(torch.ops.aten.addmm.default)
|
| 62 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 63 |
+
.set_dtype_configs(dtype_configs)
|
| 64 |
+
._set_input_type_to_index({"weight": 2, "bias": 0})
|
| 65 |
+
)
|
| 66 |
+
# linear is decomposed to `t - mm` if bias is not present
|
| 67 |
+
linear_configs.append(
|
| 68 |
+
BackendPatternConfig(torch.ops.aten.mm.default)
|
| 69 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 70 |
+
.set_dtype_configs(dtype_configs)
|
| 71 |
+
._set_input_type_to_index({"weight": 1})
|
| 72 |
+
)
|
| 73 |
+
return linear_configs
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def get_conv_configs():
|
| 77 |
+
conv_configs = []
|
| 78 |
+
observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 79 |
+
dtype_configs = [weighted_op_quint8_dtype_config]
|
| 80 |
+
conv_configs.append(
|
| 81 |
+
BackendPatternConfig(torch.ops.aten.convolution.default)
|
| 82 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 83 |
+
.set_dtype_configs(dtype_configs)
|
| 84 |
+
._set_input_type_to_index({"weight": 1, "bias": 2})
|
| 85 |
+
)
|
| 86 |
+
conv_configs.append(
|
| 87 |
+
BackendPatternConfig(
|
| 88 |
+
(torch.ops.aten.convolution.default, torch.ops.aten.relu.default)
|
| 89 |
+
)
|
| 90 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 91 |
+
.set_dtype_configs(dtype_configs)
|
| 92 |
+
._set_input_type_to_index({"weight": 1, "bias": 2})
|
| 93 |
+
)
|
| 94 |
+
# TODO: remove when functionalization is supported in PT2 mode
|
| 95 |
+
conv_configs.append(
|
| 96 |
+
BackendPatternConfig(
|
| 97 |
+
(torch.ops.aten.convolution.default, torch.ops.aten.relu_.default)
|
| 98 |
+
)
|
| 99 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 100 |
+
.set_dtype_configs(dtype_configs)
|
| 101 |
+
._set_input_type_to_index({"weight": 1, "bias": 2})
|
| 102 |
+
)
|
| 103 |
+
return conv_configs
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def get_pooling_configs():
|
| 107 |
+
backend_pattern_configs = []
|
| 108 |
+
observation_type = ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT
|
| 109 |
+
dtype_configs = [weighted_op_quint8_dtype_config]
|
| 110 |
+
|
| 111 |
+
def root_node_getter(node_pattern):
|
| 112 |
+
_getitem, maxpool, _index = node_pattern
|
| 113 |
+
return maxpool
|
| 114 |
+
|
| 115 |
+
backend_pattern_configs.append(
|
| 116 |
+
BackendPatternConfig()
|
| 117 |
+
._set_pattern_complex_format(
|
| 118 |
+
(operator.getitem, torch.ops.aten.max_pool2d_with_indices.default, 0)
|
| 119 |
+
)
|
| 120 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 121 |
+
.set_dtype_configs(dtype_configs)
|
| 122 |
+
._set_root_node_getter(root_node_getter)
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
return backend_pattern_configs
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def get_relu_configs():
|
| 129 |
+
backend_pattern_configs = []
|
| 130 |
+
observation_type = ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT
|
| 131 |
+
dtype_configs = [weighted_op_quint8_dtype_config]
|
| 132 |
+
backend_pattern_configs.append(
|
| 133 |
+
BackendPatternConfig(torch.ops.aten.relu.default)
|
| 134 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 135 |
+
.set_dtype_configs(dtype_configs)
|
| 136 |
+
)
|
| 137 |
+
return backend_pattern_configs
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def get_binary_op_configs():
|
| 141 |
+
binary_op_configs: list[BackendPatternConfig] = []
|
| 142 |
+
dtype_configs = [weighted_op_quint8_dtype_config]
|
| 143 |
+
num_tensor_args_to_observation_type_mapping = {
|
| 144 |
+
# TODO: this is not used right now since we have extra check in prepare
|
| 145 |
+
# will need to change this to NO_OBSERVER later after we implemented
|
| 146 |
+
# Tensor dtype inference properly
|
| 147 |
+
0: ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT,
|
| 148 |
+
1: ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT,
|
| 149 |
+
2: ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT,
|
| 150 |
+
}
|
| 151 |
+
for op_with_quantized_bop_scalar_variant in [
|
| 152 |
+
torch.ops.aten.add.Tensor,
|
| 153 |
+
torch.ops.aten.add_.Tensor,
|
| 154 |
+
]:
|
| 155 |
+
bop_patterns = [
|
| 156 |
+
(op_with_quantized_bop_scalar_variant, torch.ops.aten.relu.default),
|
| 157 |
+
op_with_quantized_bop_scalar_variant,
|
| 158 |
+
# TODO: remove when functionalization is supported in pt2_mode
|
| 159 |
+
(op_with_quantized_bop_scalar_variant, torch.ops.aten.relu_.default),
|
| 160 |
+
]
|
| 161 |
+
binary_op_configs.extend(
|
| 162 |
+
BackendPatternConfig(bop_pattern)
|
| 163 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 164 |
+
._set_num_tensor_args_to_observation_type(
|
| 165 |
+
num_tensor_args_to_observation_type_mapping
|
| 166 |
+
)
|
| 167 |
+
for bop_pattern in bop_patterns
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
return binary_op_configs
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def get_qnnpack_pt2e_backend_config():
|
| 174 |
+
return (
|
| 175 |
+
BackendConfig("qnnpack_pytorch_2.0_export")
|
| 176 |
+
.set_backend_pattern_configs(get_linear_configs())
|
| 177 |
+
.set_backend_pattern_configs(get_binary_op_configs())
|
| 178 |
+
.set_backend_pattern_configs(get_conv_configs())
|
| 179 |
+
.set_backend_pattern_configs(get_pooling_configs())
|
| 180 |
+
.set_backend_pattern_configs(get_relu_configs())
|
| 181 |
+
)
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/backend_config.py
ADDED
|
@@ -0,0 +1,749 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: allow-untyped-defs
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from enum import Enum
|
| 6 |
+
from typing import Any, Callable, Optional, TYPE_CHECKING, Union
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
if TYPE_CHECKING:
|
| 12 |
+
from torch.ao.quantization.utils import Pattern
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
__all__ = [
|
| 16 |
+
"BackendConfig",
|
| 17 |
+
"BackendPatternConfig",
|
| 18 |
+
"DTypeConfig",
|
| 19 |
+
"DTypeWithConstraints",
|
| 20 |
+
"ObservationType",
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# DTypeConfig dict keys
|
| 25 |
+
INPUT_DTYPE_DICT_KEY = "input_dtype"
|
| 26 |
+
OUTPUT_DTYPE_DICT_KEY = "output_dtype"
|
| 27 |
+
WEIGHT_DTYPE_DICT_KEY = "weight_dtype"
|
| 28 |
+
BIAS_DTYPE_DICT_KEY = "bias_dtype"
|
| 29 |
+
IS_DYNAMIC_DICT_KEY = "is_dynamic"
|
| 30 |
+
|
| 31 |
+
# BackendConfig dict keys
|
| 32 |
+
NAME_DICT_KEY = "name"
|
| 33 |
+
CONFIGS_DICT_KEY = "configs"
|
| 34 |
+
|
| 35 |
+
# BackendPatternConfig dict keys
|
| 36 |
+
PATTERN_DICT_KEY = "pattern"
|
| 37 |
+
PATTERN_COMPLEX_FORMAT_DICT_KEY = "pattern_complex_format"
|
| 38 |
+
OBSERVATION_TYPE_DICT_KEY = "observation_type"
|
| 39 |
+
DTYPE_CONFIGS_DICT_KEY = "dtype_configs"
|
| 40 |
+
ROOT_MODULE_DICT_KEY = "root_module"
|
| 41 |
+
QAT_MODULE_DICT_KEY = "qat_module"
|
| 42 |
+
REFERENCE_QUANTIZED_MODULE_DICT_KEY = "reference_quantized_module_for_root"
|
| 43 |
+
FUSED_MODULE_DICT_KEY = "fused_module"
|
| 44 |
+
FUSER_METHOD_DICT_KEY = "fuser_method"
|
| 45 |
+
ROOT_NODE_GETTER_DICT_KEY = "root_node_getter"
|
| 46 |
+
EXTRA_INPUTS_GETTER_DICT_KEY = "extra_inputs_getter"
|
| 47 |
+
NUM_TENSOR_ARGS_TO_OBSERVATION_TYPE_DICT_KEY = "num_tensor_args_to_observation_type"
|
| 48 |
+
INPUT_TYPE_TO_INDEX_DICT_KEY = "input_type_to_index"
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# TODO: maybe rename this to something that's not related to observer
|
| 52 |
+
# e.g. QParamsType
|
| 53 |
+
class ObservationType(Enum):
|
| 54 |
+
"""An enum that represents different ways of how an operator/operator pattern
|
| 55 |
+
should be observed
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT = 0
|
| 59 |
+
"""this means input and output are observed with different observers, based
|
| 60 |
+
on qconfig.activation
|
| 61 |
+
example: conv, linear, softmax
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
OUTPUT_SHARE_OBSERVER_WITH_INPUT = 1
|
| 65 |
+
"""this means the output will use the same observer instance as input, based
|
| 66 |
+
on qconfig.activation
|
| 67 |
+
example: torch.cat, maxpool
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
INPUT_OUTPUT_NOT_OBSERVED = 2
|
| 71 |
+
"""this means the input and output are never observed
|
| 72 |
+
example: x.shape, x.size
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@dataclass
|
| 77 |
+
class DTypeWithConstraints:
|
| 78 |
+
"""
|
| 79 |
+
Config for specifying additional constraints for a given dtype, such as quantization
|
| 80 |
+
value ranges, scale value ranges, and fixed quantization params, to be used in
|
| 81 |
+
:class:`~torch.ao.quantization.backend_config.DTypeConfig`.
|
| 82 |
+
|
| 83 |
+
The constraints currently supported are:
|
| 84 |
+
|
| 85 |
+
* `quant_min_lower_bound` and `quant_max_upper_bound`: Lower and upper
|
| 86 |
+
bounds for the minimum and maximum quantized values respectively. If
|
| 87 |
+
the QConfig's `quant_min` and `quant_max` fall outside this range,
|
| 88 |
+
then the QConfig will be ignored.
|
| 89 |
+
|
| 90 |
+
* `scale_min_lower_bound` and `scale_max_upper_bound`: Lower and upper
|
| 91 |
+
bounds for the minimum and maximum scale values respectively. If the
|
| 92 |
+
QConfig's minimum scale value (currently exposed as `eps`) falls below
|
| 93 |
+
the lower bound, then the QConfig will be ignored. Note that the upper
|
| 94 |
+
bound is currently not enforced.
|
| 95 |
+
|
| 96 |
+
* `scale_exact_match` and `zero_point_exact_match`: Exact match requirements
|
| 97 |
+
for scale and zero point, to be used for operators with fixed quantization
|
| 98 |
+
parameters such as sigmoid and tanh. If the observer specified in the QConfig
|
| 99 |
+
is neither `FixedQParamsObserver` nor `FixedQParamsFakeQuantize`, or if
|
| 100 |
+
the quantization parameters don't match, then the QConfig will be ignored.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
dtype: Optional[torch.dtype] = None
|
| 104 |
+
quant_min_lower_bound: Union[int, float, None] = None
|
| 105 |
+
quant_max_upper_bound: Union[int, float, None] = None
|
| 106 |
+
scale_min_lower_bound: Union[int, float, None] = None
|
| 107 |
+
scale_max_upper_bound: Union[int, float, None] = None
|
| 108 |
+
scale_exact_match: Optional[float] = None
|
| 109 |
+
zero_point_exact_match: Optional[int] = None
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
@dataclass
|
| 113 |
+
class DTypeConfig:
|
| 114 |
+
"""
|
| 115 |
+
Config object that specifies the supported data types passed as arguments to
|
| 116 |
+
quantize ops in the reference model spec, for input and output activations,
|
| 117 |
+
weights, and biases.
|
| 118 |
+
|
| 119 |
+
For example, consider the following reference model:
|
| 120 |
+
|
| 121 |
+
quant1 - [dequant1 - fp32_linear - quant2] - dequant2
|
| 122 |
+
|
| 123 |
+
The pattern in the square brackets refers to the reference pattern of
|
| 124 |
+
statically quantized linear. Setting the input dtype as `torch.quint8`
|
| 125 |
+
in the DTypeConfig means we pass in `torch.quint8` as the dtype argument
|
| 126 |
+
to the first quantize op (quant1). Similarly, setting the output dtype as
|
| 127 |
+
`torch.quint8` means we pass in `torch.quint8` as the dtype argument to
|
| 128 |
+
the second quantize op (quant2).
|
| 129 |
+
|
| 130 |
+
Note that the dtype here does not refer to the interface dtypes of the
|
| 131 |
+
op. For example, the "input dtype" here is not the dtype of the input
|
| 132 |
+
tensor passed to the quantized linear op. Though it can still be the
|
| 133 |
+
same as the interface dtype, this is not always the case, e.g. the
|
| 134 |
+
interface dtype is fp32 in dynamic quantization but the "input dtype"
|
| 135 |
+
specified in the DTypeConfig would still be quint8. The semantics of
|
| 136 |
+
dtypes here are the same as the semantics of the dtypes specified in
|
| 137 |
+
the observers.
|
| 138 |
+
|
| 139 |
+
These dtypes are matched against the ones specified in the user's
|
| 140 |
+
QConfig. If there is a match, and the QConfig satisfies the constraints
|
| 141 |
+
specified in the DTypeConfig (if any), then we will quantize the given
|
| 142 |
+
pattern using this DTypeConfig. Otherwise, the QConfig is ignored and
|
| 143 |
+
the pattern will not be quantized.
|
| 144 |
+
|
| 145 |
+
Example usage::
|
| 146 |
+
|
| 147 |
+
>>> # xdoctest: +SKIP(failing)
|
| 148 |
+
>>> dtype_config1 = DTypeConfig(
|
| 149 |
+
... input_dtype=torch.quint8,
|
| 150 |
+
... output_dtype=torch.quint8,
|
| 151 |
+
... weight_dtype=torch.qint8,
|
| 152 |
+
... bias_dtype=torch.float)
|
| 153 |
+
|
| 154 |
+
>>> dtype_config2 = DTypeConfig(
|
| 155 |
+
... input_dtype=DTypeWithConstraints(
|
| 156 |
+
... dtype=torch.quint8,
|
| 157 |
+
... quant_min_lower_bound=0,
|
| 158 |
+
... quant_max_upper_bound=255,
|
| 159 |
+
... ),
|
| 160 |
+
... output_dtype=DTypeWithConstraints(
|
| 161 |
+
... dtype=torch.quint8,
|
| 162 |
+
... quant_min_lower_bound=0,
|
| 163 |
+
... quant_max_upper_bound=255,
|
| 164 |
+
... ),
|
| 165 |
+
... weight_dtype=DTypeWithConstraints(
|
| 166 |
+
... dtype=torch.qint8,
|
| 167 |
+
... quant_min_lower_bound=-128,
|
| 168 |
+
... quant_max_upper_bound=127,
|
| 169 |
+
... ),
|
| 170 |
+
... bias_dtype=torch.float)
|
| 171 |
+
|
| 172 |
+
>>> dtype_config1.input_dtype
|
| 173 |
+
torch.quint8
|
| 174 |
+
|
| 175 |
+
>>> dtype_config2.input_dtype
|
| 176 |
+
torch.quint8
|
| 177 |
+
|
| 178 |
+
>>> dtype_config2.input_dtype_with_constraints
|
| 179 |
+
DTypeWithConstraints(dtype=torch.quint8, quant_min_lower_bound=0, quant_max_upper_bound=255, \
|
| 180 |
+
scale_min_lower_bound=None, scale_max_upper_bound=None)
|
| 181 |
+
"""
|
| 182 |
+
|
| 183 |
+
input_dtype_with_constraints: DTypeWithConstraints
|
| 184 |
+
output_dtype_with_constraints: DTypeWithConstraints
|
| 185 |
+
weight_dtype_with_constraints: DTypeWithConstraints
|
| 186 |
+
bias_dtype: Optional[torch.dtype]
|
| 187 |
+
is_dynamic: Optional[bool]
|
| 188 |
+
|
| 189 |
+
def __init__(
|
| 190 |
+
self,
|
| 191 |
+
input_dtype: Union[torch.dtype, DTypeWithConstraints, None] = None,
|
| 192 |
+
output_dtype: Union[torch.dtype, DTypeWithConstraints, None] = None,
|
| 193 |
+
weight_dtype: Union[torch.dtype, DTypeWithConstraints, None] = None,
|
| 194 |
+
bias_dtype: Optional[torch.dtype] = None,
|
| 195 |
+
is_dynamic: Optional[bool] = None,
|
| 196 |
+
):
|
| 197 |
+
if isinstance(input_dtype, DTypeWithConstraints):
|
| 198 |
+
self.input_dtype_with_constraints = input_dtype
|
| 199 |
+
else:
|
| 200 |
+
self.input_dtype_with_constraints = DTypeWithConstraints(dtype=input_dtype)
|
| 201 |
+
|
| 202 |
+
if isinstance(output_dtype, DTypeWithConstraints):
|
| 203 |
+
self.output_dtype_with_constraints = output_dtype
|
| 204 |
+
else:
|
| 205 |
+
self.output_dtype_with_constraints = DTypeWithConstraints(
|
| 206 |
+
dtype=output_dtype
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
if isinstance(weight_dtype, DTypeWithConstraints):
|
| 210 |
+
self.weight_dtype_with_constraints = weight_dtype
|
| 211 |
+
else:
|
| 212 |
+
self.weight_dtype_with_constraints = DTypeWithConstraints(
|
| 213 |
+
dtype=weight_dtype
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
self.bias_dtype = bias_dtype
|
| 217 |
+
self.is_dynamic = is_dynamic
|
| 218 |
+
|
| 219 |
+
@property
|
| 220 |
+
def input_dtype(self) -> Optional[torch.dtype]:
|
| 221 |
+
return self.input_dtype_with_constraints.dtype
|
| 222 |
+
|
| 223 |
+
@property
|
| 224 |
+
def output_dtype(self) -> Optional[torch.dtype]:
|
| 225 |
+
return self.output_dtype_with_constraints.dtype
|
| 226 |
+
|
| 227 |
+
@property
|
| 228 |
+
def weight_dtype(self) -> Optional[torch.dtype]:
|
| 229 |
+
return self.weight_dtype_with_constraints.dtype
|
| 230 |
+
|
| 231 |
+
@classmethod
|
| 232 |
+
def from_dict(cls, dtype_config_dict: dict[str, Any]) -> DTypeConfig:
|
| 233 |
+
"""
|
| 234 |
+
Create a ``DTypeConfig`` from a dictionary with the following items (all optional):
|
| 235 |
+
"input_dtype": torch.dtype or ``DTypeWithConstraints``
|
| 236 |
+
"output_dtype": torch.dtype or ``DTypeWithConstraints``
|
| 237 |
+
"weight_dtype": torch.dtype or ``DTypeWithConstraints``
|
| 238 |
+
"bias_type": torch.dtype
|
| 239 |
+
"is_dynamic": bool
|
| 240 |
+
"""
|
| 241 |
+
input_dtype = dtype_config_dict.get(INPUT_DTYPE_DICT_KEY, None)
|
| 242 |
+
if input_dtype is not None and not isinstance(
|
| 243 |
+
input_dtype, (torch.dtype, DTypeWithConstraints)
|
| 244 |
+
):
|
| 245 |
+
raise ValueError(
|
| 246 |
+
"Expected input_dtype to be a torch.dtype or DTypeWithConstraints"
|
| 247 |
+
)
|
| 248 |
+
output_dtype = dtype_config_dict.get(OUTPUT_DTYPE_DICT_KEY, None)
|
| 249 |
+
if output_dtype is not None and not isinstance(
|
| 250 |
+
output_dtype, (torch.dtype, DTypeWithConstraints)
|
| 251 |
+
):
|
| 252 |
+
raise ValueError(
|
| 253 |
+
"Expected output_dtype to be a torch.dtype or DTypeWithConstraints"
|
| 254 |
+
)
|
| 255 |
+
weight_dtype = dtype_config_dict.get(WEIGHT_DTYPE_DICT_KEY, None)
|
| 256 |
+
if weight_dtype is not None and not isinstance(
|
| 257 |
+
weight_dtype, (torch.dtype, DTypeWithConstraints)
|
| 258 |
+
):
|
| 259 |
+
raise ValueError(
|
| 260 |
+
"Expected weight_dtype to be a torch.dtype or DTypeWithConstraints"
|
| 261 |
+
)
|
| 262 |
+
bias_dtype = dtype_config_dict.get(BIAS_DTYPE_DICT_KEY, None)
|
| 263 |
+
is_dynamic = dtype_config_dict.get(IS_DYNAMIC_DICT_KEY, None)
|
| 264 |
+
return cls(input_dtype, output_dtype, weight_dtype, bias_dtype, is_dynamic)
|
| 265 |
+
|
| 266 |
+
def to_dict(self) -> dict[str, Any]:
|
| 267 |
+
"""
|
| 268 |
+
Convert this ``DTypeConfig`` to a dictionary with the items described in
|
| 269 |
+
:func:`~torch.ao.quantization.backend_config.DTypeConfig.from_dict`.
|
| 270 |
+
"""
|
| 271 |
+
dtype_config_dict: dict[str, Any] = {}
|
| 272 |
+
if self.input_dtype is not None:
|
| 273 |
+
dtype_config_dict[INPUT_DTYPE_DICT_KEY] = self.input_dtype_with_constraints
|
| 274 |
+
if self.output_dtype is not None:
|
| 275 |
+
dtype_config_dict[
|
| 276 |
+
OUTPUT_DTYPE_DICT_KEY
|
| 277 |
+
] = self.output_dtype_with_constraints
|
| 278 |
+
if self.weight_dtype is not None:
|
| 279 |
+
dtype_config_dict[
|
| 280 |
+
WEIGHT_DTYPE_DICT_KEY
|
| 281 |
+
] = self.weight_dtype_with_constraints
|
| 282 |
+
if self.bias_dtype is not None:
|
| 283 |
+
dtype_config_dict[BIAS_DTYPE_DICT_KEY] = self.bias_dtype
|
| 284 |
+
if self.is_dynamic is not None:
|
| 285 |
+
dtype_config_dict[IS_DYNAMIC_DICT_KEY] = self.is_dynamic
|
| 286 |
+
return dtype_config_dict
|
| 287 |
+
|
| 288 |
+
|
| 289 |
+
class BackendConfig:
|
| 290 |
+
# TODO: refer to NativeBackendConfig once that is implemented
|
| 291 |
+
"""Config that defines the set of patterns that can be quantized on a given backend, and how reference
|
| 292 |
+
quantized models can be produced from these patterns.
|
| 293 |
+
|
| 294 |
+
A pattern in this context refers to a module, a functional, an operator, or a directed acyclic graph
|
| 295 |
+
of the above. Each pattern supported on the target backend can be individually configured through
|
| 296 |
+
:class:`~torch.ao.quantization.backend_config.BackendPatternConfig` in terms of:
|
| 297 |
+
|
| 298 |
+
(1) The supported input/output activation, weight, and bias data types
|
| 299 |
+
|
| 300 |
+
(2) How observers and quant/dequant ops are inserted in order to construct the reference pattern, and
|
| 301 |
+
|
| 302 |
+
(3) (Optionally) Fusion, QAT, and reference module mappings.
|
| 303 |
+
|
| 304 |
+
The format of the patterns is described in:
|
| 305 |
+
https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/backend_config/README.md
|
| 306 |
+
|
| 307 |
+
Example usage::
|
| 308 |
+
|
| 309 |
+
import torch
|
| 310 |
+
from torch.ao.quantization.backend_config import (
|
| 311 |
+
BackendConfig,
|
| 312 |
+
BackendPatternConfig,
|
| 313 |
+
DTypeConfig,
|
| 314 |
+
ObservationType,
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
weighted_int8_dtype_config = DTypeConfig(
|
| 318 |
+
input_dtype=torch.quint8,
|
| 319 |
+
output_dtype=torch.quint8,
|
| 320 |
+
weight_dtype=torch.qint8,
|
| 321 |
+
bias_dtype=torch.float)
|
| 322 |
+
|
| 323 |
+
def fuse_conv2d_relu(is_qat, conv, relu):
|
| 324 |
+
return torch.ao.nn.intrinsic.ConvReLU2d(conv, relu)
|
| 325 |
+
|
| 326 |
+
# For quantizing Linear
|
| 327 |
+
linear_config = BackendPatternConfig(torch.nn.Linear) \
|
| 328 |
+
.set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT) \
|
| 329 |
+
.add_dtype_config(weighted_int8_dtype_config) \
|
| 330 |
+
.set_root_module(torch.nn.Linear) \
|
| 331 |
+
.set_qat_module(torch.ao.nn.qat.Linear) \
|
| 332 |
+
.set_reference_quantized_module(torch.ao.nn.quantized.reference.Linear)
|
| 333 |
+
|
| 334 |
+
# For fusing Conv2d + ReLU into ConvReLU2d
|
| 335 |
+
conv_relu_config = BackendPatternConfig((torch.nn.Conv2d, torch.nn.ReLU)) \
|
| 336 |
+
.set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT) \
|
| 337 |
+
.add_dtype_config(weighted_int8_dtype_config) \
|
| 338 |
+
.set_fused_module(torch.ao.nn.intrinsic.ConvReLU2d) \
|
| 339 |
+
.set_fuser_method(fuse_conv2d_relu)
|
| 340 |
+
|
| 341 |
+
# For quantizing ConvReLU2d
|
| 342 |
+
fused_conv_relu_config = BackendPatternConfig(torch.ao.nn.intrinsic.ConvReLU2d) \
|
| 343 |
+
.set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT) \
|
| 344 |
+
.add_dtype_config(weighted_int8_dtype_config) \
|
| 345 |
+
.set_root_module(torch.nn.Conv2d) \
|
| 346 |
+
.set_qat_module(torch.ao.nn.intrinsic.qat.ConvReLU2d) \
|
| 347 |
+
.set_reference_quantized_module(torch.ao.nn.quantized.reference.Conv2d)
|
| 348 |
+
|
| 349 |
+
backend_config = BackendConfig("my_backend") \
|
| 350 |
+
.set_backend_pattern_config(linear_config) \
|
| 351 |
+
.set_backend_pattern_config(conv_relu_config) \
|
| 352 |
+
.set_backend_pattern_config(fused_conv_relu_config)
|
| 353 |
+
|
| 354 |
+
"""
|
| 355 |
+
|
| 356 |
+
def __init__(self, name: str = ""):
|
| 357 |
+
self.name = name
|
| 358 |
+
# Store all BackendPatternConfigs in a map to handle duplicates
|
| 359 |
+
# Note: the key in this map uses the complex reversed tuple format.
|
| 360 |
+
# This is intended only for internal use; users who wish to access
|
| 361 |
+
# the original patterns should go through `self.configs` instead.
|
| 362 |
+
self._pattern_complex_format_to_config: dict[Pattern, BackendPatternConfig] = {}
|
| 363 |
+
|
| 364 |
+
def __repr__(self):
|
| 365 |
+
return f"BackendConfig({self.__dict__})"
|
| 366 |
+
|
| 367 |
+
def set_name(self, name: str) -> BackendConfig:
|
| 368 |
+
"""
|
| 369 |
+
Set the name of the target backend.
|
| 370 |
+
"""
|
| 371 |
+
self.name = name
|
| 372 |
+
return self
|
| 373 |
+
|
| 374 |
+
def set_backend_pattern_config(self, config: BackendPatternConfig) -> BackendConfig:
|
| 375 |
+
"""
|
| 376 |
+
Set the config for an pattern that can be run on the target backend.
|
| 377 |
+
This overrides any existing config for the given pattern.
|
| 378 |
+
"""
|
| 379 |
+
# Avoid circular dependencies
|
| 380 |
+
pattern_complex_format = torch.ao.quantization.backend_config.utils._get_pattern_in_reversed_nested_tuple_format(
|
| 381 |
+
config
|
| 382 |
+
) # type: ignore[attr-defined]
|
| 383 |
+
self._pattern_complex_format_to_config[pattern_complex_format] = config
|
| 384 |
+
return self
|
| 385 |
+
|
| 386 |
+
def set_backend_pattern_configs(
|
| 387 |
+
self, configs: list[BackendPatternConfig]
|
| 388 |
+
) -> BackendConfig:
|
| 389 |
+
"""
|
| 390 |
+
Set the configs for patterns that can be run on the target backend.
|
| 391 |
+
This overrides any existing config for a given pattern if it was previously registered already.
|
| 392 |
+
"""
|
| 393 |
+
for conf in configs:
|
| 394 |
+
self.set_backend_pattern_config(conf)
|
| 395 |
+
return self
|
| 396 |
+
|
| 397 |
+
@property
|
| 398 |
+
def configs(self) -> list[BackendPatternConfig]:
|
| 399 |
+
"""
|
| 400 |
+
Return a copy of the list of configs set in this `BackendConfig`.
|
| 401 |
+
"""
|
| 402 |
+
return list(self._pattern_complex_format_to_config.values())
|
| 403 |
+
|
| 404 |
+
@classmethod
|
| 405 |
+
def from_dict(cls, backend_config_dict: dict[str, Any]) -> BackendConfig:
|
| 406 |
+
"""
|
| 407 |
+
Create a ``BackendConfig`` from a dictionary with the following items:
|
| 408 |
+
|
| 409 |
+
"name": the name of the target backend
|
| 410 |
+
|
| 411 |
+
"configs": a list of dictionaries that each represents a `BackendPatternConfig`
|
| 412 |
+
|
| 413 |
+
"""
|
| 414 |
+
conf = cls(backend_config_dict.get(NAME_DICT_KEY, ""))
|
| 415 |
+
for d in backend_config_dict.get(CONFIGS_DICT_KEY, []):
|
| 416 |
+
if isinstance(d, BackendPatternConfig):
|
| 417 |
+
conf.set_backend_pattern_config(d)
|
| 418 |
+
elif isinstance(d, dict):
|
| 419 |
+
conf.set_backend_pattern_config(BackendPatternConfig.from_dict(d))
|
| 420 |
+
else:
|
| 421 |
+
raise ValueError(
|
| 422 |
+
f"Expected backend_config_dict['{CONFIGS_DICT_KEY}'] to be a dictionary"
|
| 423 |
+
)
|
| 424 |
+
return conf
|
| 425 |
+
|
| 426 |
+
def to_dict(self) -> dict[str, Any]:
|
| 427 |
+
"""
|
| 428 |
+
Convert this ``BackendConfig`` to a dictionary with the items described in
|
| 429 |
+
:func:`~torch.ao.quantization.backend_config.BackendConfig.from_dict`.
|
| 430 |
+
"""
|
| 431 |
+
return {
|
| 432 |
+
NAME_DICT_KEY: self.name,
|
| 433 |
+
CONFIGS_DICT_KEY: [c.to_dict() for c in self.configs],
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
class BackendPatternConfig:
|
| 438 |
+
"""
|
| 439 |
+
Config object that specifies quantization behavior for a given operator pattern.
|
| 440 |
+
For a detailed example usage, see :class:`~torch.ao.quantization.backend_config.BackendConfig`.
|
| 441 |
+
"""
|
| 442 |
+
|
| 443 |
+
def __init__(self, pattern: Optional[Pattern] = None):
|
| 444 |
+
self.pattern: Optional[Pattern] = pattern
|
| 445 |
+
self.observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 446 |
+
self.dtype_configs: list[DTypeConfig] = []
|
| 447 |
+
self.root_module: Optional[type[torch.nn.Module]] = None
|
| 448 |
+
self.qat_module: Optional[type[torch.nn.Module]] = None
|
| 449 |
+
self.reference_quantized_module: Optional[type[torch.nn.Module]] = None
|
| 450 |
+
self.fused_module: Optional[type[torch.nn.Module]] = None
|
| 451 |
+
self.fuser_method: Optional[Callable] = None
|
| 452 |
+
|
| 453 |
+
# Temporary/internal configs
|
| 454 |
+
self._root_node_getter: Optional[Callable] = None
|
| 455 |
+
self._extra_inputs_getter: Optional[Callable] = None
|
| 456 |
+
self._num_tensor_args_to_observation_type: dict[int, ObservationType] = {}
|
| 457 |
+
self._input_type_to_index: dict[str, int] = {}
|
| 458 |
+
self._pattern_complex_format: Optional[Pattern] = None
|
| 459 |
+
|
| 460 |
+
def __repr__(self):
|
| 461 |
+
dict_nonempty = {
|
| 462 |
+
k: v
|
| 463 |
+
for k, v in self.__dict__.items()
|
| 464 |
+
if (
|
| 465 |
+
(not isinstance(v, (list, dict)) and v is not None)
|
| 466 |
+
or (isinstance(v, (list, dict)) and len(v) > 0)
|
| 467 |
+
)
|
| 468 |
+
}
|
| 469 |
+
return f"BackendPatternConfig({dict_nonempty})"
|
| 470 |
+
|
| 471 |
+
def set_pattern(self, pattern: Pattern) -> BackendPatternConfig:
|
| 472 |
+
"""
|
| 473 |
+
Set the pattern to configure.
|
| 474 |
+
|
| 475 |
+
The pattern can be a float module, functional operator, pytorch operator, or a tuple
|
| 476 |
+
combination of the above. Tuple patterns are treated as sequential patterns, and
|
| 477 |
+
currently only tuples of 2 or 3 elements are supported.
|
| 478 |
+
"""
|
| 479 |
+
if self._pattern_complex_format is not None:
|
| 480 |
+
raise ValueError(
|
| 481 |
+
"Only one of 'pattern' or 'pattern_complex_format' can be set"
|
| 482 |
+
)
|
| 483 |
+
self.pattern = pattern
|
| 484 |
+
return self
|
| 485 |
+
|
| 486 |
+
def set_observation_type(
|
| 487 |
+
self, observation_type: ObservationType
|
| 488 |
+
) -> BackendPatternConfig:
|
| 489 |
+
"""
|
| 490 |
+
Set how observers should be inserted in the graph for this pattern.
|
| 491 |
+
|
| 492 |
+
Observation type here refers to how observers (or quant-dequant ops) will be placed
|
| 493 |
+
in the graph. This is used to produce the desired reference patterns understood by
|
| 494 |
+
the backend. Weighted ops such as linear and conv require different observers
|
| 495 |
+
(or quantization parameters passed to quantize ops in the reference model) for the
|
| 496 |
+
input and the output.
|
| 497 |
+
|
| 498 |
+
There are two observation types:
|
| 499 |
+
|
| 500 |
+
`OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT` (default): the output observer instance
|
| 501 |
+
will be different from the input. This is the most common observation type.
|
| 502 |
+
|
| 503 |
+
`OUTPUT_SHARE_OBSERVER_WITH_INPUT`: the output observer instance will be the
|
| 504 |
+
same as the input. This is useful for operators like `cat`.
|
| 505 |
+
|
| 506 |
+
Note: This will be renamed in the near future, since we will soon insert QuantDeQuantStubs
|
| 507 |
+
with observers (and fake quantizes) attached instead of observers themselves.
|
| 508 |
+
"""
|
| 509 |
+
self.observation_type = observation_type
|
| 510 |
+
return self
|
| 511 |
+
|
| 512 |
+
def add_dtype_config(self, dtype_config: DTypeConfig) -> BackendPatternConfig:
|
| 513 |
+
"""
|
| 514 |
+
Add a set of supported data types passed as arguments to quantize ops in the
|
| 515 |
+
reference model spec.
|
| 516 |
+
"""
|
| 517 |
+
self.dtype_configs.append(dtype_config)
|
| 518 |
+
return self
|
| 519 |
+
|
| 520 |
+
def set_dtype_configs(
|
| 521 |
+
self, dtype_configs: list[DTypeConfig]
|
| 522 |
+
) -> BackendPatternConfig:
|
| 523 |
+
"""
|
| 524 |
+
Set the supported data types passed as arguments to quantize ops in the
|
| 525 |
+
reference model spec, overriding all previously registered data types.
|
| 526 |
+
"""
|
| 527 |
+
self.dtype_configs = dtype_configs
|
| 528 |
+
return self
|
| 529 |
+
|
| 530 |
+
def set_root_module(
|
| 531 |
+
self, root_module: type[torch.nn.Module]
|
| 532 |
+
) -> BackendPatternConfig:
|
| 533 |
+
"""
|
| 534 |
+
Set the module that represents the root for this pattern.
|
| 535 |
+
|
| 536 |
+
When we construct the reference quantized model during the convert phase,
|
| 537 |
+
the root modules (e.g. torch.nn.Linear for torch.ao.nn.intrinsic.LinearReLU)
|
| 538 |
+
will be swapped to the corresponding reference quantized modules (e.g.
|
| 539 |
+
torch.ao.nn.reference.quantized.Linear). This allows custom backends to
|
| 540 |
+
specify custom reference quantized module implementations to match the
|
| 541 |
+
numerics of their lowered operators. Since this is a one-to-one mapping,
|
| 542 |
+
both the root module and the reference quantized module must be specified
|
| 543 |
+
in the same BackendPatternConfig in order for the conversion to take place.
|
| 544 |
+
"""
|
| 545 |
+
self.root_module = root_module
|
| 546 |
+
return self
|
| 547 |
+
|
| 548 |
+
def set_qat_module(self, qat_module: type[torch.nn.Module]) -> BackendPatternConfig:
|
| 549 |
+
"""
|
| 550 |
+
Set the module that represents the QAT implementation for this pattern.
|
| 551 |
+
"""
|
| 552 |
+
self.qat_module = qat_module
|
| 553 |
+
return self
|
| 554 |
+
|
| 555 |
+
def set_reference_quantized_module(
|
| 556 |
+
self, reference_quantized_module: type[torch.nn.Module]
|
| 557 |
+
) -> BackendPatternConfig:
|
| 558 |
+
"""
|
| 559 |
+
Set the module that represents the reference quantized implementation for
|
| 560 |
+
this pattern's root module.
|
| 561 |
+
|
| 562 |
+
For more detail, see :func:`~torch.ao.quantization.backend_config.BackendPatternConfig.set_root_module`.
|
| 563 |
+
"""
|
| 564 |
+
self.reference_quantized_module = reference_quantized_module
|
| 565 |
+
return self
|
| 566 |
+
|
| 567 |
+
def set_fused_module(
|
| 568 |
+
self, fused_module: type[torch.nn.Module]
|
| 569 |
+
) -> BackendPatternConfig:
|
| 570 |
+
"""
|
| 571 |
+
Set the module that represents the fused implementation for this pattern.
|
| 572 |
+
"""
|
| 573 |
+
self.fused_module = fused_module
|
| 574 |
+
return self
|
| 575 |
+
|
| 576 |
+
def set_fuser_method(self, fuser_method: Callable) -> BackendPatternConfig:
|
| 577 |
+
"""
|
| 578 |
+
Set the function that specifies how to fuse this BackendPatternConfig's pattern.
|
| 579 |
+
|
| 580 |
+
The first argument of this function should be `is_qat`, and the rest of the arguments
|
| 581 |
+
should be the items in the tuple pattern. The return value of this function should be
|
| 582 |
+
the resulting fused module.
|
| 583 |
+
|
| 584 |
+
For example, the fuser method for the pattern `(torch.nn.Linear, torch.nn.ReLU)` can be:
|
| 585 |
+
|
| 586 |
+
def fuse_linear_relu(is_qat, linear, relu):
|
| 587 |
+
return torch.ao.nn.intrinsic.LinearReLU(linear, relu)
|
| 588 |
+
|
| 589 |
+
For a more complicated example, see https://gist.github.com/jerryzh168/8bea7180a8ba3c279f2c9b050f2a69a6.
|
| 590 |
+
"""
|
| 591 |
+
self.fuser_method = fuser_method
|
| 592 |
+
return self
|
| 593 |
+
|
| 594 |
+
def _set_root_node_getter(self, root_node_getter: Callable) -> BackendPatternConfig:
|
| 595 |
+
self._root_node_getter = root_node_getter
|
| 596 |
+
return self
|
| 597 |
+
|
| 598 |
+
def _set_extra_inputs_getter(
|
| 599 |
+
self, extra_inputs_getter: Callable
|
| 600 |
+
) -> BackendPatternConfig:
|
| 601 |
+
self._extra_inputs_getter = extra_inputs_getter
|
| 602 |
+
return self
|
| 603 |
+
|
| 604 |
+
def _set_num_tensor_args_to_observation_type(
|
| 605 |
+
self, num_tensor_args_to_observation_type: dict[int, ObservationType]
|
| 606 |
+
) -> BackendPatternConfig:
|
| 607 |
+
self._num_tensor_args_to_observation_type = num_tensor_args_to_observation_type
|
| 608 |
+
return self
|
| 609 |
+
|
| 610 |
+
def _set_input_type_to_index(
|
| 611 |
+
self, input_type_to_index: dict[str, int]
|
| 612 |
+
) -> BackendPatternConfig:
|
| 613 |
+
self._input_type_to_index = input_type_to_index
|
| 614 |
+
return self
|
| 615 |
+
|
| 616 |
+
def _set_pattern_complex_format(self, pattern: Pattern) -> BackendPatternConfig:
|
| 617 |
+
"""
|
| 618 |
+
Set the pattern to configure, using the reversed nested tuple format.
|
| 619 |
+
|
| 620 |
+
See the BackendConfig README for more detail:
|
| 621 |
+
https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/backend_config/README.md#advanced-pattern-specification
|
| 622 |
+
"""
|
| 623 |
+
if self.pattern is not None:
|
| 624 |
+
raise ValueError(
|
| 625 |
+
"Only one of 'pattern' or 'pattern_complex_format' can be set"
|
| 626 |
+
)
|
| 627 |
+
self._pattern_complex_format = pattern
|
| 628 |
+
return self
|
| 629 |
+
|
| 630 |
+
@classmethod
|
| 631 |
+
def from_dict(
|
| 632 |
+
cls, backend_pattern_config_dict: dict[str, Any]
|
| 633 |
+
) -> BackendPatternConfig:
|
| 634 |
+
"""
|
| 635 |
+
Create a ``BackendPatternConfig`` from a dictionary with the following items:
|
| 636 |
+
|
| 637 |
+
"pattern": the pattern being configured
|
| 638 |
+
"observation_type": the :class:`~torch.ao.quantization.backend_config.ObservationType` that specifies how
|
| 639 |
+
observers should be inserted for this pattern
|
| 640 |
+
"dtype_configs": a list of dictionaries that represents :class:`~torch.ao.quantization.backend_config.DTypeConfig` s
|
| 641 |
+
"root_module": a :class:`torch.nn.Module` that represents the root for this pattern
|
| 642 |
+
"qat_module": a :class:`torch.nn.Module` that represents the QAT implementation for this pattern
|
| 643 |
+
"reference_quantized_module": a :class:`torch.nn.Module` that represents the reference quantized
|
| 644 |
+
implementation for this pattern's root module.
|
| 645 |
+
"fused_module": a :class:`torch.nn.Module` that represents the fused implementation for this pattern
|
| 646 |
+
"fuser_method": a function that specifies how to fuse the pattern for this pattern
|
| 647 |
+
"pattern_complex_format": the pattern specified in the reversed nested tuple format (deprecated)
|
| 648 |
+
|
| 649 |
+
"""
|
| 650 |
+
|
| 651 |
+
def _get_dtype_config(obj: Any) -> DTypeConfig:
|
| 652 |
+
"""
|
| 653 |
+
Convert the given object into a ``DTypeConfig`` if possible, else throw an exception.
|
| 654 |
+
"""
|
| 655 |
+
if isinstance(obj, DTypeConfig):
|
| 656 |
+
return obj
|
| 657 |
+
if isinstance(obj, dict):
|
| 658 |
+
return DTypeConfig.from_dict(obj)
|
| 659 |
+
raise ValueError(
|
| 660 |
+
f"Expected a list of DTypeConfigs in "
|
| 661 |
+
f"backend_pattern_config_dict[\"{DTYPE_CONFIGS_DICT_KEY}\"], got '{type(obj)}'"
|
| 662 |
+
)
|
| 663 |
+
|
| 664 |
+
conf = cls()
|
| 665 |
+
if PATTERN_DICT_KEY in backend_pattern_config_dict:
|
| 666 |
+
conf.set_pattern(backend_pattern_config_dict[PATTERN_DICT_KEY])
|
| 667 |
+
if OBSERVATION_TYPE_DICT_KEY in backend_pattern_config_dict:
|
| 668 |
+
conf.set_observation_type(
|
| 669 |
+
backend_pattern_config_dict[OBSERVATION_TYPE_DICT_KEY]
|
| 670 |
+
)
|
| 671 |
+
for d in backend_pattern_config_dict.get(DTYPE_CONFIGS_DICT_KEY, []):
|
| 672 |
+
conf.add_dtype_config(_get_dtype_config(d))
|
| 673 |
+
conf.set_root_module(
|
| 674 |
+
backend_pattern_config_dict.get(ROOT_MODULE_DICT_KEY, None)
|
| 675 |
+
)
|
| 676 |
+
conf.set_qat_module(backend_pattern_config_dict.get(QAT_MODULE_DICT_KEY, None))
|
| 677 |
+
conf.set_reference_quantized_module(
|
| 678 |
+
backend_pattern_config_dict.get(REFERENCE_QUANTIZED_MODULE_DICT_KEY, None)
|
| 679 |
+
)
|
| 680 |
+
conf.set_fused_module(
|
| 681 |
+
backend_pattern_config_dict.get(FUSED_MODULE_DICT_KEY, None)
|
| 682 |
+
)
|
| 683 |
+
conf.set_fuser_method(
|
| 684 |
+
backend_pattern_config_dict.get(FUSER_METHOD_DICT_KEY, None)
|
| 685 |
+
)
|
| 686 |
+
conf._set_root_node_getter(
|
| 687 |
+
backend_pattern_config_dict.get(ROOT_NODE_GETTER_DICT_KEY, None)
|
| 688 |
+
)
|
| 689 |
+
conf._set_extra_inputs_getter(
|
| 690 |
+
backend_pattern_config_dict.get(EXTRA_INPUTS_GETTER_DICT_KEY, None)
|
| 691 |
+
)
|
| 692 |
+
conf._set_num_tensor_args_to_observation_type(
|
| 693 |
+
backend_pattern_config_dict.get(
|
| 694 |
+
NUM_TENSOR_ARGS_TO_OBSERVATION_TYPE_DICT_KEY, {}
|
| 695 |
+
)
|
| 696 |
+
)
|
| 697 |
+
conf._set_input_type_to_index(
|
| 698 |
+
backend_pattern_config_dict.get(INPUT_TYPE_TO_INDEX_DICT_KEY, {})
|
| 699 |
+
)
|
| 700 |
+
if PATTERN_COMPLEX_FORMAT_DICT_KEY in backend_pattern_config_dict:
|
| 701 |
+
conf._set_pattern_complex_format(
|
| 702 |
+
backend_pattern_config_dict[PATTERN_COMPLEX_FORMAT_DICT_KEY]
|
| 703 |
+
)
|
| 704 |
+
return conf
|
| 705 |
+
|
| 706 |
+
def to_dict(self) -> dict[str, Any]:
|
| 707 |
+
"""
|
| 708 |
+
Convert this ``BackendPatternConfig`` to a dictionary with the items described in
|
| 709 |
+
:func:`~torch.ao.quantization.backend_config.BackendPatternConfig.from_dict`.
|
| 710 |
+
"""
|
| 711 |
+
backend_pattern_config_dict: dict[str, Any] = {
|
| 712 |
+
OBSERVATION_TYPE_DICT_KEY: self.observation_type,
|
| 713 |
+
DTYPE_CONFIGS_DICT_KEY: [c.to_dict() for c in self.dtype_configs],
|
| 714 |
+
}
|
| 715 |
+
if self.pattern is not None:
|
| 716 |
+
backend_pattern_config_dict[PATTERN_DICT_KEY] = self.pattern
|
| 717 |
+
if self.root_module is not None:
|
| 718 |
+
backend_pattern_config_dict[ROOT_MODULE_DICT_KEY] = self.root_module
|
| 719 |
+
if self.qat_module is not None:
|
| 720 |
+
backend_pattern_config_dict[QAT_MODULE_DICT_KEY] = self.qat_module
|
| 721 |
+
if self.reference_quantized_module is not None:
|
| 722 |
+
backend_pattern_config_dict[
|
| 723 |
+
REFERENCE_QUANTIZED_MODULE_DICT_KEY
|
| 724 |
+
] = self.reference_quantized_module
|
| 725 |
+
if self.fused_module is not None:
|
| 726 |
+
backend_pattern_config_dict[FUSED_MODULE_DICT_KEY] = self.fused_module
|
| 727 |
+
if self.fuser_method is not None:
|
| 728 |
+
backend_pattern_config_dict[FUSER_METHOD_DICT_KEY] = self.fuser_method
|
| 729 |
+
if self._root_node_getter is not None:
|
| 730 |
+
backend_pattern_config_dict[
|
| 731 |
+
ROOT_NODE_GETTER_DICT_KEY
|
| 732 |
+
] = self._root_node_getter
|
| 733 |
+
if self._extra_inputs_getter is not None:
|
| 734 |
+
backend_pattern_config_dict[
|
| 735 |
+
EXTRA_INPUTS_GETTER_DICT_KEY
|
| 736 |
+
] = self._extra_inputs_getter
|
| 737 |
+
if len(self._num_tensor_args_to_observation_type) > 0:
|
| 738 |
+
backend_pattern_config_dict[
|
| 739 |
+
NUM_TENSOR_ARGS_TO_OBSERVATION_TYPE_DICT_KEY
|
| 740 |
+
] = self._num_tensor_args_to_observation_type
|
| 741 |
+
if len(self._input_type_to_index) > 0:
|
| 742 |
+
backend_pattern_config_dict[
|
| 743 |
+
INPUT_TYPE_TO_INDEX_DICT_KEY
|
| 744 |
+
] = self._input_type_to_index
|
| 745 |
+
if self._pattern_complex_format is not None:
|
| 746 |
+
backend_pattern_config_dict[
|
| 747 |
+
PATTERN_COMPLEX_FORMAT_DICT_KEY
|
| 748 |
+
] = self._pattern_complex_format
|
| 749 |
+
return backend_pattern_config_dict
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/executorch.py
ADDED
|
@@ -0,0 +1,498 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TODO: rename executorch to qnnpack_executorch since executorch is a general runtime
|
| 2 |
+
# not a specific backend
|
| 3 |
+
|
| 4 |
+
import operator
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import torch.ao.nn.qat as nnqat
|
| 8 |
+
import torch.ao.nn.quantized.reference as nnqr
|
| 9 |
+
import torch.nn as nn
|
| 10 |
+
import torch.nn.functional as F
|
| 11 |
+
from torch.ao.quantization.fuser_method_mappings import (
|
| 12 |
+
_sequential_wrapper2,
|
| 13 |
+
fuse_conv_bn,
|
| 14 |
+
fuse_conv_bn_relu,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
from ._common_operator_config_utils import _Conv2dMetadata
|
| 18 |
+
from .backend_config import (
|
| 19 |
+
BackendConfig,
|
| 20 |
+
BackendPatternConfig,
|
| 21 |
+
DTypeConfig,
|
| 22 |
+
DTypeWithConstraints,
|
| 23 |
+
ObservationType,
|
| 24 |
+
)
|
| 25 |
+
from .qnnpack import (
|
| 26 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 27 |
+
qnnpack_weighted_op_qint8_symmetric_dtype_config,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
__all__ = [
|
| 32 |
+
"get_executorch_backend_config",
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# ===================
|
| 37 |
+
# | DTYPE CONFIGS |
|
| 38 |
+
# ===================
|
| 39 |
+
|
| 40 |
+
executorch_weighted_op_int8_dtype_config = DTypeConfig(
|
| 41 |
+
input_dtype=torch.quint8,
|
| 42 |
+
output_dtype=torch.quint8,
|
| 43 |
+
weight_dtype=torch.qint8,
|
| 44 |
+
bias_dtype=torch.float,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
executorch_default_op_quint8_dtype_config = DTypeConfig(
|
| 48 |
+
input_dtype=torch.quint8,
|
| 49 |
+
output_dtype=torch.quint8,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
executorch_default_dynamic_quint8_dtype_config = DTypeConfig(
|
| 53 |
+
input_dtype=torch.quint8,
|
| 54 |
+
output_dtype=torch.float,
|
| 55 |
+
weight_dtype=torch.qint8,
|
| 56 |
+
bias_dtype=torch.float,
|
| 57 |
+
is_dynamic=True,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
executorch_act_qint8_scale_min_2_neg_12 = DTypeWithConstraints(
|
| 61 |
+
dtype=torch.qint8,
|
| 62 |
+
scale_min_lower_bound=2**-12,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
executorch_weight_qint8_neg_127_to_127_scale_min_2_neg_12 = DTypeWithConstraints(
|
| 66 |
+
dtype=torch.qint8,
|
| 67 |
+
quant_min_lower_bound=-127,
|
| 68 |
+
quant_max_upper_bound=127,
|
| 69 |
+
scale_min_lower_bound=2**-12,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
executorch_default_dynamic_qint8_dtype_config = DTypeConfig(
|
| 73 |
+
input_dtype=executorch_act_qint8_scale_min_2_neg_12,
|
| 74 |
+
output_dtype=torch.float,
|
| 75 |
+
weight_dtype=executorch_weight_qint8_neg_127_to_127_scale_min_2_neg_12,
|
| 76 |
+
bias_dtype=torch.float,
|
| 77 |
+
is_dynamic=True,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
executorch_default_dynamic_float16_dtype_config = DTypeConfig(
|
| 81 |
+
input_dtype=torch.float16,
|
| 82 |
+
output_dtype=torch.float,
|
| 83 |
+
weight_dtype=torch.float16,
|
| 84 |
+
bias_dtype=torch.float,
|
| 85 |
+
is_dynamic=True,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
executorch_weight_only_quint8_dtype_config = DTypeConfig(
|
| 89 |
+
input_dtype=torch.float,
|
| 90 |
+
output_dtype=torch.float,
|
| 91 |
+
weight_dtype=torch.quint8,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# =============================
|
| 96 |
+
# | BACKEND PATTERN CONFIGS |
|
| 97 |
+
# =============================
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _get_linear_configs() -> list[BackendPatternConfig]:
|
| 101 |
+
"""
|
| 102 |
+
Return all configs related to linear modules and ops.
|
| 103 |
+
"""
|
| 104 |
+
observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 105 |
+
dtype_configs = [
|
| 106 |
+
qnnpack_weighted_op_qint8_symmetric_dtype_config,
|
| 107 |
+
executorch_weighted_op_int8_dtype_config,
|
| 108 |
+
executorch_default_dynamic_quint8_dtype_config,
|
| 109 |
+
executorch_default_dynamic_qint8_dtype_config,
|
| 110 |
+
executorch_default_dynamic_float16_dtype_config,
|
| 111 |
+
]
|
| 112 |
+
linear_configs: list[BackendPatternConfig] = []
|
| 113 |
+
# linear module
|
| 114 |
+
linear_configs.append(
|
| 115 |
+
BackendPatternConfig(torch.nn.Linear)
|
| 116 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 117 |
+
.set_dtype_configs(dtype_configs)
|
| 118 |
+
.set_root_module(torch.nn.Linear)
|
| 119 |
+
.set_reference_quantized_module(nnqr.Linear)
|
| 120 |
+
.set_qat_module(nnqat.Linear)
|
| 121 |
+
)
|
| 122 |
+
# linear qat module
|
| 123 |
+
linear_configs.append(
|
| 124 |
+
BackendPatternConfig(nnqat.Linear)
|
| 125 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 126 |
+
.set_dtype_configs(dtype_configs)
|
| 127 |
+
.set_root_module(torch.nn.Linear)
|
| 128 |
+
.set_reference_quantized_module(nnqr.Linear)
|
| 129 |
+
)
|
| 130 |
+
# functional linear
|
| 131 |
+
linear_configs.append(
|
| 132 |
+
BackendPatternConfig(torch.nn.functional.linear)
|
| 133 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 134 |
+
.set_dtype_configs(dtype_configs)
|
| 135 |
+
._set_input_type_to_index({"weight": 1, "bias": 2})
|
| 136 |
+
)
|
| 137 |
+
return linear_configs
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def _get_conv_configs() -> list[BackendPatternConfig]:
|
| 141 |
+
"""
|
| 142 |
+
Return all configs related to conv modules and ops.
|
| 143 |
+
"""
|
| 144 |
+
observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 145 |
+
dtype_configs = [
|
| 146 |
+
qnnpack_weighted_op_qint8_symmetric_dtype_config,
|
| 147 |
+
executorch_weighted_op_int8_dtype_config,
|
| 148 |
+
]
|
| 149 |
+
conv_configs = []
|
| 150 |
+
for convs in [_Conv2dMetadata]:
|
| 151 |
+
# (1) Single conv modules/functions
|
| 152 |
+
# -----------------------------------
|
| 153 |
+
# conv module
|
| 154 |
+
conv_configs.append(
|
| 155 |
+
BackendPatternConfig(convs.root)
|
| 156 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 157 |
+
.set_dtype_configs(dtype_configs)
|
| 158 |
+
.set_root_module(convs.root)
|
| 159 |
+
.set_reference_quantized_module(convs.reference)
|
| 160 |
+
.set_qat_module(convs.qat)
|
| 161 |
+
)
|
| 162 |
+
# conv qat module
|
| 163 |
+
conv_configs.append(
|
| 164 |
+
BackendPatternConfig(convs.qat)
|
| 165 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 166 |
+
.set_dtype_configs(dtype_configs)
|
| 167 |
+
.set_root_module(convs.root)
|
| 168 |
+
.set_reference_quantized_module(convs.reference)
|
| 169 |
+
)
|
| 170 |
+
# functional conv
|
| 171 |
+
conv_configs.append(
|
| 172 |
+
BackendPatternConfig(convs.func)
|
| 173 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 174 |
+
.set_dtype_configs(dtype_configs)
|
| 175 |
+
._set_input_type_to_index({"weight": 1, "bias": 2})
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
# (2) Conv + relu
|
| 179 |
+
# -----------------------------------
|
| 180 |
+
# conv module + relu module
|
| 181 |
+
conv_configs.append(
|
| 182 |
+
BackendPatternConfig((convs.root, nn.ReLU))
|
| 183 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 184 |
+
.set_fuser_method(_sequential_wrapper2(convs.fused_conv_relu))
|
| 185 |
+
.set_fused_module(convs.fused_conv_relu)
|
| 186 |
+
)
|
| 187 |
+
# conv module + functional relu
|
| 188 |
+
conv_configs.append(
|
| 189 |
+
BackendPatternConfig((convs.root, F.relu))
|
| 190 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 191 |
+
.set_fuser_method(_sequential_wrapper2(convs.fused_conv_relu))
|
| 192 |
+
.set_fused_module(convs.fused_conv_relu)
|
| 193 |
+
)
|
| 194 |
+
# fused conv relu module
|
| 195 |
+
conv_configs.append(
|
| 196 |
+
BackendPatternConfig(convs.fused_conv_relu)
|
| 197 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 198 |
+
.set_dtype_configs(dtype_configs)
|
| 199 |
+
.set_root_module(convs.root)
|
| 200 |
+
.set_reference_quantized_module(convs.reference)
|
| 201 |
+
.set_qat_module(convs.relu_qat)
|
| 202 |
+
)
|
| 203 |
+
# conv relu, qat fused module
|
| 204 |
+
conv_configs.append(
|
| 205 |
+
BackendPatternConfig(convs.relu_qat)
|
| 206 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 207 |
+
.set_dtype_configs(dtype_configs)
|
| 208 |
+
.set_root_module(convs.root)
|
| 209 |
+
.set_reference_quantized_module(convs.reference)
|
| 210 |
+
)
|
| 211 |
+
# functional conv + relu module
|
| 212 |
+
conv_configs.append(
|
| 213 |
+
BackendPatternConfig((convs.func, nn.ReLU))
|
| 214 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 215 |
+
.set_dtype_configs(dtype_configs)
|
| 216 |
+
)
|
| 217 |
+
# functional conv + functional relu
|
| 218 |
+
conv_configs.append(
|
| 219 |
+
BackendPatternConfig((convs.func, F.relu))
|
| 220 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 221 |
+
.set_dtype_configs(dtype_configs)
|
| 222 |
+
)
|
| 223 |
+
# fused conv relu
|
| 224 |
+
conv_configs.append(
|
| 225 |
+
BackendPatternConfig(convs.fused_conv_relu)
|
| 226 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 227 |
+
.set_qat_module(convs.relu_qat)
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
conv_configs.append(
|
| 231 |
+
BackendPatternConfig(convs.relu_qat)
|
| 232 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 233 |
+
.set_root_module(convs.root)
|
| 234 |
+
.set_reference_quantized_module(convs.reference)
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
# (3) Conv + batchnorm (+ relu)
|
| 238 |
+
# -------------------------------
|
| 239 |
+
# conv + batchnorm (+ relu)
|
| 240 |
+
conv_configs.append(
|
| 241 |
+
BackendPatternConfig((convs.root, convs.bn))
|
| 242 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 243 |
+
.set_fuser_method(fuse_conv_bn)
|
| 244 |
+
.set_fused_module(convs.fused_conv_bn)
|
| 245 |
+
)
|
| 246 |
+
# conv + bn + relu module fusion
|
| 247 |
+
conv_configs.append(
|
| 248 |
+
BackendPatternConfig((convs.root, convs.bn, nn.ReLU))
|
| 249 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 250 |
+
.set_fuser_method(fuse_conv_bn_relu)
|
| 251 |
+
.set_fused_module(convs.fused_conv_bn_relu)
|
| 252 |
+
)
|
| 253 |
+
# conv + bn + relu functional fusion
|
| 254 |
+
conv_configs.append(
|
| 255 |
+
BackendPatternConfig((convs.root, convs.bn, F.relu))
|
| 256 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 257 |
+
.set_root_module(convs.root)
|
| 258 |
+
.set_fuser_method(fuse_conv_bn_relu)
|
| 259 |
+
.set_fused_module(convs.fused_conv_bn_relu)
|
| 260 |
+
)
|
| 261 |
+
# TODO: we can add fusion for torch.relu as well
|
| 262 |
+
# 3.2 conv + bn (+ relu) fused module configs
|
| 263 |
+
# fused conv bn
|
| 264 |
+
conv_configs.append(
|
| 265 |
+
BackendPatternConfig(convs.fused_conv_bn)
|
| 266 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 267 |
+
.set_qat_module(convs.bn_qat)
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
# fused conv bn relu
|
| 271 |
+
conv_configs.append(
|
| 272 |
+
BackendPatternConfig(convs.fused_conv_bn_relu)
|
| 273 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 274 |
+
.set_qat_module(convs.bn_relu_qat)
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
# conv bn, qat fused module
|
| 278 |
+
conv_configs.append(
|
| 279 |
+
BackendPatternConfig(convs.bn_qat)
|
| 280 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 281 |
+
.set_dtype_configs(dtype_configs)
|
| 282 |
+
.set_root_module(convs.root)
|
| 283 |
+
.set_reference_quantized_module(convs.reference)
|
| 284 |
+
)
|
| 285 |
+
# conv bn relu, qat fused module
|
| 286 |
+
conv_configs.append(
|
| 287 |
+
BackendPatternConfig(convs.bn_relu_qat)
|
| 288 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 289 |
+
.set_dtype_configs(dtype_configs)
|
| 290 |
+
.set_root_module(convs.root)
|
| 291 |
+
.set_reference_quantized_module(convs.reference)
|
| 292 |
+
)
|
| 293 |
+
return conv_configs
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def _get_binary_ops_configs() -> list[BackendPatternConfig]:
|
| 297 |
+
"""
|
| 298 |
+
Return all configs related to binary ops.
|
| 299 |
+
"""
|
| 300 |
+
dtype_configs = [
|
| 301 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 302 |
+
executorch_weighted_op_int8_dtype_config,
|
| 303 |
+
]
|
| 304 |
+
num_tensor_args_to_observation_type_mapping = {
|
| 305 |
+
# TODO: this is not used right now since we have extra check in prepare
|
| 306 |
+
# will need to change this to NO_OBSERVER later after we implemented
|
| 307 |
+
# Tensor dtype inference properly
|
| 308 |
+
0: ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT,
|
| 309 |
+
1: ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT,
|
| 310 |
+
2: ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT,
|
| 311 |
+
}
|
| 312 |
+
binary_op_configs: list[BackendPatternConfig] = []
|
| 313 |
+
for op in [
|
| 314 |
+
operator.add,
|
| 315 |
+
torch.add,
|
| 316 |
+
operator.sub,
|
| 317 |
+
torch.sub,
|
| 318 |
+
operator.mul,
|
| 319 |
+
torch.mul,
|
| 320 |
+
]:
|
| 321 |
+
bop_patterns = [
|
| 322 |
+
(op, torch.nn.ReLU),
|
| 323 |
+
(op, torch.nn.functional.relu),
|
| 324 |
+
(op, torch.relu),
|
| 325 |
+
op,
|
| 326 |
+
]
|
| 327 |
+
binary_op_configs.extend(
|
| 328 |
+
BackendPatternConfig(bop_pattern)
|
| 329 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 330 |
+
._set_num_tensor_args_to_observation_type(
|
| 331 |
+
num_tensor_args_to_observation_type_mapping
|
| 332 |
+
)
|
| 333 |
+
for bop_pattern in bop_patterns
|
| 334 |
+
)
|
| 335 |
+
return binary_op_configs
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
def _get_share_qparams_ops_configs() -> list[BackendPatternConfig]:
|
| 339 |
+
"""
|
| 340 |
+
Return the operator configs for the operators that works for both float and quantized
|
| 341 |
+
input if input is quantized, the output Tensor shares the same quantization parameter
|
| 342 |
+
with input.
|
| 343 |
+
|
| 344 |
+
Example operator: avgpool2d, reshape, transpose, maxpool2d
|
| 345 |
+
Example observed operator:
|
| 346 |
+
observer_0 - avgpool2d - observer_0 (same observer instance as input)
|
| 347 |
+
"""
|
| 348 |
+
observation_type = ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT
|
| 349 |
+
dtype_configs = [
|
| 350 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 351 |
+
executorch_default_op_quint8_dtype_config,
|
| 352 |
+
]
|
| 353 |
+
share_qparams_ops = [
|
| 354 |
+
torch.nn.Flatten,
|
| 355 |
+
F.adaptive_avg_pool2d,
|
| 356 |
+
F.elu,
|
| 357 |
+
F.hardtanh,
|
| 358 |
+
F.max_pool2d,
|
| 359 |
+
F.pad,
|
| 360 |
+
F.relu,
|
| 361 |
+
F.relu6,
|
| 362 |
+
F.leaky_relu,
|
| 363 |
+
F.leaky_relu_,
|
| 364 |
+
torch.nn.AdaptiveAvgPool2d,
|
| 365 |
+
torch.nn.ConstantPad2d,
|
| 366 |
+
torch.nn.ELU,
|
| 367 |
+
torch.nn.MaxPool2d,
|
| 368 |
+
torch.nn.ReLU6,
|
| 369 |
+
torch.nn.Hardtanh,
|
| 370 |
+
torch.nn.LeakyReLU,
|
| 371 |
+
torch.clamp,
|
| 372 |
+
torch.flatten,
|
| 373 |
+
torch.mean,
|
| 374 |
+
torch.permute,
|
| 375 |
+
torch.permute_copy,
|
| 376 |
+
torch.squeeze,
|
| 377 |
+
"clamp",
|
| 378 |
+
"mean",
|
| 379 |
+
"permute",
|
| 380 |
+
"reshape",
|
| 381 |
+
"relu",
|
| 382 |
+
"relu_",
|
| 383 |
+
"squeeze",
|
| 384 |
+
"squeeze_",
|
| 385 |
+
"leaky_relu",
|
| 386 |
+
]
|
| 387 |
+
share_qparams_op_configs: list[BackendPatternConfig] = [
|
| 388 |
+
BackendPatternConfig(op)
|
| 389 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 390 |
+
.set_dtype_configs(dtype_configs)
|
| 391 |
+
for op in share_qparams_ops
|
| 392 |
+
]
|
| 393 |
+
return share_qparams_op_configs
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
def _get_bn_configs() -> list[BackendPatternConfig]:
|
| 397 |
+
"""
|
| 398 |
+
Return all configs related to batchnorm.
|
| 399 |
+
"""
|
| 400 |
+
observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 401 |
+
dtype_configs = [
|
| 402 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 403 |
+
executorch_default_op_quint8_dtype_config,
|
| 404 |
+
]
|
| 405 |
+
bn_configs = []
|
| 406 |
+
bn_configs.append(
|
| 407 |
+
BackendPatternConfig(nn.BatchNorm2d)
|
| 408 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 409 |
+
.set_dtype_configs(dtype_configs)
|
| 410 |
+
)
|
| 411 |
+
return bn_configs
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
def _get_cat_configs() -> list[BackendPatternConfig]:
|
| 415 |
+
dtype_configs = [
|
| 416 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 417 |
+
executorch_default_op_quint8_dtype_config,
|
| 418 |
+
]
|
| 419 |
+
cat_configs = []
|
| 420 |
+
cat_configs.append(
|
| 421 |
+
BackendPatternConfig(torch.cat)
|
| 422 |
+
.set_observation_type(ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT)
|
| 423 |
+
.set_dtype_configs(dtype_configs)
|
| 424 |
+
)
|
| 425 |
+
cat_configs.append(
|
| 426 |
+
BackendPatternConfig(torch.concat)
|
| 427 |
+
.set_observation_type(ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT)
|
| 428 |
+
.set_dtype_configs(dtype_configs)
|
| 429 |
+
)
|
| 430 |
+
cat_configs.append(
|
| 431 |
+
BackendPatternConfig(torch.concatenate)
|
| 432 |
+
.set_observation_type(ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT)
|
| 433 |
+
.set_dtype_configs(dtype_configs)
|
| 434 |
+
)
|
| 435 |
+
return cat_configs
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
def _get_embedding_op_configs() -> list[BackendPatternConfig]:
|
| 439 |
+
dtype_configs = [
|
| 440 |
+
executorch_weight_only_quint8_dtype_config,
|
| 441 |
+
]
|
| 442 |
+
embedding_op_configs = []
|
| 443 |
+
for embedding_op, qat_embedding_op, ref_embedding_op in [
|
| 444 |
+
(nn.Embedding, nnqat.Embedding, nnqr.Embedding),
|
| 445 |
+
(nn.EmbeddingBag, nnqat.EmbeddingBag, nnqr.EmbeddingBag),
|
| 446 |
+
]:
|
| 447 |
+
embedding_op_configs.append(
|
| 448 |
+
BackendPatternConfig(embedding_op)
|
| 449 |
+
.set_observation_type(
|
| 450 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 451 |
+
) # noqa: E131
|
| 452 |
+
.set_dtype_configs(dtype_configs)
|
| 453 |
+
.set_qat_module(qat_embedding_op)
|
| 454 |
+
.set_root_module(embedding_op)
|
| 455 |
+
.set_reference_quantized_module(ref_embedding_op)
|
| 456 |
+
)
|
| 457 |
+
# config for qat op
|
| 458 |
+
embedding_op_configs.append(
|
| 459 |
+
BackendPatternConfig(qat_embedding_op)
|
| 460 |
+
.set_observation_type(
|
| 461 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 462 |
+
) # noqa: E131
|
| 463 |
+
.set_dtype_configs(dtype_configs)
|
| 464 |
+
.set_root_module(embedding_op)
|
| 465 |
+
.set_reference_quantized_module(ref_embedding_op)
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
+
# config for functional embedding
|
| 469 |
+
embedding_op_configs.append(
|
| 470 |
+
BackendPatternConfig(torch.nn.functional.embedding)
|
| 471 |
+
.set_observation_type(
|
| 472 |
+
ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 473 |
+
) # noqa: E131
|
| 474 |
+
.set_dtype_configs(dtype_configs)
|
| 475 |
+
._set_input_type_to_index({"weight": 1})
|
| 476 |
+
)
|
| 477 |
+
return embedding_op_configs
|
| 478 |
+
|
| 479 |
+
|
| 480 |
+
# =====================
|
| 481 |
+
# | BACKEND CONFIGS |
|
| 482 |
+
# =====================
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
def get_executorch_backend_config() -> BackendConfig:
|
| 486 |
+
"""
|
| 487 |
+
Return the `BackendConfig` for backends PyTorch lowers to through the Executorch stack.
|
| 488 |
+
"""
|
| 489 |
+
return (
|
| 490 |
+
BackendConfig("executorch")
|
| 491 |
+
.set_backend_pattern_configs(_get_linear_configs())
|
| 492 |
+
.set_backend_pattern_configs(_get_conv_configs())
|
| 493 |
+
.set_backend_pattern_configs(_get_binary_ops_configs())
|
| 494 |
+
.set_backend_pattern_configs(_get_share_qparams_ops_configs())
|
| 495 |
+
.set_backend_pattern_configs(_get_bn_configs())
|
| 496 |
+
.set_backend_pattern_configs(_get_cat_configs())
|
| 497 |
+
.set_backend_pattern_configs(_get_embedding_op_configs())
|
| 498 |
+
)
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/fbgemm.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from ._common_operator_config_utils import (
|
| 4 |
+
_get_binary_op_configs,
|
| 5 |
+
_get_bn_configs,
|
| 6 |
+
_get_cat_config,
|
| 7 |
+
_get_conv_configs,
|
| 8 |
+
_get_default_op_configs,
|
| 9 |
+
_get_embedding_op_configs,
|
| 10 |
+
_get_fixed_qparams_op_configs,
|
| 11 |
+
_get_linear_configs,
|
| 12 |
+
_get_rnn_op_configs,
|
| 13 |
+
_get_share_qparams_op_configs,
|
| 14 |
+
_get_tensor_info_op_configs,
|
| 15 |
+
)
|
| 16 |
+
from .backend_config import BackendConfig, DTypeConfig
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
__all__ = [
|
| 20 |
+
"get_fbgemm_backend_config",
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
# ===================
|
| 24 |
+
# | DTYPE CONFIGS |
|
| 25 |
+
# ===================
|
| 26 |
+
|
| 27 |
+
# TODO: For now, these DTypeConfigs are identical to the ones defined in native.py
|
| 28 |
+
# In the future, once we support specifying quant_min/quant_max and scale_min/scale_max,
|
| 29 |
+
# these will diverge. In particular, for FBGEMM, we will restrict the activation quantized
|
| 30 |
+
# values to within [0, 127].
|
| 31 |
+
|
| 32 |
+
fbgemm_weighted_op_quint8_dtype_config = DTypeConfig(
|
| 33 |
+
input_dtype=torch.quint8,
|
| 34 |
+
output_dtype=torch.quint8,
|
| 35 |
+
weight_dtype=torch.qint8,
|
| 36 |
+
bias_dtype=torch.float,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
fbgemm_default_op_quint8_dtype_config = DTypeConfig(
|
| 40 |
+
input_dtype=torch.quint8,
|
| 41 |
+
output_dtype=torch.quint8,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
fbgemm_default_op_fp16_dtype_config = DTypeConfig(
|
| 45 |
+
input_dtype=torch.float16,
|
| 46 |
+
output_dtype=torch.float16,
|
| 47 |
+
weight_dtype=torch.float16,
|
| 48 |
+
bias_dtype=torch.float16,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
fbgemm_default_dynamic_int8_dtype_config = DTypeConfig(
|
| 52 |
+
input_dtype=torch.quint8,
|
| 53 |
+
output_dtype=torch.float,
|
| 54 |
+
weight_dtype=torch.qint8,
|
| 55 |
+
bias_dtype=torch.float,
|
| 56 |
+
is_dynamic=True,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
fbgemm_default_dynamic_float16_dtype_config = DTypeConfig(
|
| 60 |
+
input_dtype=torch.float16,
|
| 61 |
+
output_dtype=torch.float,
|
| 62 |
+
weight_dtype=torch.float16,
|
| 63 |
+
bias_dtype=torch.float,
|
| 64 |
+
is_dynamic=True,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
fbgemm_weight_only_quint8_dtype_config = DTypeConfig(
|
| 68 |
+
input_dtype=torch.float,
|
| 69 |
+
output_dtype=torch.float,
|
| 70 |
+
weight_dtype=torch.quint8,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
fbgemm_weight_only_quint4x2_dtype_config = DTypeConfig(
|
| 74 |
+
input_dtype=torch.float,
|
| 75 |
+
output_dtype=torch.float,
|
| 76 |
+
weight_dtype=torch.quint4x2,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# =====================
|
| 81 |
+
# | BACKEND CONFIGS |
|
| 82 |
+
# =====================
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def get_fbgemm_backend_config() -> BackendConfig:
|
| 86 |
+
"""
|
| 87 |
+
Return the `BackendConfig` for PyTorch's native FBGEMM backend.
|
| 88 |
+
"""
|
| 89 |
+
conv_dtype_configs = [fbgemm_weighted_op_quint8_dtype_config]
|
| 90 |
+
linear_dtype_configs = [
|
| 91 |
+
fbgemm_weighted_op_quint8_dtype_config,
|
| 92 |
+
fbgemm_default_dynamic_int8_dtype_config,
|
| 93 |
+
fbgemm_default_dynamic_float16_dtype_config,
|
| 94 |
+
]
|
| 95 |
+
binary_op_dtype_configs = [fbgemm_default_op_quint8_dtype_config]
|
| 96 |
+
default_op_dtype_configs = [fbgemm_default_op_quint8_dtype_config]
|
| 97 |
+
fixed_qparams_op_dtype_configs = [fbgemm_default_op_quint8_dtype_config]
|
| 98 |
+
share_qparams_op_dtype_configs = [fbgemm_default_op_quint8_dtype_config]
|
| 99 |
+
tensor_info_op_dtype_configs = [fbgemm_default_op_quint8_dtype_config]
|
| 100 |
+
rnn_op_dtype_configs = [
|
| 101 |
+
fbgemm_default_dynamic_int8_dtype_config,
|
| 102 |
+
fbgemm_default_dynamic_float16_dtype_config,
|
| 103 |
+
]
|
| 104 |
+
embedding_op_dtype_configs = [
|
| 105 |
+
fbgemm_weight_only_quint8_dtype_config,
|
| 106 |
+
fbgemm_weight_only_quint4x2_dtype_config,
|
| 107 |
+
]
|
| 108 |
+
return (
|
| 109 |
+
BackendConfig("fbgemm")
|
| 110 |
+
.set_backend_pattern_configs(_get_conv_configs(conv_dtype_configs))
|
| 111 |
+
.set_backend_pattern_configs(_get_linear_configs(linear_dtype_configs))
|
| 112 |
+
.set_backend_pattern_configs(_get_binary_op_configs(binary_op_dtype_configs))
|
| 113 |
+
.set_backend_pattern_config(_get_cat_config(default_op_dtype_configs))
|
| 114 |
+
.set_backend_pattern_configs(_get_default_op_configs(default_op_dtype_configs))
|
| 115 |
+
.set_backend_pattern_configs(
|
| 116 |
+
_get_fixed_qparams_op_configs(fixed_qparams_op_dtype_configs)
|
| 117 |
+
)
|
| 118 |
+
.set_backend_pattern_configs(
|
| 119 |
+
_get_share_qparams_op_configs(share_qparams_op_dtype_configs)
|
| 120 |
+
)
|
| 121 |
+
.set_backend_pattern_configs(
|
| 122 |
+
_get_tensor_info_op_configs(tensor_info_op_dtype_configs)
|
| 123 |
+
)
|
| 124 |
+
.set_backend_pattern_configs(_get_bn_configs(default_op_dtype_configs))
|
| 125 |
+
.set_backend_pattern_configs(_get_rnn_op_configs(rnn_op_dtype_configs))
|
| 126 |
+
.set_backend_pattern_configs(
|
| 127 |
+
_get_embedding_op_configs(embedding_op_dtype_configs)
|
| 128 |
+
)
|
| 129 |
+
)
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/native.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: allow-untyped-defs
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from ._common_operator_config_utils import (
|
| 5 |
+
_get_binary_op_configs,
|
| 6 |
+
_get_bn_configs,
|
| 7 |
+
_get_cat_config,
|
| 8 |
+
_get_conv_configs,
|
| 9 |
+
_get_default_op_configs,
|
| 10 |
+
_get_embedding_op_configs,
|
| 11 |
+
_get_fixed_qparams_op_configs,
|
| 12 |
+
_get_linear_configs,
|
| 13 |
+
_get_ln_configs,
|
| 14 |
+
_get_rnn_op_configs,
|
| 15 |
+
_get_share_qparams_op_configs,
|
| 16 |
+
_get_tensor_info_op_configs,
|
| 17 |
+
)
|
| 18 |
+
from .backend_config import BackendConfig, DTypeConfig
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
__all__ = [
|
| 22 |
+
"get_test_only_legacy_native_backend_config",
|
| 23 |
+
"default_op_quint8_dtype_config",
|
| 24 |
+
"default_op_fp16_dtype_config",
|
| 25 |
+
"default_dynamic_int8_dtype_config",
|
| 26 |
+
"default_dynamic_float16_dtype_config",
|
| 27 |
+
"input_output_only_quint8_dtype_config",
|
| 28 |
+
"weight_only_quint8_dtype_config",
|
| 29 |
+
"weight_only_quint4x2_dtype_config",
|
| 30 |
+
"get_native_backend_config",
|
| 31 |
+
"get_native_backend_config_dict",
|
| 32 |
+
"get_test_only_legacy_native_backend_config_dict",
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
# ===================
|
| 36 |
+
# | DTYPE CONFIGS |
|
| 37 |
+
# ===================
|
| 38 |
+
|
| 39 |
+
# weighted op int8 dtype config
|
| 40 |
+
# this is config for ops that has quantized weights, like linear, conv
|
| 41 |
+
weighted_op_quint8_dtype_config = DTypeConfig(
|
| 42 |
+
input_dtype=torch.quint8,
|
| 43 |
+
output_dtype=torch.quint8,
|
| 44 |
+
weight_dtype=torch.qint8,
|
| 45 |
+
bias_dtype=torch.float,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
default_op_quint8_dtype_config = DTypeConfig(
|
| 49 |
+
input_dtype=torch.quint8,
|
| 50 |
+
output_dtype=torch.quint8,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
default_op_fp16_dtype_config = DTypeConfig(
|
| 54 |
+
input_dtype=torch.float16,
|
| 55 |
+
output_dtype=torch.float16,
|
| 56 |
+
weight_dtype=torch.float16,
|
| 57 |
+
bias_dtype=torch.float16,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
default_dynamic_int8_dtype_config = DTypeConfig(
|
| 61 |
+
input_dtype=torch.quint8,
|
| 62 |
+
output_dtype=torch.float,
|
| 63 |
+
weight_dtype=torch.qint8,
|
| 64 |
+
bias_dtype=torch.float,
|
| 65 |
+
# currently the dtype check is not yet enabled, so we provided the dtype_configs but
|
| 66 |
+
# it is not really used yet,
|
| 67 |
+
# we will enable it a bit later after we moved everything to backend_config_dict
|
| 68 |
+
is_dynamic=True,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
default_dynamic_float16_dtype_config = DTypeConfig(
|
| 72 |
+
input_dtype=torch.float16,
|
| 73 |
+
output_dtype=torch.float,
|
| 74 |
+
weight_dtype=torch.float16,
|
| 75 |
+
bias_dtype=torch.float,
|
| 76 |
+
# currently the dtype check is not yet enabled, so we provided the dtype_configs but
|
| 77 |
+
# it is not really used yet,
|
| 78 |
+
# we will enable it a bit later after we moved everything to backend_config_dict
|
| 79 |
+
is_dynamic=True,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Needed for LayerNorm and f.layer_norm, since currently the kernel only supports float weights
|
| 83 |
+
input_output_only_quint8_dtype_config = DTypeConfig(
|
| 84 |
+
input_dtype=torch.quint8,
|
| 85 |
+
output_dtype=torch.quint8,
|
| 86 |
+
weight_dtype=torch.float,
|
| 87 |
+
bias_dtype=torch.float,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
weight_only_quint8_dtype_config = DTypeConfig(
|
| 91 |
+
input_dtype=torch.float,
|
| 92 |
+
output_dtype=torch.float,
|
| 93 |
+
weight_dtype=torch.quint8,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
weight_only_quint4x2_dtype_config = DTypeConfig(
|
| 97 |
+
input_dtype=torch.float,
|
| 98 |
+
output_dtype=torch.float,
|
| 99 |
+
weight_dtype=torch.quint4x2,
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# =====================
|
| 104 |
+
# | BACKEND CONFIGS |
|
| 105 |
+
# =====================
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def get_test_only_legacy_native_backend_config() -> BackendConfig:
|
| 109 |
+
"""
|
| 110 |
+
Return the `BackendConfig` for PyTorch Native backend (fbgemm/qnnpack) with various additional fp16 ops.
|
| 111 |
+
"""
|
| 112 |
+
conv_dtype_configs = [weighted_op_quint8_dtype_config]
|
| 113 |
+
linear_dtype_configs = [
|
| 114 |
+
weighted_op_quint8_dtype_config,
|
| 115 |
+
default_dynamic_int8_dtype_config,
|
| 116 |
+
default_dynamic_float16_dtype_config,
|
| 117 |
+
default_op_fp16_dtype_config,
|
| 118 |
+
]
|
| 119 |
+
binary_op_dtype_configs = [
|
| 120 |
+
default_op_quint8_dtype_config,
|
| 121 |
+
default_op_fp16_dtype_config,
|
| 122 |
+
]
|
| 123 |
+
default_op_dtype_configs = [default_op_quint8_dtype_config]
|
| 124 |
+
fixed_qparams_op_dtype_configs = [
|
| 125 |
+
default_op_quint8_dtype_config,
|
| 126 |
+
default_op_fp16_dtype_config,
|
| 127 |
+
]
|
| 128 |
+
share_qparams_op_dtype_configs = [
|
| 129 |
+
default_op_quint8_dtype_config,
|
| 130 |
+
default_op_fp16_dtype_config,
|
| 131 |
+
]
|
| 132 |
+
tensor_info_op_dtype_configs = [
|
| 133 |
+
default_op_quint8_dtype_config,
|
| 134 |
+
]
|
| 135 |
+
rnn_op_dtype_configs = [
|
| 136 |
+
default_dynamic_int8_dtype_config,
|
| 137 |
+
default_dynamic_float16_dtype_config,
|
| 138 |
+
]
|
| 139 |
+
embedding_op_dtype_configs = [
|
| 140 |
+
weight_only_quint8_dtype_config,
|
| 141 |
+
weight_only_quint4x2_dtype_config,
|
| 142 |
+
]
|
| 143 |
+
layer_norm_op_dtype_configs = [input_output_only_quint8_dtype_config]
|
| 144 |
+
return (
|
| 145 |
+
BackendConfig("_native_and_fp16")
|
| 146 |
+
.set_backend_pattern_configs(_get_conv_configs(conv_dtype_configs))
|
| 147 |
+
.set_backend_pattern_configs(_get_linear_configs(linear_dtype_configs))
|
| 148 |
+
.set_backend_pattern_configs(_get_binary_op_configs(binary_op_dtype_configs))
|
| 149 |
+
.set_backend_pattern_config(_get_cat_config(default_op_dtype_configs))
|
| 150 |
+
.set_backend_pattern_configs(_get_default_op_configs(default_op_dtype_configs))
|
| 151 |
+
.set_backend_pattern_configs(
|
| 152 |
+
_get_fixed_qparams_op_configs(fixed_qparams_op_dtype_configs)
|
| 153 |
+
)
|
| 154 |
+
.set_backend_pattern_configs(
|
| 155 |
+
_get_share_qparams_op_configs(share_qparams_op_dtype_configs)
|
| 156 |
+
)
|
| 157 |
+
.set_backend_pattern_configs(
|
| 158 |
+
_get_tensor_info_op_configs(tensor_info_op_dtype_configs)
|
| 159 |
+
)
|
| 160 |
+
.set_backend_pattern_configs(_get_bn_configs(default_op_dtype_configs))
|
| 161 |
+
.set_backend_pattern_configs(_get_ln_configs(layer_norm_op_dtype_configs))
|
| 162 |
+
.set_backend_pattern_configs(_get_rnn_op_configs(rnn_op_dtype_configs))
|
| 163 |
+
.set_backend_pattern_configs(
|
| 164 |
+
_get_embedding_op_configs(embedding_op_dtype_configs)
|
| 165 |
+
)
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def get_native_backend_config() -> BackendConfig:
|
| 170 |
+
"""
|
| 171 |
+
Return the `BackendConfig` for PyTorch Native backend (fbgemm/qnnpack).
|
| 172 |
+
"""
|
| 173 |
+
# TODO: express this BackendConfig as a union of the FBGEMM and QNNPACK BackendConfigs
|
| 174 |
+
conv_dtype_configs = [weighted_op_quint8_dtype_config]
|
| 175 |
+
linear_dtype_configs = [
|
| 176 |
+
weighted_op_quint8_dtype_config,
|
| 177 |
+
default_dynamic_int8_dtype_config,
|
| 178 |
+
default_dynamic_float16_dtype_config,
|
| 179 |
+
]
|
| 180 |
+
binary_op_dtype_configs = [default_op_quint8_dtype_config]
|
| 181 |
+
default_op_dtype_configs = [default_op_quint8_dtype_config]
|
| 182 |
+
fixed_qparams_op_dtype_configs = [default_op_quint8_dtype_config]
|
| 183 |
+
share_qparams_op_dtype_configs = [default_op_quint8_dtype_config]
|
| 184 |
+
tensor_info_op_dtype_configs = [default_op_quint8_dtype_config]
|
| 185 |
+
rnn_op_dtype_configs = [
|
| 186 |
+
default_dynamic_int8_dtype_config,
|
| 187 |
+
default_dynamic_float16_dtype_config,
|
| 188 |
+
]
|
| 189 |
+
embedding_op_dtype_configs = [
|
| 190 |
+
weight_only_quint8_dtype_config,
|
| 191 |
+
weight_only_quint4x2_dtype_config,
|
| 192 |
+
]
|
| 193 |
+
layer_norm_op_dtype_configs = [input_output_only_quint8_dtype_config]
|
| 194 |
+
return (
|
| 195 |
+
BackendConfig("native")
|
| 196 |
+
.set_backend_pattern_configs(_get_conv_configs(conv_dtype_configs))
|
| 197 |
+
.set_backend_pattern_configs(_get_linear_configs(linear_dtype_configs))
|
| 198 |
+
.set_backend_pattern_configs(_get_binary_op_configs(binary_op_dtype_configs))
|
| 199 |
+
.set_backend_pattern_config(_get_cat_config(default_op_dtype_configs))
|
| 200 |
+
.set_backend_pattern_configs(_get_default_op_configs(default_op_dtype_configs))
|
| 201 |
+
.set_backend_pattern_configs(
|
| 202 |
+
_get_fixed_qparams_op_configs(fixed_qparams_op_dtype_configs)
|
| 203 |
+
)
|
| 204 |
+
.set_backend_pattern_configs(
|
| 205 |
+
_get_share_qparams_op_configs(share_qparams_op_dtype_configs)
|
| 206 |
+
)
|
| 207 |
+
.set_backend_pattern_configs(
|
| 208 |
+
_get_tensor_info_op_configs(tensor_info_op_dtype_configs)
|
| 209 |
+
)
|
| 210 |
+
.set_backend_pattern_configs(_get_bn_configs(default_op_dtype_configs))
|
| 211 |
+
.set_backend_pattern_configs(_get_ln_configs(layer_norm_op_dtype_configs))
|
| 212 |
+
.set_backend_pattern_configs(_get_rnn_op_configs(rnn_op_dtype_configs))
|
| 213 |
+
.set_backend_pattern_configs(
|
| 214 |
+
_get_embedding_op_configs(embedding_op_dtype_configs)
|
| 215 |
+
)
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
def get_native_backend_config_dict():
|
| 220 |
+
"""
|
| 221 |
+
Return the `BackendConfig` for PyTorch Native backend (fbgemm/qnnpack) in dictionary form.
|
| 222 |
+
"""
|
| 223 |
+
return get_native_backend_config().to_dict()
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def get_test_only_legacy_native_backend_config_dict():
|
| 227 |
+
"""
|
| 228 |
+
Return the `BackendConfig` for PyTorch Native backend (fbgemm/qnnpack) with various additional
|
| 229 |
+
fp16 ops in dictionary form.
|
| 230 |
+
"""
|
| 231 |
+
return get_test_only_legacy_native_backend_config().to_dict()
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/observation_type.py
ADDED
|
File without changes
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/onednn.py
ADDED
|
@@ -0,0 +1,648 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: allow-untyped-defs
|
| 2 |
+
import itertools
|
| 3 |
+
import operator
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torch.ao.nn.intrinsic as nni
|
| 7 |
+
import torch.ao.nn.quantized.reference as nnqr
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
from torch.ao.quantization.fuser_method_mappings import _sequential_wrapper2
|
| 11 |
+
from torch.ao.quantization.utils import MatchAllNode
|
| 12 |
+
|
| 13 |
+
from ._common_operator_config_utils import (
|
| 14 |
+
_get_binary_op_configs,
|
| 15 |
+
_get_bn_configs,
|
| 16 |
+
_get_cat_config,
|
| 17 |
+
_get_conv_configs,
|
| 18 |
+
_get_default_op_configs,
|
| 19 |
+
_get_embedding_op_configs,
|
| 20 |
+
_get_fixed_qparams_op_configs,
|
| 21 |
+
_get_linear_configs,
|
| 22 |
+
_get_ln_configs,
|
| 23 |
+
_get_rnn_op_configs,
|
| 24 |
+
_get_share_qparams_op_configs,
|
| 25 |
+
)
|
| 26 |
+
from .backend_config import (
|
| 27 |
+
BackendConfig,
|
| 28 |
+
BackendPatternConfig,
|
| 29 |
+
DTypeConfig,
|
| 30 |
+
ObservationType,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# ===================
|
| 35 |
+
# | DTYPE CONFIGS |
|
| 36 |
+
# ===================
|
| 37 |
+
|
| 38 |
+
onednn_weighted_op_int8_dtype_config = DTypeConfig(
|
| 39 |
+
input_dtype=torch.quint8,
|
| 40 |
+
output_dtype=torch.quint8,
|
| 41 |
+
weight_dtype=torch.qint8,
|
| 42 |
+
bias_dtype=torch.float,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
onednn_op_quint8_dtype_config = DTypeConfig(
|
| 46 |
+
input_dtype=torch.quint8,
|
| 47 |
+
output_dtype=torch.quint8,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
onednn_dynamic_int8_dtype_config = DTypeConfig(
|
| 51 |
+
input_dtype=torch.quint8,
|
| 52 |
+
output_dtype=torch.float,
|
| 53 |
+
weight_dtype=torch.qint8,
|
| 54 |
+
bias_dtype=torch.float,
|
| 55 |
+
is_dynamic=True,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
onednn_weight_only_qint8_dtype_config = DTypeConfig(
|
| 59 |
+
input_dtype=torch.float,
|
| 60 |
+
output_dtype=torch.float,
|
| 61 |
+
weight_dtype=torch.qint8,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
onednn_input_output_only_quint8_dtype_config = DTypeConfig(
|
| 65 |
+
input_dtype=torch.quint8,
|
| 66 |
+
output_dtype=torch.quint8,
|
| 67 |
+
weight_dtype=torch.float,
|
| 68 |
+
bias_dtype=torch.float,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# ===================
|
| 72 |
+
# | FUSER METHODS |
|
| 73 |
+
# ===================
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def _fuse_linear_bn_leaky_relu(is_qat, linear, bn, leaky_relu):
|
| 77 |
+
r"""Given the linear, bn and leaky_relu modules, fuses them and returns the fused module
|
| 78 |
+
Args:
|
| 79 |
+
is_qat: a flag for whether we are using quantization aware training fusion
|
| 80 |
+
or post training quantization fusion
|
| 81 |
+
linear: Module instance of type Linear
|
| 82 |
+
bn: BatchNorm1d instance that needs to be fused with the linear layer
|
| 83 |
+
leaky_relu: LeakyReLU instance that needs to be fused with the linear layer
|
| 84 |
+
Examples::
|
| 85 |
+
>>> # xdoctest: +SKIP(failing)
|
| 86 |
+
>>> m1 = nn.Linear(20, 10)
|
| 87 |
+
>>> b1 = nn.BatchNorm1d(10)
|
| 88 |
+
>>> lr = nn.LeakyReLU(0.01)
|
| 89 |
+
>>> m2 = _fuse_linear_bn_leaky_relu(m1, b1, lr)
|
| 90 |
+
"""
|
| 91 |
+
assert (
|
| 92 |
+
linear.training == bn.training and bn.training == leaky_relu.training
|
| 93 |
+
), "Linear, BN and LeakyReLU all must be in the same mode (train or eval)."
|
| 94 |
+
|
| 95 |
+
if is_qat:
|
| 96 |
+
raise NotImplementedError(
|
| 97 |
+
f"Cannot fuse train modules: {(linear, bn, leaky_relu)}"
|
| 98 |
+
)
|
| 99 |
+
else:
|
| 100 |
+
map_to_fused_module_eval = {
|
| 101 |
+
nn.Linear: nni.LinearLeakyReLU,
|
| 102 |
+
}
|
| 103 |
+
fused_module = map_to_fused_module_eval.get(type(linear), None)
|
| 104 |
+
if fused_module is not None:
|
| 105 |
+
fused_linear = nn.utils.fusion.fuse_linear_bn_eval(linear, bn)
|
| 106 |
+
fm = fused_module(fused_linear, leaky_relu)
|
| 107 |
+
return fm
|
| 108 |
+
else:
|
| 109 |
+
raise NotImplementedError(
|
| 110 |
+
f"Cannot fuse eval modules: {(linear, bn, leaky_relu)}"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# ======================
|
| 115 |
+
# | CONFIGS FOR CONV |
|
| 116 |
+
# ======================
|
| 117 |
+
observation_type = ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT
|
| 118 |
+
|
| 119 |
+
conv_dtype_configs = [onednn_weighted_op_int8_dtype_config]
|
| 120 |
+
conv_configs = _get_conv_configs(conv_dtype_configs)
|
| 121 |
+
|
| 122 |
+
# (1) Conv2d + Add
|
| 123 |
+
|
| 124 |
+
# conv2d Y
|
| 125 |
+
# \ /
|
| 126 |
+
# add
|
| 127 |
+
|
| 128 |
+
# include:
|
| 129 |
+
# conv2d conv2d
|
| 130 |
+
# \ /
|
| 131 |
+
# add
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def _fuse_conv_add_left(is_qat, add, conv, _):
|
| 135 |
+
return nni.ConvAdd2d(conv, add)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def _conv_add_root_node_getter_left(pattern):
|
| 139 |
+
_, conv, _ = pattern
|
| 140 |
+
return conv
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def _conv_add_extra_inputs_getter_left(pattern):
|
| 144 |
+
"""get inputs pattern for extra inputs, inputs for root node
|
| 145 |
+
are assumed to be copied over from root node to the fused node
|
| 146 |
+
"""
|
| 147 |
+
_, _conv, extra_input = pattern
|
| 148 |
+
return [extra_input]
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
# conv2d
|
| 152 |
+
# \
|
| 153 |
+
# bn Y
|
| 154 |
+
# \ /
|
| 155 |
+
# add
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def _fuse_conv_bn_add_left(is_qat, add, bn_conv, _):
|
| 159 |
+
bn, conv = bn_conv
|
| 160 |
+
if is_qat:
|
| 161 |
+
raise NotImplementedError(f"Cannot fuse train modules: {(conv, bn, add)}")
|
| 162 |
+
else:
|
| 163 |
+
fused_conv = nn.utils.fusion.fuse_conv_bn_eval(conv, bn)
|
| 164 |
+
return nni.ConvAdd2d(fused_conv, add)
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def _conv_bn_add_root_node_getter_left(add_pattern):
|
| 168 |
+
_, bn_conv, _ = add_pattern
|
| 169 |
+
_bn, conv = bn_conv
|
| 170 |
+
return conv
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def _conv_bn_add_extra_inputs_getter_left(add_pattern):
|
| 174 |
+
"""get inputs pattern for extra inputs, inputs for root node
|
| 175 |
+
are assumed to be copied over from root node to the fused node
|
| 176 |
+
"""
|
| 177 |
+
_, _bn_conv, extra_input = add_pattern
|
| 178 |
+
return [extra_input]
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
conv_add_left_optioins = itertools.product(
|
| 182 |
+
[True, False], # with_bn
|
| 183 |
+
[torch.add, operator.add], # add_op
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
for with_bn, add_op in conv_add_left_optioins:
|
| 187 |
+
if with_bn:
|
| 188 |
+
conv_configs.append(
|
| 189 |
+
BackendPatternConfig()
|
| 190 |
+
._set_pattern_complex_format(
|
| 191 |
+
(add_op, (nn.BatchNorm2d, nn.Conv2d), MatchAllNode)
|
| 192 |
+
) # noqa: E131
|
| 193 |
+
.set_observation_type(observation_type)
|
| 194 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 195 |
+
.set_fuser_method(_fuse_conv_bn_add_left)
|
| 196 |
+
._set_root_node_getter(_conv_bn_add_root_node_getter_left)
|
| 197 |
+
._set_extra_inputs_getter(_conv_bn_add_extra_inputs_getter_left)
|
| 198 |
+
.set_fused_module(nni.ConvAdd2d)
|
| 199 |
+
)
|
| 200 |
+
else:
|
| 201 |
+
conv_configs.append(
|
| 202 |
+
BackendPatternConfig()
|
| 203 |
+
._set_pattern_complex_format(
|
| 204 |
+
(add_op, nn.Conv2d, MatchAllNode)
|
| 205 |
+
) # noqa: E131
|
| 206 |
+
.set_observation_type(observation_type)
|
| 207 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 208 |
+
.set_fuser_method(_fuse_conv_add_left)
|
| 209 |
+
._set_root_node_getter(_conv_add_root_node_getter_left)
|
| 210 |
+
._set_extra_inputs_getter(_conv_add_extra_inputs_getter_left)
|
| 211 |
+
.set_fused_module(nni.ConvAdd2d)
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
# Y conv2d
|
| 215 |
+
# \ /
|
| 216 |
+
# add
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
def _fuse_conv_add_right(is_qat, add, _, conv):
|
| 220 |
+
return nni.ConvAdd2d(conv, add)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def _conv_add_root_node_getter_right(pattern):
|
| 224 |
+
_add, _, conv = pattern
|
| 225 |
+
return conv
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def _conv_add_extra_inputs_getter_right(pattern):
|
| 229 |
+
"""get inputs pattern for extra inputs, inputs for root node
|
| 230 |
+
are assumed to be copied over from root node to the fused node
|
| 231 |
+
"""
|
| 232 |
+
_, extra_input, _conv = pattern
|
| 233 |
+
return [extra_input]
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
# conv2d
|
| 237 |
+
# /
|
| 238 |
+
# Y bn
|
| 239 |
+
# \ /
|
| 240 |
+
# add
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
def _fuse_conv_bn_add_right(is_qat, add, _, bn_conv):
|
| 244 |
+
bn, conv = bn_conv
|
| 245 |
+
if is_qat:
|
| 246 |
+
raise NotImplementedError(f"Cannot fuse train modules: {(conv, bn, add)}")
|
| 247 |
+
else:
|
| 248 |
+
fused_conv = nn.utils.fusion.fuse_conv_bn_eval(conv, bn)
|
| 249 |
+
return nni.ConvAdd2d(fused_conv, add)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def _conv_bn_add_root_node_getter_right(pattern):
|
| 253 |
+
_add, _, bn_conv = pattern
|
| 254 |
+
_bn, conv = bn_conv
|
| 255 |
+
return conv
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
def _conv_bn_add_extra_inputs_getter_right(pattern):
|
| 259 |
+
"""get inputs pattern for extra inputs, inputs for root node
|
| 260 |
+
are assumed to be copied over from root node to the fused node
|
| 261 |
+
"""
|
| 262 |
+
_, extra_input, _bn_conv = pattern
|
| 263 |
+
return [extra_input]
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
conv_add_optioins = itertools.product(
|
| 267 |
+
[True, False], # with_bn
|
| 268 |
+
[torch.add, operator.add], # add_op
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
for with_bn, add_op in conv_add_optioins:
|
| 272 |
+
if with_bn:
|
| 273 |
+
conv_configs.append(
|
| 274 |
+
BackendPatternConfig()
|
| 275 |
+
._set_pattern_complex_format(
|
| 276 |
+
(add_op, MatchAllNode, (nn.BatchNorm2d, nn.Conv2d))
|
| 277 |
+
) # noqa: E131
|
| 278 |
+
.set_observation_type(observation_type)
|
| 279 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 280 |
+
.set_fuser_method(_fuse_conv_bn_add_right)
|
| 281 |
+
._set_root_node_getter(_conv_bn_add_root_node_getter_right)
|
| 282 |
+
._set_extra_inputs_getter(_conv_bn_add_extra_inputs_getter_right)
|
| 283 |
+
.set_fused_module(nni.ConvAdd2d)
|
| 284 |
+
)
|
| 285 |
+
else:
|
| 286 |
+
conv_configs.append(
|
| 287 |
+
BackendPatternConfig()
|
| 288 |
+
._set_pattern_complex_format(
|
| 289 |
+
(add_op, MatchAllNode, nn.Conv2d)
|
| 290 |
+
) # noqa: E131
|
| 291 |
+
.set_observation_type(observation_type)
|
| 292 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 293 |
+
.set_fuser_method(_fuse_conv_add_right)
|
| 294 |
+
._set_root_node_getter(_conv_add_root_node_getter_right)
|
| 295 |
+
._set_extra_inputs_getter(_conv_add_extra_inputs_getter_right)
|
| 296 |
+
.set_fused_module(nni.ConvAdd2d)
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
conv_configs.append(
|
| 300 |
+
BackendPatternConfig(nni.ConvAdd2d)
|
| 301 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 302 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 303 |
+
.set_root_module(nn.Conv2d)
|
| 304 |
+
.set_reference_quantized_module(nnqr.Conv2d)
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
# (2) Conv2d + Add + Relu
|
| 308 |
+
|
| 309 |
+
# conv2d Y
|
| 310 |
+
# \ /
|
| 311 |
+
# add
|
| 312 |
+
# \
|
| 313 |
+
# relu
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
def _fuse_conv_add_relu_left(is_qat, relu, add_pattern):
|
| 317 |
+
add, conv, _ = add_pattern
|
| 318 |
+
return nni.ConvAddReLU2d(conv, add, relu)
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
def _conv_add_relu_root_node_getter_left(pattern):
|
| 322 |
+
_relu, add_pattern = pattern
|
| 323 |
+
_, conv, _ = add_pattern
|
| 324 |
+
return conv
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def _conv_add_relu_extra_inputs_getter_left(pattern):
|
| 328 |
+
"""get inputs pattern for extra inputs, inputs for root node
|
| 329 |
+
are assumed to be copied over from root node to the fused node
|
| 330 |
+
"""
|
| 331 |
+
_relu, add_pattern = pattern
|
| 332 |
+
_, _conv, extra_input = add_pattern
|
| 333 |
+
return [extra_input]
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
# conv2d
|
| 337 |
+
# \
|
| 338 |
+
# bn Y
|
| 339 |
+
# \ /
|
| 340 |
+
# add
|
| 341 |
+
# \
|
| 342 |
+
# relu
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
def _fuse_conv_bn_add_relu_left(is_qat, relu, add_pattern):
|
| 346 |
+
add, bn_conv, _ = add_pattern
|
| 347 |
+
bn, conv = bn_conv
|
| 348 |
+
if is_qat:
|
| 349 |
+
raise NotImplementedError(f"Cannot fuse train modules: {(conv, bn, add, relu)}")
|
| 350 |
+
else:
|
| 351 |
+
fused_conv = nn.utils.fusion.fuse_conv_bn_eval(conv, bn)
|
| 352 |
+
return nni.ConvAddReLU2d(fused_conv, add, relu)
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
def _conv_bn_add_relu_root_node_getter_left(pattern):
|
| 356 |
+
_relu, add_pattern = pattern
|
| 357 |
+
_, bn_conv, _ = add_pattern
|
| 358 |
+
_bn, conv = bn_conv
|
| 359 |
+
return conv
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
def _conv_bn_add_relu_extra_inputs_getter_left(pattern):
|
| 363 |
+
"""get inputs pattern for extra inputs, inputs for root node
|
| 364 |
+
are assumed to be copied over from root node to the fused node
|
| 365 |
+
"""
|
| 366 |
+
_relu, add_pattern = pattern
|
| 367 |
+
_, _bn_conv, extra_input = add_pattern
|
| 368 |
+
return [extra_input]
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
conv_add_relu_left_optioins = itertools.product(
|
| 372 |
+
[True, False], # with_bn
|
| 373 |
+
[torch.add, operator.add], # add_op
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
for with_bn, add_op in conv_add_relu_left_optioins:
|
| 377 |
+
if with_bn:
|
| 378 |
+
conv_configs.append(
|
| 379 |
+
BackendPatternConfig()
|
| 380 |
+
._set_pattern_complex_format(
|
| 381 |
+
(nn.ReLU, (add_op, (nn.BatchNorm2d, nn.Conv2d), MatchAllNode))
|
| 382 |
+
) # noqa: E131
|
| 383 |
+
.set_observation_type(observation_type)
|
| 384 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 385 |
+
.set_fuser_method(_fuse_conv_bn_add_relu_left)
|
| 386 |
+
._set_root_node_getter(_conv_bn_add_relu_root_node_getter_left)
|
| 387 |
+
._set_extra_inputs_getter(_conv_bn_add_relu_extra_inputs_getter_left)
|
| 388 |
+
.set_fused_module(nni.ConvAddReLU2d)
|
| 389 |
+
)
|
| 390 |
+
else:
|
| 391 |
+
conv_configs.append(
|
| 392 |
+
BackendPatternConfig()
|
| 393 |
+
._set_pattern_complex_format(
|
| 394 |
+
(nn.ReLU, (add_op, nn.Conv2d, MatchAllNode))
|
| 395 |
+
) # noqa: E131
|
| 396 |
+
.set_observation_type(observation_type)
|
| 397 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 398 |
+
.set_fuser_method(_fuse_conv_add_relu_left)
|
| 399 |
+
._set_root_node_getter(_conv_add_relu_root_node_getter_left)
|
| 400 |
+
._set_extra_inputs_getter(_conv_add_relu_extra_inputs_getter_left)
|
| 401 |
+
.set_fused_module(nni.ConvAddReLU2d)
|
| 402 |
+
)
|
| 403 |
+
|
| 404 |
+
# Y conv2d
|
| 405 |
+
# \ /
|
| 406 |
+
# add
|
| 407 |
+
# \
|
| 408 |
+
# relu
|
| 409 |
+
|
| 410 |
+
|
| 411 |
+
def _fuse_conv_add_relu_right(is_qat, relu, add_pattern):
|
| 412 |
+
add, _, conv = add_pattern
|
| 413 |
+
return nni.ConvAddReLU2d(conv, add, relu)
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
def _conv_add_relu_root_node_getter_right(pattern):
|
| 417 |
+
_relu, add_pattern = pattern
|
| 418 |
+
_, _extra_input, conv = add_pattern
|
| 419 |
+
return conv
|
| 420 |
+
|
| 421 |
+
|
| 422 |
+
def _conv_add_relu_extra_inputs_getter_right(pattern):
|
| 423 |
+
"""get inputs pattern for extra inputs, inputs for root node
|
| 424 |
+
are assumed to be copied over from root node to the fused node
|
| 425 |
+
"""
|
| 426 |
+
_relu, add_pattern = pattern
|
| 427 |
+
_, extra_input, _conv = add_pattern
|
| 428 |
+
return [extra_input]
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
# conv2d
|
| 432 |
+
# /
|
| 433 |
+
# Y bn
|
| 434 |
+
# \ /
|
| 435 |
+
# add
|
| 436 |
+
# \
|
| 437 |
+
# relu
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
def _fuse_conv_bn_add_relu_right(is_qat, relu, add_pattern):
|
| 441 |
+
add, _, bn_conv = add_pattern
|
| 442 |
+
bn, conv = bn_conv
|
| 443 |
+
if is_qat:
|
| 444 |
+
raise NotImplementedError(f"Cannot fuse train modules: {(conv, bn, add, relu)}")
|
| 445 |
+
else:
|
| 446 |
+
fused_conv = nn.utils.fusion.fuse_conv_bn_eval(conv, bn)
|
| 447 |
+
return nni.ConvAddReLU2d(fused_conv, add, relu)
|
| 448 |
+
|
| 449 |
+
|
| 450 |
+
def _conv_bn_add_relu_root_node_getter_right(pattern):
|
| 451 |
+
_relu, add_pattern = pattern
|
| 452 |
+
_, _, bn_conv = add_pattern
|
| 453 |
+
_bn, conv = bn_conv
|
| 454 |
+
return conv
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
def _conv_bn_add_relu_extra_inputs_getter_right(pattern):
|
| 458 |
+
"""get inputs pattern for extra inputs, inputs for root node
|
| 459 |
+
are assumed to be copied over from root node to the fused node
|
| 460 |
+
"""
|
| 461 |
+
_relu, add_pattern = pattern
|
| 462 |
+
_, extra_input, _bn_conv = add_pattern
|
| 463 |
+
return [extra_input]
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
conv_add_relu_left_optioins = itertools.product(
|
| 467 |
+
[True, False], # with_bn
|
| 468 |
+
[torch.add, operator.add], # add_op
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
for with_bn, add_op in conv_add_relu_left_optioins:
|
| 472 |
+
if with_bn:
|
| 473 |
+
conv_configs.append(
|
| 474 |
+
BackendPatternConfig()
|
| 475 |
+
._set_pattern_complex_format(
|
| 476 |
+
(nn.ReLU, (add_op, MatchAllNode, (nn.BatchNorm2d, nn.Conv2d)))
|
| 477 |
+
) # noqa: E131
|
| 478 |
+
.set_observation_type(observation_type)
|
| 479 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 480 |
+
.set_fuser_method(_fuse_conv_bn_add_relu_right)
|
| 481 |
+
._set_root_node_getter(_conv_bn_add_relu_root_node_getter_right)
|
| 482 |
+
._set_extra_inputs_getter(_conv_bn_add_relu_extra_inputs_getter_right)
|
| 483 |
+
.set_fused_module(nni.ConvAddReLU2d)
|
| 484 |
+
)
|
| 485 |
+
else:
|
| 486 |
+
conv_configs.append(
|
| 487 |
+
BackendPatternConfig()
|
| 488 |
+
._set_pattern_complex_format(
|
| 489 |
+
(nn.ReLU, (add_op, MatchAllNode, nn.Conv2d))
|
| 490 |
+
) # noqa: E131
|
| 491 |
+
.set_observation_type(observation_type)
|
| 492 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 493 |
+
.set_fuser_method(_fuse_conv_add_relu_right)
|
| 494 |
+
._set_root_node_getter(_conv_add_relu_root_node_getter_right)
|
| 495 |
+
._set_extra_inputs_getter(_conv_add_relu_extra_inputs_getter_right)
|
| 496 |
+
.set_fused_module(nni.ConvAddReLU2d)
|
| 497 |
+
)
|
| 498 |
+
|
| 499 |
+
conv_configs.append(
|
| 500 |
+
BackendPatternConfig(nni.ConvAddReLU2d)
|
| 501 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 502 |
+
.set_dtype_configs(conv_dtype_configs)
|
| 503 |
+
.set_root_module(nn.Conv2d)
|
| 504 |
+
.set_reference_quantized_module(nnqr.Conv2d)
|
| 505 |
+
)
|
| 506 |
+
|
| 507 |
+
# ========================
|
| 508 |
+
# | CONFIGS FOR LINEAR |
|
| 509 |
+
# ========================
|
| 510 |
+
|
| 511 |
+
linear_dtype_configs = [
|
| 512 |
+
onednn_weighted_op_int8_dtype_config,
|
| 513 |
+
onednn_dynamic_int8_dtype_config,
|
| 514 |
+
]
|
| 515 |
+
linear_configs = _get_linear_configs(linear_dtype_configs)
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
def _add_eltwise_fusion_configs(
|
| 519 |
+
configs,
|
| 520 |
+
root_module,
|
| 521 |
+
root_op,
|
| 522 |
+
post_module,
|
| 523 |
+
post_op,
|
| 524 |
+
dtype_configs,
|
| 525 |
+
fuser_method,
|
| 526 |
+
fused_module,
|
| 527 |
+
observation_type,
|
| 528 |
+
ref_quant_module,
|
| 529 |
+
):
|
| 530 |
+
# 1 base module + op module fusion config
|
| 531 |
+
configs.append(
|
| 532 |
+
BackendPatternConfig((root_module, post_module))
|
| 533 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 534 |
+
.set_fuser_method(fuser_method)
|
| 535 |
+
.set_fused_module(fused_module)
|
| 536 |
+
)
|
| 537 |
+
# base module + functional post op
|
| 538 |
+
configs.append(
|
| 539 |
+
BackendPatternConfig((root_module, post_op))
|
| 540 |
+
.set_dtype_configs(dtype_configs) # noqa: E131
|
| 541 |
+
.set_fuser_method(fuser_method)
|
| 542 |
+
.set_fused_module(fused_module)
|
| 543 |
+
)
|
| 544 |
+
|
| 545 |
+
# 2 fused module configs
|
| 546 |
+
configs.append(
|
| 547 |
+
BackendPatternConfig(fused_module)
|
| 548 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 549 |
+
.set_dtype_configs(dtype_configs)
|
| 550 |
+
.set_root_module(root_module)
|
| 551 |
+
.set_reference_quantized_module(ref_quant_module)
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
# 3 functional base op + post op configs
|
| 555 |
+
configs.append(
|
| 556 |
+
BackendPatternConfig((root_op, post_module))
|
| 557 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 558 |
+
.set_dtype_configs(dtype_configs)
|
| 559 |
+
)
|
| 560 |
+
configs.append(
|
| 561 |
+
BackendPatternConfig((root_op, post_op))
|
| 562 |
+
.set_observation_type(observation_type) # noqa: E131
|
| 563 |
+
.set_dtype_configs(dtype_configs)
|
| 564 |
+
)
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
# Configs for linear + leaky_relu fusion
|
| 568 |
+
_add_eltwise_fusion_configs(
|
| 569 |
+
linear_configs,
|
| 570 |
+
nn.Linear,
|
| 571 |
+
F.linear,
|
| 572 |
+
nn.LeakyReLU,
|
| 573 |
+
F.leaky_relu,
|
| 574 |
+
linear_dtype_configs,
|
| 575 |
+
_sequential_wrapper2(nni.LinearLeakyReLU),
|
| 576 |
+
nni.LinearLeakyReLU,
|
| 577 |
+
observation_type,
|
| 578 |
+
nnqr.Linear,
|
| 579 |
+
)
|
| 580 |
+
|
| 581 |
+
# Configs for linear module + batchnorm + leaky_relu
|
| 582 |
+
linear_configs.append(
|
| 583 |
+
BackendPatternConfig((nn.Linear, nn.BatchNorm1d, nn.LeakyReLU))
|
| 584 |
+
.set_dtype_configs(linear_dtype_configs) # noqa: E131
|
| 585 |
+
.set_fuser_method(_fuse_linear_bn_leaky_relu)
|
| 586 |
+
.set_fused_module(nni.LinearLeakyReLU)
|
| 587 |
+
)
|
| 588 |
+
|
| 589 |
+
# Configs for linear + tanh fusion
|
| 590 |
+
_add_eltwise_fusion_configs(
|
| 591 |
+
linear_configs,
|
| 592 |
+
nn.Linear,
|
| 593 |
+
F.linear,
|
| 594 |
+
nn.Tanh,
|
| 595 |
+
torch.tanh,
|
| 596 |
+
linear_dtype_configs,
|
| 597 |
+
_sequential_wrapper2(nni.LinearTanh),
|
| 598 |
+
nni.LinearTanh,
|
| 599 |
+
observation_type,
|
| 600 |
+
nnqr.Linear,
|
| 601 |
+
)
|
| 602 |
+
|
| 603 |
+
# ===========================
|
| 604 |
+
# | CONFIGS FOR OTHER OPS |
|
| 605 |
+
# ===========================
|
| 606 |
+
|
| 607 |
+
binary_op_dtype_configs = [onednn_op_quint8_dtype_config]
|
| 608 |
+
default_op_dtype_configs = [onednn_op_quint8_dtype_config]
|
| 609 |
+
fixed_qparams_op_dtype_configs = [onednn_op_quint8_dtype_config]
|
| 610 |
+
share_qparams_op_dtype_configs = [onednn_op_quint8_dtype_config]
|
| 611 |
+
rnn_op_dtype_configs = [onednn_dynamic_int8_dtype_config]
|
| 612 |
+
embedding_op_dtype_configs = [onednn_weight_only_qint8_dtype_config]
|
| 613 |
+
layer_norm_op_dtype_configs = [onednn_input_output_only_quint8_dtype_config]
|
| 614 |
+
|
| 615 |
+
# =====================
|
| 616 |
+
# | BACKEND CONFIGS |
|
| 617 |
+
# =====================
|
| 618 |
+
|
| 619 |
+
|
| 620 |
+
def get_onednn_backend_config() -> BackendConfig:
|
| 621 |
+
"""
|
| 622 |
+
Return the `BackendConfig` for PyTorch's native ONEDNN backend.
|
| 623 |
+
"""
|
| 624 |
+
return (
|
| 625 |
+
BackendConfig("onednn")
|
| 626 |
+
.set_backend_pattern_configs(conv_configs)
|
| 627 |
+
.set_backend_pattern_configs(linear_configs)
|
| 628 |
+
.set_backend_pattern_configs(_get_binary_op_configs(binary_op_dtype_configs))
|
| 629 |
+
.set_backend_pattern_config(_get_cat_config(default_op_dtype_configs))
|
| 630 |
+
.set_backend_pattern_configs(_get_default_op_configs(default_op_dtype_configs))
|
| 631 |
+
.set_backend_pattern_configs(
|
| 632 |
+
_get_fixed_qparams_op_configs(fixed_qparams_op_dtype_configs)
|
| 633 |
+
)
|
| 634 |
+
.set_backend_pattern_configs(
|
| 635 |
+
_get_share_qparams_op_configs(share_qparams_op_dtype_configs)
|
| 636 |
+
)
|
| 637 |
+
.set_backend_pattern_configs(_get_bn_configs(default_op_dtype_configs))
|
| 638 |
+
.set_backend_pattern_configs(_get_ln_configs(layer_norm_op_dtype_configs))
|
| 639 |
+
.set_backend_pattern_configs(_get_rnn_op_configs(rnn_op_dtype_configs))
|
| 640 |
+
.set_backend_pattern_configs(
|
| 641 |
+
_get_embedding_op_configs(embedding_op_dtype_configs)
|
| 642 |
+
)
|
| 643 |
+
)
|
| 644 |
+
|
| 645 |
+
|
| 646 |
+
__all__ = [
|
| 647 |
+
"get_onednn_backend_config",
|
| 648 |
+
]
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/qnnpack.py
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from ._common_operator_config_utils import (
|
| 4 |
+
_get_binary_op_configs,
|
| 5 |
+
_get_bn_configs,
|
| 6 |
+
_get_cat_config,
|
| 7 |
+
_get_conv_configs,
|
| 8 |
+
_get_default_op_configs,
|
| 9 |
+
_get_embedding_op_configs,
|
| 10 |
+
_get_fixed_qparams_op_configs,
|
| 11 |
+
_get_linear_configs,
|
| 12 |
+
_get_rnn_op_configs,
|
| 13 |
+
_get_share_qparams_op_configs,
|
| 14 |
+
)
|
| 15 |
+
from .backend_config import BackendConfig, DTypeConfig, DTypeWithConstraints
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
__all__ = [
|
| 19 |
+
"get_qnnpack_backend_config",
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# ===================
|
| 23 |
+
# | DTYPE CONFIGS |
|
| 24 |
+
# ===================
|
| 25 |
+
|
| 26 |
+
qnnpack_weighted_op_quint8_dtype_config = DTypeConfig(
|
| 27 |
+
input_dtype=torch.quint8,
|
| 28 |
+
output_dtype=torch.quint8,
|
| 29 |
+
weight_dtype=torch.qint8,
|
| 30 |
+
bias_dtype=torch.float,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
qnnpack_default_op_quint8_dtype_config = DTypeConfig(
|
| 34 |
+
input_dtype=torch.quint8,
|
| 35 |
+
output_dtype=torch.quint8,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
qnnpack_default_op_fp16_dtype_config = DTypeConfig(
|
| 39 |
+
input_dtype=torch.float16,
|
| 40 |
+
output_dtype=torch.float16,
|
| 41 |
+
weight_dtype=torch.float16,
|
| 42 |
+
bias_dtype=torch.float16,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
qnnpack_default_dynamic_int8_dtype_config = DTypeConfig(
|
| 46 |
+
input_dtype=torch.quint8,
|
| 47 |
+
output_dtype=torch.float,
|
| 48 |
+
weight_dtype=torch.qint8,
|
| 49 |
+
bias_dtype=torch.float,
|
| 50 |
+
is_dynamic=True,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
qnnpack_default_dynamic_float16_dtype_config = DTypeConfig(
|
| 54 |
+
input_dtype=torch.float16,
|
| 55 |
+
output_dtype=torch.float,
|
| 56 |
+
weight_dtype=torch.float16,
|
| 57 |
+
bias_dtype=torch.float,
|
| 58 |
+
is_dynamic=True,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
qnnpack_weight_only_quint8_dtype_config = DTypeConfig(
|
| 62 |
+
input_dtype=torch.float,
|
| 63 |
+
output_dtype=torch.float,
|
| 64 |
+
weight_dtype=torch.quint8,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
qnnpack_weight_only_quint4x2_dtype_config = DTypeConfig(
|
| 68 |
+
input_dtype=torch.float,
|
| 69 |
+
output_dtype=torch.float,
|
| 70 |
+
weight_dtype=torch.quint4x2,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# xnnpack compatible dtype configs
|
| 74 |
+
|
| 75 |
+
# We restrict scale values to be 2 ** -12 to ensure the
|
| 76 |
+
# requantization scale never falls below the xnnpack lower
|
| 77 |
+
# threshold. Additionally, for qint8 weight, we restrict
|
| 78 |
+
# the quantization values to [-127, +127], excluding -128.
|
| 79 |
+
# For more detail, refer to the description of
|
| 80 |
+
# `default_symmetric_qnnpack_qconfig`.
|
| 81 |
+
|
| 82 |
+
# TODO: add additional restriction on qscheme to ensure it
|
| 83 |
+
# is either per_tensor_symmetric or per_channel_symmetric
|
| 84 |
+
|
| 85 |
+
qnnpack_act_qint8_scale_min_2_neg_12 = DTypeWithConstraints(
|
| 86 |
+
dtype=torch.qint8,
|
| 87 |
+
scale_min_lower_bound=2**-12,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
qnnpack_weight_qint8_neg_127_to_127_scale_min_2_neg_12 = DTypeWithConstraints(
|
| 91 |
+
dtype=torch.qint8,
|
| 92 |
+
quant_min_lower_bound=-127,
|
| 93 |
+
quant_max_upper_bound=127,
|
| 94 |
+
scale_min_lower_bound=2**-12,
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
qnnpack_weighted_op_qint8_symmetric_dtype_config = DTypeConfig(
|
| 98 |
+
input_dtype=qnnpack_act_qint8_scale_min_2_neg_12,
|
| 99 |
+
output_dtype=qnnpack_act_qint8_scale_min_2_neg_12,
|
| 100 |
+
weight_dtype=qnnpack_weight_qint8_neg_127_to_127_scale_min_2_neg_12,
|
| 101 |
+
bias_dtype=torch.float,
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
qnnpack_default_op_qint8_symmetric_dtype_config = DTypeConfig(
|
| 105 |
+
input_dtype=qnnpack_act_qint8_scale_min_2_neg_12,
|
| 106 |
+
output_dtype=qnnpack_act_qint8_scale_min_2_neg_12,
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# =====================
|
| 111 |
+
# | BACKEND CONFIGS |
|
| 112 |
+
# =====================
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def get_qnnpack_backend_config() -> BackendConfig:
|
| 116 |
+
"""
|
| 117 |
+
Return the `BackendConfig` for PyTorch's native QNNPACK backend.
|
| 118 |
+
"""
|
| 119 |
+
conv_dtype_configs = [
|
| 120 |
+
qnnpack_weighted_op_qint8_symmetric_dtype_config,
|
| 121 |
+
qnnpack_weighted_op_quint8_dtype_config,
|
| 122 |
+
]
|
| 123 |
+
linear_dtype_configs = [
|
| 124 |
+
qnnpack_weighted_op_qint8_symmetric_dtype_config,
|
| 125 |
+
qnnpack_weighted_op_quint8_dtype_config,
|
| 126 |
+
qnnpack_default_dynamic_int8_dtype_config,
|
| 127 |
+
qnnpack_default_dynamic_float16_dtype_config,
|
| 128 |
+
]
|
| 129 |
+
binary_op_dtype_configs = [
|
| 130 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 131 |
+
qnnpack_default_op_quint8_dtype_config,
|
| 132 |
+
]
|
| 133 |
+
default_op_dtype_configs = [
|
| 134 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 135 |
+
qnnpack_default_op_quint8_dtype_config,
|
| 136 |
+
]
|
| 137 |
+
fixed_qparams_op_dtype_configs = [
|
| 138 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 139 |
+
qnnpack_default_op_quint8_dtype_config,
|
| 140 |
+
]
|
| 141 |
+
share_qparams_op_dtype_configs = [
|
| 142 |
+
qnnpack_default_op_qint8_symmetric_dtype_config,
|
| 143 |
+
qnnpack_default_op_quint8_dtype_config,
|
| 144 |
+
]
|
| 145 |
+
rnn_op_dtype_configs = [
|
| 146 |
+
qnnpack_default_dynamic_int8_dtype_config,
|
| 147 |
+
qnnpack_default_dynamic_float16_dtype_config,
|
| 148 |
+
]
|
| 149 |
+
embedding_op_dtype_configs = [
|
| 150 |
+
qnnpack_weight_only_quint8_dtype_config,
|
| 151 |
+
qnnpack_weight_only_quint4x2_dtype_config,
|
| 152 |
+
]
|
| 153 |
+
return (
|
| 154 |
+
BackendConfig("qnnpack")
|
| 155 |
+
.set_backend_pattern_configs(_get_conv_configs(conv_dtype_configs))
|
| 156 |
+
.set_backend_pattern_configs(_get_linear_configs(linear_dtype_configs))
|
| 157 |
+
.set_backend_pattern_configs(_get_binary_op_configs(binary_op_dtype_configs))
|
| 158 |
+
.set_backend_pattern_config(_get_cat_config(default_op_dtype_configs))
|
| 159 |
+
.set_backend_pattern_configs(_get_default_op_configs(default_op_dtype_configs))
|
| 160 |
+
.set_backend_pattern_configs(
|
| 161 |
+
_get_fixed_qparams_op_configs(fixed_qparams_op_dtype_configs)
|
| 162 |
+
)
|
| 163 |
+
.set_backend_pattern_configs(
|
| 164 |
+
_get_share_qparams_op_configs(share_qparams_op_dtype_configs)
|
| 165 |
+
)
|
| 166 |
+
.set_backend_pattern_configs(_get_bn_configs(default_op_dtype_configs))
|
| 167 |
+
.set_backend_pattern_configs(_get_rnn_op_configs(rnn_op_dtype_configs))
|
| 168 |
+
.set_backend_pattern_configs(
|
| 169 |
+
_get_embedding_op_configs(embedding_op_dtype_configs)
|
| 170 |
+
)
|
| 171 |
+
)
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/tensorrt.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: allow-untyped-defs
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
from ._common_operator_config_utils import (
|
| 5 |
+
_get_binary_op_configs,
|
| 6 |
+
_get_conv_configs,
|
| 7 |
+
_get_linear_configs,
|
| 8 |
+
_get_share_qparams_op_configs,
|
| 9 |
+
_get_tensor_info_op_configs,
|
| 10 |
+
)
|
| 11 |
+
from .backend_config import (
|
| 12 |
+
BackendConfig,
|
| 13 |
+
BackendPatternConfig,
|
| 14 |
+
DTypeConfig,
|
| 15 |
+
ObservationType,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
__all__ = [
|
| 20 |
+
"get_tensorrt_backend_config",
|
| 21 |
+
"get_tensorrt_backend_config_dict",
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def get_tensorrt_backend_config() -> BackendConfig:
|
| 26 |
+
"""
|
| 27 |
+
Return the `BackendConfig` for the TensorRT backend.
|
| 28 |
+
NOTE: Current api will change in the future, it's just to unblock experimentation for
|
| 29 |
+
new backends, please don't use it right now.
|
| 30 |
+
TODO: add a README when it's more stable
|
| 31 |
+
"""
|
| 32 |
+
# dtype configs
|
| 33 |
+
weighted_op_qint8_dtype_config = DTypeConfig(
|
| 34 |
+
input_dtype=torch.qint8,
|
| 35 |
+
output_dtype=torch.qint8,
|
| 36 |
+
weight_dtype=torch.qint8,
|
| 37 |
+
bias_dtype=torch.float,
|
| 38 |
+
)
|
| 39 |
+
non_weighted_op_qint8_dtype_config = DTypeConfig(
|
| 40 |
+
input_dtype=torch.qint8,
|
| 41 |
+
output_dtype=torch.qint8,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
addmm_config = (
|
| 45 |
+
BackendPatternConfig(torch.addmm)
|
| 46 |
+
.set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT)
|
| 47 |
+
.add_dtype_config(weighted_op_qint8_dtype_config)
|
| 48 |
+
._set_input_type_to_index(
|
| 49 |
+
{
|
| 50 |
+
"bias": 0,
|
| 51 |
+
"input": 1,
|
| 52 |
+
"weight": 2,
|
| 53 |
+
}
|
| 54 |
+
)
|
| 55 |
+
)
|
| 56 |
+
cat_config = (
|
| 57 |
+
BackendPatternConfig(torch.cat)
|
| 58 |
+
.set_observation_type(ObservationType.OUTPUT_SHARE_OBSERVER_WITH_INPUT)
|
| 59 |
+
.add_dtype_config(non_weighted_op_qint8_dtype_config)
|
| 60 |
+
)
|
| 61 |
+
conv_dtype_configs = [
|
| 62 |
+
weighted_op_qint8_dtype_config,
|
| 63 |
+
]
|
| 64 |
+
linear_dtype_configs = [
|
| 65 |
+
weighted_op_qint8_dtype_config,
|
| 66 |
+
]
|
| 67 |
+
binary_op_dtype_configs = [
|
| 68 |
+
weighted_op_qint8_dtype_config,
|
| 69 |
+
]
|
| 70 |
+
share_qparams_op_dtype_configs = [
|
| 71 |
+
non_weighted_op_qint8_dtype_config,
|
| 72 |
+
]
|
| 73 |
+
tensor_info_op_dtype_configs = [
|
| 74 |
+
non_weighted_op_qint8_dtype_config,
|
| 75 |
+
]
|
| 76 |
+
# there might be things not supported in fx2trt, but it will error out
|
| 77 |
+
# during fx2trt conversion and can support them after that
|
| 78 |
+
return (
|
| 79 |
+
BackendConfig("tensorrt")
|
| 80 |
+
.set_backend_pattern_configs(_get_conv_configs(conv_dtype_configs))
|
| 81 |
+
.set_backend_pattern_config(addmm_config)
|
| 82 |
+
.set_backend_pattern_config(cat_config)
|
| 83 |
+
.set_backend_pattern_configs(_get_linear_configs(linear_dtype_configs))
|
| 84 |
+
.set_backend_pattern_configs(_get_binary_op_configs(binary_op_dtype_configs))
|
| 85 |
+
.set_backend_pattern_configs(
|
| 86 |
+
_get_share_qparams_op_configs(share_qparams_op_dtype_configs)
|
| 87 |
+
)
|
| 88 |
+
.set_backend_pattern_configs(
|
| 89 |
+
_get_tensor_info_op_configs(tensor_info_op_dtype_configs)
|
| 90 |
+
)
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def get_tensorrt_backend_config_dict():
|
| 95 |
+
"""
|
| 96 |
+
Return the `BackendConfig` for the TensorRT backend in dictionary form.
|
| 97 |
+
"""
|
| 98 |
+
return get_tensorrt_backend_config().to_dict()
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/utils.py
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mypy: allow-untyped-defs
|
| 2 |
+
from typing import Any, Callable, Union
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
from torch.ao.quantization.fuser_method_mappings import _reverse2, _reverse3
|
| 8 |
+
from torch.ao.quantization.utils import Pattern
|
| 9 |
+
|
| 10 |
+
from .backend_config import BackendConfig, BackendPatternConfig, DTypeConfig
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = [
|
| 14 |
+
"get_pattern_to_dtype_configs",
|
| 15 |
+
"get_qat_module_classes",
|
| 16 |
+
"get_fused_module_classes",
|
| 17 |
+
"get_pattern_to_input_type_to_index",
|
| 18 |
+
"get_root_module_to_quantized_reference_module",
|
| 19 |
+
"get_fuser_method_mapping",
|
| 20 |
+
"get_module_to_qat_module",
|
| 21 |
+
"get_fusion_pattern_to_root_node_getter",
|
| 22 |
+
"get_fusion_pattern_to_extra_inputs_getter",
|
| 23 |
+
"remove_boolean_dispatch_from_name",
|
| 24 |
+
"pattern_to_human_readable",
|
| 25 |
+
"entry_to_pretty_str",
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get_pattern_to_dtype_configs(
|
| 30 |
+
backend_config: BackendConfig,
|
| 31 |
+
) -> dict[Pattern, list[DTypeConfig]]:
|
| 32 |
+
pattern_to_dtype_configs: dict[Pattern, list[DTypeConfig]] = {}
|
| 33 |
+
for pattern, config in backend_config._pattern_complex_format_to_config.items():
|
| 34 |
+
pattern_to_dtype_configs[pattern] = config.dtype_configs
|
| 35 |
+
return pattern_to_dtype_configs
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def get_qat_module_classes(backend_config: BackendConfig) -> tuple[type, ...]:
|
| 39 |
+
qat_module_classes = [
|
| 40 |
+
config.qat_module
|
| 41 |
+
for config in backend_config.configs
|
| 42 |
+
if config.qat_module is not None
|
| 43 |
+
]
|
| 44 |
+
return tuple(set(qat_module_classes))
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def get_fused_module_classes(backend_config: BackendConfig) -> tuple[type, ...]:
|
| 48 |
+
fused_module_classes = [
|
| 49 |
+
config.fused_module
|
| 50 |
+
for config in backend_config.configs
|
| 51 |
+
if config.fused_module is not None
|
| 52 |
+
]
|
| 53 |
+
return tuple(set(fused_module_classes))
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def get_pattern_to_input_type_to_index(
|
| 57 |
+
backend_config: BackendConfig,
|
| 58 |
+
) -> dict[Pattern, dict[str, int]]:
|
| 59 |
+
pattern_to_input_type_to_index: dict[Pattern, dict[str, int]] = {}
|
| 60 |
+
for pattern, config in backend_config._pattern_complex_format_to_config.items():
|
| 61 |
+
pattern_to_input_type_to_index[pattern] = config._input_type_to_index
|
| 62 |
+
return pattern_to_input_type_to_index
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def get_root_module_to_quantized_reference_module(
|
| 66 |
+
backend_config: BackendConfig,
|
| 67 |
+
) -> dict[type[torch.nn.Module], type[torch.nn.Module]]:
|
| 68 |
+
mapping: dict[type[torch.nn.Module], type[torch.nn.Module]] = {}
|
| 69 |
+
for config in backend_config.configs:
|
| 70 |
+
if (
|
| 71 |
+
config.root_module is not None
|
| 72 |
+
and config.reference_quantized_module is not None
|
| 73 |
+
):
|
| 74 |
+
mapping[config.root_module] = config.reference_quantized_module
|
| 75 |
+
return mapping
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def get_fuser_method_mapping(
|
| 79 |
+
backend_config: BackendConfig,
|
| 80 |
+
) -> dict[Pattern, Union[nn.Sequential, Callable]]:
|
| 81 |
+
fuser_method_mapping: dict[Pattern, Union[nn.Sequential, Callable]] = {}
|
| 82 |
+
for pattern, config in backend_config._pattern_complex_format_to_config.items():
|
| 83 |
+
if config.fuser_method is not None:
|
| 84 |
+
# Note: both the fuser method and the pattern are specified in forward order in the
|
| 85 |
+
# BackendConfig, but the internal pattern matching code uses the reversed nested tuple
|
| 86 |
+
# format, so we need to convert both to the internal format
|
| 87 |
+
fuser_method = _get_fuser_method_in_reversed_nested_tuple_format(config)
|
| 88 |
+
fuser_method_mapping[pattern] = fuser_method
|
| 89 |
+
return fuser_method_mapping
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def get_module_to_qat_module(
|
| 93 |
+
backend_config: BackendConfig,
|
| 94 |
+
) -> dict[Pattern, type[torch.nn.Module]]:
|
| 95 |
+
module_to_qat_module: dict[Pattern, type[torch.nn.Module]] = {}
|
| 96 |
+
for pattern, config in backend_config._pattern_complex_format_to_config.items():
|
| 97 |
+
if config.qat_module is not None:
|
| 98 |
+
module_to_qat_module[pattern] = config.qat_module
|
| 99 |
+
return module_to_qat_module
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def get_fusion_pattern_to_root_node_getter(
|
| 103 |
+
backend_config: BackendConfig,
|
| 104 |
+
) -> dict[Pattern, Callable]:
|
| 105 |
+
"""Get a map from fusion pattern to a function that returns the root node
|
| 106 |
+
from the fusion pattern, e.g. the most common one is:
|
| 107 |
+
def get_root_node(node_pattern):
|
| 108 |
+
while not isinstance(node_pattern[-1], Node):
|
| 109 |
+
node_pattern = node_pattern[-1]
|
| 110 |
+
return node_pattern[-1]
|
| 111 |
+
This can work for all patterns whose root node is the "last node" in the pattern,
|
| 112 |
+
e.g. (torch.add, MatchAllNode, (torch.ReLU, torch.Conv2d))
|
| 113 |
+
"""
|
| 114 |
+
root_node_getter_mapping: dict[Pattern, Callable] = {}
|
| 115 |
+
for pattern, config in backend_config._pattern_complex_format_to_config.items():
|
| 116 |
+
if config._root_node_getter is not None:
|
| 117 |
+
root_node_getter_mapping[pattern] = config._root_node_getter
|
| 118 |
+
return root_node_getter_mapping
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def get_fusion_pattern_to_extra_inputs_getter(
|
| 122 |
+
backend_config: BackendConfig,
|
| 123 |
+
) -> dict[Pattern, Callable]:
|
| 124 |
+
"""Get a map from fusion pattern to a function that returns extra input nodes
|
| 125 |
+
from the fusion pattern, in the order required by the root node. This is optional,
|
| 126 |
+
if not specified, we will not copy over any extra inputs for the root node.
|
| 127 |
+
Example:
|
| 128 |
+
# Let's say we have the pattern (torch.add, MatchAllNode, (torch.nn.BatchNorm2d, torch.nn.Conv2d))
|
| 129 |
+
# and root node is torch.nn.Conv2d, and the node in MatchAllNode would be an extra
|
| 130 |
+
# argument to the fused module, we can unpack the pattern and return the node at
|
| 131 |
+
# MatchAllNode here
|
| 132 |
+
# we can implement extra_inputs_getter as follows:
|
| 133 |
+
def extra_inputs_getter(pattern) -> List[Any]:
|
| 134 |
+
add, extra_input, conv_pattern = pattern
|
| 135 |
+
return [extra_input]
|
| 136 |
+
"""
|
| 137 |
+
extra_inputs_getter_mapping: dict[Pattern, Callable] = {}
|
| 138 |
+
for pattern, config in backend_config._pattern_complex_format_to_config.items():
|
| 139 |
+
if config._extra_inputs_getter is not None:
|
| 140 |
+
extra_inputs_getter_mapping[pattern] = config._extra_inputs_getter
|
| 141 |
+
return extra_inputs_getter_mapping
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def remove_boolean_dispatch_from_name(p) -> Any:
|
| 145 |
+
"""
|
| 146 |
+
Some ops have a default string representation such as
|
| 147 |
+
'<function boolean_dispatch.<locals>.fn at 0x7ff1106bf280>',
|
| 148 |
+
this function replaces them with the hardcoded function names.
|
| 149 |
+
"""
|
| 150 |
+
if p is F.fractional_max_pool2d:
|
| 151 |
+
return "torch.nn.functional.fractional_max_pool2d"
|
| 152 |
+
elif p is F.fractional_max_pool3d:
|
| 153 |
+
return "torch.nn.functional.fractional_max_pool3d"
|
| 154 |
+
elif p is F.max_pool1d:
|
| 155 |
+
return "torch.nn.functional.max_pool1d"
|
| 156 |
+
elif p is F.max_pool2d:
|
| 157 |
+
return "torch.nn.functional.max_pool2d"
|
| 158 |
+
elif p is F.max_pool3d:
|
| 159 |
+
return "torch.nn.functional.max_pool3d"
|
| 160 |
+
elif p is F.adaptive_max_pool1d:
|
| 161 |
+
return "torch.nn.functional.adaptive_max_pool1d"
|
| 162 |
+
elif p is F.adaptive_max_pool2d:
|
| 163 |
+
return "torch.nn.functional.adaptive_max_pool2d"
|
| 164 |
+
elif p is F.adaptive_max_pool3d:
|
| 165 |
+
return "torch.nn.functional.adaptive_max_pool3d"
|
| 166 |
+
assert "boolean_dispatch" not in str(p), (
|
| 167 |
+
f"{p} does not have a human readable representation in "
|
| 168 |
+
+ "quantization documentation"
|
| 169 |
+
)
|
| 170 |
+
return p
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def pattern_to_human_readable(p) -> Any:
|
| 174 |
+
if isinstance(p, tuple):
|
| 175 |
+
# nested patterns, recurse
|
| 176 |
+
return tuple(pattern_to_human_readable(inner_p) for inner_p in p)
|
| 177 |
+
elif isinstance(p, str):
|
| 178 |
+
# method names are already human readable
|
| 179 |
+
return p
|
| 180 |
+
else:
|
| 181 |
+
p = remove_boolean_dispatch_from_name(p)
|
| 182 |
+
return p
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
# TODO(future PR): move backend_config_dict to use dataclass and move this logic to
|
| 186 |
+
# the corresponding __str__ function
|
| 187 |
+
def entry_to_pretty_str(entry) -> str:
|
| 188 |
+
"""
|
| 189 |
+
Given a backend_config_dict entry, returns a string with the human readable
|
| 190 |
+
representation of it.
|
| 191 |
+
"""
|
| 192 |
+
s = "{\n"
|
| 193 |
+
|
| 194 |
+
# always output the pattern first
|
| 195 |
+
if "pattern" in entry:
|
| 196 |
+
pattern_str = pattern_to_human_readable(entry["pattern"])
|
| 197 |
+
|
| 198 |
+
s += f" 'pattern': {pattern_str},\n"
|
| 199 |
+
|
| 200 |
+
# custom output for dtype_configs to make it look nice
|
| 201 |
+
if "dtype_configs" in entry:
|
| 202 |
+
s += " 'dtype_configs': [\n"
|
| 203 |
+
for dtype_config in entry["dtype_configs"]:
|
| 204 |
+
s += " {\n"
|
| 205 |
+
for k, v in dtype_config.items():
|
| 206 |
+
s += f" '{k}': {v},\n"
|
| 207 |
+
s += " },\n"
|
| 208 |
+
s += " ],\n"
|
| 209 |
+
|
| 210 |
+
# custom output for num_tensor_args_to_observation_type to make it look nice
|
| 211 |
+
if "num_tensor_args_to_observation_type" in entry:
|
| 212 |
+
s += " 'num_tensor_args_to_observation_type': {\n"
|
| 213 |
+
for k, v in entry["num_tensor_args_to_observation_type"].items():
|
| 214 |
+
s += f" {k}: {v},\n"
|
| 215 |
+
s += " },\n"
|
| 216 |
+
|
| 217 |
+
# output all the other fields
|
| 218 |
+
custom_handled_fields = [
|
| 219 |
+
"pattern",
|
| 220 |
+
"dtype_configs",
|
| 221 |
+
"num_tensor_args_to_observation_type",
|
| 222 |
+
]
|
| 223 |
+
for field_name in entry:
|
| 224 |
+
if field_name in custom_handled_fields:
|
| 225 |
+
continue
|
| 226 |
+
s += f" '{field_name}': {entry[field_name]},\n"
|
| 227 |
+
|
| 228 |
+
s += "}"
|
| 229 |
+
return s
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
def _get_pattern_in_reversed_nested_tuple_format(
|
| 233 |
+
config: BackendPatternConfig,
|
| 234 |
+
) -> Pattern:
|
| 235 |
+
"""
|
| 236 |
+
Return the pattern specified in the given config in the reversed nested tuple format
|
| 237 |
+
used internally in the quantization pattern matching code.
|
| 238 |
+
|
| 239 |
+
If the pattern is not a tuple, or the pattern is already specified in the reversed
|
| 240 |
+
nested tuple format, return the pattern as is. Otherwise:
|
| 241 |
+
|
| 242 |
+
For 2-tuples (a, b), return (b, a).
|
| 243 |
+
For 3-tuples (a, b, c), return (c, (b, a)).
|
| 244 |
+
|
| 245 |
+
For example:
|
| 246 |
+
* Given nn.Linear, return nn.Linear
|
| 247 |
+
* Given (nn.Linear, nn.ReLU), return (nn.ReLU, nn.Linear)
|
| 248 |
+
* Given (nn.Conv2d, nn.BatchNorm2d, nn.ReLU), return
|
| 249 |
+
(nn.ReLU, (nn.BatchNorm2d, nn.Conv2d))
|
| 250 |
+
|
| 251 |
+
For context, the reason why this is needed is the user-facing BackendConfig
|
| 252 |
+
API accepts the flat 2-or-3-tuple format in forward order. While this simple
|
| 253 |
+
format handles the vast majority of use cases, it does not handle the more
|
| 254 |
+
complex ones, and so the internal pattern matching code for quantization uses
|
| 255 |
+
the following, more general reversed nested tuple format instead:
|
| 256 |
+
|
| 257 |
+
operator = module_type | functional | torch op | native op | MatchAllNode
|
| 258 |
+
Pattern = (operator, Pattern, Pattern, ...) | operator
|
| 259 |
+
|
| 260 |
+
In the future, we expect to replace the above complex format with the one used
|
| 261 |
+
by the subgraph rewriter in torch.fx, so we don't have to maintain our own
|
| 262 |
+
complex pattern matching code. Then we won't need this helper function anymore.
|
| 263 |
+
"""
|
| 264 |
+
if config._pattern_complex_format is not None:
|
| 265 |
+
return config._pattern_complex_format
|
| 266 |
+
if config.pattern is None:
|
| 267 |
+
raise ValueError(
|
| 268 |
+
"Either 'pattern' or 'pattern_complex_format' must be specified"
|
| 269 |
+
)
|
| 270 |
+
if not isinstance(config.pattern, tuple):
|
| 271 |
+
return config.pattern
|
| 272 |
+
|
| 273 |
+
# Pattern is specified in the simple tuple format, need to convert
|
| 274 |
+
if len(config.pattern) == 2:
|
| 275 |
+
(a, b) = config.pattern
|
| 276 |
+
return (b, a)
|
| 277 |
+
elif len(config.pattern) == 3:
|
| 278 |
+
(a, b, c) = config.pattern
|
| 279 |
+
return (c, (b, a))
|
| 280 |
+
else:
|
| 281 |
+
raise ValueError("Expected a tuple with 2 or 3 elements, got: ", config.pattern)
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
def _get_fuser_method_in_reversed_nested_tuple_format(
|
| 285 |
+
config: BackendPatternConfig,
|
| 286 |
+
) -> Callable:
|
| 287 |
+
"""
|
| 288 |
+
Return the fuser method specified in the given config in the reversed nested
|
| 289 |
+
tuple format used internally in the quantization pattern matching code.
|
| 290 |
+
|
| 291 |
+
If pattern is specified in the reversed nested tuple format, we assume the
|
| 292 |
+
fuser method is also specified in this format and simply return it as is.
|
| 293 |
+
Otherwise, we convert the fuser method as follows:
|
| 294 |
+
|
| 295 |
+
* Given f(is_qat, conv, relu), return f'(is_qat, relu, conv)
|
| 296 |
+
* Given f(is_qat, conv, bn, relu), return f'(is_qat, relu, bn_conv),
|
| 297 |
+
where bn_conv is a 2-tuple (bn, conv)
|
| 298 |
+
|
| 299 |
+
The first argument of a fuser method is always `is_qat` and is not affected
|
| 300 |
+
in the conversion. We currently only support functions with 3 or 4 arguments.
|
| 301 |
+
"""
|
| 302 |
+
assert config.fuser_method is not None
|
| 303 |
+
if config._pattern_complex_format is not None:
|
| 304 |
+
return config.fuser_method
|
| 305 |
+
if not isinstance(config.pattern, tuple):
|
| 306 |
+
raise ValueError("Expected pattern to be a tuple, got: ", config.pattern)
|
| 307 |
+
|
| 308 |
+
# Pattern is specified in the simple tuple format, need to convert
|
| 309 |
+
if len(config.pattern) == 2:
|
| 310 |
+
return _reverse2(config.fuser_method)
|
| 311 |
+
elif len(config.pattern) == 3:
|
| 312 |
+
return _reverse3(config.fuser_method)
|
| 313 |
+
else:
|
| 314 |
+
raise ValueError("Expected a tuple with 2 or 3 elements, got: ", config.pattern)
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/backend_config/x86.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
from ._common_operator_config_utils import (
|
| 4 |
+
_get_binary_op_configs,
|
| 5 |
+
_get_bn_configs,
|
| 6 |
+
_get_cat_config,
|
| 7 |
+
_get_conv_configs,
|
| 8 |
+
_get_default_op_configs,
|
| 9 |
+
_get_embedding_op_configs,
|
| 10 |
+
_get_fixed_qparams_op_configs,
|
| 11 |
+
_get_linear_configs,
|
| 12 |
+
_get_rnn_op_configs,
|
| 13 |
+
_get_share_qparams_op_configs,
|
| 14 |
+
_get_tensor_info_op_configs,
|
| 15 |
+
)
|
| 16 |
+
from .backend_config import BackendConfig, DTypeConfig
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
__all__ = [
|
| 20 |
+
"get_x86_backend_config",
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
# ===================
|
| 24 |
+
# | DTYPE CONFIGS |
|
| 25 |
+
# ===================
|
| 26 |
+
|
| 27 |
+
# X86 aligns with FBGEMM for now
|
| 28 |
+
|
| 29 |
+
x86_weighted_op_int8_dtype_config = DTypeConfig(
|
| 30 |
+
input_dtype=torch.quint8,
|
| 31 |
+
output_dtype=torch.quint8,
|
| 32 |
+
weight_dtype=torch.qint8,
|
| 33 |
+
bias_dtype=torch.float,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
x86_default_op_quint8_dtype_config = DTypeConfig(
|
| 37 |
+
input_dtype=torch.quint8,
|
| 38 |
+
output_dtype=torch.quint8,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
x86_default_op_fp16_dtype_config = DTypeConfig(
|
| 42 |
+
input_dtype=torch.float16,
|
| 43 |
+
output_dtype=torch.float16,
|
| 44 |
+
weight_dtype=torch.float16,
|
| 45 |
+
bias_dtype=torch.float16,
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
x86_default_dynamic_int8_dtype_config = DTypeConfig(
|
| 49 |
+
input_dtype=torch.quint8,
|
| 50 |
+
output_dtype=torch.float,
|
| 51 |
+
weight_dtype=torch.qint8,
|
| 52 |
+
bias_dtype=torch.float,
|
| 53 |
+
is_dynamic=True,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
x86_default_dynamic_float16_dtype_config = DTypeConfig(
|
| 57 |
+
input_dtype=torch.float16,
|
| 58 |
+
output_dtype=torch.float,
|
| 59 |
+
weight_dtype=torch.float16,
|
| 60 |
+
bias_dtype=torch.float,
|
| 61 |
+
is_dynamic=True,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
x86_weight_only_quint8_dtype_config = DTypeConfig(
|
| 65 |
+
input_dtype=torch.float,
|
| 66 |
+
output_dtype=torch.float,
|
| 67 |
+
weight_dtype=torch.quint8,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
x86_weight_only_quint4x2_dtype_config = DTypeConfig(
|
| 71 |
+
input_dtype=torch.float,
|
| 72 |
+
output_dtype=torch.float,
|
| 73 |
+
weight_dtype=torch.quint4x2,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# =====================
|
| 78 |
+
# | BACKEND CONFIGS |
|
| 79 |
+
# =====================
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def get_x86_backend_config() -> BackendConfig:
|
| 83 |
+
"""
|
| 84 |
+
Return the `BackendConfig` for PyTorch's native x86 backend.
|
| 85 |
+
"""
|
| 86 |
+
conv_dtype_configs = [x86_weighted_op_int8_dtype_config]
|
| 87 |
+
linear_dtype_configs = [
|
| 88 |
+
x86_weighted_op_int8_dtype_config,
|
| 89 |
+
x86_default_dynamic_int8_dtype_config,
|
| 90 |
+
x86_default_dynamic_float16_dtype_config,
|
| 91 |
+
]
|
| 92 |
+
binary_op_dtype_configs = [x86_weighted_op_int8_dtype_config]
|
| 93 |
+
default_op_dtype_configs = [x86_default_op_quint8_dtype_config]
|
| 94 |
+
fixed_qparams_op_dtype_configs = [x86_weighted_op_int8_dtype_config]
|
| 95 |
+
share_qparams_op_dtype_configs = [x86_default_op_quint8_dtype_config]
|
| 96 |
+
tensor_info_op_dtype_configs = [x86_default_op_quint8_dtype_config]
|
| 97 |
+
rnn_op_dtype_configs = [
|
| 98 |
+
x86_default_dynamic_int8_dtype_config,
|
| 99 |
+
x86_default_dynamic_float16_dtype_config,
|
| 100 |
+
]
|
| 101 |
+
embedding_op_dtype_configs = [
|
| 102 |
+
x86_weight_only_quint8_dtype_config,
|
| 103 |
+
x86_weight_only_quint4x2_dtype_config,
|
| 104 |
+
]
|
| 105 |
+
return (
|
| 106 |
+
BackendConfig("x86")
|
| 107 |
+
.set_backend_pattern_configs(_get_conv_configs(conv_dtype_configs))
|
| 108 |
+
.set_backend_pattern_configs(_get_linear_configs(linear_dtype_configs))
|
| 109 |
+
.set_backend_pattern_configs(_get_binary_op_configs(binary_op_dtype_configs))
|
| 110 |
+
.set_backend_pattern_config(_get_cat_config(default_op_dtype_configs))
|
| 111 |
+
.set_backend_pattern_configs(_get_default_op_configs(default_op_dtype_configs))
|
| 112 |
+
.set_backend_pattern_configs(
|
| 113 |
+
_get_fixed_qparams_op_configs(fixed_qparams_op_dtype_configs)
|
| 114 |
+
)
|
| 115 |
+
.set_backend_pattern_configs(
|
| 116 |
+
_get_share_qparams_op_configs(share_qparams_op_dtype_configs)
|
| 117 |
+
)
|
| 118 |
+
.set_backend_pattern_configs(
|
| 119 |
+
_get_tensor_info_op_configs(tensor_info_op_dtype_configs)
|
| 120 |
+
)
|
| 121 |
+
.set_backend_pattern_configs(_get_bn_configs(default_op_dtype_configs))
|
| 122 |
+
.set_backend_pattern_configs(_get_rnn_op_configs(rnn_op_dtype_configs))
|
| 123 |
+
.set_backend_pattern_configs(
|
| 124 |
+
_get_embedding_op_configs(embedding_op_dtype_configs)
|
| 125 |
+
)
|
| 126 |
+
)
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .convert import convert
|
| 2 |
+
from .fuse import fuse
|
| 3 |
+
from .prepare import prepare
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (286 Bytes). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__pycache__/_decomposed.cpython-312.pyc
ADDED
|
Binary file (50.4 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__pycache__/_equalize.cpython-312.pyc
ADDED
|
Binary file (37.9 kB). View file
|
|
|
tool_server/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/__pycache__/_lower_to_native_backend.cpython-312.pyc
ADDED
|
Binary file (58 kB). View file
|
|
|