|
| 1 | +# Copyright (c) 2025 Intel Corporation |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +from enum import Enum |
| 13 | +from typing import Tuple |
| 14 | + |
| 15 | +from nncf.common.graph.graph import NNCFNode |
| 16 | +from nncf.onnx.graph.layer_attributes import ONNXLayerAttributes |
| 17 | +from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXConvolutionMetatype |
| 18 | +from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXDepthwiseConvolutionMetatype |
| 19 | +from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXGroupConvolutionMetatype |
| 20 | +from nncf.onnx.graph.metatypes.onnx_metatypes import ONNXOpMetatype |
| 21 | + |
| 22 | + |
| 23 | +class ONNXLayoutElem(Enum): |
| 24 | + """ |
| 25 | + Layout elements descriptor for convolutional and linear onnx layers: |
| 26 | + C_IN: Input channels dimension. |
| 27 | + C_OUT: Output channels dimension. |
| 28 | + SPATIAL: Spatial dimension. |
| 29 | + GROUPS: Groups dimension. |
| 30 | + """ |
| 31 | + |
| 32 | + C_IN = "channels_in" |
| 33 | + C_OUT = "channels_out" |
| 34 | + SPATIAL = "spatial" |
| 35 | + GROUPS = "groups" |
| 36 | + |
| 37 | + |
| 38 | +_CONV_BASE_CONST_LAYOUT = { |
| 39 | + ONNXConvolutionMetatype: (ONNXLayoutElem.C_OUT, ONNXLayoutElem.C_IN), |
| 40 | + ONNXDepthwiseConvolutionMetatype: (ONNXLayoutElem.GROUPS, ONNXLayoutElem.C_OUT, ONNXLayoutElem.C_IN), |
| 41 | + ONNXGroupConvolutionMetatype: (ONNXLayoutElem.GROUPS, ONNXLayoutElem.C_OUT, ONNXLayoutElem.C_IN), |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +def get_conv_weights_layout_from_node(node: NNCFNode) -> Tuple[ONNXLayoutElem]: |
| 46 | + """ |
| 47 | + Calculates weights layout for a target convolution node. |
| 48 | +
|
| 49 | + :param node: Target convolution node. |
| 50 | + :return: Target convolution Node weights layout. |
| 51 | + """ |
| 52 | + layer_attributes = node.layer_attributes |
| 53 | + port_id = _get_constant_port_id_from_layer_attributes(layer_attributes) |
| 54 | + return get_conv_weights_layout( |
| 55 | + ONNX_metatype=node.metatype, weights_shape=layer_attributes.constant_attributes[port_id]["shape"] |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def get_linear_weights_layout_from_node(node: NNCFNode) -> Tuple[ONNXLayoutElem]: |
| 60 | + """ |
| 61 | + Calculates weights layout for a target linear node. |
| 62 | +
|
| 63 | + :param node: Target linear node. |
| 64 | + :return: Target linear Node weight layout. |
| 65 | + """ |
| 66 | + layer_attributes = node.layer_attributes |
| 67 | + port_id = _get_constant_port_id_from_layer_attributes(layer_attributes) |
| 68 | + constant_layer_attrs = layer_attributes.constant_attributes[port_id] |
| 69 | + return get_linear_input_layout( |
| 70 | + input_shape=constant_layer_attrs["shape"], |
| 71 | + transpose=constant_layer_attrs["transpose"], |
| 72 | + port_id=port_id, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def get_linear_activations_layout_from_node( |
| 77 | + node: NNCFNode, port_id: int, input_shape: Tuple[int] |
| 78 | +) -> Tuple[ONNXLayoutElem]: |
| 79 | + """ |
| 80 | + Calculates activations layout for a target linear node. |
| 81 | +
|
| 82 | + :param node: Target linear node. |
| 83 | + :param port_id: Target input port ID. |
| 84 | + :param input_shape: Shape of the input. |
| 85 | + :return: Target linear Node weight layout. |
| 86 | + """ |
| 87 | + act_layer_attrs = node.layer_attributes.input_attributes |
| 88 | + return get_linear_input_layout( |
| 89 | + input_shape=input_shape, |
| 90 | + transpose=act_layer_attrs["transpose"], |
| 91 | + port_id=port_id, |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def get_conv_weights_layout(ONNX_metatype: ONNXOpMetatype, weights_shape: Tuple[int, ...]) -> Tuple[ONNXLayoutElem]: |
| 96 | + """ |
| 97 | + Calculates weights layout for a target convolution node. |
| 98 | +
|
| 99 | + :param ONNX_metatype: Target convolution node OpenVINO metatype. |
| 100 | + :param weights_shape: Shape of the target convolution node weight. |
| 101 | + :return: Target convolution node weights layout. |
| 102 | + """ |
| 103 | + base_layout = _CONV_BASE_CONST_LAYOUT[ONNX_metatype] |
| 104 | + kernel_size = weights_shape[len(base_layout) :] |
| 105 | + weights_layout = list(base_layout) + [ONNXLayoutElem.SPATIAL] * len(kernel_size) |
| 106 | + return tuple(weights_layout) |
| 107 | + |
| 108 | + |
| 109 | +def get_linear_input_layout(input_shape: Tuple[int, ...], transpose: bool, port_id: int) -> Tuple[ONNXLayoutElem]: |
| 110 | + """ |
| 111 | + Calculates input layout for a target linear node. |
| 112 | +
|
| 113 | + :param input_shape: Shape of the target linear node input. |
| 114 | + :param port_id: Port id of the target linear node input. |
| 115 | + :return: Target linear node input layout. |
| 116 | + """ |
| 117 | + input_layout = [ONNXLayoutElem.SPATIAL] * (len(input_shape) - 2) |
| 118 | + if len(input_shape) > 1: |
| 119 | + if (transpose and port_id == 0) or (not transpose and port_id == 1): |
| 120 | + input_layout += [ONNXLayoutElem.C_IN, ONNXLayoutElem.C_OUT] |
| 121 | + else: |
| 122 | + input_layout += [ONNXLayoutElem.C_OUT, ONNXLayoutElem.C_IN] |
| 123 | + else: |
| 124 | + input_layout += [ONNXLayoutElem.C_IN] |
| 125 | + return tuple(input_layout) |
| 126 | + |
| 127 | + |
| 128 | +def _get_constant_port_id_from_layer_attributes(layer_attributes: ONNXLayerAttributes) -> int: |
| 129 | + """ |
| 130 | + Returns constant ports id for convolutional and linear ops layer attributes. |
| 131 | +
|
| 132 | + :param layer_attributes: Target convolutional/linear layer op layer attributes. |
| 133 | + :return: Constant port id for the target convolutional/linear model. |
| 134 | + """ |
| 135 | + port_ids = list(layer_attributes.constant_attributes.keys()) |
| 136 | + assert len(port_ids) == 1 |
| 137 | + return port_ids[0] |
0 commit comments