Skip to content

Commit

Permalink
preset is optional parameter of nncf.quantize(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsu52 committed Oct 25, 2023
1 parent 8c10eea commit a273d8e
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion nncf/experimental/torch/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def send_to_device(tensor):
def quantize_impl(
model: torch.nn.Module,
calibration_dataset: Dataset,
preset: QuantizationPreset,
preset: Optional[QuantizationPreset],
target_device: TargetDevice,
subset_size: int,
fast_bias_correction: bool,
Expand Down
2 changes: 1 addition & 1 deletion nncf/onnx/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
def quantize_impl(
model: onnx.ModelProto,
calibration_dataset: Dataset,
preset: QuantizationPreset,
preset: Optional[QuantizationPreset],
target_device: TargetDevice,
subset_size: int,
fast_bias_correction: bool,
Expand Down
12 changes: 9 additions & 3 deletions nncf/openvino/pot/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _create_quantization_group_config(


def _create_quantization_config(
preset: QuantizationPreset,
preset: Optional[QuantizationPreset],
target_device: TargetDevice,
subset_size: int,
fast_bias_correction: bool,
Expand Down Expand Up @@ -224,6 +224,12 @@ def _create_quantization_config(
fine-tuning the quantization algorithm.
:return: A POT quantization configuration as dict.
"""
if preset is None:
if model_type == ModelType.TRANSFORMER:
preset = QuantizationPreset.MIXED
else:
preset = QuantizationPreset.PERFORMANCE

config = {
"target_device": target_device.value,
"preset": preset.value,
Expand Down Expand Up @@ -320,7 +326,7 @@ def _create_engine_config(
def quantize_impl(
model: ov.Model,
calibration_dataset: Dataset,
preset: QuantizationPreset = QuantizationPreset.PERFORMANCE,
preset: Optional[QuantizationPreset] = None,
target_device: TargetDevice = TargetDevice.ANY,
subset_size: int = 300,
fast_bias_correction: bool = True,
Expand Down Expand Up @@ -423,7 +429,7 @@ def quantize_with_accuracy_control_impl(
validation_fn: Callable[[ov.CompiledModel, Iterable[Any]], float],
max_drop: float = 0.01,
drop_type: DropType = DropType.ABSOLUTE,
preset: QuantizationPreset = QuantizationPreset.PERFORMANCE,
preset: Optional[QuantizationPreset] = None,
target_device: TargetDevice = TargetDevice.ANY,
subset_size: int = 300,
fast_bias_correction: bool = True,
Expand Down
9 changes: 8 additions & 1 deletion nncf/quantization/algorithms/min_max/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class MinMaxQuantization(Algorithm):

def __init__(
self,
preset: QuantizationPreset = QuantizationPreset.PERFORMANCE,
preset: Optional[QuantizationPreset] = None,
target_device: TargetDevice = TargetDevice.ANY,
subset_size: int = 300,
model_type: Optional[ModelType] = None,
Expand Down Expand Up @@ -157,6 +157,13 @@ def __init__(
QuantizerGroup.ACTIVATIONS: activations_range_estimator_params,
}

# preset definition
if preset is None:
if model_type == ModelType.TRANSFORMER:
preset = QuantizationPreset.MIXED
else:
preset = QuantizationPreset.PERFORMANCE

# Calculates global quantizer constraints
self._global_quantizer_constraints = {}
for quantizer_group in QuantizerGroup:
Expand Down
2 changes: 1 addition & 1 deletion nncf/quantization/algorithms/post_training/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PostTrainingQuantization(Algorithm):

def __init__(
self,
preset: QuantizationPreset = QuantizationPreset.PERFORMANCE,
preset: Optional[QuantizationPreset] = None,
target_device: TargetDevice = TargetDevice.ANY,
subset_size: int = 300,
fast_bias_correction: bool = True,
Expand Down
2 changes: 1 addition & 1 deletion nncf/quantization/algorithms/post_training/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


def create_ptq_pipeline(
preset: QuantizationPreset = QuantizationPreset.PERFORMANCE,
preset: Optional[QuantizationPreset] = None,
target_device: TargetDevice = TargetDevice.ANY,
subset_size: int = 300,
fast_bias_correction: bool = True,
Expand Down
6 changes: 3 additions & 3 deletions nncf/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
def quantize(
model: TModel,
calibration_dataset: Dataset,
preset: QuantizationPreset = QuantizationPreset.PERFORMANCE,
preset: Optional[QuantizationPreset] = None,
target_device: TargetDevice = TargetDevice.ANY,
subset_size: int = 300,
fast_bias_correction: bool = True,
Expand Down Expand Up @@ -152,7 +152,7 @@ def quantize_with_accuracy_control(
validation_fn: Callable[[Any, Iterable[Any]], float],
max_drop: float = 0.01,
drop_type: DropType = DropType.ABSOLUTE,
preset: QuantizationPreset = QuantizationPreset.PERFORMANCE,
preset: Optional[QuantizationPreset] = None,
target_device: TargetDevice = TargetDevice.ANY,
subset_size: int = 300,
fast_bias_correction: bool = True,
Expand Down Expand Up @@ -288,7 +288,7 @@ def quantize_with_tune_hyperparams(
initial_metric_results: MetricResults,
quantized_metric_results: MetricResults,
tuner_subset_size: int = 300,
preset: QuantizationPreset = QuantizationPreset.PERFORMANCE,
preset: Optional[QuantizationPreset] = None,
target_device: TargetDevice = TargetDevice.ANY,
subset_size: int = 300,
fast_bias_correction: bool = True,
Expand Down
5 changes: 4 additions & 1 deletion nncf/tensorflow/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _create_nncf_config(
def quantize_impl(
model: tf.Module,
calibration_dataset: Dataset,
preset: QuantizationPreset,
preset: Optional[QuantizationPreset],
target_device: TargetDevice,
subset_size: int,
fast_bias_correction: bool,
Expand All @@ -157,6 +157,9 @@ def quantize_impl(
if target_device == TargetDevice.CPU_SPR:
raise RuntimeError("target_device == CPU_SPR is not supported.")

if preset is None:
preset = QuantizationPreset.PERFORMANCE

nncf_config = _create_nncf_config(preset, target_device, subset_size, ignored_scope, advanced_parameters)

calibration_data_loader = CalibrationDataLoader(calibration_dataset)
Expand Down
20 changes: 13 additions & 7 deletions nncf/torch/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _get_length(iterable) -> int:
return length


def _get_transformer_quantization_config(subset_size: int) -> Dict[str, Any]:
def _get_transformer_quantization_config(preset: QuantizationPreset, subset_size: int) -> Dict[str, Any]:
"""
Returns the quantization config for transformer-based models.
Expand All @@ -91,7 +91,7 @@ def _get_transformer_quantization_config(subset_size: int) -> Dict[str, Any]:
"""
return {
"algorithm": "quantization",
"preset": "mixed",
"preset": preset.value,
"initializer": {
"range": {"num_init_samples": subset_size, "type": DEFAULT_RANGE_TYPE},
"batchnorm_adaptation": {"num_bn_adaptation_samples": 0},
Expand Down Expand Up @@ -133,7 +133,7 @@ def _get_default_quantization_config(preset: QuantizationPreset, subset_size: in


def _create_nncf_config(
preset: QuantizationPreset,
preset: Optional[QuantizationPreset],
target_device: TargetDevice,
subset_size: int,
model_type: Optional[ModelType],
Expand Down Expand Up @@ -161,10 +161,16 @@ def _create_nncf_config(
fine-tuning the quantization algorithm.
:return: NNCFConfig for the quantization algorithm.
"""
if model_type is None:
if preset is None:
if model_type == ModelType.TRANSFORMER:
preset = QuantizationPreset.MIXED
else:
preset = QuantizationPreset.PERFORMANCE

if model_type == ModelType.TRANSFORMER:
compression_config = _get_transformer_quantization_config(preset, subset_size)
else:
compression_config = _get_default_quantization_config(preset, subset_size)
elif model_type == ModelType.TRANSFORMER:
compression_config = _get_transformer_quantization_config(subset_size)

if ignored_scope is not None:
_ignored_scope = convert_ignored_scope_to_list(ignored_scope)
Expand All @@ -186,7 +192,7 @@ def _create_nncf_config(
def quantize_impl(
model: torch.nn.Module,
calibration_dataset: Dataset,
preset: QuantizationPreset,
preset: Optional[QuantizationPreset],
target_device: TargetDevice,
subset_size: int,
fast_bias_correction: bool,
Expand Down

0 comments on commit a273d8e

Please sign in to comment.