|
17 | 17 | import torch.nn.utils.parametrize as P |
18 | 18 | from torch import nn |
19 | 19 |
|
20 | | -from coreai_opt._utils.export_utils import ( |
21 | | - COREML_SUPPORTED_ACTIVATION_DTYPES, |
22 | | - COREML_SUPPORTED_WEIGHT_DTYPES, |
23 | | -) |
| 20 | +from coreai_opt._utils.export_utils import validate_coreml_compatibility |
24 | 21 | from coreai_opt._utils.metadata_utils import CompressionType, MILCompressionMetadata |
25 | 22 | from coreai_opt._utils.torch_utils import ( |
26 | 23 | get_parent_module_and_attr_name as _get_parent_module_and_attr_name, |
27 | 24 | ) |
28 | | -from coreai_opt.common import CoreMLExportError |
29 | 25 | from coreai_opt.config.spec import CompressionTargetTensor |
30 | 26 | from coreai_opt.quantization._export_utils import ( |
31 | 27 | convert_dtype_for_torch_quantize as _convert_dtype_for_torch_quantize, |
|
35 | 31 | validate_qformulation_for_mil_export as _validate_qformulation_for_mil_export, |
36 | 32 | ) |
37 | 33 | from coreai_opt.quantization.spec.fake_quantize import FakeQuantizeImplBase |
38 | | -from coreai_opt.quantization.spec.granularity import PerBlockGranularity |
39 | 34 |
|
40 | 35 |
|
41 | 36 | def _process_weight_quantization( |
@@ -96,22 +91,16 @@ def _process_activation_quantization( |
96 | 91 | ) -> None: |
97 | 92 | """Process activation quantization by replacing with Sequential quantize/dequantize. |
98 | 93 |
|
99 | | - Supports both per-tensor and per-channel quantization based on the |
100 | | - granularity axis of the fake quantization module. |
| 94 | + By the time this runs, CoreML export compatibility has already been |
| 95 | + validated, so only per-tensor activation granularity ever reaches here. |
101 | 96 |
|
102 | 97 | Args: |
103 | 98 | parent_module: The parent module containing the fake quantizer |
104 | 99 | attr_name: The attribute name of the fake quantizer in the parent |
105 | 100 | fake_quant_mod: The fake quantization module to replace |
106 | 101 |
|
107 | | - Raises: |
108 | | - ValueError: If the granularity is per-block (not supported for MIL |
109 | | - activation export). |
110 | | -
|
111 | 102 | """ |
112 | 103 | _validate_qformulation_for_mil_export(fake_quant_mod) |
113 | | - if isinstance(fake_quant_mod.granularity, PerBlockGranularity): |
114 | | - raise ValueError("MIL export does not support per-block granularity for activations.") |
115 | 104 |
|
116 | 105 | scale, zero_point, _ = _extract_quantization_params(fake_quant_mod) |
117 | 106 | converted_dtype, converted_zero_point = _convert_dtype_for_torch_quantize( |
@@ -151,24 +140,24 @@ def prepare_for_mil_export(model: nn.Module) -> nn.Module: |
151 | 140 | """ |
152 | 141 | processed_fq_ids: set[int] = set() |
153 | 142 |
|
154 | | - # CoreML does not support certain dtypes (FP4, FP8, INT2, UINT2). Fail fast |
155 | | - # before mutating the model below. |
| 143 | + # Fail fast if model is not coreml-exportable |
156 | 144 | for module_name, module in model.named_modules(): |
157 | 145 | if P.is_parametrized(module): |
158 | 146 | for param_name, parametrizations in module.parametrizations.items(): |
159 | 147 | for p in parametrizations: |
160 | | - if ( |
161 | | - _is_module_fake_quant_target(p, CompressionTargetTensor.WEIGHT) |
162 | | - and p.dtype not in COREML_SUPPORTED_WEIGHT_DTYPES |
163 | | - ): |
164 | | - raise CoreMLExportError( |
165 | | - p.dtype, f"weight '{param_name}' of module '{module_name}'" |
| 148 | + if _is_module_fake_quant_target(p, CompressionTargetTensor.WEIGHT): |
| 149 | + validate_coreml_compatibility( |
| 150 | + CompressionTargetTensor.WEIGHT, |
| 151 | + p.dtype, |
| 152 | + f"weight '{param_name}' of module '{module_name}'", |
166 | 153 | ) |
167 | | - if ( |
168 | | - _is_module_fake_quant_target(module, CompressionTargetTensor.ACTIVATION) |
169 | | - and module.dtype not in COREML_SUPPORTED_ACTIVATION_DTYPES |
170 | | - ): |
171 | | - raise CoreMLExportError(module.dtype, f"activation quantizer of module '{module_name}'") |
| 154 | + if _is_module_fake_quant_target(module, CompressionTargetTensor.ACTIVATION): |
| 155 | + validate_coreml_compatibility( |
| 156 | + CompressionTargetTensor.ACTIVATION, |
| 157 | + module.dtype, |
| 158 | + f"activation quantizer of module '{module_name}'", |
| 159 | + module.granularity, |
| 160 | + ) |
172 | 161 |
|
173 | 162 | for name, module in list(model.named_modules()): |
174 | 163 | # Handle weight quantization parametrizations |
|
0 commit comments