Skip to content

Commit 8ccd0c6

Browse files
authored
Add quantize_and_export_to_edge and quantize_and_export_to_executorch
Differential Revision: D73397438 Pull Request resolved: #10379
1 parent fa89efa commit 8ccd0c6

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

backends/cadence/aot/compiler.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
from .utils import print_ops_info
4848

4949

50+
default_quantizer = CadenceDefaultQuantizer()
51+
52+
53+
# Note: this is not meant as a primary API since it can create inconsistencies
54+
# if the quantizer here is different from the quantizer used to convert. It is
55+
# however useful for unit tests to separate the converted model from the fused
56+
# model, to be able to get reference numerics.
57+
# If this does not apply, please use quantize_and_fuse_pt2 instead.
5058
def prepare_and_convert_pt2(
5159
model: torch.nn.Module,
5260
inputs: tuple[object, ...],
@@ -245,6 +253,28 @@ def export_to_edge(
245253
return edge_prog_manager
246254

247255

256+
def quantize_and_export_to_edge(
257+
model: torch.nn.Module,
258+
inputs: tuple[object, ...],
259+
quantizer: Optional[CadenceQuantizer] = None,
260+
dump_graphs: bool = False,
261+
constant_methods: Optional[dict[str, object]] = None,
262+
) -> EdgeProgramManager:
263+
quantized_model = quantize_pt2(
264+
model,
265+
inputs,
266+
quantizer=quantizer,
267+
dump_graphs=dump_graphs,
268+
)
269+
270+
return export_to_edge(
271+
quantized_model,
272+
inputs,
273+
dump_graphs=dump_graphs,
274+
constant_methods=constant_methods,
275+
)
276+
277+
248278
def export_to_cadence(
249279
model: torch.nn.Module,
250280
inputs: tuple[object, ...],

backends/cadence/aot/tests/test_fusion_ops_passes.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import executorch.backends.cadence.aot.ops_registrations # noqa
1313
import torch
1414
from executorch.backends.cadence.aot import compiler
15-
from executorch.backends.cadence.aot.compiler import export_to_edge, quantize_pt2
15+
from executorch.backends.cadence.aot.compiler import (
16+
export_to_edge,
17+
quantize_and_export_to_edge,
18+
)
1619
from executorch.backends.cadence.aot.fuse_ops import (
1720
FuseFullThenReshapePass,
1821
FuseMulIntoDequantPass,
@@ -414,9 +417,10 @@ def forward(self, x):
414417

415418
inputs = torch.randn(2, 12, 1, 6)
416419
model = M()
417-
quantized_model = quantize_pt2(model, (inputs,))
418420
graph_module = (
419-
export_to_edge(quantized_model, (inputs,)).exported_program().graph_module
421+
quantize_and_export_to_edge(model, (inputs,))
422+
.exported_program()
423+
.graph_module
420424
)
421425
graph_module = FuseQuantDequantToRequantizePass()(graph_module).graph_module
422426
self.check_op_counts(

backends/cadence/aot/tests/test_replace_ops_passes.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import torch
1414
import torch.nn.functional as F
1515
from executorch.backends.cadence.aot import compiler
16-
from executorch.backends.cadence.aot.compiler import export_to_edge, quantize_pt2
16+
from executorch.backends.cadence.aot.compiler import (
17+
export_to_edge,
18+
quantize_and_export_to_edge,
19+
)
1720
from executorch.backends.cadence.aot.graph_builder import (
1821
GraphBuilder,
1922
single_op_builder,
@@ -851,9 +854,8 @@ def test_replace_single_element_tensor_arguments_from_full_op_with_scalar(
851854

852855
inputs = (x,)
853856
model = torch.nn.Linear(in_features=in_features, out_features=out_features)
854-
quantized_model = quantize_pt2(model, inputs)
855857

856-
exported_program = export_to_edge(quantized_model, inputs).exported_program()
858+
exported_program = quantize_and_export_to_edge(model, inputs).exported_program()
857859

858860
# By default, the quantized linear op should have constant scalar attributes.
859861
self.assertTargetCountsEqual(
@@ -898,9 +900,8 @@ def test_replace_single_element_tensor_arguments_from_full_op_with_scalar_tuple_
898900

899901
inputs = (x,)
900902
model = torch.nn.Linear(in_features=in_features, out_features=out_features)
901-
quantized_model = quantize_pt2(model, inputs)
902903

903-
exported_program = export_to_edge(quantized_model, inputs).exported_program()
904+
exported_program = quantize_and_export_to_edge(model, inputs).exported_program()
904905

905906
# By default, the quantized linear op should have constant scalar attributes.
906907
self.assertTargetCountsEqual(

0 commit comments

Comments
 (0)