Remove operation from graph, this function will unlink removing operation from current graph, pop it from graph.operations, and remove it from all its input and output variables. Parameters of this removing operations will be removed from graph by this function, without warn
(self, removing_op: Operation,
keep_coherence: bool = False,
remove_unlinked_variable: bool = False)
| 588 | return self |
| 589 | |
| 590 | def remove_operation(self, removing_op: Operation, |
| 591 | keep_coherence: bool = False, |
| 592 | remove_unlinked_variable: bool = False): |
| 593 | """Remove operation from graph, this function will unlink removing |
| 594 | operation from current graph, pop it from graph.operations, and remove |
| 595 | it from all its input and output variables. |
| 596 | |
| 597 | Parameters of this removing operations will be removed from graph by this function, without warning. |
| 598 | |
| 599 | Args: |
| 600 | removing_op (Operation): [description] |
| 601 | |
| 602 | keep_coherence (bool): if keep_coherence = True, |
| 603 | PPQ will link downstream operations of removing op to the upstream operation. |
| 604 | if there is more than 1 input and output variable, ppq will link input[0] with output[0] |
| 605 | """ |
| 606 | if removing_op.name not in self.operations: |
| 607 | raise KeyError(f'Can not remove operation {removing_op.name}, operation not found.') |
| 608 | |
| 609 | # removing all parameters first. |
| 610 | for parameter in removing_op.inputs.copy(): |
| 611 | if keep_coherence and removing_op.type in {'Constant', 'Identity'}: break |
| 612 | if parameter.is_parameter: |
| 613 | |
| 614 | parameter.dest_ops.clear() |
| 615 | parameter.value = None # clear memory. |
| 616 | removing_op.inputs.remove(parameter) |
| 617 | |
| 618 | self.variables.pop(parameter.name) |
| 619 | |
| 620 | related_vars = [var for var in removing_op.inputs + removing_op.outputs] |
| 621 | input_var, output_var = ( |
| 622 | removing_op.inputs[0] if removing_op.num_of_input >= 1 else None, |
| 623 | removing_op.outputs[0] if removing_op.num_of_output >= 1 else None) |
| 624 | |
| 625 | # remove operation from its output variables |
| 626 | for output_var in removing_op.outputs: |
| 627 | output_var.source_op = None |
| 628 | removing_op.outputs.clear() |
| 629 | |
| 630 | # remove operation from its input variables |
| 631 | for input_var in removing_op.inputs: |
| 632 | if removing_op in input_var.dest_ops: |
| 633 | input_var.dest_ops.remove(removing_op) |
| 634 | removing_op.inputs.clear() |
| 635 | |
| 636 | if (input_var is not None and |
| 637 | output_var is not None and |
| 638 | keep_coherence): |
| 639 | |
| 640 | removing_var = output_var |
| 641 | dest_ops = removing_var.dest_ops |
| 642 | is_graph_output = removing_var.name in self.outputs |
| 643 | |
| 644 | for op in dest_ops: |
| 645 | op.inputs[op.inputs.index(removing_var)] = input_var |
| 646 | input_var.dest_ops.append(op) |
| 647 | removing_var.dest_ops.clear() |
no test coverage detected