|
| 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 | +from collections import deque |
| 12 | +from typing import Dict, List, Tuple |
| 13 | + |
| 14 | +import openvino.runtime as ov |
| 15 | +from openvino.runtime import opset13 as opset |
| 16 | +from openvino.runtime.utils.node_factory import NodeFactory |
| 17 | + |
| 18 | +from nncf.openvino.graph.model_transformer import OVModelTransformer |
| 19 | +from nncf.openvino.graph.node_utils import get_parameter_node_name |
| 20 | +from nncf.openvino.graph.node_utils import get_result_node_name |
| 21 | + |
| 22 | + |
| 23 | +class OVModelBuilder: |
| 24 | + """ |
| 25 | + The purpose of the ModelBuilder is to build a new OpenVINO model from input and output points. |
| 26 | + This Builder was created to reduce the number of model cloning that is required for ModelTransformer to work. |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + self._node_factory = NodeFactory() |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def _create_parameter(node_name: str, node_input: ov.Input) -> ov.Node: |
| 34 | + """ |
| 35 | + A method that contains steps to create a Parameter for a new model using a specific template. |
| 36 | + """ |
| 37 | + port_id = node_input.get_index() |
| 38 | + parameter_name = get_parameter_node_name(node_name, port_id) |
| 39 | + return opset.parameter( |
| 40 | + shape=node_input.get_partial_shape(), |
| 41 | + dtype=node_input.get_element_type(), |
| 42 | + name=parameter_name, |
| 43 | + ) |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def _create_result(node_name: str, node_output: ov.Input) -> ov.Node: |
| 47 | + """ |
| 48 | + A method that contains steps to create a Result for a new model using a specific template. |
| 49 | + """ |
| 50 | + port_id = node_output.get_index() |
| 51 | + result_name = get_result_node_name(node_name, port_id=port_id) |
| 52 | + result = opset.result(node_output, name=result_name) |
| 53 | + result.get_output_tensor(0).set_names({result_name}) |
| 54 | + return result |
| 55 | + |
| 56 | + def _collect_graph_nodes( |
| 57 | + self, |
| 58 | + input_ids: List[Tuple[str, int]], |
| 59 | + output_ids: List[Tuple[str, int]], |
| 60 | + node_mapping: Dict[str, ov.Node], |
| 61 | + ) -> List[ov.Node]: |
| 62 | + """ |
| 63 | + A method for aggregating layers to be further cloned. |
| 64 | + Aggregation is designed in such a way that layers are listed from right to left, |
| 65 | + as they pass from bottom to top. This is done in order to find all constants in the model and |
| 66 | + to start graph creation from them (as well as Parameter layers), because |
| 67 | + OpenVINO graph is created from top-down and cannot be created otherwise. |
| 68 | +
|
| 69 | + Legend: w - weigths, c - convert, il/ih - input low/high, ol/oh - output low/high |
| 70 | + (w) |
| 71 | + | |
| 72 | + (c) (il) (ih) (ol) (oh) |
| 73 | + \ | | / / |
| 74 | + (fake quantize) (parameter) |
| 75 | + \ / |
| 76 | + (convolution) |
| 77 | + | |
| 78 | + (result) |
| 79 | + Based on the above graph, the return value would look like this: |
| 80 | + [convolution, parameter, fake quantize, oh, ol, ih, il, c, w] |
| 81 | +
|
| 82 | + :param input_ids: List of the points in the special format - (node_name, port_id). |
| 83 | + This helps to point to the precise part of the model that may be used to define the subgraph inputs. |
| 84 | + :param output_ids: List of the points in the special format - (node_name, port_id). |
| 85 | + This helps to point to the precise part of the model that may be used to define the subgraph outputs. |
| 86 | + :param node_mapping: Original nodes mapping. |
| 87 | + :return: List of the ov.Nodes to clone. |
| 88 | + """ |
| 89 | + # Creating a list as a deque for FIFO layer acquisition and retrieval |
| 90 | + lookup_nodes = deque(node_mapping[n] for n, _ in output_ids) |
| 91 | + graph_nodes = [] |
| 92 | + |
| 93 | + while lookup_nodes: |
| 94 | + lookup_node = lookup_nodes.popleft() |
| 95 | + lookup_name = lookup_node.get_friendly_name() |
| 96 | + node_inputs = lookup_node.inputs() |
| 97 | + graph_nodes.append(lookup_node) |
| 98 | + # Reversing to lookup nodes from right to left |
| 99 | + for node_input in reversed(node_inputs): |
| 100 | + port_id = node_input.get_index() |
| 101 | + if (lookup_name, port_id) in input_ids: |
| 102 | + # We create Parameters here to avoid double creation in the future since it is not an original node, |
| 103 | + # but we need to have it as input for next node. |
| 104 | + parameter = self._create_parameter(lookup_name, node_input) |
| 105 | + lookup_nodes.append(parameter) |
| 106 | + continue |
| 107 | + parent_node = node_input.get_source_output().get_node() |
| 108 | + lookup_nodes.append(parent_node) |
| 109 | + |
| 110 | + return graph_nodes |
| 111 | + |
| 112 | + def build( |
| 113 | + self, |
| 114 | + input_ids: List[Tuple[str, int]], |
| 115 | + output_ids: List[Tuple[str, int]], |
| 116 | + node_mapping: Dict[str, ov.Node], |
| 117 | + ) -> ov.Model: |
| 118 | + """ |
| 119 | + The basic method of the algorithm. This method uses an aggregated list of layers to be recreated. |
| 120 | + Let us take a graph of this kind as an example: |
| 121 | +
|
| 122 | + Legend: w - weigths, c - convert, il/ih - input low/high, ol/oh - output low/high |
| 123 | + (w) |
| 124 | + | |
| 125 | + (c) (il) (ih) (ol) (oh) |
| 126 | + \ | | / / |
| 127 | + (fake quantize) (parameter) |
| 128 | + \ / |
| 129 | + (convolution) |
| 130 | + | |
| 131 | + (result) |
| 132 | +
|
| 133 | + The externally collected list of layers will look like this: |
| 134 | + [convolution, parameter, fake quantize, oh, ol, ih, il, c, w] |
| 135 | +
|
| 136 | + Next, this list will be circled from right to left. At the same time, the list of already created layers |
| 137 | + will be filled from left to right, which will be used in the traversal step also, from left to right, |
| 138 | + in order to keep the order of the original layer inputs. |
| 139 | + For example: |
| 140 | +
|
| 141 | + graph_nodes = [convolution, parameter, fake quantize, oh, ol, ih, il, c, w] |
| 142 | + clone_nodes = [] |
| 143 | +
|
| 144 | + *creating w - weight node.* |
| 145 | + graph_nodes = [convolution, parameter, fake quantize, oh, ol, ih, il, c] |
| 146 | + clone_nodes = [w] |
| 147 | +
|
| 148 | + *creating c - convert node. |
| 149 | + Based on the .inputs() output, we'll use the already created w-weight node to fill in the convert input. |
| 150 | + As the result, weight node would be removed from the clone_nodes list and convert node would be placed here.* |
| 151 | + graph_nodes = [convolution, parameter, fake quantize, oh, ol, ih, il] |
| 152 | + clone_nodes = [c] |
| 153 | +
|
| 154 | + *creating il/ih - input low/high, ol/oh - output low/high nodes. |
| 155 | + Since these nodes are constants and do not require any nodes as inputs, cloned nodes will not be used.* |
| 156 | + graph_nodes = [convolution, parameter, fake quantize, oh, ol, ih, il] |
| 157 | + clone_nodes = [c, il, ih, ol, oh] |
| 158 | +
|
| 159 | + *creating fake quantize node. |
| 160 | + This node requires to have input values in a specific order. |
| 161 | + All previous nodes will be connected/used for fake quantize, from left to right.* |
| 162 | + graph_nodes = [convolution, parameter] |
| 163 | + clone_nodes = [f] |
| 164 | +
|
| 165 | + *creating parameter node. |
| 166 | + In this step, the list of parameters will also be filled out with the new node.* |
| 167 | + graph_nodes = [convolution] |
| 168 | + clone_nodes = [f, parameter] |
| 169 | +
|
| 170 | + *creating convolution node. |
| 171 | + This node also requires to have inputs in a specific order. |
| 172 | + All previous nodes will be connected/used for convolution, from left to right. Also, |
| 173 | + the outputs verification step will show here that one of the convolution outputs is in the output_ids list. |
| 174 | + This means that the Result node would be created and placed into the results list.* |
| 175 | + graph_nodes = [] |
| 176 | + clone_nodes = [convolution] |
| 177 | +
|
| 178 | + The last step is to create a subgraph model based on the parameters & results lists. |
| 179 | +
|
| 180 | + :param input_ids: List of the points in the special format - (node_name, port_id). |
| 181 | + This helps to point to the precise part of the model that may be used to define the subgraph inputs. |
| 182 | + :param output_ids: List of the points in the special format - (node_name, port_id). |
| 183 | + This helps to point to the precise part of the model that may be used to define the subgraph outputs. |
| 184 | + :param node_mapping: Original nodes mapping. |
| 185 | + :return: Builded ov.Model based on parameters. |
| 186 | + """ |
| 187 | + |
| 188 | + parameters, results = [], [] |
| 189 | + clone_nodes = deque() |
| 190 | + |
| 191 | + # Collecting nodes that declares the graph. |
| 192 | + graph_nodes = self._collect_graph_nodes(input_ids, output_ids, node_mapping) |
| 193 | + |
| 194 | + while graph_nodes: |
| 195 | + graph_node = graph_nodes.pop() |
| 196 | + node_type = graph_node.get_type_name() |
| 197 | + node_name = graph_node.get_friendly_name() |
| 198 | + |
| 199 | + # To create the new OpenVINO nodes, we need to provide all possible layer attributes. |
| 200 | + attrs = graph_node.get_attributes() |
| 201 | + attrs["name"] = node_name |
| 202 | + |
| 203 | + if node_type == "Constant": |
| 204 | + # Constants creation is apart due to specific behavior. |
| 205 | + clone_node = OVModelTransformer._create_constant( |
| 206 | + graph_node.get_data(), dtype=graph_node.get_element_type(), name=attrs["name"] |
| 207 | + ) |
| 208 | + elif node_type == "Parameter": |
| 209 | + # We've created Parameter nodes on the previous step. |
| 210 | + clone_node = graph_node |
| 211 | + parameters.append(clone_node) |
| 212 | + else: |
| 213 | + # We have to have args as the inputs since all of them are nodes and are required to be as input. |
| 214 | + args = [clone_nodes.popleft() for _ in graph_node.inputs()] |
| 215 | + |
| 216 | + clone_node = self._node_factory.create(node_type, args, attrs) |
| 217 | + |
| 218 | + for node_output in clone_node.outputs(): |
| 219 | + port_id = node_output.get_index() |
| 220 | + if (node_name, port_id) in output_ids: |
| 221 | + result = self._create_result(node_name, node_output) |
| 222 | + results.append(result) |
| 223 | + |
| 224 | + clone_nodes.append(clone_node) |
| 225 | + |
| 226 | + return ov.Model(results, parameters) |
0 commit comments