44# This source code is licensed under the BSD-style license found in the
55# LICENSE file in the root directory of this source tree.
66
7- from typing import Any , List , Optional , Tuple
7+ from typing import Any , List , Optional , Sequence , Tuple
88
9+ import coremltools as ct
910import executorch
1011import executorch .backends .test .harness .stages as BaseStages
11-
12+ import functools
1213import torch
14+
15+ from executorch .backends .apple .coreml .compiler import CoreMLBackend
1316from executorch .backends .apple .coreml .partition import CoreMLPartitioner
17+ from executorch .backends .apple .coreml .quantizer import CoreMLQuantizer
1418from executorch .backends .test .harness import Tester as TesterBase
1519from executorch .backends .test .harness .stages import StageType
1620from executorch .exir import EdgeCompileConfig
1721from executorch .exir .backend .partitioner import Partitioner
1822
1923
24+ def _get_static_int8_qconfig ():
25+ return ct .optimize .torch .quantization .LinearQuantizerConfig (
26+ global_config = ct .optimize .torch .quantization .ModuleLinearQuantizerConfig (
27+ quantization_scheme = "symmetric" ,
28+ activation_dtype = torch .quint8 ,
29+ weight_dtype = torch .qint8 ,
30+ weight_per_channel = True ,
31+ )
32+ )
33+
34+
35+ class Quantize (BaseStages .Quantize ):
36+ def __init__ (
37+ self ,
38+ quantizer : Optional [CoreMLQuantizer ] = None ,
39+ quantization_config : Optional [Any ] = None ,
40+ calibrate : bool = True ,
41+ calibration_samples : Optional [Sequence [Any ]] = None ,
42+ is_qat : Optional [bool ] = False ,
43+ ):
44+ super ().__init__ (
45+ quantizer = quantizer or CoreMLQuantizer (quantization_config or _get_static_int8_qconfig ()),
46+ calibrate = calibrate ,
47+ calibration_samples = calibration_samples ,
48+ is_qat = is_qat ,
49+ )
50+
51+
52+
2053class Partition (BaseStages .Partition ):
21- def __init__ (self , partitioner : Optional [Partitioner ] = None ):
54+ def __init__ (
55+ self ,
56+ partitioner : Optional [Partitioner ] = None ,
57+ minimum_deployment_target : Optional [Any ] = ct .target .iOS15 ,
58+ ):
2259 super ().__init__ (
23- partitioner = partitioner or CoreMLPartitioner ,
60+ partitioner = partitioner or CoreMLPartitioner (
61+ compile_specs = CoreMLBackend .generate_compile_specs (
62+ minimum_deployment_target = minimum_deployment_target
63+ )
64+ ),
2465 )
2566
2667
@@ -29,9 +70,14 @@ def __init__(
2970 self ,
3071 partitioners : Optional [List [Partitioner ]] = None ,
3172 edge_compile_config : Optional [EdgeCompileConfig ] = None ,
73+ minimum_deployment_target : Optional [Any ] = ct .target .iOS15 ,
3274 ):
3375 super ().__init__ (
34- default_partitioner_cls = CoreMLPartitioner ,
76+ default_partitioner_cls = lambda : CoreMLPartitioner (
77+ compile_specs = CoreMLBackend .generate_compile_specs (
78+ minimum_deployment_target = minimum_deployment_target
79+ )
80+ ),
3581 partitioners = partitioners ,
3682 edge_compile_config = edge_compile_config ,
3783 )
@@ -43,13 +89,15 @@ def __init__(
4389 module : torch .nn .Module ,
4490 example_inputs : Tuple [torch .Tensor ],
4591 dynamic_shapes : Optional [Tuple [Any ]] = None ,
92+ minimum_deployment_target : Optional [Any ] = ct .target .iOS15 ,
4693 ):
4794 # Specialize for XNNPACK
4895 stage_classes = (
4996 executorch .backends .test .harness .Tester .default_stage_classes ()
5097 | {
51- StageType .PARTITION : Partition ,
52- StageType .TO_EDGE_TRANSFORM_AND_LOWER : ToEdgeTransformAndLower ,
98+ StageType .QUANTIZE : Quantize ,
99+ StageType .PARTITION : functools .partial (Partition , minimum_deployment_target = minimum_deployment_target ),
100+ StageType .TO_EDGE_TRANSFORM_AND_LOWER : functools .partial (ToEdgeTransformAndLower , minimum_deployment_target = minimum_deployment_target ),
53101 }
54102 )
55103
0 commit comments