|
14 | 14 |
|
15 | 15 | from nncf.common.graph import NNCFGraph
|
16 | 16 | from nncf.common.graph import NNCFNode
|
| 17 | +from nncf.common.graph.layer_attributes import ConvolutionLayerAttributes |
| 18 | +from nncf.common.graph.layer_attributes import GenericWeightedLayerAttributes |
| 19 | +from nncf.common.graph.layer_attributes import GroupNormLayerAttributes |
| 20 | +from nncf.common.graph.layer_attributes import LinearLayerAttributes |
| 21 | +from nncf.common.graph.layer_attributes import WeightedLayerAttributes |
17 | 22 | from nncf.common.graph.operator_metatypes import OperatorMetatype
|
18 | 23 | from nncf.common.logging import nncf_logger
|
19 | 24 | from nncf.common.pruning.utils import traverse_function
|
@@ -132,3 +137,78 @@ def get_reduction_axes(
|
132 | 137 | for channel_axis in sorted(channel_axes, reverse=True):
|
133 | 138 | del reduction_axes[channel_axis]
|
134 | 139 | return tuple(reduction_axes)
|
| 140 | + |
| 141 | + |
| 142 | +def get_weight_shape_legacy(layer_attributes: WeightedLayerAttributes) -> List[int]: |
| 143 | + """ |
| 144 | + Returns hard-coded weights shape layout for the given layer attributes. |
| 145 | + Applicable only for eager PyTorch and Tensorflow models. |
| 146 | +
|
| 147 | + :param layer_attributes: Layer attributes of a NNCFNode. |
| 148 | + :return: Weights shape layout. |
| 149 | + """ |
| 150 | + if isinstance(layer_attributes, LinearLayerAttributes): |
| 151 | + return [layer_attributes.out_features, layer_attributes.in_features] |
| 152 | + |
| 153 | + if isinstance(layer_attributes, ConvolutionLayerAttributes): |
| 154 | + if not layer_attributes.transpose: |
| 155 | + return [ |
| 156 | + layer_attributes.out_channels, |
| 157 | + layer_attributes.in_channels // layer_attributes.groups, |
| 158 | + *layer_attributes.kernel_size, |
| 159 | + ] |
| 160 | + return [ |
| 161 | + layer_attributes.in_channels, |
| 162 | + layer_attributes.out_channels // layer_attributes.groups, |
| 163 | + *layer_attributes.kernel_size, |
| 164 | + ] |
| 165 | + |
| 166 | + if isinstance(layer_attributes, GroupNormLayerAttributes): |
| 167 | + return [layer_attributes.num_channels] |
| 168 | + |
| 169 | + assert isinstance(layer_attributes, GenericWeightedLayerAttributes) |
| 170 | + return layer_attributes.weight_shape |
| 171 | + |
| 172 | + |
| 173 | +def get_target_dim_for_compression_legacy(layer_attributes: WeightedLayerAttributes) -> int: |
| 174 | + """ |
| 175 | + Returns hard-coded target dim for compression for the given layer attributes. |
| 176 | + Applicable only for eager PyTorch and Tensorflow models. |
| 177 | +
|
| 178 | + :param layer_attributes: Layer attributes of a NNCFNode. |
| 179 | + :return: Target dim for compression. |
| 180 | + """ |
| 181 | + if isinstance(layer_attributes, ConvolutionLayerAttributes): |
| 182 | + # Always quantize per each "out" channel |
| 183 | + return 1 if layer_attributes.transpose else 0 |
| 184 | + |
| 185 | + else: |
| 186 | + assert isinstance( |
| 187 | + layer_attributes, (GenericWeightedLayerAttributes, LinearLayerAttributes, GroupNormLayerAttributes) |
| 188 | + ) |
| 189 | + return 0 |
| 190 | + |
| 191 | + |
| 192 | +def get_bias_shape_legacy(layer_attributes: WeightedLayerAttributes) -> int: |
| 193 | + """ |
| 194 | + Returns hard-coded bias shape for the given linear layer attributes. |
| 195 | + Applicable only for eager PyTorch and Tensorflow models. |
| 196 | +
|
| 197 | + :param layer_attributes: Linear layer attributes of a NNCFNode. |
| 198 | + :return: Correspondent bias shape. |
| 199 | + """ |
| 200 | + assert isinstance(layer_attributes, LinearLayerAttributes) |
| 201 | + return layer_attributes.out_features if layer_attributes.with_bias is True else 0 |
| 202 | + |
| 203 | + |
| 204 | +def get_num_filters_legacy(layer_attributes: WeightedLayerAttributes) -> int: |
| 205 | + """ |
| 206 | + Returns hard-coded number of filters for the given layer attribues. |
| 207 | + Applicable only for eager PyTorch and Tensorflow models. |
| 208 | +
|
| 209 | + :param layer_attributes: Layer attributes of a NNCFNode. |
| 210 | + :return: Correspondent number of filters. |
| 211 | + """ |
| 212 | + assert isinstance(layer_attributes, WeightedLayerAttributes) |
| 213 | + weight_shape = get_weight_shape_legacy(layer_attributes) |
| 214 | + return weight_shape[get_target_dim_for_compression_legacy(layer_attributes)] |
0 commit comments