Skip to content

Commit

Permalink
Cadence - refactor quantizer and passes (#3539)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #3539

Update the quantizer to the latest internal version, and heavily refactor it for readability and maintenance.

Reviewed By: cccclai

Differential Revision: D57082021

fbshipit-source-id: 947753859f6ffc5ea9ea0e803d2c4a0fd30bf608
  • Loading branch information
mcremon-meta authored and facebook-github-bot committed May 13, 2024
1 parent 60c94e8 commit 629b112
Show file tree
Hide file tree
Showing 8 changed files with 1,106 additions and 859 deletions.
5 changes: 5 additions & 0 deletions backends/cadence/aot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
8 changes: 4 additions & 4 deletions backends/cadence/aot/export_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
from typing import Any, Tuple

from executorch.backends.cadence.aot.compiler import export_to_edge
from executorch.backends.cadence.aot.quantizer import (
CadenceBaseQuantizer,
QuantFusion,
from executorch.backends.cadence.aot.passes import (
ReplacePT2DequantWithCadenceDequant,
ReplacePT2QuantWithCadenceQuant,
)
from executorch.backends.cadence.aot.quantizer.fusion_pass import QuantFusion
from executorch.backends.cadence.aot.quantizer.quantizer import CadenceQuantizer
from executorch.exir import ExecutorchProgramManager
from torch import nn
from torch._export import capture_pre_autograd_graph
Expand Down Expand Up @@ -52,7 +52,7 @@ def export_model(
model: nn.Module, example_inputs: Tuple[Any], file_name: str = "CadenceDemoModel"
):
# Quantizer
quantizer = CadenceBaseQuantizer()
quantizer = CadenceQuantizer()

# Export
model_exp = capture_pre_autograd_graph(model, example_inputs)
Expand Down
42 changes: 42 additions & 0 deletions backends/cadence/aot/passes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from executorch.exir.dialects._ops import ops as exir_ops
from executorch.exir.pass_base import ExportPass


class ReplacePT2QuantWithCadenceQuant(ExportPass):
"""
Replace the pt2 quantization ops with custom cadence quantization ops.
"""

def call_operator(self, op, args, kwargs, meta):
if op not in {exir_ops.edge.quantized_decomposed.quantize_per_tensor.default}:
return super().call_operator(op, args, kwargs, meta)

return super().call_operator(
exir_ops.edge.cadence.quantize_per_tensor.default,
args,
kwargs,
meta,
)


class ReplacePT2DequantWithCadenceDequant(ExportPass):
"""
Replace the pt2 dequantization ops with custom cadence dequantization ops.
"""

def call_operator(self, op, args, kwargs, meta):
if op not in {exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default}:
return super().call_operator(op, args, kwargs, meta)

return super().call_operator(
exir_ops.edge.cadence.dequantize_per_tensor.default,
args,
kwargs,
meta,
)
Loading

0 comments on commit 629b112

Please sign in to comment.