4
4
# This source code is licensed under the BSD-style license found in the
5
5
# LICENSE file in the root directory of this source tree.
6
6
7
- from typing import Any , List , Optional , Tuple
7
+ from typing import Any , List , Optional , Sequence , Tuple
8
8
9
+ import coremltools as ct
9
10
import executorch
10
11
import executorch .backends .test .harness .stages as BaseStages
11
-
12
+ import functools
12
13
import torch
14
+
15
+ from executorch .backends .apple .coreml .compiler import CoreMLBackend
13
16
from executorch .backends .apple .coreml .partition import CoreMLPartitioner
17
+ from executorch .backends .apple .coreml .quantizer import CoreMLQuantizer
14
18
from executorch .backends .test .harness import Tester as TesterBase
15
19
from executorch .backends .test .harness .stages import StageType
16
20
from executorch .exir import EdgeCompileConfig
17
21
from executorch .exir .backend .partitioner import Partitioner
18
22
19
23
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
+
20
53
class 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
+ ):
22
59
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
+ ),
24
65
)
25
66
26
67
@@ -29,9 +70,14 @@ def __init__(
29
70
self ,
30
71
partitioners : Optional [List [Partitioner ]] = None ,
31
72
edge_compile_config : Optional [EdgeCompileConfig ] = None ,
73
+ minimum_deployment_target : Optional [Any ] = ct .target .iOS15 ,
32
74
):
33
75
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
+ ),
35
81
partitioners = partitioners ,
36
82
edge_compile_config = edge_compile_config ,
37
83
)
@@ -43,13 +89,15 @@ def __init__(
43
89
module : torch .nn .Module ,
44
90
example_inputs : Tuple [torch .Tensor ],
45
91
dynamic_shapes : Optional [Tuple [Any ]] = None ,
92
+ minimum_deployment_target : Optional [Any ] = ct .target .iOS15 ,
46
93
):
47
94
# Specialize for XNNPACK
48
95
stage_classes = (
49
96
executorch .backends .test .harness .Tester .default_stage_classes ()
50
97
| {
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 ),
53
101
}
54
102
)
55
103
0 commit comments