Skip to content

Commit

Permalink
Collect statistics from subset in weight compression
Browse files Browse the repository at this point in the history
  • Loading branch information
ljaljushkin committed Nov 6, 2024
1 parent 34cb441 commit bb51646
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
8 changes: 4 additions & 4 deletions nncf/quantization/algorithms/weight_compression/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,9 @@ def apply(
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.get_statistic_points(
model, graph, matmul_input_to_output_nodes_map.keys(), self._subset_size
)
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
Expand Down Expand Up @@ -759,15 +761,13 @@ def get_statistic_points(
model: TModel,
graph: NNCFGraph,
nodes_and_port_ids: Iterable[Tuple[NNCFNode, int]],
subset_size: Optional[int] = None,
) -> StatisticPointsContainer:
"""
Returns statistic points, for which StatisticsCollector should collect statistics.
:param model: Model for statistics collection.
:param graph: Model graph.
:param nodes_and_port_ids: Nodes and port ids for which statistics should be collected.
:param subset_size: Number of samples to collect.
:return: Statistic points, for which StatisticsCollector should collect statistics.
"""
statistic_container = StatisticPointsContainer()
Expand All @@ -781,7 +781,7 @@ def get_statistic_points(
# size dimension.
n_dims = len(graph.get_output_edges_by_port_id(node, output_port_id)[0].tensor_shape)
stat_collector = self._backend_entity.mean_statistic_collector(
reduction_axes=tuple(range(n_dims - 1)), subset_size=subset_size
reduction_axes=tuple(range(n_dims - 1)), subset_size=self._subset_size
)
statistic_container.add_statistic_point(
StatisticPoint(
Expand Down
27 changes: 18 additions & 9 deletions tests/openvino/native/quantization/test_weights_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,25 +886,34 @@ def test_compression_for_different_dtypes(activation_dtype, weight_dtype):
check_compressed_matmul_subgraph(scale_multiply_node, activation_dtype, weight_dtype)


DATASET_SIZE = 129
DATASET_SIZE = 5


@pytest.mark.parametrize(
("subset_size", "ref_size"),
("dataset_size", "subset_size", "ref_size"),
(
(1, 1),
(5, 5),
(130, DATASET_SIZE),
(DATASET_SIZE, 1, 1),
(DATASET_SIZE, DATASET_SIZE, DATASET_SIZE),
(DATASET_SIZE, DATASET_SIZE + 1, DATASET_SIZE),
),
)
def test_valid_subset_size(mocker, subset_size, ref_size):
@pytest.mark.parametrize(
("compression_args", "multiplier_of_calls"),
(
(dict(mode=CompressWeightsMode.INT4_ASYM, ratio=1), 0), # data-free, no reducers
(dict(mode=CompressWeightsMode.INT4_ASYM, ratio=0.5), 1), # 1 reducer for mixed precision
(dict(mode=CompressWeightsMode.INT4_ASYM, ratio=1, awq=True), 2), # mean & shape reducer for AWQ
(dict(mode=CompressWeightsMode.INT4_ASYM, ratio=0.5, awq=True), 3), # 2 - for AWQ + 1 - for Mixed Precision
),
)
def test_data_aware_all_layers(mocker, dataset_size, subset_size, ref_size, compression_args, multiplier_of_calls):
model = IdentityMatmul().ov_model
dataset = Dataset([ACTIVATION] * DATASET_SIZE)
dataset = Dataset([ACTIVATION] * dataset_size)
stats_spy = mocker.spy(AggregatorBase, "register_reduced_input")

compress_weights(model, mode=CompressWeightsMode.INT4_ASYM, ratio=0.5, dataset=dataset, subset_size=subset_size)
compress_weights(model, dataset=dataset, subset_size=subset_size, **compression_args)

assert stats_spy.call_count == ref_size
assert stats_spy.call_count == ref_size * multiplier_of_calls


def test_default_subset_value():
Expand Down

0 comments on commit bb51646

Please sign in to comment.