-
Notifications
You must be signed in to change notification settings - Fork 259
[Torch FX] Compress PT2E Support #3663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 29 commits
190f9d5
c52fcca
4e56cb5
9651ceb
14daeb5
3746815
0815dc5
1b8d940
7d35374
427ebc2
24dbfb6
4bb8c1a
8902842
3842538
2e70c2e
b1c9aad
33fe01c
d8e1006
88a8472
7a8e51a
fed5052
2866473
7171d56
3e3b067
5b7b210
71a479f
b24a59c
d12225a
9870ee2
8015629
0804218
1f1fda3
623ce46
d14a6eb
e91b455
448bf84
8e23572
36ddf53
07b730b
d5dd422
076a76b
2ce9eec
1bebf3e
ea81cfd
e82920f
82cc10b
beae508
8bd95df
aac9d3f
4278cfd
6fd5216
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright (c) 2025 Intel Corporation | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,100 @@ | ||||||
# Copyright (c) 2025 Intel Corporation | ||||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
# you may not use this file except in compliance with the License. | ||||||
# You may obtain a copy of the License at | ||||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||||
# Unless required by applicable law or agreed to in writing, software | ||||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
# See the License for the specific language governing permissions and | ||||||
# limitations under the License. | ||||||
|
||||||
import torch | ||||||
|
||||||
import nncf | ||||||
from nncf import SensitivityMetric | ||||||
from nncf.common.graph.graph import NNCFGraph | ||||||
from nncf.common.tensor_statistics.statistic_point import StatisticPointsContainer | ||||||
from nncf.common.utils.backend import BackendType | ||||||
from nncf.quantization.algorithms.algorithm import Algorithm | ||||||
from nncf.quantization.algorithms.weight_compression.algorithm import WeightCompression | ||||||
|
||||||
|
||||||
class WeightsCompressionPT2E(Algorithm): | ||||||
|
class WeightsCompressionPT2E(Algorithm): | |
class WeightCompression(Algorithm): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I rename it to ExperimentalWeightCompression
instead? since it could be confused with the original
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is inside the experimental directory, that should be descriptive enough. I suggest the WeightCompression
name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typehints an docstring are missing
daniil-lyakhov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
daniil-lyakhov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,9 +24,19 @@ class OpenVINOQuantizerAdapter(Quantizer): | |||||
|
||||||
def __init__(self, quantizer: OpenVINOQuantizer): | ||||||
self._quantizer = quantizer | ||||||
if hasattr(self._quantizer, "weight_compression_configuration"): | ||||||
self._weight_compression_configuration = self._quantizer.weight_compression_configuration | ||||||
|
||||||
def transform_prior_quantization(self, model: torch.fx.GraphModule) -> torch.fx.GraphModule: | ||||||
return self._quantizer.transform_for_annotation(model) | ||||||
|
||||||
def get_quantization_setup(self, model: torch.fx.GraphModule, nncf_graph: NNCFGraph) -> SingleConfigQuantizerSetup: | ||||||
return self._quantizer.get_nncf_quantization_setup(model, nncf_graph) | ||||||
|
||||||
def get_weight_compression_setup( | ||||||
|
def get_weight_compression_setup( | |
def get_weight_compression_params( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -769,10 +769,47 @@ def is_weight_compression_supported( | |
|
||
return is_supported_dtype and not no_bit_reduction | ||
|
||
def collect_weight_compression_statistics( | ||
self, | ||
model: TModel, | ||
graph: NNCFGraph, | ||
dataset: Dataset, | ||
weight_params: list[WeightCompressionParameters], | ||
statistic_points: Optional[StatisticPointsContainer] = None, | ||
) -> Optional[dict[str, Any]]: | ||
""" | ||
Collects statistics for weight compression if data-aware compression or | ||
mixed-precision is enabled. | ||
|
||
:param model: Backend-specific input model. | ||
:param graph: NNCFGraph instance. | ||
:param dataset: Dataset for statistics collection. | ||
:param weight_params: Weight parameters for which to collect statistics. | ||
:param statistic_points: Optional pre-collected statistic points. | ||
:return: A dictionary of collected statistics, or None if not applicable. | ||
""" | ||
statistics = None | ||
if not (self._data_aware_mixed_precision or self._data_aware_compression) or not dataset: | ||
return statistics, statistic_points | ||
matmul_nodes_to_compress = [ | ||
wp.node_with_weight | ||
for wp in weight_params | ||
if wp.node_with_weight.metatype in self._backend_entity.matmul_metatypes | ||
] | ||
matmul_input_to_output_nodes_map = self.get_matmul_input_to_output_nodes_map(matmul_nodes_to_compress, graph) | ||
|
||
if statistic_points is None: | ||
statistic_points = self.get_statistic_points(model, graph, matmul_input_to_output_nodes_map.keys()) | ||
statistic_points = self._collect_statistics(dataset, graph, model, statistic_points) | ||
|
||
statistics = self._get_statistics_for_weights_compression(matmul_input_to_output_nodes_map, statistic_points) | ||
return statistics, statistic_points | ||
|
||
def get_weight_compression_parameters( | ||
self, | ||
model: TModel, | ||
graph: NNCFGraph, | ||
nodes_to_compress: list[NNCFNode], | ||
statistic_points: Optional[StatisticPointsContainer] = None, | ||
dataset: Optional[Dataset] = None, | ||
) -> tuple[list[WeightCompressionParameters], Optional[dict[str, WCTensorStatistic]]]: | ||
|
@@ -791,8 +828,6 @@ def get_weight_compression_parameters( | |
Compression algorithm configuration, and a mapping of target node names to the | ||
collected statistics. | ||
""" | ||
nodes_to_compress = self.get_nodes_to_compress(graph) | ||
|
||
all_weight_params: list[WeightCompressionParameters] = [] | ||
skipped_weight_params: list[WeightCompressionParameters] = [] | ||
|
||
|
@@ -870,23 +905,10 @@ def get_weight_compression_parameters( | |
group_size_values = {w_params.weight_name: self._group_size for w_params in ratio_defining_params} | ||
|
||
# Collect statistics for the weights compression | ||
statistics = None | ||
if (self._data_aware_mixed_precision or self._data_aware_compression) and dataset: | ||
weight_params = ratio_defining_params if self._backup_mode == BackupMode.NONE else all_weight_params | ||
matmul_nodes_to_compress = [ | ||
wp.node_with_weight | ||
for wp in weight_params | ||
if wp.node_with_weight.metatype in self._backend_entity.matmul_metatypes | ||
] | ||
matmul_input_to_output_nodes_map = self.get_matmul_input_to_output_nodes_map( | ||
matmul_nodes_to_compress, graph | ||
) | ||
if statistic_points is None: | ||
statistic_points = self.get_statistic_points(model, graph, matmul_input_to_output_nodes_map.keys()) | ||
statistic_points = self._collect_statistics(dataset, graph, model, statistic_points) | ||
statistics = self._get_statistics_for_weights_compression( | ||
matmul_input_to_output_nodes_map, statistic_points | ||
) | ||
weight_params = ratio_defining_params if self._backup_mode == BackupMode.NONE else all_weight_params | ||
statistics, statistic_points = self.collect_weight_compression_statistics( | ||
model, graph, dataset, weight_params, statistic_points | ||
) | ||
|
||
# Set weight compression configuration | ||
self._set_weight_compression_config(ratio_defining_params, model, graph, statistic_points, group_size_values) | ||
|
@@ -901,18 +923,14 @@ def get_weight_compression_parameters( | |
|
||
return all_weight_params, statistics | ||
|
||
def apply( | ||
def apply_wc_algos( | ||
self, | ||
model: TModel, | ||
graph: NNCFGraph, | ||
statistic_points: Optional[StatisticPointsContainer] = None, | ||
all_weight_params: list[WeightCompressionParameters], | ||
statistics: dict[str, Any], | ||
dataset: Optional[Dataset] = None, | ||
) -> TModel: | ||
self.set_backend_entity(model) | ||
|
||
# Get processed weight compression parameters ready for compression | ||
all_weight_params, statistics = self.get_weight_compression_parameters(model, graph, statistic_points, dataset) | ||
|
||
if self._awq: | ||
model = self.awq_algo.apply(model, graph, all_weight_params, statistics, self._backend_entity) | ||
# After applying AWQ we need to update statistics since AWQ alters the activations | ||
|
@@ -967,7 +985,7 @@ def apply( | |
self._backend_entity.dump_parameters( | ||
model, | ||
parameters={ | ||
"mode": self._mode.value, | ||
"mode": self._mode.value if not isinstance(self._mode, str) else self._mode, | ||
|
||
"group_size": self._group_size, | ||
"ratio": self._ratio, | ||
"all_layers": self._all_layers, | ||
|
@@ -983,6 +1001,24 @@ def apply( | |
}, | ||
algo_name="weight_compression", | ||
) | ||
|
||
return transformed_model | ||
|
||
def apply( | ||
self, | ||
model: TModel, | ||
graph: NNCFGraph, | ||
statistic_points: Optional[StatisticPointsContainer] = None, | ||
dataset: Optional[Dataset] = None, | ||
) -> TModel: | ||
self.set_backend_entity(model) | ||
nodes_to_compress = self.get_nodes_to_compress(graph) | ||
# Get processed weight compression parameters ready for compression | ||
all_weight_params, statistics = self.get_weight_compression_parameters( | ||
model, graph, nodes_to_compress, statistic_points, dataset | ||
) | ||
transformed_model = self.apply_wc_algos(model, graph, all_weight_params, statistics, dataset) | ||
daniil-lyakhov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
return transformed_model | ||
|
||
def _get_activation_node_and_port(self, node: NNCFNode, nncf_graph: NNCFGraph) -> tuple[NNCFNode, int]: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, good catch! I will change it.