Skip to content

Commit e3c63e5

Browse files
Revert "Revert "Merge branch 'feature_tpc_improvement' into feature_tpc_improvement""
This reverts commit 42998d6.
1 parent 42998d6 commit e3c63e5

File tree

11 files changed

+122
-253
lines changed

11 files changed

+122
-253
lines changed

model_compression_toolkit/verify_packages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
FOUND_TORCHVISION = importlib.util.find_spec("torchvision") is not None
3131
FOUND_ONNX = importlib.util.find_spec("onnx") is not None
3232
FOUND_ONNXRUNTIME = importlib.util.find_spec("onnxruntime") is not None
33-
FOUND_TPC = importlib.util.find_spec("edgemdt_tpc") is not None
33+

model_compression_toolkit/wrapper/constants.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@
1313
# limitations under the License.
1414
# ==============================================================================
1515

16+
1617
# TPC parameters
1718
FW_NAME = 'fw_name'
18-
TARGET_PLATFORM_NAME = 'target_platform_name'
19-
TARGET_PLATFORM_VERSION = 'target_platform_version'
20-
TPC_VERSION = 'tpc_version'
21-
DEVICE_TYPE = 'device_type'
22-
EXTENDED_VERSION = 'extended_version'
19+
SDSP_VERSION = 'sdsp_version'
2320

2421
# MixedPrecisionQuantizationConfig parameters
2522
NUM_OF_IMAGES = 'num_of_images'

model_compression_toolkit/wrapper/mct_wrapper.py

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
from typing import Dict, Any, List, Optional, Tuple
1717
import model_compression_toolkit as mct
1818
from model_compression_toolkit.logger import Logger
19-
from model_compression_toolkit.verify_packages import FOUND_TPC
2019
from model_compression_toolkit.wrapper.constants import (
21-
REPRESENTATIVE_DATA_GEN, CORE_CONFIG, FW_NAME, TARGET_PLATFORM_VERSION,
22-
TARGET_PLATFORM_NAME, TPC_VERSION, DEVICE_TYPE, EXTENDED_VERSION,
20+
REPRESENTATIVE_DATA_GEN, CORE_CONFIG, FW_NAME, SDSP_VERSION,
2321
NUM_OF_IMAGES, USE_HESSIAN_BASED_SCORES, IN_MODEL, IN_MODULE, MODEL,
2422
TARGET_PLATFORM_CAPABILITIES, TARGET_RESOURCE_UTILIZATION,
2523
ACTIVATION_ERROR_METHOD, WEIGHTS_ERROR_METHOD, WEIGHTS_BIAS_CORRECTION,
@@ -56,8 +54,6 @@ def __init__(self):
5654
:header: "Parameter Key", "Default Value", "Description"
5755
:widths: 30, 30, 40
5856
59-
"target_platform_version", "'v1'", "Target platform version (use_internal_tpc=True)"
60-
"tpc_version", "'5.0'", "TPC version (use_internal_tpc=False)"
6157
"activation_error_method", "mct.core.QuantizationErrorMethod.MSE", "Activation quantization error method"
6258
"weights_bias_correction", "True", "Enable weights bias correction"
6359
"z_threshold", "float('inf')", "Z-threshold for quantization"
@@ -71,8 +67,6 @@ def __init__(self):
7167
:header: "Parameter Key", "Default Value", "Description"
7268
:widths: 30, 30, 40
7369
74-
"target_platform_version", "'v1'", "Target platform version (use_internal_tpc=True)"
75-
"tpc_version", "'5.0'", "TPC version (use_internal_tpc=False)"
7670
"num_of_images", "5", "Number of images for mixed precision"
7771
"use_hessian_based_scores", "False", "Use Hessian-based scores for mixed precision"
7872
"weights_compression_ratio", "None", "Weights compression ratio for resource util"
@@ -84,8 +78,6 @@ def __init__(self):
8478
:header: "Parameter Key", "Default Value", "Description"
8579
:widths: 30, 30, 40
8680
87-
"target_platform_version", "'v1'", "Target platform version (use_internal_tpc=True)"
88-
"tpc_version", "'5.0'", "TPC version (use_internal_tpc=False)"
8981
"n_epochs", "5", "Number of training epochs for GPTQ"
9082
"optimizer", "None", "Optimizer for GPTQ training"
9183
"save_model_path", "'./qmodel.keras' / './qmodel.onnx'", "Path to save quantized model (Keras/Pytorch)"
@@ -96,8 +88,6 @@ def __init__(self):
9688
:header: "Parameter Key", "Default Value", "Description"
9789
:widths: 30, 30, 40
9890
99-
"target_platform_version", "'v1'", "Target platform version (use_internal_tpc=True)"
100-
"tpc_version", "'5.0'", "TPC version (use_internal_tpc=False)"
10191
"n_epochs", "5", "Number of training epochs for GPTQ"
10292
"optimizer", "None", "Optimizer for GPTQ training"
10393
"num_of_images", "5", "Number of images for mixed precision"
@@ -109,8 +99,7 @@ def __init__(self):
10999
self.params: Dict[str, Any] = {
110100
# TPC
111101
FW_NAME: 'pytorch',
112-
TARGET_PLATFORM_VERSION: 'v1',
113-
TPC_VERSION: '5.0',
102+
SDSP_VERSION: '3.14',
114103

115104
# QuantizationConfig
116105
ACTIVATION_ERROR_METHOD: mct.core.QuantizationErrorMethod.MSE,
@@ -140,9 +129,8 @@ def __init__(self):
140129

141130
def _initialize_and_validate(self, float_model: Any,
142131
representative_dataset: Optional[Any],
143-
method: str,
144132
framework: str,
145-
use_internal_tpc: bool,
133+
method: str,
146134
use_mixed_precision: bool
147135
) -> None:
148136
"""
@@ -151,9 +139,8 @@ def _initialize_and_validate(self, float_model: Any,
151139
Args:
152140
float_model: The float model to be quantized.
153141
representative_dataset (Callable, np.array, tf.Tensor): Representative dataset for calibration.
154-
method (str): Quantization method ('PTQ', 'GPTQ', 'LQPTQ').
155142
framework (str): Target framework ('tensorflow', 'pytorch').
156-
use_internal_tpc (bool): Whether to use MCT's built-in TPC.
143+
method (str): Quantization method ('PTQ', 'GPTQ', 'LQPTQ').
157144
use_mixed_precision (bool): Whether to use mixed-precision quantization.
158145
159146
Raises:
@@ -172,29 +159,25 @@ def _initialize_and_validate(self, float_model: Any,
172159
self.representative_dataset = representative_dataset
173160
self.method = method
174161
self.framework = framework
175-
self.use_internal_tpc = use_internal_tpc
176162
self.use_mixed_precision = use_mixed_precision
177163

178164
# Keep only the parameters you need for the quantization mode
179165
if method == 'PTQ':
180166
if not use_mixed_precision:
181-
allowed_keys = [ FW_NAME, TARGET_PLATFORM_VERSION, TPC_VERSION,
182-
ACTIVATION_ERROR_METHOD, WEIGHTS_BIAS_CORRECTION,
167+
allowed_keys = [ FW_NAME, SDSP_VERSION, ACTIVATION_ERROR_METHOD, WEIGHTS_BIAS_CORRECTION,
183168
Z_THRESHOLD, LINEAR_COLLAPSING, RESIDUAL_COLLAPSING,
184169
SAVE_MODEL_PATH ]
185170
else:
186-
allowed_keys = [ FW_NAME, TARGET_PLATFORM_VERSION, TPC_VERSION,
187-
NUM_OF_IMAGES, USE_HESSIAN_BASED_SCORES,
171+
allowed_keys = [ FW_NAME, SDSP_VERSION, NUM_OF_IMAGES, USE_HESSIAN_BASED_SCORES,
188172
WEIGHTS_COMPRESSION_RATIO, SAVE_MODEL_PATH ]
189173
else:
190174
if not use_mixed_precision:
191-
allowed_keys = [ FW_NAME, TARGET_PLATFORM_VERSION, TPC_VERSION,
192-
N_EPOCHS, OPTIMIZER, SAVE_MODEL_PATH ]
175+
allowed_keys = [FW_NAME, SDSP_VERSION, N_EPOCHS, OPTIMIZER,
176+
SAVE_MODEL_PATH]
193177
else:
194-
allowed_keys = [ FW_NAME, TARGET_PLATFORM_VERSION, TPC_VERSION,
195-
N_EPOCHS, OPTIMIZER, NUM_OF_IMAGES,
196-
USE_HESSIAN_BASED_SCORES, WEIGHTS_COMPRESSION_RATIO,
197-
SAVE_MODEL_PATH ]
178+
allowed_keys = [FW_NAME, SDSP_VERSION, N_EPOCHS, OPTIMIZER,
179+
NUM_OF_IMAGES, USE_HESSIAN_BASED_SCORES,
180+
WEIGHTS_COMPRESSION_RATIO, SAVE_MODEL_PATH]
198181

199182
self.params = { k: v for k, v in self.params.items() if k in allowed_keys }
200183

@@ -313,36 +296,18 @@ def _select_argname(self) -> None:
313296

314297
def _get_tpc(self) -> None:
315298
"""
316-
Configure Target Platform Capabilities (TPC) based on selected option.
299+
Configure Target Platform Capabilities (TPC).
317300
318-
Sets up either MCT's built-in TPC or external EdgeMDT TPC configuration
319-
for the IMX500 target platform.
301+
Sets up TPC configuration for the target platform.
320302
321303
Note:
322304
This method sets self.tpc attribute with the configured TPC object.
323305
"""
324-
if self.use_internal_tpc:
325-
# Use MCT's built-in TPC configuration
326-
params_TPC = {
327-
FW_NAME: self.params[FW_NAME],
328-
TARGET_PLATFORM_NAME: 'imx500',
329-
TARGET_PLATFORM_VERSION: self.params[TARGET_PLATFORM_VERSION],
330-
}
331-
# Get TPC from MCT framework
332-
self.tpc = mct.get_target_platform_capabilities(**params_TPC)
333-
else:
334-
if FOUND_TPC:
335-
import edgemdt_tpc
336-
# Use external EdgeMDT TPC configuration
337-
params_TPC = {
338-
TPC_VERSION: self.params[TPC_VERSION],
339-
DEVICE_TYPE: 'imx500',
340-
EXTENDED_VERSION: None
341-
}
342-
# Get TPC from EdgeMDT framework
343-
self.tpc = edgemdt_tpc.get_target_platform_capabilities(**params_TPC)
344-
else:
345-
raise Exception("EdgeMDT TPC module is not available.")
306+
# Get default TPC for the framework
307+
params_TPC = {
308+
SDSP_VERSION: self.params[SDSP_VERSION]
309+
}
310+
self.tpc = mct.get_target_platform_capabilities_sdsp(**params_TPC)
346311

347312
def _setting_PTQ_mixed_precision(self) -> Dict[str, Any]:
348313
"""
@@ -510,8 +475,6 @@ def _export_model(self, quantized_model: Any) -> None:
510475
params_export = {
511476
'model': quantized_model,
512477
'save_model_path': self.params['save_model_path'],
513-
'serialization_format': (mct.exporter.KerasExportSerializationFormat.KERAS),
514-
'quantization_format': (mct.exporter.QuantizationFormat.FAKELY_QUANT)
515478
}
516479
elif self.framework == 'pytorch':
517480
params_export = {
@@ -523,9 +486,8 @@ def _export_model(self, quantized_model: Any) -> None:
523486

524487
def quantize_and_export(self, float_model: Any,
525488
representative_dataset: Any,
526-
method: str = 'PTQ',
527489
framework: str = 'pytorch',
528-
use_internal_tpc: bool = True,
490+
method: str = 'PTQ',
529491
use_mixed_precision: bool = False,
530492
param_items: Optional[List[List[Any]]] = None
531493
) -> Tuple[bool, Any]:
@@ -536,12 +498,10 @@ def quantize_and_export(self, float_model: Any,
536498
float_model: The float model to be quantized.
537499
representative_dataset (Callable, np.array, tf.Tensor):
538500
Representative dataset for calibration.
539-
method (str): Quantization method, e.g., 'PTQ' or 'GPTQ'.
540-
Default: 'PTQ'
541501
framework (str): 'tensorflow' or 'pytorch'.
542502
Default: 'pytorch'
543-
use_internal_tpc (bool): Whether to use internal_tpc.
544-
Default: True
503+
method (str): Quantization method, e.g., 'PTQ' or 'GPTQ'.
504+
Default: 'PTQ'
545505
use_mixed_precision (bool): Whether to use mixed-precision
546506
quantization. Default: False
547507
param_items (list): List of parameter settings.
@@ -565,11 +525,10 @@ def quantize_and_export(self, float_model: Any,
565525
566526
>>> wrapper = mct.MCTWrapper()
567527
568-
set method, framework, and other parameters
528+
set framework, method, and other parameters
569529
570-
>>> method = 'PTQ'
571530
>>> framework = 'tensorflow'
572-
>>> use_internal_tpc = True
531+
>>> method = 'PTQ'
573532
>>> use_mixed_precision = False
574533
575534
set parameters if needed
@@ -581,19 +540,17 @@ def quantize_and_export(self, float_model: Any,
581540
>>> flag, quantized_model = wrapper.quantize_and_export(
582541
... float_model=float_model,
583542
... representative_dataset=representative_dataset,
584-
... method=method,
585543
... framework=framework,
586-
... use_internal_tpc=use_internal_tpc,
544+
... method=method,
587545
... use_mixed_precision=use_mixed_precision,
588546
... param_items=param_items
589547
... )
590548
591549
"""
592550
try:
593551
# Step 1: Initialize and validate all input parameters
594-
self._initialize_and_validate(
595-
float_model, representative_dataset, method, framework,
596-
use_internal_tpc, use_mixed_precision)
552+
self._initialize_and_validate( float_model, representative_dataset,
553+
framework, method, use_mixed_precision)
597554

598555
# Step 2: Apply custom parameter modifications
599556
self._modify_params(param_items)

tests_pytest/keras_tests/e2e_tests/wrapper/test_mct_wrapper_keras_e2e.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,13 @@ def PTQ_Keras(float_model: keras.Model) -> Tuple[bool, keras.Model]:
128128
predefined target platform capabilities for optimal quantization settings.
129129
"""
130130
# Quantization method configuration
131-
method = 'PTQ'
132131
framework = 'tensorflow'
133-
use_internal_tpc = True
132+
method = 'PTQ'
134133
use_mixed_precision = False
135134

136135
# Configure quantization parameters for optimal model performance
137136
param_items = [
138-
['target_platform_version', 'v1'], # The version of the TPC to use.
137+
['sdsp_version', '3.14'], # The version of the SDSP converter.
139138
['activation_error_method', QuantizationErrorMethod.MSE], # ErrorMethod.
140139
['weights_bias_correction', True], # Enable bias correction
141140
['z_threshold', float('inf')], # Z threshold
@@ -146,7 +145,7 @@ def PTQ_Keras(float_model: keras.Model) -> Tuple[bool, keras.Model]:
146145

147146
# Execute quantization using MCTWrapper
148147
wrapper = mct.wrapper.mct_wrapper.MCTWrapper()
149-
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, method, framework, use_internal_tpc, use_mixed_precision, param_items)
148+
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, framework, method, use_mixed_precision, param_items)
150149
return flag, quantized_model
151150

152151
#########################################################################
@@ -160,14 +159,13 @@ def PTQ_Keras_mixed_precision(float_model: keras.Model) -> Tuple[bool, keras.Mod
160159
optimizing the trade-off between model size and accuracy.
161160
"""
162161
# Quantization method configuration
163-
method = 'PTQ'
164162
framework = 'tensorflow'
165-
use_internal_tpc = True
163+
method = 'PTQ'
166164
use_mixed_precision = True
167165

168166
# Configure mixed precision parameters for optimal compression
169167
param_items = [
170-
['target_platform_version', 'v1'], # The version of the TPC to use.
168+
['sdsp_version', '3.14'], # The version of the SDSP converter.
171169
['num_of_images', 5], # Number of images
172170
['use_hessian_based_scores', False], # Use Hessian scores
173171
['weights_compression_ratio', 0.75], # Compression ratio
@@ -176,7 +174,7 @@ def PTQ_Keras_mixed_precision(float_model: keras.Model) -> Tuple[bool, keras.Mod
176174

177175
# Execute quantization with mixed precision using MCTWrapper
178176
wrapper = mct.wrapper.mct_wrapper.MCTWrapper()
179-
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, method, framework, use_internal_tpc, use_mixed_precision, param_items)
177+
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, framework, method, use_mixed_precision, param_items)
180178
return flag, quantized_model
181179

182180
#########################################################################
@@ -190,35 +188,33 @@ def GPTQ_Keras(float_model: keras.Model) -> Tuple[bool, keras.Model]:
190188
resulting in better model accuracy compared to standard PTQ.
191189
"""
192190
# Quantization method configuration
193-
method = 'GPTQ'
194191
framework = 'tensorflow'
195-
use_internal_tpc = True
192+
method = 'GPTQ'
196193
use_mixed_precision = False
197194

198195
# Configure GPTQ-specific parameters for gradient-based optimization
199196
param_items = [
200-
['target_platform_version', 'v1'], # The version of the TPC to use.
197+
['sdsp_version', '3.14'], # The version of the SDSP converter.
201198
['n_epochs', 5], # Number of training epochs
202199
['optimizer', None], # Optimizer for training
203200
['save_model_path', './qmodel_GPTQ_Keras.keras'] # Path to save the model.
204201
]
205202

206203
# Execute gradient-based quantization using MCTWrapper
207204
wrapper = mct.wrapper.mct_wrapper.MCTWrapper()
208-
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, method, framework, use_internal_tpc, use_mixed_precision, param_items)
205+
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, framework, method, use_mixed_precision, param_items)
209206
return flag, quantized_model
210207

211208
#########################################################################
212209
# Run GPTQ + Mixed Precision Quantization (mixed_precision) with Keras
213210
@decorator
214211
def GPTQ_Keras_mixed_precision(float_model: keras.Model) -> Tuple[bool, keras.Model]:
215-
method = 'GPTQ'
216212
framework = 'tensorflow'
217-
use_internal_tpc = True
213+
method = 'GPTQ'
218214
use_mixed_precision = True
219215

220216
param_items = [
221-
['target_platform_version', 'v1'], # The version of the TPC to use.
217+
['sdsp_version', '3.14'], # The version of the SDSP converter.
222218
['n_epochs', 5], # Number of training epochs
223219
['optimizer', None], # Optimizer for training
224220
['num_of_images', 5], # Number of images
@@ -228,16 +224,15 @@ def GPTQ_Keras_mixed_precision(float_model: keras.Model) -> Tuple[bool, keras.Mo
228224
]
229225

230226
wrapper = mct.wrapper.mct_wrapper.MCTWrapper()
231-
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, method, framework, use_internal_tpc, use_mixed_precision, param_items)
227+
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, framework, method, use_mixed_precision, param_items)
232228
return flag, quantized_model
233229

234230
#########################################################################
235231
# Run LQPTQ (Low-bit Quantizer PTQ) with Keras
236232
@decorator
237233
def LQPTQ_Keras(float_model: keras.Model) -> Tuple[bool, keras.Model]:
238-
method = 'LQPTQ'
239234
framework = 'tensorflow'
240-
use_internal_tpc = True
235+
method = 'LQPTQ'
241236
use_mixed_precision = False
242237

243238
param_items = [
@@ -246,8 +241,8 @@ def LQPTQ_Keras(float_model: keras.Model) -> Tuple[bool, keras.Model]:
246241
['save_model_path', './qmodel_LQPTQ_Keras.keras'] # Path to save the model.
247242
]
248243

249-
wrapper = mct.wrapper.wrap.MCTWrapper()
250-
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, method, framework, use_internal_tpc, use_mixed_precision, param_items)
244+
wrapper = mct.wrapper.mct_wrapper.MCTWrapper()
245+
flag, quantized_model = wrapper.quantize_and_export(float_model, representative_dataset_gen, framework, method, use_mixed_precision, param_items)
251246
return flag, quantized_model
252247

253248
# Execute the selected quantization method

0 commit comments

Comments
 (0)