Skip to content

Commit 7c2e1ad

Browse files
committed
[NNCF] Experimental torch backend: Replace (openvinotoolkit#3249)
Changes: Calls to following class methods in nncf/common/graph/layer_attributes.py were replaced with calls to their corresponding self-contained legacy functions in nncf/common/graph/utils.py: - layer_attributes.get_weight_shape -> get_weight_shape_legacy(layer_attributes: WeightedLayerAttributes) - layer_attributes.get_bias_shape -> get_bias_shape_legacy(layer_attributes: WeightedLayerAttributes) Locations in /nncf/experimental/torch/sparsity/movement/ folder - layers.py - structured_mask_handler.py Reason for changes: (openvinotoolkit#3249) Torch and Tensorflow backend-specific methods need to be removed from common layer attributes and all related calls need to be replaced by their corresponding legacy function calls
1 parent b37408f commit 7c2e1ad

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

nncf/experimental/torch/sparsity/movement/layers.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import nncf
2020
from nncf.common.graph import NNCFNode
21+
from nncf.common.graph.utils import get_weight_shape_legacy
22+
from nncf.common.graph.utils import get_bias_shape_legacy
2123
from nncf.experimental.torch.sparsity.movement.functions import binary_mask_by_threshold
2224
from nncf.torch.layer_utils import COMPRESSION_MODULES
2325
from nncf.torch.layer_utils import CompressionParameter
@@ -167,7 +169,7 @@ def __init__(
167169
self._importance_threshold = -math.inf
168170
self._importance_regularization_factor = 0.0
169171

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

187189
if self.prune_bias:
188-
bias_shape = target_module_node.layer_attributes.get_bias_shape()
190+
bias_shape = get_bias_shape_legacy(target_module_node.layer_attributes)
189191
self.bias_ctx = BinaryMask(bias_shape)
190192
bias_importance_shape = weight_importance_shape[0]
191193
self.bias_importance = CompressionParameter(

nncf/experimental/torch/sparsity/movement/structured_mask_handler.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
from nncf.common.graph.graph import NNCFNodeName
2020
from nncf.common.graph.layer_attributes import LinearLayerAttributes
21+
from nncf.common.graph.utils import get_weight_shape_legacy
22+
from nncf.common.graph.utils import get_bias_shape_legacy
2123
from nncf.common.logging import nncf_logger
2224
from nncf.experimental.common.pruning.nodes_grouping import get_pruning_groups
2325
from nncf.experimental.common.pruning.nodes_grouping import select_largest_groups
@@ -209,9 +211,9 @@ def gather_statistics_from_operand(self) -> StructuredMaskContextStatistics:
209211
"""
210212
node = self.sparsifier_operand.target_module_node
211213
assert isinstance(node.layer_attributes, tuple(EXPECTED_NODE_LAYER_ATTRS))
212-
weight_shape: Tuple[int, int] = tuple(node.layer_attributes.get_weight_shape())
214+
weight_shape: Tuple[int, int] = tuple(get_weight_shape_legacy(node.layer_attributes))
213215
bias_shape: Tuple[int] = (
214-
(node.layer_attributes.get_bias_shape(),) if self.sparsifier_operand.prune_bias else (0,)
216+
(get_bias_shape_legacy(node.layer_attributes),) if self.sparsifier_operand.prune_bias else (0,)
215217
)
216218

217219
pruned_weight_shape = list(weight_shape)

0 commit comments

Comments
 (0)