Skip to content

Commit

Permalink
Move XNNPACKQuantizer from PyTorch to ExecuTorch (#7804)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #7804

X-link: pytorch/pytorch#144940

This migrates XNNPACKQuantizer from PyTorch to ExecuTorch.

Rationale:
Main motivation is to avoid pytorch pin update in OSS after updating XNNPACKQuantizer, which can be rather frequent.

Other impact and considerations:
- PT2e flow (which lives in PyTorch) relies havily on XNNPACKQuantizer for a "example" implementation for quantizer and more importantly tests. Fow now, we will keep the torch.ao.quantization.xnnpack_quantizer as is but mark is as not BC, and deprecated to discourace future new dependencies on it.

- Other OSS repository using XNNPACKQuantizer from PyTorch now have to take an additional dependency on ExecuTorch.

Reviewed By: mcr229

Differential Revision: D68191752
  • Loading branch information
digantdesai authored and facebook-github-bot committed Jan 22, 2025
1 parent 99912cd commit d6d19ce
Show file tree
Hide file tree
Showing 36 changed files with 3,919 additions and 73 deletions.
1 change: 1 addition & 0 deletions backends/cadence/aot/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ python_library(
"//executorch/backends/cadence/aot/quantizer:fusion_pass",
"//executorch/backends/cadence/runtime:runtime",
"//executorch/backends/cadence/aot/quantizer:quantizer",
"//executorch/backends/xnnpack/quantizer:xnnpack_quantizer",
"//executorch/backends/transforms:decompose_sdpa",
"//executorch/backends/transforms:remove_clone_ops",
"//executorch/exir:lib",
Expand Down
8 changes: 4 additions & 4 deletions backends/cadence/aot/export_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
from executorch.backends.cadence.aot.quantizer.quantizer import CadenceDefaultQuantizer
from executorch.backends.cadence.runtime import runtime
from executorch.backends.cadence.runtime.executor import BundledProgramManager
from executorch.exir import ExecutorchProgramManager
from torch import nn
from torch.ao.quantization.observer import HistogramObserver, MinMaxObserver
from torch.ao.quantization.quantizer.xnnpack_quantizer_utils import (
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer_utils import (
QuantizationConfig,
QuantizationSpec,
)
from executorch.exir import ExecutorchProgramManager
from torch import nn
from torch.ao.quantization.observer import HistogramObserver, MinMaxObserver

from .utils import save_bpte_program, save_pte_program

Expand Down
1 change: 1 addition & 0 deletions backends/cadence/aot/quantizer/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ python_library(
":patterns",
":utils",
"//caffe2:torch",
"//executorch/backends/xnnpack/quantizer:xnnpack_quantizer_utils",
],
)

Expand Down
12 changes: 6 additions & 6 deletions backends/cadence/aot/quantizer/quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
is_annotated,
no_outside_users,
)
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer_utils import (
OperatorConfig,
QuantizationAnnotation,
QuantizationConfig,
QuantizationSpec,
)

from torch import fx

from torch.ao.quantization.observer import HistogramObserver, MinMaxObserver
from torch.ao.quantization.quantizer import DerivedQuantizationSpec, Quantizer
from torch.ao.quantization.quantizer.composable_quantizer import ComposableQuantizer
from torch.ao.quantization.quantizer.xnnpack_quantizer_utils import (
OperatorConfig,
QuantizationAnnotation,
QuantizationConfig,
QuantizationSpec,
)


act_qspec = QuantizationSpec(
Expand Down
1 change: 1 addition & 0 deletions backends/example/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python_library(
deps = [
"//caffe2:torch",
"//executorch/backends/example/example_operators:example_operators_lib",
"//executorch/backends/xnnpack/quantizer:xnnpack_quantizer",
],
)

Expand Down
2 changes: 1 addition & 1 deletion backends/example/example_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

import torch
from executorch.backends.example.example_operators.ops import module_to_annotator
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer_utils import OperatorConfig
from torch import fx
from torch.ao.quantization.observer import HistogramObserver, MinMaxObserver
from torch.ao.quantization.pt2e.graph_utils import find_sequential_partitions
from torch.ao.quantization.quantizer import QuantizationSpec, Quantizer
from torch.ao.quantization.quantizer.xnnpack_quantizer_utils import OperatorConfig


def get_uint8_tensor_spec(observer_or_fake_quant_ctr):
Expand Down
1 change: 1 addition & 0 deletions backends/transforms/targets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def define_common_targets():
deps = [
"fbsource//third-party/pypi/expecttest:expecttest", # @manual
":duplicate_dynamic_quant_chain",
"//executorch/backends/xnnpack/quantizer:xnnpack_quantizer",
"//caffe2:torch",
"//executorch/exir:lib",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from executorch.backends.transforms.duplicate_dynamic_quant_chain import (
DuplicateDynamicQuantChainPass,
)
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
from torch.ao.quantization.quantizer.xnnpack_quantizer import (
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer import (
get_symmetric_quantization_config,
XNNPACKQuantizer,
)
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e

# TODO: Move away from using torch's internal testing utils
from torch.testing._internal.common_quantization import (
Expand Down
1 change: 1 addition & 0 deletions backends/vulkan/quantizer/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ python_library(
],
deps = [
"//caffe2:torch",
"//executorch/backends/xnnpack/quantizer:xnnpack_quantizer_utils",
],
)
8 changes: 4 additions & 4 deletions backends/vulkan/quantizer/vulkan_quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
from typing import Any, Callable, Dict, Optional

import torch
from torch.ao.quantization.observer import MinMaxObserver, PerChannelMinMaxObserver
from torch.ao.quantization.qconfig import _ObserverOrFakeQuantizeConstructor
from torch.ao.quantization.quantizer import QuantizationSpec, Quantizer
from torch.ao.quantization.quantizer.xnnpack_quantizer_utils import (
from executorch.backends.xnnpack.quantizer.xnnpack_quantizer_utils import (
_convert_scalars_to_attrs,
OP_TO_ANNOTATOR,
propagate_annotation,
QuantizationConfig,
)
from torch.ao.quantization.observer import MinMaxObserver, PerChannelMinMaxObserver
from torch.ao.quantization.qconfig import _ObserverOrFakeQuantizeConstructor
from torch.ao.quantization.quantizer import QuantizationSpec, Quantizer
from torch.fx import Node


Expand Down
20 changes: 20 additions & 0 deletions backends/xnnpack/quantizer/TARGETS
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@fbcode_macros//build_defs:python_library.bzl", "python_library")

python_library(
name = "xnnpack_quantizer",
srcs = ["xnnpack_quantizer.py"],
deps = [
":xnnpack_quantizer_utils",
"//caffe2:torch",
"//executorch/exir:lib",
],
)

python_library(
name = "xnnpack_quantizer_utils",
srcs = ["xnnpack_quantizer_utils.py"],
deps = [
"//caffe2:torch",
"//executorch/exir:lib",
],
)
Loading

0 comments on commit d6d19ce

Please sign in to comment.