Skip to content

Commit

Permalink
Replace method
Browse files Browse the repository at this point in the history
  • Loading branch information
KodiaqQ committed Oct 17, 2023
1 parent 19f7cb1 commit 90dc04e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
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 @@ -90,7 +90,7 @@ def __init__(

sq_params = advanced_parameters.smooth_quant_params

if model_type == ModelType.TRANSFORMER and sq_params.convolution >= 0 and sq_params.matmul >= 0:
if model_type == ModelType.TRANSFORMER and (sq_params.convolution >= 0 or sq_params.matmul >= 0):
alpha_map = {"convolution": sq_params.convolution, "matmul": sq_params.matmul}
smooth_quant_algorithm = SmoothQuant(
subset_size=subset_size, inplace_statistics=advanced_parameters.inplace_statistics, alpha_map=alpha_map
Expand Down
37 changes: 24 additions & 13 deletions nncf/quantization/algorithms/smooth_quant/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def __init__(
self._backend_entity = None
self._algorithm_key = f"SQ_{hash(self)}"
self._cached_multiply_names = Counter()
self._validate_alpha_map(alpha_map)
self._alpha_map = alpha_map

@property
Expand All @@ -97,16 +96,6 @@ def _set_backend_entity(self, model: TModel) -> None:
"Cannot return backend-specific entity because {} is not supported!".format(model_backend.value)
)

def _validate_alpha_map(self, alpha_map: Dict[str, float]) -> None:
"""
Validates the alpha map for case of the negative value.
:param alpha_map: Map for the validation.
"""
for layer_type, alpha_value in alpha_map.items():
if alpha_value < 0:
raise RuntimeError(f"Smooth Quant algorithm does not support negative parameter for {layer_type}!")

def apply(
self,
model: TModel,
Expand All @@ -115,7 +104,7 @@ def apply(
dataset: Optional[Dataset] = None,
) -> TModel:
self._set_backend_entity(model)
alpha_map = self._backend_entity.get_alpha_map(self._alpha_map)
alpha_map = self._get_alpha_map()

nodes_to_smooth_data = self._get_nodes_to_smooth_data(graph, alpha_map.keys())
model_transformer = ModelTransformerFactory.create(model)
Expand Down Expand Up @@ -238,7 +227,7 @@ def get_statistic_points(self, model: TModel, graph: NNCFGraph) -> StatisticPoin
statistic_container = StatisticPointsContainer()

self._set_backend_entity(model)
alpha_map = self._backend_entity.get_alpha_map(self._alpha_map)
alpha_map = self._get_alpha_map()

nodes_to_smooth_data = self._get_nodes_to_smooth_data(graph, alpha_map.keys())

Expand Down Expand Up @@ -377,3 +366,25 @@ def _create_scale_node_name(self, source_name: str, source_port_id: int) -> str:
unique_index = self._cached_multiply_names[scale_node_name]
self._cached_multiply_names[scale_node_name] += 1
return f"{scale_node_name}_{unique_index}/sq_multiply"

def _get_alpha_map(self) -> Dict[OperatorMetatype, float]:
"""
Returns alpha map by metatypes.
:return: Alpha map by metatypes.
"""
alpha_by_metatype_map = {}
name_to_metatype = {
"convolution": self._backend_entity.convolution_metatype,
"matmul": self._backend_entity.matmul_metatype,
}
for type_name, alpha_value in self._alpha_map.items():
if alpha_value < 0:
nncf_logger.debug(
f"Smooth Quant algorithm does not support negative parameter for {type_name}! "
"Skipping these layers."
)
continue
metatype = name_to_metatype[type_name]
alpha_by_metatype_map[metatype] = alpha_value
return alpha_by_metatype_map
15 changes: 0 additions & 15 deletions nncf/quantization/algorithms/smooth_quant/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,6 @@


class SmoothQuantAlgoBackend(ABC):
def get_alpha_map(self, alpha_map: Dict[str, float]) -> Dict[OperatorMetatype, float]:
"""
Returns alpha map by metatypes.
:param convolution_alpha: Alpha value for Convolution layers.
:param matmul_alpha: Alpha value for MatMul layers.
:return: Alpha map by metatypes.
"""
alpha_by_metatype_map = {}
name_to_metatype = {"convolution": self.convolution_metatype, "matmul": self.matmul_metatype}
for type_name, alpha_value in alpha_map.items():
metatype = name_to_metatype[type_name]
alpha_by_metatype_map[metatype] = alpha_value
return alpha_by_metatype_map

@property
@abstractmethod
def convolution_metatype(self) -> OperatorMetatype:
Expand Down
9 changes: 8 additions & 1 deletion tests/post_training/model_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

from nncf import ModelType
from nncf import QuantizationPreset
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters
from tests.post_training.pipelines.base import ALL_PTQ_BACKENDS
from tests.post_training.pipelines.base import NNCF_PTQ_BACKENDS
from tests.post_training.pipelines.base import BackendType
Expand Down Expand Up @@ -52,6 +54,9 @@
"ptq_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
"advanced_parameters": AdvancedQuantizationParameters(
smooth_quant_params=AdvancedSmoothQuantParameters(matmul=-1.0)
),
},
"backends": [BackendType.TORCH, BackendType.ONNX, BackendType.OV, BackendType.POT],
},
Expand All @@ -71,6 +76,9 @@
"ptq_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
"advanced_parameters": AdvancedQuantizationParameters(
smooth_quant_params=AdvancedSmoothQuantParameters(matmul=-1.0)
),
},
"backends": ALL_PTQ_BACKENDS,
},
Expand Down Expand Up @@ -143,7 +151,6 @@
"ptq_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
# "advanced_parameters": AdvancedQuantizationParameters(smooth_quant_alpha=0.05),
},
"backends": [BackendType.TORCH, BackendType.ONNX, BackendType.OV],
},
Expand Down

0 comments on commit 90dc04e

Please sign in to comment.