mct-nightly 2.1.0.20240707.450__py3-none-any.whl → 2.1.0.20240709.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.
- {mct_nightly-2.1.0.20240707.450.dist-info → mct_nightly-2.1.0.20240709.429.dist-info}/METADATA +1 -1
- {mct_nightly-2.1.0.20240707.450.dist-info → mct_nightly-2.1.0.20240709.429.dist-info}/RECORD +31 -31
- model_compression_toolkit/__init__.py +1 -1
- model_compression_toolkit/core/common/framework_implementation.py +12 -12
- model_compression_toolkit/core/common/hessian/__init__.py +1 -1
- model_compression_toolkit/core/common/hessian/hessian_info_service.py +74 -69
- model_compression_toolkit/core/common/hessian/hessian_info_utils.py +1 -1
- model_compression_toolkit/core/common/hessian/{trace_hessian_calculator.py → hessian_scores_calculator.py} +11 -11
- model_compression_toolkit/core/common/hessian/{trace_hessian_request.py → hessian_scores_request.py} +15 -15
- model_compression_toolkit/core/common/mixed_precision/mixed_precision_search_facade.py +2 -2
- model_compression_toolkit/core/common/mixed_precision/sensitivity_evaluation.py +8 -8
- model_compression_toolkit/core/common/pruning/importance_metrics/lfh_importance_metric.py +5 -5
- model_compression_toolkit/core/common/quantization/quantization_params_generation/error_functions.py +4 -4
- model_compression_toolkit/core/common/quantization/quantization_params_generation/qparams_computation.py +5 -5
- model_compression_toolkit/core/keras/hessian/{activation_trace_hessian_calculator_keras.py → activation_hessian_scores_calculator_keras.py} +26 -26
- model_compression_toolkit/core/keras/hessian/{trace_hessian_calculator_keras.py → hessian_scores_calculator_keras.py} +14 -14
- model_compression_toolkit/core/keras/hessian/{weights_trace_hessian_calculator_keras.py → weights_hessian_scores_calculator_keras.py} +27 -27
- model_compression_toolkit/core/keras/keras_implementation.py +30 -30
- model_compression_toolkit/core/pytorch/hessian/{activation_trace_hessian_calculator_pytorch.py → activation_hessian_scores_calculator_pytorch.py} +25 -25
- model_compression_toolkit/core/pytorch/hessian/{trace_hessian_calculator_pytorch.py → hessian_scores_calculator_pytorch.py} +14 -14
- model_compression_toolkit/core/pytorch/hessian/{weights_trace_hessian_calculator_pytorch.py → weights_hessian_scores_calculator_pytorch.py} +25 -25
- model_compression_toolkit/core/pytorch/pytorch_implementation.py +30 -30
- model_compression_toolkit/core/quantization_prep_runner.py +1 -1
- model_compression_toolkit/gptq/common/gptq_training.py +30 -30
- model_compression_toolkit/gptq/keras/gptq_training.py +1 -1
- model_compression_toolkit/gptq/pytorch/gptq_training.py +1 -1
- model_compression_toolkit/gptq/runner.py +2 -2
- model_compression_toolkit/qat/pytorch/quantization_facade.py +1 -1
- {mct_nightly-2.1.0.20240707.450.dist-info → mct_nightly-2.1.0.20240709.429.dist-info}/LICENSE.md +0 -0
- {mct_nightly-2.1.0.20240707.450.dist-info → mct_nightly-2.1.0.20240709.429.dist-info}/WHEEL +0 -0
- {mct_nightly-2.1.0.20240707.450.dist-info → mct_nightly-2.1.0.20240709.429.dist-info}/top_level.txt +0 -0
@@ -21,11 +21,11 @@ from mct_quantizers import KerasQuantizationWrapper, KerasActivationQuantization
|
|
21
21
|
from tensorflow.keras.models import Model
|
22
22
|
|
23
23
|
from model_compression_toolkit.constants import HESSIAN_NUM_ITERATIONS
|
24
|
-
from model_compression_toolkit.core.common.hessian import
|
24
|
+
from model_compression_toolkit.core.common.hessian import HessianScoresRequest, HessianMode, HessianInfoService
|
25
25
|
from model_compression_toolkit.core.keras.graph_substitutions.substitutions.remove_identity import RemoveIdentity
|
26
|
-
from model_compression_toolkit.core.keras.hessian.
|
27
|
-
|
28
|
-
from model_compression_toolkit.core.keras.hessian.
|
26
|
+
from model_compression_toolkit.core.keras.hessian.activation_hessian_scores_calculator_keras import \
|
27
|
+
ActivationHessianScoresCalculatorKeras
|
28
|
+
from model_compression_toolkit.core.keras.hessian.weights_hessian_scores_calculator_keras import WeightsHessianScoresCalculatorKeras
|
29
29
|
from model_compression_toolkit.exporter.model_wrapper.fw_agnostic.get_inferable_quantizers import \
|
30
30
|
get_inferable_quantizers
|
31
31
|
from model_compression_toolkit.exporter.model_wrapper.keras.builder.node_to_quantizer import \
|
@@ -364,7 +364,7 @@ class KerasImplementation(FrameworkImplementation):
|
|
364
364
|
representative_data_gen: Dataset to use for retrieving images for the models inputs.
|
365
365
|
fw_info: FrameworkInfo object with information about the specific framework's model.
|
366
366
|
disable_activation_for_metric: Whether to disable activation quantization when computing the MP metric.
|
367
|
-
hessian_info_service:
|
367
|
+
hessian_info_service: HessianScoresService to fetch scores based on a Hessian-approximation for the float model.
|
368
368
|
|
369
369
|
Returns:
|
370
370
|
A SensitivityEvaluation object.
|
@@ -460,36 +460,36 @@ class KerasImplementation(FrameworkImplementation):
|
|
460
460
|
return compute_cs
|
461
461
|
return partial(compute_mse, norm=norm_mse)
|
462
462
|
|
463
|
-
def
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
463
|
+
def get_hessian_scores_calculator(self,
|
464
|
+
graph: Graph,
|
465
|
+
input_images: List[Any],
|
466
|
+
hessian_scores_request: HessianScoresRequest,
|
467
|
+
num_iterations_for_approximation: int = HESSIAN_NUM_ITERATIONS):
|
468
468
|
"""
|
469
|
-
Get Keras
|
469
|
+
Get Keras Hessian-approximation scores calculator based on the request.
|
470
470
|
Args:
|
471
471
|
input_images: Images to use for computation.
|
472
472
|
graph: Float graph to compute the approximation of its different nodes.
|
473
|
-
|
474
|
-
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian
|
475
|
-
|
476
|
-
Returns:
|
477
|
-
|
478
|
-
"""
|
479
|
-
if
|
480
|
-
return
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
elif
|
486
|
-
return
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
473
|
+
hessian_scores_request: HessianScoresRequest to search for the desired calculator.
|
474
|
+
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian scores.
|
475
|
+
|
476
|
+
Returns: HessianScoresCalculatorKeras to use for the Hessian-approximation scores computation for this request.
|
477
|
+
|
478
|
+
"""
|
479
|
+
if hessian_scores_request.mode == HessianMode.ACTIVATION:
|
480
|
+
return ActivationHessianScoresCalculatorKeras(graph=graph,
|
481
|
+
hessian_scores_request=hessian_scores_request,
|
482
|
+
input_images=input_images,
|
483
|
+
fw_impl=self,
|
484
|
+
num_iterations_for_approximation=num_iterations_for_approximation)
|
485
|
+
elif hessian_scores_request.mode == HessianMode.WEIGHTS:
|
486
|
+
return WeightsHessianScoresCalculatorKeras(graph=graph,
|
487
|
+
hessian_scores_request=hessian_scores_request,
|
488
|
+
input_images=input_images,
|
489
|
+
fw_impl=self,
|
490
|
+
num_iterations_for_approximation=num_iterations_for_approximation)
|
491
491
|
else:
|
492
|
-
Logger.critical(f"Unsupported Hessian mode for Keras: {
|
492
|
+
Logger.critical(f"Unsupported Hessian mode for Keras: {hessian_scores_request.mode}.") # pragma: no cover
|
493
493
|
|
494
494
|
def is_output_node_compatible_for_hessian_score_computation(self,
|
495
495
|
node: BaseNode) -> Any:
|
@@ -21,48 +21,48 @@ import numpy as np
|
|
21
21
|
|
22
22
|
from model_compression_toolkit.constants import MIN_HESSIAN_ITER, HESSIAN_COMP_TOLERANCE, HESSIAN_NUM_ITERATIONS
|
23
23
|
from model_compression_toolkit.core.common import Graph
|
24
|
-
from model_compression_toolkit.core.common.hessian import
|
24
|
+
from model_compression_toolkit.core.common.hessian import HessianScoresRequest, HessianScoresGranularity
|
25
25
|
from model_compression_toolkit.core.pytorch.back2framework.float_model_builder import FloatPyTorchModelBuilder
|
26
|
-
from model_compression_toolkit.core.pytorch.hessian.
|
27
|
-
|
26
|
+
from model_compression_toolkit.core.pytorch.hessian.hessian_scores_calculator_pytorch import \
|
27
|
+
HessianScoresCalculatorPytorch
|
28
28
|
from model_compression_toolkit.core.pytorch.utils import torch_tensor_to_numpy
|
29
29
|
from model_compression_toolkit.logger import Logger
|
30
30
|
import torch
|
31
31
|
|
32
32
|
|
33
|
-
class
|
33
|
+
class ActivationHessianScoresCalculatorPytorch(HessianScoresCalculatorPytorch):
|
34
34
|
"""
|
35
|
-
Pytorch implementation of the
|
35
|
+
Pytorch implementation of the Hessian approximation scores Calculator for activations.
|
36
36
|
"""
|
37
37
|
def __init__(self,
|
38
38
|
graph: Graph,
|
39
39
|
input_images: List[torch.Tensor],
|
40
40
|
fw_impl,
|
41
|
-
|
41
|
+
hessian_scores_request: HessianScoresRequest,
|
42
42
|
num_iterations_for_approximation: int = HESSIAN_NUM_ITERATIONS):
|
43
43
|
"""
|
44
44
|
Args:
|
45
45
|
graph: Computational graph for the float model.
|
46
46
|
input_images: List of input images for the computation.
|
47
|
-
fw_impl: Framework-specific implementation for
|
48
|
-
|
49
|
-
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian
|
47
|
+
fw_impl: Framework-specific implementation for Hessian approximation scores computation.
|
48
|
+
hessian_scores_request: Configuration request for which to compute the Hessian approximation scores.
|
49
|
+
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian scores.
|
50
50
|
|
51
51
|
"""
|
52
|
-
super(
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
super(ActivationHessianScoresCalculatorPytorch, self).__init__(graph=graph,
|
53
|
+
input_images=input_images,
|
54
|
+
fw_impl=fw_impl,
|
55
|
+
hessian_scores_request=hessian_scores_request,
|
56
|
+
num_iterations_for_approximation=num_iterations_for_approximation)
|
57
57
|
|
58
58
|
def compute(self) -> List[np.ndarray]:
|
59
59
|
"""
|
60
|
-
Compute the
|
60
|
+
Compute the scores that are based on the approximation of the Hessian w.r.t the requested target nodes' activations.
|
61
61
|
|
62
62
|
Returns:
|
63
|
-
List[np.ndarray]:
|
63
|
+
List[np.ndarray]: Scores based on the approximated Hessian for the requested nodes.
|
64
64
|
"""
|
65
|
-
if self.hessian_request.granularity ==
|
65
|
+
if self.hessian_request.granularity == HessianScoresGranularity.PER_TENSOR:
|
66
66
|
|
67
67
|
model_output_nodes = [ot.node for ot in self.graph.get_outputs()]
|
68
68
|
|
@@ -99,7 +99,7 @@ class ActivationTraceHessianCalculatorPytorch(TraceHessianCalculatorPytorch):
|
|
99
99
|
# First, we need to unfold all outputs that are given as list, to extract the actual output tensors
|
100
100
|
output = self.concat_tensors(output_tensors)
|
101
101
|
|
102
|
-
|
102
|
+
ipts_hessian_approx_scores = [torch.tensor([0.0],
|
103
103
|
requires_grad=True,
|
104
104
|
device=device)
|
105
105
|
for _ in range(len(target_activation_tensors))]
|
@@ -109,7 +109,7 @@ class ActivationTraceHessianCalculatorPytorch(TraceHessianCalculatorPytorch):
|
|
109
109
|
v = torch.randn(output.shape, device=device)
|
110
110
|
f_v = torch.sum(v * output)
|
111
111
|
for i, ipt_tensor in enumerate(target_activation_tensors): # Per Interest point activation tensor
|
112
|
-
# Computing the hessian
|
112
|
+
# Computing the hessian-approximation scores by getting the gradient of (output * v)
|
113
113
|
hess_v = autograd.grad(outputs=f_v,
|
114
114
|
inputs=ipt_tensor,
|
115
115
|
retain_graph=True,
|
@@ -121,24 +121,24 @@ class ActivationTraceHessianCalculatorPytorch(TraceHessianCalculatorPytorch):
|
|
121
121
|
continue # pragma: no cover
|
122
122
|
|
123
123
|
# Mean over all dims but the batch (CXHXW for conv)
|
124
|
-
|
124
|
+
hessian_approx_scores = torch.sum(hess_v ** 2.0, dim=tuple(d for d in range(1, len(hess_v.shape))))
|
125
125
|
|
126
126
|
# Update node Hessian approximation mean over random iterations
|
127
|
-
|
127
|
+
ipts_hessian_approx_scores[i] = (j * ipts_hessian_approx_scores[i] + hessian_approx_scores) / (j + 1)
|
128
128
|
|
129
129
|
# If the change to the maximal mean Hessian approximation is insignificant we stop the calculation
|
130
130
|
if j > MIN_HESSIAN_ITER:
|
131
131
|
if prev_mean_results is not None:
|
132
|
-
new_mean_res = torch.mean(torch.stack(
|
132
|
+
new_mean_res = torch.mean(torch.stack(ipts_hessian_approx_scores), dim=1)
|
133
133
|
relative_delta_per_node = (torch.abs(new_mean_res - prev_mean_results) /
|
134
134
|
(torch.abs(new_mean_res) + 1e-6))
|
135
135
|
max_delta = torch.max(relative_delta_per_node)
|
136
136
|
if max_delta < HESSIAN_COMP_TOLERANCE:
|
137
137
|
break
|
138
|
-
prev_mean_results = torch.mean(torch.stack(
|
138
|
+
prev_mean_results = torch.mean(torch.stack(ipts_hessian_approx_scores), dim=1)
|
139
139
|
|
140
140
|
# Convert results to list of numpy arrays
|
141
|
-
hessian_results = [torch_tensor_to_numpy(h) for h in
|
141
|
+
hessian_results = [torch_tensor_to_numpy(h) for h in ipts_hessian_approx_scores]
|
142
142
|
# Extend the Hessian tensors shape to align with expected return type
|
143
143
|
# TODO: currently, only per-tensor Hessian is available for activation.
|
144
144
|
# Once implementing per-channel or per-element, this alignment needs to be verified and handled separately.
|
@@ -147,6 +147,6 @@ class ActivationTraceHessianCalculatorPytorch(TraceHessianCalculatorPytorch):
|
|
147
147
|
return hessian_results
|
148
148
|
|
149
149
|
else: # pragma: no cover
|
150
|
-
Logger.critical(f"PyTorch activation Hessian's
|
150
|
+
Logger.critical(f"PyTorch activation Hessian's approximation scores does not support "
|
151
151
|
f"{self.hessian_request.granularity} granularity.")
|
152
152
|
|
@@ -17,38 +17,38 @@ from typing import Union, List
|
|
17
17
|
|
18
18
|
from model_compression_toolkit.constants import HESSIAN_NUM_ITERATIONS
|
19
19
|
from model_compression_toolkit.core.common import Graph
|
20
|
-
from model_compression_toolkit.core.common.hessian import
|
21
|
-
from model_compression_toolkit.core.common.hessian.
|
20
|
+
from model_compression_toolkit.core.common.hessian import HessianScoresRequest
|
21
|
+
from model_compression_toolkit.core.common.hessian.hessian_scores_calculator import HessianScoresCalculator
|
22
22
|
from model_compression_toolkit.logger import Logger
|
23
23
|
import torch
|
24
24
|
|
25
25
|
|
26
|
-
class
|
26
|
+
class HessianScoresCalculatorPytorch(HessianScoresCalculator):
|
27
27
|
"""
|
28
|
-
Pytorch-specific implementation of the
|
29
|
-
This class serves as a base for other Pytorch-specific
|
28
|
+
Pytorch-specific implementation of the Hessian approximation scores Calculator.
|
29
|
+
This class serves as a base for other Pytorch-specific Hessian approximation scores calculators.
|
30
30
|
"""
|
31
31
|
def __init__(self,
|
32
32
|
graph: Graph,
|
33
33
|
input_images: List[torch.Tensor],
|
34
34
|
fw_impl,
|
35
|
-
|
35
|
+
hessian_scores_request: HessianScoresRequest,
|
36
36
|
num_iterations_for_approximation: int = HESSIAN_NUM_ITERATIONS):
|
37
37
|
"""
|
38
38
|
|
39
39
|
Args:
|
40
40
|
graph: Computational graph for the float model.
|
41
41
|
input_images: List of input images for the computation.
|
42
|
-
fw_impl: Framework-specific implementation for
|
43
|
-
|
44
|
-
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian
|
42
|
+
fw_impl: Framework-specific implementation for Hessian scores computation.
|
43
|
+
hessian_scores_request: Configuration request for which to compute the Hessian approximation scores.
|
44
|
+
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian based scores.
|
45
45
|
|
46
46
|
"""
|
47
|
-
super(
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
super(HessianScoresCalculatorPytorch, self).__init__(graph=graph,
|
48
|
+
input_images=input_images,
|
49
|
+
fw_impl=fw_impl,
|
50
|
+
hessian_scores_request=hessian_scores_request,
|
51
|
+
num_iterations_for_approximation=num_iterations_for_approximation)
|
52
52
|
|
53
53
|
|
54
54
|
def concat_tensors(self, tensors_to_concate: Union[torch.Tensor, List[torch.Tensor]]) -> torch.Tensor:
|
@@ -18,41 +18,41 @@ import torch
|
|
18
18
|
from torch import autograd
|
19
19
|
import numpy as np
|
20
20
|
from model_compression_toolkit.core.common import Graph
|
21
|
-
from model_compression_toolkit.core.common.hessian import
|
22
|
-
from model_compression_toolkit.core.pytorch.hessian.
|
23
|
-
|
21
|
+
from model_compression_toolkit.core.common.hessian import HessianScoresRequest, HessianScoresGranularity
|
22
|
+
from model_compression_toolkit.core.pytorch.hessian.hessian_scores_calculator_pytorch import \
|
23
|
+
HessianScoresCalculatorPytorch
|
24
24
|
from model_compression_toolkit.logger import Logger
|
25
25
|
from model_compression_toolkit.core.pytorch.back2framework.float_model_builder import FloatPyTorchModelBuilder
|
26
26
|
from model_compression_toolkit.core.pytorch.default_framework_info import DEFAULT_PYTORCH_INFO
|
27
27
|
from model_compression_toolkit.constants import HESSIAN_NUM_ITERATIONS, MIN_HESSIAN_ITER, HESSIAN_COMP_TOLERANCE, HESSIAN_EPS
|
28
28
|
|
29
29
|
|
30
|
-
class
|
30
|
+
class WeightsHessianScoresCalculatorPytorch(HessianScoresCalculatorPytorch):
|
31
31
|
"""
|
32
|
-
Pytorch-specific implementation of the
|
32
|
+
Pytorch-specific implementation of the Hessian approximation scores computation w.r.t node's weights.
|
33
33
|
"""
|
34
34
|
|
35
35
|
def __init__(self,
|
36
36
|
graph: Graph,
|
37
37
|
input_images: List[torch.Tensor],
|
38
38
|
fw_impl,
|
39
|
-
|
39
|
+
hessian_scores_request: HessianScoresRequest,
|
40
40
|
num_iterations_for_approximation: int = HESSIAN_NUM_ITERATIONS):
|
41
41
|
"""
|
42
42
|
|
43
43
|
Args:
|
44
44
|
graph: Computational graph for the float model.
|
45
45
|
input_images: List of input images for the computation.
|
46
|
-
fw_impl: Framework-specific implementation for
|
47
|
-
|
48
|
-
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian
|
46
|
+
fw_impl: Framework-specific implementation for Hessian scores computation.
|
47
|
+
hessian_scores_request: Configuration request for which to compute the Hessian approximation scores.
|
48
|
+
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian scores.
|
49
49
|
"""
|
50
50
|
|
51
|
-
super(
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
super(WeightsHessianScoresCalculatorPytorch, self).__init__(graph=graph,
|
52
|
+
input_images=input_images,
|
53
|
+
fw_impl=fw_impl,
|
54
|
+
hessian_scores_request=hessian_scores_request,
|
55
|
+
num_iterations_for_approximation=num_iterations_for_approximation)
|
56
56
|
|
57
57
|
def compute(self) -> List[np.ndarray]:
|
58
58
|
"""
|
@@ -77,9 +77,9 @@ class WeightsTraceHessianCalculatorPytorch(TraceHessianCalculatorPytorch):
|
|
77
77
|
output_tensor = self.concat_tensors(outputs)
|
78
78
|
device = output_tensor.device
|
79
79
|
|
80
|
-
|
81
|
-
|
82
|
-
|
80
|
+
ipts_hessian_approx_scores = [torch.tensor([0.0],
|
81
|
+
requires_grad=True,
|
82
|
+
device=device)
|
83
83
|
for _ in range(len(self.hessian_request.target_nodes))]
|
84
84
|
|
85
85
|
prev_mean_results = None
|
@@ -107,9 +107,9 @@ class WeightsTraceHessianCalculatorPytorch(TraceHessianCalculatorPytorch):
|
|
107
107
|
# Get the output channel index
|
108
108
|
output_channel_axis, _ = DEFAULT_PYTORCH_INFO.kernel_channels_mapping.get(ipt_node.type)
|
109
109
|
shape_channel_axis = [i for i in range(len(weights_tensor.shape))]
|
110
|
-
if self.hessian_request.granularity ==
|
110
|
+
if self.hessian_request.granularity == HessianScoresGranularity.PER_OUTPUT_CHANNEL:
|
111
111
|
shape_channel_axis.remove(output_channel_axis)
|
112
|
-
elif self.hessian_request.granularity ==
|
112
|
+
elif self.hessian_request.granularity == HessianScoresGranularity.PER_ELEMENT:
|
113
113
|
shape_channel_axis = ()
|
114
114
|
|
115
115
|
# Compute gradients of f_v with respect to the weights
|
@@ -123,13 +123,13 @@ class WeightsTraceHessianCalculatorPytorch(TraceHessianCalculatorPytorch):
|
|
123
123
|
approx = torch.sum(approx, dim=shape_channel_axis)
|
124
124
|
|
125
125
|
# Update node Hessian approximation mean over random iterations
|
126
|
-
|
126
|
+
ipts_hessian_approx_scores[i] = (j * ipts_hessian_approx_scores[i] + approx) / (j + 1)
|
127
127
|
|
128
128
|
# If the change to the maximal mean Hessian approximation is insignificant we stop the calculation
|
129
129
|
# Note that we do not consider granularity when computing the mean
|
130
130
|
if j > MIN_HESSIAN_ITER:
|
131
131
|
if prev_mean_results is not None:
|
132
|
-
new_mean_res = torch.as_tensor([torch.mean(res) for res in
|
132
|
+
new_mean_res = torch.as_tensor([torch.mean(res) for res in ipts_hessian_approx_scores],
|
133
133
|
device=device)
|
134
134
|
relative_delta_per_node = (torch.abs(new_mean_res - prev_mean_results) /
|
135
135
|
(torch.abs(new_mean_res) + 1e-6))
|
@@ -137,16 +137,16 @@ class WeightsTraceHessianCalculatorPytorch(TraceHessianCalculatorPytorch):
|
|
137
137
|
if max_delta < HESSIAN_COMP_TOLERANCE:
|
138
138
|
break
|
139
139
|
|
140
|
-
prev_mean_results = torch.as_tensor([torch.mean(res) for res in
|
140
|
+
prev_mean_results = torch.as_tensor([torch.mean(res) for res in ipts_hessian_approx_scores], device=device)
|
141
141
|
|
142
142
|
# Make sure all final shape are tensors and not scalar
|
143
|
-
if self.hessian_request.granularity ==
|
144
|
-
|
143
|
+
if self.hessian_request.granularity == HessianScoresGranularity.PER_TENSOR:
|
144
|
+
ipts_hessian_approx_scores = [final_approx.reshape(1) for final_approx in ipts_hessian_approx_scores]
|
145
145
|
|
146
146
|
# Add a batch axis to the Hessian approximation tensor (to align with the expected returned shape).
|
147
147
|
# We assume per-image computation, so the batch axis size is 1.
|
148
148
|
final_approx = [r_final_approx[np.newaxis, ...].detach().cpu().numpy()
|
149
|
-
for r_final_approx in
|
149
|
+
for r_final_approx in ipts_hessian_approx_scores]
|
150
150
|
|
151
151
|
return final_approx
|
152
152
|
|
@@ -30,7 +30,7 @@ from model_compression_toolkit.core import QuantizationConfig, FrameworkInfo, Co
|
|
30
30
|
from model_compression_toolkit.core import common
|
31
31
|
from model_compression_toolkit.core.common import Graph, BaseNode
|
32
32
|
from model_compression_toolkit.core.common.framework_implementation import FrameworkImplementation
|
33
|
-
from model_compression_toolkit.core.common.hessian import
|
33
|
+
from model_compression_toolkit.core.common.hessian import HessianScoresRequest, HessianMode, HessianInfoService
|
34
34
|
from model_compression_toolkit.core.common.mixed_precision.sensitivity_evaluation import SensitivityEvaluation
|
35
35
|
from model_compression_toolkit.core.common.mixed_precision.set_layer_to_bitwidth import set_layer_to_bitwidth
|
36
36
|
from model_compression_toolkit.core.common.model_builder_mode import ModelBuilderMode
|
@@ -76,10 +76,10 @@ from model_compression_toolkit.core.pytorch.graph_substitutions.substitutions.we
|
|
76
76
|
WeightsActivationSplit
|
77
77
|
from model_compression_toolkit.core.pytorch.graph_substitutions.substitutions.concat_threshold_update import \
|
78
78
|
ConcatThresholdUpdate
|
79
|
-
from model_compression_toolkit.core.pytorch.hessian.
|
80
|
-
|
81
|
-
from model_compression_toolkit.core.pytorch.hessian.
|
82
|
-
|
79
|
+
from model_compression_toolkit.core.pytorch.hessian.activation_hessian_scores_calculator_pytorch import \
|
80
|
+
ActivationHessianScoresCalculatorPytorch
|
81
|
+
from model_compression_toolkit.core.pytorch.hessian.weights_hessian_scores_calculator_pytorch import \
|
82
|
+
WeightsHessianScoresCalculatorPytorch
|
83
83
|
from model_compression_toolkit.core.pytorch.mixed_precision.configurable_activation_quantizer import \
|
84
84
|
ConfigurableActivationQuantizer
|
85
85
|
from model_compression_toolkit.core.pytorch.mixed_precision.configurable_weights_quantizer import \
|
@@ -353,7 +353,7 @@ class PytorchImplementation(FrameworkImplementation):
|
|
353
353
|
representative_data_gen: Dataset to use for retrieving images for the models inputs.
|
354
354
|
fw_info: FrameworkInfo object with information about the specific framework's model.
|
355
355
|
disable_activation_for_metric: Whether to disable activation quantization when computing the MP metric.
|
356
|
-
hessian_info_service:
|
356
|
+
hessian_info_service: HessianScoresService to fetch approximations of the hessian scores for the float model.
|
357
357
|
|
358
358
|
Returns:
|
359
359
|
A SensitivityEvaluation object.
|
@@ -515,34 +515,34 @@ class PytorchImplementation(FrameworkImplementation):
|
|
515
515
|
|
516
516
|
return model(*inputs)
|
517
517
|
|
518
|
-
def
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
518
|
+
def get_hessian_scores_calculator(self,
|
519
|
+
graph: Graph,
|
520
|
+
input_images: List[Any],
|
521
|
+
hessian_scores_request: HessianScoresRequest,
|
522
|
+
num_iterations_for_approximation: int = HESSIAN_NUM_ITERATIONS):
|
523
523
|
"""
|
524
|
-
Get Pytorch
|
524
|
+
Get Pytorch hessian scores calculator based on the hessian scores request.
|
525
525
|
Args:
|
526
526
|
input_images: Images to use for computation.
|
527
527
|
graph: Float graph to compute the approximation of its different nodes.
|
528
|
-
|
529
|
-
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian
|
530
|
-
|
531
|
-
Returns:
|
532
|
-
|
533
|
-
"""
|
534
|
-
if
|
535
|
-
return
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
elif
|
541
|
-
return
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
528
|
+
hessian_scores_request: HessianScoresRequest to search for the desired calculator.
|
529
|
+
num_iterations_for_approximation: Number of iterations to use when approximating the Hessian scores.
|
530
|
+
|
531
|
+
Returns: HessianScoresCalculatorPytorch to use for the hessian approximation scores computation for this request.
|
532
|
+
|
533
|
+
"""
|
534
|
+
if hessian_scores_request.mode == HessianMode.ACTIVATION:
|
535
|
+
return ActivationHessianScoresCalculatorPytorch(graph=graph,
|
536
|
+
hessian_scores_request=hessian_scores_request,
|
537
|
+
input_images=input_images,
|
538
|
+
fw_impl=self,
|
539
|
+
num_iterations_for_approximation=num_iterations_for_approximation)
|
540
|
+
elif hessian_scores_request.mode == HessianMode.WEIGHTS:
|
541
|
+
return WeightsHessianScoresCalculatorPytorch(graph=graph,
|
542
|
+
hessian_scores_request=hessian_scores_request,
|
543
|
+
input_images=input_images,
|
544
|
+
fw_impl=self,
|
545
|
+
num_iterations_for_approximation=num_iterations_for_approximation)
|
546
546
|
|
547
547
|
def get_inferable_quantizers(self, node: BaseNode):
|
548
548
|
"""
|
@@ -40,7 +40,7 @@ def quantization_preparation_runner(graph: Graph,
|
|
40
40
|
fw_info: FrameworkInfo,
|
41
41
|
fw_impl: FrameworkImplementation,
|
42
42
|
tb_w: TensorboardWriter = None,
|
43
|
-
hessian_info_service: HessianInfoService = None,) -> Graph:
|
43
|
+
hessian_info_service: HessianInfoService = None, ) -> Graph:
|
44
44
|
"""
|
45
45
|
Prepares a trained model for post-training quantization.
|
46
46
|
First, the model graph is optimized using several transformations (e.g. folding BatchNormalization to preceding layers).
|