mct-nightly 2.3.0.20250420.609__py3-none-any.whl → 2.3.0.20250421.604__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mct-nightly
3
- Version: 2.3.0.20250420.609
3
+ Version: 2.3.0.20250421.604
4
4
  Summary: A Model Compression Toolkit for neural networks
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: License :: OSI Approved :: Apache Software License
@@ -1,6 +1,6 @@
1
- mct_nightly-2.3.0.20250420.609.dist-info/licenses/LICENSE.md,sha256=aYSSIb-5AFPeITTvXm1UAoe0uYBiMmSS8flvXaaFUks,10174
2
- model_compression_toolkit/__init__.py,sha256=D1sRRa_8IioYkA1BziKW6o2A7xhRrpl4XfEgAotkrFg,1557
3
- model_compression_toolkit/constants.py,sha256=2ltuH-gdaLZoZV4CPUgKjC3S9ojz2z4OTVdenyVEypU,3912
1
+ mct_nightly-2.3.0.20250421.604.dist-info/licenses/LICENSE.md,sha256=aYSSIb-5AFPeITTvXm1UAoe0uYBiMmSS8flvXaaFUks,10174
2
+ model_compression_toolkit/__init__.py,sha256=wnmK1gqXxy1bGqYedwsyNhvn4OUAZel25ytzES08fmk,1557
3
+ model_compression_toolkit/constants.py,sha256=iJ6vfTjC2oFIZWt8wvHoxEw5YJi3yl0Hd4q30_8q0Zc,3958
4
4
  model_compression_toolkit/defaultdict.py,sha256=LSc-sbZYXENMCw3U9F4GiXuv67IKpdn0Qm7Fr11jy-4,2277
5
5
  model_compression_toolkit/logger.py,sha256=L3q7tn3Uht0i_7phnlOWMR2Te2zvzrt2HOz9vYEInts,4529
6
6
  model_compression_toolkit/metadata.py,sha256=x_Bk4VpzILdsFax6--CZ3X18qUTP28sbF_AhoQW8dNc,4003
@@ -112,7 +112,7 @@ model_compression_toolkit/core/common/quantization/quantization_fn_selection.py,
112
112
  model_compression_toolkit/core/common/quantization/quantization_params_fn_selection.py,sha256=7eG7dl1TcbdnHwgmvyjarxLs0o6Lw_9VAjXAm4rsiBk,3791
113
113
  model_compression_toolkit/core/common/quantization/quantize_graph_weights.py,sha256=N005MSvx8UypVpa7XrxNrB2G732n2wHj3RmLyjTgd3I,2728
114
114
  model_compression_toolkit/core/common/quantization/quantize_node.py,sha256=cdzGNWfT4MRogIU8ehs0tr3lVjnzAI-jeoS9b4TwVBo,2854
115
- model_compression_toolkit/core/common/quantization/set_node_quantization_config.py,sha256=3jyOBaRFwoZQsiyB1nF7ayox1XSo6jf2fDc9V30wKkg,21431
115
+ model_compression_toolkit/core/common/quantization/set_node_quantization_config.py,sha256=IeBy1kh3Rdp_LFEd0K2Jc_XANDPYJQDYP9MYrpTE29k,29550
116
116
  model_compression_toolkit/core/common/quantization/quantization_params_generation/__init__.py,sha256=eCDGwsWYLU6z7qbEVb4TozMW_nd5VEP_iCJ6PcvyEPw,1486
117
117
  model_compression_toolkit/core/common/quantization/quantization_params_generation/error_functions.py,sha256=_m-XkEMJMHf0gYwVIXAoHVjdRa2NXt_gYdwBlw76ZR8,24031
118
118
  model_compression_toolkit/core/common/quantization/quantization_params_generation/lut_kmeans_params.py,sha256=RL-PklAjGyC-26anSt8fU07a6pB_LBQFQy9o4e9giN0,8739
@@ -528,7 +528,7 @@ model_compression_toolkit/xquant/pytorch/model_analyzer.py,sha256=b93o800yVB3Z-i
528
528
  model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py,sha256=UVN_S9ULHBEldBpShCOt8-soT8YTQ5oE362y96qF_FA,3950
529
529
  model_compression_toolkit/xquant/pytorch/similarity_functions.py,sha256=CERxq5K8rqaiE-DlwhZBTUd9x69dtYJlkHOPLB54vm8,2354
530
530
  model_compression_toolkit/xquant/pytorch/tensorboard_utils.py,sha256=mkoEktLFFHtEKzzFRn_jCnxjhJolK12TZ5AQeDHzUO8,9767
531
- mct_nightly-2.3.0.20250420.609.dist-info/METADATA,sha256=dTtpvCNUhT_VuyTM9niP_cILC_O7nIEw3stZRimbolo,25413
532
- mct_nightly-2.3.0.20250420.609.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
533
- mct_nightly-2.3.0.20250420.609.dist-info/top_level.txt,sha256=gsYA8juk0Z-ZmQRKULkb3JLGdOdz8jW_cMRjisn9ga4,26
534
- mct_nightly-2.3.0.20250420.609.dist-info/RECORD,,
531
+ mct_nightly-2.3.0.20250421.604.dist-info/METADATA,sha256=fRMmNKrtVjZdLZAaNnma2VUWCi47C4GsOq2HbD4Dyoc,25413
532
+ mct_nightly-2.3.0.20250421.604.dist-info/WHEEL,sha256=pxyMxgL8-pra_rKaQ4drOZAegBVuX-G_4nRHjjgWbmo,91
533
+ mct_nightly-2.3.0.20250421.604.dist-info/top_level.txt,sha256=gsYA8juk0Z-ZmQRKULkb3JLGdOdz8jW_cMRjisn9ga4,26
534
+ mct_nightly-2.3.0.20250421.604.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (78.1.1)
2
+ Generator: setuptools (79.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -27,4 +27,4 @@ from model_compression_toolkit import data_generation
27
27
  from model_compression_toolkit import pruning
28
28
  from model_compression_toolkit.trainable_infrastructure.keras.load_model import keras_load_quantized_model
29
29
 
30
- __version__ = "2.3.0.20250420.000609"
30
+ __version__ = "2.3.0.20250421.000604"
@@ -63,6 +63,8 @@ WEIGHTS_NBITS_ATTRIBUTE = 'weights_n_bits'
63
63
  CORRECTED_BIAS_ATTRIBUTE = 'corrected_bias'
64
64
  ACTIVATION_N_BITS_ATTRIBUTE = 'activation_n_bits'
65
65
  SUPPORTED_INPUT_ACTIVATION_NBITS_ATTRIBUTE = 'supported_input_activation_n_bits'
66
+ WEIGHTS = 'weights'
67
+ ACTIVATION = 'activation'
66
68
 
67
69
  # Quantization Parameters Iterative Search Defaults:
68
70
  SYMMETRIC_TENSOR_N_ITER = 40
@@ -12,12 +12,11 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
  # ==============================================================================
15
-
16
-
17
15
  import copy
18
- from typing import List, Tuple, Optional
16
+ from typing import List, Tuple, Dict, Optional
19
17
 
20
- from mct_quantizers.common.constants import ACTIVATION_N_BITS
18
+ from mct_quantizers.common.constants import WEIGHTS_N_BITS, ACTIVATION_N_BITS
19
+ from model_compression_toolkit.constants import WEIGHTS, ACTIVATION
21
20
  from model_compression_toolkit.core.common import BaseNode
22
21
  from model_compression_toolkit.core.common.quantization.bit_width_config import BitWidthConfig
23
22
  from model_compression_toolkit.logger import Logger
@@ -30,14 +29,13 @@ from model_compression_toolkit.core.common.quantization.node_quantization_config
30
29
  from model_compression_toolkit.core.common.quantization.quantization_config import QuantizationConfig, \
31
30
  QuantizationErrorMethod
32
31
  from model_compression_toolkit.core.common.quantization.quantization_params_fn_selection import \
33
- get_activation_quantization_params_fn, get_weights_quantization_params_fn
34
- from model_compression_toolkit.core.common.quantization.quantization_fn_selection import \
35
- get_weights_quantization_fn
32
+ get_activation_quantization_params_fn
36
33
  from model_compression_toolkit.target_platform_capabilities.schema.schema_functions import max_input_activation_n_bits
37
34
  from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import OpQuantizationConfig, \
38
35
  QuantizationConfigOptions
39
36
  from model_compression_toolkit.target_platform_capabilities.targetplatform2framework.framework_quantization_capabilities import \
40
37
  FrameworkQuantizationCapabilities
38
+ from model_compression_toolkit.core.common.graph.base_node import WeightAttrT
41
39
 
42
40
 
43
41
  def set_quantization_configuration_to_graph(graph: Graph,
@@ -66,16 +64,19 @@ def set_quantization_configuration_to_graph(graph: Graph,
66
64
  Logger.warning("Using the HMSE error method for weights quantization parameters search. "
67
65
  "Note: This method may significantly increase runtime during the parameter search process.")
68
66
 
69
- nodes_to_manipulate_bit_widths = {} if bit_width_config is None else bit_width_config.get_nodes_to_manipulate_activation_bit_widths(graph)
67
+ nodes_to_manipulate_activation_bit_widths = {} if bit_width_config is None else bit_width_config.get_nodes_to_manipulate_activation_bit_widths(graph)
68
+ nodes_to_manipulate_weights_bit_widths = {} if bit_width_config is None else bit_width_config.get_nodes_to_manipulate_weights_bit_widths(graph)
70
69
 
71
70
  for n in graph.nodes:
71
+ manual_bit_width_override = {ACTIVATION: nodes_to_manipulate_activation_bit_widths.get(n),
72
+ WEIGHTS: nodes_to_manipulate_weights_bit_widths.get(n)}
72
73
  set_quantization_configs_to_node(node=n,
73
74
  graph=graph,
74
75
  quant_config=quant_config,
75
76
  fw_info=graph.fw_info,
76
77
  fqc=graph.fqc,
77
78
  mixed_precision_enable=mixed_precision_enable,
78
- manual_bit_width_override=nodes_to_manipulate_bit_widths.get(n))
79
+ manual_bit_width_override=manual_bit_width_override)
79
80
  return graph
80
81
 
81
82
 
@@ -151,7 +152,7 @@ def set_quantization_configs_to_node(node: BaseNode,
151
152
  fw_info: FrameworkInfo,
152
153
  fqc: FrameworkQuantizationCapabilities,
153
154
  mixed_precision_enable: bool = False,
154
- manual_bit_width_override: Optional[int] = None):
155
+ manual_bit_width_override: Optional[Dict] = None):
155
156
  """
156
157
  Create and set quantization configurations to a node (for both weights and activation).
157
158
 
@@ -167,8 +168,11 @@ def set_quantization_configs_to_node(node: BaseNode,
167
168
  node_qc_options = node.get_qco(fqc)
168
169
  base_config, node_qc_options_list = filter_node_qco_by_graph(node, fqc, graph, node_qc_options)
169
170
 
170
- # If a manual_bit_width_override is given, filter node_qc_options_list to retain only the options with activation bits equal to manual_bit_width_override,
171
+ # If a manual_bit_width_override is given, filter node_qc_options_list to retain only the options with activation and weights bits equal to manual_bit_width_override,
171
172
  # and update base_config accordingly.
173
+ if manual_bit_width_override is None:
174
+ manual_bit_width_override = {ACTIVATION: None, WEIGHTS: None}
175
+
172
176
  base_config, node_qc_options_list = filter_qc_options_with_manual_bit_width(
173
177
  node=node,
174
178
  node_qc_options_list=node_qc_options_list,
@@ -322,7 +326,7 @@ def filter_qc_options_with_manual_bit_width(
322
326
  node: BaseNode,
323
327
  node_qc_options_list: List[OpQuantizationConfig],
324
328
  base_config: OpQuantizationConfig,
325
- manual_bit_width_override: Optional[int],
329
+ manual_bit_width_override: Optional[Dict],
326
330
  mixed_precision_enable: bool) -> Tuple[OpQuantizationConfig, List[OpQuantizationConfig]]:
327
331
  """
328
332
  Update the quantization configurations for a node, allowing manual bit-width overrides if specified.
@@ -331,29 +335,61 @@ def filter_qc_options_with_manual_bit_width(
331
335
  node (BaseNode): A node to set quantization configuration candidates to.
332
336
  node_qc_options_list (List[OpQuantizationConfig]): List of quantization configs for the node.
333
337
  base_config (OpQuantizationConfig): Base quantization config for the node.
334
- manual_bit_width_override (Optional[int]): Specifies a custom bit-width to override the node's activation bit-width.
338
+ manual_bit_width_override (Optional[Dict]): Specifies a custom bit-width to override the node's activation and weights bit-width.
335
339
  mixed_precision_enable (bool): Whether mixed precision is enabled.
336
340
 
337
341
  Returns:
338
342
  Tuple[OpQuantizationConfig, List[OpQuantizationConfig]]: The updated base configuration and the filtered list of quantization configs.
339
343
  """
340
- if manual_bit_width_override is None:
344
+ base_config, node_qc_options_list = filter_activation_qc_options_with_manual_bit_width(node,
345
+ node_qc_options_list,
346
+ base_config,
347
+ manual_bit_width_override.get(ACTIVATION),
348
+ mixed_precision_enable)
349
+
350
+ base_config, node_qc_options_list = filter_weights_qc_options_with_manual_bit_width(node,
351
+ node_qc_options_list,
352
+ base_config,
353
+ manual_bit_width_override.get(WEIGHTS),
354
+ mixed_precision_enable)
355
+ return base_config, node_qc_options_list
356
+
357
+
358
+ def filter_activation_qc_options_with_manual_bit_width(
359
+ node: BaseNode,
360
+ node_qc_options_list: List[OpQuantizationConfig],
361
+ base_config: OpQuantizationConfig,
362
+ activation_manual_bit_width_override: Optional[int],
363
+ mixed_precision_enable: bool) -> Tuple[OpQuantizationConfig, List[OpQuantizationConfig]]:
364
+ """
365
+ Update the activation quantization configurations for a node, allowing manual bit-width overrides if specified.
366
+
367
+ Args:
368
+ node (BaseNode): A node to set quantization configuration candidates to.
369
+ node_qc_options_list (List[OpQuantizationConfig]): List of quantization configs for the node.
370
+ base_config (OpQuantizationConfig): Base quantization config for the node.
371
+ activation_manual_bit_width_override (Optional[Dict]): Specifies a custom bit-width to override the node's activation bit-width.
372
+ mixed_precision_enable (bool): Whether mixed precision is enabled.
373
+
374
+ Returns:
375
+ Tuple[OpQuantizationConfig, List[OpQuantizationConfig]]: The updated base configuration and the filtered list of quantization configs.
376
+ """
377
+ if activation_manual_bit_width_override is None:
341
378
  return base_config, node_qc_options_list
342
379
 
343
- # Filter node_qc_options_list to retain only the options with activation bits equal to manual_bit_width_override.
380
+ # Filter node_qc_options_list to retain only the options with activation bits equal to activation_manual_bit_width_override.
344
381
  node_qc_options_list = [op_cfg for op_cfg in node_qc_options_list if
345
- manual_bit_width_override == op_cfg.activation_n_bits]
346
-
382
+ activation_manual_bit_width_override == op_cfg.activation_n_bits]
347
383
  if len(node_qc_options_list) == 0:
348
- Logger.critical(f"Manually selected activation bit-width {manual_bit_width_override} is invalid for node {node}.")
384
+ Logger.critical(f"Manually selected activation bit-width {activation_manual_bit_width_override} is invalid for node {node}.")
349
385
  else:
350
386
  # Update the base_config to one of the values from the filtered node_qc_options_list.
351
- # First, check if a configuration similar to the original base_config but with activation bits equal to manual_bit_width_override exists.
387
+ # First, check if a configuration similar to the original base_config but with activation bits equal to activation_manual_bit_width_override exists.
352
388
  # If it does, use it as the base_config. If not, choose a different configuration from node_qc_options_list.
353
- Logger.info(f"Setting node {node} bit-width to manually selected bit-width: {manual_bit_width_override} bits.")
354
- updated_base_config = base_config.clone_and_edit({ACTIVATION_N_BITS, manual_bit_width_override})
389
+ Logger.info(f"Setting node {node} bit-width to manually selected bit-width: {activation_manual_bit_width_override} bits.")
390
+ updated_base_config = base_config.clone_and_edit({ACTIVATION_N_BITS, activation_manual_bit_width_override})
355
391
  if updated_base_config in node_qc_options_list:
356
- # If a base_config with the specified manual_bit_width_override exists in the node_qc_options_list,
392
+ # If a base_config with the specified activation_manual_bit_width_override exists in the node_qc_options_list,
357
393
  # point the base_config to this option.
358
394
  base_config = node_qc_options_list[node_qc_options_list.index(updated_base_config)]
359
395
  else:
@@ -361,6 +397,107 @@ def filter_qc_options_with_manual_bit_width(
361
397
  base_config = node_qc_options_list[0]
362
398
  if len(node_qc_options_list) > 0 and not mixed_precision_enable:
363
399
  Logger.info(
364
- f"Request received to select {manual_bit_width_override} activation bits. However, the base configuration for layer type {node.type} is missing in the node_qc_options_list."
365
- f" Overriding base_config with an option that uses {manual_bit_width_override} bit activations.") # pragma: no cover
366
- return base_config, node_qc_options_list
400
+ f"Request received to select {activation_manual_bit_width_override} activation bits. However, the base configuration for layer type {node.type} is missing in the node_qc_options_list."
401
+ f" Overriding base_config with an option that uses {activation_manual_bit_width_override} bit activations.") # pragma: no cover
402
+
403
+ return base_config, node_qc_options_list
404
+
405
+
406
+ def filter_weights_qc_options_with_manual_bit_width(
407
+ node: BaseNode,
408
+ node_qc_options_list: List[OpQuantizationConfig],
409
+ base_config: OpQuantizationConfig,
410
+ weights_manual_bit_width_override: Optional[Tuple[int, WeightAttrT]],
411
+ mixed_precision_enable: bool) -> Tuple[OpQuantizationConfig, List[OpQuantizationConfig]]:
412
+ """
413
+ Update the weights quantization configurations for a node, allowing manual bit-width overrides if specified.
414
+
415
+ Args:
416
+ node (BaseNode): A node to set quantization configuration candidates to.
417
+ node_qc_options_list (List[OpQuantizationConfig]): List of quantization configs for the node.
418
+ base_config (OpQuantizationConfig): Base quantization config for the node.
419
+ weights_manual_bit_width_override (Optional[[int, WeightAttrT]]): Specifies a custom bit-width to override the node's weights bit-width.
420
+ mixed_precision_enable (bool): Whether mixed precision is enabled.
421
+
422
+ Returns:
423
+ Tuple[OpQuantizationConfig, List[OpQuantizationConfig]]: The updated base configuration and the filtered list of quantization configs.
424
+ """
425
+ if not weights_manual_bit_width_override:
426
+ return base_config, node_qc_options_list
427
+
428
+ # Filter node_qc_options_list to retain only the options with weights bits equal to weights_manual_bit_width_override.
429
+ node_qc_options_weights_list = _filter_options(node_qc_options_list, weights_manual_bit_width_override)
430
+
431
+ if len(node_qc_options_weights_list) == 0:
432
+ Logger.critical(f"Manually selected weights bit-width {weights_manual_bit_width_override} is invalid for node {node}.")
433
+ else:
434
+ # Update the base_config to one of the values from the filtered node_qc_options_list.
435
+ # First, check if a configuration similar to the original base_config but with weights bits equal to weights_manual_bit_width_override exists.
436
+ # If it does, use it as the base_config. If not, choose a different configuration from node_qc_options_list.
437
+ updated_base_config = base_config.clone_and_edit()
438
+
439
+ for bit_width, attr in weights_manual_bit_width_override:
440
+ Logger.info(f"Setting node {node} bit-width to manually selected {attr} bit-width: {bit_width} bits.")
441
+ updated_base_config = updated_base_config.clone_and_edit(attr_to_edit={attr : {WEIGHTS_N_BITS: bit_width}})
442
+
443
+ if updated_base_config in node_qc_options_weights_list:
444
+ # If a base_config with the specified weights_manual_bit_width_override exists in the node_qc_options_list,
445
+ # point the base_config to this option.
446
+ base_config = node_qc_options_weights_list[node_qc_options_weights_list.index(updated_base_config)]
447
+ else:
448
+ # Choose a different configuration from node_qc_options_list. If multiple options exist, issue a warning.
449
+ base_config = node_qc_options_weights_list[0]
450
+ if len(node_qc_options_weights_list) > 0 and not mixed_precision_enable:
451
+ Logger.info(
452
+ f"Request received to select weights bit-widths {weights_manual_bit_width_override}."
453
+ f"However, the base configuration for layer type {node.type} is missing in the node_qc_options_list."
454
+ f" Overriding base_config with an option that uses manually selected weights bit-widths {weights_manual_bit_width_override}.") # pragma: no cover
455
+
456
+ return base_config, node_qc_options_weights_list
457
+
458
+
459
+ def _is_valid_option(
460
+ op_cfg: OpQuantizationConfig,
461
+ attr: WeightAttrT,
462
+ bit_width: int) -> bool:
463
+ """
464
+ Judge whether the specified option is valid based on the specified attribute and bit width.
465
+
466
+ Args:
467
+ op_cfg (OpQuantizationConfig): The quantization configuration to be judged.
468
+ attr (WeightAttrT): The filtered node's attributes to apply bit-width manipulation to.
469
+ bit_width (int): The bit width to be applied to the selected nodes.
470
+
471
+ Returns:
472
+ Result to judge whether the specified option is valid based on the specified attribute and bit width
473
+ """
474
+ weights_attrs = op_cfg.attr_weights_configs_mapping.keys()
475
+
476
+ if attr not in weights_attrs:
477
+ return False
478
+
479
+ weights_n_bits = op_cfg.attr_weights_configs_mapping[attr].weights_n_bits
480
+ return weights_n_bits == bit_width
481
+
482
+
483
+ def _filter_options(
484
+ node_qc_options_list: List[OpQuantizationConfig],
485
+ weights_manual_bit_width_override: Tuple[int, WeightAttrT]) -> List[OpQuantizationConfig]:
486
+ """
487
+ Filter the options based on the specified bit width and attribute.
488
+
489
+ Args:
490
+ node_qc_options_list (List[OpQuantizationConfig]): List of quantization configs for the node.
491
+ weights_manual_bit_width_override (Tuple[int, WeightAttrT])): Specifies a custom bit-width to override the node's weights bit-width.
492
+
493
+ Returns:
494
+ List[OpQuantizationConfig]: Filtered the options based on the specified bit width and attribute.
495
+ """
496
+ filtered_options = []
497
+
498
+ for bit_width, attr in weights_manual_bit_width_override:
499
+ for op_cfg in node_qc_options_list:
500
+ if _is_valid_option(op_cfg, attr, bit_width):
501
+ filtered_options.append(op_cfg)
502
+
503
+ return filtered_options