Skip to content

Commit 6a8d286

Browse files
authored
migrate convert/prepare to torchao
Differential Revision: D75095744 Pull Request resolved: #11015
1 parent 18859b0 commit 6a8d286

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+106
-71
lines changed

.lintrunner.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,31 @@ command = [
378378
'--',
379379
'@{{PATHSFILE}}',
380380
]
381+
382+
[[linter]]
383+
code = "TORCH_AO_IMPORT"
384+
include_patterns = ["**/*.py"]
385+
exclude_patterns = [
386+
"third-party/**",
387+
]
388+
389+
command = [
390+
"python3",
391+
"-m",
392+
"lintrunner_adapters",
393+
"run",
394+
"grep_linter",
395+
"--pattern=\\bfrom torch\\.ao\\.quantization\\.(?:quantize_pt2e)(?:\\.[A-Za-z0-9_]+)*\\b",
396+
"--linter-name=TorchAOImport",
397+
"--error-name=Prohibited torch.ao.quantization import",
398+
"""--error-description=\
399+
Imports from torch.ao.quantization are not allowed. \
400+
Please import from torchao.quantization.pt2e instead.\n \
401+
* torchao.quantization.pt2e (includes all the utils, including observers, fake quants etc.) \n \
402+
* torchao.quantization.pt2e.quantizer (quantizer related objects and utils) \n \
403+
* torchao.quantization.pt2e.quantize_pt2e (prepare_pt2e, prepare_qat_pt2e, convert_pt2e) \n\n \
404+
If you need something from torch.ao.quantization, you can add your file to an exclude_patterns for TORCH_AO_IMPORT in .lintrunner.toml. \
405+
""",
406+
"--",
407+
"@{{PATHSFILE}}",
408+
]

.mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,6 @@ ignore_missing_imports = True
9797

9898
[mypy-zstd]
9999
ignore_missing_imports = True
100+
101+
[mypy-torchao.*]
102+
follow_untyped_imports = True

backends/apple/coreml/test/test_coreml_quantizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
)
1616

1717
from executorch.backends.apple.coreml.quantizer import CoreMLQuantizer
18-
from torch.ao.quantization.quantize_pt2e import (
18+
from torch.export import export_for_training
19+
from torchao.quantization.pt2e.quantize_pt2e import (
1920
convert_pt2e,
2021
prepare_pt2e,
2122
prepare_qat_pt2e,
2223
)
23-
from torch.export import export_for_training
2424

2525

2626
class TestCoreMLQuantizer:

backends/cadence/aot/compiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
from executorch.exir.passes.sym_shape_eval_pass import HintBasedSymShapeEvalPass
3838
from executorch.exir.program._program import to_edge_with_preserved_ops
3939
from torch._inductor.decomposition import remove_decompositions
40-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
4140

4241
from torch.export.exported_program import ExportedProgram
42+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
4343

4444
from .passes import get_cadence_passes
4545

@@ -123,7 +123,7 @@ def prepare_and_convert_pt2(
123123
assert isinstance(model_gm, torch.fx.GraphModule)
124124

125125
# Prepare
126-
prepared_model = prepare_pt2e(model_gm, quantizer)
126+
prepared_model = prepare_pt2e(model_gm, quantizer) # pyre-ignore[6]
127127

128128
# Calibrate
129129
# If no calibration data is provided, use the inputs

backends/cortex_m/test/test_replace_quant_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
)
1818
from executorch.exir.dialects._ops import ops as exir_ops
1919
from torch.ao.quantization.observer import HistogramObserver
20-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
2120
from torch.ao.quantization.quantizer.quantizer import (
2221
QuantizationAnnotation,
2322
QuantizationSpec,
2423
Quantizer,
2524
)
2625
from torch.export import export, export_for_training
2726
from torch.fx import GraphModule
27+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
2828

2929

3030
@dataclass(eq=True, frozen=True)

backends/example/example_partitioner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
)
2020
from executorch.exir.dialects._ops import ops as exir_ops
2121
from executorch.exir.graph_module import get_control_flow_submodules
22-
from torch.ao.quantization.pt2e.graph_utils import find_sequential_partitions
2322
from torch.export import ExportedProgram
2423
from torch.fx.passes.operator_support import OperatorSupportBase
24+
from torchao.quantization.pt2e.graph_utils import find_sequential_partitions
2525

2626

2727
@final

backends/example/example_quantizer.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
import torch
1111
from executorch.backends.example.example_operators.ops import module_to_annotator
12-
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer_utils import OperatorConfig
1312
from torch import fx
14-
from torch.ao.quantization.observer import HistogramObserver, MinMaxObserver
15-
from torch.ao.quantization.pt2e.graph_utils import find_sequential_partitions
16-
from torch.ao.quantization.quantizer import QuantizationSpec, Quantizer
13+
from torchao.quantization.pt2e.graph_utils import find_sequential_partitions
14+
from torchao.quantization.pt2e.observer import HistogramObserver, MinMaxObserver
15+
from torchao.quantization.pt2e.quantizer import (
16+
OperatorConfig,
17+
QuantizationSpec,
18+
Quantizer,
19+
)
1720

1821

1922
def get_uint8_tensor_spec(observer_or_fake_quant_ctr):

backends/example/test_example_delegate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
DuplicateDequantNodePass,
1818
)
1919
from executorch.exir.delegate import executorch_call_delegate
20-
21-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
2220
from torch.export import export
2321

22+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
23+
2424
from torchvision.models.quantization import mobilenet_v2
2525

2626

backends/nxp/tests/executorch_pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
to_edge_transform_and_lower,
2121
)
2222
from torch import nn
23-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
23+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
2424

2525

2626
def _quantize_model(model, calibration_inputs: list[tuple[torch.Tensor]]):

backends/nxp/tests/test_quantizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import executorch.backends.nxp.tests.models as models
99
import torch
1010
from executorch.backends.nxp.quantizer.neutron_quantizer import NeutronQuantizer
11-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
11+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
1212

1313

1414
def _get_target_name(node):

backends/qualcomm/tests/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
from executorch.exir.pass_base import ExportPass
4545
from executorch.exir.passes.memory_planning_pass import MemoryPlanningPass
4646
from executorch.exir.program import ExecutorchProgram, ExecutorchProgramManager
47-
from torch.ao.quantization.quantize_pt2e import (
47+
from torch.fx.passes.infra.pass_base import PassResult
48+
from torchao.quantization.pt2e.quantize_pt2e import (
4849
convert_pt2e,
4950
prepare_pt2e,
5051
prepare_qat_pt2e,
5152
)
52-
from torch.fx.passes.infra.pass_base import PassResult
5353

5454

5555
def generate_context_binary(

backends/qualcomm/utils/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,8 @@ def skip_annotation(
607607
from executorch.backends.qualcomm.serialization.qc_schema_serialize import (
608608
flatbuffer_to_option,
609609
)
610-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
611610
from torch.fx.passes.infra.partitioner import CapabilityBasedPartitioner
611+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
612612

613613
def prepare_subgm(subgm, subgm_name):
614614
# prepare current submodule for quantization annotation

backends/transforms/test/test_duplicate_dynamic_quant_chain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
get_symmetric_quantization_config,
1616
XNNPACKQuantizer,
1717
)
18-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
1918

2019
# TODO: Move away from using torch's internal testing utils
2120
from torch.testing._internal.common_quantization import (
2221
NodeSpec as ns,
2322
QuantizationTestCase,
2423
TestHelperModules,
2524
)
25+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
2626

2727

2828
class MyTestHelperModules:

backends/vulkan/test/test_vulkan_delegate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
ExecutorchProgramManager,
2525
)
2626

27-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
28-
2927
from torch.ao.quantization.quantizer import Quantizer
3028
from torch.export import Dim, export, export_for_training, ExportedProgram
3129

30+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
31+
3232
ctypes.CDLL("libvulkan.so.1")
3333

3434

backends/vulkan/test/test_vulkan_passes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
from executorch.exir.backend.canonical_partitioners.config_partitioner import (
1717
format_target_name,
1818
)
19-
20-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
2119
from torch.ao.quantization.quantizer import Quantizer
2220

21+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
22+
2323
###################
2424
## Common Models ##
2525
###################

backends/xnnpack/test/ops/test_check_quant_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
)
1010
from executorch.backends.xnnpack.utils.utils import get_param_tensor
1111
from executorch.exir import to_edge_transform_and_lower
12-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
1312
from torch.export import export_for_training
13+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
1414

1515

1616
class TestCheckQuantParams(unittest.TestCase):

backends/xnnpack/test/quantizer/test_pt2e_quantization.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
weight_observer_range_neg_127_to_127,
3333
)
3434
from torch.ao.quantization.qconfig_mapping import QConfigMapping
35-
from torch.ao.quantization.quantize_pt2e import (
36-
convert_pt2e,
37-
prepare_pt2e,
38-
prepare_qat_pt2e,
39-
)
4035
from torch.ao.quantization.quantizer import Quantizer
4136
from torch.ao.quantization.quantizer.composable_quantizer import ComposableQuantizer
4237
from torch.ao.quantization.quantizer.embedding_quantizer import EmbeddingQuantizer
@@ -51,6 +46,11 @@
5146
TemporaryFileName,
5247
TestCase,
5348
)
49+
from torchao.quantization.pt2e.quantize_pt2e import (
50+
convert_pt2e,
51+
prepare_pt2e,
52+
prepare_qat_pt2e,
53+
)
5454

5555

5656
class TestQuantizePT2E(PT2EQuantizationTestCase):

backends/xnnpack/test/quantizer/test_representation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
XNNPACKQuantizer,
99
)
1010
from torch._higher_order_ops.out_dtype import out_dtype # noqa: F401
11-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
1211
from torch.ao.quantization.quantizer import Quantizer
1312
from torch.export import export_for_training
1413
from torch.testing._internal.common_quantization import (
@@ -17,6 +16,7 @@
1716
skipIfNoQNNPACK,
1817
TestHelperModules,
1918
)
19+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
2020

2121

2222
@skipIfNoQNNPACK

backends/xnnpack/test/quantizer/test_xnnpack_quantizer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
convert_to_reference_fx,
2929
prepare_fx,
3030
)
31-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
3231
from torch.export import export_for_training
3332
from torch.testing._internal.common_quantization import (
3433
NodeSpec as ns,
@@ -38,6 +37,7 @@
3837
TestHelperModules,
3938
)
4039
from torch.testing._internal.common_quantized import override_quantized_engine
40+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
4141

4242

4343
@skipIfNoQNNPACK

backends/xnnpack/test/test_xnnpack_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@
7070
_convert_to_reference_decomposed_fx,
7171
prepare_fx,
7272
)
73-
74-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
7573
from torch.export import export_for_training
7674

7775
from torch.testing import FileCheck
7876

77+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
78+
7979

8080
def randomize_bn(num_features: int, dimensionality: int = 2) -> torch.nn.Module:
8181
if dimensionality == 1:

backends/xnnpack/test/tester/tester.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@
5555
)
5656
from executorch.exir.program._program import _transform
5757
from torch._export.pass_base import PassType
58-
from torch.ao.quantization.quantize_pt2e import (
59-
convert_pt2e,
60-
prepare_pt2e,
61-
prepare_qat_pt2e,
62-
)
6358
from torch.ao.quantization.quantizer.quantizer import Quantizer
6459
from torch.export import export, ExportedProgram
6560
from torch.testing import FileCheck
6661
from torch.utils._pytree import tree_flatten
62+
from torchao.quantization.pt2e.quantize_pt2e import (
63+
convert_pt2e,
64+
prepare_pt2e,
65+
prepare_qat_pt2e,
66+
)
6767

6868

6969
class Stage(ABC):

docs/source/backends-arm-ethos-u.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ from executorch.exir import (
3333
ExecutorchBackendConfig,
3434
to_edge_transform_and_lower,
3535
)
36-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
36+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
3737
from torchvision.models import mobilenetv2
3838

3939
mobilenet_v2 = mobilenetv2.mobilenet_v2(
@@ -98,4 +98,4 @@ Finally, run the elf file on FVP using the script
9898
`executorch/backends/arm/scripts/run_fvp.sh --elf=executorch/mv2_arm_ethos_u55/cmake-out/arm_executor_runner --target=ethos-u55-128`.
9999

100100
## See Also
101-
- [Arm Ethos-U Backend Tutorial](tutorial-arm-ethos-u.md)
101+
- [Arm Ethos-U Backend Tutorial](tutorial-arm-ethos-u.md)

docs/source/backends-coreml.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ import torchvision.models as models
104104
from torchvision.models.mobilenetv2 import MobileNet_V2_Weights
105105
from executorch.backends.apple.coreml.quantizer import CoreMLQuantizer
106106
from executorch.backends.apple.coreml.partition import CoreMLPartitioner
107-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
107+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
108108
from executorch.exir import to_edge_transform_and_lower
109109
from executorch.backends.apple.coreml.compiler import CoreMLBackend
110110

docs/source/backends-xnnpack.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# XNNPACK Backend
22

3-
The XNNPACK delegate is the ExecuTorch solution for CPU execution on mobile CPUs. [XNNPACK](https://github.com/google/XNNPACK/tree/master) is a library that provides optimized kernels for machine learning operators on Arm and x86 CPUs.
3+
The XNNPACK delegate is the ExecuTorch solution for CPU execution on mobile CPUs. [XNNPACK](https://github.com/google/XNNPACK/tree/master) is a library that provides optimized kernels for machine learning operators on Arm and x86 CPUs.
44

55
## Features
66

@@ -18,7 +18,7 @@ The XNNPACK delegate is the ExecuTorch solution for CPU execution on mobile CPUs
1818

1919
## Development Requirements
2020

21-
The XNNPACK delegate does not introduce any development system requirements beyond those required by
21+
The XNNPACK delegate does not introduce any development system requirements beyond those required by
2222
the core ExecuTorch runtime.
2323

2424
----
@@ -63,7 +63,7 @@ After generating the XNNPACK-delegated .pte, the model can be tested from Python
6363

6464
## Quantization
6565

66-
The XNNPACK delegate can also be used as a backend to execute symmetrically quantized models. To quantize a PyTorch model for the XNNPACK backend, use the `XNNPACKQuantizer`. `Quantizers` are backend specific, which means the `XNNPACKQuantizer` is configured to quantize models to leverage the quantized operators offered by the XNNPACK Library.
66+
The XNNPACK delegate can also be used as a backend to execute symmetrically quantized models. To quantize a PyTorch model for the XNNPACK backend, use the `XNNPACKQuantizer`. `Quantizers` are backend specific, which means the `XNNPACKQuantizer` is configured to quantize models to leverage the quantized operators offered by the XNNPACK Library.
6767

6868
### Supported Quantization Schemes
6969
The XNNPACK delegate supports the following quantization schemes:
@@ -94,7 +94,7 @@ from torchvision.models.mobilenetv2 import MobileNet_V2_Weights
9494
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import XNNPACKQuantizer
9595
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
9696
from executorch.exir import to_edge_transform_and_lower
97-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
97+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
9898
from torch.ao.quantization.quantizer.xnnpack_quantizer import get_symmetric_quantization_config
9999

100100
model = models.mobilenetv2.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).eval()

docs/source/llm/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import (
619619
get_symmetric_quantization_config,
620620
XNNPACKQuantizer,
621621
)
622-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
622+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
623623
```
624624

625625
```python

docs/source/tutorial-xnnpack-delegate-lowering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ sample_inputs = (torch.randn(1, 3, 224, 224), )
8585

8686
mobilenet_v2 = export_for_training(mobilenet_v2, sample_inputs).module() # 2-stage export for quantization path
8787

88-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
88+
from torchao.quantization.pt2e.quantize_pt2e import convert_pt2e, prepare_pt2e
8989
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import (
9090
get_symmetric_quantization_config,
9191
XNNPACKQuantizer,

0 commit comments

Comments
 (0)