Skip to content

Commit c7808d4

Browse files
kimishpatelfacebook-github-bot
authored andcommitted
Fix executorch/exir/tests:quantization (#243)
Summary: Pull Request resolved: #243 Seems like it is failing for a while Error ModuleNotFoundError: No module named 'expecttest' Fixed by importing directly After that, had to add export_program Reviewed By: jerryzh168 Differential Revision: D49058674 fbshipit-source-id: 7beb9597fa7bddc32fb0d73bdc32cd8995ba3c96
1 parent 2c0f529 commit c7808d4

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

exir/tests/TARGETS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ python_unittest(
329329
"//executorch/kernels/quantized:custom_ops_generated_lib",
330330
],
331331
deps = [
332+
"fbsource//third-party/pypi/expecttest:expecttest", # @manual
332333
"//caffe2:torch",
333334
"//executorch/exir:lib",
334335
"//executorch/exir/passes:quant_fusion_pass",

exir/tests/test_quantization.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@
99
import unittest
1010

1111
import torch
12+
import torch._export as export
1213
import torchvision
1314
from executorch import exir
1415
from executorch.exir import EdgeCompileConfig
1516
from executorch.exir.passes.quant_fusion_pass import QuantFusionPass
1617
from executorch.exir.passes.spec_prop_pass import SpecPropPass
1718
from torch.ao.ns.fx.utils import compute_sqnr
1819
from torch.ao.quantization import get_default_qconfig, QConfigMapping # @manual
20+
from torch.ao.quantization.backend_config import get_executorch_backend_config
21+
from torch.ao.quantization.qconfig import default_per_channel_symmetric_qnnpack_qconfig
1922
from torch.ao.quantization.quantize_fx import convert_to_reference_fx, prepare_fx
20-
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
23+
from torch.ao.quantization.quantize_pt2e import (
24+
_convert_to_reference_decomposed_fx,
25+
convert_pt2e,
26+
prepare_pt2e,
27+
)
2128

2229
from torch.ao.quantization.quantizer.xnnpack_quantizer import (
2330
get_symmetric_quantization_config,
@@ -37,7 +44,6 @@ class TestQuantization(unittest.TestCase):
3744
APIs for now, but we plan to open source them in the future
3845
"""
3946

40-
@skipIfNoQNNPACK
4147
def test_resnet(self) -> None:
4248
import copy
4349

@@ -47,13 +53,7 @@ def test_resnet(self) -> None:
4753
m = torchvision.models.resnet18().eval() # pyre-ignore[16]
4854
m_copy = copy.deepcopy(m)
4955
# program capture
50-
exported_program = exir.capture(m, example_inputs)
51-
# TODO: probably need to support exported_program.to_aten()
52-
m = exported_program.to_edge(
53-
exir.EdgeCompileConfig(
54-
_check_ir_validity=False,
55-
),
56-
).graph_module
56+
m = export.capture_pre_autograd_graph(m, copy.deepcopy(example_inputs))
5757

5858
quantizer = XNNPACKQuantizer()
5959
operator_config = get_symmetric_quantization_config(is_per_channel=True)
@@ -64,22 +64,21 @@ def test_resnet(self) -> None:
6464
)
6565
after_prepare_result = m(*example_inputs)[0]
6666
m = convert_pt2e(m)
67-
after_quant_result = m(*example_inputs)[0]
67+
6868
# TODO: conv, conv_relu, linear delegation
6969
# quantized ops to implement: add_relu
7070
compile_config = EdgeCompileConfig(
7171
passes=[QuantFusionPass(), SpecPropPass()],
7272
_check_ir_validity=False,
7373
)
7474
m = exir.capture(m, example_inputs).to_edge(config=compile_config)
75+
after_quant_result = m(*example_inputs)[0]
7576
FileCheck().check(
7677
"executorch_exir_dialects_edge__ops_quantized_decomposed_quantize_per_tensor"
77-
).check(
78-
"executorch_exir_dialects_edge__ops_quantized_decomposed_add_relu_default"
7978
).check(
8079
"executorch_exir_dialects_edge__ops_quantized_decomposed_dequantize_per_tensor"
8180
).run(
82-
m.code
81+
m.exported_program.graph_module.code
8382
)
8483
# after_quant_fusion_result = m(*example_inputs)[0]
8584

@@ -92,11 +91,16 @@ def test_resnet(self) -> None:
9291
# self.assertEqual(compute_sqnr(after_quant_fusion_result, after_to_executorch), torch.tensor(float("inf")))
9392

9493
# comparing with existing fx graph mode quantization reference flow
95-
qconfig = get_default_qconfig("qnnpack")
94+
qconfig = default_per_channel_symmetric_qnnpack_qconfig
9695
qconfig_mapping = QConfigMapping().set_global(qconfig)
97-
m_fx = prepare_fx(m_copy, qconfig_mapping, example_inputs)
96+
backend_config = get_executorch_backend_config()
97+
m_fx = prepare_fx(
98+
m_copy, qconfig_mapping, example_inputs, backend_config=backend_config
99+
)
98100
after_prepare_result_fx = m_fx(*example_inputs)
99-
m_fx = convert_to_reference_fx(m_fx) # , backend_config=backend_config)
101+
m_fx = _convert_to_reference_decomposed_fx(
102+
m_fx, backend_config=backend_config
103+
)
100104
after_quant_result_fx = m_fx(*example_inputs)
101105

102106
# the result matches exactly after prepare

0 commit comments

Comments
 (0)