Skip to content

Commit 0deaf81

Browse files
POC histogramm aggregator plugged in to MinMax
1 parent 468196a commit 0deaf81

File tree

8 files changed

+144
-10
lines changed

8 files changed

+144
-10
lines changed

src/nncf/experimental/common/tensor_statistics/collectors.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from nncf.common.tensor_statistics.collectors import ReductionAxes
2424
from nncf.experimental.common.tensor_statistics.statistical_functions import mean_per_channel
2525
from nncf.experimental.common.tensor_statistics.statistics import MedianMADTensorStatistic
26+
from nncf.experimental.common.tensor_statistics.statistics import MinMaxTensorStatistic
2627
from nncf.experimental.common.tensor_statistics.statistics import TensorStatistic
2728
from nncf.quantization.advanced_parameters import AggregatorType
2829
from nncf.quantization.range_estimator import StatisticsType
@@ -1154,8 +1155,9 @@ def _register_reduced_input_impl(self, x: Tensor):
11541155
self.min_val = new_min
11551156
self.max_val = new_max
11561157

1157-
def _aggregate_impl(self) -> Any:
1158-
return self._non_linear_param_search()
1158+
def _aggregate_impl(self) -> dict[str, float]:
1159+
min_, max_ = self._non_linear_param_search()
1160+
return {MinMaxTensorStatistic.MIN_STAT: min_, MinMaxTensorStatistic.MAX_STAT: max_}
11591161

11601162

11611163
REDUCERS_MAP = {
@@ -1165,6 +1167,7 @@ def _aggregate_impl(self) -> Any:
11651167
StatisticsType.MEAN: MeanReducer,
11661168
StatisticsType.QUANTILE: QuantileReducer,
11671169
StatisticsType.ABS_QUANTILE: AbsQuantileReducer,
1170+
StatisticsType.RAW: RawReducer,
11681171
}
11691172

11701173
AGGREGATORS_MAP = {
@@ -1174,4 +1177,5 @@ def _aggregate_impl(self) -> Any:
11741177
AggregatorType.MEAN_NO_OUTLIERS: MeanNoOutliersAggregator,
11751178
AggregatorType.MEDIAN: MedianAggregator,
11761179
AggregatorType.MEDIAN_NO_OUTLIERS: MedianNoOutliersAggregator,
1180+
AggregatorType.HISTOGRAM: HistogramAggregator,
11771181
}

src/nncf/experimental/common/tensor_statistics/statistics.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def from_config(cls, config: dict[str, Any]) -> TensorStatistic:
7777
class MinMaxTensorStatistic(TensorStatistic):
7878
MIN_STAT: ClassVar[str] = "min_values"
7979
MAX_STAT: ClassVar[str] = "max_values"
80+
MIN_MAX_STAT: ClassVar[str] = "min_max_values"
8081

8182
min_values: Tensor
8283
max_values: Tensor
@@ -86,6 +87,12 @@ def __eq__(self, other: TensorStatistic):
8687
return fns.allclose(self.min_values, other.min_values) and fns.allclose(self.max_values, other.max_values)
8788
return False
8889

90+
@classmethod
91+
def from_config(cls, config: dict[str, Any]) -> TensorStatistic:
92+
if cls.MIN_MAX_STAT in config:
93+
return cls(**config[cls.MIN_MAX_STAT])
94+
return cls(min_values=config[cls.MIN_STAT], max_values=config[cls.MAX_STAT])
95+
8996

9097
@dataclass
9198
class AbsMaxTensorStatistic(TensorStatistic):

src/nncf/openvino/statistics/collectors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,5 @@ def get_raw_stat_collector(num_samples: Optional[int] = None) -> TensorCollector
151151
StatisticsType.MEAN: OVMeanReducer,
152152
StatisticsType.QUANTILE: OVQuantileReducer,
153153
StatisticsType.ABS_QUANTILE: OVAbsQuantileReducer,
154+
StatisticsType.RAW: RawReducer,
154155
}

src/nncf/quantization/advanced_parameters.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from nncf.parameters import StrEnum
2828
from nncf.quantization.range_estimator import AggregatorType
2929
from nncf.quantization.range_estimator import RangeEstimatorParameters
30+
from nncf.quantization.range_estimator import StatisticsCollectorParameters
3031
from nncf.quantization.range_estimator import StatisticsType
3132

3233
TTensor = Any
@@ -272,8 +273,12 @@ class AdvancedQuantizationParameters:
272273
quantizer_propagation_rule: QuantizerPropagationRule = QuantizerPropagationRule.MERGE_ALL_IN_ONE
273274

274275
# Range estimator parameters
275-
activations_range_estimator_params: RangeEstimatorParameters = field(default_factory=RangeEstimatorParameters)
276-
weights_range_estimator_params: RangeEstimatorParameters = field(default_factory=RangeEstimatorParameters)
276+
activations_range_estimator_params: Union[RangeEstimatorParameters, StatisticsCollectorParameters] = field(
277+
default_factory=RangeEstimatorParameters
278+
)
279+
weights_range_estimator_params: Union[RangeEstimatorParameters, StatisticsCollectorParameters] = field(
280+
default_factory=RangeEstimatorParameters
281+
)
277282

278283
# Advanced BiasCorrection algorithm parameters
279284
bias_correction_params: AdvancedBiasCorrectionParameters = field(default_factory=AdvancedBiasCorrectionParameters)

src/nncf/quantization/algorithms/min_max/algorithm.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from nncf.quantization.range_estimator import AggregatorType
6868
from nncf.quantization.range_estimator import RangeEstimatorParameters
6969
from nncf.quantization.range_estimator import RangeEstimatorParametersSet
70+
from nncf.quantization.range_estimator import StatisticsCollectorParameters
7071
from nncf.quantization.range_estimator import StatisticsType
7172
from nncf.scopes import IgnoredScope
7273
from nncf.scopes import get_ignored_node_names_from_ignored_scope
@@ -433,6 +434,15 @@ def _get_range_estimator_parameters(
433434
if user_params is None:
434435
return deepcopy(params)
435436

437+
if isinstance(user_params, StatisticsCollectorParameters):
438+
if quantizer_config.per_channel:
439+
msg = (
440+
f"Could not create signle aggregator with parameters {user_params}",
441+
" Per channel statistic collection is not supported for the single aggregator case yet.",
442+
)
443+
raise nncf.InternalError(msg)
444+
return deepcopy(user_params)
445+
436446
min_changes = changes_asdict(user_params.min)
437447
min_statistic_collector = dataclasses.replace(params.min, **min_changes)
438448

@@ -495,7 +505,7 @@ def _get_stat_collector(
495505

496506
def _get_statistic_collector(
497507
self,
498-
range_estimator_params: RangeEstimatorParameters,
508+
range_estimator_params: Union[RangeEstimatorParameters, StatisticsCollectorParameters],
499509
use_abs_max: bool,
500510
reduction_axes: Optional[tuple[int, ...]],
501511
aggregation_axes: Optional[tuple[int, ...]],
@@ -513,10 +523,24 @@ def _get_statistic_collector(
513523
:param num_samples: Maximum number of samples to collect.
514524
:return: TensorCollector for the statistics calculation.
515525
"""
526+
collector = TensorCollector(MinMaxTensorStatistic)
527+
if isinstance(range_estimator_params, StatisticsCollectorParameters):
528+
if range_estimator_params.statistics_type is not StatisticsType.RAW:
529+
msg = "Only RAW statistic type is suppored for single aggregator case."
530+
raise nncf.InternalError(msg)
531+
532+
if range_estimator_params.aggregator_type is not AggregatorType.HISTOGRAM:
533+
msg = "Only HISTOGRAM aggregator type is suppored for single aggregator case."
534+
raise nncf.InternalError(msg)
535+
536+
reducer = self._backend_entity.reducer_map[StatisticsType.RAW]()
537+
aggregator = AGGREGATORS_MAP[AggregatorType.HISTOGRAM]()
538+
collector.register_statistic_branch(MinMaxTensorStatistic.MIN_MAX_STAT, reducer, aggregator)
539+
return collector
540+
516541
if not self._backend_entity.supports_inplace_statistics:
517542
inplace = False
518543

519-
collector = TensorCollector(MinMaxTensorStatistic)
520544
for params, container_key in zip(
521545
[range_estimator_params.min, range_estimator_params.max],
522546
[MinMaxTensorStatistic.MIN_STAT, MinMaxTensorStatistic.MAX_STAT],

src/nncf/quantization/range_estimator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class StatisticsType(Enum):
3737
QUANTILE = "quantile"
3838
ABS_QUANTILE = "abs_quantile"
3939
MEAN = "mean"
40+
RAW = "raw"
4041

4142

4243
@api()
@@ -59,6 +60,7 @@ class AggregatorType(Enum):
5960
MEDIAN = "median"
6061
MEAN_NO_OUTLIERS = "mean_no_outliers"
6162
MEDIAN_NO_OUTLIERS = "median_no_outliers"
63+
HISTOGRAM = "histogram"
6264

6365

6466
@api()

src/nncf/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
__version__ = "2.18.0"
1313

1414

15-
BKC_TORCH_SPEC = "==2.7.*"
15+
BKC_TORCH_SPEC = "==2.8.*"
1616
BKC_TF_SPEC = "==2.15.*"
1717
STRICT_TF_SPEC = ">=2.9.3,<2.16.0"

tests/post_training/model_scope.py

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
2424
from nncf.quantization.advanced_parameters import AdvancedScaleEstimationParameters
2525
from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters
26+
from nncf.quantization.range_estimator import AggregatorType
27+
from nncf.quantization.range_estimator import StatisticsCollectorParameters
28+
from nncf.quantization.range_estimator import StatisticsType
2629
from tests.post_training.pipelines.base import ALL_PTQ_BACKENDS
2730
from tests.post_training.pipelines.base import FX_BACKENDS
2831
from tests.post_training.pipelines.base import NNCF_PTQ_BACKENDS
@@ -89,6 +92,11 @@
8992
"pipeline_cls": ImageClassificationTorchvision,
9093
"compression_params": {
9194
"subset_size": 2,
95+
"advanced_parameters": AdvancedQuantizationParameters(
96+
activations_range_estimator_params=StatisticsCollectorParameters(
97+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
98+
)
99+
),
92100
},
93101
"backends": FX_BACKENDS
94102
+ [
@@ -117,7 +125,12 @@
117125
"pipeline_cls": ImageClassificationTorchvision,
118126
"compression_params": {
119127
"model_type": ModelType.TRANSFORMER,
120-
"advanced_parameters": AdvancedQuantizationParameters(smooth_quant_alpha=0.15),
128+
"advanced_parameters": AdvancedQuantizationParameters(
129+
smooth_quant_alpha=0.15,
130+
activations_range_estimator_params=StatisticsCollectorParameters(
131+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
132+
),
133+
),
121134
},
122135
"backends": FX_BACKENDS + [BackendType.OV],
123136
"batch_size": 1,
@@ -128,7 +141,12 @@
128141
"pipeline_cls": ImageClassificationTorchvision,
129142
"compression_params": {
130143
"model_type": ModelType.TRANSFORMER,
131-
"advanced_parameters": AdvancedQuantizationParameters(smooth_quant_alpha=0.5),
144+
"advanced_parameters": AdvancedQuantizationParameters(
145+
smooth_quant_alpha=0.5,
146+
activations_range_estimator_params=StatisticsCollectorParameters(
147+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
148+
),
149+
),
132150
},
133151
"backends": FX_BACKENDS + [BackendType.OV],
134152
"batch_size": 1,
@@ -180,6 +198,11 @@
180198
"compression_params": {
181199
"subset_size": 2,
182200
"preset": QuantizationPreset.MIXED,
201+
"advanced_parameters": AdvancedQuantizationParameters(
202+
activations_range_estimator_params=StatisticsCollectorParameters(
203+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
204+
)
205+
),
183206
},
184207
"backends": ALL_PTQ_BACKENDS,
185208
"batch_size": 128,
@@ -191,6 +214,11 @@
191214
"compression_params": {
192215
"subset_size": 2,
193216
"preset": QuantizationPreset.MIXED,
217+
"advanced_parameters": AdvancedQuantizationParameters(
218+
activations_range_estimator_params=StatisticsCollectorParameters(
219+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
220+
)
221+
),
194222
},
195223
"backends": ALL_PTQ_BACKENDS,
196224
"batch_size": 128,
@@ -202,6 +230,11 @@
202230
"compression_params": {
203231
"subset_size": 2,
204232
"preset": QuantizationPreset.MIXED,
233+
"advanced_parameters": AdvancedQuantizationParameters(
234+
activations_range_estimator_params=StatisticsCollectorParameters(
235+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
236+
)
237+
),
205238
},
206239
"backends": ALL_PTQ_BACKENDS,
207240
"batch_size": 128,
@@ -214,6 +247,11 @@
214247
"subset_size": 2,
215248
"preset": QuantizationPreset.MIXED,
216249
"fast_bias_correction": False,
250+
"advanced_parameters": AdvancedQuantizationParameters(
251+
activations_range_estimator_params=StatisticsCollectorParameters(
252+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
253+
)
254+
),
217255
},
218256
"backends": [BackendType.ONNX, BackendType.OV],
219257
"batch_size": 128,
@@ -225,6 +263,11 @@
225263
"compression_params": {
226264
"subset_size": 2,
227265
"preset": QuantizationPreset.MIXED,
266+
"advanced_parameters": AdvancedQuantizationParameters(
267+
activations_range_estimator_params=StatisticsCollectorParameters(
268+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
269+
)
270+
),
228271
},
229272
"backends": ALL_PTQ_BACKENDS,
230273
"batch_size": 128,
@@ -236,6 +279,11 @@
236279
"compression_params": {
237280
"subset_size": 2,
238281
"preset": QuantizationPreset.MIXED,
282+
"advanced_parameters": AdvancedQuantizationParameters(
283+
activations_range_estimator_params=StatisticsCollectorParameters(
284+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
285+
)
286+
),
239287
},
240288
"backends": ALL_PTQ_BACKENDS,
241289
"batch_size": 128,
@@ -246,6 +294,11 @@
246294
"pipeline_cls": ImageClassificationTimm,
247295
"compression_params": {
248296
"subset_size": 4,
297+
"advanced_parameters": AdvancedQuantizationParameters(
298+
activations_range_estimator_params=StatisticsCollectorParameters(
299+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
300+
)
301+
),
249302
},
250303
"backends": NNCF_PTQ_BACKENDS,
251304
"batch_size": 64,
@@ -303,6 +356,11 @@
303356
"compression_params": {
304357
"subset_size": 2,
305358
"preset": QuantizationPreset.MIXED,
359+
"advanced_parameters": AdvancedQuantizationParameters(
360+
activations_range_estimator_params=StatisticsCollectorParameters(
361+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
362+
)
363+
),
306364
},
307365
"backends": ALL_PTQ_BACKENDS,
308366
"batch_size": 128,
@@ -314,6 +372,11 @@
314372
"compression_params": {
315373
"subset_size": 2,
316374
"preset": QuantizationPreset.MIXED,
375+
"advanced_parameters": AdvancedQuantizationParameters(
376+
activations_range_estimator_params=StatisticsCollectorParameters(
377+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
378+
)
379+
),
317380
},
318381
"backends": ALL_PTQ_BACKENDS,
319382
"batch_size": 128,
@@ -326,6 +389,11 @@
326389
"subset_size": 9,
327390
"preset": QuantizationPreset.MIXED,
328391
"model_type": ModelType.TRANSFORMER,
392+
"advanced_parameters": AdvancedQuantizationParameters(
393+
activations_range_estimator_params=StatisticsCollectorParameters(
394+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
395+
)
396+
),
329397
},
330398
"backends": [BackendType.OV],
331399
"batch_size": 32,
@@ -339,7 +407,10 @@
339407
"preset": QuantizationPreset.MIXED,
340408
"model_type": ModelType.TRANSFORMER,
341409
"advanced_parameters": AdvancedQuantizationParameters(
342-
smooth_quant_alphas=AdvancedSmoothQuantParameters(matmul=-1)
410+
smooth_quant_alphas=AdvancedSmoothQuantParameters(matmul=-1),
411+
activations_range_estimator_params=StatisticsCollectorParameters(
412+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
413+
),
343414
),
344415
},
345416
"backends": [BackendType.TORCH, BackendType.CUDA_TORCH, BackendType.ONNX],
@@ -352,6 +423,11 @@
352423
"compression_params": {
353424
"subset_size": 2,
354425
"preset": QuantizationPreset.MIXED,
426+
"advanced_parameters": AdvancedQuantizationParameters(
427+
activations_range_estimator_params=StatisticsCollectorParameters(
428+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
429+
)
430+
),
355431
},
356432
"backends": ALL_PTQ_BACKENDS,
357433
"batch_size": 128,
@@ -362,6 +438,11 @@
362438
"pipeline_cls": ImageClassificationTimm,
363439
"compression_params": {
364440
"subset_size": 2,
441+
"advanced_parameters": AdvancedQuantizationParameters(
442+
activations_range_estimator_params=StatisticsCollectorParameters(
443+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
444+
)
445+
),
365446
},
366447
"backends": NNCF_PTQ_BACKENDS,
367448
"batch_size": 128,
@@ -374,6 +455,11 @@
374455
"subset_size": 2,
375456
"preset": QuantizationPreset.MIXED,
376457
"model_type": ModelType.TRANSFORMER,
458+
"advanced_parameters": AdvancedQuantizationParameters(
459+
activations_range_estimator_params=StatisticsCollectorParameters(
460+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
461+
)
462+
),
377463
},
378464
"backends": ALL_PTQ_BACKENDS,
379465
"batch_size": 128,
@@ -385,6 +471,11 @@
385471
"compression_params": {
386472
"subset_size": 2,
387473
"preset": QuantizationPreset.MIXED,
474+
"advanced_parameters": AdvancedQuantizationParameters(
475+
activations_range_estimator_params=StatisticsCollectorParameters(
476+
statistics_type=StatisticsType.RAW, aggregator_type=AggregatorType.HISTOGRAM
477+
)
478+
),
388479
},
389480
"backends": ALL_PTQ_BACKENDS,
390481
"batch_size": 128,

0 commit comments

Comments
 (0)