mct-nightly 2.1.0.20240617.451__py3-none-any.whl → 2.1.0.20240619.429__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.
Files changed (38) hide show
  1. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240619.429.dist-info}/METADATA +2 -2
  2. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240619.429.dist-info}/RECORD +38 -12
  3. model_compression_toolkit/__init__.py +1 -1
  4. model_compression_toolkit/gptq/keras/gptq_training.py +1 -1
  5. model_compression_toolkit/gptq/keras/graph_info.py +1 -1
  6. model_compression_toolkit/gptq/pytorch/gptq_training.py +5 -2
  7. model_compression_toolkit/gptq/pytorch/graph_info.py +2 -1
  8. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3/tpc_pytorch.py +3 -2
  9. model_compression_toolkit/target_platform_capabilities/tpc_models/imx500_tpc/v3_lut/tpc_pytorch.py +3 -2
  10. model_compression_toolkit/xquant/__init__.py +19 -0
  11. model_compression_toolkit/xquant/common/__init__.py +15 -0
  12. model_compression_toolkit/xquant/common/constants.py +38 -0
  13. model_compression_toolkit/xquant/common/core_report_generator.py +83 -0
  14. model_compression_toolkit/xquant/common/dataset_utils.py +43 -0
  15. model_compression_toolkit/xquant/common/framework_report_utils.py +89 -0
  16. model_compression_toolkit/xquant/common/model_analyzer.py +99 -0
  17. model_compression_toolkit/xquant/common/model_folding_utils.py +104 -0
  18. model_compression_toolkit/xquant/common/similarity_calculator.py +194 -0
  19. model_compression_toolkit/xquant/common/similarity_functions.py +81 -0
  20. model_compression_toolkit/xquant/common/tensorboard_utils.py +101 -0
  21. model_compression_toolkit/xquant/common/xquant_config.py +39 -0
  22. model_compression_toolkit/xquant/keras/__init__.py +15 -0
  23. model_compression_toolkit/xquant/keras/dataset_utils.py +57 -0
  24. model_compression_toolkit/xquant/keras/facade_xquant_report.py +63 -0
  25. model_compression_toolkit/xquant/keras/keras_report_utils.py +60 -0
  26. model_compression_toolkit/xquant/keras/model_analyzer.py +136 -0
  27. model_compression_toolkit/xquant/keras/similarity_functions.py +75 -0
  28. model_compression_toolkit/xquant/keras/tensorboard_utils.py +84 -0
  29. model_compression_toolkit/xquant/pytorch/__init__.py +15 -0
  30. model_compression_toolkit/xquant/pytorch/dataset_utils.py +76 -0
  31. model_compression_toolkit/xquant/pytorch/facade_xquant_report.py +62 -0
  32. model_compression_toolkit/xquant/pytorch/model_analyzer.py +132 -0
  33. model_compression_toolkit/xquant/pytorch/pytorch_report_utils.py +61 -0
  34. model_compression_toolkit/xquant/pytorch/similarity_functions.py +68 -0
  35. model_compression_toolkit/xquant/pytorch/tensorboard_utils.py +87 -0
  36. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240619.429.dist-info}/LICENSE.md +0 -0
  37. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240619.429.dist-info}/WHEEL +0 -0
  38. {mct_nightly-2.1.0.20240617.451.dist-info → mct_nightly-2.1.0.20240619.429.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,61 @@
1
+ # Copyright 2024 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 model_compression_toolkit.core.pytorch.utils import get_working_device
16
+
17
+ from model_compression_toolkit.ptq.pytorch.quantization_facade import DEFAULT_PYTORCH_TPC
18
+ from model_compression_toolkit.xquant.common.framework_report_utils import FrameworkReportUtils
19
+ from model_compression_toolkit.core.pytorch.default_framework_info import DEFAULT_PYTORCH_INFO
20
+ from model_compression_toolkit.core.pytorch.pytorch_implementation import PytorchImplementation
21
+ from model_compression_toolkit.xquant.common.model_folding_utils import ModelFoldingUtils
22
+ from model_compression_toolkit.xquant.common.similarity_calculator import SimilarityCalculator
23
+ from model_compression_toolkit.xquant.pytorch.dataset_utils import PytorchDatasetUtils
24
+ from model_compression_toolkit.xquant.pytorch.model_analyzer import PytorchModelAnalyzer
25
+ from model_compression_toolkit.xquant.pytorch.similarity_functions import PytorchSimilarityFunctions
26
+ from model_compression_toolkit.xquant.pytorch.tensorboard_utils import PytorchTensorboardUtils
27
+
28
+
29
+ class PytorchReportUtils(FrameworkReportUtils):
30
+ """
31
+ Class with various utility components required for generating the report for a Pytorch model.
32
+ """
33
+ def __init__(self, report_dir: str):
34
+ """
35
+ Args:
36
+ report_dir: Logging dir path.
37
+ """
38
+ fw_info = DEFAULT_PYTORCH_INFO
39
+ fw_impl = PytorchImplementation()
40
+
41
+ dataset_utils = PytorchDatasetUtils()
42
+ model_folding = ModelFoldingUtils(fw_info=fw_info,
43
+ fw_impl=fw_impl,
44
+ fw_default_tpc=DEFAULT_PYTORCH_TPC)
45
+
46
+ similarity_calculator = SimilarityCalculator(dataset_utils=dataset_utils,
47
+ model_folding=model_folding,
48
+ similarity_functions=PytorchSimilarityFunctions(),
49
+ model_analyzer_utils=PytorchModelAnalyzer(),
50
+ device=get_working_device())
51
+
52
+ tb_utils = PytorchTensorboardUtils(report_dir=report_dir,
53
+ fw_impl=fw_impl,
54
+ fw_info=fw_info)
55
+
56
+ super().__init__(fw_info=fw_info,
57
+ fw_impl=fw_impl,
58
+ tb_utils=tb_utils,
59
+ dataset_utils=dataset_utils,
60
+ similarity_calculator=similarity_calculator,
61
+ model_folding_utils=model_folding)
@@ -0,0 +1,68 @@
1
+ # Copyright 2024 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
+
16
+
17
+ from model_compression_toolkit.xquant.common.similarity_functions import SimilarityFunctions
18
+ import torch
19
+
20
+ class PytorchSimilarityFunctions(SimilarityFunctions):
21
+
22
+ @staticmethod
23
+ def compute_mse(x: torch.Tensor, y: torch.Tensor) -> float:
24
+ """
25
+ Computes Mean Squared Error between between two tensors (usually, the float and quantized tensors).
26
+
27
+ Args:
28
+ x: Float model predictions.
29
+ y: Quantized model predictions.
30
+
31
+ Returns:
32
+ Mean Squared Error as a float.
33
+ """
34
+ mse = torch.nn.functional.mse_loss(x, y)
35
+ return mse.item()
36
+
37
+ @staticmethod
38
+ def compute_cs(x: torch.Tensor, y: torch.Tensor) -> float:
39
+ """
40
+ Computes Cosine Similarity between two tensors (usually, the float and quantized tensors).
41
+
42
+ Args:
43
+ x: Float model predictions.
44
+ y: Quantized model predictions.
45
+
46
+ Returns:
47
+ Cosine Similarity as a float.
48
+ """
49
+ cs = torch.nn.functional.cosine_similarity(x.flatten(), y.flatten(), dim=0)
50
+ return cs.item()
51
+
52
+ @staticmethod
53
+ def compute_sqnr(x: torch.Tensor, y: torch.Tensor) -> float:
54
+ """
55
+ Computes Signal-to-Quantization-Noise Ratio between two tensors (usually, the float and quantized tensors).
56
+
57
+ Args:
58
+ x: Float model predictions.
59
+ y: Quantized model predictions.
60
+
61
+ Returns:
62
+ Signal-to-Quantization-Noise Ratio as a float.
63
+ """
64
+ signal_power = torch.mean(x ** 2)
65
+ noise_power = torch.mean((x - y) ** 2)
66
+ sqnr = signal_power / noise_power
67
+ return sqnr.item()
68
+
@@ -0,0 +1,87 @@
1
+ # Copyright 2024 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
+
16
+ from model_compression_toolkit.core.common.framework_info import FrameworkInfo
17
+ from typing import Dict, Any, Callable
18
+
19
+ import torch
20
+
21
+ from model_compression_toolkit.core.common.framework_implementation import FrameworkImplementation
22
+ from model_compression_toolkit.core.pytorch.reader.reader import model_reader
23
+ from model_compression_toolkit.xquant.common.constants import XQUANT_REPR, INTERMEDIATE_SIMILARITY_METRICS_REPR, XQUANT_VAL, INTERMEDIATE_SIMILARITY_METRICS_VAL
24
+ from model_compression_toolkit.xquant.common.model_folding_utils import ModelFoldingUtils
25
+ from model_compression_toolkit.xquant.common.tensorboard_utils import TensorboardUtils
26
+
27
+ class PytorchTensorboardUtils(TensorboardUtils):
28
+ """
29
+ Utility class for handling PyTorch models with TensorBoard. Inherits from TensorboardUtils.
30
+ This class provides functionalities to display quantized model graphs on TensorBoard.
31
+ """
32
+
33
+ def __init__(self,
34
+ report_dir: str,
35
+ fw_info: FrameworkInfo,
36
+ fw_impl: FrameworkImplementation):
37
+ """
38
+ Initialize the PytorchTensorboardUtils instance.
39
+
40
+ Args:
41
+ report_dir: Directory where the reports are stored.
42
+ fw_info: Information about the framework being used.
43
+ fw_impl: Implementation methods for the framework.
44
+ """
45
+ super().__init__(report_dir,
46
+ fw_info,
47
+ fw_impl)
48
+
49
+ def get_graph_for_tensorboard_display(self,
50
+ quantized_model: torch.nn.Module,
51
+ similarity_metrics: Dict[str, Any],
52
+ repr_dataset: Callable):
53
+ """
54
+ Get the graph to display on TensorBoard. The graph represents the quantized model
55
+ with the similarity metrics that were measured.
56
+
57
+ Args:
58
+ quantized_model: The quantized model to be displayed on TensorBoard.
59
+ similarity_metrics: Dictionary containing the collected similarity metrics values.
60
+ repr_dataset: Callable that generates the representative dataset used during graph building.
61
+
62
+ Returns:
63
+ The updated quantized model graph with similarity metrics embedded.
64
+ """
65
+ # Read the model and generate a graph representation
66
+ quant_graph = model_reader(quantized_model,
67
+ representative_data_gen=repr_dataset,
68
+ to_tensor=self.fw_impl.to_tensor,
69
+ to_numpy=self.fw_impl.to_numpy)
70
+
71
+ # Iterate through each node in the graph
72
+ for node in quant_graph.nodes:
73
+ # Check and add similarity metrics for each node in the graph
74
+ if node.name in similarity_metrics[INTERMEDIATE_SIMILARITY_METRICS_REPR].keys():
75
+ node.framework_attr[XQUANT_REPR] = similarity_metrics[INTERMEDIATE_SIMILARITY_METRICS_REPR][f"{node.name}"]
76
+ elif node.name.removesuffix("_layer") in similarity_metrics[INTERMEDIATE_SIMILARITY_METRICS_REPR].keys():
77
+ node.framework_attr[XQUANT_REPR] = similarity_metrics[INTERMEDIATE_SIMILARITY_METRICS_REPR][
78
+ node.name.removesuffix("_layer")]
79
+
80
+ # Check and add validation similarity metrics for each node in the graph
81
+ if node.name in similarity_metrics[INTERMEDIATE_SIMILARITY_METRICS_VAL].keys():
82
+ node.framework_attr[XQUANT_VAL] = similarity_metrics[INTERMEDIATE_SIMILARITY_METRICS_VAL][f"{node.name}"]
83
+ elif node.name.removesuffix("_layer") in similarity_metrics[INTERMEDIATE_SIMILARITY_METRICS_VAL].keys():
84
+ node.framework_attr[XQUANT_VAL] = similarity_metrics[INTERMEDIATE_SIMILARITY_METRICS_VAL][
85
+ node.name.removesuffix("_layer")]
86
+
87
+ return quant_graph