def preprocess(graph: 'graphs.Graph', isMain: 'bool'): # replace inputs if not isMain: input_values = graph.input_values.copy() copied_input_values = [ functions.generate_copied_value(v) for v in input_values ] old2new = {} for i in range(len(input_values)): copied_input_values[i].name = input_values[i].name + '_in' old2new[input_values[i]] = copied_input_values[i] for node in graph.nodes: for i in range(len(input_values)): node.replace_inputs(input_values[i], copied_input_values[i]) graph.input_values = copied_input_values for i in range(len(graph.output_values)): if graph.output_values[i] in old2new.keys(): graph.output_values[i] = old2new[graph.output_values[i]] replacing = {} for value in graph.output_values: if value in graph.input_values: copied_value = functions.generate_copied_value(value) copied_value.name = value.name + '_cp' replacing[value] = copied_value node = nodes.NodeCopy(value) node.set_outputs([copied_value]) graph.add_node(node) for i in range(len(graph.output_values)): if graph.output_values[i] in replacing.keys(): graph.output_values[i] = replacing[graph.output_values[i]] for node in graph.nodes: for subgraph in node.subgraphs: preprocess(subgraph, False)
def preprocess(graph: 'graphs.Graph', isMain: 'bool'): replacing = {} for value in graph.output_values: if value in graph.input_values: copied_value = functions.generate_copied_value(value) copied_value.name = value.name + '_cp' replacing[value] = copied_value node = nodes.NodeCopy(value) node.set_outputs([copied_value]) graph.add_node(node) for i in range(len(graph.output_values)): if graph.output_values[i] in replacing.keys(): graph.output_values[i] = replacing[graph.output_values[i]] # fix duplicates (if same output value exsits, error is caused.) output_values = graph.output_values.copy() duplicates = {} for i in range(len(output_values)): if output_values[i] in duplicates.keys(): copied_value = functions.generate_copied_value(output_values[i]) node = nodes.NodeCopy(output_values[i]) node.set_outputs([copied_value]) graph.add_node(node) copied_value.name = output_values[i].name + '_cp_out_' + str( duplicates[output_values[i]]) duplicates[output_values[i]] += 1 output_values[i] = copied_value else: duplicates[output_values[i]] = 0 graph.output_values = output_values for node in graph.nodes: for subgraph in node.subgraphs: preprocess(subgraph, False)
def preprocess(graph: 'graphs.Graph'): replacing = {} for value in graph.output_values: if value in graph.input_values: copied_value = functions.generate_copied_value(value) replacing[value] = copied_value node = nodes.NodeCopy(value) node.set_outputs([copied_value]) graph.add_node(node) for i in range(len(graph.output_values)): if graph.output_values[i] in replacing.keys(): graph.output_values[i] = replacing[graph.output_values[i]] for node in graph.nodes: for subgraph in node.subgraphs: preprocess(subgraph)