could either build graph from scratch or append new nodes to a existing graph, if build form scratch, graph should be None, otherwise we use the given graph and append nodes and variables.
(initializer: Dict[str, dict], nodes: List[dict], inputs: List[str]=[], outputs: List[str]=[], graph: BaseGraph=None)
| 18 | |
| 19 | |
| 20 | def build_temp_graph(initializer: Dict[str, dict], nodes: List[dict], inputs: List[str]=[], outputs: List[str]=[], graph: BaseGraph=None) -> BaseGraph: |
| 21 | """could either build graph from scratch or append new nodes to a existing |
| 22 | graph, if build form scratch, graph should be None, otherwise we use the |
| 23 | given graph and append nodes and variables.""" |
| 24 | from_scratch = False |
| 25 | if graph is None: |
| 26 | graph = BaseGraph(name='Infer', built_from=NetworkFramework.CAFFE) |
| 27 | from_scratch = True |
| 28 | |
| 29 | op_inputs_dict, op_outputs_dict = {}, {} |
| 30 | for node in nodes: |
| 31 | if node['name'] in graph.operations: |
| 32 | raise KeyError(f"Duplicated operation {node['name']} was found.") |
| 33 | graph.operations[node['name']] = Operation(name=node['name'], op_type=node['op_type'], |
| 34 | attributes=node['attribute'].copy(), opset=Opset(domain=CAFFE_DOMAIN, version=1)) |
| 35 | op_inputs_dict[node['name']] = [_ for _ in node['inputs']] |
| 36 | op_outputs_dict[node['name']] = [_ for _ in node['outputs']] |
| 37 | |
| 38 | var_list = [] |
| 39 | for op_name, input_vars in op_inputs_dict.items(): |
| 40 | var_list.extend(input_vars) |
| 41 | for op_name, output_vars in op_outputs_dict.items(): |
| 42 | var_list.extend(output_vars) |
| 43 | |
| 44 | # create all variable at once. |
| 45 | for var_name in set(var_list): |
| 46 | if var_name in graph.variables: |
| 47 | continue |
| 48 | graph.variables[var_name] = Variable(name=var_name) |
| 49 | |
| 50 | # build graph's input, output variables. |
| 51 | # we only set graph inputs and outputs when build from scratch, i.e., when we are sure about graph inputs and outputs, |
| 52 | # otherwise the graph inputs and outputs should manually set after the whole appending process from outside |
| 53 | if from_scratch: |
| 54 | try: |
| 55 | for var_name in inputs: |
| 56 | if var_name not in graph.variables: continue |
| 57 | graph.inputs[var_name] = graph.variables[var_name] |
| 58 | for var_name in outputs: |
| 59 | graph.outputs[var_name] = graph.variables[var_name] |
| 60 | except KeyError as e: |
| 61 | raise KeyError( |
| 62 | 'seems you got an input/output variable that is not linked to any operation.') |
| 63 | |
| 64 | # build operation inputs, outputs variables. |
| 65 | for op_name in op_inputs_dict: |
| 66 | for var_name in op_inputs_dict[op_name]: |
| 67 | var = graph.variables[var_name] |
| 68 | var.dest_ops.append(graph.operations[op_name]) |
| 69 | graph.operations[op_name].inputs.append(graph.variables[var_name]) |
| 70 | for var_name in op_outputs_dict[op_name]: |
| 71 | var = graph.variables[var_name] |
| 72 | var.source_op = graph.operations[op_name] |
| 73 | graph.operations[op_name].outputs.append(graph.variables[var_name]) |
| 74 | |
| 75 | # initialize variable |
| 76 | for var_name in initializer: |
| 77 | if var_name in graph.variables: |
no test coverage detected