99import logging
1010import tempfile
1111
12+ import torch
13+
1214from executorch .backends .cadence .aot .ops_registrations import * # noqa
1315from typing import Any , Tuple
1416
1719 export_to_cadence_edge_executorch ,
1820 fuse_pt2 ,
1921)
22+
2023from executorch .backends .cadence .aot .quantizer .quantizer import CadenceQuantizer
2124from executorch .backends .cadence .runtime import runtime
2225from executorch .backends .cadence .runtime .executor import BundledProgramManager
2326from executorch .exir import ExecutorchProgramManager
2427from torch import nn
28+ from torch .ao .quantization .observer import HistogramObserver , MinMaxObserver
29+ from torch .ao .quantization .quantizer .xnnpack_quantizer_utils import (
30+ QuantizationConfig ,
31+ QuantizationSpec ,
32+ )
2533
2634from .utils import save_bpte_program , save_pte_program
2735
2836
2937FORMAT = "[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s"
3038logging .basicConfig (level = logging .INFO , format = FORMAT )
3139
40+ act_qspec = QuantizationSpec (
41+ dtype = torch .int8 ,
42+ quant_min = - 128 ,
43+ quant_max = 127 ,
44+ qscheme = torch .per_tensor_affine ,
45+ is_dynamic = False ,
46+ observer_or_fake_quant_ctr = HistogramObserver .with_args (eps = 2 ** - 12 ),
47+ )
48+
49+ wgt_qspec = QuantizationSpec (
50+ dtype = torch .int8 ,
51+ quant_min = - 128 ,
52+ quant_max = 127 ,
53+ qscheme = torch .per_tensor_affine ,
54+ is_dynamic = False ,
55+ observer_or_fake_quant_ctr = MinMaxObserver ,
56+ )
57+
3258
3359def export_model (
3460 model : nn .Module ,
@@ -39,8 +65,15 @@ def export_model(
3965 working_dir = tempfile .mkdtemp (dir = "/tmp" )
4066 logging .debug (f"Created work directory { working_dir } " )
4167
68+ qconfig = QuantizationConfig (
69+ act_qspec ,
70+ act_qspec ,
71+ wgt_qspec ,
72+ None ,
73+ )
74+
4275 # Instantiate the quantizer
43- quantizer = CadenceQuantizer ()
76+ quantizer = CadenceQuantizer (qconfig )
4477
4578 # Convert the model
4679 converted_model = convert_pt2 (model , example_inputs , quantizer )
0 commit comments