mct-nightly 2.3.0.20250410.526__py3-none-any.whl → 2.3.0.20250411.529__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.20250410.526
3
+ Version: 2.3.0.20250411.529
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,5 +1,5 @@
1
- mct_nightly-2.3.0.20250410.526.dist-info/licenses/LICENSE.md,sha256=aYSSIb-5AFPeITTvXm1UAoe0uYBiMmSS8flvXaaFUks,10174
2
- model_compression_toolkit/__init__.py,sha256=hH7K5n9ZDkBMIcujI3umeAh3pUxoyxZu2pWu83zoGgk,1557
1
+ mct_nightly-2.3.0.20250411.529.dist-info/licenses/LICENSE.md,sha256=aYSSIb-5AFPeITTvXm1UAoe0uYBiMmSS8flvXaaFUks,10174
2
+ model_compression_toolkit/__init__.py,sha256=WhSK1dvJUbxfWbql3nwFOz2qB65dB1-38OZGodX3_Bc,1557
3
3
  model_compression_toolkit/constants.py,sha256=2ltuH-gdaLZoZV4CPUgKjC3S9ojz2z4OTVdenyVEypU,3912
4
4
  model_compression_toolkit/defaultdict.py,sha256=LSc-sbZYXENMCw3U9F4GiXuv67IKpdn0Qm7Fr11jy-4,2277
5
5
  model_compression_toolkit/logger.py,sha256=L3q7tn3Uht0i_7phnlOWMR2Te2zvzrt2HOz9vYEInts,4529
@@ -433,12 +433,13 @@ model_compression_toolkit/qat/pytorch/quantizer/ste_rounding/uniform_ste.py,sha2
433
433
  model_compression_toolkit/target_platform_capabilities/__init__.py,sha256=8RVOriZg-XNjSt53h_4Yum0oRgOe2gp5H45dfG_lZxE,1415
434
434
  model_compression_toolkit/target_platform_capabilities/constants.py,sha256=JRz9DoxLRpkqvu532TFkIvv0595Bfb9NtU4pRp4urDY,1540
435
435
  model_compression_toolkit/target_platform_capabilities/immutable.py,sha256=YhROBiXEIB3TU-bAFrnL3qbAsb1yuWPBAQ_CLOJbYUU,1827
436
- model_compression_toolkit/target_platform_capabilities/tpc_io_handler.py,sha256=4ydTWWKv_PEOAFok2JtxFNj8rav-0IlqcXKF6lnhHNE,4157
436
+ model_compression_toolkit/target_platform_capabilities/tpc_io_handler.py,sha256=-SGcHXGsNkQyasxsK7f4e05doHumJgyeHDJOGLdamE8,4615
437
437
  model_compression_toolkit/target_platform_capabilities/schema/__init__.py,sha256=pKAdbTCFM_2BrZXUtTIw0ouKotrWwUDF_hP3rPwCM2k,696
438
438
  model_compression_toolkit/target_platform_capabilities/schema/mct_current_schema.py,sha256=PvO8eHxnb3A55gyExT5fZGnOUl3ce7BbbT5SPxCEXNo,541
439
+ model_compression_toolkit/target_platform_capabilities/schema/schema_compatability.py,sha256=TtMPbiibV6Hk53nl5Y_ctfpI6mSbd8VVH9fxnv5j9eM,4430
439
440
  model_compression_toolkit/target_platform_capabilities/schema/schema_functions.py,sha256=vBkXxVJagm9JKB9cdm4Pvi7u_luriXUjvNn0-m8Zr0k,4653
440
441
  model_compression_toolkit/target_platform_capabilities/schema/v1.py,sha256=4CGpWENuOyjwaIMaGrFI0Act7jsSeT7m94pjrv91dxE,27516
441
- model_compression_toolkit/target_platform_capabilities/schema/v2.py,sha256=yg0ZrsaqaS69lmDvxRrz636CRARzx_eZbokTMVHNEXc,4555
442
+ model_compression_toolkit/target_platform_capabilities/schema/v2.py,sha256=cDNtJW1bVemStg07sDyVqmhHTUiQx66diq3-fqGPNek,4557
442
443
  model_compression_toolkit/target_platform_capabilities/targetplatform2framework/__init__.py,sha256=XjNws3zoiJkeH4ixKqrLA5xBvpv5rq31qX7wYQjNpZM,1447
443
444
  model_compression_toolkit/target_platform_capabilities/targetplatform2framework/attach2fw.py,sha256=HJ8uc3PFfyxg-WpVXPBg4mGaox8Z9bRqtQNbRfIyAk4,3745
444
445
  model_compression_toolkit/target_platform_capabilities/targetplatform2framework/attach2keras.py,sha256=Ehwpd_sL6zxmJFpJugOdN9uNxNX05nijvOCilNfHnFs,7162
@@ -527,7 +528,7 @@ model_compression_toolkit/xquant/pytorch/model_analyzer.py,sha256=b93o800yVB3Z-i
527
528
  model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py,sha256=UVN_S9ULHBEldBpShCOt8-soT8YTQ5oE362y96qF_FA,3950
528
529
  model_compression_toolkit/xquant/pytorch/similarity_functions.py,sha256=CERxq5K8rqaiE-DlwhZBTUd9x69dtYJlkHOPLB54vm8,2354
529
530
  model_compression_toolkit/xquant/pytorch/tensorboard_utils.py,sha256=mkoEktLFFHtEKzzFRn_jCnxjhJolK12TZ5AQeDHzUO8,9767
530
- mct_nightly-2.3.0.20250410.526.dist-info/METADATA,sha256=lnLhgKNdIybbtKdxLN61inSjhX0CQulfk_9gDUF387o,27148
531
- mct_nightly-2.3.0.20250410.526.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
532
- mct_nightly-2.3.0.20250410.526.dist-info/top_level.txt,sha256=gsYA8juk0Z-ZmQRKULkb3JLGdOdz8jW_cMRjisn9ga4,26
533
- mct_nightly-2.3.0.20250410.526.dist-info/RECORD,,
531
+ mct_nightly-2.3.0.20250411.529.dist-info/METADATA,sha256=GgXBoHZPtZctQu0fjlvWVpu1RRmhv4ooC7q_qeNgry8,27148
532
+ mct_nightly-2.3.0.20250411.529.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
533
+ mct_nightly-2.3.0.20250411.529.dist-info/top_level.txt,sha256=gsYA8juk0Z-ZmQRKULkb3JLGdOdz8jW_cMRjisn9ga4,26
534
+ mct_nightly-2.3.0.20250411.529.dist-info/RECORD,,
@@ -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.20250410.000526"
30
+ __version__ = "2.3.0.20250411.000529"
@@ -0,0 +1,84 @@
1
+ # Copyright 2025 Sony Semiconductor Israel, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+ from typing import Any, Union
16
+
17
+ import model_compression_toolkit.target_platform_capabilities.schema.v1 as schema_v1
18
+ import model_compression_toolkit.target_platform_capabilities.schema.v2 as schema_v2
19
+ import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as current_schema
20
+
21
+ ALL_SCHEMA_VERSIONS = [schema_v1] # needs to be updated with all active schema versions
22
+ FUTURE_SCHEMA_VERSIONS = [schema_v2] # once future schema becomes current schema, move to it ALL_SCHEMA_VERSIONS
23
+ all_tpc_types = tuple([s.TargetPlatformCapabilities for s in ALL_SCHEMA_VERSIONS])
24
+ tpc_or_str_type = all_tpc_types + (str,)
25
+
26
+
27
+ def is_tpc_instance(tpc_obj_or_path: Any) -> bool:
28
+ """
29
+ Checks if the given object is an instance of a TargetPlatformCapabilities
30
+ :param tpc_obj_or_path: Object to check its type
31
+ :return: True if the given object is an instance of a TargetPlatformCapabilities, False otherwise
32
+ """
33
+ return type(tpc_obj_or_path) in all_tpc_types
34
+
35
+
36
+ def _schema_v1_to_v2(tpc: schema_v1.TargetPlatformCapabilities) -> schema_v2.TargetPlatformCapabilities: # pragma: no cover
37
+ """
38
+ Converts given tpc of schema version 1 to schema version 2
39
+ :return: TargetPlatformCapabilities instance of of schema version 2
40
+ """
41
+ raise NotImplementedError("Once schema v2 is implemented, add necessary adaptations to _schema_v1_to_v2 function and remove 'pragma: no cover'")
42
+ return schema_v2.TargetPlatformCapabilities(default_qco=tpc.default_qco,
43
+ operator_set=tpc.operator_set,
44
+ fusing_patterns=tpc.fusing_patterns,
45
+ tpc_minor_version=tpc.tpc_minor_version,
46
+ tpc_patch_version=tpc.tpc_patch_version,
47
+ tpc_platform_type=tpc.tpc_platform_type,
48
+ add_metadata=tpc.add_metadata)
49
+
50
+ def get_conversion_map() -> dict:
51
+ """
52
+ Retrieves the schema conversion map.
53
+ :return: A dictionary where:
54
+ - Keys representing supported source schema versions.
55
+ - Values are Callable functions that take tpc in one schema version and return it in the next (higher) version
56
+ """
57
+ conversion_map = {
58
+ schema_v1.TargetPlatformCapabilities: _schema_v1_to_v2,
59
+ }
60
+ return conversion_map
61
+
62
+
63
+ def tpc_to_current_schema_version(tpc: Union[all_tpc_types]) -> current_schema.TargetPlatformCapabilities: # pragma: no cover
64
+ """
65
+ Given tpc instance of some schema version, convert it to the current MCT schema version.
66
+
67
+ In case a new schema is added to MCT, need to add a conversion function from the previous version to the new
68
+ version, e.g. if the current schema version was updated from v4 to v5, need to add _schema_v4_to_v5 function to
69
+ this file, and add it to the conversion_map.
70
+
71
+ :param tpc: TargetPlatformCapabilities of some schema version
72
+ :return: TargetPlatformCapabilities with the current MCT schema version
73
+ """
74
+ conversion_map = get_conversion_map()
75
+ prev_tpc_type = type(tpc)
76
+ while not isinstance(tpc, current_schema.TargetPlatformCapabilities):
77
+ if type(tpc) not in conversion_map:
78
+ raise KeyError(f"TPC using schema version {tpc.SCHEMA_VERSION} which is not in schemas conversion map. "
79
+ f"Make sure the schema version is supported, or add it in case it's a new schema version")
80
+ tpc = conversion_map[type(tpc)](tpc)
81
+ if isinstance(tpc, prev_tpc_type):
82
+ raise RuntimeError(f"TPC of type {prev_tpc_type} failed to update to next schema version")
83
+ prev_tpc_type = type(tpc)
84
+ return tpc
@@ -110,4 +110,5 @@ class TargetPlatformCapabilities(BaseModel):
110
110
  """
111
111
  Display the TargetPlatformCapabilities.
112
112
  """
113
- pprint.pprint(self.get_info(), sort_dicts=False)
113
+ pprint.pprint(self.get_info(), sort_dicts=False)
114
+
@@ -15,12 +15,39 @@
15
15
  from pathlib import Path
16
16
  from typing import Union
17
17
 
18
- from model_compression_toolkit.logger import Logger
19
- from model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema import TargetPlatformCapabilities
20
- import json
18
+ import model_compression_toolkit.target_platform_capabilities.schema.mct_current_schema as schema
19
+ from model_compression_toolkit.target_platform_capabilities.schema.schema_compatability import is_tpc_instance, \
20
+ tpc_to_current_schema_version, tpc_or_str_type
21
21
 
22
22
 
23
- def load_target_platform_capabilities(tpc_obj_or_path: Union[TargetPlatformCapabilities, str]) -> TargetPlatformCapabilities:
23
+ def _get_tpc_from_json(tpc_path: str) -> schema.TargetPlatformCapabilities:
24
+ """
25
+ Given a TPC json file path, parse it and returns a TargetPlatformCapabilities instance
26
+ :param tpc_path: json file path
27
+ :return: Parsed TargetPlatformCapabilities instance
28
+ """
29
+ path = Path(tpc_path)
30
+
31
+ if not path.exists() or not path.is_file():
32
+ raise FileNotFoundError(f"The path '{tpc_path}' is not a valid file.")
33
+ # Verify that the file has a .json extension
34
+ if path.suffix.lower() != '.json':
35
+ raise ValueError(f"The file '{path}' does not have a '.json' extension.")
36
+ try:
37
+ with path.open('r', encoding='utf-8') as file:
38
+ data = file.read()
39
+ except OSError as e:
40
+ raise ValueError(f"Error reading the file '{tpc_path}': {e.strerror}.") from e
41
+
42
+ try:
43
+ return schema.TargetPlatformCapabilities.parse_raw(data)
44
+ except ValueError as e:
45
+ raise ValueError(f"Invalid JSON for loading TargetPlatformCapabilities in '{tpc_path}': {e}.") from e
46
+ except Exception as e:
47
+ raise ValueError(f"Unexpected error while initializing TargetPlatformCapabilities: {e}.") from e
48
+
49
+
50
+ def load_target_platform_capabilities(tpc_obj_or_path: Union[tpc_or_str_type]) -> schema.TargetPlatformCapabilities:
24
51
  """
25
52
  Parses the tpc input, which can be either a TargetPlatformCapabilities object
26
53
  or a string path to a JSON file.
@@ -36,37 +63,22 @@ def load_target_platform_capabilities(tpc_obj_or_path: Union[TargetPlatformCapab
36
63
  ValueError: If the JSON content is invalid or cannot initialize the TargetPlatformCapabilities.
37
64
  TypeError: If the input is neither a TargetPlatformCapabilities nor a valid JSON file path.
38
65
  """
39
- if isinstance(tpc_obj_or_path, TargetPlatformCapabilities):
40
- return tpc_obj_or_path
41
-
42
- if isinstance(tpc_obj_or_path, str):
43
- path = Path(tpc_obj_or_path)
44
-
45
- if not path.exists() or not path.is_file():
46
- raise FileNotFoundError(f"The path '{tpc_obj_or_path}' is not a valid file.")
47
- # Verify that the file has a .json extension
48
- if path.suffix.lower() != '.json':
49
- raise ValueError(f"The file '{path}' does not have a '.json' extension.")
50
- try:
51
- with path.open('r', encoding='utf-8') as file:
52
- data = file.read()
53
- except OSError as e:
54
- raise ValueError(f"Error reading the file '{tpc_obj_or_path}': {e.strerror}.") from e
55
-
56
- try:
57
- return TargetPlatformCapabilities.parse_raw(data)
58
- except ValueError as e:
59
- raise ValueError(f"Invalid JSON for loading TargetPlatformCapabilities in '{tpc_obj_or_path}': {e}.") from e
60
- except Exception as e:
61
- raise ValueError(f"Unexpected error while initializing TargetPlatformCapabilities: {e}.") from e
62
-
63
- raise TypeError(
64
- f"tpc_obj_or_path must be either a TargetPlatformCapabilities instance or a string path to a JSON file, "
65
- f"but received type '{type(tpc_obj_or_path).__name__}'."
66
- )
67
-
68
-
69
- def export_target_platform_capabilities(model: TargetPlatformCapabilities, export_path: Union[str, Path]) -> None:
66
+ if is_tpc_instance(tpc_obj_or_path):
67
+ tpc = tpc_obj_or_path
68
+ elif isinstance(tpc_obj_or_path, str):
69
+ tpc = _get_tpc_from_json(tpc_obj_or_path)
70
+ else:
71
+ raise TypeError(
72
+ f"tpc_obj_or_path must be either a TargetPlatformCapabilities instance or a string path to a JSON file, "
73
+ f"but received type '{type(tpc_obj_or_path).__name__}'."
74
+ )
75
+
76
+ if isinstance(tpc, schema.TargetPlatformCapabilities): # if tpc is of current schema version
77
+ return tpc
78
+ return tpc_to_current_schema_version(tpc)
79
+
80
+
81
+ def export_target_platform_capabilities(model: schema.TargetPlatformCapabilities, export_path: Union[str, Path]) -> None:
70
82
  """
71
83
  Exports a TargetPlatformCapabilities instance to a JSON file.
72
84
 
@@ -78,7 +90,7 @@ def export_target_platform_capabilities(model: TargetPlatformCapabilities, expor
78
90
  ValueError: If the model is not an instance of TargetPlatformCapabilities.
79
91
  OSError: If there is an issue writing to the file.
80
92
  """
81
- if not isinstance(model, TargetPlatformCapabilities):
93
+ if not is_tpc_instance(model):
82
94
  raise ValueError("The provided model is not a valid TargetPlatformCapabilities instance.")
83
95
 
84
96
  path = Path(export_path)