Skip to content
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

[NNCF] (#3249) Remove backend-specific methods from common layer attributes #3287

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
868335b
[NNCF] Add get_weight_shape_legacy function (#3249)
shumaari Feb 16, 2025
f04a525
[NNCF] Add get_target_dim_for_compression_legacy function (#3249)
shumaari Feb 17, 2025
a94e2a1
[NNCF] Add get_bias_shape_legacy function (#3249)
shumaari Feb 17, 2025
e0db330
[NNCF] Experimental torch backend: Replace (#3249)
shumaari Feb 16, 2025
433ff6b
[NNCF] Experimental tensorflow backend: Replace (#3249)
shumaari Feb 16, 2025
e437274
[NNCF] Experimental common backend: Replace (#3249)
shumaari Feb 16, 2025
39efbbd
[NNCF] Torch backend: Replace (#3249)
shumaari Feb 17, 2025
eb7905f
[NNCF] Remove backend-specific methods from common layer attributes
shumaari Feb 17, 2025
58d5112
[NNCF] Missing import statements in utils.py
shumaari Feb 18, 2025
bdd8b61
[NNCF] Remove abstract methods from common layer attributes
shumaari Feb 21, 2025
6c3e101
[NNCF] Add get_num_filters_legacy function
shumaari Feb 21, 2025
53f1ede
[NNCF] Replace get_num_filters: experimental torch backend
shumaari Feb 21, 2025
948ae9f
[NNCF] Remove get_num_filters from common layer attributes
shumaari Feb 21, 2025
0425fbb
[NNCF] Update get_weight_shape_legacy function
shumaari Feb 21, 2025
b6f3a39
[NNCF] Update get_target_dim_for_compression_legacy function
shumaari Feb 21, 2025
02ecd45
[NNCF] Update get_bias_shape_legacy function
shumaari Feb 21, 2025
907f35f
Formatting changes due black and isort
shumaari Feb 21, 2025
b70c03a
Merge branch 'openvinotoolkit:develop' into 3249__remove_backend_spec…
shumaari Feb 22, 2025
d4493d5
[NNCF] Replace calls: Torch backend
shumaari Feb 23, 2025
c24ac61
Implement suggested changes in doc strings
shumaari Feb 24, 2025
7de7e8b
Implement suggested refactoring of code
shumaari Feb 24, 2025
0377d87
Implement suggested changes
shumaari Feb 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions nncf/common/graph/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,61 @@ def get_reduction_axes(
for channel_axis in sorted(channel_axes, reverse=True):
del reduction_axes[channel_axis]
return tuple(reduction_axes)


def get_weight_shape_legacy(
layer_attributes: WeightedLayerAttributes) -> List[int]:
"""
Returns hard-coded weights shape layout only for Torch and Tensorflow models.

:param layer_attributes: layer attributes of NNCFNode.
:return: weights shape layout.
"""
if isinstance(layer_attributes, GenericWeightedLayerAttributes):
return layer_attributes.weight_shape

if isinstance(layer_attributes, LinearLayerAttributes):
return[layer_attributes.out_features, layer_attributes.in_features]

if isinstance(layer_attributes, ConvolutionLayerAttributes):
if not layer_attributes.transpose:
return [layer_attributes.out_channels,
layer_attributes.in_channels // layer_attributes.groups,
*layer_attributes.kernel_size]
return [layer_attributes.in_channels,
layer_attributes.out_channels // layer_attributes.groups,
*layer_attributes.kernel_size]

if isinstance(layer_attributes, GroupNormLayerAttributes):
return [layer_attributes.num_channels]


def get_target_dim_for_compression_legacy(
layer_attributes: WeightedLayerAttributes) -> int:
"""
Returns hard-coded target dim for compression only for Torch and Tensorflow models.

:param layer_attributes: layer attributes of NNCFNode.
:return: target dim for compression.
"""
if isinstance(layer_attributes, (GenericWeightedLayerAttributes,
LinearLayerAttributes, GroupNormLayerAttributes)):
return 0

if isinstance(layer_attributes, ConvolutionLayerAttributes):
# Always quantize per each "out" channel
if layer_attributes.transpose:
return 1
return 0


def get_bias_shape_legacy(
layer_attributes: WeightedLayerAttributes) -> int:
"""
Returns hard-coded bias shape only for Torch and Tensorflow models.

:param layer_attributes: layer attributes of NNCFNode.
:return: bias shape.
"""
if isinstance(layer_attributes, LinearLayerAttributes):
return layer_attributes.out_features if layer_attributes.with_bias is True else 0
3 changes: 2 additions & 1 deletion nncf/experimental/common/pruning/nodes_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from nncf.common.graph.graph import NNCFNode
from nncf.common.graph.layer_attributes import ConvolutionLayerAttributes
from nncf.common.graph.layer_attributes import LinearLayerAttributes
from nncf.common.graph.utils import get_target_dim_for_compression_legacy
from nncf.common.pruning.mask_propagation import MaskPropagationAlgorithm
from nncf.common.pruning.utils import PruningOperationsMetatypeRegistry
from nncf.experimental.common.graph.netron import save_for_netron
Expand Down Expand Up @@ -76,7 +77,7 @@ def get_pruning_groups(
roots = {}
for node in all_nodes_to_prune:
assert isinstance(node.layer_attributes, (LinearLayerAttributes, ConvolutionLayerAttributes))
pruning_dim = node.layer_attributes.get_target_dim_for_compression()
pruning_dim = get_target_dim_for_compression_legacy(node.layer_attributes)
output_tensors_shapes = [x.tensor_shape for x in graph.get_output_edges(node)]
assert not len(set(output_tensors_shapes)) > 1, node.node_name
output_tensors_shape = output_tensors_shapes[0]
Expand Down
3 changes: 2 additions & 1 deletion nncf/experimental/tensorflow/quantization/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from nncf.common.graph.transformations.commands import TargetType
from nncf.common.graph.transformations.commands import TransformationPriority
from nncf.common.graph.utils import get_first_nodes_of_type
from nncf.common.graph.utils import get_weight_shape_legacy
from nncf.common.logging import nncf_logger
from nncf.common.quantization.quantizer_setup import ActivationQuantizationInsertionPoint
from nncf.common.quantization.quantizer_setup import QuantizationPointId
Expand Down Expand Up @@ -133,7 +134,7 @@ def _get_tensor_specs(
assert len(metatype.weight_definitions) == 1

channel_axes = metatype.weight_definitions[0].channel_axes
weight_shape = node.layer_attributes.get_weight_shape()
weight_shape = get_weight_shape_legacy(node.layer_attributes)
tensor_specs.append((weight_shape, channel_axes))
else:
data_format = node.layer_attributes.get_data_format()
Expand Down
6 changes: 4 additions & 2 deletions nncf/experimental/torch/sparsity/movement/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import nncf
from nncf.common.graph import NNCFNode
from nncf.common.graph.utils import get_weight_shape_legacy
from nncf.common.graph.utils import get_bias_shape_legacy
from nncf.experimental.torch.sparsity.movement.functions import binary_mask_by_threshold
from nncf.torch.layer_utils import COMPRESSION_MODULES
from nncf.torch.layer_utils import CompressionParameter
Expand Down Expand Up @@ -167,7 +169,7 @@ def __init__(
self._importance_threshold = -math.inf
self._importance_regularization_factor = 0.0

weight_shape: List[int] = target_module_node.layer_attributes.get_weight_shape()
weight_shape: List[int] = get_weight_shape_legacy(target_module_node.layer_attributes)
assert len(weight_shape) == 2, "Unsupported module with weight shape not in 2D."
self.weight_ctx = BinaryMask(weight_shape)
self.sparse_factors = self._get_sparse_factors(weight_shape, sparse_cfg)
Expand All @@ -185,7 +187,7 @@ def __init__(
self.weight_ctx.binary_mask = self._calc_training_binary_mask()

if self.prune_bias:
bias_shape = target_module_node.layer_attributes.get_bias_shape()
bias_shape = get_bias_shape_legacy(target_module_node.layer_attributes)
self.bias_ctx = BinaryMask(bias_shape)
bias_importance_shape = weight_importance_shape[0]
self.bias_importance = CompressionParameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

from nncf.common.graph.graph import NNCFNodeName
from nncf.common.graph.layer_attributes import LinearLayerAttributes
from nncf.common.graph.utils import get_weight_shape_legacy
from nncf.common.graph.utils import get_bias_shape_legacy
from nncf.common.logging import nncf_logger
from nncf.experimental.common.pruning.nodes_grouping import get_pruning_groups
from nncf.experimental.common.pruning.nodes_grouping import select_largest_groups
Expand Down Expand Up @@ -209,9 +211,9 @@ def gather_statistics_from_operand(self) -> StructuredMaskContextStatistics:
"""
node = self.sparsifier_operand.target_module_node
assert isinstance(node.layer_attributes, tuple(EXPECTED_NODE_LAYER_ATTRS))
weight_shape: Tuple[int, int] = tuple(node.layer_attributes.get_weight_shape())
weight_shape: Tuple[int, int] = tuple(get_weight_shape_legacy(node.layer_attributes))
bias_shape: Tuple[int] = (
(node.layer_attributes.get_bias_shape(),) if self.sparsifier_operand.prune_bias else (0,)
(get_bias_shape_legacy(node.layer_attributes),) if self.sparsifier_operand.prune_bias else (0,)
)

pruned_weight_shape = list(weight_shape)
Expand Down
8 changes: 5 additions & 3 deletions nncf/torch/quantization/algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from nncf.common.graph.patterns.manager import TargetDevice
from nncf.common.graph.transformations.commands import TargetType
from nncf.common.graph.utils import get_first_nodes_of_type
from nncf.common.graph.utils import get_weight_shape_legacy
from nncf.common.graph.utils import get_target_dim_for_compression_legacy
from nncf.common.hardware.config import HWConfig
from nncf.common.hardware.config import HWConfigType
from nncf.common.hardware.config import get_hw_config_type
Expand Down Expand Up @@ -773,10 +775,10 @@ def _get_quantizer_setup(self, target_model: NNCFNetwork) -> PTQuantizerSetup:
layer_attributes = target_node.layer_attributes
assert isinstance(layer_attributes, WeightedLayerAttributes)
scale_shape = get_scale_shape(
layer_attributes.get_weight_shape(),
get_weight_shape_legacy(layer_attributes),
is_weights=True,
per_channel=qconfig.per_channel,
channel_idx=layer_attributes.get_target_dim_for_compression(),
channel_idx=get_target_dim_for_compression_legacy(layer_attributes),
)
else:
input_shape = target_model_graph.get_input_shape_for_insertion_point(insertion_point)
Expand Down Expand Up @@ -1182,7 +1184,7 @@ def is_weights(ip: PTTargetPoint) -> bool:
)
module_node = target_model_graph.get_node_by_name(primary_ip.target_node_name)
layer_attributes = module_node.layer_attributes
input_shape = layer_attributes.get_weight_shape()
input_shape = get_weight_shape_legacy(layer_attributes)
self._quantizers_input_shapes[primary_qid] = tuple(input_shape)
else:
primary_qid = NonWeightQuantizerId(primary_ip.target_node_name, primary_ip.input_port_id)
Expand Down
6 changes: 4 additions & 2 deletions nncf/torch/quantization/init_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import nncf
from nncf.common.graph.layer_attributes import WeightedLayerAttributes
from nncf.common.graph.utils import get_weight_shape_legacy
from nncf.common.graph.utils import get_target_dim_for_compression_legacy
from nncf.common.quantization.initialization.range import RangeInitCollectorParams
from nncf.common.quantization.initialization.range import RangeInitConfig
from nncf.common.quantization.initialization.range import RangeInitParams
Expand Down Expand Up @@ -226,8 +228,8 @@ def get_all_scale_shapes_with_params(
module_node = target_nncf_graph.get_node_by_name(qp.insertion_point.target_node_name)
layer_attributes = module_node.layer_attributes
assert isinstance(layer_attributes, WeightedLayerAttributes)
input_shape = layer_attributes.get_weight_shape()
channel_idx = layer_attributes.get_target_dim_for_compression()
input_shape = get_weight_shape_legacy(layer_attributes)
channel_idx = get_target_dim_for_compression_legacy(layer_attributes)
else:
input_shape = target_nncf_graph.get_input_shape_for_insertion_point(qp.insertion_point)
channel_idx = 1 # channel dim for activations
Expand Down
Loading