Create a link with given variable from upstream_op to downstream_op variable will be appended to upstream_op's output and downstream_op's input given variable must have empty source_op or its source_op == upstream_op. Sometime you may want to link a single upstream_o
(self, A: Operation, B: Operation, variable: Variable = None)
| 507 | down_op.inputs[down_op.inputs.index(up_var)] = link_var |
| 508 | |
| 509 | def create_link_with_op(self, A: Operation, B: Operation, variable: Variable = None): |
| 510 | """Create a link with given variable from upstream_op to downstream_op |
| 511 | variable will be appended to upstream_op's output and downstream_op's |
| 512 | input given variable must have empty source_op or its source_op == |
| 513 | upstream_op. |
| 514 | |
| 515 | Sometime you may want to link a single upstream_op to many downstream_ops with a same variable, |
| 516 | you are supposed to invoke this function for each downstream_op then. |
| 517 | |
| 518 | You can set upstream_op = None if your variable is a parameter variable. |
| 519 | |
| 520 | Example: |
| 521 | create_link_with_op(var1, op1, op2) |
| 522 | create_link_with_op(var1, op1, op3) |
| 523 | |
| 524 | Will makes: |
| 525 | --> op2 |
| 526 | op1 --| |
| 527 | --> op3 |
| 528 | |
| 529 | Args: |
| 530 | link_variable (Variable): _description_ |
| 531 | upstream_op (Operation): _description_ |
| 532 | downstream_op (Operation): _description_ |
| 533 | """ |
| 534 | if variable is None: |
| 535 | variable = self.create_variable() |
| 536 | if variable.name not in self.variables: |
| 537 | raise KeyError(f'Can not find your variable {variable.name} in current graph.') |
| 538 | if A is not None and A.name not in self.operations: |
| 539 | raise KeyError(f'Can not find your operation {A.name} in current graph.') |
| 540 | if B is not None and B.name not in self.operations: |
| 541 | raise KeyError(f'Can not find your operation {B.name} in current graph.') |
| 542 | |
| 543 | if variable.source_op is None: variable.source_op = A |
| 544 | if variable.source_op != A: |
| 545 | raise PermissionError(f'Can not create link with variable {variable}, ' |
| 546 | f'cause its source operations != {A}') |
| 547 | |
| 548 | # For complex graph, following logic might have some error. |
| 549 | if A is not None and variable not in A.outputs: |
| 550 | A.outputs.append(variable) |
| 551 | if B is None: return |
| 552 | if B is not None and variable not in B.inputs: |
| 553 | variable.dest_ops.append(B) |
| 554 | B.inputs.append(variable) |
| 555 | else: |
| 556 | variable.dest_ops.append(B) |
| 557 | B.inputs.append(variable) |
| 558 | ppq_warning(f'You are trying to link variable with operation, ' |
| 559 | f'however Variable {variable.name} has already linked with downstream op {B.name}') |
| 560 | |
| 561 | def create_link_with_var(self, A: Variable, B: Variable): |
| 562 | """connect upstream_variable.source_op with |
no test coverage detected